/
HybridController.cs
212 lines (168 loc) · 8.99 KB
/
HybridController.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
using System;
using JetBrains.Annotations;
using KSP.Localization;
using UnityEngine;
namespace MuMech.AttitudeControllers
{
internal class HybridController : BaseAttitudeController
{
[UsedImplicitly]
[Persistent(pass = (int)Pass.GLOBAL)]
public readonly EditableDouble maxStoppingTime = new EditableDouble(2);
[UsedImplicitly]
[Persistent(pass = (int)Pass.GLOBAL)]
public readonly EditableDoubleMult rollControlRange = new EditableDoubleMult(5 * Mathf.Deg2Rad, Mathf.Deg2Rad);
[UsedImplicitly]
[Persistent(pass = (int)Pass.GLOBAL)]
public bool useControlRange = true;
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);
[Persistent(pass = (int)Pass.GLOBAL)]
public bool useInertia = 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 HybridController(MechJebModuleAttitudeController controller) : base(controller)
{
}
private const double EPSILON = 1e-16;
public override void DrivePre(FlightCtrlState s, out Vector3d act, out Vector3d deltaEuler)
{
UpdatePredictionPI();
UpdateControl(s);
deltaEuler = phiVector * Mathf.Rad2Deg;
act = Actuation;
}
public void UpdatePhi()
{
Transform vesselTransform = ac.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.
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
phiTotal = 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)
var temp = new Vector3d(Math.Sin(pitch), Math.Cos(pitch) * Math.Sin(-yaw), 0);
temp = temp.normalized * phiTotal;
// 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
);
phi.Scale(ac.AxisControl);
if (useInertia)
phi -= ac.inertia;
phiVector = phi;
}
private void UpdatePredictionPI()
{
Omega = -ac.Vessel.angularVelocity;
UpdatePhi();
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 (useControlRange && 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(FlightCtrlState c)
{
/* TODO: static engine torque and/or differential throttle */
for (int i = 0; i < 3; i++)
{
Actuation[i] = TargetTorque[i] / ControlTorque[i];
if (Math.Abs(Actuation[i]) < EPSILON || double.IsNaN(Actuation[i]))
Actuation[i] = 0;
}
}
public override void GUI()
{
useInertia = GUILayout.Toggle(useInertia, Localizer.Format("#MechJeb_HybridController_checkbox1")); //"useInertia"
GUILayout.BeginHorizontal();
GUILayout.Label(Localizer.Format("#MechJeb_HybridController_label1"), GUILayout.ExpandWidth(false)); //"MaxStoppingTime"
maxStoppingTime.text = GUILayout.TextField(maxStoppingTime.text, GUILayout.ExpandWidth(true), GUILayout.Width(60));
GUILayout.EndHorizontal();
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();
// Not used yet
//GuiUtils.SimpleTextBox("Maximum Relative Angular Velocity", kWlimit, "%");
//double tmp_kWlimit = kWlimit;
//tmp_kWlimit = (EditableDouble)GUILayout.HorizontalSlider((float)tmp_kWlimit, 0.0F, 1.0F);
//
//const int sliderPrecision = 3;
//if (Math.Round(Math.Abs(tmp_kWlimit - kWlimit), sliderPrecision) > 0)
//{
// kWlimit.val = Math.Round(tmp_kWlimit, sliderPrecision);
//}
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_label3"), GUILayout.ExpandWidth(true)); //"phiVector"
GUILayout.Label(MuUtils.PrettyPrint(phiVector), GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Omega", GUILayout.ExpandWidth(true));
GUILayout.Label(MuUtils.PrettyPrint(Omega), 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_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(ControlTorque), GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label(Localizer.Format("#MechJeb_HybridController_label6"), GUILayout.ExpandWidth(true)); //"Inertia"
GUILayout.Label("|" + ac.inertia.magnitude.ToString("F3") + "| " + MuUtils.PrettyPrint(ac.inertia), GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
}
}
}