Advertisement
Ozin

Plane auto-pitch & roll

Aug 31st, 2020 (edited)
1,338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 6.05 KB | None | 0 0
  1. //auto pitch & roll for planes. Lock steering as usual, will roll and pitch for you.
  2. // !runscript DbxqrVs5
  3. HUDTEXT("Running script: plane auto-pitch & roll",20,2,22,white,false).
  4. set autoPitch to true.
  5. set autopilot to true.
  6. set ppid to pidloop(2.0,0.7,0,-1,1).
  7. set rpid to pidloop(0.5,0.05,0,-1,1).
  8. set aoalimit to 6.
  9. set maxclimb to 15.
  10. set mul to 3. set limit to 50.
  11. set talt to round(altitude).
  12. set takeoffspeed to 50.
  13. set altNoBank to 20.
  14. set toggledAutopilot to true.
  15.  
  16. set heightMargin to 100. //minimum altitude above terrain when using keepalt()
  17. set dst to 1.
  18. set terSlope to 0.
  19. set terSlope2 to 0.
  20. set terTime to time:seconds.
  21. set terMax to 12. //amount of seconds ahead to sample terrain.
  22.  
  23. function disableYaw {
  24.     if ship:status = "flying" {
  25.          set ship:control:yaw to 0.0001.
  26.     } else {
  27.         set ship:control:yaw to 0.
  28.          when ship:status = "flying" then set ship:control:yaw to 0.0001.
  29.     }
  30. }
  31. disableYaw().
  32. when true then {
  33.     if autopilot {
  34.     //pitch
  35.     set vp to vang(up:vector, velocity:surface).
  36.     set sp to max(vp - aoalimit,min(vp + aoalimit,vang(up:vector,steeringmanager:target:vector))).
  37.     set ppid:setpoint to max(-0.1,(vp - sp) / (40 + airspeed / 30)).
  38.     set ship:control:pitch to ppid:update(time:seconds, -vdot(vxcl(up:vector,facing:starvector),ship:angularvel)).
  39.    
  40.     //roll
  41.     set stv to steeringmanager:target:vector.
  42.     set angE to max(0,vang(vxcl(up:vector,stv),vxcl(up:vector, velocity:surface)) - 0) ^ 0.85.
  43.     if vdot(vxcl(velocity:surface,facing:starvector),stv) < 0 set angE to -angE.
  44.     set tr to choose 0 if alt:radar < altNoBank or (vp - sp < -2) else min(limit,max(-limit,angE * mul)).
  45.     set rpid:setpoint to (tr - (vang(facing:starvector,up:vector) - 90)) / 90.
  46.     set ship:control:roll to rpid:update(time:seconds, vdot(-facing:vector, ship:angularvel)).
  47.    
  48.     if not(toggledAutopilot)  {
  49.         toggledAutopilot on.
  50.         set ship:control:yaw to 0.0001.
  51.     }
  52.     }
  53.     else if toggledAutopilot {
  54.         set ship:control:pitch to 0. set ship:control:roll to 0. set ship:control:yaw to 0. toggledAutopilot off. steeringmanager:resetpids().
  55.     }
  56.     return autoPitch.
  57. }
  58.  
  59.  
  60. function autoThrottle {
  61.     if not(defined sl) set sl to round(airspeed).
  62.         set tpid to pidloop(0.1,0.002,0.2,0,1).
  63.     lock throttle to tpid:update(time:seconds,vdot(facing:vector,ship:velocity:surface) - sl).
  64.         HUDTEXT("Auto-throttle enabled, change 'SL'",15,2,18,yellow,false).
  65. }
  66. function takeoff {
  67.     lock x to round(body:geopositionof(facing:vector * 1000):heading).
  68.     lock p to choose 8 if airspeed > takeoffspeed else (90-vang(up:vector, velocity:surface)). lock steering to heading(x,p).
  69.     lock throttle to 1. if ship:maxthrust = 0 stage.
  70.     disableYaw().
  71.     brakes off.
  72. }
  73. function keepAlt {
  74.     parameter taltIn is round(altitude).
  75.     set talt to taltIn.
  76.     lock p to max(-25,min(maxClimb, (talt-altitude) / (airspeed/8))).
  77.     lock steering to heading(x,max(p, avoidTerrain())).
  78. }
  79.  
  80. function avoidTerrain {
  81.     set dst to choose 1 if dst > terMax else dst + 0.1.
  82.     set gi to body:geopositionof(velocity:surface * dst).
  83.     set pi to choose gi:position if gi:terrainheight > 0 else gi:altitudeposition(0).
  84.  
  85.     set tempSlope to 90 - vang(up:vector, pi + up:vector * heightMargin).
  86.     if tempSlope > terSlope {
  87.         set terSlope to tempSlope.
  88.         set terTime to time:seconds.
  89.     }
  90.     else {
  91.         set terSlope2 to max(tempSlope, terSlope2 * 0.999 + 0.001 * tempSlope).
  92.         if time:seconds > terTime + 3 {
  93.             set terTime to time:seconds. set terSlope to terSlope2. set terSlope2 to max(-30, terSlope2 - 25).
  94.         }
  95.     }
  96.     return terSlope.
  97. }
  98.  
  99. set vesselSettings to lexicon().
  100. if homeconnection:isconnected and exists("0:/json/vesselsettings.json") {
  101.     set vesselSettings to readjson("0:/json/vesselsettings.json").
  102. }
  103.  
  104. if vesselSettings:haskey(ship:name) {
  105.     print "Found vessel settings in json:".
  106.     set cfg to vesselSettings[ship:name].
  107.     print cfg.
  108.     set ppid:kp to cfg["ppid_kp"].
  109.     set ppid:ki to cfg["ppid_ki"].
  110.     set ppid:kd to cfg["ppid_kd"].
  111.     set rpid:kp to cfg["rpid_kp"].
  112.     set rpid:ki to cfg["rpid_ki"].
  113.     set rpid:kd to cfg["rpid_kd"].
  114.     set maxclimb to cfg["maxclimb"].
  115.     set takeoffspeed to cfg["takeoffspeed"].
  116.     set mul to cfg["mul"].
  117.     set limit to cfg["limit"].
  118. }
  119. function saveSettings {
  120.     set vesselSettings[ship:name] to lexicon(
  121.         "ppid_kp", ppid:kp, "ppid_ki", ppid:ki, "ppid_kd", ppid:kd,
  122.         "rpid_kp", rpid:kp, "rpid_ki", rpid:ki, "rpid_kd", rpid:kd,
  123.         "maxclimb", maxclimb,
  124.         "takeoffspeed", takeoffspeed,
  125.         "mul", mul,
  126.         "limit", limit
  127.     ).
  128.     if (homeconnection:isconnected) {
  129.         print "Saving vessel settings to 0:/json/vesselsettings.json".
  130.         print vesselSettings[ship:name].
  131.         writejson(vesselSettings, "0:/json/vesselsettings.json").
  132.     }
  133.     else {
  134.         HUDTEXT("Error saving: no connection",20,2,22,red,true).
  135.     }
  136. }
  137.  
  138.  
  139.  
  140. set campitch to 8.
  141. set trackCam to false.
  142. set cam to addons:camera:flightcamera.
  143. set shipsize to ship:bounds:size:mag.
  144. when trackCam then {
  145.     set cam:position to angleaxis(campitch, facing:starvector) * (facing:vector * (-2 - shipsize  * 1.25)).
  146.     return true.
  147. }
  148.  
  149.  
  150. set menu to gui(180, 30). Set menu:x to -30. set menu:y to 400. menu:show.
  151. menu:addlabel("<b><color=yellow>Auto pitch & roll</color></b>").
  152. set m1 to menu:addlabel("SL (speedlimit):").
  153. set m2 to menu:addlabel("TAlt:").
  154. set m3 to menu:addlabel("Limit:").
  155. set m4 to menu:addlabel("Mul:").
  156. set m5 to menu:addlabel("MaxClimb:").
  157. set m6 to menu:addlabel("Trackcam:").
  158. set mlast to menu:addlabel("Functions: autothrottle(), takeoff(), keepAlt(altitude), savesettings()").
  159.  
  160. menu:addlabel("<i>'menu:hide'</i> to close").
  161. for txt in menu:widgets { set txt:style:fontsize to 10. set txt:style:padding:v to 1. }
  162.  
  163. on time:second {
  164.     if (defined sl)
  165.         set m1:text to "SL (speedlimit): <color=white>" + sl + "</color>m/s".
  166.     set m2:text to "TAlt: <color=white>" + talt + "</color>m".
  167.     set m3:text to "Limit: <color=white>" + limit + "</color> max bank".
  168.     set m4:text to "Mul: <color=white>" + mul + "</color>x (roll sensitivity)".
  169.     set m5:text to "MaxClimb: <color=white>" + maxclimb + "</color>" + char(176).
  170.     set m6:text to "Trackcam: <color=white>" + trackcam + "</color>".
  171.     return menu:visible.
  172. }
  173.  
  174.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement