Ozin

Docking

Aug 28th, 2020 (edited)
4,048
3
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 9.39 KB | None | 3 0
  1. //docking script - !runscript z1fNRuEf
  2. clearscreen.
  3. clearvecdraws().
  4. sas off.
  5.  
  6. if not hastarget { hudtext("Target must be set to a vessel or a dockingport before running this script.", 10, 2, 24, yellow, false). }
  7.  
  8. set dock to true.
  9. set cam to addons:camera:flightcamera.
  10. if not(defined(localport)) { set localport to ship:controlpart. hudtext("Localport set to controlling part: " + localport:title, 10, 2, 24, yellow, false). }
  11. on ship:controlpart { set localport to ship:controlpart. return dock. }
  12. set sep to 5.
  13. set safety to true.
  14. set safetyAngle to 33.
  15. if not ( defined sl ) set sl to 0.
  16. set rotation to 0.
  17. set acc to 0.1.
  18. set fa to facing.
  19. lock offset to v(0,0,0). //alter this to manually offset the target position.
  20. if target:istype("Vessel") set tVes to target. else set tVes to target:ship.
  21.  
  22. function deadband {
  23.     parameter db is 0.05.
  24.     for rcsb in ship:rcs {
  25.         set rcsb:deadband to db.
  26.     }
  27. }
  28. deadband(0).
  29.  
  30.  
  31.  
  32. set axesT to lexicon("fore",0,"aft",0,"star", 0, "port", 0, "top", 0, "bottom", 0).
  33. set axesAcc to lexicon("fore",0,"aft",0,"star", 0, "port", 0, "top", 0, "bottom", 0).
  34.  
  35. function rcsMult {
  36.     parameter mult.
  37.     set fpid to pidloop(4 * (mult/((axesAcc["fore"] + axesAcc["aft"])/2)),0,0,-1,1).
  38.     set tpid to pidloop(4 * (mult/((axesAcc["top"] + axesAcc["bottom"])/2)),0,0,-1,1).
  39.     set spid to pidloop(4 * (mult/((axesAcc["star"] + axesAcc["port"])/2)),0,0,-1,1).
  40. }
  41. function rcsCalc {
  42.     for axisKey in axesT:keys set axesT[axisKey] to 0.
  43.     for rp in ship:rcs {
  44.         for tv in rp:thrustvectors {
  45.             if (rp:foreenabled) {
  46.                 local vd is vdot(facing:vector, tv).
  47.                 if vd > 0 set axesT:fore to axesT:fore + rp:availablethrust * vd.
  48.                 else set axesT:aft to axesT:aft - rp:availablethrust * vd.
  49.             }
  50.             if (rp:STARBOARDENABLED) {
  51.                 local vd is vdot(facing:starvector, tv).
  52.                 if vd > 0 set axesT:star to axesT:star + rp:availablethrust * vd.
  53.                 else set axesT:port to axesT:port - rp:availablethrust * vd.
  54.             }
  55.             if (rp:TOPENABLED) {
  56.                 local vd is vdot(facing:topvector, tv).
  57.                 if vd > 0 set axesT:top to axesT:top + rp:availablethrust * vd.
  58.                 else set axesT:bottom to axesT:bottom - rp:availablethrust * vd.
  59.             }
  60.         }
  61.        
  62.     }
  63.     print "RCS acc: ".
  64.     set minAcc to 9999.
  65.     for axisKey in axesT:keys {
  66.         local axisThrust is axesT[axisKey].
  67.         local axisAcc to axisThrust/mass.
  68.         set minAcc to min(minAcc,axisAcc).
  69.         print "  "+ axisKey + ": " + round(axisThrust/mass,3) + " m/s2".
  70.         set axesAcc[axisKey] to axisAcc.
  71.     }
  72.     set acc to max(0.005,minAcc * 0.6).
  73.     rcsMult(2).
  74. }
  75.  
  76.  
  77. rcsCalc().
  78. on ship:controlpart { rcsCalc(). return dock. }
  79.  
  80. set vd_fore to vecdraw(v(0,0,0), v(0,0,0), yellow, "", 1, TRUE, 0.1).
  81. set vd_star to vecdraw(v(0,0,0), v(0,0,0), magenta, "", 1, TRUE, 0.1).
  82. set vd_top to vecdraw(v(0,0,0), v(0,0,0), cyan, "", 1, TRUE, 0.1).
  83.  
  84. set tBounds to tVes:bounds.
  85. set sBounds to ship:bounds.
  86. set sphere_radius to 3 + sBounds:extents:mag + tBounds:extents:mag.
  87. when true then {
  88.     set fa to choose target:portfacing if target:istype("Dockingport") else target:facing.
  89.     set localPortPos to choose localport:nodeposition if localport:istype("Dockingport") else ship:position.
  90.     set targetPortPos to choose target:nodeposition if target:istype("Dockingport") else target:position.
  91.     //set rel_v to velocity:orbit - tVes:velocity:orbit.
  92.     set rel_v to velocityat(ship,time:seconds):orbit - velocityat(tVes,time:seconds):orbit.
  93.    
  94.     set posAlignment to round(vang(fa:vector, localPortPos-targetPortPos),2).
  95.  
  96.     if safety and posAlignment > safetyAngle and (localPortPos-targetPortPos):mag > 3 {
  97.         set tCent to tBounds:abscenter.
  98.         set wv_pos to tCent + tCent:normalized * -sphere_radius + vxcl(tCent,targetPortPos + fa:vector * sphere_radius):normalized * max(0, 10 + min(0,tCent:mag - sphere_radius)).
  99.     }
  100.     else {
  101.         set wv_pos to (targetPortPos + offset + fa:vector * sep) - localPortPos.
  102.     }
  103.  
  104.    
  105.     set wanted_v to wv_pos:normalized * min(sl , sqrt(2 * acc * max(wv_pos:mag,0.0001))).
  106.     if wv_pos:mag < 1 set wanted_v:mag to min(wanted_v:mag, wv_pos:mag * 0.5).
  107.     set wanted_v to wanted_v + vcrs(tves:angularvel, -tves:position).
  108.     set ve to wanted_v - rel_v.
  109.  
  110.     set rcs_v to v(
  111.         -spid:update(time:seconds, vdot(facing:starvector,ve)),
  112.         -tpid:update(time:seconds,vdot(facing:topvector,ve)),
  113.         -fpid:update(time:seconds,vdot(facing:vector,ve))
  114.     ).
  115.     set ship:control:translation to rcs_v.
  116.    
  117.     set vd_pos to cam:position:normalized * (cam:distance - 3).
  118.     set vd_fore:start to vd_pos.
  119.     set vd_star:start to vd_pos.
  120.     set vd_top:start to vd_pos.
  121.    
  122.     set vd_fore:vec to facing:vector * rcs_v:z.
  123.     set vd_star:vec to facing:starvector * rcs_v:x.
  124.     set vd_top:vec to facing:topvector * rcs_v:y.
  125.    
  126.     return dock.
  127. }
  128.  
  129. lock steering to lookdirup(-fa:vector,angleaxis(rotation, fa:vector) * fa:topvector).
  130. lock distanceBetweenPorts to (targetPortPos-localPortPos):mag.
  131.  
  132. function approach {
  133.     set sl to max(0.2, sl).
  134.     hudtext("Approaching. Set SL to 0 to cancel", 10, 2, 36, yellow, false).
  135.     when true then {  set sep to max(-0.1, distanceBetweenPorts-1). return sl > 0. }
  136.     on ship:parts:length { exitDock(). hudtext("Docked? Or something fell off. Done", 10, 2, 24, yellow, false). }
  137. }
  138. function exitDock {
  139.     rcs off. dock off. unlock steering. menu:dispose. set ship:control:translation to v(0,0,0). print "Exiting docking script.". clearvecdraws().
  140. }
  141. function loadDist {
  142.     parameter dist is 3000.
  143.     SET target:loaddistance:ORBIT:UNLOAD TO dist + 100.
  144.     SET target:loaddistance:ORBIT:LOAD TO dist + 50.
  145.     WAIT 0.
  146.     SET target:loaddistance:ORBIT:PACK TO dist + 90.
  147.     SET target:loaddistance:ORBIT:UNPACK TO dist.
  148.     wait 0.
  149. }
  150.  
  151. set portI to 0.
  152. local prt is 0.
  153. function nextPort {
  154.     if not(hastarget) return.
  155.    
  156.     if target:typename = "Vessel" {
  157.         if not(target:unpacked) loadDist(target:distance + 200).
  158.         set target to target:dockingports[0].
  159.     }
  160.     local ports is target:ship:dockingports.
  161.     local cnt to 0.
  162.     local done is false.
  163.     until done {
  164.         set prt to ports[portI].
  165.         set portI to portI + 1.
  166.         if portI >= ports:length set portI to 0.
  167.         if localport:typename = "DockingPort" and prt:state = "Ready" and prt:nodetype = localport:nodetype {
  168.             done on.
  169.         } else if localport:typename <> "DockingPort" and prt:state = "Ready" {
  170.             done on.
  171.         } else if cnt > ports:length {
  172.             done on.
  173.         }
  174.         set cnt to cnt + 1.
  175.     }
  176.     if cnt <= ports:length + 1 {
  177.         trackcam off.
  178.         set camVel to (cam:position- target:position):normalized.
  179.         set target to prt.
  180.         set camOldPos to cam:position.
  181.         set cam:target to prt.
  182.         wait 0.
  183.         //set cam:position to prt:portfacing:vector * 20 + prt:position.
  184.         set cam:position to camOldPos.
  185.        
  186.        
  187.         when true then {
  188.             set camTargetPosRel to ((prt:portfacing:vector * 10 + prt:nodeposition) - cam:position).
  189.             set camVel to camVel + camTargetPosRel:normalized * 0.08.
  190.             set camVel:mag to min(1, camTargetPosRel:mag / 20).
  191.             set cam:position to cam:position + camVel * 0.75.
  192.             return camTargetPosRel:mag > 1.
  193.         }
  194.     }
  195. }
  196.  
  197. set trackCam to false.
  198. when trackCam and hastarget then { set cam:position to -target:position:normalized * 20 * r(15,10,0). return dock. }
  199.  
  200. function resetCam {
  201.     trackcam off.
  202.     set cam:target to localport. wait 0.
  203.     set cam:position to (target:position:normalized * -20) * r(20,10,0).
  204. }
  205.  
  206.  
  207.  
  208.  
  209. set menu to gui(300, 30). Set menu:x to -60. set menu:y to 400. menu:show.
  210. menu:addlabel("<b><color=yellow>Docking parameters</color></b>").
  211. set m1 to menu:addlabel("SL:").
  212. set mAcc to menu:addlabel("Acc:").
  213. set m2 to menu:addlabel("SEP:").
  214. set mRot to menu:addlabel("Extra Port Rotation:").
  215. set m3 to menu:addlabel("Localport:").
  216. set m4 to menu:addlabel("Target:").
  217.  
  218. set m8 to menu:addlabel("Steering Alignment: ").
  219. set mPosAlignment to menu:addlabel("Positional alignment:").
  220. set m5 to menu:addlabel("Distance fwd:").
  221. set m6 to menu:addlabel("Distance side:").
  222. set mTrack to menu:addlabel("TrackCam:").
  223.  
  224. menu:addlabel("Functions: <i>nextport(), rcsMult(#), approach(), resetCam()</i>").
  225. for txt in menu:widgets set txt:style:fontsize to 18.
  226.  
  227. on time:second {
  228.     set sideDist to round(vxcl(facing:vector, targetPortPos-localPortPos):mag,2).
  229.     set angError to vang(-facing:vector, fa:vector).
  230.     set m1:text to "SL: <color=white>" + round(sl,2) + "m/s</color>".
  231.     set mAcc:text to "Acc: <color=white>" + round(acc,3) + "m/s2</color>".
  232.     set m2:text to "SEP: <color=white>" + round(sep,2) + "m</color>".
  233.     set mRot:text to "Rotation: <color=white>" + rotation + "</color>" + char(0176).
  234.    
  235.     if (localport:istype("Dockingport")) set m3:text to "Localport: <color=white>" + localport + "</color>".
  236.     else set m3:text to "Localport: <color=orange>" + localport + "</color>".
  237.    
  238.     if (target:istype("Dockingport")) set m4:text to "Target: <color=white>" + target + "</color>".
  239.     else set m4:text to "Target: <color=orange>" + target + "</color>".
  240.    
  241.     set m5:text to "Distance: " + round(distanceBetweenPorts,2) + "m".
  242.     if sideDist <= 0.1 set m6:text to "Distance side: " + sideDist + "m".
  243.     else set m6:text to "Distance side: <color=orange>" + sideDist + "m</color>".
  244.  
  245.     if angError <= 5 set m8:text to "Steering Alignment: " + round(angError,1) + char(0176).
  246.     else set m8:text to "Steering Alignment: <color=orange>" + round(angError,1) + char(0176) + "</color>".
  247.    
  248.    
  249.     if posAlignment <= 5 set mPosAlignment:text to "Positional alignment: " + posAlignment + char(0176).
  250.     else set mPosAlignment:text to "Positional alignment: <color=orange>" + posAlignment + char(0176) + "</color>".
  251.    
  252.     set mTrack:text to "TrackCam: <color=white>" + trackCam + "</color>".
  253.     return menu:visible.
  254. }
  255.  
  256.  
Add Comment
Please, Sign In to add comment