-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpid.lua
More file actions
296 lines (265 loc) · 10.2 KB
/
Copy pathpid.lua
File metadata and controls
296 lines (265 loc) · 10.2 KB
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
--- A basic PID type and common PID operations. This may be useful
-- when working with control systems.
--
-- An introduction to PIDs can be found on [Wikipedia][wiki].
--
-- [wiki]: https://en.wikipedia.org/wiki/Proportional-integral-derivative_controller
--
-- If you are interested in using [CCSharp][ccsharp], here is the compatible [PID.cs][ccsharp-pid] file.
--
-- [ccsharp]: https://github.com/monkeymanboy/CCSharp
-- [ccsharp-pid]: https://github.com/monkeymanboy/CCSharp/blob/master/src/CCSharp/AdvancedMath/PID.cs
--
-- @module pid
-- @author TechTastic
local expect = require "cc.expect"
local expect = expect.expect
local metatable
--- Performs a PID control step if the setpoint is a scalar (number) value
--
-- @tparam pid self The PID instance
-- @tparam number value The current value being measured
-- @tparam number dt The time since the last step
-- @treturn the control output
-- @usage output = pid:step(value)
-- @usage output = pid:step(value, 0.5)
-- @local
local function scalarStep(self, value, dt)
expect(1, value, "number")
expect(2, dt, "number", "nil")
dt = dt or 1
local error = self.sp - value
local p = self.kp * error
if self.discrete then
self.integral = self.integral + error * dt
else
self.integral = self.integral + (error + self.prev_error) * dt * 0.5
end
if self.integral_min and self.integral_max then
self.integral = math.max(self.integral_min, math.min(self.integral_max, self.integral))
end
local i = self.ki * self.integral
local d = self.kd * (error - self.prev_error) / dt
self.prev_error = error
local output = p + i + d
if self.output_min and self.output_max then
return math.max(self.output_min, math.min(self.output_max, output))
end
return output
end
--- Performs a PID control step if the setpoint is a vector value
--
-- @tparam pid self The PID instance
-- @tparam number value The current value being measured
-- @tparam number dt The time since the last step
-- @treturn the control output vector
-- @usage output = pid:step(value)
-- @usage output = pid:step(value, 0.5)
-- @local
local function vectorStep(self, value, dt)
expect(1, value, "table")
if (getmetatable(value) or {}).__name ~= "vector" then expect(1, value, "vector") end
expect(2, dt, "number", "nil")
dt = dt or 1
local error = self.sp - value
local p = error * self.kp
if self.discrete then
self.integral = self.integral + error * dt
else
self.integral = self.integral + (error + self.prev_error) * dt * 0.5
end
if self.integral_min and self.integral_max then
self.integral = vector.new(
math.max(self.integral_min, math.min(self.integral_max, self.integral.x)),
math.max(self.integral_min, math.min(self.integral_max, self.integral.y)),
math.max(self.integral_min, math.min(self.integral_max, self.integral.z))
)
end
local i = self.integral * self.ki
local d = (error - self.prev_error) * (self.kd / dt)
self.prev_error = error
local output = p + i + d
if self.output_min and self.output_max then
return vector.new(
math.max(self.output_min, math.min(self.output_max, output.x)),
math.max(self.output_min, math.min(self.output_max, output.y)),
math.max(self.output_min, math.min(self.output_max, output.z))
)
end
return output
end
--- Performs a PID control step if the setpoint is a quaternion value
--
-- @tparam pid self The PID instance
-- @tparam number value The current value being measured
-- @tparam number dt The time since the last step
-- @treturn the angular velocity control output
-- @usage output = pid:step(value)
-- @usage output = pid:step(value, 0.5)
-- @local
-- @see quaternion
local function quaternionStep(self, value, dt)
expect(1, value, "table")
if (getmetatable(value) or {}).__name ~= "quaternion" then expect(1, value, "quaternion") end
expect(2, dt, "number", "nil")
dt = dt or 1
local error_quat = self.sp * value:inverse()
local error_vec = error_quat:getAxis() * error_quat:getAngle()
local p = error_vec * self.kp
if self.discrete then
self.integral = self.integral + error_vec * dt
else
self.integral = self.integral + (error_vec + self.prev_error) * dt * 0.5
end
if self.integral_min and self.integral_max then
self.integral = vector.new(
math.max(self.integral_min, math.min(self.integral_max, self.integral.x)),
math.max(self.integral_min, math.min(self.integral_max, self.integral.y)),
math.max(self.integral_min, math.min(self.integral_max, self.integral.z))
)
end
local i = self.integral * self.ki
local d = (error_vec - self.prev_error) * (self.kd / dt)
self.prev_error = error_vec
local output = p + i + d
if self.output_min and self.output_max then
return vector.new(
math.max(self.output_min, math.min(self.output_max, output.x)),
math.max(self.output_min, math.min(self.output_max, output.y)),
math.max(self.output_min, math.min(self.output_max, output.z))
)
end
return output
end
--- Constructors
--
-- @section Constructors
--- Constructs a new PID controller for either a scalar, vector, or quaternion target.
--
-- @tparam number|vector|quaternion target The setpoint to reach
-- @tparam number p Proportional gain - how aggressively to respond to the current error
-- @tparam number i Integral gain - how aggressively to eliminate accumulated error
-- @tparam number d Derivative gain - how aggressively to dampen the rate of change
-- @tparam boolean discrete Whether to treat the PID as discrete or continuous
-- @treturn The PID initialized with the given arguments
-- @usage pid = pid.new(target)
-- @export
-- @see quaternion
function new(target, p, i, d, discrete)
expect(1, target, "table", "number")
local targetMeta = getmetatable(target) or {}
if type(target) == "table" and targetMeta.__name ~= "vector" and targetMeta.__name ~= "quaternion" then
expect(1, target, "vector", "quaternion", "number")
end
expect(2, p, "number", "nil")
expect(3, i, "number", "nil")
expect(4, d, "number", "nil")
expect(5, discrete, "boolean", "nil")
local controller = {
sp = target or 1,
kp = p or 1,
ki = i or 0,
kd = d or 0,
discrete = discrete or true
}
if type(target) == "number" then
controller.step = scalarStep
controller.integral = 0
controller.prev_error = 0
elseif type(target) == "table" then
if targetMeta.__name == "vector" then
controller.step = vectorStep
controller.integral = vector.new()
controller.prev_error = vector.new()
elseif targetMeta.__name == "quaternion" then
controller.step = quaternionStep
controller.integral = vector.new()
controller.prev_error = vector.new()
end
end
return setmetatable(controller, metatable)
end
--- A PID, with a scalar, vector, or quaternion setpoint, kP, kI, and kD, both as discrete and continuous.
--
-- @type PID
local pid = {
--- The setpoint to reach. Uses [Vector](https://tweaked.cc/module/vector.html) or Quaternion types when applicable.
-- @field sp
-- @tparam number|vector|quaternion sp
-- @see quaternion
--- The proportional gain - how aggressively to respond to the current error
-- @field kp
-- @tparam number kp
--- The integral gain - how aggressively to eliminate accumulated error
-- @field ki
-- @tparam number ki
--- The derivative gain - how aggressively to dampen the rate of change
-- @field kd
-- @tparam number kd
--- Whether to treat the PID as discrete or continuous
-- @field discrete
-- @tparam boolean discrete
--- Performs a PID control step.
-- Uses [Vector](https://tweaked.cc/module/vector.html) or Quaternion types when applicable.
-- @tparam PID self The PID instance
-- @tparam number|vector|quaternion value The current value being measured
-- @tparam number dt The time since the last step
-- @treturn number|vector|quaternion The control output
-- @usage output = pid:step(value)
-- @usage output = pid:step(value, 0.5)
-- @see quaternion
step = function() end, -- to be replaced in constructor
--- Enables/disables the clamping of the output value
--
-- @tparam PID self The PID instance
-- @tparam number min The minimum clamp for the output
-- @tparam number max The maximum clamp for the output
clampOutput = function(self, min, max)
expect(2, min, "number", "nil")
if min then
expect(3, max, "number")
else
expect(3, max, "nil")
end
if min >= max then
error("Invalid limits! Min must be less than max!")
end
self.output_min = min
self.output_max = max
end,
--- Enables/disables the integral limits for anti-windup
--
-- @tparam PID self The PID instance
-- @tparam number min The minimum limit for the integral
-- @tparam number max The maximum limit for the integral
limitIntegral = function(self, min, max)
expect(2, min, "number", "nil")
if min then
expect(3, max, "number")
else
expect(3, max, "nil")
end
if min >= max then
error("Invalid limits! Min must be less than max!")
end
self.integral_min = min
self.integral_max = max
end,
--- Converts the PID instance into a human-readable string
--
-- @tparam PID self The PID instance
-- @treturn string The PID instance as a human-readable string
tostring = function(self)
local mode = self.discrete and "Discrete" or "Continuous"
local sp = tostring(self.sp)
-- FIX: was using %d for Kp and Kd, which truncates floats like 0.5 → 0.
-- %g prints the shortest accurate representation of any number.
return string.format("%s PID {SP = %s, Kp = %g, Ki = %g, Kd = %g}", mode, sp, self.kp, self.ki, self.kd)
end
}
metatable = {
__name = "PID",
__index = pid,
__tostring = pid.tostring
}
return {new = new}