/
BetterController.cs
436 lines (346 loc) · 18.4 KB
/
BetterController.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
using System;
using JetBrains.Annotations;
using KSP.Localization;
using MechJebLib.Control;
using UnityEngine;
namespace MuMech.AttitudeControllers
{
internal class BetterController : BaseAttitudeController
{
private static readonly Vector3d _vector3dnan = new Vector3d(double.NaN, double.NaN, double.NaN);
private Vessel Vessel => Ac.Vessel;
/* FIXME: when you do that look at ModuleGimbal gimbalResponseSpeed and model the time delay and use the XLR11 since it has slow gimbal */
[UsedImplicitly]
[Persistent(pass = (int)(Pass.TYPE | Pass.GLOBAL))]
public readonly EditableDouble PosKp = new EditableDouble(1.98);
[UsedImplicitly]
[Persistent(pass = (int)(Pass.TYPE | Pass.GLOBAL))]
public readonly EditableDouble PosDeadband = new EditableDouble(0.002);
[UsedImplicitly]
[Persistent(pass = (int)(Pass.TYPE | Pass.GLOBAL))]
public readonly EditableDouble VelKp = new EditableDouble(10);
[UsedImplicitly]
[Persistent(pass = (int)(Pass.TYPE | Pass.GLOBAL))]
public readonly EditableDouble VelKi = new EditableDouble(20);
[UsedImplicitly]
[Persistent(pass = (int)(Pass.TYPE | Pass.GLOBAL))]
public readonly EditableDouble VelKd = new EditableDouble(0.425);
[UsedImplicitly]
[Persistent(pass = (int)(Pass.TYPE | Pass.GLOBAL))]
public readonly EditableDouble VelN = new EditableDouble(84.1994541201249);
[UsedImplicitly]
[Persistent(pass = (int)(Pass.TYPE | Pass.GLOBAL))]
public readonly EditableDouble VelB = new EditableDouble(0.994);
[UsedImplicitly]
[Persistent(pass = (int)(Pass.TYPE | Pass.GLOBAL))]
public readonly EditableDouble VelC = new EditableDouble(0.0185);
[UsedImplicitly]
[Persistent(pass = (int)(Pass.TYPE | Pass.GLOBAL))]
public readonly EditableDouble VelDeadband = new EditableDouble(0.0001);
[UsedImplicitly]
[Persistent(pass = (int)(Pass.TYPE | Pass.GLOBAL))]
public bool VelClegg;
[UsedImplicitly]
[Persistent(pass = (int)(Pass.TYPE | Pass.GLOBAL))]
public readonly EditableDouble VelSmoothIn = new EditableDouble(1);
[UsedImplicitly]
[Persistent(pass = (int)(Pass.TYPE | Pass.GLOBAL))]
public readonly EditableDouble VelSmoothOut = new EditableDouble(1);
[UsedImplicitly]
[Persistent(pass = (int)(Pass.TYPE | Pass.GLOBAL))]
public readonly EditableDouble PosSmoothIn = new EditableDouble(1);
[UsedImplicitly]
[Persistent(pass = (int)(Pass.TYPE | Pass.GLOBAL))]
public readonly EditableDouble MaxStoppingTime = new EditableDouble(2.0);
[UsedImplicitly]
[Persistent(pass = (int)(Pass.TYPE | Pass.GLOBAL))]
public readonly EditableDouble MinFlipTime = new EditableDouble(120);
[UsedImplicitly]
[Persistent(pass = (int)(Pass.TYPE | Pass.GLOBAL))]
public readonly EditableDouble RollControlRange = new EditableDouble(5);
[UsedImplicitly]
[Persistent(pass = (int)(Pass.TYPE | Pass.GLOBAL))]
public bool UseControlRange = true;
[UsedImplicitly]
[Persistent(pass = (int)(Pass.TYPE | Pass.GLOBAL))]
public bool UseFlipTime = true;
[UsedImplicitly]
[Persistent(pass = (int)(Pass.TYPE | Pass.GLOBAL))]
public bool UseStoppingTime = true;
[UsedImplicitly]
[Persistent(pass = (int)(Pass.TYPE | Pass.GLOBAL))]
public int Version = -1;
private const int SETTINGS_VERSION = 5;
private void Defaults()
{
PosKp.Val = 1.98;
PosDeadband.Val = 0.002;
VelKp.Val = 10;
VelKi.Val = 20;
VelKd.Val = 0.425;
VelN.Val = 84.1994541201249;
VelB.Val = 0.994;
VelC.Val = 0.0185;
VelDeadband.Val = 0.0001;
VelClegg = false;
VelSmoothIn.Val = 1.0;
VelSmoothOut.Val = 1.0;
PosSmoothIn.Val = 1.0;
MaxStoppingTime.Val = 2;
MinFlipTime.Val = 120;
RollControlRange.Val = 5;
Version = SETTINGS_VERSION;
}
private readonly PIDLoop[] _pid = { new PIDLoop(), new PIDLoop(), new PIDLoop() };
/* error in pitch, roll, yaw */
private Vector3d _error0 = Vector3d.zero;
private Vector3d _error1 = _vector3dnan;
/* max angular acceleration */
private Vector3d _maxAlpha = Vector3d.zero;
/* max angular rotation */
private Vector3d _maxOmega = Vector3d.zero;
private Vector3d _omega0 = _vector3dnan;
private Vector3d _targetOmega = Vector3d.zero;
private Vector3d _targetTorque = Vector3d.zero;
private Vector3d _actuation = Vector3d.zero;
/* error */
private double _errorTotal;
public BetterController(MechJebModuleAttitudeController controller) : base(controller)
{
}
public override void OnModuleEnabled()
{
if (Version < SETTINGS_VERSION)
Defaults();
Reset();
}
private const double EPS = 2.2204e-16;
public override void DrivePre(FlightCtrlState s, out Vector3d act, out Vector3d deltaEuler)
{
UpdatePredictionPI();
deltaEuler = -_error0 * Mathf.Rad2Deg;
for (int i = 0; i < 3; i++)
if (Math.Abs(_actuation[i]) < EPS || double.IsNaN(_actuation[i]))
_actuation[i] = 0;
act = _actuation;
}
private void UpdateError()
{
Transform vesselTransform = Vessel.ReferenceTransform;
// 1. The Euler(-90) here is because the unity transform puts "up" as the pointy end, which is wrong. The rotation means that
// "forward" becomes the pointy end, and "up" and "right" correctly define e.g. AoA/pitch and AoS/yaw. This is just KSP being KSP.
// 2. We then use the inverse ship rotation to transform the requested attitude into the ship frame (we do everything in the ship frame
// first, and then negate the error to get the error in the target reference frame at the end).
Quaternion deltaRotation = Quaternion.Inverse(vesselTransform.transform.rotation * Quaternion.Euler(-90, 0, 0)) * Ac.RequestedAttitude;
// get us some euler angles for the target transform
Vector3d ea = deltaRotation.eulerAngles;
double pitch = ea[0] * UtilMath.Deg2Rad;
double yaw = ea[1] * UtilMath.Deg2Rad;
double roll = ea[2] * UtilMath.Deg2Rad;
// law of cosines for the "distance" of the miss in radians
_errorTotal = Math.Acos(MuUtils.Clamp(Math.Cos(pitch) * Math.Cos(yaw), -1, 1));
// this is the initial direction of the great circle route of the requested transform
// (pitch is latitude, yaw is -longitude, and we are "navigating" from 0,0)
// doing this calculation is the ship frame is a bit easier to reason about.
var temp = new Vector3d(Math.Sin(pitch), Math.Cos(pitch) * Math.Sin(-yaw), 0);
temp = temp.normalized * _errorTotal;
// we assemble phi in the pitch, roll, yaw basis that vessel.MOI uses (right handed basis)
var phi = new Vector3d(
MuUtils.ClampRadiansPi(temp[0]), // pitch distance around the geodesic
MuUtils.ClampRadiansPi(roll),
MuUtils.ClampRadiansPi(temp[1]) // yaw distance around the geodesic
);
// apply the axis control from the parent controller
phi.Scale(Ac.AxisControl);
// the error in the ship's position is the negative of the reference position in the ship frame
_error0 = -phi;
}
private void UpdatePredictionPI()
{
_omega0 = Vessel.angularVelocityD;
UpdateError();
// lowpass filter on the error input
_error0 = _error1.IsFinite() ? _error1 + PosSmoothIn * (_error0 - _error1) : _error0;
Vector3d controlTorque = Ac.torque;
// needed to stop wiggling at higher phys warp
double warpFactor = Ac.VesselState.deltaT / 0.02;
// see https://archive.is/NqoUm and the "Alt Hold Controller", the acceleration PID is not implemented so we only
// have the first two PIDs in the cascade.
for (int i = 0; i < 3; i++)
{
double error = _error0[i];
if (Math.Abs(error) < PosDeadband)
error = 0;
else
error -= Math.Sign(error) * PosDeadband;
_maxAlpha[i] = controlTorque[i] / Vessel.MOI[i];
if (_maxAlpha[i] == 0)
_maxAlpha[i] = 1;
if (Ac.OmegaTarget[i].IsFinite())
{
_targetOmega[i] = Ac.OmegaTarget[i];
}
else
{
double posKp = PosKp / warpFactor;
double effLD = _maxAlpha[i] / (2 * posKp * posKp);
if (Math.Abs(error) <= 2 * effLD)
{
// linear ramp down of acceleration
_targetOmega[i] = -posKp * error;
}
else
{
// v = - sqrt(2 * F * x / m) is target stopping velocity based on distance
_targetOmega[i] = -Math.Sqrt(2 * _maxAlpha[i] * (Math.Abs(error) - effLD)) * Math.Sign(error);
}
if (UseStoppingTime)
{
_maxOmega[i] = _maxAlpha[i] * MaxStoppingTime;
if (UseFlipTime) _maxOmega[i] = Math.Max(_maxOmega[i], Math.PI / MinFlipTime);
_targetOmega[i] = MuUtils.Clamp(_targetOmega[i], -_maxOmega[i], _maxOmega[i]);
}
if (UseControlRange && _errorTotal * Mathf.Rad2Deg > RollControlRange)
_targetOmega[1] = 0;
}
_pid[i].Kp = VelKp / (_maxAlpha[i] * warpFactor);
_pid[i].Ki = VelKi / (_maxAlpha[i] * warpFactor * warpFactor);
_pid[i].Kd = VelKd / _maxAlpha[i];
_pid[i].N = VelN;
_pid[i].B = VelB;
_pid[i].C = VelC;
_pid[i].Ts = Ac.VesselState.deltaT;
_pid[i].SmoothIn = MuUtils.Clamp01(VelSmoothIn);
_pid[i].SmoothOut = MuUtils.Clamp01(VelSmoothOut);
_pid[i].MinOutput = -1;
_pid[i].MaxOutput = 1;
_pid[i].Deadband = VelDeadband;
_pid[i].Clegg = VelClegg;
// need the negative from the pid due to KSP's orientation of actuation
_actuation[i] = -_pid[i].Update(_targetOmega[i], _omega0[i]);
if (Math.Abs(_actuation[i]) < EPS || double.IsNaN(_actuation[i]))
_actuation[i] = 0;
_targetTorque[i] = _actuation[i] * Ac.torque[i];
if (Ac.ActuationControl[i] == 0)
Reset(i);
}
_error1 = _error0;
}
public override void Reset()
{
Reset(0);
Reset(1);
Reset(2);
}
public override void Reset(int i)
{
_pid[i].Reset();
_omega0[i] = _error0[i] = _error1[i] = double.NaN;
}
public override void GUI()
{
GUILayout.BeginHorizontal();
UseStoppingTime = GUILayout.Toggle(UseStoppingTime, "Maximum Stopping Time", GUILayout.ExpandWidth(false));
MaxStoppingTime.Text = GUILayout.TextField(MaxStoppingTime.Text, GUILayout.ExpandWidth(true), GUILayout.Width(60));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
UseFlipTime = GUILayout.Toggle(UseFlipTime, "Minimum Flip Time", GUILayout.ExpandWidth(false));
MinFlipTime.Text = GUILayout.TextField(MinFlipTime.Text, GUILayout.ExpandWidth(true), GUILayout.Width(60));
GUILayout.EndHorizontal();
if (!UseStoppingTime)
UseFlipTime = false;
GUILayout.BeginHorizontal();
UseControlRange = GUILayout.Toggle(UseControlRange, Localizer.Format("#MechJeb_HybridController_checkbox2"),
GUILayout.ExpandWidth(false)); //"RollControlRange"
RollControlRange.Text = GUILayout.TextField(RollControlRange.Text, GUILayout.ExpandWidth(true), GUILayout.Width(60));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Pos SmoothIn", GUILayout.ExpandWidth(false));
PosSmoothIn.Text = GUILayout.TextField(PosSmoothIn.Text, GUILayout.ExpandWidth(true), GUILayout.Width(50));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Pos Kp", GUILayout.ExpandWidth(false));
PosKp.Text = GUILayout.TextField(PosKp.Text, GUILayout.ExpandWidth(true), GUILayout.Width(50));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Pos Deadband", GUILayout.ExpandWidth(false));
PosDeadband.Text = GUILayout.TextField(PosDeadband.Text, GUILayout.ExpandWidth(true), GUILayout.Width(50));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Vel Kp", GUILayout.ExpandWidth(false));
VelKp.Text = GUILayout.TextField(VelKp.Text, GUILayout.ExpandWidth(true), GUILayout.Width(50));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Vel Ki", GUILayout.ExpandWidth(false));
VelKi.Text = GUILayout.TextField(VelKi.Text, GUILayout.ExpandWidth(true), GUILayout.Width(50));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Vel Kd", GUILayout.ExpandWidth(false));
VelKd.Text = GUILayout.TextField(VelKd.Text, GUILayout.ExpandWidth(true), GUILayout.Width(50));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Vel N", GUILayout.ExpandWidth(false));
VelN.Text = GUILayout.TextField(VelN.Text, GUILayout.ExpandWidth(true), GUILayout.Width(50));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Vel B", GUILayout.ExpandWidth(false));
VelB.Text = GUILayout.TextField(VelB.Text, GUILayout.ExpandWidth(true), GUILayout.Width(50));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Vel C", GUILayout.ExpandWidth(false));
VelC.Text = GUILayout.TextField(VelC.Text, GUILayout.ExpandWidth(true), GUILayout.Width(50));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Vel Deadband", GUILayout.ExpandWidth(false));
VelDeadband.Text = GUILayout.TextField(VelDeadband.Text, GUILayout.ExpandWidth(true), GUILayout.Width(50));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
VelClegg = GUILayout.Toggle(VelClegg, "Vel Clegg", GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Vel SmoothIn", GUILayout.ExpandWidth(false));
VelSmoothIn.Text = GUILayout.TextField(VelSmoothIn.Text, GUILayout.ExpandWidth(true), GUILayout.Width(50));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Vel SmoothOut", GUILayout.ExpandWidth(false));
VelSmoothOut.Text = GUILayout.TextField(VelSmoothOut.Text, GUILayout.ExpandWidth(true), GUILayout.Width(50));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
if (GUILayout.Button(Localizer.Format("Reset Tuning Values")))
Defaults();
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Error", GUILayout.ExpandWidth(true));
GUILayout.Label(MuUtils.PrettyPrint(_error0), GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("TargetOmega", GUILayout.ExpandWidth(true));
GUILayout.Label(MuUtils.PrettyPrint(_targetOmega), GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Omega", GUILayout.ExpandWidth(true));
GUILayout.Label(MuUtils.PrettyPrint(_omega0), GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("MaxOmega", GUILayout.ExpandWidth(true));
GUILayout.Label(MuUtils.PrettyPrint(_maxOmega), GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label(Localizer.Format("#MechJeb_HybridController_label2"), GUILayout.ExpandWidth(true)); //"Actuation"
GUILayout.Label(MuUtils.PrettyPrint(_actuation), GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label(Localizer.Format("#MechJeb_HybridController_label4"), GUILayout.ExpandWidth(true)); //"TargetTorque"
GUILayout.Label(MuUtils.PrettyPrint(_targetTorque), GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label(Localizer.Format("#MechJeb_HybridController_label5"), GUILayout.ExpandWidth(true)); //"ControlTorque"
GUILayout.Label(MuUtils.PrettyPrint(Ac.torque), GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("MaxAlpha", GUILayout.ExpandWidth(true));
GUILayout.Label(MuUtils.PrettyPrint(_maxAlpha), GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
}
}
}