-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhover.ks
More file actions
168 lines (129 loc) · 4.39 KB
/
hover.ks
File metadata and controls
168 lines (129 loc) · 4.39 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
@LAZYGLOBAL OFF.
// Load libraries
run lib_pid.
// PID Controller Settings
global velocity_pid is PID_Init(0.1, 0.2, 0.005, 0, 1).
LOCAL altitude_pid IS PID_Init(0.2, 0.0, 0.050, -10, 10).
LOCAL pitch_pid IS PID_Init(2, 0.05, 0.05, -45, 45).
LOCAL yaw_pid IS PID_Init(2, 0.05, 0.05, -45, 45).
// Profiles
GLOBAL profileAsc IS "Ascending".
GLOBAL profileDesc IS "Descending".
GLOBAL profileOrb IS "Orbiting".
GLOBAL uiX IS 8.
GLOBAL spaces IS " ".
LOCK surfacePrograde to R(0,0,0) * V(0-VELOCITY:SURFACE:X, 0-VELOCITY:SURFACE:Y, 0-VELOCITY:SURFACE:Z).
global targetVelocity is 0.
global currentThrottle is 0.
global steerDirection to UP + R(0,0,0).
global pitchOffset is 0.
global yawOffset is 0.
global offsetX is 1.
PRINT "Waiting for launch...".
WAIT UNTIL STATUS = "FLYING" OR STATUS = "SUB_ORBITAL".
PRINT "Waiting for descent...".
WAIT UNTIL GetProfile() = profileDesc.
PRINT "Hover descent started...".
SAS OFF.
LOCK STEERING TO steerDirection.
LOCK THROTTLE TO currentThrottle.
local surfVec is vecdraw().
local midProgradeec is vecdraw().
UNTIL STATUS = "LANDED" OR STATUS = "SPLASHED" {
// local dirA is UP + R(0, 0, 180).
// local dirB is surfacePrograde:direction.
// local deltaPitch is dirA:pitch - dirB:pitch.
// local deltaYaw is dirA:yaw - dirB:yaw.
// local pitchAngle is round(vang(r(dirA:pitch, 0, 180):vector, r(dirB:pitch, 0, 180):vector)).
// local yawAngle is round(vang(r(0, dirA:yaw, 180):vector, r(0, dirB:yaw, 180):vector)).
// set pitchOffset to (pitchAngle / 6).
// set yawOffset to (yawAngle / 6).
// if deltaPitch < -5 and deltaPitch > -90 {
// set pitchOffset to pitchOffset * 1.
// } else if (deltaPitch < -275 and deltaPitch > -360) or (deltaPitch > 5 and deltaPitch < 90) {
// set pitchOffset to pitchOffset * -1.
// }
// if deltaYaw > 5 and deltaYaw < 90{
// set yawOffset to yawOffset * -1.
// } else if deltaYaw < -5 and deltaYaw > -90 {
// set yawOffset to yawOffset * 1.
// }
if GetTrueAltitude() > 500 {
set targetVelocity to -100.
}
if GetTrueAltitude() < 500 {
set targetVelocity to -14.
}
if GetTrueAltitude() < 250 {
set targetVelocity to -7.
}
if GetTrueAltitude() < 100 {
set targetVelocity to -3.
}
if GetTrueAltitude() < 50 {
set targetVelocity to -1.
}
local midPrograde is (surfacePrograde:normalized + (up+r(0,0,0)):vector:normalized):normalized.
if vang(midPrograde, (up+r(0,0,0)):vector) < 5 {
SET pitchOffset TO -1 * PID_Seek(pitch_pid, 0, SHIP:VELOCITY:SURFACE * SHIP:FACING:TOPVECTOR).
SET yawOffset TO PID_Seek(yaw_pid, 0, SHIP:VELOCITY:SURFACE * SHIP:FACING:STARVECTOR).
set steerDirection to up + r(pitchOffset, yawOffset, 0).
} else {
set steerDirection to midPrograde.
}
if GetTrueAltitude() < 50 {
LOCAL targetAltitude IS PID_Seek(altitude_pid, 45, GetTrueAltitude()).
SET steerDirection TO UP + R(0,0,0).
SET currentThrottle TO PID_Seek(velocity_pid, targetAltitude, VERTICALSPEED).
} ELSE {
set currentThrottle to PID_Seek(velocity_pid, targetVelocity, VERTICALSPEED).
}
wait 0.1.
}
LOCK THROTTLE TO 0.
function CalcDirectionDelta {
parameter dirA, dirB.
local deltaPitch is ABS(COS(dirA:PITCH)-COS(dirB:PITCH))
+ABS(SIN(dirA:PITCH)-SIN(dirB:PITCH)).
local deltaYaw is ABS(COS(dirA:YAW)-COS(dirB:YAW))
+ABS(SIN(dirA:YAW)-SIN(dirB:YAW)).
return deltaPitch + deltaYaw.
}
function DeltaToRetro {
LOCAL deltaPitch IS ABS(COS(SHIP:FACING:PITCH)-COS(surfacePrograde:DIRECTION:PITCH))
+ABS(SIN(SHIP:FACING:PITCH)-SIN(surfacePrograde:DIRECTION:PITCH)).
LOCAL deltaYaw IS ABS(COS(SHIP:FACING:YAW)-COS(surfacePrograde:DIRECTION:YAW))
+ABS(SIN(SHIP:FACING:YAW)-SIN(surfacePrograde:DIRECTION:YAW)).
RETURN deltaPitch + deltaYaw.
}
function DeltaToUp {
LOCAL deltaPitch IS ABS(COS(SHIP:FACING:PITCH)-COS(UP:PITCH))
+ABS(SIN(SHIP:FACING:PITCH)-SIN(UP:PITCH)).
LOCAL deltaYaw IS ABS(COS(SHIP:FACING:YAW)-COS(UP:YAW))
+ABS(SIN(SHIP:FACING:YAW)-SIN(UP:YAW)).
RETURN deltaPitch + deltaYaw.
}
function GetProfile {
IF STATUS = "LANDED" {
RETURN "N/A".
}
IF APOAPSIS > 0 AND PERIAPSIS > 0 {
RETURN profileOrb.
}
IF ETA:APOAPSIS < ETA:PERIAPSIS {
RETURN profileAsc.
} ELSE {
RETURN profileDesc.
}
}
function GetTrueAltitude {
LOCAL terrainHeight IS SHIP:GEOPOSITION:TERRAINHEIGHT.
IF ALT:RADAR = ALTITUDE {
RETURN ALTITUDE.
} ELSE {
IF terrainHeight <= 0 {
SET terrainHeight TO 0.
}
RETURN ALTITUDE - terrainHeight.
}
}