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 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 3 | from aos.util.trapezoid_profile import TrapezoidProfile |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 4 | from frc971.control_loops.python import control_loop |
| 5 | from frc971.control_loops.python import controls |
| 6 | import numpy |
| 7 | import sys |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 8 | from matplotlib import pylab |
| 9 | import gflags |
| 10 | import glog |
| 11 | |
| 12 | FLAGS = gflags.FLAGS |
| 13 | |
| 14 | try: |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 15 | gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.') |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 16 | except gflags.DuplicateFlagError: |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 17 | pass |
| 18 | |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 19 | |
| 20 | class Hood(control_loop.ControlLoop): |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 21 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 22 | def __init__(self, name='Hood'): |
| 23 | super(Hood, self).__init__(name) |
| 24 | # Stall Torque in N m |
| 25 | self.stall_torque = 0.43 |
| 26 | # Stall Current in Amps |
| 27 | self.stall_current = 53.0 |
| 28 | self.free_speed_rpm = 13180.0 |
| 29 | # Free Speed in rotations/second. |
| 30 | self.free_speed = self.free_speed_rpm / 60 |
| 31 | # Free Current in Amps |
| 32 | self.free_current = 1.8 |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 33 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 34 | # Resistance of the motor |
| 35 | self.R = 12.0 / self.stall_current |
| 36 | # Motor velocity constant |
| 37 | self.Kv = ((self.free_speed * 2.0 * numpy.pi) / |
| 38 | (12.0 - self.R * self.free_current)) |
| 39 | # Torque constant |
| 40 | self.Kt = self.stall_torque / self.stall_current |
| 41 | # First axle gear ratio off the motor |
| 42 | self.G1 = (12.0 / 60.0) |
| 43 | # Second axle gear ratio off the motor |
| 44 | self.G2 = self.G1 * (14.0 / 36.0) |
| 45 | # Third axle gear ratio off the motor |
| 46 | self.G3 = self.G2 * (14.0 / 36.0) |
| 47 | # The last gear reduction (encoder -> hood angle) |
| 48 | self.last_G = (20.0 / 345.0) |
| 49 | # Gear ratio |
| 50 | self.G = (12.0 / 60.0) * (14.0 / 36.0) * (14.0 / 36.0) * self.last_G |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 51 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 52 | # 36 tooth gear inertia in kg * m^2 |
| 53 | self.big_gear_inertia = 0.5 * 0.039 * ((36.0 / 40.0 * 0.025)**2) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 54 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 55 | # Motor inertia in kg * m^2 |
| 56 | self.motor_inertia = 0.000006 |
| 57 | glog.debug(self.big_gear_inertia) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 58 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 59 | # Moment of inertia, measured in CAD. |
| 60 | # Extra mass to compensate for friction is added on. |
| 61 | self.J = 0.08 + 2.3 + \ |
| 62 | self.big_gear_inertia * ((self.G1 / self.G) ** 2) + \ |
| 63 | self.big_gear_inertia * ((self.G2 / self.G) ** 2) + \ |
| 64 | self.motor_inertia * ((1.0 / self.G) ** 2.0) |
| 65 | glog.debug('J effective %f', self.J) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 66 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 67 | # Control loop time step |
| 68 | self.dt = 0.005 |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 69 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 70 | # State is [position, velocity] |
| 71 | # Input is [Voltage] |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 72 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 73 | C1 = self.Kt / (self.R * self.J * self.Kv * self.G * self.G) |
| 74 | C2 = self.Kt / (self.J * self.R * self.G) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 75 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 76 | self.A_continuous = numpy.matrix([[0, 1], [0, -C1]]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 77 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 78 | # Start with the unmodified input |
| 79 | self.B_continuous = numpy.matrix([[0], [C2]]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 80 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 81 | self.C = numpy.matrix([[1, 0]]) |
| 82 | self.D = numpy.matrix([[0]]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 83 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [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 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 87 | controllability = controls.ctrb(self.A, self.B) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 88 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 89 | glog.debug('Free speed is %f', |
| 90 | -self.B_continuous[1, 0] / self.A_continuous[1, 1] * 12.0) |
| 91 | glog.debug(repr(self.A_continuous)) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 92 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 93 | # Calculate the LQR controller gain |
| 94 | q_pos = 0.015 |
| 95 | q_vel = 0.40 |
| 96 | self.Q = numpy.matrix([[(1.0 / (q_pos**2.0)), 0.0], |
| 97 | [0.0, (1.0 / (q_vel**2.0))]]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 98 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 99 | self.R = numpy.matrix([[(5.0 / (12.0**2.0))]]) |
| 100 | self.K = controls.dlqr(self.A, self.B, self.Q, self.R) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 101 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 102 | # Calculate the feed forwards gain. |
| 103 | q_pos_ff = 0.005 |
| 104 | q_vel_ff = 1.0 |
| 105 | self.Qff = numpy.matrix([[(1.0 / (q_pos_ff**2.0)), 0.0], |
| 106 | [0.0, (1.0 / (q_vel_ff**2.0))]]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 107 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 108 | self.Kff = controls.TwoStateFeedForwards(self.B, self.Qff) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 109 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 110 | glog.debug('K %s', repr(self.K)) |
| 111 | glog.debug('Poles are %s', |
| 112 | repr(numpy.linalg.eig(self.A - self.B * self.K)[0])) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 113 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 114 | q_pos = 0.10 |
| 115 | q_vel = 1.65 |
| 116 | self.Q = numpy.matrix([[(q_pos**2.0), 0.0], [0.0, (q_vel**2.0)]]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 117 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 118 | r_volts = 0.025 |
| 119 | self.R = numpy.matrix([[(r_volts**2.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.KalmanGain, self.Q_steady = controls.kalman(A=self.A, |
| 122 | B=self.B, |
| 123 | C=self.C, |
| 124 | Q=self.Q, |
| 125 | R=self.R) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 126 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 127 | glog.debug('Kal %s', repr(self.KalmanGain)) |
| 128 | self.L = self.A * self.KalmanGain |
| 129 | glog.debug('KalL is %s', repr(self.L)) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 130 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 131 | # The box formed by U_min and U_max must encompass all possible values, |
| 132 | # or else Austin's code gets angry. |
| 133 | self.U_max = numpy.matrix([[12.0]]) |
| 134 | self.U_min = numpy.matrix([[-12.0]]) |
| 135 | |
| 136 | self.InitializeState() |
| 137 | |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 138 | |
| 139 | class IntegralHood(Hood): |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 140 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 141 | def __init__(self, name='IntegralHood'): |
| 142 | super(IntegralHood, self).__init__(name=name) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 143 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [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 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [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 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [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 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [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 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 158 | self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, |
| 159 | self.B_continuous, self.dt) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 160 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 161 | q_pos = 0.01 |
| 162 | q_vel = 4.0 |
| 163 | q_voltage = 4.0 |
| 164 | self.Q = numpy.matrix([[(q_pos**2.0), 0.0, 0.0], |
| 165 | [0.0, (q_vel**2.0), 0.0], |
| 166 | [0.0, 0.0, (q_voltage**2.0)]]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 167 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 168 | r_pos = 0.001 |
| 169 | self.R = numpy.matrix([[(r_pos**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 | self.KalmanGain, self.Q_steady = controls.kalman(A=self.A, |
| 172 | B=self.B, |
| 173 | C=self.C, |
| 174 | Q=self.Q, |
| 175 | R=self.R) |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 176 | self.L = self.A * self.KalmanGain |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 177 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 178 | self.K_unaugmented = self.K |
| 179 | self.K = numpy.matrix(numpy.zeros((1, 3))) |
| 180 | self.K[0, 0:2] = self.K_unaugmented |
| 181 | self.K[0, 2] = 1 |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 182 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 183 | self.Kff = numpy.concatenate( |
| 184 | (self.Kff, numpy.matrix(numpy.zeros((1, 1)))), axis=1) |
| 185 | |
| 186 | self.InitializeState() |
| 187 | |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 188 | |
| 189 | class ScenarioPlotter(object): |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 190 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 191 | def __init__(self): |
| 192 | # Various lists for graphing things. |
| 193 | self.t = [] |
| 194 | self.x = [] |
| 195 | self.v = [] |
| 196 | self.v_hat = [] |
| 197 | self.a = [] |
| 198 | self.x_hat = [] |
| 199 | self.u = [] |
| 200 | self.offset = [] |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 201 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 202 | def run_test(self, |
| 203 | hood, |
| 204 | end_goal, |
| 205 | controller_hood, |
| 206 | observer_hood=None, |
| 207 | iterations=200): |
| 208 | """Runs the hood plant with an initial condition and goal. |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 209 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 210 | Args: |
| 211 | hood: hood object to use. |
| 212 | end_goal: end_goal state. |
| 213 | controller_hood: Hood object to get K from, or None if we should |
| 214 | use hood. |
| 215 | observer_hood: Hood object to use for the observer, or None if we should |
| 216 | use the actual state. |
| 217 | iterations: Number of timesteps to run the model for. |
| 218 | """ |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 219 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 220 | if controller_hood is None: |
| 221 | controller_hood = hood |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 222 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 223 | vbat = 12.0 |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 224 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 225 | if self.t: |
| 226 | initial_t = self.t[-1] + hood.dt |
| 227 | else: |
| 228 | initial_t = 0 |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 229 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 230 | goal = numpy.concatenate((hood.X, numpy.matrix(numpy.zeros((1, 1)))), |
| 231 | axis=0) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 232 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 233 | profile = TrapezoidProfile(hood.dt) |
| 234 | profile.set_maximum_acceleration(10.0) |
| 235 | profile.set_maximum_velocity(1.0) |
| 236 | profile.SetGoal(goal[0, 0]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 237 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 238 | U_last = numpy.matrix(numpy.zeros((1, 1))) |
Austin Schuh | 5ea4847 | 2021-02-02 20:46:41 -0800 | [diff] [blame] | 239 | for i in range(iterations): |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 240 | observer_hood.Y = hood.Y |
| 241 | observer_hood.CorrectObserver(U_last) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 242 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 243 | self.offset.append(observer_hood.X_hat[2, 0]) |
| 244 | self.x_hat.append(observer_hood.X_hat[0, 0]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 245 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 246 | next_goal = numpy.concatenate( |
| 247 | (profile.Update(end_goal[0, 0], end_goal[1, 0]), |
| 248 | numpy.matrix(numpy.zeros((1, 1)))), |
| 249 | axis=0) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 250 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 251 | ff_U = controller_hood.Kff * (next_goal - observer_hood.A * goal) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 252 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 253 | U_uncapped = controller_hood.K * (goal - |
| 254 | observer_hood.X_hat) + ff_U |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 255 | U = U_uncapped.copy() |
| 256 | U[0, 0] = numpy.clip(U[0, 0], -vbat, vbat) |
| 257 | self.x.append(hood.X[0, 0]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 258 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 259 | if self.v: |
| 260 | last_v = self.v[-1] |
| 261 | else: |
| 262 | last_v = 0 |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 263 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 264 | self.v.append(hood.X[1, 0]) |
| 265 | self.a.append((self.v[-1] - last_v) / hood.dt) |
| 266 | self.v_hat.append(observer_hood.X_hat[1, 0]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 267 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 268 | offset = 0.0 |
| 269 | if i > 100: |
| 270 | offset = 2.0 |
| 271 | hood.Update(U + offset) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 272 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 273 | observer_hood.PredictObserver(U) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 274 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 275 | self.t.append(initial_t + i * hood.dt) |
| 276 | self.u.append(U[0, 0]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 277 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 278 | ff_U -= U_uncapped - U |
| 279 | goal = controller_hood.A * goal + controller_hood.B * ff_U |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 280 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 281 | if U[0, 0] != U_uncapped[0, 0]: |
| 282 | profile.MoveCurrentState( |
| 283 | numpy.matrix([[goal[0, 0]], [goal[1, 0]]])) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 284 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 285 | glog.debug('Time: %f', self.t[-1]) |
| 286 | glog.debug('goal_error %s', repr(end_goal - goal)) |
| 287 | glog.debug('error %s', repr(observer_hood.X_hat - end_goal)) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 288 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 289 | def Plot(self): |
| 290 | pylab.subplot(3, 1, 1) |
| 291 | pylab.plot(self.t, self.x, label='x') |
| 292 | pylab.plot(self.t, self.x_hat, label='x_hat') |
| 293 | pylab.plot(self.t, self.v, label='v') |
| 294 | pylab.plot(self.t, self.v_hat, label='v_hat') |
| 295 | pylab.legend() |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 296 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 297 | pylab.subplot(3, 1, 2) |
| 298 | pylab.plot(self.t, self.u, label='u') |
| 299 | pylab.plot(self.t, self.offset, label='voltage_offset') |
| 300 | pylab.legend() |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 301 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 302 | pylab.subplot(3, 1, 3) |
| 303 | pylab.plot(self.t, self.a, label='a') |
| 304 | pylab.legend() |
| 305 | |
| 306 | pylab.show() |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 307 | |
| 308 | |
| 309 | def main(argv): |
| 310 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 311 | scenario_plotter = ScenarioPlotter() |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 312 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 313 | hood = Hood() |
| 314 | hood_controller = IntegralHood() |
| 315 | observer_hood = IntegralHood() |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 316 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 317 | # Test moving the hood with constant separation. |
| 318 | initial_X = numpy.matrix([[0.0], [0.0]]) |
| 319 | R = numpy.matrix([[numpy.pi / 4.0], [0.0], [0.0]]) |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 320 | scenario_plotter.run_test(hood, |
| 321 | end_goal=R, |
| 322 | controller_hood=hood_controller, |
| 323 | observer_hood=observer_hood, |
| 324 | iterations=200) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 325 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 326 | if FLAGS.plot: |
| 327 | scenario_plotter.Plot() |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 328 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 329 | # Write the generated constants out to a file. |
| 330 | if len(argv) != 5: |
| 331 | glog.fatal( |
| 332 | 'Expected .h file name and .cc file name for the hood and integral hood.' |
| 333 | ) |
| 334 | else: |
| 335 | namespaces = ['y2017', 'control_loops', 'superstructure', 'hood'] |
| 336 | hood = Hood('Hood') |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 337 | loop_writer = control_loop.ControlLoopWriter('Hood', [hood], |
| 338 | namespaces=namespaces) |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 339 | loop_writer.AddConstant( |
| 340 | control_loop.Constant('kFreeSpeed', '%f', hood.free_speed)) |
| 341 | loop_writer.AddConstant( |
| 342 | control_loop.Constant('kOutputRatio', '%f', hood.G)) |
| 343 | loop_writer.Write(argv[1], argv[2]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 344 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 345 | integral_hood = IntegralHood('IntegralHood') |
| 346 | integral_loop_writer = control_loop.ControlLoopWriter( |
| 347 | 'IntegralHood', [integral_hood], namespaces=namespaces) |
| 348 | integral_loop_writer.AddConstant( |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 349 | control_loop.Constant('kLastReduction', '%f', |
| 350 | integral_hood.last_G)) |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 351 | integral_loop_writer.Write(argv[3], argv[4]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 352 | |
| 353 | |
| 354 | if __name__ == '__main__': |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 355 | argv = FLAGS(sys.argv) |
| 356 | glog.init() |
| 357 | sys.exit(main(argv)) |