blob: 9c5484c21fc982eb5ea8085e52190836761e1804 [file] [log] [blame]
Austin Schuh085eab92020-11-26 13:54:51 -08001#!/usr/bin/python3
Austin Schuh2e554032019-01-21 15:07:27 -08002
3from aos.util.trapezoid_profile import TrapezoidProfile
4from frc971.control_loops.python import control_loop
5from frc971.control_loops.python import controls
6import numpy
7from matplotlib import pylab
8import glog
9
10
11class AngularSystemParams(object):
Ravago Jones5127ccc2022-07-31 16:32:45 -070012
Austin Schuh2e554032019-01-21 15:07:27 -080013 def __init__(self,
14 name,
15 motor,
16 G,
17 J,
18 q_pos,
19 q_vel,
20 kalman_q_pos,
21 kalman_q_vel,
22 kalman_q_voltage,
23 kalman_r_position,
Ravago Jones5127ccc2022-07-31 16:32:45 -070024 radius=None,
Austin Schuh087613b2024-02-19 12:39:08 -080025 dt=0.00505,
James Kuszmaula4226f52024-03-01 21:29:12 -080026 enable_voltage_error=True,
James Kuszmaul9ea3ff92024-06-14 15:02:15 -070027 delayed_u=0,
28 wrap_point=0.0):
Austin Schuhc1c957a2020-02-20 17:47:58 -080029 """Constructs an AngularSystemParams object.
30
31 Args:
32 motor: Motor object with the motor constants.
33 G: float, Gear ratio. Less than 1 means output moves slower than the
34 input.
35 """
Austin Schuh2e554032019-01-21 15:07:27 -080036 self.name = name
37 self.motor = motor
38 self.G = G
39 self.J = J
40 self.q_pos = q_pos
41 self.q_vel = q_vel
42 self.kalman_q_pos = kalman_q_pos
43 self.kalman_q_vel = kalman_q_vel
44 self.kalman_q_voltage = kalman_q_voltage
45 self.kalman_r_position = kalman_r_position
Milo Lin5d49af02022-02-05 12:50:32 -080046 self.radius = radius
Austin Schuh2e554032019-01-21 15:07:27 -080047 self.dt = dt
James Kuszmaula4226f52024-03-01 21:29:12 -080048 self.enable_voltage_error = enable_voltage_error
Austin Schuh087613b2024-02-19 12:39:08 -080049 self.delayed_u = delayed_u
James Kuszmaul9ea3ff92024-06-14 15:02:15 -070050 self.wrap_point = wrap_point
Austin Schuh2e554032019-01-21 15:07:27 -080051
52
53class AngularSystem(control_loop.ControlLoop):
Ravago Jones5127ccc2022-07-31 16:32:45 -070054
Austin Schuh2e554032019-01-21 15:07:27 -080055 def __init__(self, params, name="AngularSystem"):
56 super(AngularSystem, self).__init__(name)
57 self.params = params
58
59 self.motor = params.motor
60
61 # Gear ratio
62 self.G = params.G
63
64 # Moment of inertia in kg m^2
Austin Schuh2e282d12024-02-19 12:00:58 -080065 self.J_motor = self.motor.motor_inertia / (self.G**2.0)
66 self.J = params.J + self.J_motor
Austin Schuh2e554032019-01-21 15:07:27 -080067
68 # Control loop time step
69 self.dt = params.dt
70
71 # State is [position, velocity]
72 # Input is [Voltage]
Ravago Jones5127ccc2022-07-31 16:32:45 -070073 C1 = self.motor.Kt / (self.G * self.G * self.motor.resistance *
74 self.J * self.motor.Kv)
Austin Schuh2e554032019-01-21 15:07:27 -080075 C2 = self.motor.Kt / (self.G * self.J * self.motor.resistance)
76
77 self.A_continuous = numpy.matrix([[0, 1], [0, -C1]])
78
79 # Start with the unmodified input
80 self.B_continuous = numpy.matrix([[0], [C2]])
81 glog.debug(repr(self.A_continuous))
82 glog.debug(repr(self.B_continuous))
83
84 self.C = numpy.matrix([[1, 0]])
85 self.D = numpy.matrix([[0]])
86
87 self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
88 self.B_continuous, self.dt)
89
90 controllability = controls.ctrb(self.A, self.B)
91 glog.debug('Controllability of %d',
92 numpy.linalg.matrix_rank(controllability))
93 glog.debug('J: %f', self.J)
Milo Lin5d49af02022-02-05 12:50:32 -080094 glog.debug('Stall torque: %f (N m)', self.motor.stall_torque / self.G)
95 if self.params.radius is not None:
96 glog.debug('Stall force: %f (N)',
97 self.motor.stall_torque / self.G / self.params.radius)
Ravago Jones5127ccc2022-07-31 16:32:45 -070098 glog.debug(
99 'Stall force: %f (lbf)', self.motor.stall_torque / self.G /
100 self.params.radius * 0.224809)
Milo Lin5d49af02022-02-05 12:50:32 -0800101
102 glog.debug('Stall acceleration: %f (rad/s^2)',
Austin Schuh2e554032019-01-21 15:07:27 -0800103 self.motor.stall_torque / self.G / self.J)
104
Milo Lin5d49af02022-02-05 12:50:32 -0800105 glog.debug('Free speed is %f (rad/s)',
Austin Schuh2e554032019-01-21 15:07:27 -0800106 -self.B_continuous[1, 0] / self.A_continuous[1, 1] * 12.0)
107
108 self.Q = numpy.matrix([[(1.0 / (self.params.q_pos**2.0)), 0.0],
109 [0.0, (1.0 / (self.params.q_vel**2.0))]])
110
111 self.R = numpy.matrix([[(1.0 / (12.0**2.0))]])
112 self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
113
114 q_pos_ff = 0.005
115 q_vel_ff = 1.0
116 self.Qff = numpy.matrix([[(1.0 / (q_pos_ff**2.0)), 0.0],
117 [0.0, (1.0 / (q_vel_ff**2.0))]])
118
119 self.Kff = controls.TwoStateFeedForwards(self.B, self.Qff)
120
121 glog.debug('K %s', repr(self.K))
122 glog.debug('Poles are %s',
123 repr(numpy.linalg.eig(self.A - self.B * self.K)[0]))
124
125 self.Q = numpy.matrix([[(self.params.kalman_q_pos**2.0), 0.0],
126 [0.0, (self.params.kalman_q_vel**2.0)]])
127
128 self.R = numpy.matrix([[(self.params.kalman_r_position**2.0)]])
129
milind-u53ad98a2023-02-20 16:26:09 -0800130 # From testing, these continuous Q and R's appear to be good approximations of Q and R.
131 self.Q_continuous = self.Q / self.dt
132 self.R_continuous = self.R * self.dt
133
134 self.KalmanGain, self.P_steady_state = controls.kalman(A=self.A,
135 B=self.B,
136 C=self.C,
137 Q=self.Q,
138 R=self.R)
Austin Schuh2e554032019-01-21 15:07:27 -0800139
140 glog.debug('Kal %s', repr(self.KalmanGain))
141
142 # The box formed by U_min and U_max must encompass all possible values,
143 # or else Austin's code gets angry.
144 self.U_max = numpy.matrix([[12.0]])
145 self.U_min = numpy.matrix([[-12.0]])
146
147 self.InitializeState()
148
James Kuszmaul9ea3ff92024-06-14 15:02:15 -0700149 self.wrap_point = numpy.matrix([[self.params.wrap_point]])
150
Austin Schuh2e554032019-01-21 15:07:27 -0800151
152class IntegralAngularSystem(AngularSystem):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700153
Austin Schuh2e554032019-01-21 15:07:27 -0800154 def __init__(self, params, name="IntegralAngularSystem"):
155 super(IntegralAngularSystem, self).__init__(params, name=name)
156
157 self.A_continuous_unaugmented = self.A_continuous
158 self.B_continuous_unaugmented = self.B_continuous
159
160 self.A_continuous = numpy.matrix(numpy.zeros((3, 3)))
161 self.A_continuous[0:2, 0:2] = self.A_continuous_unaugmented
162 self.A_continuous[0:2, 2] = self.B_continuous_unaugmented
163
164 self.B_continuous = numpy.matrix(numpy.zeros((3, 1)))
165 self.B_continuous[0:2, 0] = self.B_continuous_unaugmented
166
167 self.C_unaugmented = self.C
168 self.C = numpy.matrix(numpy.zeros((1, 3)))
169 self.C[0:1, 0:2] = self.C_unaugmented
170
171 self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
172 self.B_continuous, self.dt)
173
Tyler Chatow6738c362019-02-16 14:12:30 -0800174 self.Q = numpy.matrix([[(self.params.kalman_q_pos**2.0), 0.0, 0.0],
175 [0.0, (self.params.kalman_q_vel**2.0), 0.0],
Ravago Jones5127ccc2022-07-31 16:32:45 -0700176 [0.0, 0.0,
177 (self.params.kalman_q_voltage**2.0)]])
Austin Schuh2e554032019-01-21 15:07:27 -0800178
179 self.R = numpy.matrix([[(self.params.kalman_r_position**2.0)]])
180
milind-u53ad98a2023-02-20 16:26:09 -0800181 # From testing, these continuous Q and R's appear to be good approximations of Q and R.
182 self.Q_continuous = self.Q / self.dt
183 self.R_continuous = self.R * self.dt
184
185 self.KalmanGain, self.P_steady_state = controls.kalman(A=self.A,
186 B=self.B,
187 C=self.C,
188 Q=self.Q,
189 R=self.R)
Austin Schuh2e554032019-01-21 15:07:27 -0800190
191 self.K_unaugmented = self.K
192 self.K = numpy.matrix(numpy.zeros((1, 3)))
193 self.K[0, 0:2] = self.K_unaugmented
James Kuszmaula4226f52024-03-01 21:29:12 -0800194 if params.enable_voltage_error:
195 self.K[0, 2] = 1
Austin Schuh2e554032019-01-21 15:07:27 -0800196
197 self.Kff = numpy.concatenate(
198 (self.Kff, numpy.matrix(numpy.zeros((1, 1)))), axis=1)
199
200 self.InitializeState()
201
James Kuszmaul9ea3ff92024-06-14 15:02:15 -0700202 self.wrap_point = numpy.matrix([[self.params.wrap_point]])
203
Austin Schuh2e554032019-01-21 15:07:27 -0800204
205def RunTest(plant,
206 end_goal,
207 controller,
208 observer=None,
209 duration=1.0,
210 use_profile=True,
211 kick_time=0.5,
Lee Mracek28795ef2019-01-27 05:29:37 -0500212 kick_magnitude=0.0,
213 max_velocity=10.0,
214 max_acceleration=70.0):
Austin Schuh2e554032019-01-21 15:07:27 -0800215 """Runs the plant with an initial condition and goal.
216
217 Args:
218 plant: plant object to use.
219 end_goal: end_goal state.
220 controller: AngularSystem object to get K from, or None if we should
221 use plant.
222 observer: AngularSystem object to use for the observer, or None if we
223 should use the actual state.
224 duration: float, time in seconds to run the simulation for.
225 kick_time: float, time in seconds to kick the robot.
226 kick_magnitude: float, disturbance in volts to apply.
Lee Mracek28795ef2019-01-27 05:29:37 -0500227 max_velocity: float, The maximum velocity for the profile.
228 max_acceleration: float, The maximum acceleration for the profile.
Austin Schuh2e554032019-01-21 15:07:27 -0800229 """
230 t_plot = []
231 x_plot = []
232 v_plot = []
233 a_plot = []
Austin Schuha5aa9362022-02-07 21:26:08 -0800234 motor_current_plot = []
235 battery_current_plot = []
Austin Schuh2e554032019-01-21 15:07:27 -0800236 x_goal_plot = []
237 v_goal_plot = []
238 x_hat_plot = []
239 u_plot = []
Austin Schuh2e282d12024-02-19 12:00:58 -0800240 power_rotor_plot = []
241 power_mechanism_plot = []
242 power_overall_plot = []
243 power_electrical_plot = []
Austin Schuh2e554032019-01-21 15:07:27 -0800244 offset_plot = []
245
246 if controller is None:
247 controller = plant
248
249 vbat = 12.0
250
Tyler Chatow6738c362019-02-16 14:12:30 -0800251 goal = numpy.concatenate((plant.X, numpy.matrix(numpy.zeros((1, 1)))),
252 axis=0)
Austin Schuh2e554032019-01-21 15:07:27 -0800253
254 profile = TrapezoidProfile(plant.dt)
Lee Mracek28795ef2019-01-27 05:29:37 -0500255 profile.set_maximum_acceleration(max_acceleration)
256 profile.set_maximum_velocity(max_velocity)
Austin Schuh2e554032019-01-21 15:07:27 -0800257 profile.SetGoal(goal[0, 0])
258
259 U_last = numpy.matrix(numpy.zeros((1, 1)))
260 iterations = int(duration / plant.dt)
Austin Schuh5ea48472021-02-02 20:46:41 -0800261 for i in range(iterations):
Austin Schuh2e554032019-01-21 15:07:27 -0800262 t = i * plant.dt
263 observer.Y = plant.Y
264 observer.CorrectObserver(U_last)
265
266 offset_plot.append(observer.X_hat[2, 0])
267 x_hat_plot.append(observer.X_hat[0, 0])
268
Ravago Jones5127ccc2022-07-31 16:32:45 -0700269 next_goal = numpy.concatenate((profile.Update(
270 end_goal[0, 0], end_goal[1, 0]), numpy.matrix(numpy.zeros(
271 (1, 1)))),
272 axis=0)
Austin Schuh2e554032019-01-21 15:07:27 -0800273
274 ff_U = controller.Kff * (next_goal - observer.A * goal)
275
276 if use_profile:
277 U_uncapped = controller.K * (goal - observer.X_hat) + ff_U
278 x_goal_plot.append(goal[0, 0])
279 v_goal_plot.append(goal[1, 0])
280 else:
281 U_uncapped = controller.K * (end_goal - observer.X_hat)
282 x_goal_plot.append(end_goal[0, 0])
283 v_goal_plot.append(end_goal[1, 0])
284
285 U = U_uncapped.copy()
Austin Schuha5aa9362022-02-07 21:26:08 -0800286
Austin Schuh2e554032019-01-21 15:07:27 -0800287 U[0, 0] = numpy.clip(U[0, 0], -vbat, vbat)
Austin Schuha5aa9362022-02-07 21:26:08 -0800288
Ravago Jones5127ccc2022-07-31 16:32:45 -0700289 motor_current = (U[0, 0] - plant.X[1, 0] / plant.G /
290 plant.motor.Kv) / plant.motor.resistance
Austin Schuha5aa9362022-02-07 21:26:08 -0800291 motor_current_plot.append(motor_current)
Austin Schuh2e282d12024-02-19 12:00:58 -0800292 battery_current = U[0, 0] * motor_current / vbat
293 power_electrical_plot.append(battery_current * vbat)
Austin Schuha5aa9362022-02-07 21:26:08 -0800294 battery_current_plot.append(battery_current)
Austin Schuh2e282d12024-02-19 12:00:58 -0800295
296 # Instantaneous acceleration.
297 X_dot = plant.A_continuous * plant.X + plant.B_continuous * U
298 # Torque = J * alpha (accel).
299 power_rotor_plot.append(X_dot[1, 0] * plant.J_motor * plant.X[1, 0])
300 power_mechanism_plot.append(X_dot[1, 0] * plant.params.J *
301 plant.X[1, 0])
302 power_overall_plot.append(X_dot[1, 0] * plant.J * plant.X[1, 0])
303
Austin Schuh2e554032019-01-21 15:07:27 -0800304 x_plot.append(plant.X[0, 0])
305
306 if v_plot:
307 last_v = v_plot[-1]
308 else:
309 last_v = 0
310
311 v_plot.append(plant.X[1, 0])
312 a_plot.append((v_plot[-1] - last_v) / plant.dt)
313
314 u_offset = 0.0
315 if t >= kick_time:
316 u_offset = kick_magnitude
317 plant.Update(U + u_offset)
318
319 observer.PredictObserver(U)
320
321 t_plot.append(t)
322 u_plot.append(U[0, 0])
323
324 ff_U -= U_uncapped - U
325 goal = controller.A * goal + controller.B * ff_U
326
327 if U[0, 0] != U_uncapped[0, 0]:
Ravago Jones5127ccc2022-07-31 16:32:45 -0700328 profile.MoveCurrentState(numpy.matrix([[goal[0, 0]], [goal[1,
329 0]]]))
Austin Schuh2e554032019-01-21 15:07:27 -0800330
331 glog.debug('Time: %f', t_plot[-1])
332 glog.debug('goal_error %s', repr(end_goal - goal))
333 glog.debug('error %s', repr(observer.X_hat - end_goal))
334
Austin Schuh2e282d12024-02-19 12:00:58 -0800335 pylab.suptitle(f'Gear ratio {plant.G}')
336 position_ax1 = pylab.subplot(3, 1, 1)
337 position_ax1.plot(t_plot, x_plot, label='x')
338 position_ax1.plot(t_plot, x_hat_plot, label='x_hat')
339 position_ax1.plot(t_plot, x_goal_plot, label='x_goal')
Austin Schuh2e554032019-01-21 15:07:27 -0800340
Austin Schuh2e282d12024-02-19 12:00:58 -0800341 power_ax2 = position_ax1.twinx()
342 power_ax2.set_xlabel("time(s)")
343 power_ax2.set_ylabel("Power (W)")
344 power_ax2.plot(t_plot, power_rotor_plot, label='Rotor power')
345 power_ax2.plot(t_plot, power_mechanism_plot, label='Mechanism power')
346 power_ax2.plot(t_plot,
347 power_overall_plot,
348 label='Overall mechanical power')
349 power_ax2.plot(t_plot, power_electrical_plot, label='Electrical power')
350
351 position_ax1.legend()
352 power_ax2.legend(loc='lower right')
353
354 voltage_ax1 = pylab.subplot(3, 1, 2)
355 voltage_ax1.plot(t_plot, u_plot, label='u')
356 voltage_ax1.plot(t_plot, offset_plot, label='voltage_offset')
357 voltage_ax1.legend()
Austin Schuh2e554032019-01-21 15:07:27 -0800358
Austin Schuha5aa9362022-02-07 21:26:08 -0800359 ax1 = pylab.subplot(3, 1, 3)
360 ax1.set_xlabel("time(s)")
361 ax1.set_ylabel("rad/s^2")
Austin Schuh2e282d12024-02-19 12:00:58 -0800362 ax1.plot(t_plot, a_plot, label='acceleration')
Austin Schuha5aa9362022-02-07 21:26:08 -0800363
364 ax2 = ax1.twinx()
365 ax2.set_xlabel("time(s)")
366 ax2.set_ylabel("Amps")
367 ax2.plot(t_plot, battery_current_plot, 'g', label='battery')
368 ax2.plot(t_plot, motor_current_plot, 'r', label='motor')
Austin Schuh2e554032019-01-21 15:07:27 -0800369 pylab.legend()
370
371 pylab.show()
372
373
Austin Schuh9d9d3742019-02-15 23:00:13 -0800374def PlotStep(params, R, plant_params=None):
Austin Schuh2e554032019-01-21 15:07:27 -0800375 """Plots a step move to the goal.
376
377 Args:
Austin Schuh9d9d3742019-02-15 23:00:13 -0800378 params: AngularSystemParams for the controller and observer
379 plant_params: AngularSystemParams for the plant. Defaults to params if
380 plant_params is None.
Austin Schuh2e554032019-01-21 15:07:27 -0800381 R: numpy.matrix(2, 1), the goal"""
Austin Schuh9d9d3742019-02-15 23:00:13 -0800382 plant = AngularSystem(plant_params or params, params.name)
Austin Schuh2e554032019-01-21 15:07:27 -0800383 controller = IntegralAngularSystem(params, params.name)
384 observer = IntegralAngularSystem(params, params.name)
385
386 # Test moving the system.
387 initial_X = numpy.matrix([[0.0], [0.0]])
388 augmented_R = numpy.matrix(numpy.zeros((3, 1)))
389 augmented_R[0:2, :] = R
Ravago Jones5127ccc2022-07-31 16:32:45 -0700390 RunTest(plant,
391 end_goal=augmented_R,
392 controller=controller,
393 observer=observer,
394 duration=2.0,
395 use_profile=False,
396 kick_time=1.0,
397 kick_magnitude=0.0)
Austin Schuh2e554032019-01-21 15:07:27 -0800398
399
Austin Schuh9d9d3742019-02-15 23:00:13 -0800400def PlotKick(params, R, plant_params=None):
Austin Schuh2e554032019-01-21 15:07:27 -0800401 """Plots a step motion with a kick at 1.0 seconds.
402
403 Args:
Austin Schuh9d9d3742019-02-15 23:00:13 -0800404 params: AngularSystemParams for the controller and observer
405 plant_params: AngularSystemParams for the plant. Defaults to params if
406 plant_params is None.
Austin Schuh2e554032019-01-21 15:07:27 -0800407 R: numpy.matrix(2, 1), the goal"""
Austin Schuh9d9d3742019-02-15 23:00:13 -0800408 plant = AngularSystem(plant_params or params, params.name)
Austin Schuh2e554032019-01-21 15:07:27 -0800409 controller = IntegralAngularSystem(params, params.name)
410 observer = IntegralAngularSystem(params, params.name)
411
412 # Test moving the system.
413 initial_X = numpy.matrix([[0.0], [0.0]])
414 augmented_R = numpy.matrix(numpy.zeros((3, 1)))
415 augmented_R[0:2, :] = R
Ravago Jones5127ccc2022-07-31 16:32:45 -0700416 RunTest(plant,
417 end_goal=augmented_R,
418 controller=controller,
419 observer=observer,
420 duration=2.0,
421 use_profile=False,
422 kick_time=1.0,
423 kick_magnitude=2.0)
Austin Schuh2e554032019-01-21 15:07:27 -0800424
425
Austin Schuh9d9d3742019-02-15 23:00:13 -0800426def PlotMotion(params,
427 R,
428 max_velocity=10.0,
429 max_acceleration=70.0,
430 plant_params=None):
Austin Schuh2e554032019-01-21 15:07:27 -0800431 """Plots a trapezoidal motion.
432
433 Args:
Austin Schuh9d9d3742019-02-15 23:00:13 -0800434 params: AngularSystemParams for the controller and observer
435 plant_params: AngularSystemParams for the plant. Defaults to params if
436 plant_params is None.
Austin Schuh2e554032019-01-21 15:07:27 -0800437 R: numpy.matrix(2, 1), the goal,
Lee Mracek28795ef2019-01-27 05:29:37 -0500438 max_velocity: float, The max velocity of the profile.
439 max_acceleration: float, The max acceleration of the profile.
Austin Schuh2e554032019-01-21 15:07:27 -0800440 """
Austin Schuh9d9d3742019-02-15 23:00:13 -0800441 plant = AngularSystem(plant_params or params, params.name)
Austin Schuh2e554032019-01-21 15:07:27 -0800442 controller = IntegralAngularSystem(params, params.name)
443 observer = IntegralAngularSystem(params, params.name)
444
445 # Test moving the system.
446 initial_X = numpy.matrix([[0.0], [0.0]])
447 augmented_R = numpy.matrix(numpy.zeros((3, 1)))
448 augmented_R[0:2, :] = R
Ravago Jones5127ccc2022-07-31 16:32:45 -0700449 RunTest(plant,
450 end_goal=augmented_R,
451 controller=controller,
452 observer=observer,
453 duration=2.0,
454 use_profile=True,
455 max_velocity=max_velocity,
456 max_acceleration=max_acceleration)
Austin Schuh2e554032019-01-21 15:07:27 -0800457
458
milind-u53ad98a2023-02-20 16:26:09 -0800459def WriteAngularSystem(params,
460 plant_files,
461 controller_files,
462 year_namespaces,
463 plant_type='StateFeedbackPlant',
464 observer_type='StateFeedbackObserver'):
Austin Schuh2e554032019-01-21 15:07:27 -0800465 """Writes out the constants for a angular system to a file.
466
467 Args:
Tyler Chatowd3afdef2019-04-06 22:15:26 -0700468 params: list of AngularSystemParams or AngularSystemParams, the
469 parameters defining the system.
Austin Schuh2e554032019-01-21 15:07:27 -0800470 plant_files: list of strings, the cc and h files for the plant.
471 controller_files: list of strings, the cc and h files for the integral
472 controller.
473 year_namespaces: list of strings, the namespace list to use.
474 """
475 # Write the generated constants out to a file.
Tyler Chatowd3afdef2019-04-06 22:15:26 -0700476 angular_systems = []
477 integral_angular_systems = []
478
479 if type(params) is list:
480 name = params[0].name
481 for index, param in enumerate(params):
482 angular_systems.append(
483 AngularSystem(param, param.name + str(index)))
484 integral_angular_systems.append(
Ravago Jones26f7ad02021-02-05 15:45:59 -0800485 IntegralAngularSystem(param,
486 'Integral' + param.name + str(index)))
Tyler Chatowd3afdef2019-04-06 22:15:26 -0700487 else:
488 name = params.name
489 angular_systems.append(AngularSystem(params, params.name))
490 integral_angular_systems.append(
491 IntegralAngularSystem(params, 'Integral' + params.name))
492
Ravago Jones5127ccc2022-07-31 16:32:45 -0700493 loop_writer = control_loop.ControlLoopWriter(name,
494 angular_systems,
milind-u53ad98a2023-02-20 16:26:09 -0800495 namespaces=year_namespaces,
496 plant_type=plant_type,
497 observer_type=observer_type)
Lee Mracek17cb4892019-02-07 11:24:49 -0500498 loop_writer.AddConstant(
Tyler Chatowd3afdef2019-04-06 22:15:26 -0700499 control_loop.Constant('kOutputRatio', '%f', angular_systems[0].G))
Lee Mracek17cb4892019-02-07 11:24:49 -0500500 loop_writer.AddConstant(
Ravago Jones26f7ad02021-02-05 15:45:59 -0800501 control_loop.Constant('kFreeSpeed', '%f',
502 angular_systems[0].motor.free_speed))
James Kuszmauleeb98e92024-01-14 22:15:32 -0800503 loop_writer.Write(plant_files[0], plant_files[1],
504 None if len(plant_files) < 3 else plant_files[2])
Austin Schuh2e554032019-01-21 15:07:27 -0800505
Austin Schuh2e554032019-01-21 15:07:27 -0800506 integral_loop_writer = control_loop.ControlLoopWriter(
Tyler Chatowd3afdef2019-04-06 22:15:26 -0700507 'Integral' + name,
508 integral_angular_systems,
milind-u53ad98a2023-02-20 16:26:09 -0800509 namespaces=year_namespaces,
510 plant_type=plant_type,
511 observer_type=observer_type)
James Kuszmauleeb98e92024-01-14 22:15:32 -0800512 integral_loop_writer.Write(
513 controller_files[0], controller_files[1],
514 None if len(controller_files) < 3 else controller_files[2])