-
Notifications
You must be signed in to change notification settings - Fork 0
/
alts.ks
148 lines (115 loc) · 4.65 KB
/
alts.ks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// ALTS - Automatic Lander Touchdown System
clearScreen.
// target
set targetHoverslam to latlng(-0.0972092543643722, -74.557706433623).
addons:tr:settarget(targetHoverslam).
// initial variables
set runmode to 1.
lock trueRadar to actualHeight().
lock g0 to constant:g * ship:body:mass/ship:body:radius^2.
lock shipAcc to (ship:availableThrust/ship:mass) - g0.
lock decelHeight to (ship:verticalspeed^2/(2 * shipAcc)) * 4.
lock throtVal to decelHeight/trueRadar.
lock errorDistance to haversineDistance(addons:tr:impactpos, targetHoverslam).
// calculation function references
declare local function actualHeight { // get actual height from KSP collision box system
local bounds_box is ship:bounds.
return bounds_box:bottomaltradar.
}
declare local function getBearingFromAtoB { // get vector to heading(magnetic) between the predicted impact point and targeted impact point
// B is target
// A is impact pos from trajectories.
if ADDONS:TR:HASIMPACT {
local deltaLNG is targetHoverslam:lng - addons:tr:impactpos:lng.
local x is cos(targetHoverslam:lat) * sin (deltaLNG).
local y is cos(addons:tr:impactpos:lat) * sin(targetHoverslam:lat) - sin (addons:tr:impactpos:lat) * cos(targetHoverslam:lat) * cos(deltaLNG).
return arcTan2(x,y).
}
else {
return 0.
}
}
declare local function haversineDistance {
if addons:tr:hasimpact {
local parameter geoPointA.
local parameter geoPointB.
//convert to radians for haversine function
local geoPointALat is geoPointA:lat * (constant:pi / 180).
local geoPointALng is geoPointA:lng * (constant:pi / 180).
local geoPointBLat is geoPointB:lat * (constant:pi / 180).
local geoPointBLng is geoPointB:lng * (constant:pi / 180).
//calculate the first haversine function
local dLat is geoPointBLat - geoPointALat.
local dLon is geoPointBLng - geoPointALng.
local havLatitude is sin(dLat/2)^2.
local havLongitude is sin(dLon/2)^2.
//distance from A to B. Travel from A -> B.
local distanceStepOne is havLatitude + cos(geoPointALat) * cos(geoPointBLat) * havLongitude.
local distanceStepTwo is arcSin(sqrt(distanceStepOne)).
local distance is distanceStepTwo * ship:body:radius * 2.
return distance.
}
else {
return 0.
}
}
declare local function descentAOA_X { // TDAG EW - trajectory discrepancy avoidance guidance East <-> West.
if addons:tr:hasimpact {
return min((sqrt(errorDistance)), max(-(sqrt(errorDistance)), (getBearingFromAtoB()))).
}
else {
return 0.
}
}
declare local function descentAOA_Y { // TDAG NS -trajectory discrepancy avoidance guidance for North <-> South.
if addons:tr:hasimpact {
return -min(sqrt(errorDistance)*2, max(-sqrt(errorDistance)*2, ((addons:tr:impactpos:lat - targetHoverslam:lat)*10448.6454))).
}
else {
return 0.
}
}
declare local function getTwr { // get TWR for first correctional burn
return g0 * (ship:mass/ship:availableThrust).
}
declare local function printfunc {
print "AUTOMATIC LANDER TOUCHDOWN SYSTEM(ALTS)" at (2,5).
print "=======================" at (2,7).
print "Height above terrain: " + round(trueRadar, 1) + "m " at (2,9).
print "Vertical velocity: " + round(abs(ship:verticalspeed),1) + "m/s " at (2,11).
print "Horizontal velocity: " + round(ship:groundspeed,1) + "m/s " at (2,13).
print "DeltaV remaining: " + round(stage:deltaV:current,1) + "m/s " at (2,15).
print "Error in distance: " + round(haversineDistance(addons:tr:impactpos, targetHoverslam),1) + "m " at (2,17).
print "=======================" at (2,21).
print sqrt(haversineDistance(addons:tr:impactpos, targetHoverslam))/10 at (2,23).
}
// Actual code
until runmode = 0 {
printfunc().
if runmode = 1 {
lock throttle to 4 * getTwr().
lock steering to srfRetrograde.
print "Status: IVRP - Initial Velocity Reduction Phase " at (2, 19).
when abs(ship:verticalspeed) < 50 and ship:groundspeed < 50 then {
set runmode to 2.
}
}
if runmode = 2 {
lock steering to -R(descentAOA_Y(), descentAOA_X(), 0) * UP.
print "Status: MVRP - Midterm Velocity Redsuction Phase " at (2, 19).
lock throttle to sqrt(errorDistance)/7 * getTwr().
set runmode to 3.
}
if runmode = 3 and trueRadar < decelHeight {
print "Status: FDGP - Final Descent Guidance Phase " at (2, 19).
lock throttle to throtVal.
when ship:verticalspeed > -2 then {
lock steering to srfRetrograde.
}
when ship:verticalspeed > -0.01 then{
lock throttle to 0.
set runmode to 0.
}
}
wait 0.15.
}