/
MechJebModuleAscentGTAutopilot.cs
241 lines (188 loc) · 8.01 KB
/
MechJebModuleAscentGTAutopilot.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
using System;
using JetBrains.Annotations;
using KSP.Localization;
/*
* NOTE: THAT THIS IS NOT INTENDED TO BE A PERFECTLY FAITHFUL REIMPLEMENTATION OF
* THE GRAVITY TURN MOD FOR KSP.
*/
namespace MuMech
{
[UsedImplicitly]
public class MechJebModuleAscentGTAutopilot : MechJebModuleAscentBaseAutopilot
{
public MechJebModuleAscentGTAutopilot(MechJebCore core) : base(core) { }
protected override void OnModuleEnabled()
{
base.OnModuleEnabled();
_maxholdAPTime = 0.0F;
_mode = AscentMode.VERTICAL_ASCENT;
}
private bool IsVerticalAscent(double altitude, double velocity)
{
if (altitude < AscentSettings.TurnStartAltitude && velocity < AscentSettings.TurnStartVelocity)
{
return true;
}
return false;
}
private enum AscentMode { VERTICAL_ASCENT, INITIATE_TURN, GRAVITY_TURN, HOLD_AP, COAST_TO_APOAPSIS, EXIT }
private AscentMode _mode;
protected override bool DriveAscent2()
{
switch (_mode)
{
case AscentMode.VERTICAL_ASCENT:
DriveVerticalAscent();
break;
case AscentMode.INITIATE_TURN:
DriveInitiateTurn();
break;
case AscentMode.GRAVITY_TURN:
DriveGravityTurn();
break;
case AscentMode.HOLD_AP:
DriveHoldAP();
break;
case AscentMode.COAST_TO_APOAPSIS:
DriveCoastToApoapsis();
break;
case AscentMode.EXIT:
return false;
default:
throw new ArgumentOutOfRangeException();
}
return true;
}
private void DriveVerticalAscent()
{
if (!IsVerticalAscent(VesselState.altitudeTrue, VesselState.speedSurface)) _mode = AscentMode.INITIATE_TURN;
if (Orbit.ApA > AscentSettings.IntermediateAltitude) _mode = AscentMode.GRAVITY_TURN;
//during the vertical ascent we just thrust straight up at max throttle
AttitudeTo(90);
bool liftedOff = Vessel.LiftedOff() && !Vessel.Landed;
Core.Attitude.SetAxisControl(liftedOff, liftedOff, liftedOff && VesselState.altitudeBottom > AscentSettings.RollAltitude);
Core.Thrust.TargetThrottle = 1.0F;
if (!Vessel.LiftedOff() || Vessel.Landed) Status = Localizer.Format("#MechJeb_Ascent_status6"); //"Awaiting liftoff"
else Status = Localizer.Format("#MechJeb_Ascent_status18"); //"Vertical ascent"
}
private void DriveInitiateTurn()
{
//stop the intermediate "burn" when our apoapsis reaches the desired altitude
if (Orbit.ApA > AscentSettings.DesiredOrbitAltitude)
{
_mode = AscentMode.COAST_TO_APOAPSIS;
return;
}
if (90 - AscentSettings.TurnStartPitch >= SrfvelPitch())
{
_mode = AscentMode.GRAVITY_TURN;
return;
}
if (Orbit.ApA > AscentSettings.IntermediateAltitude)
{
_mode = AscentMode.GRAVITY_TURN;
return;
}
//if we've fallen below the turn start altitude, go back to vertical ascent
if (IsVerticalAscent(VesselState.altitudeTrue, VesselState.speedSurface))
{
_mode = AscentMode.VERTICAL_ASCENT;
return;
}
AttitudeTo(90 - AscentSettings.TurnStartPitch);
Core.Thrust.TargetThrottle = ThrottleToRaiseApoapsis(Orbit.ApR, AscentSettings.IntermediateAltitude + MainBody.Radius);
if (Core.Thrust.TargetThrottle < 1.0F)
{
Status = Localizer.Format("#MechJeb_Ascent_status19"); //"Fine tuning intermediate altitude"
return;
}
Status = Localizer.Format("#MechJeb_Ascent_status20"); //"Initiate gravity turn"
}
private double FixedTimeToAp()
{
if (Vessel.orbit.timeToPe < Vessel.orbit.timeToAp)
return Vessel.orbit.timeToAp - Vessel.orbit.period;
return Vessel.orbit.timeToAp;
}
private double _maxholdAPTime;
private void DriveGravityTurn()
{
//stop the intermediate "burn" when our apoapsis reaches the desired altitude
if (Orbit.ApA > AscentSettings.DesiredOrbitAltitude)
{
_mode = AscentMode.COAST_TO_APOAPSIS;
return;
}
if (FixedTimeToAp() < AscentSettings.HoldAPTime && _maxholdAPTime > AscentSettings.HoldAPTime)
{
_mode = AscentMode.HOLD_AP;
return;
}
_maxholdAPTime = Math.Max(_maxholdAPTime, FixedTimeToAp());
// fade pitch from AoA at 90% of intermediateAltitude to 0 at 95% of intermediateAltitude
double pitchfade = MuUtils.Clamp(-20 * VesselState.altitudeASL / AscentSettings.IntermediateAltitude + 19, 0.0, 1.0);
// srfvelPitch == zero AoA
AttitudeTo(SrfvelPitch() * pitchfade);
Core.Thrust.TargetThrottle = ThrottleToRaiseApoapsis(Orbit.ApR, AscentSettings.IntermediateAltitude + MainBody.Radius);
if (Core.Thrust.TargetThrottle < 1.0F)
{
Status = Localizer.Format("#MechJeb_Ascent_status19"); //"Fine tuning intermediate altitude"
return;
}
Status = Localizer.Format("#MechJeb_Ascent_status22"); //"Gravity turn"
}
private void DriveHoldAP()
{
//stop the intermediate "burn" when our apoapsis reaches the desired altitude
if (Orbit.ApA > AscentSettings.DesiredOrbitAltitude)
{
_mode = AscentMode.COAST_TO_APOAPSIS;
return;
}
// generally this is in response to typing numbers into the intermediate altitude text box and
// an accidental transition to later in the state machine
if (Orbit.ApA < 0.98 * AscentSettings.IntermediateAltitude)
{
_mode = AscentMode.GRAVITY_TURN;
return;
}
AttitudeTo(0); /* FIXME: corrective steering */
Core.Thrust.TargetThrottle = FixedTimeToAp() < AscentSettings.HoldAPTime ? 1.0F : 0.1F;
Status = Localizer.Format("#MechJeb_Ascent_status24"); //"Holding AP"
}
private void DriveCoastToApoapsis()
{
Core.Thrust.TargetThrottle = 0;
if (VesselState.altitudeASL > MainBody.RealMaxAtmosphereAltitude())
{
_mode = AscentMode.EXIT;
Core.Warp.MinimumWarp();
return;
}
//if our apoapsis has fallen too far, resume the gravity turn
if (Orbit.ApA < AscentSettings.DesiredOrbitAltitude - 1000.0)
{
_mode = AscentMode.HOLD_AP;
Core.Warp.MinimumWarp();
return;
}
Core.Thrust.TargetThrottle = 0;
// follow surface velocity to reduce flipping
AttitudeTo(SrfvelPitch());
if (Orbit.ApA < AscentSettings.DesiredOrbitAltitude)
{
Core.Warp.WarpPhysicsAtRate(1);
Core.Thrust.TargetThrottle = ThrottleToRaiseApoapsis(Orbit.ApR, AscentSettings.DesiredOrbitAltitude + MainBody.Radius);
}
else
{
if (Core.Node.Autowarp)
{
//warp at x2 physical warp:
Core.Warp.WarpPhysicsAtRate(2);
}
}
Status = Localizer.Format("#MechJeb_Ascent_status23"); //"Coasting to edge of atmosphere"
}
}
}