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