Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print "Starting ascent script.".
- function sign {
- parameter x.
- if x < 0 {
- return -1.
- } else {
- return 1.
- }
- }
- function tradmod {
- parameter x, y.
- return x - y * floor(x / y).
- }
- // Create GUI
- set descentGUI to gui(300).
- // Add labels to GUI
- set headingHeader to descentGUI:addlabel("<b>Heading Info</b>").
- set headingHeader:style:align to "CENTER".
- set headingHeader:style:richtext to true.
- set shipFacingYaw to descentGUI:addlabel("").
- set shipFacingPitch to descentGUI:addlabel("").
- set shipFacingRoll to descentGUI:addlabel("").
- set vehicleHeader to descentGUI:addlabel("<b>Vehicle Info</b>").
- set vehicleHeader:style:align to "CENTER".
- set vehicleHeader:style:richtext to true.
- set altitudeLabel to descentGUI:addlabel("").
- set radarLabel to descentGUI:addlabel("").
- set verticalSpeedLabel to descentGUI:addlabel("").
- set horizontalSpeedLabel to descentGUI:addlabel("").
- set throttleLabel to descentGUI:addlabel("").
- set massLabel to descentGUI:addlabel("").
- set thrustLabel to descentGUI:addlabel("").
- set statusLabel to descentGUI:addlabel("").
- set descentHeader to descentGUI:addlabel("<b>Descent Trajectory Info</b>").
- set descentHeader:style:align to "CENTER".
- set descentHeader:style:richtext to true.
- set decelerationLabel to descentGUI:addlabel("").
- set stopDistanceLabel to descentGUI:addlabel("").
- set distanceLeftLabel to descentGUI:addlabel("").
- set necessaryDecelerationLabel to descentGUI:addlabel("").
- set necessaryThrustLabel to descentGUI:addlabel("").
- set stationaryCounterLabel to descentGUI:addlabel("").
- function UpdateGeneralGUI {
- set shipFacingYaw:text to "Ship Yaw: " + round(ship:facing:yaw) + "°".
- set shipFacingPitch:text to "Ship Pitch: " + round(ship:facing:pitch) + "°".
- set shipFacingRoll:text to "Ship Roll: " + round(ship:facing:roll) + "°".
- set altitudeLabel:text to "Altitude: " + round(ship:altitude) + " m".
- set radarLabel:text to "Radar: " + round(ship:bounds:bottomaltradar) + " m".
- set verticalSpeedLabel:text to "Vertical Speed: " + round(ship:verticalspeed) + " m/s".
- set horizontalSpeedLabel:text to "Horizontal Speed: " + round((ship:velocity:surface - (ship:up:vector * ship:verticalspeed)):mag) + " m/s".
- set throttleLabel:text to "Throttle: " + round(throttle * 100) + " %".
- set massLabel:text to "Mass: " + round(ship:mass, 2) + " t".
- set thrustLabel:text to "Thrust: " + round(ship:availablethrust) + " kN".
- set statusLabel:text to "Vessel Status: " + ship:status.
- }
- function UpdateDescentGUI {
- set decelerationLabel:text to "Maximum Deceleration: " + round(DECELERATION, 2) + " m/s²".
- set stopDistanceLabel:text to "Distance Needed to Stop: " + round(STOPDISTANCE, 2) + " m".
- set distanceLeftLabel:text to "Distance Remaining: " + round(DISTANCELEFT, 2) + " m".
- }
- // Define the update function
- function UpdateAllGUI {
- UpdateGeneralGUI().
- UpdateDescentGUI().
- }
- set launchPosition to latlng(ship:geoposition:lat, ship:geoposition:lng):position. // landing target location, TODO rename this
- set chopstickPos to ship:partstagged("chopsticks")[0]:position.
- set neutralVec to chopstickPos - launchPosition - (vdot(chopstickPos - launchPosition, ship:up:vector) * ship:up:vector).
- // Show the GUI
- descentGUI:show().
- set targetAltitude to 75000. // Target apoapsis for orbit.
- lock steering to heading(90, 90). // Point straight up initially.
- lock throttle to 1. // Full throttle for launch.
- toggle ag3. // Start deluge
- // Countdown and launch.
- print "5...".
- wait 1.
- print "4...".
- wait 1.
- print "3...".
- wait 1.
- print "2...".
- wait 1.
- print "Ignition!".
- stage.
- print "1...".
- wait 1.
- print "Liftoff!".
- toggle ag1. // Retract QD arm
- stage. // Activate the first stage.
- UpdateGeneralGUI(). // TODO improve gui updates
- // Store launch location.
- set originalLaunchLocation to latlng(ship:geoposition:lat, ship:geoposition:lng).
- set launchLocation to latlng(ship:geoposition:lat, ship:geoposition:lng + 0.00025). // landing target location, TODO rename this
- // Main ascent loop.
- print "Starting gravity turn.".
- until apoapsis >= targetAltitude {
- UpdateGeneralGUI().
- // equation that gives pitch based on altitude: y=10.73485+(89.75962-10.73485)/(1+(x/11504.65))
- set pitch to 10.73485+(89.75962-10.73485)/(1+(ALTITUDE/11504.65)).
- // make sure we can never pitch down
- if pitch < 0 {
- set pitch to 0.
- }
- lock steering to heading(90, pitch).
- wait 0.04.
- }
- lock throttle to 0.3. // throttle down for separation
- toggle ag5. // switch to 13 engine mode
- wait 0.5.
- print "Throttling down, MECO.".
- toggle ag5. // switch to 3 engine mode
- wait 0.5.
- // Stage separation and booster control.
- stage. // Stage separation.
- print "Hotstaging.".
- // Activate RCS for the booster turn.
- wait 0.5.
- rcs on.
- print "Reaction control systems online.".
- print "Turnaround for boostback burn.".
- lock steering to heading(90, -60). // start to turn retrograde for
- wait 2.
- // switch back to 13 engine mode
- toggle ag5.
- toggle ag5.
- // gradual change of heading to make the flip direction more predictable
- lock throttle to 0.2.
- wait 1.5.
- lock steering to heading(90, -89).
- wait 1.
- lock steering to heading(270, -45).
- wait 1.
- lock steering to heading(270, 0).
- wait 2.
- print "Booster turnaround complete.".
- wait until vang(ship:facing:forevector, heading(270, 0):forevector) < 30. // Wait until properly aligned.
- // using 340 isp
- set deltaVBeforeBoostback to 9.81 * 340 * ln(ship:mass / ship:drymass).
- set targetDrainedDeltaV to 1600.
- set threeEngines to false. // flag for keeping track of engine mode
- set boostbackOffset to 0.015. // (working value: 0.015)
- // Perform boostback burn with dynamic adjustment to minimize perpendicular velocity.
- print "Boostback burn initiated.".
- lock throttle to 1.
- if addons:tr:available {
- addons:tr:settarget(launchLocation). // Set the launch location as the target.
- until addons:tr:hasimpact and (addons:tr:impactpos:lng < launchLocation:lng + boostbackOffset) {
- // drain excess fuel to reduce weight
- set valves to ship:partstagged("drainvalve").
- set currentDeltaV to 9.81 * 340 * ln(ship:mass / ship:drymass).
- if (currentDeltaV > deltaVBeforeBoostback - targetDrainedDeltaV) {
- for valve in valves {
- valve:getmodule("ModuleResourceDrain"):doaction("drain", true).
- }
- } else {
- for valve in valves {
- valve:getmodule("ModuleResourceDrain"):doaction("stop draining", true).
- }
- }
- // boostback burn control loop
- UpdateGeneralGUI().
- if addons:tr:hasimpact {
- set targetBearing to launchLocation:heading.
- set horizontalVelocityVec to ship:velocity:surface - (ship:verticalspeed * ship:up:vector).
- set toTargetVector to (launchLocation:position - ship:position):normalized. // Vector pointing from the ship to the target
- set sidewaysDirVector to vcrs(toTargetVector, ship:up:vector):normalized.
- set sidewaysVelocity to vdot(horizontalVelocityVec, sidewaysDirVector).
- // Use sidewaysVelocity to adjust the heading
- set Kp to 0.2.
- set headingCorrection to sidewaysVelocity * Kp.
- // Limit the correction angle
- set maxCorrectionAngle to 10.
- if abs(headingCorrection) > maxCorrectionAngle {
- set headingCorrection to maxCorrectionAngle * sign(headingCorrection).
- }
- // Adjust throttle for finer control as we approach the target impact point
- set impactLongitudeError to abs(addons:tr:impactpos:lng - launchLocation:lng).
- if impactLongitudeError < 0.15 {
- set headingCorrection to (launchLocation:lat - addons:tr:impactpos:lat) * 100 .//+ sidewaysVelocity * Kp.
- lock throttle to 0.4. // throttle down for final adjustment
- } else if impactLongitudeError < 0.6 {
- if not threeEngines {
- toggle ag5. // switch to 3 engine mode
- set threeEngines to true.
- }
- set headingCorrection to (launchLocation:lat - addons:tr:impactpos:lat) * 100 .//+ sidewaysVelocity * Kp.
- }
- // Adjust the desired heading, normalizing it to 0-360
- set desiredHeading to tradmod(targetBearing + headingCorrection, 360).
- lock steering to heading(desiredHeading, 0).
- }
- wait 0.05.
- }
- set ship:control:pilotmainthrottle to 0.
- print "Boostback burn complete.".
- } else {
- print "Trajectories mod not available. Cannot perform boostback.".
- }
- lock throttle to 0.
- toggle ag2. // toggle grid fins
- print "Starting entry reorientation.".
- // switch to 13 engine mode
- wait 0.2.
- toggle ag5.
- wait 0.2.
- toggle ag5.
- set initialFacing to ship:facing. // initial facing after boostback burn
- set targetYaw to tradmod(ship:facing:yaw + 180, 360).
- // slow precise reorientation for entry
- until (ship:facing:yaw > targetYaw - 15 and ship:facing:yaw < targetYaw + 10) {
- lock steering to R(initialFacing:pitch, ship:facing:yaw + 20, initialFacing:roll).
- UpdateGeneralGUI().
- }
- lock steering to retrograde.
- print "Reorientation for entry complete.".
- set initiatedArmClose to false. // flag for keeping track of arm closure
- set targetAltitude to 300. // (working value: 300)
- set targetSpeed to 50.
- set landingBurnMultiplier to 0.9. // planned throttle level (working value: 0.9)
- lock distanceLeft to max(1, ship:bounds:bottomaltradar - targetAltitude). // distance left to target altitude
- lock slowDownLeft to ship:verticalspeed + targetSpeed. // vertical speed left to slow down to target speed
- lock deceleration to landingBurnMultiplier * ship:availablethrust / ship:mass. // planned deceleration (m/s^2)
- lock stopDistance to (slowDownLeft * slowDownLeft) / (2 * deceleration). // distance needed to stop with planned deceleration
- set gAcc to constant:g * body:mass / body:radius^2.
- // Descent loops with GUI updates
- if addons:tr:available {
- print "Waiting for landing burn conditions to be met.".
- // First loop: wait until landing burn conditions are met
- until (stopDistance > distanceLeft) {
- UpdateAllGUI().
- wait 0.02.
- }
- lock steering to addons:tr:plannedvec.
- lock toTargetVector to addons:tr:gettarget:position - addons:tr:impactpos:position.
- // Second loop: stage of the large positional corrections and most of the deceleration
- print "Landing burn startup.".
- set steeringStrength to 0.
- until (-ship:verticalspeed < targetSpeed + 3) {
- if steeringStrength < 0.0025 {
- set steeringStrength to steeringStrength + 0.00005. // increases to full strength over 1 second
- }
- if (-ship:verticalspeed > targetSpeed + 50) {
- lock steering to addons:tr:plannedvec + steeringStrength * toTargetVector.
- } else {
- lock steering to R(ship:up:pitch, ship:up:yaw, -90).
- }
- // adjust throttle for planned deceleration
- set necessaryDeceleration to gAcc - (targetSpeed*targetSpeed - ship:verticalspeed*ship:verticalspeed)/(2 * distanceLeft).
- set necessaryThrust to necessaryDeceleration * SHIP:MASS.
- lock throttle to necessaryThrust / ship:availablethrust.
- // Update GUI
- UpdateAllGUI().
- set necessaryDecelerationLabel:text to "Necessary Deceleration for Exact Stop: " + round(necessaryDeceleration, 2) + " m/s²".
- set necessaryThrustLabel:text to "Necessary Thrust for Exact Deceleration: " + round(necessaryThrust) + " kN".
- wait 0.02.
- }
- // Third loop: final descent phase
- print "Chopstick manouvering commenced.".
- set stationaryCounter to 0.
- set finalDescent to false.
- set steeringStrength to 0.005.
- set targetTWR to 1.0.
- until (stationaryCounter > 20) {
- if addons:tr:hasimpact {
- set horizontalVelocityVec to ship:velocity:surface - (ship:verticalspeed * ship:up:vector).
- set steeringVector to ship:up:vector + steeringStrength * toTargetVector.
- set steeringVectorDir to steeringVector:direction.
- set thrustAngleMultiplier to 1 / (steeringVector:normalized * ship:up:vector).
- if (ship:bounds:bottomaltradar > 43 and ship:bounds:bottomaltradar < 120) {
- set activeLoc to latlng(ship:geoposition:lat, ship:geoposition:lng):position.
- set directedVec to chopstickPos - activeLoc - (vdot(chopstickPos - activeLoc, ship:up:vector) * ship:up:vector).
- set directionSign to sign(vdot(vcrs(neutralVec, directedVec), ship:up:vector)).
- set movingAngle to vang(neutralVec, directedVec).
- vessel("Mechazilla"):connection:sendmessage(directionSign * movingAngle).
- }
- // original steering strength: 0.003
- if (-ship:verticalspeed > 15) { // if we're still going too fast at the start of the final descent
- set targetTWR to 2.0.
- set steeringStrength to 0.003.
- } else if (ship:bounds:bottomaltradar > 100) { // move towards chopsticks with a constant velocity
- set targetTWR to 1.0.
- set steeringStrength to 0.005.
- if (not finalDescent) {
- toggle ag5. // switch to 3 engine mode
- set finalDescent to true.
- print "Cut back to 3 engines.".
- }
- } else if (-ship:verticalspeed > 4) { // slow down towards the second phase, arriving at the chopsticks (slower, but not final descent)
- set targetTWR to 2.0.
- set steeringStrength to 0.005.
- //set launchLocation to originalLaunchLocation.
- } else if (ship:bounds:bottomaltradar > 46) { // second phase, slow descent into the chopsticks almost until the end
- set targetTWR to 1.0.
- set steeringStrength to 0.005.
- set steeringVector to ship:up:vector - (0.03 * horizontalVelocityVec).
- set steeringVectorDir to steeringVector:direction.
- if not initiatedArmClose {
- vessel("Mechazilla"):connection:sendmessage("Close arms").
- set initiatedArmClose to true.
- print "Final landing insertion.".
- print "Chopsticks closing.".
- }
- } else if (-ship:verticalspeed > 1.1) {
- set targetTWR to 1.5.
- set steeringStrength to 0.005.
- } else if (-ship:verticalspeed < 0.8) {
- set targetTWR to 0.75.
- set steeringStrength to 0.005.
- } else { // third and final phase, slow descent into the chopsticks
- set targetTWR to 1.0.
- set steeringStrength to 0.003.
- set steeringVector to ship:up:vector - (0.03 * horizontalVelocityVec).
- set steeringVectorDir to steeringVector:direction.
- }
- lock steering to R(steeringVectorDir:pitch, steeringVectorDir:yaw, -90).
- lock throttle to targetTWR * thrustAngleMultiplier * ship:mass / (ship:availablethrust / 9.80665)..
- // Update GUI
- UpdateAllGUI().
- }
- if (ship:status = "LANDED" and ship:bounds:bottomaltradar < 42.5) {
- set stationaryCounter to stationaryCounter + 1.
- } else {
- set stationaryCounter to 0.
- }
- wait 0.02.
- }
- set ship:control:pilotmainthrottle to 0.
- print "Chopstick landing successful.".
- }
- // Hide the GUI after landing
- descentGUI:hide().
- descentGUI:dispose().
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement