Difference between revisions of "Lua Script for Orbit Stabilization"

From OrbiterWiki
Jump to: navigation, search
(Lua script for orbit stabilization as described in Chapter 2:Smack! of Go Play In Space)
 
m
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
 
-- Lua script for chapter 2 of "Go Play in Space" - Orbit Stabilization
 
-- Lua script for chapter 2 of "Go Play in Space" - Orbit Stabilization
 +
 
--
 
--
 +
 
-- Author: David Murley
 
-- Author: David Murley
 +
 
-- Date: 7/01/2019
 
-- Date: 7/01/2019
 +
 
-- Version: 1.1  
 
-- Version: 1.1  
 +
 
--
 
--
 +
 
--  
 
--  
 +
  
 
term.out('Script invoked')
 
term.out('Script invoked')
 +
  
 
-- Get access to the Delta Gliders GL-01 and GL-02. With the interface handles
 
-- 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.
 
-- obtained, access to various controls and flight data are acquired.
 +
  
  
 
hGL1=vessel.get_interface('GL-01')
 
hGL1=vessel.get_interface('GL-01')
 +
 
hGL2=vessel.get_interface('GL-02')
 
hGL2=vessel.get_interface('GL-02')
 +
  
 
-- Wait until vessels are docked before continuing with script
 
-- Wait until vessels are docked before continuing with script
 +
  
  
 
term.out ('Waiting until vessels are docked before')
 
term.out ('Waiting until vessels are docked before')
 +
 
term.out ('  executing remainder of script')
 
term.out ('  executing remainder of script')
 +
 
continue = true
 
continue = true
 +
 
hDckPort = hGL1:get_dockhandle(0)
 
hDckPort = hGL1:get_dockhandle(0)
 +
 
continue=true
 
continue=true
 +
 
while continue do
 
while continue do
hDckStatus=hGL1:get_dockstatus(hDckPort)
+
 
 +
  hDckStatus=hGL1:get_dockstatus(hDckPort)
 +
 
 
   if hDckStatus ~= nil then
 
   if hDckStatus ~= nil then
 +
 
     continue = false
 
     continue = false
 +
 
   else
 
   else
 +
 
     proc.wait_simdt(5)
 
     proc.wait_simdt(5)
 +
 
   end
 
   end
 +
 
end
 
end
 +
 
    
 
    
 
-- Kill Rotation
 
-- Kill Rotation
 +
  
 
term.out('Kill Rotation')
 
term.out('Kill Rotation')
 +
 
hGL1:set_navmode(NAVMODE.KILLROT)
 
hGL1:set_navmode(NAVMODE.KILLROT)
 +
 
proc.wait_simdt(10)
 
proc.wait_simdt(10)
 +
  
 
-- Undock
 
-- Undock
 +
  
 
term.out('Undock')
 
term.out('Undock')
 +
 
hGL1:undock(ALLDOCKS)
 
hGL1:undock(ALLDOCKS)
 +
  
 
-- Set RCS mode on GL-01 to Translation
 
-- Set RCS mode on GL-01 to Translation
 +
  
 
term.out('Set RCS mode to translate.')
 
term.out('Set RCS mode to translate.')
 +
 
hGL1:set_rcsmode(RCSMODE.LIN)
 
hGL1:set_rcsmode(RCSMODE.LIN)
 +
  
 
-- Thrust away from GL-02. The resulting distance from GL-02 is about 35 meters.
 
-- 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')
 
term.out('Separation of GL-01 and GL-02 to +/-35 meters')
 +
 
-- oapi.set_tacc(10)
 
-- oapi.set_tacc(10)
 +
 
thrstGrpHdl1=hGL1:get_thrustergrouphandle(THGROUP.ATT_BACK)
 
thrstGrpHdl1=hGL1:get_thrustergrouphandle(THGROUP.ATT_BACK)
 +
 
hGL1:set_thrustergrouplevel(THGROUP.ATT_BACK,.1)
 
hGL1:set_thrustergrouplevel(THGROUP.ATT_BACK,.1)
 +
 
proc.wait_simdt(11.237)
 
proc.wait_simdt(11.237)
 +
 
hGL1:set_thrustergrouplevel(THGROUP.ATT_BACK,0)
 
hGL1:set_thrustergrouplevel(THGROUP.ATT_BACK,0)
 +
 
