4throck
Enthusiast !
- Joined
- Jun 19, 2008
- Messages
- 3,502
- Reaction score
- 996
- Points
- 153
- Location
- Lisbon
- Website
- orbiterspaceport.blogspot.com
Another simple example. This time, let's automate the landing gear.
We want it to retract above 300 meters and to extend below that.
The example is very simple with human readable code, and it will only work if you start landed.
The goal is to demonstrate simple logic and how to send key presses to a vessel.
The scenario is based on \Scenarios\2016 Edition\KSC Update.scn
The Lua code (save as \Script\lua_test2):
You can change the code to send other key-presses depending on other situations and different vessels.
For example, you can have a spacecraft.dll vessel open a parachute bellow a certain altitude.
On that case beware that you need some way to determine if the vessel is being launched or reentering
We want it to retract above 300 meters and to extend below that.
The example is very simple with human readable code, and it will only work if you start landed.
The goal is to demonstrate simple logic and how to send key presses to a vessel.
The scenario is based on \Scenarios\2016 Edition\KSC Update.scn
Code:
BEGIN_HYPERDESC
Lua test2 - landing gear
END_HYPERDESC
BEGIN_ENVIRONMENT
System Sol
Date MJD 54723.6694482476
[B] Script lua_test2[/B]
END_ENVIRONMENT
BEGIN_FOCUS
Ship GL-01S
END_FOCUS
BEGIN_CAMERA
TARGET GL-01S
MODE Extern
POS 799.63 36.01 -15.36
TRACKMODE TargetRelative
FOV 40.00
END_CAMERA
BEGIN_HUD
TYPE Surface
END_HUD
BEGIN_MFD Left
TYPE HSI
NAV 0 1
OBS 5.34 5.76
END_MFD
BEGIN_MFD Right
TYPE Map
REF Earth
BTARGET Habana
OTARGET ISS
TRACK ON
END_MFD
BEGIN_VC
END_VC
BEGIN_SHIPS
ISS:ProjectAlpha_ISS
STATUS Orbiting Earth
RPOS -188474.34 -6472054.19 1863160.27
RVEL -7556.889 587.260 1298.273
AROT 110.01 -10.03 80.00
PRPLEVEL 0:1.000
IDS 0:588 10 1:586 10 2:584 10 3:582 10 4:580 10
NAVFREQ 0 0
XPDR 466
END
Mir:Mir
STATUS Orbiting Earth
RPOS -3039099.46 362684.28 5930014.87
RVEL -6881.230 -214.626 -3509.259
AROT 0.02 -44.96 89.99
IDS 0:540 100 1:542 100 2:544 100
XPDR 482
END
Luna-OB1:Wheel
STATUS Orbiting Moon
RPOS -1688662.61 1469540.71 -56.18
RVEL -971.369 -1116.517 -0.203
AROT 0.00 0.00 38.08
VROT -0.00 0.00 10.00
IDS 0:560 100 1:564 100
XPDR 494
END
GL-01S:DG-S
STATUS Landed Earth
POS -80.6825940 28.5969240
HEADING 330.01
RCSMODE 0
PRPLEVEL 0:0.995 1:1.000 2:1.000
NAVFREQ 94 524 84 114
XPDR 0
GEAR 1 1.0000
PSNGR 2 3 4
TANKCONFIG 1
END
SH-03:ShuttleA
STATUS Landed Earth
BASE Habana:4
POS -82.3982414 23.0005396
HEADING 70.00
PRPLEVEL 0:1.000 1:1.000
NAVFREQ 0 0
XPDR 0
PODANGLE 0.0000 0.0000
DOCKSTATE 0 0.0000
AIRLOCK 0 0.0000
GEAR 0 0.0000
PAYLOAD MASS 0.0 0
END
PB-01:ShuttlePB
STATUS Landed Earth
BASE Habana:1
POS -82.4000000 22.9994604
HEADING 22.00
PRPLEVEL 0:1.000
NAVFREQ 0 0
END
GL-02:DeltaGlider
STATUS Landed Mars
BASE Olympus:3
POS -135.4300000 12.7366196
HEADING 0.00
PRPLEVEL 0:1.000 1:1.000
NAVFREQ 0 0 0 0
XPDR 0
GEAR 1 1.0000
END
SH-01:ShuttleA
STATUS Landed Moon
BASE Brighton Beach:1
POS -33.4375000 41.1184067
HEADING 0.00
PRPLEVEL 0:1.000 1:1.000
NAVFREQ 0 0
XPDR 0
PODANGLE 0.0000 0.0000
DOCKSTATE 0 0.0000
AIRLOCK 0 0.0000
GEAR 0 0.0000
PAYLOAD MASS 6.0 0
END
END_SHIPS
The Lua code (save as \Script\lua_test2):
Code:
-- get our vessels, names must match scenario
v1 = vessel.get_interface('GL-01S')
-- we say the gear is down.
-- it will only work if we start landed on the scenario
gear = 1
data= " Gear down"
-- dumb infinite loop
i = 1
while i <= 5 do
-- get relative altitude
alt = v1:get_altitude(1)
-- simple logic, if we are high and gear is still down, send "G" key to retract
if alt>300 and gear==1 then
gear=0
res = v1:send_bufferedkey(OAPI_KEY.G)
-- setup output text
data= " Gear up"
end
-- revere logic. Notice that we must use a status variable and altitute, otherwise the condition would be trigered all the time.
if alt<300 and gear==0 then
gear=1
res = v1:send_bufferedkey(OAPI_KEY.G)
-- setup output text
data= " Gear down"
end
-- write to screen
oapi.dbg_out(alt .. data)
-- refresh update
proc.wait_sysdt(0.1)
end
You can change the code to send other key-presses depending on other situations and different vessels.
For example, you can have a spacecraft.dll vessel open a parachute bellow a certain altitude.
On that case beware that you need some way to determine if the vessel is being launched or reentering
Last edited: