Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // kOS lander script
- // calculates desired speed at altitude until 50 m AGL
- // gear down at 100 m AGL
- clearscreen.
- sas on.
- set sasmode to "retrograde".
- print" Lander script".
- print" Landing on " + ship:body:name.
- print" Altitude AGL = ".
- print" Desired speed = ".
- print" Actual speed = ".
- print" Speed Error = ".
- print" ".
- print" Throttle Setting = ".
- // load PID functions
- run lib_pid.
- set ship:control:pilotmainthrottle to 0.
- // Call to update the display of numbers.
- function display_block {
- parameter startCol, startRow. // define where the block of text should be positioned
- print round(alt:radar,2) + " m " at (startCol,startRow).
- print round(seekSpeed,2) + " m/s " at (startCol,startRow+1).
- print round(currentSpeed,2) + " m/s " at (startCol,startRow+2).
- print round(seekSpeed - currentSpeed,2) + " m/s " at (startCol,startRow+3).
- print round(myth,3) + " " at (startCol,startRow+5).
- }.
- // Start with 0 throttle.
- set myTh to 0.
- lock throttle to myTh.
- // Initialize PID.
- set myPID to PID_init( 0.20, 0.08, 0.1, 0, 1 ). // Kp, Ki, Kd, min, max control range vals.
- gear on. gear off. // on then off because of the weird KSP 'have to hit g twice' bug.
- // desired speed at final altitude.
- set finalSpeed to 1.
- set finalAltitude to 250.
- // desired deceleration in m/s.
- set shipG to 10.
- // current gravity.
- lock g to ship:body:mu / (ship:body:radius^2+ alt:radar).
- //lock g to ship:body:mu / ship:body:radius^2.
- // current radar altitude.
- lock radarAlt to alt:radar.
- // current speed.
- lock currentSpeed to ship:velocity:surface:mag.
- // in runstate 0 we coast to deceleration altitude.
- set runState to 0.
- until runstate > 0 {
- set seekSpeed to sqrt(finalSpeed^2 + 2 * (shipG+g) * (radarAlt - finalAltitude) ).
- if seekSpeed < finalSpeed { set seekSpeed to finalSpeed.}
- // currentSpeed and seekSpeed are reversed due to vector math.
- set myTh to PID_seek( myPID, currentSpeed, seekSpeed).
- display_block(20,2).
- wait 0.001.
- if radarAlt < finalAltitude {
- gear on.
- sas off.
- lock steering to heading(0,90).
- set runState to 1.
- }
- }
- set myTh to 0.
- // Now we wait until we are mostly aligned.
- until( abs(heading(0,90):pitch - facing:pitch) < 0.15 and abs(heading(0,90):yaw - facing:yaw) < 0.15) {
- display_block(20,2).
- wait 0.001.
- }
- // Now we need to switch to vertical speed...
- lock currentSpeed to ship:verticalspeed.
- until runState > 1 {
- if radarAlt < 10 { set seekSpeed to -1. }
- if radarAlt >= 10 { set seekSpeed to -5. }
- if radarAlt >= 50 { set seekSpeed to -10. }
- if radarAlt > 200 { set seekSpeed to -20. }
- set myTh to PID_seek( myPID, seekSpeed, currentSpeed).
- display_block(20,2).
- wait 0.001.
- if ship:status = "LANDED" {
- set runState to 2.
- }
- }
- rcs off.
- sas off.
- unlock steering.
- set ship:control:pilotmainthrottle to 0.
- unlock throttle.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement