Austin Schuh | 085eab9 | 2020-11-26 13:54:51 -0800 | [diff] [blame] | 1 | #!/usr/bin/python3 |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 2 | |
| 3 | from aos.util.trapezoid_profile import TrapezoidProfile |
| 4 | from frc971.control_loops.python import control_loop |
| 5 | from frc971.control_loops.python import controls |
| 6 | import numpy |
| 7 | from matplotlib import pylab |
| 8 | import glog |
| 9 | |
| 10 | |
| 11 | class AngularSystemParams(object): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 12 | |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 13 | 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 Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 24 | radius=None, |
Austin Schuh | 087613b | 2024-02-19 12:39:08 -0800 | [diff] [blame] | 25 | dt=0.00505, |
James Kuszmaul | a4226f5 | 2024-03-01 21:29:12 -0800 | [diff] [blame^] | 26 | enable_voltage_error=True, |
Austin Schuh | 087613b | 2024-02-19 12:39:08 -0800 | [diff] [blame] | 27 | delayed_u=0): |
Austin Schuh | c1c957a | 2020-02-20 17:47:58 -0800 | [diff] [blame] | 28 | """Constructs an AngularSystemParams object. |
| 29 | |
| 30 | Args: |
| 31 | motor: Motor object with the motor constants. |
| 32 | G: float, Gear ratio. Less than 1 means output moves slower than the |
| 33 | input. |
| 34 | """ |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 35 | self.name = name |
| 36 | self.motor = motor |
| 37 | self.G = G |
| 38 | self.J = J |
| 39 | self.q_pos = q_pos |
| 40 | self.q_vel = q_vel |
| 41 | self.kalman_q_pos = kalman_q_pos |
| 42 | self.kalman_q_vel = kalman_q_vel |
| 43 | self.kalman_q_voltage = kalman_q_voltage |
| 44 | self.kalman_r_position = kalman_r_position |
Milo Lin | 5d49af0 | 2022-02-05 12:50:32 -0800 | [diff] [blame] | 45 | self.radius = radius |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 46 | self.dt = dt |
James Kuszmaul | a4226f5 | 2024-03-01 21:29:12 -0800 | [diff] [blame^] | 47 | self.enable_voltage_error = enable_voltage_error |
Austin Schuh | 087613b | 2024-02-19 12:39:08 -0800 | [diff] [blame] | 48 | self.delayed_u = delayed_u |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 49 | |
| 50 | |
| 51 | class AngularSystem(control_loop.ControlLoop): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 52 | |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 53 | def __init__(self, params, name="AngularSystem"): |
| 54 | super(AngularSystem, self).__init__(name) |
| 55 | self.params = params |
| 56 | |
| 57 | self.motor = params.motor |
| 58 | |
| 59 | # Gear ratio |
| 60 | self.G = params.G |
| 61 | |
| 62 | # Moment of inertia in kg m^2 |
Austin Schuh | 2e282d1 | 2024-02-19 12:00:58 -0800 | [diff] [blame] | 63 | self.J_motor = self.motor.motor_inertia / (self.G**2.0) |
| 64 | self.J = params.J + self.J_motor |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 65 | |
| 66 | # Control loop time step |
| 67 | self.dt = params.dt |
| 68 | |
| 69 | # State is [position, velocity] |
| 70 | # Input is [Voltage] |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 71 | C1 = self.motor.Kt / (self.G * self.G * self.motor.resistance * |
| 72 | self.J * self.motor.Kv) |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 73 | C2 = self.motor.Kt / (self.G * self.J * self.motor.resistance) |
| 74 | |
| 75 | self.A_continuous = numpy.matrix([[0, 1], [0, -C1]]) |
| 76 | |
| 77 | # Start with the unmodified input |
| 78 | self.B_continuous = numpy.matrix([[0], [C2]]) |
| 79 | glog.debug(repr(self.A_continuous)) |
| 80 | glog.debug(repr(self.B_continuous)) |
| 81 | |
| 82 | self.C = numpy.matrix([[1, 0]]) |
| 83 | self.D = numpy.matrix([[0]]) |
| 84 | |
| 85 | self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, |
| 86 | self.B_continuous, self.dt) |
| 87 | |
| 88 | controllability = controls.ctrb(self.A, self.B) |
| 89 | glog.debug('Controllability of %d', |
| 90 | numpy.linalg.matrix_rank(controllability)) |
| 91 | glog.debug('J: %f', self.J) |
Milo Lin | 5d49af0 | 2022-02-05 12:50:32 -0800 | [diff] [blame] | 92 | glog.debug('Stall torque: %f (N m)', self.motor.stall_torque / self.G) |
| 93 | if self.params.radius is not None: |
| 94 | glog.debug('Stall force: %f (N)', |
| 95 | self.motor.stall_torque / self.G / self.params.radius) |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 96 | glog.debug( |
| 97 | 'Stall force: %f (lbf)', self.motor.stall_torque / self.G / |
| 98 | self.params.radius * 0.224809) |
Milo Lin | 5d49af0 | 2022-02-05 12:50:32 -0800 | [diff] [blame] | 99 | |
| 100 | glog.debug('Stall acceleration: %f (rad/s^2)', |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 101 | self.motor.stall_torque / self.G / self.J) |
| 102 | |
Milo Lin | 5d49af0 | 2022-02-05 12:50:32 -0800 | [diff] [blame] | 103 | glog.debug('Free speed is %f (rad/s)', |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 104 | -self.B_continuous[1, 0] / self.A_continuous[1, 1] * 12.0) |
| 105 | |
| 106 | self.Q = numpy.matrix([[(1.0 / (self.params.q_pos**2.0)), 0.0], |
| 107 | [0.0, (1.0 / (self.params.q_vel**2.0))]]) |
| 108 | |
| 109 | self.R = numpy.matrix([[(1.0 / (12.0**2.0))]]) |
| 110 | self.K = controls.dlqr(self.A, self.B, self.Q, self.R) |
| 111 | |
| 112 | q_pos_ff = 0.005 |
| 113 | q_vel_ff = 1.0 |
| 114 | self.Qff = numpy.matrix([[(1.0 / (q_pos_ff**2.0)), 0.0], |
| 115 | [0.0, (1.0 / (q_vel_ff**2.0))]]) |
| 116 | |
| 117 | self.Kff = controls.TwoStateFeedForwards(self.B, self.Qff) |
| 118 | |
| 119 | glog.debug('K %s', repr(self.K)) |
| 120 | glog.debug('Poles are %s', |
| 121 | repr(numpy.linalg.eig(self.A - self.B * self.K)[0])) |
| 122 | |
| 123 | self.Q = numpy.matrix([[(self.params.kalman_q_pos**2.0), 0.0], |
| 124 | [0.0, (self.params.kalman_q_vel**2.0)]]) |
| 125 | |
| 126 | self.R = numpy.matrix([[(self.params.kalman_r_position**2.0)]]) |
| 127 | |
milind-u | 53ad98a | 2023-02-20 16:26:09 -0800 | [diff] [blame] | 128 | # From testing, these continuous Q and R's appear to be good approximations of Q and R. |
| 129 | self.Q_continuous = self.Q / self.dt |
| 130 | self.R_continuous = self.R * self.dt |
| 131 | |
| 132 | self.KalmanGain, self.P_steady_state = controls.kalman(A=self.A, |
| 133 | B=self.B, |
| 134 | C=self.C, |
| 135 | Q=self.Q, |
| 136 | R=self.R) |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 137 | |
| 138 | glog.debug('Kal %s', repr(self.KalmanGain)) |
| 139 | |
| 140 | # The box formed by U_min and U_max must encompass all possible values, |
| 141 | # or else Austin's code gets angry. |
| 142 | self.U_max = numpy.matrix([[12.0]]) |
| 143 | self.U_min = numpy.matrix([[-12.0]]) |
| 144 | |
| 145 | self.InitializeState() |
| 146 | |
| 147 | |
| 148 | class IntegralAngularSystem(AngularSystem): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 149 | |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 150 | def __init__(self, params, name="IntegralAngularSystem"): |
| 151 | super(IntegralAngularSystem, self).__init__(params, name=name) |
| 152 | |
| 153 | self.A_continuous_unaugmented = self.A_continuous |
| 154 | self.B_continuous_unaugmented = self.B_continuous |
| 155 | |
| 156 | self.A_continuous = numpy.matrix(numpy.zeros((3, 3))) |
| 157 | self.A_continuous[0:2, 0:2] = self.A_continuous_unaugmented |
| 158 | self.A_continuous[0:2, 2] = self.B_continuous_unaugmented |
| 159 | |
| 160 | self.B_continuous = numpy.matrix(numpy.zeros((3, 1))) |
| 161 | self.B_continuous[0:2, 0] = self.B_continuous_unaugmented |
| 162 | |
| 163 | self.C_unaugmented = self.C |
| 164 | self.C = numpy.matrix(numpy.zeros((1, 3))) |
| 165 | self.C[0:1, 0:2] = self.C_unaugmented |
| 166 | |
| 167 | self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, |
| 168 | self.B_continuous, self.dt) |
| 169 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 170 | self.Q = numpy.matrix([[(self.params.kalman_q_pos**2.0), 0.0, 0.0], |
| 171 | [0.0, (self.params.kalman_q_vel**2.0), 0.0], |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 172 | [0.0, 0.0, |
| 173 | (self.params.kalman_q_voltage**2.0)]]) |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 174 | |
| 175 | self.R = numpy.matrix([[(self.params.kalman_r_position**2.0)]]) |
| 176 | |
milind-u | 53ad98a | 2023-02-20 16:26:09 -0800 | [diff] [blame] | 177 | # From testing, these continuous Q and R's appear to be good approximations of Q and R. |
| 178 | self.Q_continuous = self.Q / self.dt |
| 179 | self.R_continuous = self.R * self.dt |
| 180 | |
| 181 | self.KalmanGain, self.P_steady_state = controls.kalman(A=self.A, |
| 182 | B=self.B, |
| 183 | C=self.C, |
| 184 | Q=self.Q, |
| 185 | R=self.R) |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 186 | |
| 187 | self.K_unaugmented = self.K |
| 188 | self.K = numpy.matrix(numpy.zeros((1, 3))) |
| 189 | self.K[0, 0:2] = self.K_unaugmented |
James Kuszmaul | a4226f5 | 2024-03-01 21:29:12 -0800 | [diff] [blame^] | 190 | if params.enable_voltage_error: |
| 191 | self.K[0, 2] = 1 |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 192 | |
| 193 | self.Kff = numpy.concatenate( |
| 194 | (self.Kff, numpy.matrix(numpy.zeros((1, 1)))), axis=1) |
| 195 | |
| 196 | self.InitializeState() |
| 197 | |
| 198 | |
| 199 | def RunTest(plant, |
| 200 | end_goal, |
| 201 | controller, |
| 202 | observer=None, |
| 203 | duration=1.0, |
| 204 | use_profile=True, |
| 205 | kick_time=0.5, |
Lee Mracek | 28795ef | 2019-01-27 05:29:37 -0500 | [diff] [blame] | 206 | kick_magnitude=0.0, |
| 207 | max_velocity=10.0, |
| 208 | max_acceleration=70.0): |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 209 | """Runs the plant with an initial condition and goal. |
| 210 | |
| 211 | Args: |
| 212 | plant: plant object to use. |
| 213 | end_goal: end_goal state. |
| 214 | controller: AngularSystem object to get K from, or None if we should |
| 215 | use plant. |
| 216 | observer: AngularSystem object to use for the observer, or None if we |
| 217 | should use the actual state. |
| 218 | duration: float, time in seconds to run the simulation for. |
| 219 | kick_time: float, time in seconds to kick the robot. |
| 220 | kick_magnitude: float, disturbance in volts to apply. |
Lee Mracek | 28795ef | 2019-01-27 05:29:37 -0500 | [diff] [blame] | 221 | max_velocity: float, The maximum velocity for the profile. |
| 222 | max_acceleration: float, The maximum acceleration for the profile. |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 223 | """ |
| 224 | t_plot = [] |
| 225 | x_plot = [] |
| 226 | v_plot = [] |
| 227 | a_plot = [] |
Austin Schuh | a5aa936 | 2022-02-07 21:26:08 -0800 | [diff] [blame] | 228 | motor_current_plot = [] |
| 229 | battery_current_plot = [] |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 230 | x_goal_plot = [] |
| 231 | v_goal_plot = [] |
| 232 | x_hat_plot = [] |
| 233 | u_plot = [] |
Austin Schuh | 2e282d1 | 2024-02-19 12:00:58 -0800 | [diff] [blame] | 234 | power_rotor_plot = [] |
| 235 | power_mechanism_plot = [] |
| 236 | power_overall_plot = [] |
| 237 | power_electrical_plot = [] |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 238 | offset_plot = [] |
| 239 | |
| 240 | if controller is None: |
| 241 | controller = plant |
| 242 | |
| 243 | vbat = 12.0 |
| 244 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 245 | goal = numpy.concatenate((plant.X, numpy.matrix(numpy.zeros((1, 1)))), |
| 246 | axis=0) |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 247 | |
| 248 | profile = TrapezoidProfile(plant.dt) |
Lee Mracek | 28795ef | 2019-01-27 05:29:37 -0500 | [diff] [blame] | 249 | profile.set_maximum_acceleration(max_acceleration) |
| 250 | profile.set_maximum_velocity(max_velocity) |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 251 | profile.SetGoal(goal[0, 0]) |
| 252 | |
| 253 | U_last = numpy.matrix(numpy.zeros((1, 1))) |
| 254 | iterations = int(duration / plant.dt) |
Austin Schuh | 5ea4847 | 2021-02-02 20:46:41 -0800 | [diff] [blame] | 255 | for i in range(iterations): |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 256 | t = i * plant.dt |
| 257 | observer.Y = plant.Y |
| 258 | observer.CorrectObserver(U_last) |
| 259 | |
| 260 | offset_plot.append(observer.X_hat[2, 0]) |
| 261 | x_hat_plot.append(observer.X_hat[0, 0]) |
| 262 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 263 | next_goal = numpy.concatenate((profile.Update( |
| 264 | end_goal[0, 0], end_goal[1, 0]), numpy.matrix(numpy.zeros( |
| 265 | (1, 1)))), |
| 266 | axis=0) |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 267 | |
| 268 | ff_U = controller.Kff * (next_goal - observer.A * goal) |
| 269 | |
| 270 | if use_profile: |
| 271 | U_uncapped = controller.K * (goal - observer.X_hat) + ff_U |
| 272 | x_goal_plot.append(goal[0, 0]) |
| 273 | v_goal_plot.append(goal[1, 0]) |
| 274 | else: |
| 275 | U_uncapped = controller.K * (end_goal - observer.X_hat) |
| 276 | x_goal_plot.append(end_goal[0, 0]) |
| 277 | v_goal_plot.append(end_goal[1, 0]) |
| 278 | |
| 279 | U = U_uncapped.copy() |
Austin Schuh | a5aa936 | 2022-02-07 21:26:08 -0800 | [diff] [blame] | 280 | |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 281 | U[0, 0] = numpy.clip(U[0, 0], -vbat, vbat) |
Austin Schuh | a5aa936 | 2022-02-07 21:26:08 -0800 | [diff] [blame] | 282 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 283 | motor_current = (U[0, 0] - plant.X[1, 0] / plant.G / |
| 284 | plant.motor.Kv) / plant.motor.resistance |
Austin Schuh | a5aa936 | 2022-02-07 21:26:08 -0800 | [diff] [blame] | 285 | motor_current_plot.append(motor_current) |
Austin Schuh | 2e282d1 | 2024-02-19 12:00:58 -0800 | [diff] [blame] | 286 | battery_current = U[0, 0] * motor_current / vbat |
| 287 | power_electrical_plot.append(battery_current * vbat) |
Austin Schuh | a5aa936 | 2022-02-07 21:26:08 -0800 | [diff] [blame] | 288 | battery_current_plot.append(battery_current) |
Austin Schuh | 2e282d1 | 2024-02-19 12:00:58 -0800 | [diff] [blame] | 289 | |
| 290 | # Instantaneous acceleration. |
| 291 | X_dot = plant.A_continuous * plant.X + plant.B_continuous * U |
| 292 | # Torque = J * alpha (accel). |
| 293 | power_rotor_plot.append(X_dot[1, 0] * plant.J_motor * plant.X[1, 0]) |
| 294 | power_mechanism_plot.append(X_dot[1, 0] * plant.params.J * |
| 295 | plant.X[1, 0]) |
| 296 | power_overall_plot.append(X_dot[1, 0] * plant.J * plant.X[1, 0]) |
| 297 | |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 298 | x_plot.append(plant.X[0, 0]) |
| 299 | |
| 300 | if v_plot: |
| 301 | last_v = v_plot[-1] |
| 302 | else: |
| 303 | last_v = 0 |
| 304 | |
| 305 | v_plot.append(plant.X[1, 0]) |
| 306 | a_plot.append((v_plot[-1] - last_v) / plant.dt) |
| 307 | |
| 308 | u_offset = 0.0 |
| 309 | if t >= kick_time: |
| 310 | u_offset = kick_magnitude |
| 311 | plant.Update(U + u_offset) |
| 312 | |
| 313 | observer.PredictObserver(U) |
| 314 | |
| 315 | t_plot.append(t) |
| 316 | u_plot.append(U[0, 0]) |
| 317 | |
| 318 | ff_U -= U_uncapped - U |
| 319 | goal = controller.A * goal + controller.B * ff_U |
| 320 | |
| 321 | if U[0, 0] != U_uncapped[0, 0]: |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 322 | profile.MoveCurrentState(numpy.matrix([[goal[0, 0]], [goal[1, |
| 323 | 0]]])) |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 324 | |
| 325 | glog.debug('Time: %f', t_plot[-1]) |
| 326 | glog.debug('goal_error %s', repr(end_goal - goal)) |
| 327 | glog.debug('error %s', repr(observer.X_hat - end_goal)) |
| 328 | |
Austin Schuh | 2e282d1 | 2024-02-19 12:00:58 -0800 | [diff] [blame] | 329 | pylab.suptitle(f'Gear ratio {plant.G}') |
| 330 | position_ax1 = pylab.subplot(3, 1, 1) |
| 331 | position_ax1.plot(t_plot, x_plot, label='x') |
| 332 | position_ax1.plot(t_plot, x_hat_plot, label='x_hat') |
| 333 | position_ax1.plot(t_plot, x_goal_plot, label='x_goal') |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 334 | |
Austin Schuh | 2e282d1 | 2024-02-19 12:00:58 -0800 | [diff] [blame] | 335 | power_ax2 = position_ax1.twinx() |
| 336 | power_ax2.set_xlabel("time(s)") |
| 337 | power_ax2.set_ylabel("Power (W)") |
| 338 | power_ax2.plot(t_plot, power_rotor_plot, label='Rotor power') |
| 339 | power_ax2.plot(t_plot, power_mechanism_plot, label='Mechanism power') |
| 340 | power_ax2.plot(t_plot, |
| 341 | power_overall_plot, |
| 342 | label='Overall mechanical power') |
| 343 | power_ax2.plot(t_plot, power_electrical_plot, label='Electrical power') |
| 344 | |
| 345 | position_ax1.legend() |
| 346 | power_ax2.legend(loc='lower right') |
| 347 | |
| 348 | voltage_ax1 = pylab.subplot(3, 1, 2) |
| 349 | voltage_ax1.plot(t_plot, u_plot, label='u') |
| 350 | voltage_ax1.plot(t_plot, offset_plot, label='voltage_offset') |
| 351 | voltage_ax1.legend() |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 352 | |
Austin Schuh | a5aa936 | 2022-02-07 21:26:08 -0800 | [diff] [blame] | 353 | ax1 = pylab.subplot(3, 1, 3) |
| 354 | ax1.set_xlabel("time(s)") |
| 355 | ax1.set_ylabel("rad/s^2") |
Austin Schuh | 2e282d1 | 2024-02-19 12:00:58 -0800 | [diff] [blame] | 356 | ax1.plot(t_plot, a_plot, label='acceleration') |
Austin Schuh | a5aa936 | 2022-02-07 21:26:08 -0800 | [diff] [blame] | 357 | |
| 358 | ax2 = ax1.twinx() |
| 359 | ax2.set_xlabel("time(s)") |
| 360 | ax2.set_ylabel("Amps") |
| 361 | ax2.plot(t_plot, battery_current_plot, 'g', label='battery') |
| 362 | ax2.plot(t_plot, motor_current_plot, 'r', label='motor') |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 363 | pylab.legend() |
| 364 | |
| 365 | pylab.show() |
| 366 | |
| 367 | |
Austin Schuh | 9d9d374 | 2019-02-15 23:00:13 -0800 | [diff] [blame] | 368 | def PlotStep(params, R, plant_params=None): |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 369 | """Plots a step move to the goal. |
| 370 | |
| 371 | Args: |
Austin Schuh | 9d9d374 | 2019-02-15 23:00:13 -0800 | [diff] [blame] | 372 | params: AngularSystemParams for the controller and observer |
| 373 | plant_params: AngularSystemParams for the plant. Defaults to params if |
| 374 | plant_params is None. |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 375 | R: numpy.matrix(2, 1), the goal""" |
Austin Schuh | 9d9d374 | 2019-02-15 23:00:13 -0800 | [diff] [blame] | 376 | plant = AngularSystem(plant_params or params, params.name) |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 377 | controller = IntegralAngularSystem(params, params.name) |
| 378 | observer = IntegralAngularSystem(params, params.name) |
| 379 | |
| 380 | # Test moving the system. |
| 381 | initial_X = numpy.matrix([[0.0], [0.0]]) |
| 382 | augmented_R = numpy.matrix(numpy.zeros((3, 1))) |
| 383 | augmented_R[0:2, :] = R |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 384 | RunTest(plant, |
| 385 | end_goal=augmented_R, |
| 386 | controller=controller, |
| 387 | observer=observer, |
| 388 | duration=2.0, |
| 389 | use_profile=False, |
| 390 | kick_time=1.0, |
| 391 | kick_magnitude=0.0) |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 392 | |
| 393 | |
Austin Schuh | 9d9d374 | 2019-02-15 23:00:13 -0800 | [diff] [blame] | 394 | def PlotKick(params, R, plant_params=None): |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 395 | """Plots a step motion with a kick at 1.0 seconds. |
| 396 | |
| 397 | Args: |
Austin Schuh | 9d9d374 | 2019-02-15 23:00:13 -0800 | [diff] [blame] | 398 | params: AngularSystemParams for the controller and observer |
| 399 | plant_params: AngularSystemParams for the plant. Defaults to params if |
| 400 | plant_params is None. |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 401 | R: numpy.matrix(2, 1), the goal""" |
Austin Schuh | 9d9d374 | 2019-02-15 23:00:13 -0800 | [diff] [blame] | 402 | plant = AngularSystem(plant_params or params, params.name) |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 403 | controller = IntegralAngularSystem(params, params.name) |
| 404 | observer = IntegralAngularSystem(params, params.name) |
| 405 | |
| 406 | # Test moving the system. |
| 407 | initial_X = numpy.matrix([[0.0], [0.0]]) |
| 408 | augmented_R = numpy.matrix(numpy.zeros((3, 1))) |
| 409 | augmented_R[0:2, :] = R |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 410 | RunTest(plant, |
| 411 | end_goal=augmented_R, |
| 412 | controller=controller, |
| 413 | observer=observer, |
| 414 | duration=2.0, |
| 415 | use_profile=False, |
| 416 | kick_time=1.0, |
| 417 | kick_magnitude=2.0) |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 418 | |
| 419 | |
Austin Schuh | 9d9d374 | 2019-02-15 23:00:13 -0800 | [diff] [blame] | 420 | def PlotMotion(params, |
| 421 | R, |
| 422 | max_velocity=10.0, |
| 423 | max_acceleration=70.0, |
| 424 | plant_params=None): |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 425 | """Plots a trapezoidal motion. |
| 426 | |
| 427 | Args: |
Austin Schuh | 9d9d374 | 2019-02-15 23:00:13 -0800 | [diff] [blame] | 428 | params: AngularSystemParams for the controller and observer |
| 429 | plant_params: AngularSystemParams for the plant. Defaults to params if |
| 430 | plant_params is None. |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 431 | R: numpy.matrix(2, 1), the goal, |
Lee Mracek | 28795ef | 2019-01-27 05:29:37 -0500 | [diff] [blame] | 432 | max_velocity: float, The max velocity of the profile. |
| 433 | max_acceleration: float, The max acceleration of the profile. |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 434 | """ |
Austin Schuh | 9d9d374 | 2019-02-15 23:00:13 -0800 | [diff] [blame] | 435 | plant = AngularSystem(plant_params or params, params.name) |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 436 | controller = IntegralAngularSystem(params, params.name) |
| 437 | observer = IntegralAngularSystem(params, params.name) |
| 438 | |
| 439 | # Test moving the system. |
| 440 | initial_X = numpy.matrix([[0.0], [0.0]]) |
| 441 | augmented_R = numpy.matrix(numpy.zeros((3, 1))) |
| 442 | augmented_R[0:2, :] = R |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 443 | RunTest(plant, |
| 444 | end_goal=augmented_R, |
| 445 | controller=controller, |
| 446 | observer=observer, |
| 447 | duration=2.0, |
| 448 | use_profile=True, |
| 449 | max_velocity=max_velocity, |
| 450 | max_acceleration=max_acceleration) |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 451 | |
| 452 | |
milind-u | 53ad98a | 2023-02-20 16:26:09 -0800 | [diff] [blame] | 453 | def WriteAngularSystem(params, |
| 454 | plant_files, |
| 455 | controller_files, |
| 456 | year_namespaces, |
| 457 | plant_type='StateFeedbackPlant', |
| 458 | observer_type='StateFeedbackObserver'): |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 459 | """Writes out the constants for a angular system to a file. |
| 460 | |
| 461 | Args: |
Tyler Chatow | d3afdef | 2019-04-06 22:15:26 -0700 | [diff] [blame] | 462 | params: list of AngularSystemParams or AngularSystemParams, the |
| 463 | parameters defining the system. |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 464 | plant_files: list of strings, the cc and h files for the plant. |
| 465 | controller_files: list of strings, the cc and h files for the integral |
| 466 | controller. |
| 467 | year_namespaces: list of strings, the namespace list to use. |
| 468 | """ |
| 469 | # Write the generated constants out to a file. |
Tyler Chatow | d3afdef | 2019-04-06 22:15:26 -0700 | [diff] [blame] | 470 | angular_systems = [] |
| 471 | integral_angular_systems = [] |
| 472 | |
| 473 | if type(params) is list: |
| 474 | name = params[0].name |
| 475 | for index, param in enumerate(params): |
| 476 | angular_systems.append( |
| 477 | AngularSystem(param, param.name + str(index))) |
| 478 | integral_angular_systems.append( |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 479 | IntegralAngularSystem(param, |
| 480 | 'Integral' + param.name + str(index))) |
Tyler Chatow | d3afdef | 2019-04-06 22:15:26 -0700 | [diff] [blame] | 481 | else: |
| 482 | name = params.name |
| 483 | angular_systems.append(AngularSystem(params, params.name)) |
| 484 | integral_angular_systems.append( |
| 485 | IntegralAngularSystem(params, 'Integral' + params.name)) |
| 486 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 487 | loop_writer = control_loop.ControlLoopWriter(name, |
| 488 | angular_systems, |
milind-u | 53ad98a | 2023-02-20 16:26:09 -0800 | [diff] [blame] | 489 | namespaces=year_namespaces, |
| 490 | plant_type=plant_type, |
| 491 | observer_type=observer_type) |
Lee Mracek | 17cb489 | 2019-02-07 11:24:49 -0500 | [diff] [blame] | 492 | loop_writer.AddConstant( |
Tyler Chatow | d3afdef | 2019-04-06 22:15:26 -0700 | [diff] [blame] | 493 | control_loop.Constant('kOutputRatio', '%f', angular_systems[0].G)) |
Lee Mracek | 17cb489 | 2019-02-07 11:24:49 -0500 | [diff] [blame] | 494 | loop_writer.AddConstant( |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 495 | control_loop.Constant('kFreeSpeed', '%f', |
| 496 | angular_systems[0].motor.free_speed)) |
James Kuszmaul | eeb98e9 | 2024-01-14 22:15:32 -0800 | [diff] [blame] | 497 | loop_writer.Write(plant_files[0], plant_files[1], |
| 498 | None if len(plant_files) < 3 else plant_files[2]) |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 499 | |
Austin Schuh | 2e55403 | 2019-01-21 15:07:27 -0800 | [diff] [blame] | 500 | integral_loop_writer = control_loop.ControlLoopWriter( |
Tyler Chatow | d3afdef | 2019-04-06 22:15:26 -0700 | [diff] [blame] | 501 | 'Integral' + name, |
| 502 | integral_angular_systems, |
milind-u | 53ad98a | 2023-02-20 16:26:09 -0800 | [diff] [blame] | 503 | namespaces=year_namespaces, |
| 504 | plant_type=plant_type, |
| 505 | observer_type=observer_type) |
James Kuszmaul | eeb98e9 | 2024-01-14 22:15:32 -0800 | [diff] [blame] | 506 | integral_loop_writer.Write( |
| 507 | controller_files[0], controller_files[1], |
| 508 | None if len(controller_files) < 3 else controller_files[2]) |