Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 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 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 121 | self.KalmanGain, self.Q_steady = controls.kalman( |
| 122 | A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 123 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 124 | glog.debug('Kal %s', repr(self.KalmanGain)) |
| 125 | self.L = self.A * self.KalmanGain |
| 126 | glog.debug('KalL is %s', repr(self.L)) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 127 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 128 | # The box formed by U_min and U_max must encompass all possible values, |
| 129 | # or else Austin's code gets angry. |
| 130 | self.U_max = numpy.matrix([[12.0]]) |
| 131 | self.U_min = numpy.matrix([[-12.0]]) |
| 132 | |
| 133 | self.InitializeState() |
| 134 | |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 135 | |
| 136 | class IntegralHood(Hood): |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 137 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 138 | def __init__(self, name='IntegralHood'): |
| 139 | super(IntegralHood, self).__init__(name=name) |
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 | self.A_continuous_unaugmented = self.A_continuous |
| 142 | self.B_continuous_unaugmented = self.B_continuous |
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 = numpy.matrix(numpy.zeros((3, 3))) |
| 145 | self.A_continuous[0:2, 0:2] = self.A_continuous_unaugmented |
| 146 | self.A_continuous[0:2, 2] = self.B_continuous_unaugmented |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 147 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 148 | self.B_continuous = numpy.matrix(numpy.zeros((3, 1))) |
| 149 | self.B_continuous[0:2, 0] = 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.C_unaugmented = self.C |
| 152 | self.C = numpy.matrix(numpy.zeros((1, 3))) |
| 153 | self.C[0:1, 0:2] = self.C_unaugmented |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 154 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 155 | self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, |
| 156 | self.B_continuous, self.dt) |
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 | q_pos = 0.01 |
| 159 | q_vel = 4.0 |
| 160 | q_voltage = 4.0 |
| 161 | self.Q = numpy.matrix([[(q_pos**2.0), 0.0, 0.0], |
| 162 | [0.0, (q_vel**2.0), 0.0], |
| 163 | [0.0, 0.0, (q_voltage**2.0)]]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 164 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 165 | r_pos = 0.001 |
| 166 | self.R = numpy.matrix([[(r_pos**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 | self.KalmanGain, self.Q_steady = controls.kalman( |
| 169 | A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R) |
| 170 | self.L = self.A * self.KalmanGain |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 171 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 172 | self.K_unaugmented = self.K |
| 173 | self.K = numpy.matrix(numpy.zeros((1, 3))) |
| 174 | self.K[0, 0:2] = self.K_unaugmented |
| 175 | self.K[0, 2] = 1 |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 176 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 177 | self.Kff = numpy.concatenate( |
| 178 | (self.Kff, numpy.matrix(numpy.zeros((1, 1)))), axis=1) |
| 179 | |
| 180 | self.InitializeState() |
| 181 | |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 182 | |
| 183 | class ScenarioPlotter(object): |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 184 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 185 | def __init__(self): |
| 186 | # Various lists for graphing things. |
| 187 | self.t = [] |
| 188 | self.x = [] |
| 189 | self.v = [] |
| 190 | self.v_hat = [] |
| 191 | self.a = [] |
| 192 | self.x_hat = [] |
| 193 | self.u = [] |
| 194 | self.offset = [] |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 195 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 196 | def run_test(self, |
| 197 | hood, |
| 198 | end_goal, |
| 199 | controller_hood, |
| 200 | observer_hood=None, |
| 201 | iterations=200): |
| 202 | """Runs the hood plant with an initial condition and goal. |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 203 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 204 | Args: |
| 205 | hood: hood object to use. |
| 206 | end_goal: end_goal state. |
| 207 | controller_hood: Hood object to get K from, or None if we should |
| 208 | use hood. |
| 209 | observer_hood: Hood object to use for the observer, or None if we should |
| 210 | use the actual state. |
| 211 | iterations: Number of timesteps to run the model for. |
| 212 | """ |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 213 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 214 | if controller_hood is None: |
| 215 | controller_hood = hood |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 216 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 217 | vbat = 12.0 |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 218 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 219 | if self.t: |
| 220 | initial_t = self.t[-1] + hood.dt |
| 221 | else: |
| 222 | initial_t = 0 |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 223 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 224 | goal = numpy.concatenate((hood.X, numpy.matrix(numpy.zeros((1, 1)))), |
| 225 | axis=0) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 226 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 227 | profile = TrapezoidProfile(hood.dt) |
| 228 | profile.set_maximum_acceleration(10.0) |
| 229 | profile.set_maximum_velocity(1.0) |
| 230 | profile.SetGoal(goal[0, 0]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 231 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 232 | U_last = numpy.matrix(numpy.zeros((1, 1))) |
Austin Schuh | 5ea4847 | 2021-02-02 20:46:41 -0800 | [diff] [blame^] | 233 | for i in range(iterations): |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 234 | observer_hood.Y = hood.Y |
| 235 | observer_hood.CorrectObserver(U_last) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 236 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 237 | self.offset.append(observer_hood.X_hat[2, 0]) |
| 238 | self.x_hat.append(observer_hood.X_hat[0, 0]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 239 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 240 | next_goal = numpy.concatenate( |
| 241 | (profile.Update(end_goal[0, 0], end_goal[1, 0]), |
| 242 | numpy.matrix(numpy.zeros((1, 1)))), |
| 243 | axis=0) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 244 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 245 | ff_U = controller_hood.Kff * (next_goal - observer_hood.A * goal) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 246 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 247 | U_uncapped = controller_hood.K * (goal - observer_hood.X_hat) + ff_U |
| 248 | U = U_uncapped.copy() |
| 249 | U[0, 0] = numpy.clip(U[0, 0], -vbat, vbat) |
| 250 | self.x.append(hood.X[0, 0]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 251 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 252 | if self.v: |
| 253 | last_v = self.v[-1] |
| 254 | else: |
| 255 | last_v = 0 |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 256 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 257 | self.v.append(hood.X[1, 0]) |
| 258 | self.a.append((self.v[-1] - last_v) / hood.dt) |
| 259 | self.v_hat.append(observer_hood.X_hat[1, 0]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 260 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 261 | offset = 0.0 |
| 262 | if i > 100: |
| 263 | offset = 2.0 |
| 264 | hood.Update(U + offset) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 265 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 266 | observer_hood.PredictObserver(U) |
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 | self.t.append(initial_t + i * hood.dt) |
| 269 | self.u.append(U[0, 0]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 270 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 271 | ff_U -= U_uncapped - U |
| 272 | goal = controller_hood.A * goal + controller_hood.B * ff_U |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 273 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 274 | if U[0, 0] != U_uncapped[0, 0]: |
| 275 | profile.MoveCurrentState( |
| 276 | numpy.matrix([[goal[0, 0]], [goal[1, 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 | glog.debug('Time: %f', self.t[-1]) |
| 279 | glog.debug('goal_error %s', repr(end_goal - goal)) |
| 280 | glog.debug('error %s', repr(observer_hood.X_hat - end_goal)) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 281 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 282 | def Plot(self): |
| 283 | pylab.subplot(3, 1, 1) |
| 284 | pylab.plot(self.t, self.x, label='x') |
| 285 | pylab.plot(self.t, self.x_hat, label='x_hat') |
| 286 | pylab.plot(self.t, self.v, label='v') |
| 287 | pylab.plot(self.t, self.v_hat, label='v_hat') |
| 288 | pylab.legend() |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 289 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 290 | pylab.subplot(3, 1, 2) |
| 291 | pylab.plot(self.t, self.u, label='u') |
| 292 | pylab.plot(self.t, self.offset, label='voltage_offset') |
| 293 | pylab.legend() |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 294 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 295 | pylab.subplot(3, 1, 3) |
| 296 | pylab.plot(self.t, self.a, label='a') |
| 297 | pylab.legend() |
| 298 | |
| 299 | pylab.show() |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 300 | |
| 301 | |
| 302 | def main(argv): |
| 303 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 304 | scenario_plotter = ScenarioPlotter() |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 305 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 306 | hood = Hood() |
| 307 | hood_controller = IntegralHood() |
| 308 | observer_hood = IntegralHood() |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 309 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 310 | # Test moving the hood with constant separation. |
| 311 | initial_X = numpy.matrix([[0.0], [0.0]]) |
| 312 | R = numpy.matrix([[numpy.pi / 4.0], [0.0], [0.0]]) |
| 313 | scenario_plotter.run_test( |
| 314 | hood, |
| 315 | end_goal=R, |
| 316 | controller_hood=hood_controller, |
| 317 | observer_hood=observer_hood, |
| 318 | iterations=200) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 319 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 320 | if FLAGS.plot: |
| 321 | scenario_plotter.Plot() |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 322 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 323 | # Write the generated constants out to a file. |
| 324 | if len(argv) != 5: |
| 325 | glog.fatal( |
| 326 | 'Expected .h file name and .cc file name for the hood and integral hood.' |
| 327 | ) |
| 328 | else: |
| 329 | namespaces = ['y2017', 'control_loops', 'superstructure', 'hood'] |
| 330 | hood = Hood('Hood') |
| 331 | loop_writer = control_loop.ControlLoopWriter( |
| 332 | 'Hood', [hood], namespaces=namespaces) |
| 333 | loop_writer.AddConstant( |
| 334 | control_loop.Constant('kFreeSpeed', '%f', hood.free_speed)) |
| 335 | loop_writer.AddConstant( |
| 336 | control_loop.Constant('kOutputRatio', '%f', hood.G)) |
| 337 | loop_writer.Write(argv[1], argv[2]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 338 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 339 | integral_hood = IntegralHood('IntegralHood') |
| 340 | integral_loop_writer = control_loop.ControlLoopWriter( |
| 341 | 'IntegralHood', [integral_hood], namespaces=namespaces) |
| 342 | integral_loop_writer.AddConstant( |
| 343 | control_loop.Constant('kLastReduction', '%f', integral_hood.last_G)) |
| 344 | integral_loop_writer.Write(argv[3], argv[4]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 345 | |
| 346 | |
| 347 | if __name__ == '__main__': |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 348 | argv = FLAGS(sys.argv) |
| 349 | glog.init() |
| 350 | sys.exit(main(argv)) |