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: |
| 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): |
| 20 | def __init__(self, name='Intake'): |
| 21 | super(Intake, self).__init__(name) |
| 22 | # Stall Torque in N m |
| 23 | self.stall_torque = 0.71 |
| 24 | # Stall Current in Amps |
| 25 | self.stall_current = 134.0 |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 26 | self.free_speed_rpm = 18730.0 |
| 27 | # Free Speed in rotations/second. |
| 28 | self.free_speed = self.free_speed_rpm / 60.0 |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 29 | # Free Current in Amps |
| 30 | self.free_current = 0.7 |
| 31 | |
| 32 | # Resistance of the motor |
| 33 | self.R = 12.0 / self.stall_current |
| 34 | # Motor velocity constant |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 35 | self.Kv = ((self.free_speed * 2.0 * numpy.pi) / |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 36 | (12.0 - self.R * self.free_current)) |
| 37 | # Torque constant |
| 38 | self.Kt = self.stall_torque / self.stall_current |
| 39 | |
| 40 | # (1 / 35.0) * (20.0 / 40.0) -> 16 tooth sprocket on #25 chain |
| 41 | # Gear ratio |
| 42 | self.G = (1.0 / 35.0) * (20.0 / 40.0) |
| 43 | self.r = 16.0 * 0.25 / (2.0 * numpy.pi) * 0.0254 |
| 44 | |
| 45 | # Motor inertia in kg m^2 |
| 46 | self.motor_inertia = 0.00001187 |
| 47 | |
| 48 | # 5.4 kg of moving mass for the intake |
| 49 | self.mass = 5.4 + self.motor_inertia / ((self.G * self.r) ** 2.0) |
| 50 | |
| 51 | # Control loop time step |
| 52 | self.dt = 0.005 |
| 53 | |
| 54 | # State is [position, velocity] |
| 55 | # Input is [Voltage] |
| 56 | |
| 57 | C1 = self.Kt / (self.G * self.G * self.r * self.r * self.R * self.mass * self.Kv) |
| 58 | C2 = self.Kt / (self.G * self.r * self.R * self.mass) |
| 59 | |
| 60 | self.A_continuous = numpy.matrix( |
| 61 | [[0, 1], |
| 62 | [0, -C1]]) |
| 63 | |
| 64 | # Start with the unmodified input |
| 65 | self.B_continuous = numpy.matrix( |
| 66 | [[0], |
| 67 | [C2]]) |
| 68 | glog.debug(repr(self.A_continuous)) |
| 69 | glog.debug(repr(self.B_continuous)) |
| 70 | |
| 71 | self.C = numpy.matrix([[1, 0]]) |
| 72 | self.D = numpy.matrix([[0]]) |
| 73 | |
| 74 | self.A, self.B = self.ContinuousToDiscrete( |
| 75 | self.A_continuous, self.B_continuous, self.dt) |
| 76 | |
| 77 | controllability = controls.ctrb(self.A, self.B) |
| 78 | |
| 79 | glog.debug('Free speed is %f', |
| 80 | -self.B_continuous[1, 0] / self.A_continuous[1, 1] * 12.0) |
| 81 | |
| 82 | q_pos = 0.015 |
| 83 | q_vel = 0.3 |
| 84 | self.Q = numpy.matrix([[(1.0 / (q_pos ** 2.0)), 0.0], |
| 85 | [0.0, (1.0 / (q_vel ** 2.0))]]) |
| 86 | |
| 87 | self.R = numpy.matrix([[(1.0 / (12.0 ** 2.0))]]) |
| 88 | self.K = controls.dlqr(self.A, self.B, self.Q, self.R) |
| 89 | |
| 90 | q_pos_ff = 0.005 |
| 91 | q_vel_ff = 1.0 |
| 92 | self.Qff = numpy.matrix([[(1.0 / (q_pos_ff ** 2.0)), 0.0], |
| 93 | [0.0, (1.0 / (q_vel_ff ** 2.0))]]) |
| 94 | |
| 95 | self.Kff = controls.TwoStateFeedForwards(self.B, self.Qff) |
| 96 | |
| 97 | glog.debug('K %s', repr(self.K)) |
| 98 | glog.debug('Poles are %s', |
| 99 | repr(numpy.linalg.eig(self.A - self.B * self.K)[0])) |
| 100 | |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 101 | q_pos = 0.10 |
| 102 | q_vel = 1.65 |
| 103 | self.Q = numpy.matrix([[(q_pos ** 2.0), 0.0], |
| 104 | [0.0, (q_vel ** 2.0)]]) |
| 105 | |
| 106 | r_volts = 0.025 |
| 107 | self.R = numpy.matrix([[(r_volts ** 2.0)]]) |
| 108 | |
| 109 | self.KalmanGain, self.Q_steady = controls.kalman( |
| 110 | A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R) |
| 111 | |
| 112 | glog.debug('Kal %s', repr(self.KalmanGain)) |
| 113 | self.L = self.A * self.KalmanGain |
| 114 | glog.debug('KalL is %s', repr(self.L)) |
| 115 | |
| 116 | # The box formed by U_min and U_max must encompass all possible values, |
| 117 | # or else Austin's code gets angry. |
| 118 | self.U_max = numpy.matrix([[12.0]]) |
| 119 | self.U_min = numpy.matrix([[-12.0]]) |
| 120 | |
| 121 | self.InitializeState() |
| 122 | |
| 123 | class IntegralIntake(Intake): |
| 124 | def __init__(self, name='IntegralIntake'): |
| 125 | super(IntegralIntake, self).__init__(name=name) |
| 126 | |
| 127 | self.A_continuous_unaugmented = self.A_continuous |
| 128 | self.B_continuous_unaugmented = self.B_continuous |
| 129 | |
| 130 | self.A_continuous = numpy.matrix(numpy.zeros((3, 3))) |
| 131 | self.A_continuous[0:2, 0:2] = self.A_continuous_unaugmented |
| 132 | self.A_continuous[0:2, 2] = self.B_continuous_unaugmented |
| 133 | |
| 134 | self.B_continuous = numpy.matrix(numpy.zeros((3, 1))) |
| 135 | self.B_continuous[0:2, 0] = self.B_continuous_unaugmented |
| 136 | |
| 137 | self.C_unaugmented = self.C |
| 138 | self.C = numpy.matrix(numpy.zeros((1, 3))) |
| 139 | self.C[0:1, 0:2] = self.C_unaugmented |
| 140 | |
| 141 | self.A, self.B = self.ContinuousToDiscrete( |
| 142 | self.A_continuous, self.B_continuous, self.dt) |
| 143 | |
| 144 | q_pos = 0.12 |
| 145 | q_vel = 2.00 |
| 146 | q_voltage = 40.0 |
| 147 | self.Q = numpy.matrix([[(q_pos ** 2.0), 0.0, 0.0], |
| 148 | [0.0, (q_vel ** 2.0), 0.0], |
| 149 | [0.0, 0.0, (q_voltage ** 2.0)]]) |
| 150 | |
| 151 | r_pos = 0.05 |
| 152 | self.R = numpy.matrix([[(r_pos ** 2.0)]]) |
| 153 | |
| 154 | self.KalmanGain, self.Q_steady = controls.kalman( |
| 155 | A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R) |
| 156 | self.L = self.A * self.KalmanGain |
| 157 | |
| 158 | self.K_unaugmented = self.K |
| 159 | self.K = numpy.matrix(numpy.zeros((1, 3))) |
| 160 | self.K[0, 0:2] = self.K_unaugmented |
| 161 | self.K[0, 2] = 1 |
| 162 | |
| 163 | self.Kff = numpy.concatenate((self.Kff, numpy.matrix(numpy.zeros((1, 1)))), axis=1) |
| 164 | |
| 165 | self.InitializeState() |
| 166 | |
| 167 | class ScenarioPlotter(object): |
| 168 | def __init__(self): |
| 169 | # Various lists for graphing things. |
| 170 | self.t = [] |
| 171 | self.x = [] |
| 172 | self.v = [] |
| 173 | self.a = [] |
| 174 | self.x_hat = [] |
| 175 | self.u = [] |
| 176 | self.offset = [] |
| 177 | |
| 178 | def run_test(self, intake, end_goal, |
| 179 | controller_intake, |
| 180 | observer_intake=None, |
| 181 | iterations=200): |
| 182 | """Runs the intake plant with an initial condition and goal. |
| 183 | |
| 184 | Args: |
| 185 | intake: intake object to use. |
| 186 | end_goal: end_goal state. |
| 187 | controller_intake: Intake object to get K from, or None if we should |
| 188 | use intake. |
| 189 | observer_intake: Intake object to use for the observer, or None if we should |
| 190 | use the actual state. |
| 191 | iterations: Number of timesteps to run the model for. |
| 192 | """ |
| 193 | |
| 194 | if controller_intake is None: |
| 195 | controller_intake = intake |
| 196 | |
| 197 | vbat = 12.0 |
| 198 | |
| 199 | if self.t: |
| 200 | initial_t = self.t[-1] + intake.dt |
| 201 | else: |
| 202 | initial_t = 0 |
| 203 | |
| 204 | goal = numpy.concatenate((intake.X, numpy.matrix(numpy.zeros((1, 1)))), axis=0) |
| 205 | |
| 206 | profile = TrapezoidProfile(intake.dt) |
| 207 | profile.set_maximum_acceleration(10.0) |
| 208 | profile.set_maximum_velocity(0.3) |
| 209 | profile.SetGoal(goal[0, 0]) |
| 210 | |
| 211 | U_last = numpy.matrix(numpy.zeros((1, 1))) |
| 212 | for i in xrange(iterations): |
| 213 | observer_intake.Y = intake.Y |
| 214 | observer_intake.CorrectObserver(U_last) |
| 215 | |
| 216 | self.offset.append(observer_intake.X_hat[2, 0]) |
| 217 | self.x_hat.append(observer_intake.X_hat[0, 0]) |
| 218 | |
| 219 | next_goal = numpy.concatenate( |
| 220 | (profile.Update(end_goal[0, 0], end_goal[1, 0]), |
| 221 | numpy.matrix(numpy.zeros((1, 1)))), |
| 222 | axis=0) |
| 223 | |
| 224 | ff_U = controller_intake.Kff * (next_goal - observer_intake.A * goal) |
| 225 | |
| 226 | U_uncapped = controller_intake.K * (goal - observer_intake.X_hat) + ff_U |
| 227 | U_uncapped = controller_intake.K * (end_goal - observer_intake.X_hat) |
| 228 | U = U_uncapped.copy() |
| 229 | U[0, 0] = numpy.clip(U[0, 0], -vbat, vbat) |
| 230 | self.x.append(intake.X[0, 0]) |
| 231 | |
| 232 | if self.v: |
| 233 | last_v = self.v[-1] |
| 234 | else: |
| 235 | last_v = 0 |
| 236 | |
| 237 | self.v.append(intake.X[1, 0]) |
| 238 | self.a.append((self.v[-1] - last_v) / intake.dt) |
| 239 | |
| 240 | offset = 0.0 |
| 241 | if i > 100: |
| 242 | offset = 2.0 |
| 243 | intake.Update(U + offset) |
| 244 | |
| 245 | observer_intake.PredictObserver(U) |
| 246 | |
| 247 | self.t.append(initial_t + i * intake.dt) |
| 248 | self.u.append(U[0, 0]) |
| 249 | |
| 250 | ff_U -= U_uncapped - U |
| 251 | goal = controller_intake.A * goal + controller_intake.B * ff_U |
| 252 | |
| 253 | if U[0, 0] != U_uncapped[0, 0]: |
| 254 | profile.MoveCurrentState( |
| 255 | numpy.matrix([[goal[0, 0]], [goal[1, 0]]])) |
| 256 | |
| 257 | glog.debug('Time: %f', self.t[-1]) |
| 258 | glog.debug('goal_error %s', repr(end_goal - goal)) |
| 259 | glog.debug('error %s', repr(observer_intake.X_hat - end_goal)) |
| 260 | |
| 261 | def Plot(self): |
| 262 | pylab.subplot(3, 1, 1) |
| 263 | pylab.plot(self.t, self.x, label='x') |
| 264 | pylab.plot(self.t, self.x_hat, label='x_hat') |
| 265 | pylab.legend() |
| 266 | |
| 267 | pylab.subplot(3, 1, 2) |
| 268 | pylab.plot(self.t, self.u, label='u') |
| 269 | pylab.plot(self.t, self.offset, label='voltage_offset') |
| 270 | pylab.legend() |
| 271 | |
| 272 | pylab.subplot(3, 1, 3) |
| 273 | pylab.plot(self.t, self.a, label='a') |
| 274 | pylab.legend() |
| 275 | |
| 276 | pylab.show() |
| 277 | |
| 278 | |
| 279 | def main(argv): |
| 280 | scenario_plotter = ScenarioPlotter() |
| 281 | |
| 282 | intake = Intake() |
| 283 | intake_controller = IntegralIntake() |
| 284 | observer_intake = IntegralIntake() |
| 285 | |
| 286 | # Test moving the intake with constant separation. |
| 287 | initial_X = numpy.matrix([[0.0], [0.0]]) |
| 288 | R = numpy.matrix([[0.1], [0.0], [0.0]]) |
| 289 | scenario_plotter.run_test(intake, end_goal=R, |
| 290 | controller_intake=intake_controller, |
| 291 | observer_intake=observer_intake, iterations=400) |
| 292 | |
| 293 | if FLAGS.plot: |
| 294 | scenario_plotter.Plot() |
| 295 | |
| 296 | # Write the generated constants out to a file. |
| 297 | if len(argv) != 5: |
| 298 | glog.fatal('Expected .h file name and .cc file name for the intake and integral intake.') |
| 299 | else: |
| 300 | namespaces = ['y2017', 'control_loops', 'superstructure', 'intake'] |
| 301 | intake = Intake('Intake') |
| 302 | loop_writer = control_loop.ControlLoopWriter('Intake', [intake], |
| 303 | namespaces=namespaces) |
Brian Silverman | 052e69d | 2017-02-12 16:19:55 -0800 | [diff] [blame] | 304 | loop_writer.AddConstant(control_loop.Constant('kFreeSpeed', '%f', |
| 305 | intake.free_speed)) |
| 306 | loop_writer.AddConstant(control_loop.Constant('kOutputRatio', '%f', |
| 307 | intake.G * intake.r)) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 308 | loop_writer.Write(argv[1], argv[2]) |
| 309 | |
| 310 | integral_intake = IntegralIntake('IntegralIntake') |
| 311 | integral_loop_writer = control_loop.ControlLoopWriter('IntegralIntake', [integral_intake], |
| 312 | namespaces=namespaces) |
| 313 | integral_loop_writer.Write(argv[3], argv[4]) |
| 314 | |
| 315 | if __name__ == '__main__': |
| 316 | argv = FLAGS(sys.argv) |
| 317 | glog.init() |
| 318 | sys.exit(main(argv)) |