/
MechJebModuleAttitudeController.cs
475 lines (397 loc) · 20.3 KB
/
MechJebModuleAttitudeController.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using MuMech.AttitudeControllers;
using UnityEngine;
namespace MuMech
{
public enum AttitudeReference
{
INERTIAL, //world coordinate system.
INERTIAL_COT, //world coordinate system fixed for CoT offset.
ORBIT, //forward = prograde, left = normal plus, up = radial plus
ORBIT_HORIZONTAL, //forward = surface projection of orbit velocity, up = surface normal
SURFACE_NORTH, //forward = north, left = west, up = surface normal
SURFACE_NORTH_COT, //forward = north, left = west, up = surface normal, fixed for CoT offset
SURFACE_VELOCITY, //forward = surface frame vessel velocity, up = perpendicular component of surface normal
TARGET, //forward = toward target, up = perpendicular component of vessel heading
RELATIVE_VELOCITY, //forward = toward relative velocity direction, up = tbd
TARGET_ORIENTATION, //forward = direction target is facing, up = target up
MANEUVER_NODE, //forward = next maneuver node direction, up = tbd
MANEUVER_NODE_COT, //forward = next maneuver node direction, up = tbd, fixed for CoT offset
SUN, //forward = orbit velocity of the parent body orbiting the sun, up = radial plus of that orbit
SURFACE_HORIZONTAL //forward = surface velocity horizontal component, up = surface normal
}
[UsedImplicitly]
public class MechJebModuleAttitudeController : ComputerModule
{
private float timeCount;
private Part lastReferencePart;
public bool RCS_auto = false;
private readonly bool attitudeRCScontrol = true;
[Persistent(pass = (int)Pass.GLOBAL)]
[ValueInfoItem("#MechJeb_SteeringError", InfoItem.Category.Vessel, format = "F1", units = "º")]
//Steering error
public MovingAverage steeringError = new MovingAverage();
public bool attitudeKILLROT;
private bool attitudeChanged;
private AttitudeReference _attitudeReference = AttitudeReference.INERTIAL;
public Vector3d AxisControl { get; private set; } = Vector3d.one;
public Vector3d ActuationControl { get; private set; } = Vector3d.one;
public Vector3d OmegaTarget { get; private set; } = new Vector3d(double.NaN, double.NaN, double.NaN);
public BaseAttitudeController Controller { get; private set; }
private readonly List<BaseAttitudeController> _controllers = new List<BaseAttitudeController>();
[Persistent(pass = (int)Pass.GLOBAL)]
public int activeController = 3;
public void SetActiveController(int i)
{
activeController = i;
Controller = _controllers[activeController];
Controller.OnStart();
}
protected override void OnModuleEnabled()
{
timeCount = 50;
SetAxisControl(true, true, true);
SetActuationControl();
SetOmegaTarget();
Controller.OnModuleEnabled();
}
public AttitudeReference attitudeReference
{
get => _attitudeReference;
private set
{
if (_attitudeReference == value) return;
_attitudeReference = value;
attitudeChanged = true;
}
}
private Quaternion _attitudeTarget = Quaternion.identity;
public Quaternion attitudeTarget
{
get => _attitudeTarget;
private set
{
if (Math.Abs(Vector3d.Angle(_attitudeTarget * Vector3d.forward, value * Vector3d.forward)) > 10)
{
SetAxisControl(true, true, true);
attitudeChanged = true;
}
_attitudeTarget = value;
}
}
public Quaternion RequestedAttitude { get; private set; } = Quaternion.identity;
//[Persistent(pass = (int)Pass.Global | (int)Pass.Type), ToggleInfoItem("Use stock SAS", InfoItem.Category.Vessel)]
// Disable the use of Stock SAS for now
private readonly bool useSAS = false;
private Quaternion lastSAS;
public double attitudeError;
public Vector3d torque;
public Vector3d inertia;
public MechJebModuleAttitudeController(MechJebCore core)
: base(core)
{
Priority = 800;
_controllers.Add(new MJAttitudeController(this));
_controllers.Add(new KosAttitudeController(this));
_controllers.Add(new HybridController(this));
_controllers.Add(new BetterController(this));
Controller = new BetterController(this);
}
protected override void OnModuleDisabled()
{
if (useSAS) Part.vessel.ActionGroups.SetGroup(KSPActionGroup.SAS, false);
Controller.OnModuleDisabled();
}
public override void OnLoad(ConfigNode local, ConfigNode type, ConfigNode global)
{
base.OnLoad(local, type, global);
foreach (BaseAttitudeController attitudeController in _controllers) attitudeController.OnLoad(local, type, global);
}
public override void OnStart(PartModule.StartState state)
{
SetActiveController(activeController);
}
public override void OnSave(ConfigNode local, ConfigNode type, ConfigNode global)
{
base.OnSave(local, type, global);
foreach (BaseAttitudeController attitudeController in _controllers) attitudeController.OnSave(local, type, global);
}
public void SetAxisControl(bool pitch, bool yaw, bool roll)
{
AxisControl = new Vector3d(pitch ? 1 : 0, roll ? 1 : 0, yaw ? 1 : 0);
}
public void SetActuationControl(bool pitch = true, bool yaw = true, bool roll = true)
{
ActuationControl = new Vector3d(pitch ? 1 : 0, roll ? 1 : 0, yaw ? 1 : 0);
}
public void SetOmegaTarget(double pitch = double.NaN, double yaw = double.NaN, double roll = double.NaN)
{
OmegaTarget = new Vector3d(pitch, roll, yaw);
}
public Quaternion attitudeGetReferenceRotation(AttitudeReference reference)
{
Vector3 fwd, up;
Quaternion rotRef = Quaternion.identity;
if (Core.Target.Target == null && (reference == AttitudeReference.TARGET || reference == AttitudeReference.TARGET_ORIENTATION ||
reference == AttitudeReference.RELATIVE_VELOCITY))
{
attitudeDeactivate();
return rotRef;
}
if ((reference == AttitudeReference.MANEUVER_NODE || reference == AttitudeReference.MANEUVER_NODE_COT) &&
Vessel.patchedConicSolver.maneuverNodes.Count == 0)
{
attitudeDeactivate();
return rotRef;
}
Vector3d thrustForward = VesselState.thrustForward;
// the off-axis thrust modifications get into a fight with the differential throttle so do not use them when diffthrottle is used
if (Core.Thrust.differentialThrottle)
thrustForward = VesselState.forward;
switch (reference)
{
case AttitudeReference.INERTIAL_COT:
rotRef = Quaternion.FromToRotation(thrustForward, VesselState.forward) * rotRef;
break;
case AttitudeReference.ORBIT:
rotRef = Quaternion.LookRotation(VesselState.orbitalVelocity.normalized, VesselState.up);
break;
case AttitudeReference.ORBIT_HORIZONTAL:
rotRef = Quaternion.LookRotation(Vector3d.Exclude(VesselState.up, VesselState.orbitalVelocity.normalized), VesselState.up);
break;
case AttitudeReference.SURFACE_NORTH:
rotRef = VesselState.rotationSurface;
break;
case AttitudeReference.SURFACE_NORTH_COT:
rotRef = VesselState.rotationSurface;
rotRef = Quaternion.FromToRotation(thrustForward, VesselState.forward) * rotRef;
break;
case AttitudeReference.SURFACE_VELOCITY:
rotRef = Quaternion.LookRotation(VesselState.surfaceVelocity.normalized, VesselState.up);
break;
case AttitudeReference.TARGET:
fwd = (Core.Target.Position - Vessel.GetTransform().position).normalized;
up = Vector3d.Cross(fwd, VesselState.normalPlus);
Vector3.OrthoNormalize(ref fwd, ref up);
rotRef = Quaternion.LookRotation(fwd, up);
break;
case AttitudeReference.RELATIVE_VELOCITY:
fwd = Core.Target.RelativeVelocity.normalized;
up = Vector3d.Cross(fwd, VesselState.normalPlus);
Vector3.OrthoNormalize(ref fwd, ref up);
rotRef = Quaternion.LookRotation(fwd, up);
break;
case AttitudeReference.TARGET_ORIENTATION:
Transform targetTransform = Core.Target.Transform;
Vector3 targetUp = targetTransform.up;
rotRef = Core.Target.CanAlign
? Quaternion.LookRotation(targetTransform.forward, targetUp)
: Quaternion.LookRotation(targetUp, targetTransform.right);
break;
case AttitudeReference.MANEUVER_NODE:
fwd = Vessel.patchedConicSolver.maneuverNodes[0].GetBurnVector(Orbit);
up = Vector3d.Cross(fwd, VesselState.normalPlus);
Vector3.OrthoNormalize(ref fwd, ref up);
rotRef = Quaternion.LookRotation(fwd, up);
break;
case AttitudeReference.MANEUVER_NODE_COT:
fwd = Vessel.patchedConicSolver.maneuverNodes[0].GetBurnVector(Orbit);
up = Vector3d.Cross(fwd, VesselState.normalPlus);
Vector3.OrthoNormalize(ref fwd, ref up);
rotRef = Quaternion.LookRotation(fwd, up);
rotRef = Quaternion.FromToRotation(thrustForward, VesselState.forward) * rotRef;
break;
case AttitudeReference.SUN:
Orbit baseOrbit = Vessel.mainBody == Planetarium.fetch.Sun ? Vessel.orbit : Orbit.TopParentOrbit();
up = VesselState.CoM - Planetarium.fetch.Sun.transform.position;
fwd = Vector3d.Cross(-baseOrbit.GetOrbitNormal().xzy.normalized, up);
rotRef = Quaternion.LookRotation(fwd, up);
break;
case AttitudeReference.SURFACE_HORIZONTAL:
rotRef = Quaternion.LookRotation(Vector3d.Exclude(VesselState.up, VesselState.surfaceVelocity.normalized), VesselState.up);
break;
}
return rotRef;
}
private Vector3d attitudeWorldToReference(Vector3d vector, AttitudeReference reference)
{
return Quaternion.Inverse(attitudeGetReferenceRotation(reference)) * vector;
}
private Vector3d attitudeReferenceToWorld(Vector3d vector, AttitudeReference reference)
{
return attitudeGetReferenceRotation(reference) * vector;
}
public void attitudeTo(Quaternion attitude, AttitudeReference reference, object controller, bool AxisCtrlPitch = true,
bool AxisCtrlYaw = true, bool AxisCtrlRoll = true)
{
Users.Add(controller);
attitudeReference = reference;
attitudeTarget = attitude;
SetAxisControl(AxisCtrlPitch, AxisCtrlYaw, AxisCtrlRoll);
}
public void attitudeTo(Vector3d direction, AttitudeReference reference, object controller)
{
//double ang_diff = Math.Abs(Vector3d.Angle(attitudeGetReferenceRotation(attitudeReference) * attitudeTarget * Vector3d.forward, attitudeGetReferenceRotation(reference) * direction));
Vector3 up, dir = direction;
if (!Enabled)
up = attitudeWorldToReference(-Vessel.GetTransform().forward, reference);
else
up = attitudeWorldToReference(attitudeReferenceToWorld(attitudeTarget * Vector3d.up, reference), reference);
Vector3.OrthoNormalize(ref dir, ref up);
attitudeTo(Quaternion.LookRotation(dir, up), reference, controller);
SetAxisControl(true, true, false);
}
public void attitudeTo(double heading, double pitch, double roll, object controller, bool AxisCtrlPitch = true, bool AxisCtrlYaw = true,
bool AxisCtrlRoll = true, bool fixCOT = false)
{
Quaternion attitude = Quaternion.AngleAxis((float)heading, Vector3.up) * Quaternion.AngleAxis(-(float)pitch, Vector3.right) *
Quaternion.AngleAxis(-(float)roll, Vector3.forward);
AttitudeReference reference = fixCOT ? AttitudeReference.SURFACE_NORTH_COT : AttitudeReference.SURFACE_NORTH;
attitudeTo(attitude, reference, controller, AxisCtrlPitch,
AxisCtrlYaw, AxisCtrlRoll);
}
public void attitudeDeactivate()
{
Users.Clear();
attitudeChanged = true;
}
//angle in degrees between the vessel's current pointing direction and the attitude target, ignoring roll
public double attitudeAngleFromTarget()
{
return Enabled
? Math.Abs(Vector3d.Angle(attitudeGetReferenceRotation(attitudeReference) * attitudeTarget * Vector3d.forward, VesselState.forward))
: 0;
}
public Vector3d targetAttitude()
{
if (Enabled)
return attitudeGetReferenceRotation(attitudeReference) * attitudeTarget * Vector3d.forward;
return Vector3d.zero;
}
public override void OnFixedUpdate()
{
steeringError.value = attitudeError = attitudeAngleFromTarget();
if (useSAS)
return;
torque = VesselState.torqueAvailable;
if (Core.Thrust.differentialThrottle &&
Core.Thrust.differentialThrottleSuccess == MechJebModuleThrustController.DifferentialThrottleStatus.Success)
torque += VesselState.torqueDiffThrottle * Vessel.ctrlState.mainThrottle / 2.0;
// Inertia is a bad name. It's the "angular distance to stop"
inertia = 0.5 * Vector3d.Scale(
VesselState.angularMomentum.Sign(),
Vector3d.Scale(
Vector3d.Scale(VesselState.angularMomentum, VesselState.angularMomentum),
Vector3d.Scale(torque, VesselState.MoI).InvertNoNaN()
)
);
Controller.OnFixedUpdate();
}
public override void OnUpdate()
{
if (attitudeChanged)
{
if (attitudeReference != AttitudeReference.INERTIAL && attitudeReference != AttitudeReference.INERTIAL_COT) attitudeKILLROT = false;
Controller.Reset();
attitudeChanged = false;
}
Controller.OnUpdate();
}
public override void Drive(FlightCtrlState s)
{
// Direction we want to be facing
RequestedAttitude = attitudeGetReferenceRotation(attitudeReference) * attitudeTarget;
if (useSAS)
{
// TODO : This most likely require some love to use all the new SAS magic
RequestedAttitude = attitudeGetReferenceRotation(attitudeReference) * attitudeTarget * Quaternion.Euler(90, 0, 0);
if (!Part.vessel.ActionGroups[KSPActionGroup.SAS])
{
Part.vessel.ActionGroups.SetGroup(KSPActionGroup.SAS, true);
Part.vessel.Autopilot.SAS.SetTargetOrientation(RequestedAttitude * Vector3.up, false);
lastSAS = RequestedAttitude;
}
else if (Quaternion.Angle(lastSAS, RequestedAttitude) > 10)
{
Part.vessel.Autopilot.SAS.SetTargetOrientation(RequestedAttitude * Vector3.up, false);
lastSAS = RequestedAttitude;
}
else
{
Part.vessel.Autopilot.SAS.SetTargetOrientation(RequestedAttitude * Vector3.up, true);
}
Core.Thrust.differentialThrottleDemandedTorque = Vector3d.zero;
}
else
{
Vector3d act;
Vector3d deltaEuler;
Controller.DrivePre(s, out act, out deltaEuler);
act.Scale(ActuationControl);
SetFlightCtrlState(act, deltaEuler, s, 1);
act = new Vector3d(s.pitch, s.roll, s.yaw);
// Feed the control torque to the differential throttle
if (Core.Thrust.differentialThrottleSuccess == MechJebModuleThrustController.DifferentialThrottleStatus.Success)
Core.Thrust.differentialThrottleDemandedTorque =
-Vector3d.Scale(act, VesselState.torqueDiffThrottle * Vessel.ctrlState.mainThrottle);
}
}
private void SetFlightCtrlState(Vector3d act, Vector3d deltaEuler, FlightCtrlState s, float drive_limit)
{
bool userCommandingPitch = !Mathfx.Approx(s.pitch, s.pitchTrim, 0.1F);
bool userCommandingYaw = !Mathfx.Approx(s.yaw, s.yawTrim, 0.1F);
bool userCommandingRoll = !Mathfx.Approx(s.roll, s.rollTrim, 0.1F);
// Disable the new SAS so it won't interfere. But enable it while in timewarp for compatibility with PersistentRotation
if (TimeWarp.WarpMode != TimeWarp.Modes.HIGH || TimeWarp.CurrentRateIndex == 0)
Part.vessel.ActionGroups.SetGroup(KSPActionGroup.SAS, false);
if (attitudeKILLROT)
if (lastReferencePart != Vessel.GetReferenceTransformPart() || userCommandingPitch || userCommandingYaw || userCommandingRoll)
{
attitudeTo(Quaternion.LookRotation(Vessel.GetTransform().up, -Vessel.GetTransform().forward), AttitudeReference.INERTIAL, null);
lastReferencePart = Vessel.GetReferenceTransformPart();
}
if (userCommandingPitch)
Controller.Reset(0);
if (userCommandingRoll)
Controller.Reset(1);
if (userCommandingYaw)
Controller.Reset(2);
if (!userCommandingRoll)
if (!double.IsNaN(act.y))
s.roll = Mathf.Clamp((float)act.y, -drive_limit, drive_limit);
if (!userCommandingPitch && !userCommandingYaw)
{
if (!double.IsNaN(act.x)) s.pitch = Mathf.Clamp((float)act.x, -drive_limit, drive_limit);
if (!double.IsNaN(act.z)) s.yaw = Mathf.Clamp((float)act.z, -drive_limit, drive_limit);
}
// RCS and SAS control:
Vector3d absErr; // Absolute error (exag º)
absErr.x = Math.Abs(deltaEuler.x);
absErr.y = Math.Abs(deltaEuler.y);
absErr.z = Math.Abs(deltaEuler.z);
if (absErr.x < 0.4 && absErr.y < 0.4 && absErr.z < 0.4)
{
if (timeCount < 50)
{
timeCount++;
}
else
{
if (RCS_auto)
if (attitudeRCScontrol && Core.RCS.Users.Count == 0)
Part.vessel.ActionGroups.SetGroup(KSPActionGroup.RCS, false);
}
}
else if (absErr.x > 1.0 || absErr.y > 1.0 || absErr.z > 1.0)
{
timeCount = 0;
if (RCS_auto && (absErr.x > 3.0 || absErr.y > 3.0 || absErr.z > 3.0) &&
Core.Thrust.limiter != MechJebModuleThrustController.LimitMode.UnstableIgnition)
if (attitudeRCScontrol)
Part.vessel.ActionGroups.SetGroup(KSPActionGroup.RCS, true);
}
} // end of SetFlightCtrlState
}
}