Advertisement
Guest User

edited proportional algorithm script

a guest
Oct 9th, 2015
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. //Missile by Peter Sharpe
  2. //For KSP with the kOS mod
  3.  
  4. //YOU MUST MANUALLY TARGET YOUR TARGET BEFORE EXECUTING THIS PROGRAM.
  5.  
  6. //Editable Parameters
  7. wait 5.
  8. set explosionradius to 8.
  9. set DrawVectors to True.
  10.  
  11. //Don't edit these unless you really understand what you're doing!
  12. set iterations to 5.
  13. set initialguess to 15.
  14. set gravcorrection to 0.5.
  15. set dragcorrection to 1.15.
  16.  
  17.  
  18. //Line 26
  19.  
  20. Clearscreen.
  21.  
  22. //Do Targeting
  23. print "Enemy heat signature acquired. Launching...".
  24.  
  25. //Launch Sequence
  26. stage.
  27. lock throttle to 1.
  28. lock steering to up.
  29. wait until alt:radar>100.
  30. print "Launch sequence completed. Now tracking target.".
  31.  
  32. //Set up Staging
  33. //when ship:maxthrust=0 then {stage. If ship:maxthrust>0 {preserve.}.}.
  34.  
  35. //Set up outputs
  36. print "Time to interception:" at (0,10).
  37. print "Distance to Target:" at (0,12).
  38. print "Steering Error:" at (0,14).
  39. print "throttlelock:" at (0,16).
  40.  
  41. //Take steering control, lock it to "steeringangle"
  42. set steeringangle to target:direction.
  43. sas off.
  44. rcs on.
  45. lock steering to steeringangle.
  46.  
  47. //Arm the warhead to trigger with radius of "explosionradius" + 1 frame to decouple.
  48. set ralf to 1.
  49. when target:distance < 500 {stage.}.
  50. print "Warhead armed. Initiating homing algorithm.".
  51.  
  52. //Homing Algorithm, infinite loop
  53. set t to 10.
  54. until 0
  55. {
  56.  
  57.  
  58.  
  59. set rpos to (0-1)*(target:Position).
  60. set rvel to (ship:velocity:surface-target:velocity:orbit).
  61. set gravfactor to gravcorrection.
  62. if target:loaded {set rvel to ship:velocity:surface-target:velocity:surface.}.
  63. if target:altitude>25000 {set rvel to ship:velocity:orbit-target:velocity:orbit.}.
  64. set amag to ship:maxthrust/(ship:mass*9.81).
  65.  
  66. //Solve for t, where 0=at^4+bt^2+ct+d. First get coefficients
  67. set a to 0-((amag)^2)/4.
  68. set b to (rvel:sqrmagnitude).
  69. set c to 2*(rvel*rpos).
  70. set d to (rpos:sqrmagnitude).
  71.  
  72. //Do a few Newton-Raphson iterations:
  73. set timeguesses to list().
  74. timeguesses:add(initialguess).
  75. set position to 0.
  76. until position>=iterations
  77. {
  78. timeguesses:add(timeguesses[position]-(a*timeguesses[position]^4+b*timeguesses[position]^2+c*timeguesses[position]+d)/(4*a*timeguesses[position]^3+2*b*timeguesses[position]+c)).
  79. set position to position+1.
  80. }.
  81. set initialguess to abs(timeguesses[iterations]).
  82.  
  83. //Then calculate your desired direction. There is a correction for drag.
  84. set t to abs(timeguesses[iterations])/dragcorrection.
  85. if altitude>25000 {set t to abs(timeguesses[iterations]). set gravfactor to 0.}.
  86. print t at (22,10).
  87. print target:distance at (20,12).
  88. set steeringvector to v((0-1)*(rpos:x+(rvel:x)*t),(0-1)*(rpos:y+(rvel:y)*t),(0-1)*(rpos:z+(rvel:z)*t)).
  89. set correctedsteeringvector to (9.81*amag*steeringvector:normalized+9.81*gravfactor*(up:vector)).
  90. set steeringangle to correctedsteeringvector:direction.
  91. print vectorangle(steeringvector,ship:facing:vector) at (16,14).
  92.  
  93.  
  94. //Draw Vectors
  95. if DrawVectors=True {
  96. set Steerdraw TO VECDRAWARGS(v(0,0,0), 9.81*v((0-2)*(rpos:x+(rvel:x)*t)/(t^2),(0-2)*(rpos:y+(rvel:y)*t)/(t^2),(0-2)*(rpos:z+(rvel:z)*t)/(t^2)), rgb(0.5,1,0.5),"Algo. Output", 1, true ).
  97. set UPdraw TO VECDRAWARGS(9.81*v((0-2)*(rpos:x+(rvel:x)*t)/(t^2),(0-2)*(rpos:y+(rvel:y)*t)/(t^2),(0-2)*(rpos:z+(rvel:z)*t)/(t^2)), 9.81*gravfactor*(up:vector), rgb(0.5,0.5,1),"Grav. Corr.", 1, true ).
  98. set Vectordraw TO VECDRAWARGS(v(0,0,0), correctedsteeringvector:normalized, rgb(1,0.5,0.5),"Sum", 12.0, true ).
  99. wait 0.01.
  100. }.
  101. }.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement