Beginning with Lua 2

4throck

Enthusiast !
Joined
Jun 19, 2008
Messages
3,502
Reaction score
985
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

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:

martins

Orbiter Founder
Orbiter Founder
Joined
Mar 31, 2008
Messages
2,407
Reaction score
341
Points
83
Website
orbit.medphys.ucl.ac.uk
Nice idea to provide simple scripts for beginners.

One little point about the automatic gear up/down script: I would introduce a hysteresis to avoid continuous up/down events for a vessel flying level at 300m:

Code:
if alt > 350 and gear == 1 then
  -- pull up gear
end

if alt < 250 and gear == 0 then
  -- drop gear
end
 

4throck

Enthusiast !
Joined
Jun 19, 2008
Messages
3,502
Reaction score
985
Points
153
Location
Lisbon
Website
orbiterspaceport.blogspot.com
Thanks! I hope more people will get started with scripting, it's really simple and fast to get good results.


To make it clearer to some, Martin's example creates a region between 250 to 350 meters where no action is taken. Otherwise, as you fly a little higher or lower than 300m, the gear will keep receiving many up / down commands each second...

Another option is to store time, and only allow new commands after n seconds have passed ;) . But that's for another example !
 

N_Molson

Addon Developer
Addon Developer
Donator
Joined
Mar 5, 2010
Messages
8,860
Reaction score
2,593
Points
203
Location
Toulouse
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

I guess you could do that by tracking vertical speed. A capsule-type vessel that goes downwards during the launch phase has anyways a very serious problem, and is in emergency abort mode :)
 

Xyon

Puts the Fun in Dysfunctional
Administrator
Moderator
Addon Developer
Webmaster
GFX Staff
Donator
Beta Tester
Joined
Aug 9, 2009
Messages
6,870
Reaction score
668
Points
203
Location
10.0.0.1
Website
www.orbiter-radio.co.uk
Preferred Pronouns
she/her
I guess you could do that by tracking vertical speed. A capsule-type vessel that goes downwards during the launch phase has anyways a very serious problem, and is in emergency abort mode :)

Is that a state tracking thing? You could probably accomplish that by counting mission time (time since "wheels up" rather than sim time spent waiting for the window) or a script flag that's changed when the vessel enters a new flight phase (state = LAUNCHING, state = CRUISE, state = CRASHING, etc etc)
 

N_Molson

Addon Developer
Addon Developer
Donator
Joined
Mar 5, 2010
Messages
8,860
Reaction score
2,593
Points
203
Location
Toulouse
I wasn't thinking that way.

But now I think that an altitude condition is obviously needed, else the parachute will deploy in orbit sooner or later.

And when it comes to chute deployment, altitude should probably be tracked as a barometric pressure valure (not only for realism issues, also because it prevents silly parachute "deployment" if you attempt a very low lunar orbit or some asteroid close encounter. Also it makes sure that the parachute will deploy at a suitable altitude if used on another planet with atmosphere. Of course, on bodies like Mars, 0.6 atm will never happen and you'll crash at full speed. But it should work for Titan or Venus -we don't even care for temperatures or what the atmosphere is actually made of here -).

I obviously don't have the barometry vs altitude graph for Earth in mind, also my values for parachute deployment are sort of random, but it should be something like :

This isn't specific code, only a general layout

if ((barometric_pressure < 0.6 atm == true) AND (vertical_velocity > 10 m/s == true))
- then
-- [deloy chute]
-- [add some "do-once i = i+1" logic]
That way it should work (again, for a "traditionnal" capsule like Apollo or Soyuz).
 
Last edited:

4throck

Enthusiast !
Joined
Jun 19, 2008
Messages
3,502
Reaction score
985
Points
153
Location
Lisbon
Website
orbiterspaceport.blogspot.com
On Gemini B I'm looking for the presence of the jettisoned service module as a vessel. That gives me "reentry" status.

It can also work with a multistage launcher, by checking for individual stage vessels.
On that case I can get a "orbit" status.

Depends on what you are doing really ;)
 

Donamy

Addon Developer
Addon Developer
Donator
Beta Tester
Joined
Oct 16, 2007
Messages
6,840
Reaction score
93
Points
123
Location
Cape
Forgive the ignorance, but could lua script be used to make a dialog box, to control spacecraft3 animation commands or whatever?
 

4throck

Enthusiast !
Joined
Jun 19, 2008
Messages
3,502
Reaction score
985
Points
153
Location
Lisbon
Website
orbiterspaceport.blogspot.com
Affirmative. You can create a dialog box to ask the user for input.

From orbiter help:

str = proc.wait_input(title)
Open a dialog box and wait for user input.

Parameters:
title (string): dialog box title

Return values:
str (string): user input

Notes:
This function uses oapi.open_inputbox() to open the dialog box, and then enters a loop checking oapi.receive_input() until the user has entered a response.

Cancelling the input box with Esc or a mouse button is not allowed.
 

Donamy

Addon Developer
Addon Developer
Donator
Beta Tester
Joined
Oct 16, 2007
Messages
6,840
Reaction score
93
Points
123
Location
Cape
Also, can you open a dialog box, that would have buttons that can be clicked by a mouse, to
start and stop an animation, that could also be dragged to another screen ? Such as the Canadarm2 dialog box, done by David413.
 

DaveS

Space Shuttle Ultra Project co-developer
Addon Developer
Donator
Beta Tester
Joined
Feb 4, 2008
Messages
9,282
Reaction score
563
Points
203
Also, can you open a dialog box, that would have buttons that can be clicked by a mouse, to
start and stop an animation, that could also be dragged to another screen ? Such as the Canadarm2 dialog box, done by David413.
That's a dialog window, not a dialog box. Boxes only support manual text string entries, while windows have buttons and other custom items like check boxes, radio buttons etc.
 

Donamy

Addon Developer
Addon Developer
Donator
Beta Tester
Joined
Oct 16, 2007
Messages
6,840
Reaction score
93
Points
123
Location
Cape
Thanks for the clarification. The question is the same.
 
Top