japsert

Trajectory prediction using orbital velocity

Aug 24th, 2023 (edited)
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | Science | 0 0
  1. @lazyGlobal off.
  2. clearScreen.
  3. clearVecDraws().
  4.  
  5. local parameter doInitialBurn is false.
  6.  
  7. // Time step in seconds
  8. global DT is 1.
  9. print "dT: " + DT + "s" at (0, 0).
  10.  
  11. // Draw trajectory vectors when 'w' is pressed
  12. local drawDebugVectors is false.
  13. when terminal:input:haschar then {
  14. if terminal:input:getchar() = "w" {
  15. set drawDebugVectors to true.
  16. }
  17. preserve.
  18. }
  19.  
  20. if doInitialBurn {
  21. stage.
  22. lock throttle to 1.
  23. lock steering to heading(90, 80, 270).
  24. wait until ship:altitude > 1000.
  25. lock throttle to 0.
  26.  
  27. // Wait 5 ticks to make sure throttle delay doesn't influence acceleration
  28. wait 0.1.
  29. // Draw debug vectors for the first loop
  30. set drawDebugVectors to true.
  31. }
  32.  
  33.  
  34. // Draws a vector from pos to pos + vec
  35. function drawDebugVec {
  36. local parameter geopos.
  37. local parameter alt_.
  38. local parameter vecToNewPos.
  39. local parameter i.
  40.  
  41. vecDraw(
  42. { return geopos:altitudePosition(alt_). }, vecToNewPos,
  43. red, i, 1, true
  44. ).
  45. }
  46.  
  47. until false {
  48. local posVec is ship:position.
  49. local velVec is ship:velocity:orbit.
  50. local correctedGeopos is ship:geoposition.
  51. local i is 0.
  52. local reachedGround is false.
  53. local drawDebugVectorsThisIteration is drawDebugVectors. // DEBUG
  54. until reachedGround {
  55. set i to i+1.
  56.  
  57. // Calculate altitude
  58. local centerToPosVec is (posVec - body:position).
  59. local altFromCenter is centerToPosVec:mag.
  60. local alt_ is altFromCenter - body:radius.
  61.  
  62. // Determine acceleration at altitude
  63. local g is (body:mu)/(altFromCenter)^2.
  64. local gravAccVec is -centerToPosVec:normalized * g.
  65. local accVec is gravAccVec.
  66.  
  67. // Calculate vector pointing to next position (starting from current
  68. // position vector)
  69. local positionChangeVec is velVec * DT + 0.5 * accVec * DT^2.
  70.  
  71. // Add this vector to the position vector
  72. local newPosVec is posVec + positionChangeVec.
  73.  
  74. // Calculate new velocity vector
  75. local newVelVec is velVec + accVec * DT.
  76.  
  77. // Convert new position vector (starting from ship) to
  78. // geocoordinates/altitude
  79. local newGeopos is body:geopositionOf(newPosVec).
  80. local newAlt is body:altitudeOf(newPosVec).
  81.  
  82. // Correct geocoordinates for body rotation
  83. local bodyRotationPerStep is // should be precomputed
  84. body:angularVel:mag * constant:radToDeg * DT.
  85. local bodyRotationSinceStart is bodyRotationPerStep * i.
  86. local correctedNewGeopos is
  87. latLng(newGeopos:lat, newGeopos:lng - bodyRotationSinceStart).
  88.  
  89. // Check if we reached the ground
  90. if newAlt <= max(correctedNewGeopos:terrainHeight, 0)
  91. set reachedGround to true.
  92.  
  93. // DEBUG: Draw debug vector
  94. // Draw vector from rotation corrected geocoordinates/altitude from
  95. // previous iteration to rotation corrected geocoordinates/altitude from
  96. // current iteration
  97. local srfPosVec is correctedGeopos:altitudePosition(alt_).
  98. local newSrfPosVec is correctedNewGeopos:altitudePosition(newAlt).
  99. local vecToNewPos is newSrfPosVec - srfPosVec.
  100. if drawDebugVectorsThisIteration
  101. drawDebugVec(correctedGeopos, alt_, vecToNewPos, i).
  102.  
  103. // Set the position vector (starting from ship) and the velocity vector
  104. // for the next iteration
  105. set posVec to newPosVec.
  106. set velVec to newVelVec.
  107. set correctedGeopos to correctedNewGeopos. // DEBUG
  108. }
  109. // Finished first loop, stop drawing debug vectors
  110. set drawDebugVectors to false. // DEBUG
  111.  
  112. if reachedGround
  113. print "Reached ground after " + i + " iterations" at (0, 1).
  114.  
  115. wait 0.
  116. }
  117.  
Tags: KSP KOS
Advertisement
Add Comment
Please, Sign In to add comment