forked from Majiir/MuMechLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MechJebModuleILS.cs
364 lines (303 loc) · 14.6 KB
/
MechJebModuleILS.cs
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MuMech;
using UnityEngine;
using SharpLua.LuaTypes;
namespace MuMech
{
public class MechJebModuleILS : ComputerModule
{
public double targetHeading;
public String targetHeadingString = "90";
public double targetAltitude;
public String targetAltitudeString = "5000";
public bool holdHeadingAndAlt = false;
public bool ilsOn = false;
public bool autoLand = false;
public bool loweredGear = false;
public double runwayStartLat = -0.040633;
public double runwayStartLon = -74.6908;
public double runwayStartAlt = 67;
public double runwayEndLat = -0.041774;
public double runwayEndLon = -74.5341702;
public double runwayEndAlt = 67;
public double glideslope = 3;
public String glideslopeString = "3";
NavBall navball;
GameObject waypoint = new GameObject();
Transform defaultPurpleWaypoint;
public double leftSpeed;
public double leftDisplacement;
public double horizontalDistanceToRunway;
public double flightPathAngleToRunway;
public double flightPathAngle;
public Vector3d runwayStart;
public Vector3d runwayEnd;
public Vector3d runwayDir;
private bool _minimized = false;
public bool minimized
{
get { return _minimized; }
set
{
if(value != _minimized) core.settingsChanged = true;
_minimized = value;
}
}
public MechJebModuleILS(MechJebCore core) : base(core) { }
public override void onLoadGlobalSettings(SettingsManager settings)
{
minimized = settings["ILS_minimized"].valueBool(false);
base.onLoadGlobalSettings(settings);
}
public override void onSaveGlobalSettings(SettingsManager settings)
{
settings["ILS_minimized"].value_bool = minimized;
base.onSaveGlobalSettings(settings);
}
public override void registerLuaMembers(LuaTable index)
{
index.Register("ilsHold", proxyHold);
index.Register("ilsLand", proxyLand);
}
public LuaValue proxyHold(LuaValue[] args)
{
if (args.Count() != 2) throw new Exception("ilsHold usage: ilsHold(altitude [in meters], heading [in degrees])");
double altitude;
double heading;
try
{
altitude = ((LuaNumber)args[0]).Number;
}
catch (Exception)
{
throw new Exception("ilsHold: invalid altitude");
}
try
{
heading = ((LuaNumber)args[1]).Number;
}
catch (Exception)
{
throw new Exception("ilsHold: invalid heading");
}
hold(altitude, heading);
return LuaNil.Nil;
}
public LuaValue proxyLand(LuaValue[] args)
{
land();
return LuaNil.Nil;
}
public void hold(double altitude, double heading)
{
this.enabled = true;
targetHeading = heading;
targetAltitude = altitude;
targetHeadingString = targetHeading.ToString();
targetAltitudeString = targetAltitude.ToString();
autoLand = false;
holdHeadingAndAlt = true;
ilsOn = true;
core.controlClaim(this);
}
public void land()
{
this.enabled = true;
autoLand = true;
holdHeadingAndAlt = false;
ilsOn = true;
loweredGear = false;
core.controlClaim(this);
}
protected override void WindowGUI(int windowID)
{
if (minimized)
{
if (GUILayout.Button("Maximize")) minimized = false;
}
else
{
GUILayout.BeginVertical();
if (GUILayout.Button("Minimize")) minimized = true;
targetHeading = ARUtils.doGUITextInput("Heading:", 70.0F, targetHeadingString, 70.0F, "d", 40.0F, out targetHeadingString, targetHeading);
targetAltitude = ARUtils.doGUITextInput("Altitude:", 70.0F, targetAltitudeString, 70.0F, "m", 40.0F, out targetAltitudeString, targetAltitude);
bool oldHold = holdHeadingAndAlt;
holdHeadingAndAlt = GUILayout.Toggle(holdHeadingAndAlt, "Hold Heading + Alt");
if (holdHeadingAndAlt && !oldHold)
{
autoLand = false;
core.controlClaim(this);
}
if (!holdHeadingAndAlt && oldHold)
{
core.attitudeDeactivate(this);
core.controlRelease(this);
}
ilsOn = GUILayout.Toggle(ilsOn, "ILS", new GUIStyle(GUI.skin.button));
if (ilsOn)
{
GUILayout.Label(String.Format("Lateral speed = {0:0.00} m/s " + (leftSpeed < 0 ? "right" : "left"), Math.Abs(leftSpeed)));
GUILayout.Label(String.Format("Lateral displacement = {0:0} m " + (leftDisplacement < 0 ? "right" : "left"), Math.Abs(leftDisplacement)));
GUILayout.Label(String.Format("Flight Path Angle = {0:0.00}°", flightPathAngle));
GUILayout.Label(String.Format("FPA to runway = {0:0.00}°", flightPathAngleToRunway));
GUILayout.Label(String.Format("Distance to runway = {0:0.00} km", horizontalDistanceToRunway / 1000.0));
GUILayout.Label("The purple circle on the navball points toward the runway. Align your velocity vector with the purple circle and you'll land on the runway, properly aligned with it.");
bool oldAutoLand = autoLand;
autoLand = GUILayout.Toggle(autoLand, "Auto Land");
if (autoLand && !oldAutoLand)
{
holdHeadingAndAlt = false;
core.controlClaim(this);
loweredGear = false;
}
if (!autoLand && oldAutoLand)
{
core.attitudeDeactivate(this);
core.controlRelease(this);
}
if (autoLand) glideslope = ARUtils.doGUITextInput("Glideslope:", 70.0F, glideslopeString, 70.0F, "d", 40.0F, out glideslopeString, glideslope);
}
else
{
autoLand = false;
}
GUILayout.EndVertical();
}
GUI.DragWindow();
}
public override void onPartFixedUpdate()
{
if (!this.enabled) return;
//positions of the start and end of the runway
runwayStart = part.vessel.mainBody.GetWorldSurfacePosition(runwayStartLat, runwayStartLon, runwayStartAlt);
runwayEnd = part.vessel.mainBody.GetWorldSurfacePosition(runwayEndLat, runwayEndLon, runwayEndAlt);
//swap the start and end if we are closer to the end
if ((vesselState.CoM - runwayEnd).magnitude < (vesselState.CoM - runwayStart).magnitude)
{
Vector3d newStart = runwayEnd;
runwayEnd = runwayStart;
runwayStart = newStart;
}
runwayDir = (runwayEnd - runwayStart).normalized;
//a coordinate system oriented to the runway
Vector3d runwayUpUnit = part.vessel.mainBody.GetSurfaceNVector(runwayStartLat, runwayStartLon).normalized;
Vector3d runwayHorizontalUnit = Vector3d.Exclude(runwayUpUnit, runwayStart - runwayEnd).normalized;
Vector3d runwayLeftUnit = -Vector3d.Cross(runwayHorizontalUnit, runwayUpUnit).normalized;
Vector3d vesselUnit = (vesselState.CoM - runwayStart).normalized;
leftSpeed = Vector3d.Dot(vesselState.velocityVesselSurface, runwayLeftUnit);
double verticalSpeed = vesselState.speedVertical;
double horizontalSpeed = vesselState.speedHorizontal;
flightPathAngle = 180 / Math.PI * Math.Atan2(verticalSpeed, horizontalSpeed);
leftDisplacement = Vector3d.Dot(runwayLeftUnit, vesselState.CoM - runwayStart);
Vector3d vectorToRunway = runwayStart - vesselState.CoM;
double verticalDistanceToRunway = Vector3d.Dot(vectorToRunway, vesselState.up);
horizontalDistanceToRunway = Math.Sqrt(vectorToRunway.sqrMagnitude - verticalDistanceToRunway * verticalDistanceToRunway);
flightPathAngleToRunway = 180 / Math.PI * Math.Atan2(verticalDistanceToRunway, horizontalDistanceToRunway);
//set the purple waypoint to guide the plane into the runway, correcting for lateral offset
if ((vesselState.CoM - runwayStart).magnitude > 200.0)
{
waypoint.transform.position = runwayStart - 3 * leftDisplacement * runwayLeftUnit;
}
else
{
waypoint.transform.position = runwayEnd;
}
if (ilsOn) highjackPurpleWaypoint();
else unhighjackPurpleWaypoint();
}
public override void drive(FlightCtrlState s)
{
if (autoLand)
{
if (!part.vessel.Landed)
{
if (!loweredGear && (vesselState.CoM - runwayStart).magnitude < 1000.0)
{
part.vessel.rootPart.SendEvent("LowerLandingGear");
loweredGear = true;
}
Vector3d vectorToWaypoint = waypoint.transform.position - vesselState.CoM;
double headingToWaypoint = 180 / Math.PI * Math.Atan2(Vector3d.Dot(vectorToWaypoint, vesselState.east), Vector3d.Dot(vectorToWaypoint, vesselState.north));
double desiredFPA = Mathf.Clamp((float)(flightPathAngleToRunway + 3 * (flightPathAngleToRunway + glideslope)), -20.0F, 0.0F);
aimVelocityVector(desiredFPA, headingToWaypoint);
}
else
{
//keep the plane aligned with the runway:
Vector3d target = runwayEnd;
if (Vector3d.Dot(target - vesselState.CoM, vesselState.forward) < 0) target = runwayStart;
core.attitudeTo((target- vesselState.CoM).normalized, MechJebCore.AttitudeReference.INERTIAL, this);
}
}
else if (holdHeadingAndAlt)
{
double targetClimbRate = (targetAltitude - vesselState.altitudeASL) / 30.0;
double targetFlightPathAngle = 180 / Math.PI * Math.Asin(Mathf.Clamp((float)(targetClimbRate / vesselState.speedSurface), (float)Math.Sin(-Math.PI / 9), (float)Math.Sin(Math.PI / 9)));
aimVelocityVector(targetFlightPathAngle, targetHeading);
}
}
public double stableAoA = 0; //we average AoA over time to get an estimate of what pitch will produce what FPA
public double pitchCorrection = 0; //we average (commanded pitch - actual pitch) over time in order to fix this offset in our commands
public float maxYaw = 10.0F;
public float maxRoll = 10.0F;
public float maxPitchCorrection = 5.0F;
public double AoAtimeConstant = 2.0;
public double pitchCorrectionTimeConstant = 15.0;
void aimVelocityVector(double desiredFpa, double desiredHeading)
{
//horizontal control
double velocityHeading = 180 / Math.PI * Math.Atan2(Vector3d.Dot(vesselState.velocityVesselSurface, vesselState.east),
Vector3d.Dot(vesselState.velocityVesselSurface, vesselState.north));
double headingTurn = Mathf.Clamp((float)ARUtils.clampDegrees(desiredHeading - velocityHeading), -maxYaw, maxYaw);
double noseHeading = velocityHeading + headingTurn;
double noseRoll = (maxRoll / maxYaw) * headingTurn;
//vertical control
double nosePitch = desiredFpa + stableAoA + pitchCorrection;
core.attitudeTo(noseHeading, nosePitch, noseRoll, this);
double flightPathAngle = 180 / Math.PI * Math.Atan2(vesselState.speedVertical, vesselState.speedHorizontal);
double AoA = vesselState.vesselPitch - flightPathAngle;
stableAoA = (AoAtimeConstant * stableAoA + vesselState.deltaT * AoA) / (AoAtimeConstant + vesselState.deltaT); //a sort of integral error
pitchCorrection = (pitchCorrectionTimeConstant * pitchCorrection + vesselState.deltaT * (nosePitch - vesselState.vesselPitch)) / (pitchCorrectionTimeConstant + vesselState.deltaT);
pitchCorrection = Mathf.Clamp((float)pitchCorrection, -maxPitchCorrection, maxPitchCorrection);
}
public override void onModuleDisabled()
{
unhighjackPurpleWaypoint();
}
void highjackPurpleWaypoint()
{
//highjack the purple navball waypoint
if (navball == null) navball = (NavBall)GameObject.FindObjectOfType(typeof(NavBall));
if (navball != null)
{
if (navball.target != waypoint.transform)
{
defaultPurpleWaypoint = navball.target; //save the usual waypoint
navball.target = waypoint.transform;
}
}
}
void unhighjackPurpleWaypoint()
{
if (navball != null)
{
if (navball.target == waypoint.transform)
{
navball.target = defaultPurpleWaypoint;
}
}
}
public override GUILayoutOption[] windowOptions()
{
if(minimized) return new GUILayoutOption[] { GUILayout.Width(150), GUILayout.Height(40) };
else return new GUILayoutOption[] { GUILayout.Width(250) };
}
public override String getName()
{
return "Instrument Landing System";
}
}
}