-
Notifications
You must be signed in to change notification settings - Fork 0
/
ControlSystem.py
296 lines (205 loc) · 8.68 KB
/
ControlSystem.py
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
import typing
import SimMath
from SimMath import Vector
"""
This is the Glider's control system module.
It provides a PID controller class, a state machine class, and logging.
"""
class Logger:
"""
A class for logging glider and control data.
"""
def __init__(self) -> None:
"""
Initializes a Logger object.
"""
self.glider_log: list = []
self.control_log: list = []
class PIDController:
"""
Simple PID controller implementation.
Includes derivative kickback, integral windup, and output protections.
Attributes:
kp (float): Proportional gain.
ki (float): Integral gain.
kd (float): Derivative gain.
integral_limit (float): Integral windup limit.
output_limit (float): Output limit.
prev_error (float): Previous error value.
prev_input (float): Previous input value.
prev_time (float | None): Previous time value.
integral (float): Integral term.
Methods:
update(target: float, input: float, time: float) -> float:
Updates the PID controller with the given target, input, and time values.
"""
def __init__(self, kp: float = 1, ki: float = 0, kd: float = 0,
integral_limit: float = 1_000, output_limit: float = 1_000) -> None:
"""
Initializes a PIDController object.
Args:
kp (float): Proportional gain (default: 1).
ki (float): Integral gain (default: 0).
kd (float): Derivative gain (default: 0).
integral_limit (float): Integral windup limit (default: 1000).
output_limit (float): Output limit (default: 1000).
Returns:
None
"""
# Tuning parameters
self.kp: float = kp
self.ki: float = ki
self.kd: float = kd
# Integral windup limit
self.integral_limit: float = integral_limit
# Output limit
self.output_limit: float = output_limit
# Previous values
self.prev_error: float = 0.0
self.prev_input: float = 0.0
self.prev_time: float | None = None
# Integral term
self.integral: float = 0.0
def update(self, target: float, input: float, time: float) -> float:
"""
Updates the PID controller with the given target, input, and time values.
Args:
target (float): The desired target value.
input (float): The current input value.
time (float): The current time value.
Returns:
float: The calculated output value.
"""
error = target - input
# Don't return anything on first call
if self.prev_time is None:
self.prev_error = error
self.prev_time = time
self.prev_input = input
return 0
time_delta = time - self.prev_time
# Integral windup prevention
self.integral = SimMath.clamp_mag(self.integral + (error * time_delta), self.integral_limit)
# Derivative kickback prevention
derivative = (error - self.prev_error) / time_delta
output = (self.kp * error) + (self.ki * self.integral) + (self.kd * derivative)
# Store previous values
self.prev_error = error
self.prev_time = time
self.prev_input = input
return SimMath.clamp_mag(output, self.output_limit)
"""
Typedef for the StateMachine class
TODO: This may need to be a full class
"""
State = int
StateGraph = typing.Dict[State, typing.List[State]]
diving = State(0)
surfacing = State(1)
class StateMachine:
"""
A class representing a state machine.
TODO: Very limited, rules for state transition are not defined
Attributes:
state_graph (dict[State, list[State]]): A dictionary representing the state graph.
initial_state (State): The initial state of the state machine.
state (State): The current state of the state machine.
"""
def __init__(self, state_graph: StateGraph, initial_state: State) -> None:
"""
Initializes a new instance of the StateMachine class.
Args:
state_graph (dict[State, list[State]]): A dictionary representing the state graph.
initial_state (State): The initial state of the state machine.
"""
self.state: State = initial_state
self.state_graph: StateGraph = state_graph
def next(self) -> None:
"""
Moves the state machine to the next state.
"""
self.state = self.state_graph[self.state][0]
class ControlSystem:
"""
The ControlSystem class represents the control system for the glider simulation.
Attributes:
state_machine (StateMachine): The state machine for managing the glider's state.
frequency (int): The control system frequency.
period (float): The control system period.
time (float): The current time.
prev_update_time (float): The time of the previous update.
prev_command (float): The previous command value.
pid_depth (PIDController): The PID controller for depth control.
pid_v_vel (PIDController): The PID controller for vertical velocity control.
pid_v_acc (PIDController): The PID controller for vertical acceleration control.
min_depth (float): The minimum depth for the glider.
max_depth (float): The maximum depth for the glider.
target_depth (float): The target depth for the glider.
logger (Logger): The logger for logging control system data.
"""
def __init__(self, config: dict) -> None:
"""
Initializes a new instance of the ControlSystem class.
Args:
config (dict): The configuration parameters for the control system.
"""
# Initialize state machine
state_graph: StateGraph = {
diving : [surfacing],
surfacing : [diving]
}
self.state_machine: StateMachine = StateMachine(state_graph, diving)
self.frequency: int = config["frequency"]
self.period: float = 1.0 / self.frequency
self.time: float = 0.0
self.prev_update_time: float = self.time
self.prev_command: float = 0.0
# Create cascading PID controllers
self.pid_depth = PIDController(**config['pid_depth'])
self.pid_v_vel = PIDController(**config['pid_v_vel'])
self.pid_v_acc = PIDController(**config['pid_v_acc'])
# Glide path parameters
self.min_depth: float = config["high_depth"]
self.max_depth: float = config["low_depth"]
self.target_depth: float = self.max_depth
# Logging
self.logger = Logger()
def calc_acc(self, position: Vector, velocity: Vector, acceleration: Vector, tank: float, time: float,
other_to_log: list = []) -> float:
"""
Calculates the acceleration command for the glider.
Args:
position (Vector): The current position of the glider.
velocity (Vector): The current velocity of the glider.
acceleration (Vector): The current acceleration of the glider.
tank (float): The current tank level.
time (float): The current time.
Returns:
float: The acceleration command for the glider.
"""
self.time = time
self.logger.glider_log.append([time,
position.x(), position.y(), position.z(),
velocity, acceleration,
(tank - 1) * 10] + other_to_log)
if time < self.prev_update_time + self.period:
return self.prev_command
self.prev_update_time = time
# Swap states when needed
# TODO: There needs to be a better way to do this (the state machine should do it with a single method call)
if self.state_machine.state == diving:
if position.z() <= self.target_depth:
self.target_depth = self.min_depth
self.state_machine.next()
else:
if position.z() >= self.target_depth:
self.target_depth = self.max_depth
self.state_machine.next()
# depth -> v_vel -> v_acc
pid_depth_output = self.pid_depth.update(self.target_depth, position.z(), time)
pid_v_vel_output = self.pid_v_vel.update(pid_depth_output, velocity.z(), time)
pid_v_acc_output = self.pid_v_acc.update(pid_v_vel_output, acceleration.z(), time)
self.logger.control_log.append([time, self.target_depth, pid_depth_output, pid_v_vel_output, pid_v_acc_output])
command = -pid_v_acc_output
self.prev_command = command
return command