Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // NOTE - this is a purpose-built script and is not indicative of the more modular way the KSA will approach launch scripts once the program begins anew
- abort off.
- sas on.
- lights on.
- lock throttle to 0.
- // from the KSLib
- // https://github.com/KSP-KOS/KSLib/blob/master/library/lib_circle_nav.ks
- function circle_distance {
- parameter
- p1, //...this point...
- p2, //...to this point...
- radius. //...around a body of this radius. (note: if you are flying you may want to use ship:body:radius + altitude).
- local A is sin((p1:lat-p2:lat)/2)^2 + cos(p1:lat)*cos(p2:lat)*sin((p1:lng-p2:lng)/2)^2.
- return radius*constant():PI*arctan2(sqrt(A),sqrt(1-A))/90.
- }.
- // from the KSLib
- // https://github.com/KSP-KOS/KSLib/blob/master/library/lib_navball.ks
- function east_for {
- parameter ves.
- return vcrs(ves:up:vector, ves:north:vector).
- }
- function compass_for {
- parameter ves.
- local pointing is ves:facing:forevector.
- local east is east_for(ves).
- local trig_x is vdot(ves:north:vector, pointing).
- local trig_y is vdot(east, pointing).
- local result is arctan2(trig_y, trig_x).
- if result < 0 {
- return 360 + result.
- } else {
- return result.
- }
- }
- function pitch_for {
- parameter ves.
- return 90 - vang(ves:up:vector, ves:facing:forevector).
- }
- function roll_for {
- parameter ves.
- if vang(ship:facing:vector,ship:up:vector) < 0.2 { //this is the dead zone for roll when the ship is vertical
- return 0.
- } else {
- local raw is vang(vxcl(ship:facing:vector,ship:up:vector), ves:facing:starvector).
- if vang(ves:up:vector, ves:facing:topvector) > 90 {
- if raw > 90 {
- return 270 - raw.
- } else {
- return -90 - raw.
- }
- } else {
- return raw - 90.
- }
- }
- }.
- on abort {
- set running to false.
- }.
- //when stage:number < 2 then { set fuelOffset to 180. }
- set TotalFuelIndex to 1.
- set StageFuelIndex to 0.
- set bTotalFuel to true.
- set bStageFuel to true.
- set dstTraveled to 0.
- set logInterval to 1.
- set TotalFuel to 1.
- set StageFuel to 1.
- //set fuelOffset to 0.
- set running to true.
- set currTime to ceiling(time:seconds).
- set launchTime to 59747400.
- set startStage to stage:number.
- set initialVelocity to ship:velocity:surface:mag.
- set launchPosition to ship:geoposition.
- set surfaceGravity to (ship:orbit:body:mass * constant:G)/(ship:orbit:body:radius^2).
- set SFCapacity to ship:resources[3]:capacity.
- set pitch to 89.
- set timeDelay to 99999999.
- lock srb to ship:partstagged("srb")[0]:getmodule("moduleengines"):getfield("status").
- lock lifter to ship:partstagged("lifter")[0]:getmodule("moduleengines"):getfield("status").
- set event to "Final Countdown".
- when stage:number = 5 then {
- set event to "Main Engine Start".
- lock throttle to 0.15.
- }.
- when stage:number = 4 then {
- set event to "Liftoff!".
- lock throttle to 0.38.
- }.
- when ship:velocity:surface:mag > 180 then{
- set event to "Beginning Gravity Turn".
- sas off.
- lock steering to heading(90,pitch).
- set pitchTime to floor(time:seconds).
- }.
- when srb = "flame-out!" then {
- set event to "SRB Drop, Main Engine Throttle-up".
- lock throttle to 0.75.
- stage.
- }.
- when ship:altitude > 55000 then {
- set event to "Fairing Jettison".
- stage.
- }.
- when ship:obt:apoapsis > 75000 then {
- set event to "MECO, Coast to Orbital Insertion Burn".
- lock throttle to 0.
- lock steering to prograde.
- }.
- when ship:altitude > 74000 then {
- set event to "Orbital Insertion Burn".
- lock throttle to 1.
- }.
- when lifter = "flame-out!" then {
- set event to "Main Engine Flameout".
- set timeDelay to time:seconds.
- stage.
- }.
- when stage:number = 1 and time:seconds - timeDelay > 2 then {
- set event to "Orbital Engine Ignition".
- stage.
- }.
- when ship:obt:periapsis > 70500 then {
- set event to "Orbital Insertion Completed".
- lock throttle to 0.
- }.
- // print out the initial information for us to update as we go along
- clearscreen.
- print "Time to Launch: ".
- print "Compass: " + round(compass_for(ship), 1) + "°".
- print "Pitch: " + round(pitch_for(ship), 1) + "°".
- print "Roll: " + round(roll_for(ship), 1) + "°".
- print "StageFuel: 100%".
- print "TotalFuel: 100%".
- print "Distance Traveled: 0 km".
- print "Altitude: " + round(ship:altitude/1000, 3) + " km".
- print "".
- list files in fileList.
- for fil in fileList {
- if fil:name = "TelemetryLog.ks" { delete TelemetryLog. }.
- }
- // create the CSV headers
- log "ID,Heading,Pitch,Roll,DstTraveled,StageFuel,TotalFuel,Q,Mass,AoA,Altitude,Lat,Lon,Apoapsis,Periapsis,Inclination,Velocity,Thrust,Gravity,DstDownrange,Throttle,AoAWarn,Video,Camera,CommLost,Event,Image,Tweet" to TelemetryLog.
- when launchTime - currTime = 8 then stage.
- when launchTime - currTime = 0 then stage.
- // loop until program exit is triggered by action group
- until not running {
- wait until time:seconds - currTime > logInterval.
- set currTime to floor(time:seconds).
- if launchTime - currTime < 13 {
- set thrust to 0.
- list engines in allEngines.
- for eng in allEngines {
- if eng:ignition { set thrust to thrust + eng:thrust. }.
- }.
- if stage:number = 5 {
- set TotalFuel to (ship:resources[1]:amount + ship:resources[3]:amount) / ((ship:resources[1]:capacity-180) + SFCapacity).
- print "TotalFuel: " + 100*TotalFuel + "% " at (0,4).
- } else if stage:number = 4 {
- set TotalFuel to (ship:resources[1]:amount + ship:resources[3]:amount) / ((ship:resources[1]:capacity-180) + SFCapacity).
- set StageFuel to stage:resources[2]:amount / stage:resources[2]:capacity.
- print "TotalFuel: " + 100*TotalFuel + "% " at (0,4).
- print "StageFuel: " + 100*StageFuel + "% " at (0,5).
- } else if stage:number = 3 or stage:number = 2 {
- set TotalFuel to ship:resources[1]:amount/(ship:resources[1]:capacity-180+SFCapacity).
- set StageFuel to stage:resources[0]:amount/(stage:resources[0]:capacity).
- print "TotalFuel: " + 100*TotalFuel + "% " at (0,4).
- print "StageFuel: " + 100*StageFuel + "% " at (0,5).
- } else if stage:number < 2 {
- set TotalFuel to ship:resources[1]:amount/(ship:resources[1]:capacity-180+SFCapacity).
- set StageFuel to ship:resources[1]:amount/45.
- print "TotalFuel: " + 100*TotalFuel + "% " at (0,4).
- print "StageFuel: " + 100*StageFuel + "% " at (0,5).
- }.
- if ship:obt:apoapsis < 75000 and ship:velocity:surface:mag > 180 { set pitch to pitch - 0.75. }.
- // update information text with new data
- if currTime > launchTime {
- // update our distance traveled every second based on our speed (which is in m/s)
- // https://answers.yahoo.com/question/index?qid=20100423120148AADAkZ2
- // this should be set to ship:velocity:orbital:mag if launching into orbit
- set a to (initialVelocity - ship:velocity:surface:mag)/logInterval.
- set d to (initialVelocity * logInterval) + 0.5 * ((a * logInterval)^2).
- set dstTraveled to dstTraveled + d.
- set initialVelocity to ship:velocity:surface:mag.
- print "Mission Elapsed Time: " + (currTime - launchTime) + "s " at (0,0).
- print "Compass: " + round(compass_for(ship), 1) + "°" + " " at (0,1).
- print "Pitch: " + round(pitch_for(ship), 1) + "°" + " " at (0,2).
- print "Roll: " + round(roll_for(ship), 1) + "°" + " " at (0,3).
- print "Distance Traveled: " + round(dstTraveled/1000, 3) + " km " at (0,6).
- print "Altitude: " + round(ship:altitude/1000, 3) + " km " at (0,7).
- }.
- // log all the data
- log currTime + "," +
- compass_for(ship) + "," +
- pitch_for(ship) + "," +
- roll_for(ship) + "," +
- dstTraveled + "," +
- StageFuel + "," +
- TotalFuel + "," +
- ship:Q * constant:AtmToKPa + "," +
- ship:mass + "," +
- VANG(ship:facing:vector, ship:srfprograde:vector) + "," +
- ship:altitude + "," +
- ship:geoposition:lat + "," +
- ship:geoposition:lng + "," +
- ship:orbit:apoapsis + "," +
- ship:orbit:periapsis + "," +
- ship:orbit:inclination + "," +
- ship:velocity:surface:mag + "," +
- thrust + "," +
- surfaceGravity/((((ship:orbit:body:radius + ship:altitude)/1000)/(ship:orbit:body:radius/1000))^2) + "," +
- circle_distance(launchPosition, ship:geoposition, ship:orbit:body:radius) + "," +
- throttle + ",,,,," +
- event
- to TelemetryLog.
- }.
- if launchTime - currTime >= 0 { print "Time to Launch: " + (launchTime - currTime) + "s " at (0,0). }.
- }.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement