einsteinK

Suicide burn logic

Oct 15th, 2020 (edited)
1,373
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. # More of an abstract example to give you an idea of how the suicide burn code might look
  3. # Also doesn't account for drag, but let's just assume that's the margin of error
  4.  
  5. # current_alt(): Gives the current distance from the ground
  6. # The other ones below don't exist yet I think
  7. # current_speed(): Gives the current speed (negative value for falling)
  8. # current_twr(): The current TWR
  9. # max_twr(): The TWR we would hit if we set throttle to 100%
  10. # max_acceleration(): The max m/s² our engines can (currently) achieve
  11.  
  12. # At this point, the code assumes that we're already falling back down
  13.  
  14. target_height = 10 # In meters. Could be higher for margin of error
  15. target_speed = 5 # In absolute m/s
  16. while True:
  17.     # This is how many seconds it'll take until we hit the ground (if speed doesn't change)
  18.     time_to_impact = (current_alt() - target_height) / -current_speed()
  19.     # This is how many seconds it'll take to slow down to our target speed
  20.     time_to_slow_down = (-current_speed() - target_speed) / max_acceleration()
  21.     # If we need to start the burn now, break the while loop
  22.     if time_to_impact <= time_to_slow_down:
  23.         break
  24.     time.sleep(0.1)
  25.  
  26. # Start suicide burn
  27. set_throttle(1)
  28.  
  29. # I don't know if current_twr() updates immediately after setting the throttle, so wait?
  30. time.sleep(0.01)
  31.  
  32. # TWR will slowly increase, which would skew our stuff
  33. # I don't know the complex math to calculate future TWR and
  34. # start the suicide burn later to handle that, so let's just
  35. # keep the TWR constant
  36. target_twr = current_twr()
  37. while current_speed() < -target_speed:
  38.     set_throttle(target_twr/max_twr())
  39.     # ^ Should set the throttle so we have exactly a TWR of target_twr
  40.     time.sleep(0.01)
  41.  
  42. # The while loop above ended, which means we hit our target speed.
  43. # This should also mean we're target_height meters from the ground, hopefully
  44. # (well, most likely not because we were slowing down more than expected because of drag)
  45.  
  46. # This part shouldn't be necessary (if the burn time calculation is perfect)
  47. # but if we're somehow still above our target_height, just keep our current speed.
  48. # Keeping a TWR of 1 should do the job, so we're negating the acceleration from gravity
  49. while current_heigh() > target_height:
  50.     set_throttle(1/max_twr) # Should set the throttle so we have exactly a TWR of 1
  51.     time.sleep(0.01)
  52.  
  53. # Less than 10m from the ground, just shutdown stuff I guess?
  54. set_throttle(0)
  55.  
RAW Paste Data