In order to improve the reentry of a rocket , we need to remember some basic physics.
the starting velocity.
the ending velocity.
elapsed time
the change in height (e.g. going from altitude 200 to altitude 0 would be -200).
the uniform acceleration
thrust-to-weight ratio
throttle
local gravity
We can get the velocity and altitude from the rocket avionics (VelocityRelativeY and Altitude respectively). In order to decide what acceleration we need, we can start with the formula for motion under constant acceleration and solve for . If then Once we know the acceleration we want, we must adjust the engine's throttle to match.
Rather than calculate a mapping from throttle to thrust, we can just set the throttle to something other than 0, and ask the rocket avionics for the ThrustToWeight value. We can then calculate a new throttle setting by assuming a linear relationship between thrust-to-weight ratio and throttle . That formula assues we know what thrust-to-weight ratio we want. This can be calculated using our calculated target acceleration and the local gravity (which is -3.7 on Mars. Pull the Weight and Mass from the avionics and divide to avoid hardcoding) Once you have calculated the desired thrust-to-weight ratio plug it into the formula to calculate throttle and set the appropriate value on the rocket engine.
alias engine d0
alias avionics d1
alias engine d2
alias homeLocation r15
move homeLocation 4010 # asteroid belt
start:
yield
jal memorizeHome
jal scanner
jal land
j start
memorizeHome:
l r0 avionics Altitude
bnez r0 ra
l homeLocation avionics CurrentCode
j ra
land:
alias a r14
alias throttle r13
alias altitude r12
alias oldTTW r11
alias g r10
alias v r9
l altitude avionics Altitude
blez altitude ra # in space, don't manage
l r0 engine On
beqz r0 ra # engine is not on
l r0 avionics DestinationCode
beq r0 homeLocation land2
s engine Throttle 100 # launching to space
j ra
land2:
#destined for home, let's land safely
l throttle engine Throttle
l v avionics VelocityRelativeY
l oldTTW avionics ThrustToWeight
l r1 avionics Weight
l r2 avionics Mass
div g r1 r2
sub g 0 g
mul r0 v v
mul r0 0.5 r0
div r0 r0 altitude # r0 = v*v/2/delta_y = target_accel
div r0 r0 g
sub r0 1 r0 # r0 = target_ttw = 1-target_accel/g
mul r0 throttle r0
div r0 r0 oldTTW # throttle = old_throttle * target_ttw / old_thrust_to_weight
s db Setting r0
slt r1 throttle 70
select r2 r1 80 70 # what is our floor?
slt r1 r0 r2
select r0 r1 1 r0 # if we don't need much thrust, don't bother thrusting
s engine Throttle r0
j ra
######################################################################
scanner:
l r0 avionics Mode
sge r3 r0 3
sb HASH("StructureRocketScanner") On r3
j ra
You may have noticed that the script has logic to keep the engine on
even if the required throttle dips below 80. This is because the
rocket weight decreases as fuel is burned, and earlier versions of the
script tended to pulse the engine on and off which was annoying, and
perhaps dangerous.
Another characteristic of this script is that if your rocket is capable of high thrust-to-weight ratios, the landing can be quite terrifying to an observer, as the rocket approaches the pad at a high speed. I am considering complicating the script by adding a two-stage deceleration logic that uses a lower throttle for the final dozen eters.