/
PIDController.cs
226 lines (183 loc) · 7.81 KB
/
PIDController.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
using System;
namespace MuMech
{
public class PIDController : IConfigNode
{
private double _prevError;
public double INTAccum, Kp, Ki, Kd;
private readonly double _max;
private readonly double _min;
public PIDController(double kp = 0, double ki = 0, double kd = 0, double max = double.MaxValue, double min = double.MinValue)
{
Kp = kp;
Ki = ki;
Kd = kd;
_max = max;
_min = min;
Reset();
}
public double Compute(double error)
{
INTAccum += error * TimeWarp.fixedDeltaTime;
double action = Kp * error + Ki * INTAccum + Kd * (error - _prevError) / TimeWarp.fixedDeltaTime;
double clamped = Math.Max(_min, Math.Min(_max, action));
// ReSharper disable once CompareOfFloatsByEqualityOperator
if (clamped != action)
INTAccum -= error * TimeWarp.fixedDeltaTime;
_prevError = error;
return action;
}
public void Reset() => _prevError = INTAccum = 0;
public void Load(ConfigNode node)
{
if (node.HasValue("Kp"))
Kp = Convert.ToDouble(node.GetValue("Kp"));
if (node.HasValue("Ki"))
Ki = Convert.ToDouble(node.GetValue("Ki"));
if (node.HasValue("Kd"))
Kd = Convert.ToDouble(node.GetValue("Kd"));
}
public void Save(ConfigNode node)
{
node.SetValue("Kp", Kp.ToString());
node.SetValue("Ki", Ki.ToString());
node.SetValue("Kd", Kd.ToString());
}
}
public class PIDControllerV2 : IConfigNode
{
private Vector3d _intAccum;
private Vector3d _derivativeAct;
private Vector3d _propAct;
public double Kp, Ki, Kd;
private readonly double _max;
private readonly double _min;
public PIDControllerV2(double kp = 0, double ki = 0, double kd = 0, double max = double.MaxValue, double min = double.MinValue)
{
Kp = kp;
Ki = ki;
Kd = kd;
_max = max;
_min = min;
Reset();
}
public Vector3d Compute(Vector3d error, Vector3d omega)
{
_derivativeAct = omega * Kd;
// integral actíon + Anti Windup
_intAccum.x = Math.Abs(_derivativeAct.x) < 0.6 * _max ? _intAccum.x + error.x * Ki * TimeWarp.fixedDeltaTime : 0.9 * _intAccum.x;
_intAccum.y = Math.Abs(_derivativeAct.y) < 0.6 * _max ? _intAccum.y + error.y * Ki * TimeWarp.fixedDeltaTime : 0.9 * _intAccum.y;
_intAccum.z = Math.Abs(_derivativeAct.z) < 0.6 * _max ? _intAccum.z + error.z * Ki * TimeWarp.fixedDeltaTime : 0.9 * _intAccum.z;
_propAct = error * Kp;
Vector3d action = _propAct + _derivativeAct + _intAccum;
// action clamp
action = new Vector3d(Math.Max(_min, Math.Min(_max, action.x)),
Math.Max(_min, Math.Min(_max, action.y)),
Math.Max(_min, Math.Min(_max, action.z)));
return action;
}
public Vector3d Compute(Vector3d error, Vector3d omega, Vector3d wlimit)
{
_derivativeAct = omega * Kd;
wlimit *= Kd;
// integral actíon + Anti Windup
_intAccum.x = Math.Abs(_derivativeAct.x) < 0.6 * _max ? _intAccum.x + error.x * Ki * TimeWarp.fixedDeltaTime : 0.9 * _intAccum.x;
_intAccum.y = Math.Abs(_derivativeAct.y) < 0.6 * _max ? _intAccum.y + error.y * Ki * TimeWarp.fixedDeltaTime : 0.9 * _intAccum.y;
_intAccum.z = Math.Abs(_derivativeAct.z) < 0.6 * _max ? _intAccum.z + error.z * Ki * TimeWarp.fixedDeltaTime : 0.9 * _intAccum.z;
_propAct = error * Kp;
Vector3d action = _propAct + _intAccum;
// Clamp (propAct + intAccum) to limit the angular velocity:
action = new Vector3d(Math.Max(-wlimit.x, Math.Min(wlimit.x, action.x)),
Math.Max(-wlimit.y, Math.Min(wlimit.y, action.y)),
Math.Max(-wlimit.z, Math.Min(wlimit.z, action.z)));
// add. derivative action
action += _derivativeAct;
// action clamp
action = new Vector3d(Math.Max(_min, Math.Min(_max, action.x)),
Math.Max(_min, Math.Min(_max, action.y)),
Math.Max(_min, Math.Min(_max, action.z)));
return action;
}
public void Reset() => _intAccum = Vector3d.zero;
public void Load(ConfigNode node)
{
if (node.HasValue("Kp"))
{
Kp = Convert.ToDouble(node.GetValue("Kp"));
}
if (node.HasValue("Ki"))
{
Ki = Convert.ToDouble(node.GetValue("Ki"));
}
if (node.HasValue("Kd"))
{
Kd = Convert.ToDouble(node.GetValue("Kd"));
}
}
public void Save(ConfigNode node)
{
node.SetValue("Kp", Kp.ToString());
node.SetValue("Ki", Ki.ToString());
node.SetValue("Kd", Kd.ToString());
}
}
public class PIDControllerV3 : IConfigNode
{
public Vector3d Kp, Ki, Kd, INTAccum, DerivativeAct, PropAct;
private readonly double _max;
private readonly double _min;
public PIDControllerV3(Vector3d kp, Vector3d ki, Vector3d kd, double max = double.MaxValue, double min = double.MinValue)
{
Kp = kp;
Ki = ki;
Kd = kd;
_max = max;
_min = min;
Reset();
}
public Vector3d Compute(Vector3d error, Vector3d omega, Vector3d wlimit)
{
DerivativeAct = Vector3d.Scale(omega, Kd);
wlimit = Vector3d.Scale(wlimit, Kd);
// integral actíon + Anti Windup
INTAccum.x = Math.Abs(DerivativeAct.x) < 0.6 * _max ? INTAccum.x + error.x * Ki.x * TimeWarp.fixedDeltaTime : 0.9 * INTAccum.x;
INTAccum.y = Math.Abs(DerivativeAct.y) < 0.6 * _max ? INTAccum.y + error.y * Ki.y * TimeWarp.fixedDeltaTime : 0.9 * INTAccum.y;
INTAccum.z = Math.Abs(DerivativeAct.z) < 0.6 * _max ? INTAccum.z + error.z * Ki.z * TimeWarp.fixedDeltaTime : 0.9 * INTAccum.z;
PropAct = Vector3d.Scale(error, Kp);
Vector3d action = PropAct + INTAccum;
// Clamp (propAct + intAccum) to limit the angular velocity:
action = new Vector3d(Math.Max(-wlimit.x, Math.Min(wlimit.x, action.x)),
Math.Max(-wlimit.y, Math.Min(wlimit.y, action.y)),
Math.Max(-wlimit.z, Math.Min(wlimit.z, action.z)));
// add. derivative action
action += DerivativeAct;
// action clamp
action = new Vector3d(Math.Max(_min, Math.Min(_max, action.x)),
Math.Max(_min, Math.Min(_max, action.y)),
Math.Max(_min, Math.Min(_max, action.z)));
return action;
}
public void Reset() => INTAccum = Vector3d.zero;
public void Load(ConfigNode node)
{
if (node.HasValue("Kp"))
{
Kp = ConfigNode.ParseVector3D(node.GetValue("Kp"));
}
if (node.HasValue("Ki"))
{
Ki = ConfigNode.ParseVector3D(node.GetValue("Ki"));
}
if (node.HasValue("Kd"))
{
Kd = ConfigNode.ParseVector3D(node.GetValue("Kd"));
}
}
public void Save(ConfigNode node)
{
node.SetValue("Kp", Kp.ToString());
node.SetValue("Ki", Ki.ToString());
node.SetValue("Kd", Kd.ToString());
}
}
}