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