proc.wait_simdt(52.777)
 
proc.wait_simdt(52.777)
 +
 
thrstGrpHdl2=hGL1:get_thrustergrouphandle(THGROUP.ATT_FORWARD)
 
thrstGrpHdl2=hGL1:get_thrustergrouphandle(THGROUP.ATT_FORWARD)
 +
 
hGL1:set_thrustergrouplevel(THGROUP.ATT_FORWARD,.1)
 
hGL1:set_thrustergrouplevel(THGROUP.ATT_FORWARD,.1)
 +
 
proc.wait_simdt(18.733)
 
proc.wait_simdt(18.733)
 +
 
hGL1:set_thrustergrouplevel(THGROUP.ATT_FORWARD,0)
 
hGL1:set_thrustergrouplevel(THGROUP.ATT_FORWARD,0)
 +
 
-- oapi.set_tacc(1)
 
-- oapi.set_tacc(1)
 +
 
term.out('Separation completed.')
 
term.out('Separation completed.')
 +
  
 
-- Set the prograde autopilot for both vessels to on.
 
-- Set the prograde autopilot for both vessels to on.
 +
  
 
term.out('Set prograde autopilot for both vessels.')
 
term.out('Set prograde autopilot for both vessels.')
 +
 
hGL1:set_navmode(NAVMODE.PROGRADE)
 
hGL1:set_navmode(NAVMODE.PROGRADE)
 +
 
hGL2:set_navmode(NAVMODE.PROGRADE)
 
hGL2:set_navmode(NAVMODE.PROGRADE)
 +
 
proc.wait_simdt(25)
 
proc.wait_simdt(25)
 +
  
 
-- The following 14 lines set up the annotations that are used to display
 
-- The following 14 lines set up the annotations that are used to display
 +
 
-- time to apoapsis for both vessels
 
-- time to apoapsis for both vessels
 +
  
 
hApT1Label = oapi.create_annotation()
 
hApT1Label = oapi.create_annotation()
 +
 
hApT1Label:set_pos(0.7,0.7,1,1)
 
hApT1Label:set_pos(0.7,0.7,1,1)
 +
 
hApT1Label:set_colour({r=1, g=0.7, b=0.3})
 
hApT1Label:set_colour({r=1, g=0.7, b=0.3})
 +
 
hApT1Label:set_text ('ApT (GL-01): ')
 
hApT1Label:set_text ('ApT (GL-01): ')
 +
  
 
hApT1 = oapi.create_annotation()
 
hApT1 = oapi.create_annotation()
 +
 
hApT1:set_pos(0.8,0.7,1,1)
 
hApT1:set_pos(0.8,0.7,1,1)
 +
 
hApT1:set_colour({r=1, g=0.7, b=0.3})
 
hApT1:set_colour({r=1, g=0.7, b=0.3})
 +
  
 
hApT2Label = oapi.create_annotation()
 
hApT2Label = oapi.create_annotation()
 +
 
hApT2Label:set_pos(0.7,0.75,1,1)
 
hApT2Label:set_pos(0.7,0.75,1,1)
 +
 
hApT2Label:set_colour({r=1, g=0.7, b=0.3})
 
hApT2Label:set_colour({r=1, g=0.7, b=0.3})
 +
 
hApT2Label:set_text ('ApT (GL-02): ')
 
hApT2Label:set_text ('ApT (GL-02): ')
 +
 +
  
 
hApT2 = oapi.create_annotation()
 
hApT2 = oapi.create_annotation()
 +
 
hApT2:set_pos(0.8,0.75,1,1)
 
hApT2:set_pos(0.8,0.75,1,1)
 +
 
hApT2:set_colour({r=1, g=0.7, b=0.3})
 
hApT2:set_colour({r=1, g=0.7, b=0.3})
 +
  
 
-- Wait until +/- 20 seconds before apoapsis, then slow time acceleration to 1x.
 
-- Wait until +/- 20 seconds before apoapsis, then slow time acceleration to 1x.
 +
 
-- Ignition of main engines on both vessels occurs around +/- 10 seconds before  
 
-- Ignition of main engines on both vessels occurs around +/- 10 seconds before  
 +
 
-- apoapsis. Time acceleration during the waiting period is set to 100x to
 
