Austin Schuh | 085eab9 | 2020-11-26 13:54:51 -0800 | [diff] [blame] | 1 | #!/usr/bin/python3 |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 2 | |
| 3 | from frc971.control_loops.python import control_loop |
| 4 | from frc971.control_loops.python import controls |
| 5 | import numpy |
| 6 | import sys |
| 7 | from matplotlib import pylab |
| 8 | |
| 9 | import gflags |
| 10 | import glog |
| 11 | |
| 12 | FLAGS = gflags.FLAGS |
| 13 | |
Austin Schuh | 82a66dc | 2017-03-04 15:06:44 -0800 | [diff] [blame] | 14 | try: |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 15 | gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.') |
Austin Schuh | 82a66dc | 2017-03-04 15:06:44 -0800 | [diff] [blame] | 16 | except gflags.DuplicateFlagError: |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 17 | pass |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 18 | |
Austin Schuh | cd3237a | 2017-02-18 14:19:26 -0800 | [diff] [blame] | 19 | gflags.DEFINE_bool('stall', False, 'If true, stall the indexer.') |
| 20 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 21 | |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 22 | class VelocityIndexer(control_loop.ControlLoop): |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 23 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 24 | def __init__(self, name='VelocityIndexer'): |
| 25 | super(VelocityIndexer, self).__init__(name) |
| 26 | # Stall Torque in N m |
| 27 | self.stall_torque = 0.71 |
| 28 | # Stall Current in Amps |
| 29 | self.stall_current = 134 |
| 30 | self.free_speed_rpm = 18730.0 |
| 31 | # Free Speed in rotations/second. |
| 32 | self.free_speed = self.free_speed_rpm / 60.0 |
| 33 | # Free Current in Amps |
| 34 | self.free_current = 0.7 |
| 35 | # Moment of inertia of the indexer halves in kg m^2 |
| 36 | # This is measured as Iyy in CAD (the moment of inertia around the Y axis). |
| 37 | # Inner part of indexer -> Iyy = 59500 lb * mm * mm |
| 38 | # Inner spins with 12 / 48 * 18 / 48 * 24 / 36 * 16 / 72 |
| 39 | # Outer part of indexer -> Iyy = 210000 lb * mm * mm |
| 40 | # 1 775 pro -> 12 / 48 * 18 / 48 * 30 / 422 |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 41 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 42 | self.J_inner = 0.0269 |
| 43 | self.J_outer = 0.0952 |
| 44 | # Gear ratios for the inner and outer parts. |
| 45 | self.G_inner = (12.0 / 48.0) * (20.0 / 34.0) * (18.0 / 36.0) * (12.0 / |
| 46 | 84.0) |
| 47 | self.G_outer = (12.0 / 48.0) * (20.0 / 34.0) * (18.0 / 36.0) * (24.0 / |
| 48 | 420.0) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 49 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 50 | # Motor inertia in kg m^2 |
| 51 | self.motor_inertia = 0.00001187 |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 52 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 53 | # The output coordinate system is in radians for the inner part of the |
| 54 | # indexer. |
| 55 | # Compute the effective moment of inertia assuming all the mass is in that |
| 56 | # coordinate system. |
| 57 | self.J = ( |
| 58 | self.J_inner * self.G_inner * self.G_inner + |
| 59 | self.J_outer * self.G_outer * self.G_outer) / (self.G_inner * self.G_inner) + \ |
| 60 | self.motor_inertia * ((1.0 / self.G_inner) ** 2.0) |
| 61 | glog.debug('Indexer J is %f', self.J) |
| 62 | self.G = self.G_inner |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 63 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 64 | # Resistance of the motor, divided by 2 to account for the 2 motors |
| 65 | self.resistance = 12.0 / self.stall_current |
| 66 | # Motor velocity constant |
| 67 | self.Kv = ((self.free_speed * 2.0 * numpy.pi) / |
| 68 | (12.0 - self.resistance * self.free_current)) |
| 69 | # Torque constant |
| 70 | self.Kt = self.stall_torque / self.stall_current |
| 71 | # Control loop time step |
| 72 | self.dt = 0.005 |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 73 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 74 | # State feedback matrices |
| 75 | # [angular velocity] |
| 76 | self.A_continuous = numpy.matrix([[ |
| 77 | -self.Kt / self.Kv / (self.J * self.G * self.G * self.resistance) |
| 78 | ]]) |
| 79 | self.B_continuous = numpy.matrix( |
| 80 | [[self.Kt / (self.J * self.G * self.resistance)]]) |
| 81 | self.C = numpy.matrix([[1]]) |
| 82 | self.D = numpy.matrix([[0]]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 83 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 84 | self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, |
| 85 | self.B_continuous, self.dt) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 86 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 87 | self.PlaceControllerPoles([.75]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 88 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 89 | self.PlaceObserverPoles([0.3]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 90 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 91 | self.U_max = numpy.matrix([[12.0]]) |
| 92 | self.U_min = numpy.matrix([[-12.0]]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 93 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 94 | qff_vel = 8.0 |
| 95 | self.Qff = numpy.matrix([[1.0 / (qff_vel**2.0)]]) |
| 96 | |
| 97 | self.Kff = controls.TwoStateFeedForwards(self.B, self.Qff) |
| 98 | self.InitializeState() |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 99 | |
| 100 | |
| 101 | class Indexer(VelocityIndexer): |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 102 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 103 | def __init__(self, name='Indexer'): |
| 104 | super(Indexer, self).__init__(name) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 105 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 106 | self.A_continuous_unaugmented = self.A_continuous |
| 107 | self.B_continuous_unaugmented = self.B_continuous |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 108 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 109 | self.A_continuous = numpy.matrix(numpy.zeros((2, 2))) |
| 110 | self.A_continuous[1:2, 1:2] = self.A_continuous_unaugmented |
| 111 | self.A_continuous[0, 1] = 1 |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 112 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 113 | self.B_continuous = numpy.matrix(numpy.zeros((2, 1))) |
| 114 | self.B_continuous[1:2, 0] = self.B_continuous_unaugmented |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 115 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 116 | # State feedback matrices |
| 117 | # [position, angular velocity] |
| 118 | self.C = numpy.matrix([[1, 0]]) |
| 119 | self.D = numpy.matrix([[0]]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 120 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 121 | self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, |
| 122 | self.B_continuous, self.dt) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 123 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 124 | self.rpl = .45 |
| 125 | self.ipl = 0.07 |
| 126 | self.PlaceObserverPoles( |
| 127 | [self.rpl + 1j * self.ipl, self.rpl - 1j * self.ipl]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 128 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 129 | self.K_unaugmented = self.K |
| 130 | self.K = numpy.matrix(numpy.zeros((1, 2))) |
| 131 | self.K[0, 1:2] = self.K_unaugmented |
| 132 | self.Kff_unaugmented = self.Kff |
| 133 | self.Kff = numpy.matrix(numpy.zeros((1, 2))) |
| 134 | self.Kff[0, 1:2] = self.Kff_unaugmented |
| 135 | |
| 136 | self.InitializeState() |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 137 | |
| 138 | |
| 139 | class IntegralIndexer(Indexer): |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 140 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 141 | def __init__(self, name="IntegralIndexer", voltage_error_noise=None): |
| 142 | super(IntegralIndexer, self).__init__(name=name) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 143 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 144 | self.A_continuous_unaugmented = self.A_continuous |
| 145 | self.B_continuous_unaugmented = self.B_continuous |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 146 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 147 | self.A_continuous = numpy.matrix(numpy.zeros((3, 3))) |
| 148 | self.A_continuous[0:2, 0:2] = self.A_continuous_unaugmented |
| 149 | self.A_continuous[0:2, 2] = self.B_continuous_unaugmented |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 150 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 151 | self.B_continuous = numpy.matrix(numpy.zeros((3, 1))) |
| 152 | self.B_continuous[0:2, 0] = self.B_continuous_unaugmented |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 153 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 154 | self.C_unaugmented = self.C |
| 155 | self.C = numpy.matrix(numpy.zeros((1, 3))) |
| 156 | self.C[0:1, 0:2] = self.C_unaugmented |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 157 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 158 | self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, |
| 159 | self.B_continuous, self.dt) |
Austin Schuh | cd3237a | 2017-02-18 14:19:26 -0800 | [diff] [blame] | 160 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 161 | q_pos = 0.01 |
| 162 | q_vel = 2.0 |
| 163 | q_voltage = 0.6 |
| 164 | if voltage_error_noise is not None: |
| 165 | q_voltage = voltage_error_noise |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 166 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 167 | self.Q = numpy.matrix([[(q_pos**2.0), 0.0, 0.0], |
| 168 | [0.0, (q_vel**2.0), 0.0], |
| 169 | [0.0, 0.0, (q_voltage**2.0)]]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 170 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 171 | r_pos = 0.001 |
| 172 | self.R = numpy.matrix([[(r_pos**2.0)]]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 173 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 174 | self.KalmanGain, self.Q_steady = controls.kalman(A=self.A, |
| 175 | B=self.B, |
| 176 | C=self.C, |
| 177 | Q=self.Q, |
| 178 | R=self.R) |
| 179 | self.L = self.A * self.KalmanGain |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 180 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 181 | self.K_unaugmented = self.K |
| 182 | self.K = numpy.matrix(numpy.zeros((1, 3))) |
| 183 | self.K[0, 0:2] = self.K_unaugmented |
| 184 | self.K[0, 2] = 1 |
| 185 | self.Kff_unaugmented = self.Kff |
| 186 | self.Kff = numpy.matrix(numpy.zeros((1, 3))) |
| 187 | self.Kff[0, 0:2] = self.Kff_unaugmented |
| 188 | |
| 189 | self.InitializeState() |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 190 | |
| 191 | |
| 192 | class ScenarioPlotter(object): |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 193 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 194 | def __init__(self): |
| 195 | # Various lists for graphing things. |
| 196 | self.t = [] |
| 197 | self.x = [] |
| 198 | self.v = [] |
| 199 | self.a = [] |
| 200 | self.stall_ratio = [] |
| 201 | self.x_hat = [] |
| 202 | self.u = [] |
| 203 | self.offset = [] |
| 204 | |
| 205 | def run_test(self, |
| 206 | indexer, |
| 207 | goal, |
| 208 | iterations=200, |
| 209 | controller_indexer=None, |
| 210 | observer_indexer=None): |
| 211 | """Runs the indexer plant with an initial condition and goal. |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 212 | |
| 213 | Args: |
| 214 | indexer: Indexer object to use. |
| 215 | goal: goal state. |
| 216 | iterations: Number of timesteps to run the model for. |
| 217 | controller_indexer: Indexer object to get K from, or None if we should |
| 218 | use indexer. |
| 219 | observer_indexer: Indexer object to use for the observer, or None if we |
| 220 | should use the actual state. |
| 221 | """ |
| 222 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 223 | if controller_indexer is None: |
| 224 | controller_indexer = indexer |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 225 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 226 | vbat = 12.0 |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 227 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 228 | if self.t: |
| 229 | initial_t = self.t[-1] + indexer.dt |
Austin Schuh | cd3237a | 2017-02-18 14:19:26 -0800 | [diff] [blame] | 230 | else: |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 231 | initial_t = 0 |
Austin Schuh | cd3237a | 2017-02-18 14:19:26 -0800 | [diff] [blame] | 232 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 233 | for i in range(iterations): |
| 234 | X_hat = indexer.X |
Austin Schuh | cd3237a | 2017-02-18 14:19:26 -0800 | [diff] [blame] | 235 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 236 | if observer_indexer is not None: |
| 237 | X_hat = observer_indexer.X_hat |
| 238 | observer_indexer.Y = indexer.Y |
| 239 | observer_indexer.CorrectObserver(numpy.matrix([[0.0]])) |
| 240 | self.x_hat.append(observer_indexer.X_hat[1, 0]) |
| 241 | self.offset.append(observer_indexer.X_hat[2, 0]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 242 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 243 | ff_U = controller_indexer.Kff * (goal - observer_indexer.A * goal) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 244 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 245 | U = controller_indexer.K * (goal - X_hat) + ff_U |
| 246 | U[0, 0] = numpy.clip(U[0, 0], -vbat, vbat) |
| 247 | self.x.append(indexer.X[0, 0]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 248 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 249 | if self.v: |
| 250 | last_v = self.v[-1] |
| 251 | else: |
| 252 | last_v = 0 |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 253 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 254 | self.v.append(indexer.X[1, 0]) |
| 255 | self.a.append((self.v[-1] - last_v) / indexer.dt) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 256 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 257 | applied_U = U.copy() |
| 258 | if i >= 40: |
| 259 | applied_U -= 2 |
| 260 | |
| 261 | if FLAGS.stall and i >= 40: |
| 262 | indexer.X[1, 0] = 0.0 |
| 263 | else: |
| 264 | indexer.Update(applied_U) |
| 265 | |
| 266 | if observer_indexer is not None: |
| 267 | clipped_u = U[0, 0] |
| 268 | clip_u_value = 3.0 |
| 269 | if clipped_u < 0: |
| 270 | clipped_u = min(clipped_u, -clip_u_value) |
| 271 | else: |
| 272 | clipped_u = max(clipped_u, clip_u_value) |
| 273 | |
| 274 | self.stall_ratio.append(10 * (-self.offset[-1] / clipped_u)) |
| 275 | |
| 276 | observer_indexer.PredictObserver(U) |
| 277 | |
| 278 | self.t.append(initial_t + i * indexer.dt) |
| 279 | self.u.append(U[0, 0]) |
| 280 | |
| 281 | def Plot(self): |
| 282 | pylab.subplot(3, 1, 1) |
| 283 | pylab.plot(self.t, self.v, label='x') |
| 284 | pylab.plot(self.t, self.x_hat, label='x_hat') |
| 285 | pylab.legend() |
| 286 | |
| 287 | pylab.subplot(3, 1, 2) |
| 288 | pylab.plot(self.t, self.u, label='u') |
| 289 | pylab.plot(self.t, self.offset, label='voltage_offset') |
| 290 | pylab.plot(self.t, self.stall_ratio, label='stall_ratio') |
| 291 | pylab.plot(self.t, |
| 292 | [10.0 if x > 6.0 else 0.0 for x in self.stall_ratio], |
| 293 | label='is_stalled') |
| 294 | pylab.legend() |
| 295 | |
| 296 | pylab.subplot(3, 1, 3) |
| 297 | pylab.plot(self.t, self.a, label='a') |
| 298 | pylab.legend() |
| 299 | |
| 300 | pylab.show() |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 301 | |
| 302 | |
| 303 | def main(argv): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 304 | scenario_plotter = ScenarioPlotter() |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 305 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 306 | indexer = Indexer() |
| 307 | indexer_controller = IntegralIndexer() |
| 308 | observer_indexer = IntegralIndexer() |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 309 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 310 | initial_X = numpy.matrix([[0.0], [0.0]]) |
| 311 | R = numpy.matrix([[0.0], [20.0], [0.0]]) |
| 312 | scenario_plotter.run_test(indexer, |
| 313 | goal=R, |
| 314 | controller_indexer=indexer_controller, |
| 315 | observer_indexer=observer_indexer, |
| 316 | iterations=200) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 317 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 318 | if FLAGS.plot: |
| 319 | scenario_plotter.Plot() |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 320 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 321 | scenario_plotter = ScenarioPlotter() |
Austin Schuh | cd3237a | 2017-02-18 14:19:26 -0800 | [diff] [blame] | 322 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 323 | indexer = Indexer() |
| 324 | indexer_controller = IntegralIndexer(voltage_error_noise=1.5) |
| 325 | observer_indexer = IntegralIndexer(voltage_error_noise=1.5) |
Austin Schuh | cd3237a | 2017-02-18 14:19:26 -0800 | [diff] [blame] | 326 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 327 | initial_X = numpy.matrix([[0.0], [0.0]]) |
| 328 | R = numpy.matrix([[0.0], [20.0], [0.0]]) |
| 329 | scenario_plotter.run_test(indexer, |
| 330 | goal=R, |
| 331 | controller_indexer=indexer_controller, |
| 332 | observer_indexer=observer_indexer, |
| 333 | iterations=200) |
Austin Schuh | cd3237a | 2017-02-18 14:19:26 -0800 | [diff] [blame] | 334 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 335 | if FLAGS.plot: |
| 336 | scenario_plotter.Plot() |
Austin Schuh | cd3237a | 2017-02-18 14:19:26 -0800 | [diff] [blame] | 337 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 338 | if len(argv) != 7: |
| 339 | glog.fatal('Expected .h file name and .cc file names') |
| 340 | else: |
| 341 | namespaces = ['y2017', 'control_loops', 'superstructure', 'indexer'] |
| 342 | indexer = Indexer('Indexer') |
| 343 | loop_writer = control_loop.ControlLoopWriter('Indexer', [indexer], |
| 344 | namespaces=namespaces) |
| 345 | loop_writer.AddConstant( |
| 346 | control_loop.Constant('kFreeSpeed', '%f', indexer.free_speed)) |
| 347 | loop_writer.AddConstant( |
| 348 | control_loop.Constant('kOutputRatio', '%f', indexer.G)) |
| 349 | loop_writer.Write(argv[1], argv[2]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 350 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 351 | integral_indexer = IntegralIndexer('IntegralIndexer') |
| 352 | integral_loop_writer = control_loop.ControlLoopWriter( |
| 353 | 'IntegralIndexer', [integral_indexer], namespaces=namespaces) |
| 354 | integral_loop_writer.Write(argv[3], argv[4]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 355 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 356 | stuck_integral_indexer = IntegralIndexer('StuckIntegralIndexer', |
| 357 | voltage_error_noise=1.5) |
| 358 | stuck_integral_loop_writer = control_loop.ControlLoopWriter( |
| 359 | 'StuckIntegralIndexer', [stuck_integral_indexer], |
| 360 | namespaces=namespaces) |
| 361 | stuck_integral_loop_writer.Write(argv[5], argv[6]) |
Austin Schuh | cd3237a | 2017-02-18 14:19:26 -0800 | [diff] [blame] | 362 | |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 363 | |
| 364 | if __name__ == '__main__': |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 365 | argv = FLAGS(sys.argv) |
| 366 | glog.init() |
| 367 | sys.exit(main(argv)) |