sumguytwitches

SG-22 Control

Jun 15th, 2022 (edited)
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. for i in range(terminal:height-3) Print "":padright(terminal:width).
  2.  
  3.  
  4. //////////////////// VARS //////////////////
  5.  
  6.  
  7. set taralt to 100.
  8.  
  9. lock x to body:geopositionof(ship:facing:vector):heading.
  10.  
  11. lock p to 0.
  12.  
  13. lock sl to 0.
  14.  
  15. lock vtolrpmtarget to min(460,max(0,((taralt-alt:radar)-verticalspeed) * 460)).
  16. lock pushrpmtarget to min(460,max(0,throttle * 460)).
  17. set speedPID to pidloop(1.5, 0, 0, -5, 5).
  18. set steeringmanager:rollcontrolanglerange to 180.
  19.  
  20.  
  21. set servos to ship:modulesnamed("ModuleRoboticRotationServo").
  22.  
  23. set proprad to 5.8.
  24. set rotormods to ship:modulesnamed("ModuleRoboticServoRotor").
  25. set rotors to list().
  26. for rm in rotormods {
  27.  
  28. local rotor to lexicon().
  29. set rotor["part"] to rm:part.
  30. set rotor["mod"] to rm.
  31. set rotor["job"] to choose "push" if rm:part:parent:tag:contains("push") else "vtol".
  32. print "Found rotor module for job of " + rotor:job.
  33. set rotor["mode"] to "flight".
  34.  
  35. set rotor["blades"] to list().
  36. for bm in rm:part:ModulesNamed("ModuleControlSurface") {
  37. local b to bm:part.
  38.  
  39. local blade to lexicon().
  40. set blade["mod"] to bm.
  41. set blade["offset"] to vdot(-b:Facing:starvector, b:position - b:parent:position).
  42. set blade["proprad"] to proprad.
  43. set blade["angleratio"] to 2 * constant:pi * (blade:proprad + blade:offset)/60.
  44.  
  45. bm:setfield("deploy", true).
  46.  
  47. rotor["blades"]:add(blade).
  48. }
  49.  
  50. rotors:add(rotor).
  51. if rm:hasfield("torque limit(%)") rm:setfield("torque limit(%)", 100).
  52. }
  53. print "rotors setup".
  54.  
  55. set aoatopressure to 9.
  56.  
  57. set taraoa to 4.68.
  58. set deploycopy to 9.
  59.  
  60. set takeoff to true.
  61. set flightmode to "vtol".
  62.  
  63. function SetAngle {
  64. parameter targetangle.
  65. if flightmode = "debug" print targetangle at (0,0).
  66. for s in servos if s:hasfield("target angle") s:setfield("target angle", targetangle).
  67. }
  68.  
  69. function stop {
  70. print "stopping".
  71. lock vtolrpmtarget to throttle * 460.
  72. lock pushrpmtarget to 0.
  73. toggle ag1.
  74. set x2 to x.
  75. lock p to -verticalspeed * 3.
  76.  
  77. lock steering to heading(x2,p).
  78. lock throttle to choose 1 if verticalspeed > 0 else -verticalspeed*3.
  79. set stopping to true.
  80. when stopping then {
  81. setangle(max(-90,min(90,-vdot(ship:velocity:surface,ship:facing:vector)))).
  82. return stopping.
  83. }
  84.  
  85.  
  86. when groundspeed < 10 then {
  87. set stopping to false.
  88. setangle(0).
  89. ship:partsdubbedpattern("probo")[0]:controlfrom().
  90. lock steering to lookdirup(up:Vector*10-ship:Velocity:surface*0.1, heading(x+180,0):vector).
  91. lock throttle to -verticalspeed - min(20,alt:Radar/10).
  92. }
  93. }
  94.  
  95. function go {
  96. print "going".
  97. lock vtolrpmtarget to max(0,min(460,throttle * 460)).
  98. lock vtolrpmtarget to max(0,min(460,throttle * 460)).
  99. set taralt to alt:radar + 100.
  100. toggle ag1.
  101. lock p to min(15,max(-15, -verticalSpeed*1.7 + (max(taralt,alt:radar - body:geopositionof(ship:velocity:surface*10):terrainheight + taralt) - alt:radar)/2.5)).
  102. lock steering to heading(x, p).
  103. lock throttle to ship:electriccharge-100.
  104. set flightmode to "stol".
  105.  
  106. }
  107.  
  108. function save {
  109. print "waiting for valid save state".
  110. when (ship:Status = "Landed" or ship:status = "Splashed") and ship:velocity:surface:mag < 0.1 then {
  111. print "saving".
  112. kuniverse:quicksaveto(ship:name).
  113. print "saved to '" + ship:name + "'".
  114. }
  115. }
  116.  
  117. function vtoltotarget {
  118. parameter tgt is target:geoposition.
  119. lock x to tgt:heading.
  120.  
  121. for rm in rotors {
  122. if rm:job = "push" { set rm:mode to "vtol". rm:mod:setfield("torque limit(%)",0).}
  123. }
  124.  
  125. when tgt:distance < 1000 then set x to tgt:heading.
  126. set vtoltargeting to true.
  127. lock talt to max(ship:geoposition:terrainheight + taralt, tgt:terrainheight + 20).
  128. SetAngle(0).
  129. lock vtolrpmtarget to ((-verticalspeed*1.7 + max(-10,min(20,talt-altitude)))/5)*460.
  130. lock pushrpmtarget to 0.
  131.  
  132. lock sl to 5.
  133. lock tv to tgt:position:normalized * min(sl,(tgt:distance/6) ^ 0.85).
  134.  
  135. set relVelVec to vxcl(up:vector, tv - velocity:surface) / 3.5.
  136. set relVelVec:mag to min(sl,relVelVec:mag).
  137. set st to lookdirup(up:vector * 10 + relVelVec, heading(x+180,0):vector).
  138.  
  139. toggle ag2.
  140. lock steering to st.
  141.  
  142. when true then {
  143. set relVelVec to vxcl(up:vector, tv - velocity:surface) / 3.5.
  144. set relVelVec:mag to min(sl,relVelVec:mag).
  145. set st to lookdirup(up:vector * 10 + relVelVec, heading(x+180,0):vector).
  146. return vtoltargeting.
  147. }
  148.  
  149. when (ship:geoposition:position - tgt:position):mag < 0.1 and ship:groundspeed < 0.1 then {
  150. lock vtolrpmtarget to (-verticalspeed - 1)*460.
  151. wait 1.
  152. When ship:verticalspeed > 0 then lock vtolrpmtarget to 0.
  153. }
  154.  
  155. }
  156. when true then {
  157. set vtolrpmcopy to max(0,min(460,vtolrpmtarget)).
  158. set pushrpmcopy to max(0,min(460,pushrpmtarget)).
  159.  
  160. if ship:partstaggedpattern("Pusher"):length > 0 {
  161. set speedCopy to speedPID:update(time:seconds, vdot(ship:velocity:surface, ship:partstaggedpattern("Pusher")[0]:facing:vector) - sl).
  162. }
  163. for rotor in rotors {
  164. if rotor["job"] = "vtol"
  165. {
  166. rotor:mod:setfield("rpm limit", vtolrpmcopy ).
  167. for blade in rotor:blades {
  168. set calcAirspeed to blade["angleratio"] * vtolrpmcopy.
  169. set deploycopy to max(3, taraoa + arctan2(vdot(rotor:part:parent:facing:vector,ship:Velocity:surface), calcAirspeed)).
  170. blade:mod:setfield("deploy angle", deploycopy).
  171. }
  172. }
  173.  
  174. if rotor["job"] = "push" {
  175. if rotor["mode"] = "flight" {
  176. rotor:mod:setfield("rpm limit", pushrpmcopy ).
  177. for blade in rotor:blades {
  178. set calcAirspeed to blade["angleratio"] * pushrpmcopy.
  179. set deploycopy to max(3, taraoa + arctan2(airspeed, calcAirspeed)).
  180. blade:mod:setfield("deploy angle", deploycopy).
  181. }
  182. }
  183. if rotor["mode"] = "stopping" or rotor["mode"] = "vtol" {
  184. rotor:mod:setfield("rpm limit", 460 ).
  185. for blade in rotor:blades {
  186. set calcAirspeed to blade["angleratio"] * 460.
  187. set deploycopy to speedCopy + arctan2(airspeed, calcAirspeed).
  188. blade:mod:setfield("deploy angle", deploycopy).
  189. }
  190. }
  191. }
  192. }
  193.  
  194. if(flightmode = "stol")
  195. {
  196. SetAngle(max(0,min(90,(alt:radar/taralt)*90))).
  197. }
  198.  
  199.  
  200. return true.
  201. }
  202.  
  203. when airspeed > 100 and flightmode = "stol" then {set flightmode to "plane". setangle(90). return true.}
  204.  
  205.  
  206. if (ship:Status = "Landed" or ship:status = "Splashed" or ship:velocity:surface:mag < 10) {
  207. print "found landed or splashed, taking off".
  208. lock x to body:geopositionof(ship:facing:vector):heading.
  209. if hastarget lock x to target:heading.
  210. lock p to 0.
  211. toggle ag1.
  212. lock steering to heading(x,p).
  213. setangle(0).
  214. //lock throttle to (taralt-alt:radar)/10-verticalspeed.
  215. }
  216.  
  217. if (ship:Status = "flying" and ship:velocity:surface:mag >= 10) {
  218. print "found flying".
  219. set flightmode to "plane".
  220. set currheading to body:geopositionof(ship:facing:vector):heading.
  221. lock x to currheading.
  222. lock p to -verticalspeed*3.
  223. toggle ag1.
  224. lock steering to heading(x,p).
  225. setangle(90).
  226. lock throttle to ship:electriccharge - 100.
  227. lock pushrpmtarget to min(460,max(0,throttle * 460)).
  228. lock vtolrpmtarget to min(460,max(0,throttle * 460)).
  229. }
Add Comment
Please, Sign In to add comment