-- apoapsis. Time acceleration during the waiting period is set to 100x to
 +
 
-- reduce waiting time for those who are easily bored.
 
-- reduce waiting time for those who are easily bored.
 +
  
 
term.out ('Wait until +/- 10 seconds from aoapis. Then fire')
 
term.out ('Wait until +/- 10 seconds from aoapis. Then fire')
 +
 
term.out ('  main engines to stabilize orbit')
 
term.out ('  main engines to stabilize orbit')
 +
 
oapi.set_tacc(100)
 
oapi.set_tacc(100)
 +
 
continue = true
 
continue = true
 +
 
while continue do
 
while continue do
 +
 
   el,op = hGL1:get_elementsex()
 
   el,op = hGL1:get_elementsex()
 +
 
   TimeToApoapsis= op.ApT
 
   TimeToApoapsis= op.ApT
 +
 
   hApT1:set_text(TimeToApoapsis)
 
   hApT1:set_text(TimeToApoapsis)
 +
 
   el,op=hGL2:get_elementsex()
 
   el,op=hGL2:get_elementsex()
 +
 
   TimeToAposis=op.ApT
 
   TimeToAposis=op.ApT
 +
 
   hApT2:set_text(TimeToApoapsis)
 
   hApT2:set_text(TimeToApoapsis)
 +
 
   if TimeToApoapsis < 20 then
 
   if TimeToApoapsis < 20 then
 +
 
     continue=false
 
     continue=false
 +
 
   end
 
   end
 +
 
   proc.wait_simdt(1)
 
   proc.wait_simdt(1)
 +
 
end
 
end
 +
 
oapi.set_tacc(1)
 
oapi.set_tacc(1)
 +
  
  
 
-- Track time to apoapsis from time=+/-20 sec in 1x time.
 
-- Track time to apoapsis from time=+/-20 sec in 1x time.
 +
  
 
continue=true
 
continue=true
 +
 
while continue do
 
while continue do
 +
 
   el,op = hGL1:get_elementsex()
 
   el,op = hGL1:get_elementsex()
 +
 
   TimeToApoapsis1 =op.ApT
 
   TimeToApoapsis1 =op.ApT
 +
 
   hApT1:set_text(TimeToApoapsis1)
 
   hApT1:set_text(TimeToApoapsis1)
 +
 
   el,op = hGL2:get_elementsex()
 
   el,op = hGL2:get_elementsex()
 +
 
   TimeToApoapsis2=op.ApT
 
   TimeToApoapsis2=op.ApT
 +
 
   hApT2:set_text(TimeToApoapsis2)
 
   hApT2:set_text(TimeToApoapsis2)
 +
 
   if TimeToApoapsis1 <= 10 then
 
   if TimeToApoapsis1 <= 10 then
 +
 
     continue=false
 
     continue=false
 +
 
     oapi.del_annotation(hApT1)
 
     oapi.del_annotation(hApT1)
 +
 
     oapi.del_annotation(hApT1Label)
 
     oapi.del_annotation(hApT1Label)
 +
 
     oapi.del_annotation(hApT2)
 
     oapi.del_annotation(hApT2)
 +
 
     oapi.del_annotation(hApT2Label)
 
     oapi.del_annotation(hApT2Label)
 +
 
   else
 
   else
 +
 
     proc.wait_simdt(1)
 
     proc.wait_simdt(1)
 +
 
   end
 
   end
 +
 
end
 
end
 +
  
 
-- Set up annotations to show orbit eccentricities
 
-- Set up annotations to show orbit eccentricities
 +
  
 
hEcc1Label = oapi.create_annotation()
 
hEcc1Label = oapi.create_annotation()
 +
 
hEcc1Label:set_pos(0.7,0.7,1,1)
 
hEcc1Label:set_pos(0.7,0.7,1,1)
 +
 
hEcc1Label:set_colour({r=1, g=0.7, b=0.3})
 
hEcc1Label:set_colour({r=1, g=0.7, b=0.3})
 +
 
hEcc1Label:set_text ('Ecc (GL-01): ')
 
hEcc1Label:set_text ('Ecc (GL-01): ')
 +
  
 
hEcc1 = oapi.create_annotation()
 
hEcc1 = oapi.create_annotation()
 +
 
hEcc1:set_pos(0.8,0.7,1,1)
 
hEcc1:set_pos(0.8,0.7,1,1)
 +
 
hEcc1:set_colour({r=1, g=0.7, b=0.3})
 
hEcc1:set_colour({r=1, g=0.7, b=0.3})
 +
 +
  
 
hEcc2Label = oapi.create_annotation()
 
hEcc2Label = oapi.create_annotation()
 +
 
hEcc2Label:set_pos(0.7,0.75,1,1)
 
hEcc2Label:set_pos(0.7,0.75,1,1)
 +
 
hEcc2Label:set_colour({r=1, g=0.7, b=0.3})
 
hEcc2Label:set_colour({r=1, g=0.7, b=0.3})
 +
 
hEcc2Label:set_text ('Ecc (GL-02): ')
 
hEcc2Label:set_text ('Ecc (GL-02): ')
 +
  
 
hEcc2 = oapi.create_annotation()
 
hEcc2 = oapi.create_annotation()
 +
 
hEcc2:set_pos(0.8,0.75,1,1)
 
hEcc2:set_pos(0.8,0.75,1,1)
 +
 
hEcc2:set_colour({r=1, g=0.7, b=0.3})
 
hEcc2:set_colour({r=1, g=0.7, b=0.3})
 +
  
 
-- Stabilize orbits
 
-- Stabilize orbits
 +
  
 
term.out('Start Stabilization of Orbits')
 
term.out('Start Stabilization of Orbits')
 +
 
hGL1:set_thrustergrouplevel(THGROUP.MAIN, 1)
 
hGL1:set_thrustergrouplevel(THGROUP.MAIN, 1)
 +
 
hGL2:set_thrustergrouplevel(THGROUP.MAIN, 1)
 
hGL2:set_thrustergrouplevel(THGROUP.MAIN, 1)
 +
 +
 
continue1=true
 
continue1=true
 +
 
continue2 = true
 
continue2 = true
 +
 
while continue1 or continue2 do
 
while continue1 or continue2 do
 +
 
   el=hGL1:get_elements()
 
   el=hGL1:get_elements()
 +
 
   e1 = el.e
 
   e1 = el.e
 +
 
   hEcc1:set_text(e1)
 
   hEcc1:set_text(e1)
 +
 
   if e1 <= .005 then
 
   if e1 <= .005 then
 
     hGL1:set_thrustergrouplevel (THGROUP.MAIN, 0)
 
     hGL1:set_thrustergrouplevel (THGROUP.MAIN, 0)
 +
 
     continue1 = false
 
     continue1 = false
 +
 
   end  
 
   end  
 +
 
   el = hGL2:get_elements()
 
   el = hGL2:get_elements()
 +
 
   e2 = el.e
 
   e2 = el.e
 +
 
   hEcc2:set_text(e2)
 
   hEcc2:set_text(e2)
 +
 
   if e2 <= .005 then
 
   if e2 <= .005 then
 +
 
     hGL2:set_thrustergrouplevel (THGROUP.MAIN, 0)
 
     hGL2:set_thrustergrouplevel (THGROUP.MAIN, 0)
 +
 
     continue2 = false
 
     continue2 = false
 +
 
   end
 
   end
 +
 
   proc.wait_simdt(1)
 
   proc.wait_simdt(1)
 +
 
end
 
end
 +
 
term.out ('Orbits stabilized.')
 
term.out ('Orbits stabilized.')
 +
 
term.out ('')
 
term.out ('')
 +
 
term.out ('')
 
term.out ('')
 +
  
 
-- Wait a few seconds and then delete eccentricity annotations
 
-- Wait a few seconds and then delete eccentricity annotations
 +
  
 
proc.wait_simdt(10)
 
proc.wait_simdt(10)
 +
 
oapi.del_annotation(hEcc1)
 
oapi.del_annotation(hEcc1)
 +
 
oapi.del_annotation(hEcc1Label)
 
oapi.del_annotation(hEcc1Label)
 +
 
oapi.del_annotation(hEcc2)
 
oapi.del_annotation(hEcc2)
 +
 
oapi.del_annotation(hEcc2Label)
 
oapi.del_annotation(hEcc2Label)
 +
 
    
 
    
  
  
 
term.out ('End Script')
 
term.out ('End Script')

Revision as of 20:42, 12 July 2019

-- 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)

continue=true

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(25)


-- 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')