Austin Schuh | 085eab9 | 2020-11-26 13:54:51 -0800 | [diff] [blame] | 1 | #!/usr/bin/python3 |
Michael Schuh | 10dd1e0 | 2018-01-20 13:19:44 -0800 | [diff] [blame] | 2 | |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 3 | from frc971.control_loops.python import control_loop |
| 4 | from frc971.control_loops.python import controls |
Michael Schuh | 10dd1e0 | 2018-01-20 13:19:44 -0800 | [diff] [blame] | 5 | import numpy |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 6 | import sys |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 7 | from matplotlib import pylab |
| 8 | import gflags |
| 9 | import glog |
Michael Schuh | 10dd1e0 | 2018-01-20 13:19:44 -0800 | [diff] [blame] | 10 | |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 11 | FLAGS = gflags.FLAGS |
Michael Schuh | 10dd1e0 | 2018-01-20 13:19:44 -0800 | [diff] [blame] | 12 | |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 13 | try: |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 14 | gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.') |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 15 | except gflags.DuplicateFlagError: |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 16 | pass |
| 17 | |
Michael Schuh | 10dd1e0 | 2018-01-20 13:19:44 -0800 | [diff] [blame] | 18 | |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 19 | class Intake(control_loop.ControlLoop): |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 20 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 21 | def __init__(self, name="Intake"): |
| 22 | super(Intake, self).__init__(name) |
| 23 | self.motor = control_loop.BAG() |
| 24 | # Stall Torque in N m |
| 25 | self.stall_torque = self.motor.stall_torque |
| 26 | # Stall Current in Amps |
| 27 | self.stall_current = self.motor.stall_current |
| 28 | # Free Speed in RPM |
| 29 | self.free_speed = self.motor.free_speed |
| 30 | # Free Current in Amps |
| 31 | self.free_current = self.motor.free_current |
Michael Schuh | 10dd1e0 | 2018-01-20 13:19:44 -0800 | [diff] [blame] | 32 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 33 | # Resistance of the motor |
| 34 | self.resistance = self.motor.resistance |
| 35 | # Motor velocity constant |
| 36 | self.Kv = self.motor.Kv |
| 37 | # Torque constant |
| 38 | self.Kt = self.motor.Kt |
| 39 | # Gear ratio |
| 40 | self.G = 1.0 / 102.6 |
Michael Schuh | 10dd1e0 | 2018-01-20 13:19:44 -0800 | [diff] [blame] | 41 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 42 | self.motor_inertia = 0.00000589 * 1.2 |
Michael Schuh | 10dd1e0 | 2018-01-20 13:19:44 -0800 | [diff] [blame] | 43 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 44 | # Series elastic moment of inertia |
| 45 | self.Je = self.motor_inertia / (self.G * self.G) |
| 46 | # Grabber moment of inertia |
| 47 | self.Jo = 0.0363 |
Michael Schuh | 10dd1e0 | 2018-01-20 13:19:44 -0800 | [diff] [blame] | 48 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 49 | # Bot has a time constant of 0.22 |
| 50 | # Current physics has a time constant of 0.18 |
Michael Schuh | 10dd1e0 | 2018-01-20 13:19:44 -0800 | [diff] [blame] | 51 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 52 | # Spring constant (N m / radian) |
| 53 | self.Ks = 32.74 |
Michael Schuh | 10dd1e0 | 2018-01-20 13:19:44 -0800 | [diff] [blame] | 54 | |
Michael Schuh | d77b08c | 2018-02-27 22:15:07 -0800 | [diff] [blame] | 55 | # Damper constant (N m s/ radian) |
| 56 | # 0.01 is small and 1 is big |
| 57 | self.b = 0.1 |
| 58 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 59 | # Control loop time step |
| 60 | self.dt = 0.00505 |
Michael Schuh | 10dd1e0 | 2018-01-20 13:19:44 -0800 | [diff] [blame] | 61 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 62 | # State is [output_position, output_velocity, |
| 63 | # elastic_position, elastic_velocity] |
| 64 | # The output position is the absolute position of the intake arm. |
| 65 | # The elastic position is the absolute position of the motor side of the |
| 66 | # series elastic. |
| 67 | # Input is [voltage] |
Michael Schuh | 10dd1e0 | 2018-01-20 13:19:44 -0800 | [diff] [blame] | 68 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 69 | self.A_continuous = numpy.matrix( |
| 70 | [[0.0, 1.0, 0.0, 0.0], |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 71 | [(-self.Ks / self.Jo), (-self.b / self.Jo), (self.Ks / self.Jo), |
| 72 | (self.b / self.Jo)], [0.0, 0.0, 0.0, 1.0], |
| 73 | [(self.Ks / self.Je), (self.b / self.Je), (-self.Ks / self.Je), |
| 74 | (-self.b / self.Je) - self.Kt / |
| 75 | (self.Je * self.resistance * self.Kv * self.G * self.G)]]) |
Michael Schuh | 10dd1e0 | 2018-01-20 13:19:44 -0800 | [diff] [blame] | 76 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 77 | # Start with the unmodified input |
| 78 | self.B_continuous = numpy.matrix( |
| 79 | [[0.0], [0.0], [0.0], |
| 80 | [self.Kt / (self.G * self.Je * self.resistance)]]) |
Michael Schuh | 10dd1e0 | 2018-01-20 13:19:44 -0800 | [diff] [blame] | 81 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 82 | self.C = numpy.matrix([[1.0, 0.0, -1.0, 0.0], [0.0, 0.0, 1.0, 0.0]]) |
| 83 | self.D = numpy.matrix([[0.0], [0.0]]) |
Michael Schuh | 10dd1e0 | 2018-01-20 13:19:44 -0800 | [diff] [blame] | 84 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 85 | self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, |
| 86 | self.B_continuous, self.dt) |
Michael Schuh | 10dd1e0 | 2018-01-20 13:19:44 -0800 | [diff] [blame] | 87 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 88 | #controllability = controls.ctrb(self.A, self.B) |
| 89 | #glog.debug('ctrb: ' + repr(numpy.linalg.matrix_rank(controllability))) |
Michael Schuh | 10dd1e0 | 2018-01-20 13:19:44 -0800 | [diff] [blame] | 90 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 91 | #observability = controls.ctrb(self.A.T, self.C.T) |
| 92 | #glog.debug('obs: ' + repr(numpy.linalg.matrix_rank(observability))) |
Michael Schuh | 10dd1e0 | 2018-01-20 13:19:44 -0800 | [diff] [blame] | 93 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 94 | glog.debug('A_continuous ' + repr(self.A_continuous)) |
| 95 | glog.debug('B_continuous ' + repr(self.B_continuous)) |
Michael Schuh | 10dd1e0 | 2018-01-20 13:19:44 -0800 | [diff] [blame] | 96 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 97 | self.K = numpy.matrix(numpy.zeros((1, 4))) |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 98 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 99 | q_pos = 0.05 |
| 100 | q_vel = 2.65 |
| 101 | self.Q = numpy.matrix( |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 102 | numpy.diag([(q_pos**2.0), (q_vel**2.0), (q_pos**2.0), |
| 103 | (q_vel**2.0)])) |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 104 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 105 | r_nm = 0.025 |
| 106 | self.R = numpy.matrix(numpy.diag([(r_nm**2.0), (r_nm**2.0)])) |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 107 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 108 | self.KalmanGain, self.Q_steady = controls.kalman(A=self.A, |
| 109 | B=self.B, |
| 110 | C=self.C, |
| 111 | Q=self.Q, |
| 112 | R=self.R) |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 113 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 114 | # The box formed by U_min and U_max must encompass all possible values, |
| 115 | # or else Austin's code gets angry. |
| 116 | self.U_max = numpy.matrix([[12.0]]) |
| 117 | self.U_min = numpy.matrix([[-12.0]]) |
| 118 | |
| 119 | self.InitializeState() |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 120 | |
| 121 | |
| 122 | class DelayedIntake(Intake): |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 123 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 124 | def __init__(self, name="DelayedIntake"): |
| 125 | super(DelayedIntake, self).__init__(name=name) |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 126 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 127 | self.A_undelayed = self.A |
| 128 | self.B_undelayed = self.B |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 129 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 130 | self.C_unaugmented = self.C |
| 131 | self.C = numpy.matrix(numpy.zeros((2, 5))) |
| 132 | self.C[0:2, 0:4] = self.C_unaugmented |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 133 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 134 | # Model this as X[4] is the last power. And then B applies to the last |
| 135 | # power. This lets us model the 1 cycle PWM delay accurately. |
| 136 | self.A = numpy.matrix(numpy.zeros((5, 5))) |
| 137 | self.A[0:4, 0:4] = self.A_undelayed |
| 138 | self.A[0:4, 4] = self.B_undelayed |
| 139 | self.B = numpy.matrix(numpy.zeros((5, 1))) |
| 140 | self.B[4, 0] = 1.0 |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 141 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 142 | # Coordinate transform fom absolute angles to relative angles. |
| 143 | # [output_position, output_velocity, spring_angle, spring_velocity, voltage] |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 144 | abs_to_rel = numpy.matrix([[1.0, 0.0, 0.0, 0.0, 0.0], |
| 145 | [0.0, 1.0, 0.0, 0.0, 0.0], |
| 146 | [1.0, 0.0, -1.0, 0.0, 0.0], |
| 147 | [0.0, 1.0, 0.0, -1.0, 0.0], |
| 148 | [0.0, 0.0, 0.0, 0.0, 1.0]]) |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 149 | # and back again. |
| 150 | rel_to_abs = numpy.matrix(numpy.linalg.inv(abs_to_rel)) |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 151 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 152 | # Now, get A and B in the relative coordinate system. |
| 153 | self.A_transformed_full = numpy.matrix(numpy.zeros((5, 5))) |
| 154 | self.B_transformed_full = numpy.matrix(numpy.zeros((5, 1))) |
| 155 | (self.A_transformed_full[0:4, 0:4], |
| 156 | self.A_transformed_full[0:4, 4]) = self.ContinuousToDiscrete( |
| 157 | abs_to_rel[0:4, 0:4] * self.A_continuous * rel_to_abs[0:4, 0:4], |
| 158 | abs_to_rel[0:4, 0:4] * self.B_continuous, self.dt) |
| 159 | self.B_transformed_full[4, 0] = 1.0 |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 160 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 161 | # Pull out the components of the dynamics which don't include the spring |
| 162 | # output position so we can do partial state feedback on what we care about. |
| 163 | self.A_transformed = self.A_transformed_full[1:5, 1:5] |
| 164 | self.B_transformed = self.B_transformed_full[1:5, 0] |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 165 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 166 | glog.debug('A_transformed_full ' + str(self.A_transformed_full)) |
| 167 | glog.debug('B_transformed_full ' + str(self.B_transformed_full)) |
| 168 | glog.debug('A_transformed ' + str(self.A_transformed)) |
| 169 | glog.debug('B_transformed ' + str(self.B_transformed)) |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 170 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 171 | # Now, let's design a controller in |
| 172 | # [output_velocity, spring_position, spring_velocity, delayed_voltage] |
| 173 | # space. |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 174 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 175 | q_output_vel = 1.0 |
Austin Schuh | ede4732 | 2018-07-08 16:04:36 -0700 | [diff] [blame] | 176 | q_spring_pos = 0.10 |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 177 | q_spring_vel = 2.0 |
| 178 | q_voltage = 1000000000000.0 |
| 179 | self.Q_lqr = numpy.matrix( |
| 180 | numpy.diag([ |
| 181 | 1.0 / (q_output_vel**2.0), 1.0 / (q_spring_pos**2.0), |
| 182 | 1.0 / (q_spring_vel**2.0), 1.0 / (q_voltage**2.0) |
| 183 | ])) |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 184 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 185 | self.R = numpy.matrix([[(1.0 / (12.0**2.0))]]) |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 186 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 187 | self.K_transformed = controls.dlqr(self.A_transformed, |
| 188 | self.B_transformed, self.Q_lqr, |
| 189 | self.R) |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 190 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 191 | # Write the controller back out in the absolute coordinate system. |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 192 | self.K = numpy.hstack( |
| 193 | (numpy.matrix([[0.0]]), self.K_transformed)) * abs_to_rel |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 194 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 195 | controllability = controls.ctrb(self.A_transformed, self.B_transformed) |
| 196 | glog.debug('ctrb: ' + repr(numpy.linalg.matrix_rank(controllability))) |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 197 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 198 | w, v = numpy.linalg.eig(self.A_transformed - |
| 199 | self.B_transformed * self.K_transformed) |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 200 | glog.debug('Poles are %s, for %s', repr(w), self._name) |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 201 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 202 | for i in range(len(w)): |
| 203 | glog.debug(' Pole %s -> %s', repr(w[i]), v[:, i]) |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 204 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 205 | glog.debug('K is %s', repr(self.K_transformed)) |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 206 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 207 | # Design a kalman filter here as well. |
| 208 | q_pos = 0.05 |
| 209 | q_vel = 2.65 |
| 210 | q_volts = 0.005 |
| 211 | self.Q = numpy.matrix( |
| 212 | numpy.diag([(q_pos**2.0), (q_vel**2.0), (q_pos**2.0), (q_vel**2.0), |
| 213 | (q_volts**2.0)])) |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 214 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 215 | r_nm = 0.025 |
| 216 | self.R = numpy.matrix(numpy.diag([(r_nm**2.0), (r_nm**2.0)])) |
| 217 | |
| 218 | glog.debug('Overall poles are %s, for %s', |
| 219 | repr(numpy.linalg.eig(self.A - self.B * self.K)[0]), |
| 220 | self._name) |
| 221 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 222 | self.KalmanGain, self.Q_steady = controls.kalman(A=self.A, |
| 223 | B=self.B, |
| 224 | C=self.C, |
| 225 | Q=self.Q, |
| 226 | R=self.R) |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 227 | |
| 228 | self.InitializeState() |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 229 | |
| 230 | |
| 231 | class ScenarioPlotter(object): |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 232 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 233 | def __init__(self): |
| 234 | # Various lists for graphing things. |
| 235 | self.t = [] |
| 236 | self.x_motor = [] |
| 237 | self.x_output = [] |
| 238 | self.v = [] |
| 239 | self.goal_v = [] |
| 240 | self.a = [] |
| 241 | self.spring = [] |
| 242 | self.x_hat = [] |
| 243 | self.u = [] |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 244 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 245 | def run_test(self, |
| 246 | intake, |
| 247 | iterations=400, |
| 248 | controller_intake=None, |
| 249 | observer_intake=None): |
| 250 | """Runs the intake plant with an initial condition and goal. |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 251 | |
| 252 | Test for whether the goal has been reached and whether the separation |
| 253 | goes outside of the initial and goal values by more than |
| 254 | max_separation_error. |
| 255 | |
| 256 | Prints out something for a failure of either condition and returns |
| 257 | False if tests fail. |
| 258 | Args: |
| 259 | intake: intake object to use. |
| 260 | iterations: Number of timesteps to run the model for. |
| 261 | controller_intake: Intake object to get K from, or None if we should |
| 262 | use intake. |
| 263 | observer_intake: Intake object to use for the observer, or None if we |
| 264 | should use the actual state. |
| 265 | """ |
| 266 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 267 | if controller_intake is None: |
| 268 | controller_intake = intake |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 269 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 270 | vbat = 12.0 |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 271 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 272 | if self.t: |
| 273 | initial_t = self.t[-1] + intake.dt |
| 274 | else: |
| 275 | initial_t = 0 |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 276 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 277 | # Delay U by 1 cycle in our simulation to make it more realistic. |
| 278 | last_U = numpy.matrix([[0.0]]) |
| 279 | intake.Y = intake.C * intake.X |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 280 | |
Michael Schuh | d77b08c | 2018-02-27 22:15:07 -0800 | [diff] [blame] | 281 | # Start with the intake deflected by 0.2 radians |
| 282 | # intake.X[0,0] = 0.2 |
| 283 | # intake.Y[0,0] = intake.X[0,0] |
| 284 | # observer_intake.X_hat[0,0] = intake.X[0,0] |
| 285 | |
Austin Schuh | 5ea4847 | 2021-02-02 20:46:41 -0800 | [diff] [blame] | 286 | for i in range(iterations): |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 287 | X_hat = intake.X |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 288 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 289 | if observer_intake is not None: |
| 290 | X_hat = observer_intake.X_hat |
| 291 | self.x_hat.append(observer_intake.X_hat[0, 0]) |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 292 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 293 | goal_angle = 3.0 |
| 294 | goal_velocity = numpy.clip((goal_angle - X_hat[0, 0]) * 6.0, -1.0, |
| 295 | 1.0) |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 296 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 297 | self.goal_v.append(goal_velocity) |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 298 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 299 | # Nominal: 1.8 N at 0.25 m -> 0.45 N m |
| 300 | # Nominal: 13 N at 0.25 m at 0.5 radians -> 3.25 N m -> 6 N m / radian |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 301 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 302 | R = numpy.matrix([[0.0], [goal_velocity], [0.0], [goal_velocity], |
| 303 | [goal_velocity / (intake.G * intake.Kv)]]) |
| 304 | U = controller_intake.K * (R - X_hat) + R[4, 0] |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 305 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 306 | U[0, 0] = numpy.clip(U[0, 0], -vbat, vbat) # * 0.0 |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 307 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 308 | self.x_output.append(intake.X[0, 0]) |
| 309 | self.x_motor.append(intake.X[2, 0]) |
| 310 | self.spring.append(intake.X[0, 0] - intake.X[2, 0]) |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 311 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 312 | if self.v: |
| 313 | last_v = self.v[-1] |
| 314 | else: |
| 315 | last_v = 0 |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 316 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 317 | self.v.append(intake.X[1, 0]) |
| 318 | self.a.append((self.v[-1] - last_v) / intake.dt) |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 319 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 320 | if observer_intake is not None: |
| 321 | observer_intake.Y = intake.Y |
| 322 | observer_intake.CorrectObserver(U) |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 323 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 324 | intake.Update(last_U + 0.0) |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 325 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 326 | if observer_intake is not None: |
| 327 | observer_intake.PredictObserver(U) |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 328 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 329 | self.t.append(initial_t + i * intake.dt) |
| 330 | self.u.append(U[0, 0]) |
| 331 | last_U = U |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 332 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 333 | def Plot(self): |
| 334 | pylab.subplot(3, 1, 1) |
| 335 | pylab.plot(self.t, self.x_output, label='x output') |
| 336 | pylab.plot(self.t, self.x_motor, label='x motor') |
| 337 | pylab.plot(self.t, self.x_hat, label='x_hat') |
| 338 | pylab.legend() |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 339 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 340 | spring_ax1 = pylab.subplot(3, 1, 2) |
| 341 | spring_ax1.plot(self.t, self.u, 'k', label='u') |
| 342 | spring_ax2 = spring_ax1.twinx() |
| 343 | spring_ax2.plot(self.t, self.spring, label='spring_angle') |
| 344 | spring_ax1.legend(loc=2) |
| 345 | spring_ax2.legend() |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 346 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 347 | accel_ax1 = pylab.subplot(3, 1, 3) |
| 348 | accel_ax1.plot(self.t, self.a, 'r', label='a') |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 349 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 350 | accel_ax2 = accel_ax1.twinx() |
| 351 | accel_ax2.plot(self.t, self.v, label='v') |
| 352 | accel_ax2.plot(self.t, self.goal_v, label='goal_v') |
| 353 | accel_ax1.legend(loc=2) |
| 354 | accel_ax2.legend() |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 355 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 356 | pylab.show() |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 357 | |
| 358 | |
| 359 | def main(argv): |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 360 | scenario_plotter = ScenarioPlotter() |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 361 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 362 | intake = Intake() |
| 363 | intake.X[0, 0] = 0.0 |
| 364 | intake_controller = DelayedIntake() |
| 365 | observer_intake = DelayedIntake() |
| 366 | observer_intake.X_hat[0, 0] = intake.X[0, 0] |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 367 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 368 | # Test moving the intake with constant separation. |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 369 | scenario_plotter.run_test(intake, |
| 370 | controller_intake=intake_controller, |
| 371 | observer_intake=observer_intake, |
| 372 | iterations=200) |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 373 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 374 | if FLAGS.plot: |
| 375 | scenario_plotter.Plot() |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 376 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 377 | # Write the generated constants out to a file. |
| 378 | if len(argv) != 5: |
| 379 | glog.fatal( |
| 380 | 'Expected .h file name and .cc file name for intake and delayed_intake.' |
| 381 | ) |
| 382 | else: |
| 383 | namespaces = ['y2018', 'control_loops', 'superstructure', 'intake'] |
| 384 | intake = Intake('Intake') |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 385 | loop_writer = control_loop.ControlLoopWriter('Intake', [intake], |
| 386 | namespaces=namespaces) |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 387 | loop_writer.AddConstant( |
| 388 | control_loop.Constant('kGearRatio', '%f', intake.G)) |
| 389 | loop_writer.AddConstant( |
| 390 | control_loop.Constant('kMotorVelocityConstant', '%f', intake.Kv)) |
| 391 | loop_writer.AddConstant( |
| 392 | control_loop.Constant('kFreeSpeed', '%f', intake.free_speed)) |
| 393 | loop_writer.Write(argv[1], argv[2]) |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 394 | |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 395 | delayed_intake = DelayedIntake('DelayedIntake') |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 396 | loop_writer = control_loop.ControlLoopWriter('DelayedIntake', |
| 397 | [delayed_intake], |
| 398 | namespaces=namespaces) |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 399 | loop_writer.Write(argv[3], argv[4]) |
| 400 | |
Sabina Davis | 3922dfa | 2018-02-10 23:10:05 -0800 | [diff] [blame] | 401 | |
Michael Schuh | 10dd1e0 | 2018-01-20 13:19:44 -0800 | [diff] [blame] | 402 | if __name__ == '__main__': |
Austin Schuh | 17dd089 | 2018-03-02 20:06:31 -0800 | [diff] [blame] | 403 | argv = FLAGS(sys.argv) |
| 404 | glog.init() |
| 405 | sys.exit(main(argv)) |