Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | from frc971.control_loops.python import control_loop |
| 4 | from frc971.control_loops.python import controls |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 5 | import numpy |
| 6 | import sys |
| 7 | import matplotlib |
| 8 | from matplotlib import pylab |
| 9 | import gflags |
| 10 | import glog |
| 11 | |
| 12 | FLAGS = gflags.FLAGS |
| 13 | |
| 14 | try: |
| 15 | gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.') |
| 16 | except gflags.DuplicateFlagError: |
| 17 | pass |
| 18 | |
| 19 | class Intake(control_loop.ControlLoop): |
Austin Schuh | 07cb585 | 2016-01-31 00:58:46 -0800 | [diff] [blame] | 20 | def __init__(self, name="Intake"): |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 21 | super(Intake, self).__init__(name) |
| 22 | # TODO(constants): Update all of these & retune poles. |
| 23 | # Stall Torque in N m |
Austin Schuh | 07cb585 | 2016-01-31 00:58:46 -0800 | [diff] [blame] | 24 | self.stall_torque = 0.71 |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 25 | # Stall Current in Amps |
Austin Schuh | 07cb585 | 2016-01-31 00:58:46 -0800 | [diff] [blame] | 26 | self.stall_current = 134 |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 27 | # Free Speed in RPM |
Austin Schuh | 07cb585 | 2016-01-31 00:58:46 -0800 | [diff] [blame] | 28 | self.free_speed = 18730 |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 29 | # Free Current in Amps |
Austin Schuh | 07cb585 | 2016-01-31 00:58:46 -0800 | [diff] [blame] | 30 | self.free_current = 0.7 |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 31 | |
| 32 | # Resistance of the motor |
| 33 | self.R = 12.0 / self.stall_current |
| 34 | # Motor velocity constant |
| 35 | self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) / |
| 36 | (12.0 - self.R * self.free_current)) |
| 37 | # Torque constant |
| 38 | self.Kt = self.stall_torque / self.stall_current |
| 39 | # Gear ratio |
Austin Schuh | 07cb585 | 2016-01-31 00:58:46 -0800 | [diff] [blame] | 40 | self.G = (56.0 / 12.0) * (54.0 / 14.0) * (64.0 / 18.0) * (48.0 / 16.0) |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 41 | |
Austin Schuh | 07cb585 | 2016-01-31 00:58:46 -0800 | [diff] [blame] | 42 | self.J = 0.9 |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 43 | |
| 44 | # Control loop time step |
| 45 | self.dt = 0.005 |
| 46 | |
| 47 | # State is [position, velocity] |
| 48 | # Input is [Voltage] |
| 49 | |
| 50 | C1 = self.G * self.G * self.Kt / (self.R * self.J * self.Kv) |
| 51 | C2 = self.Kt * self.G / (self.J * self.R) |
| 52 | |
| 53 | self.A_continuous = numpy.matrix( |
| 54 | [[0, 1], |
| 55 | [0, -C1]]) |
| 56 | |
| 57 | # Start with the unmodified input |
| 58 | self.B_continuous = numpy.matrix( |
| 59 | [[0], |
| 60 | [C2]]) |
| 61 | |
| 62 | self.C = numpy.matrix([[1, 0]]) |
| 63 | self.D = numpy.matrix([[0]]) |
| 64 | |
| 65 | self.A, self.B = self.ContinuousToDiscrete( |
| 66 | self.A_continuous, self.B_continuous, self.dt) |
| 67 | |
| 68 | controllability = controls.ctrb(self.A, self.B) |
| 69 | |
Austin Schuh | a88c407 | 2016-02-06 14:31:03 -0800 | [diff] [blame^] | 70 | glog.debug("Free speed is %f", self.free_speed * numpy.pi * 2.0 / 60.0 / self.G) |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 71 | |
Austin Schuh | 07cb585 | 2016-01-31 00:58:46 -0800 | [diff] [blame] | 72 | q_pos = 0.20 |
| 73 | q_vel = 5.5 |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 74 | self.Q = numpy.matrix([[(1.0 / (q_pos ** 2.0)), 0.0], |
| 75 | [0.0, (1.0 / (q_vel ** 2.0))]]) |
| 76 | |
| 77 | self.R = numpy.matrix([[(1.0 / (12.0 ** 2.0))]]) |
| 78 | self.K = controls.dlqr(self.A, self.B, self.Q, self.R) |
| 79 | |
Austin Schuh | a88c407 | 2016-02-06 14:31:03 -0800 | [diff] [blame^] | 80 | glog.debug('K %s', repr(self.K)) |
| 81 | glog.debug('Poles are %s', |
| 82 | repr(numpy.linalg.eig(self.A - self.B * self.K)[0])) |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 83 | |
| 84 | self.rpl = 0.30 |
| 85 | self.ipl = 0.10 |
| 86 | self.PlaceObserverPoles([self.rpl + 1j * self.ipl, |
| 87 | self.rpl - 1j * self.ipl]) |
| 88 | |
Austin Schuh | a88c407 | 2016-02-06 14:31:03 -0800 | [diff] [blame^] | 89 | glog.debug('L is %s', repr(self.L)) |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 90 | |
| 91 | q_pos = 0.05 |
| 92 | q_vel = 2.65 |
| 93 | self.Q = numpy.matrix([[(q_pos ** 2.0), 0.0], |
| 94 | [0.0, (q_vel ** 2.0)]]) |
| 95 | |
| 96 | r_volts = 0.025 |
| 97 | self.R = numpy.matrix([[(r_volts ** 2.0)]]) |
| 98 | |
| 99 | self.KalmanGain, self.Q_steady = controls.kalman( |
| 100 | A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R) |
| 101 | |
Austin Schuh | a88c407 | 2016-02-06 14:31:03 -0800 | [diff] [blame^] | 102 | glog.debug('Kal %s', repr(self.KalmanGain)) |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 103 | self.L = self.A * self.KalmanGain |
Austin Schuh | a88c407 | 2016-02-06 14:31:03 -0800 | [diff] [blame^] | 104 | glog.debug('KalL is %s', repr(self.L)) |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 105 | |
| 106 | # The box formed by U_min and U_max must encompass all possible values, |
| 107 | # or else Austin's code gets angry. |
| 108 | self.U_max = numpy.matrix([[12.0]]) |
| 109 | self.U_min = numpy.matrix([[-12.0]]) |
| 110 | |
| 111 | self.InitializeState() |
| 112 | |
| 113 | class IntegralIntake(Intake): |
Austin Schuh | 07cb585 | 2016-01-31 00:58:46 -0800 | [diff] [blame] | 114 | def __init__(self, name="IntegralIntake"): |
| 115 | super(IntegralIntake, self).__init__(name=name) |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 116 | |
| 117 | self.A_continuous_unaugmented = self.A_continuous |
| 118 | self.B_continuous_unaugmented = self.B_continuous |
| 119 | |
| 120 | self.A_continuous = numpy.matrix(numpy.zeros((3, 3))) |
| 121 | self.A_continuous[0:2, 0:2] = self.A_continuous_unaugmented |
| 122 | self.A_continuous[0:2, 2] = self.B_continuous_unaugmented |
| 123 | |
| 124 | self.B_continuous = numpy.matrix(numpy.zeros((3, 1))) |
| 125 | self.B_continuous[0:2, 0] = self.B_continuous_unaugmented |
| 126 | |
| 127 | self.C_unaugmented = self.C |
| 128 | self.C = numpy.matrix(numpy.zeros((1, 3))) |
| 129 | self.C[0:1, 0:2] = self.C_unaugmented |
| 130 | |
| 131 | self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, self.B_continuous, self.dt) |
| 132 | |
| 133 | q_pos = 0.08 |
| 134 | q_vel = 4.00 |
Austin Schuh | 07cb585 | 2016-01-31 00:58:46 -0800 | [diff] [blame] | 135 | q_voltage = 3.0 |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 136 | self.Q = numpy.matrix([[(q_pos ** 2.0), 0.0, 0.0], |
| 137 | [0.0, (q_vel ** 2.0), 0.0], |
| 138 | [0.0, 0.0, (q_voltage ** 2.0)]]) |
| 139 | |
| 140 | r_pos = 0.05 |
| 141 | self.R = numpy.matrix([[(r_pos ** 2.0)]]) |
| 142 | |
| 143 | self.KalmanGain, self.Q_steady = controls.kalman( |
| 144 | A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R) |
| 145 | self.L = self.A * self.KalmanGain |
| 146 | |
| 147 | self.K_unaugmented = self.K |
| 148 | self.K = numpy.matrix(numpy.zeros((1, 3))) |
| 149 | self.K[0, 0:2] = self.K_unaugmented |
| 150 | self.K[0, 2] = 1 |
| 151 | |
| 152 | self.InitializeState() |
Austin Schuh | 07cb585 | 2016-01-31 00:58:46 -0800 | [diff] [blame] | 153 | |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 154 | class ScenarioPlotter(object): |
| 155 | def __init__(self): |
| 156 | # Various lists for graphing things. |
| 157 | self.t = [] |
| 158 | self.x = [] |
| 159 | self.v = [] |
| 160 | self.a = [] |
| 161 | self.x_hat = [] |
| 162 | self.u = [] |
Austin Schuh | 07cb585 | 2016-01-31 00:58:46 -0800 | [diff] [blame] | 163 | self.offset = [] |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 164 | |
| 165 | def run_test(self, intake, goal, iterations=200, controller_intake=None, |
| 166 | observer_intake=None): |
| 167 | """Runs the intake plant with an initial condition and goal. |
| 168 | |
| 169 | Test for whether the goal has been reached and whether the separation |
| 170 | goes outside of the initial and goal values by more than |
| 171 | max_separation_error. |
| 172 | |
| 173 | Prints out something for a failure of either condition and returns |
| 174 | False if tests fail. |
| 175 | Args: |
| 176 | intake: intake object to use. |
| 177 | goal: goal state. |
| 178 | iterations: Number of timesteps to run the model for. |
| 179 | controller_intake: Intake object to get K from, or None if we should |
| 180 | use intake. |
| 181 | observer_intake: Intake object to use for the observer, or None if we should |
| 182 | use the actual state. |
| 183 | """ |
| 184 | |
| 185 | if controller_intake is None: |
| 186 | controller_intake = intake |
| 187 | |
| 188 | vbat = 12.0 |
| 189 | |
| 190 | if self.t: |
| 191 | initial_t = self.t[-1] + intake.dt |
| 192 | else: |
| 193 | initial_t = 0 |
| 194 | |
| 195 | for i in xrange(iterations): |
| 196 | X_hat = intake.X |
| 197 | |
| 198 | if observer_intake is not None: |
| 199 | X_hat = observer_intake.X_hat |
| 200 | self.x_hat.append(observer_intake.X_hat[0, 0]) |
| 201 | |
| 202 | U = controller_intake.K * (goal - X_hat) |
| 203 | U[0, 0] = numpy.clip(U[0, 0], -vbat, vbat) |
| 204 | self.x.append(intake.X[0, 0]) |
| 205 | |
| 206 | if self.v: |
| 207 | last_v = self.v[-1] |
| 208 | else: |
| 209 | last_v = 0 |
| 210 | |
| 211 | self.v.append(intake.X[1, 0]) |
| 212 | self.a.append((self.v[-1] - last_v) / intake.dt) |
| 213 | |
| 214 | if observer_intake is not None: |
| 215 | observer_intake.Y = intake.Y |
| 216 | observer_intake.CorrectObserver(U) |
Austin Schuh | 07cb585 | 2016-01-31 00:58:46 -0800 | [diff] [blame] | 217 | self.offset.append(observer_intake.X_hat[2, 0]) |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 218 | |
Austin Schuh | 07cb585 | 2016-01-31 00:58:46 -0800 | [diff] [blame] | 219 | intake.Update(U + 2.0) |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 220 | |
| 221 | if observer_intake is not None: |
| 222 | observer_intake.PredictObserver(U) |
| 223 | |
| 224 | self.t.append(initial_t + i * intake.dt) |
| 225 | self.u.append(U[0, 0]) |
| 226 | |
| 227 | glog.debug('Time: %f', self.t[-1]) |
| 228 | |
| 229 | def Plot(self): |
| 230 | pylab.subplot(3, 1, 1) |
| 231 | pylab.plot(self.t, self.x, label='x') |
| 232 | pylab.plot(self.t, self.x_hat, label='x_hat') |
| 233 | pylab.legend() |
| 234 | |
| 235 | pylab.subplot(3, 1, 2) |
| 236 | pylab.plot(self.t, self.u, label='u') |
Austin Schuh | 07cb585 | 2016-01-31 00:58:46 -0800 | [diff] [blame] | 237 | pylab.plot(self.t, self.offset, label='voltage_offset') |
| 238 | pylab.legend() |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 239 | |
| 240 | pylab.subplot(3, 1, 3) |
| 241 | pylab.plot(self.t, self.a, label='a') |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 242 | pylab.legend() |
Austin Schuh | 07cb585 | 2016-01-31 00:58:46 -0800 | [diff] [blame] | 243 | |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 244 | pylab.show() |
| 245 | |
| 246 | |
| 247 | def main(argv): |
| 248 | argv = FLAGS(argv) |
Austin Schuh | a88c407 | 2016-02-06 14:31:03 -0800 | [diff] [blame^] | 249 | glog.init() |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 250 | |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 251 | scenario_plotter = ScenarioPlotter() |
| 252 | |
Austin Schuh | 07cb585 | 2016-01-31 00:58:46 -0800 | [diff] [blame] | 253 | intake = Intake() |
| 254 | intake_controller = IntegralIntake() |
| 255 | observer_intake = IntegralIntake() |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 256 | |
| 257 | # Test moving the intake with constant separation. |
| 258 | initial_X = numpy.matrix([[0.0], [0.0]]) |
Austin Schuh | 07cb585 | 2016-01-31 00:58:46 -0800 | [diff] [blame] | 259 | R = numpy.matrix([[numpy.pi/2.0], [0.0], [0.0]]) |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 260 | scenario_plotter.run_test(intake, goal=R, controller_intake=intake_controller, |
| 261 | observer_intake=observer_intake, iterations=200) |
| 262 | |
| 263 | if FLAGS.plot: |
| 264 | scenario_plotter.Plot() |
| 265 | |
| 266 | # Write the generated constants out to a file. |
| 267 | if len(argv) != 5: |
| 268 | glog.fatal('Expected .h file name and .cc file name for the intake and integral intake.') |
| 269 | else: |
| 270 | namespaces = ['y2016', 'control_loops', 'superstructure'] |
| 271 | intake = Intake("Intake") |
| 272 | loop_writer = control_loop.ControlLoopWriter('Intake', [intake], |
| 273 | namespaces=namespaces) |
| 274 | loop_writer.Write(argv[1], argv[2]) |
| 275 | |
Austin Schuh | 07cb585 | 2016-01-31 00:58:46 -0800 | [diff] [blame] | 276 | integral_intake = IntegralIntake("IntegralIntake") |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 277 | integral_loop_writer = control_loop.ControlLoopWriter("IntegralIntake", [integral_intake], |
Austin Schuh | 07cb585 | 2016-01-31 00:58:46 -0800 | [diff] [blame] | 278 | namespaces=namespaces) |
Comran Morshed | 2ae094e | 2016-01-23 20:43:20 +0000 | [diff] [blame] | 279 | integral_loop_writer.Write(argv[3], argv[4]) |
| 280 | |
| 281 | if __name__ == '__main__': |
| 282 | sys.exit(main(sys.argv)) |