Advertisement
YassMes

Untitled

Feb 7th, 2024 (edited)
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.31 KB | Gaming | 0 0
  1. //KEEP IN MIND MY NOTES MAY BE INCORRECT AS IT HAS BEEN A YEAR SINCE THIS CODE HAS BEEN TOUCHED, *MY COMMENTS MAY BE WRONG*
  2. CLEARSCREEN.
  3. CLEARVECDRAWS().
  4.  
  5. //VARIABLES
  6. SET looping TO TRUE.
  7. SET L TO 0.
  8. SET sm TO "". //sm just means steering mode
  9. SET lastClear TO TIME:SECONDS.
  10. SET LZ1 TO Kerbin:GEOPOSITIONLATLNG(-0.0973726146087016,-74.534842938848).
  11. SET LAUNCHPAD TO Kerbin:GEOPOSITIONLATLNG(-0.0971980934745649,-74.5576639199546).
  12. SET LZ TO LZ1.
  13.  
  14.  
  15. //FUNCTIONS YOULL NEED
  16. FUNCTION YASS_PID_LOOP {
  17.  
  18. PARAMETER kP.
  19. PARAMETER kI.
  20. PARAMETER kD.
  21.  
  22. PARAMETER input.
  23. PARAMETER wanted.
  24.  
  25. IF DEFINED lastTime {
  26. }ELSE{
  27. SET P TO 0.
  28. SET I TO 0.
  29. SET D TO 0.
  30.  
  31. SET lastP TO 0.
  32. SET lastI TO 0.
  33. SET lastTime TO TIME:SECONDS.
  34. }
  35.  
  36. LOCAL thisTime IS TIME:SECONDS.
  37. LOCAL deltaTime IS (thisTime-lastTime).
  38.  
  39. SET P TO (wanted-input).
  40.  
  41. IF deltaTime > 0 {
  42. SET I TO (lastI) + (((P+lastP)/2) * deltaTime).
  43. SET D TO (P-lastP) / (deltaTime).
  44. }
  45.  
  46. SET outputValue TO (P*kP) + (I*kI) + (D*kD).
  47.  
  48. RETURN outputValue.
  49.  
  50. SET lastTime TO thisTime.
  51. SET lastP TO P.
  52. SET lastI TO I.
  53.  
  54. //IF(TIME:SECONDS-now > 0.05) {
  55. //LOG input TO TESTHOVER0.csv.
  56. //LOG (TIME:SECONDS-takeOffTime) TO TESTHOVER1.csv.
  57. //SET now TO TIME:SECONDS.
  58. //}
  59. //PRINT "P : " + round(P,2) + " " AT (0,1).
  60. //PRINT "I : " + round(I,2) + " " AT (0,2).
  61. //PRINT "D : " + round(D,2) + " " AT (0,3).
  62. //
  63. //PRINT "output : " + round(outputValue,2) + " " AT (0,5).
  64. //WAIT 0.
  65. }
  66.  
  67.  
  68. FUNCTION EQ { // d1=(yB-yA) --- d2=(xB-xA) --- Termine noto=((d2*yA)+(d1*-xA)) --- Result =((d1*currentInput)+termineNoto)/d2
  69. PARAMETER xA. //Starting input
  70. PARAMETER yA. //Starting output
  71. PARAMETER xB. //Final input
  72. PARAMETER yB. //Final output
  73. PARAMETER currentInput. //Example eqCalc(0,90,65000,0,SHIP:APOAPSIS)
  74. PARAMETER allowGrowing IS FALSE.
  75.  
  76. LOCAL returner IS (((yB-yA)*currentInput)+(((xB-xA)*yA)+((yB-yA)*-xA)))/(xB-xA).
  77.  
  78. IF allowGrowing = TRUE {
  79. RETURN returner.
  80. }ELSE IF allowGrowing = FALSE {
  81. RETURN minmax(yb,ya,returner).
  82. }
  83. }
  84.  
  85. FUNCTION constantTWR { //mantain a constant twr
  86. PARAMETER wantedTWR.
  87.  
  88. LOCAL output IS (wantedTWR*SHIP:MASS*gValue())/((SHIP:AVAILABLETHRUST)+1).
  89. RETURN MIN(1, MAX(0,output)).
  90. }
  91.  
  92. FUNCTION minmax { //clamp between minv and maxv
  93. DECLARE PARAMETER minv.
  94. DECLARE PARAMETER maxv.
  95. DECLARE PARAMETER input.
  96.  
  97. IF maxv > minv {
  98. RETURN MIN(maxv, MAX(minv,input)).
  99. }ELSE IF maxv < minv {
  100. RETURN MIN(minv, MAX(maxv,input)).
  101. }
  102. }
  103.  
  104. FUNCTION dToRad { //degrees to radians
  105. PARAMETER d.
  106. RETURN d * (CONSTANT:PI()/180).
  107. }
  108.  
  109. FUNCTION haversine { //haversine is just a math formula used to calculate distance between two points on a sphere
  110. DECLARE LOCAL PARAMETER pointA.
  111. DECLARE LOCAL PARAMETER pointB.
  112. DECLARE LOCAL PARAMETER zeroer IS "".
  113.  
  114. SET aLAT TO dToRad(pointA:LAT).
  115. SET aLNG TO dToRad(pointA:LNG).
  116. SET bLAT TO dToRad(pointB:LAT).
  117. SET bLNG TO dToRad(pointB:LNG).
  118. SET deltaLAT TO ABS(aLAT-bLAT).
  119. SET deltaLNG TO ABS(aLNG-bLNG).
  120. IF zeroer = "LNG" {
  121. SET deltaLAT TO 0.
  122. }ELSE IF zeroer = "LAT" {
  123. SET deltaLNG TO 0.
  124. }
  125. LOCAL a IS (SIN(deltaLAT/2))^2+COS(aLAT)*COS(bLAT)*(SIN(deltaLNG/2))^2.
  126. RETURN 2*ARCSIN(SQRT(a))*BODY:RADIUS.
  127. }
  128.  
  129.  
  130. FUNCTION line { //used for display output
  131. DECLARE PARAMETER adder.
  132. SET L TO L+adder.
  133. }
  134.  
  135.  
  136. FUNCTION gValue {
  137. DECLARE LOCAL PARAMETER altit IS ALTITUDE.
  138. RETURN (CONSTANT:G*BODY:MASS)/((BODY:RADIUS+altit)^2).
  139. }
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147. //LANDING VARIABLES
  148. SET radarOffset TO 50. //Account for legs (add about +3m more)
  149.  
  150. RCS ON. SAS OFF. BRAKES OFF.
  151. LOCK THROTTLE TO t.
  152. LOCK STEERING TO LOOKDIRUP(xy,NORTH:VECTOR*-1).
  153.  
  154. //VECDRAWS
  155. WHEN showVec = 1 THEN {
  156. VECDRAW({RETURN SHIP:POSITION.},{RETURN IP:POSITION.},YELLOW,"SHIP-IP",1,TRUE,.1). //VECTOR TO VESSEL IMPACTPOINT
  157. VECDRAW({RETURN SHIP:POSITION.},{RETURN LZ:POSITION.},WHITE,"SHIP-LZ",1,TRUE,.1). //VECTOR TO OUR LANDING ZONE (OR HOVER TARGET)
  158. VECDRAW({RETURN SHIP:POSITION.},{RETURN HVvec:NORMALIZED.},RGB(153,0,153),"Horizontal",35,TRUE,.005). //VECTOR OF OUR HORIZONTAL VELOCITY
  159. VECDRAW({RETURN SHIP:POSITION.},{RETURN xy.},BLACK,"steering",40,TRUE,.005). //VECTOR OF OUR COMMANDED STEERING HEADING
  160. VECDRAW({RETURN SHIP:POSITION.},{RETURN VXCL(UP:VECTOR,xy):NORMALIZED.},BLACK,"steering",20,TRUE,.005). //VECOTR OF THE HORIZONTAL COMPONENT OF OUR COMMANDED STEERING HEADING
  161. VECDRAW({RETURN SHIP:POSITION.},{RETURN vecDiffHoriz.},WHITE,"steering",35,TRUE,.005). //HORIZONTAL VECTOR TO OUR LZ (basically the one which we will probe and will add our horizontal velocity to)
  162. VECDRAW({RETURN SHIP:POSITION.},{RETURN errorVec:NORMALIZED.},BLUE,"errorVec",35,TRUE,.005). //mirrored vector (See line 102)
  163. VECDRAW({RETURN SHIP:POSITION.},{RETURN SHIP:FACING:VECTOR.},GREEN,"Facing Vector",35,TRUE,.005). //Facing vector
  164. }
  165.  
  166. //DISPLAY READOUTS //line(1). PRINT "" AT (0,L).
  167. FUNCTION readouts {
  168. line(1). PRINT "STEERING" AT (0,L).
  169. PRINT "mode: " + sm AT (10,L).
  170. line(1). PRINT "--------" AT (0,L).
  171. line(1). PRINT "TILTANGLE" AT (0,L).
  172. PRINT "± " + ROUND(tiltAngleRange,1) AT (10,L).
  173. line(1). PRINT "Real: " + ROUND(VANG(xy,UP:VECTOR),1) AT (0,L).
  174. line(1). PRINT "Raw: " + ROUND(tiltAngleRaw,1) AT (0,L).
  175. PRINT +"|" + ROUND(tiltAngle,1) AT (10,L).
  176. PRINT " comm" AT (16,L).
  177. line(1). PRINT "targetHVRaw: " + ROUND(targetHVRaw,1) AT (0,L).
  178. PRINT "|" + ROUND(targetHV,1) AT (17,L).
  179. PRINT " comm" AT (22,L).
  180. line(1). PRINT "vecDiffHorizHV: " + ROUND(vecDiffHorizHV,1) AT (0,L).
  181. line(1). PRINT "HVvec:MAG: " + ROUND(HVvec:MAG,1) AT (0,L).
  182. line(1). PRINT "VANG HVvec-steeringVec : " + ROUND(VANG(HVvec,vecDiffHoriz),1) AT (0,L).
  183.  
  184. line(3). PRINT "THROTTLE" AT (0,L).
  185. line(1). PRINT "--------" AT (0,L).
  186. line(1). PRINT "t: " + ROUND(t,1) AT (0,L).
  187. PRINT "|" + ROUND(SHIP:VERTICALSPEED,1) AT (15,L).
  188.  
  189. line(1). PRINT "hs IPLZ : " + ROUND(distanceIPLZ,1) AT (0,L).
  190. line(1). PRINT "hs SHIPLZ : " + ROUND(haversine(SHIP:GEOPOSITION,LZ),1) AT (0,L).
  191.  
  192. SET L TO 0.
  193. }
  194.  
  195. //SCRIPT
  196. UNTIL looping = FALSE {
  197.  
  198. IF ADDONS:TR:HASIMPACT {
  199. SET IP TO ADDONS:TR:IMPACTPOS.
  200. SET distanceIPLZ TO haversine(IP,LZ).
  201. }ELSE{
  202. SET IP TO SHIP:GEOPOSITION. //IP is impact point
  203. }
  204.  
  205. IF TIME:SECONDS - lastClear > .25 {
  206. CLEARSCREEN.
  207. SET lastClear TO TIME:SECONDS.
  208. }
  209.  
  210.  
  211. SET HVvec TO VXCL(UP:VECTOR,SHIP:VELOCITY:SURFACE). //HVvec is the horizontal velocicty vector
  212.  
  213. SET vecDiff TO (LZ:POSITION-IP:POSITION). //vecDiff is the difference (subtraction) between the landing zone (target) and the impact point (where you are headed basically), and vecDiffHoriz just takes only the horizontal component of that vector, the one we need. This vector will be used for all calculations
  214. SET vecDiffHoriz TO VXCL(UP:VECTOR,vecDiff):NORMALIZED. //N=(0,y,0). E=(x,0,z). ErrorN is HVvec-N
  215.  
  216. //i explain the need of this thing below at line 102
  217. SET inverseHVvec TO HVvec*-1. //https://math.stackexchange.com/questions/13261/how-to-get-a-reflection-vector
  218. SET errorVec TO inverseHVvec:NORMALIZED-2* VDOT(inverseHVvec:NORMALIZED,vecDiffHoriz:NORMALIZED) *vecDiffHoriz:NORMALIZED.
  219.  
  220. //i dont 100% remember what this does but its needed so that we can input these vector into some kind of steering information
  221. SET vecDiffHorizOffset TO VCRS(UP:VECTOR,vecDiffHoriz).
  222. SET errorVecOffset TO VCRS(UP:VECTOR,errorVec).
  223.  
  224. SET targetHVRaw TO EQ(30,distanceIPLZ/2,0,1+distanceIPLZ/2,distanceIPLZ). //target horizontal velcity we want towards the target only, which i clamp between a min and max of 40
  225. SET targetHV TO minmax(-40,40,targetHVRaw). //TODO: CHANGE targetHV limits so if need be it should move faster towards LZ
  226.  
  227. SET vecDiffHorizHV TO VDOT(vecDiffHoriz,HVvec). //this here is to calculate how much horizontal velocity do we have towards the target
  228. SET tiltAngleRaw TO YASS_PID_LOOP(0.8,0,0.06,vecDiffHorizHV,targetHV). //pid loop to tilt our vessel which i again clamp between a range i calculate with a simple equation (if the distanceIPLZ is 0 the range is 10, if its 25 the range is 45)
  229. SET tiltAngleRange TO EQ(0,10,25,45,distanceIPLZ).
  230. SET tiltAngle TO minmax(tiltAngleRange*-1,tiltAngleRange,tiltAngleRaw).
  231.  
  232.  
  233. //STEERING
  234. //Sometimes the vessel just accumulates too much error and it starts to pick up horizontal velocity in a different direction and im just telling it if the angle between the horizontal v vector and the vector pointing at the LZ is greater than .1 then you are to steer not anymore towards the LZ but towards its mirrored vector (to basically cancel out the error, until it goes below .1 again). The upper limit of 15 was added because once youre really close to the target it goes way above 15 and you cant keep heading towards that errorvector or youll just fly way off
  235. IF VANG(HVvec,vecDiffHoriz) > .1 AND VANG(HVvec,vecDiffHoriz) < 15 {
  236. SET xy TO errorVec+ANGLEAXIS(tiltAngle-90,errorVecOffset).
  237. SET sm TO "CONVERGING".
  238. }ELSE{
  239. SET xy TO vecDiffHoriz+ANGLEAXIS(tiltAngle-90,vecDiffHorizOffset).
  240. SET sm TO "DIRECT".
  241. }
  242.  
  243. //THROTTLE CONTROL
  244. SET t TO constantTWR(1)+YASS_PID_LOOP(.08,0.00005,.06,SHIP:VERTICALSPEED,0). //doesnt need to be this complex, it is beacuse this is originally a landing script (which originally was a hover from A to B script)
  245.  
  246. SET showVec TO 1.
  247. readouts().
  248. }
Tags: kos ksp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement