/
KosAttitudeController.cs
195 lines (154 loc) · 7.42 KB
/
KosAttitudeController.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
using System;
using UnityEngine;
namespace MuMech.AttitudeControllers
{
internal class KosAttitudeController : BaseAttitudeController
{
[Persistent(pass = (int)Pass.GLOBAL)]
private readonly EditableDouble maxStoppingTime = new EditableDouble(2);
[Persistent(pass = (int)Pass.GLOBAL)]
private readonly EditableDoubleMult rollControlRange = new EditableDoubleMult(5 * Mathf.Deg2Rad, Mathf.Deg2Rad);
//public double RollControlRange {
// get { return this.rollControlRange; }
// set { this.rollControlRange.val = Math.Max(EPSILON, Math.Min(Math.PI, value)); }
//}
public TorquePI pitchPI = new TorquePI();
public TorquePI yawPI = new TorquePI();
public TorquePI rollPI = new TorquePI();
public KosPIDLoop pitchRatePI = new KosPIDLoop(1, 0.1, 0, extraUnwind: true);
public KosPIDLoop yawRatePI = new KosPIDLoop(1, 0.1, 0, extraUnwind: true);
public KosPIDLoop rollRatePI = new KosPIDLoop(1, 0.1, 0, extraUnwind: true);
private Vector3d Actuation = Vector3d.zero;
private Vector3d TargetTorque = Vector3d.zero;
private Vector3d Omega = Vector3d.zero;
/* error */
private double phiTotal;
/* error in pitch, roll, yaw */
private Vector3d phiVector = Vector3d.zero;
private Vector3d TargetOmega = Vector3d.zero;
/* max angular rotation */
private Vector3d MaxOmega = Vector3d.zero;
private Vector3d ControlTorque => ac.torque;
public KosAttitudeController(MechJebModuleAttitudeController controller) : base(controller)
{
}
private const double EPSILON = 1e-16;
public override void DrivePre(FlightCtrlState s, out Vector3d act, out Vector3d deltaEuler)
{
UpdatePredictionPI();
UpdateControl();
deltaEuler = phiVector * Mathf.Rad2Deg;
act = Actuation;
}
/* temporary state vectors */
private Quaternion vesselRotation;
private Vector3d vesselForward;
private Vector3d vesselTop;
private Vector3d vesselStarboard;
private Vector3d targetForward;
private Vector3d targetTop;
/* private Vector3d targetStarboard; */
private void UpdateStateVectors()
{
/* FIXME: may get called more than once per tick */
vesselRotation = ac.Vessel.ReferenceTransform.rotation * Quaternion.Euler(-90, 0, 0);
vesselForward = vesselRotation * Vector3d.forward;
vesselTop = vesselRotation * Vector3d.up;
vesselStarboard = vesselRotation * Vector3d.right;
targetForward = ac.RequestedAttitude * Vector3d.forward;
targetTop = ac.RequestedAttitude * Vector3d.up;
/* targetStarboard = target * Vector3d.right; */
Omega = -ac.Vessel.angularVelocity;
}
public double PhiTotal()
{
UpdateStateVectors();
double PhiTotal = Vector3d.Angle(vesselForward, targetForward) * Mathf.Deg2Rad;
if (Vector3d.Angle(vesselTop, targetForward) > 90)
PhiTotal *= -1;
return PhiTotal;
}
public Vector3d PhiVector()
{
Vector3d Phi = Vector3d.zero;
Phi[0] = Vector3d.Angle(vesselForward, Vector3d.Exclude(vesselStarboard, targetForward)) * Mathf.Deg2Rad;
if (Vector3d.Angle(vesselTop, Vector3d.Exclude(vesselStarboard, targetForward)) > 90)
Phi[0] *= -1;
Phi[1] = Vector3d.Angle(vesselTop, Vector3d.Exclude(vesselForward, targetTop)) * Mathf.Deg2Rad;
if (Vector3d.Angle(vesselStarboard, Vector3d.Exclude(vesselForward, targetTop)) > 90)
Phi[1] *= -1;
Phi[2] = Vector3d.Angle(vesselForward, Vector3d.Exclude(vesselTop, targetForward)) * Mathf.Deg2Rad;
if (Vector3d.Angle(vesselStarboard, Vector3d.Exclude(vesselTop, targetForward)) > 90)
Phi[2] *= -1;
return Phi;
}
private void UpdatePredictionPI()
{
phiTotal = PhiTotal();
phiVector = PhiVector();
for (int i = 0; i < 3; i++)
{
MaxOmega[i] = ControlTorque[i] * maxStoppingTime / ac.VesselState.MoI[i];
}
TargetOmega[0] = pitchRatePI.Update(-phiVector[0], 0, MaxOmega[0]);
TargetOmega[1] = rollRatePI.Update(-phiVector[1], 0, MaxOmega[1]);
TargetOmega[2] = yawRatePI.Update(-phiVector[2], 0, MaxOmega[2]);
if (Math.Abs(phiTotal) > rollControlRange)
{
TargetOmega[1] = 0;
rollRatePI.ResetI();
}
TargetTorque[0] = pitchPI.Update(Omega[0], TargetOmega[0], ac.VesselState.MoI[0], ControlTorque[0]);
TargetTorque[1] = rollPI.Update(Omega[1], TargetOmega[1], ac.VesselState.MoI[1], ControlTorque[1]);
TargetTorque[2] = yawPI.Update(Omega[2], TargetOmega[2], ac.VesselState.MoI[2], ControlTorque[2]);
}
public override void Reset()
{
pitchPI.ResetI();
yawPI.ResetI();
rollPI.ResetI();
pitchRatePI.ResetI();
yawRatePI.ResetI();
rollRatePI.ResetI();
}
private void UpdateControl()
{
/* TODO: static engine torque and/or differential throttle */
for (int i = 0; i < 3; i++)
{
double clamp = Math.Max(Math.Abs(Actuation[i]), 0.005) * 2;
Actuation[i] = TargetTorque[i] / ControlTorque[i];
if (Math.Abs(Actuation[i]) < EPSILON || double.IsNaN(Actuation[i]))
Actuation[i] = 0;
Actuation[i] = Math.Max(Math.Min(Actuation[i], clamp), -clamp);
}
}
public override void GUI()
{
GUILayout.BeginHorizontal();
GUILayout.Label("MaxStoppingTime", GUILayout.ExpandWidth(false));
maxStoppingTime.text = GUILayout.TextField(maxStoppingTime.text, GUILayout.ExpandWidth(true), GUILayout.Width(40));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("RollControlRange", GUILayout.ExpandWidth(false));
rollControlRange.text = GUILayout.TextField(rollControlRange.text, GUILayout.ExpandWidth(true), GUILayout.Width(40));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Actuation", GUILayout.ExpandWidth(true));
GUILayout.Label(MuUtils.PrettyPrint(Actuation), GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("phiVector", GUILayout.ExpandWidth(true));
GUILayout.Label(MuUtils.PrettyPrint(phiVector), GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("TargetTorque", GUILayout.ExpandWidth(true));
GUILayout.Label(MuUtils.PrettyPrint(TargetTorque), GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("ControlTorque", GUILayout.ExpandWidth(true));
GUILayout.Label(MuUtils.PrettyPrint(ControlTorque), GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
}
}
}