Lua Script for Orbit Stabilization

From OrbiterWiki
Revision as of 16:02, 14 October 2022 by Arvil (talk | contribs) (Added category.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Lua script for chapter 2 of Go Play In Space by Bruce Irving, Andy McSorley, and Mark Paton for Orbiter Space Flight Simulator.


-- Lua script for chapter 2 of "Go Play in Space" - Orbit Stabilization
--
-- Author:	David Murley
-- Date:	7/01/2019
-- Version:	1.1 
--
-- 

term.out('Script invoked')

-- Get access to the Delta Gliders GL-01 and GL-02. With the interface handles
-- obtained, access to various controls and flight data are acquired.


hGL1=vessel.get_interface('GL-01')
hGL2=vessel.get_interface('GL-02')

-- Wait until vessels are docked before continuing with script


term.out ('Waiting until vessels are docked before')
term.out ('  executing remainder of script')
continue = true
hDckPort = hGL1:get_dockhandle(0)
while continue do
hDckStatus=hGL1:get_dockstatus(hDckPort)
  if hDckStatus ~= nil then
    continue = false
  else
    proc.wait_simdt(5)
  end
end
  
-- Kill Rotation

term.out('Kill Rotation')
hGL1:set_navmode(NAVMODE.KILLROT)
proc.wait_simdt(10)

-- Undock

term.out('Undock')
hGL1:undock(ALLDOCKS)

-- Set RCS mode on GL-01 to Translation

term.out('Set RCS mode to translate.')
hGL1:set_rcsmode(RCSMODE.LIN)

-- Thrust away from GL-02. The resulting distance from GL-02 is about 35 meters.

term.out('Separation of GL-01 and GL-02 to +/-35 meters')
-- oapi.set_tacc(10)
thrstGrpHdl1=hGL1:get_thrustergrouphandle(THGROUP.ATT_BACK)
hGL1:set_thrustergrouplevel(THGROUP.ATT_BACK,.1)
proc.wait_simdt(11.237)
hGL1:set_thrustergrouplevel(THGROUP.ATT_BACK,0)
proc.wait_simdt(52.777)
thrstGrpHdl2=hGL1:get_thrustergrouphandle(THGROUP.ATT_FORWARD)
hGL1:set_thrustergrouplevel(THGROUP.ATT_FORWARD,.1)
proc.wait_simdt(18.733)
hGL1:set_thrustergrouplevel(THGROUP.ATT_FORWARD,0)
-- oapi.set_tacc(1)
term.out('Separation completed.')

-- Set the prograde autopilot for both vessels to on.

term.out('Set prograde autopilot for both vessels.')
hGL1:set_navmode(NAVMODE.PROGRADE)
hGL2:set_navmode(NAVMODE.PROGRADE)
proc.wait_simdt(30)

-- The following 14 lines set up the annotations that are used to display
-- time to apoapsis for both vessels

hApT1Label = oapi.create_annotation()
hApT1Label:set_pos(0.7,0.7,1,1)
hApT1Label:set_colour({r=1, g=0.7, b=0.3})
hApT1Label:set_text ('ApT (GL-01): ')

hApT1 = oapi.create_annotation()
hApT1:set_pos(0.8,0.7,1,1)
hApT1:set_colour({r=1, g=0.7, b=0.3})

hApT2Label = oapi.create_annotation()
hApT2Label:set_pos(0.7,0.75,1,1)
hApT2Label:set_colour({r=1, g=0.7, b=0.3})
hApT2Label:set_text ('ApT (GL-02): ')

hApT2 = oapi.create_annotation()
hApT2:set_pos(0.8,0.75,1,1)
hApT2:set_colour({r=1, g=0.7, b=0.3})

-- Wait until +/- 20 seconds before apoapsis, then slow time acceleration to 1x.
-- Ignition of main engines on both vessels occurs around +/- 10 seconds before 
-- apoapsis. Time acceleration during the waiting period is set to 100x to
-- reduce waiting time for those who are easily bored.

term.out ('Wait until +/- 10 seconds from aoapis. Then fire')
term.out ('  main engines to stabilize orbit')
oapi.set_tacc(100)
continue = true
while continue do
  el,op = hGL1:get_elementsex()
  TimeToApoapsis= op.ApT
  hApT1:set_text(TimeToApoapsis)
  el,op=hGL2:get_elementsex()
  TimeToAposis=op.ApT
  hApT2:set_text(TimeToApoapsis)
  if TimeToApoapsis < 20 then
     continue=false
  end
  proc.wait_simdt(1)
end
oapi.set_tacc(1)


-- Track time to apoapsis from time=+/-20 sec in 1x time.

continue=true
while continue do
  el,op = hGL1:get_elementsex()
  TimeToApoapsis1 =op.ApT
  hApT1:set_text(TimeToApoapsis1)
  el,op = hGL2:get_elementsex()
  TimeToApoapsis2=op.ApT
  hApT2:set_text(TimeToApoapsis2)
  if TimeToApoapsis1 <= 10 then
    continue=false
    oapi.del_annotation(hApT1)
    oapi.del_annotation(hApT1Label)
    oapi.del_annotation(hApT2)
    oapi.del_annotation(hApT2Label)
  else
    proc.wait_simdt(1)
  end
end

-- Set up annotations to show orbit eccentricities

hEcc1Label = oapi.create_annotation()
hEcc1Label:set_pos(0.7,0.7,1,1)
hEcc1Label:set_colour({r=1, g=0.7, b=0.3})
hEcc1Label:set_text ('Ecc (GL-01): ')

hEcc1 = oapi.create_annotation()
hEcc1:set_pos(0.8,0.7,1,1)
hEcc1:set_colour({r=1, g=0.7, b=0.3})

hEcc2Label = oapi.create_annotation()
hEcc2Label:set_pos(0.7,0.75,1,1)
hEcc2Label:set_colour({r=1, g=0.7, b=0.3})
hEcc2Label:set_text ('Ecc (GL-02): ')

hEcc2 = oapi.create_annotation()
hEcc2:set_pos(0.8,0.75,1,1)
hEcc2:set_colour({r=1, g=0.7, b=0.3})

-- Stabilize orbits

term.out('Start Stabilization of Orbits')
hGL1:set_thrustergrouplevel(THGROUP.MAIN, 1)
hGL2:set_thrustergrouplevel(THGROUP.MAIN, 1)
continue1=true
continue2 = true
while continue1 or continue2 do
  el=hGL1:get_elements()
  e1 = el.e
  hEcc1:set_text(e1)
  if e1 <= .005 then
    hGL1:set_thrustergrouplevel (THGROUP.MAIN, 0)
    continue1 = false
  end 
  el = hGL2:get_elements()
  e2 = el.e
  hEcc2:set_text(e2)
  if e2 <= .005 then
    hGL2:set_thrustergrouplevel (THGROUP.MAIN, 0)
    continue2 = false
  end
  proc.wait_simdt(1)
end
term.out ('Orbits stabilized.')
term.out ('')
term.out ('')

-- Wait a few seconds and then delete eccentricity annotations

proc.wait_simdt(10)
oapi.del_annotation(hEcc1)
oapi.del_annotation(hEcc1Label)
oapi.del_annotation(hEcc2)
oapi.del_annotation(hEcc2Label)
  


term.out ('End Script')