Advertisement
dekyos

functions.ks

Jan 7th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.44 KB | None | 0 0
  1. function setHoverPIDLOOPS{
  2.     //Controls altitude by changing climbPID setpoint
  3.     SET hoverPID TO PIDLOOP(1, 0.01, 0.0, -50, 50).
  4.     //Controls vertical speed
  5.     SET climbPID TO PIDLOOP(0.1, 0.3, 0.005, 0, 1).
  6.     //Controls horizontal speed by tilting rocket
  7.     SET eastVelPID TO PIDLOOP(3, 0.01, 0.0, -20, 20).
  8.     SET northVelPID TO PIDLOOP(3, 0.01, 0.0, -20, 20).
  9.      //controls horizontal position by changing velPID setpoints
  10.     SET eastPosPID TO PIDLOOP(1700, 0, 100, -40,40).
  11.     SET northPosPID TO PIDLOOP(1700, 0, 100, -40,40).
  12. }
  13. function sProj { //Scalar projection of two vectors.
  14.     parameter a.
  15.     parameter b.
  16.     if b:mag = 0 { PRINT "sProj: Divide by 0. Returning 1". RETURN 1. }
  17.     RETURN VDOT(a, b) * (1/b:MAG).
  18. }
  19.  
  20. function cVel {
  21.     local v IS SHIP:VELOCITY:SURFACE.
  22.     local eVect is VCRS(UP:VECTOR, NORTH:VECTOR).
  23.     local eComp IS sProj(v, eVect).
  24.     local nComp IS sProj(v, NORTH:VECTOR).
  25.     local uComp IS sProj(v, UP:VECTOR).
  26.     RETURN V(eComp, uComp, nComp).
  27. }
  28. function updateHoverSteering{
  29.     SET cVelLast TO cVel().
  30.     SET eastVelPID:SETPOINT TO eastPosPID:UPDATE(TIME:SECONDS, SHIP:GEOPOSITION:LNG).
  31.     SET northVelPID:SETPOINT TO northPosPID:UPDATE(TIME:SECONDS,SHIP:GEOPOSITION:LAT).
  32.     LOCAL eastVelPIDOut IS eastVelPID:UPDATE(TIME:SECONDS, cVelLast:X).
  33.     LOCAL northVelPIDOut IS northVelPID:UPDATE(TIME:SECONDS, cVelLast:Z).
  34.     LOCAL eastPlusNorth is MAX(ABS(eastVelPIDOut), ABS(northVelPIDOut)).
  35.     SET steeringPitch TO 90 - eastPlusNorth.
  36.     LOCAL steeringDirNonNorm IS ARCTAN2(eastVelPID:OUTPUT, northVelPID:OUTPUT). //might be negative
  37.     if steeringDirNonNorm >= 0 {
  38.         SET steeringDir TO steeringDirNonNorm.
  39.     } else {
  40.         SET steeringDir TO 360 + steeringDirNonNorm.
  41.     }
  42.     LOCK STEERING TO HEADING(steeringDir,steeringPitch).
  43. }
  44. function setHoverTarget{
  45.     parameter lat.
  46.     parameter lng.
  47.     SET eastPosPID:SETPOINT TO lng.
  48.     SET northPosPID:SETPOINT TO lat.
  49. }
  50. function setHoverAltitude{ //set just below landing altitude to touchdown smoothly
  51.     parameter a.
  52.     SET hoverPID:SETPOINT TO a.
  53. }
  54. function setHoverDescendSpeed{
  55.     parameter a.
  56.     SET hoverPID:MAXOUTPUT TO a.
  57.     SET hoverPID:MINOUTPUT TO -1*a.
  58.     SET climbPID:SETPOINT TO hoverPID:UPDATE(TIME:SECONDS, SHIP:ALTITUDE). //control descent speed with throttle
  59.     SET thrott TO climbPID:UPDATE(TIME:SECONDS, SHIP:VERTICALSPEED).   
  60. }
  61. function setHoverMaxSteerAngle{
  62.     parameter a.
  63.     SET eastVelPID:MAXOUTPUT TO a.
  64.     SET eastVelPID:MINOUTPUT TO -1*a.
  65.     SET northVelPID:MAXOUTPUT TO a.
  66.     SET northVelPID:MINOUTPUT TO -1*a.
  67. }
  68. function setHoverMaxHorizSpeed{
  69.     parameter a.
  70.     SET eastPosPID:MAXOUTPUT TO a.
  71.     SET eastPosPID:MINOUTPUT TO -1*a.
  72.     SET northPosPID:MAXOUTPUT TO a.
  73.     SET northPosPID:MINOUTPUT TO -1*a.
  74. }
  75. function setThrottleSensitivity{
  76.     parameter a.
  77.     SET climbPID:KP TO a.
  78. }
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. function calcDistance { //Approx in meters
  86.     parameter geo1.
  87.     parameter geo2.
  88.     return (geo1:POSITION - geo2:POSITION):MAG.
  89. }
  90. function geoDir {
  91.     parameter geo1.
  92.     parameter geo2.
  93.     return ARCTAN2(geo1:LNG - geo2:LNG, geo1:LAT - geo2:LAT).
  94. }
  95. function updateMaxAccel {
  96.     SET g TO constant:G * BODY:Mass / BODY:RADIUS^2.
  97.     SET maxAccel TO (SHIP:AVAILABLETHRUST) / SHIP:MASS - g. //max acceleration in up direction the engines can create
  98. }
  99. function getPhaseAngleToTarget{
  100.     parameter targetBody.
  101.     set shippos to SHIP:VELOCITY:ORBIT.
  102.     set targetpos to targetBody:orbit:position.
  103.     return vang(shippos,targetpos).
  104. }
  105. function getKerbinOrbitAngleToTarget{
  106.     parameter targetBody.
  107.     set kerbinpos to Body("Kerbin"):position.
  108.     set targetpos to targetBody:orbit:position.
  109.     //SET drawVelocityVector1 TO VECDRAW(
  110.       //V(0,0,0),kerbinpos,
  111.       //RGB(0,0,1),"",1.0,TRUE,1
  112.     //).
  113.     return vang(kerbinpos,targetpos).
  114. }
  115.  
  116. function getVectorRadialin{
  117.     SET normalVec TO getVectorNormal().
  118.     return vcrs(ship:velocity:orbit,normalVec).
  119. }
  120. function getVectorRadialout{
  121.     SET normalVec TO getVectorNormal().
  122.     return -1*vcrs(ship:velocity:orbit,normalVec).
  123. }
  124. function getVectorNormal{
  125.     return vcrs(ship:velocity:orbit,-body:position).
  126. }
  127. function getVectorAntinormal{
  128.     return -1*vcrs(ship:velocity:orbit,-body:position).
  129. }
  130. function getVectorSurfaceRetrograde{
  131.     return -1*ship:velocity:surface.
  132. }
  133. function getVectorSurfacePrograde{
  134.     return ship:velocity:surface.
  135. }
  136. function getOrbitLongitude{
  137.     return MOD(OBT:LAN + OBT:ARGUMENTOFPERIAPSIS + OBT:TRUEANOMALY, 360).
  138. }
  139. function getBodyAscendingnodeLongitude{
  140.     return SHIP:ORBIT:LONGITUDEOFASCENDINGNODE.
  141. }
  142. function getBodyDescendingnodeLongitude{
  143.     return SHIP:ORBIT:LONGITUDEOFASCENDINGNODE+180.
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement