Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | from frc971.control_loops.python import control_loop |
| 4 | from frc971.control_loops.python import controls |
| 5 | import numpy |
| 6 | import sys |
| 7 | from matplotlib import pylab |
| 8 | |
| 9 | import gflags |
| 10 | import glog |
| 11 | |
| 12 | FLAGS = gflags.FLAGS |
| 13 | |
| 14 | gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.') |
| 15 | |
| 16 | class VelocityIndexer(control_loop.ControlLoop): |
| 17 | def __init__(self, name='VelocityIndexer'): |
| 18 | super(VelocityIndexer, self).__init__(name) |
| 19 | # Stall Torque in N m |
| 20 | self.stall_torque = 0.71 |
| 21 | # Stall Current in Amps |
| 22 | self.stall_current = 134 |
| 23 | # Free Speed in RPM |
| 24 | self.free_speed = 18730.0 |
| 25 | # Free Current in Amps |
| 26 | self.free_current = 0.7 |
| 27 | # Moment of inertia of the indexer halves in kg m^2 |
| 28 | # This is measured as Iyy in CAD (the moment of inertia around the Y axis). |
| 29 | # Inner part of indexer -> Iyy = 59500 lb * mm * mm |
| 30 | # Inner spins with 12 / 48 * 18 / 48 * 24 / 36 * 16 / 72 |
| 31 | # Outer part of indexer -> Iyy = 210000 lb * mm * mm |
| 32 | # 1 775 pro -> 12 / 48 * 18 / 48 * 30 / 422 |
| 33 | |
| 34 | self.J_inner = 0.0269 |
| 35 | self.J_outer = 0.0952 |
| 36 | # Gear ratios for the inner and outer parts. |
Ed Jordan | 8683f43 | 2017-02-12 00:13:26 +0000 | [diff] [blame^] | 37 | self.G_inner = (12.0 / 48.0) * (18.0 / 36.0) * (12.0 / 84.0) |
| 38 | self.G_outer = (12.0 / 48.0) * (18.0 / 36.0) * (24.0 / 420.0) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 39 | |
| 40 | # Motor inertia in kg * m^2 |
| 41 | self.motor_inertia = 0.000006 |
| 42 | |
| 43 | # The output coordinate system is in radians for the inner part of the |
| 44 | # indexer. |
| 45 | # Compute the effective moment of inertia assuming all the mass is in that |
| 46 | # coordinate system. |
| 47 | self.J = ( |
| 48 | self.J_inner * self.G_inner * self.G_inner + |
| 49 | self.J_outer * self.G_outer * self.G_outer) / (self.G_inner * self.G_inner) + \ |
| 50 | self.motor_inertia * ((1.0 / self.G_inner) ** 2.0) |
| 51 | glog.debug('J is %f', self.J) |
| 52 | self.G = self.G_inner |
| 53 | |
| 54 | # Resistance of the motor, divided by 2 to account for the 2 motors |
| 55 | self.R = 12.0 / self.stall_current |
| 56 | # Motor velocity constant |
| 57 | self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) / |
| 58 | (12.0 - self.R * self.free_current)) |
| 59 | # Torque constant |
| 60 | self.Kt = self.stall_torque / self.stall_current |
| 61 | # Control loop time step |
| 62 | self.dt = 0.005 |
| 63 | |
| 64 | # State feedback matrices |
| 65 | # [angular velocity] |
| 66 | self.A_continuous = numpy.matrix( |
| 67 | [[-self.Kt / self.Kv / (self.J * self.G * self.G * self.R)]]) |
| 68 | self.B_continuous = numpy.matrix( |
| 69 | [[self.Kt / (self.J * self.G * self.R)]]) |
| 70 | self.C = numpy.matrix([[1]]) |
| 71 | self.D = numpy.matrix([[0]]) |
| 72 | |
| 73 | self.A, self.B = self.ContinuousToDiscrete( |
| 74 | self.A_continuous, self.B_continuous, self.dt) |
| 75 | |
| 76 | self.PlaceControllerPoles([.82]) |
| 77 | glog.debug(repr(self.K)) |
| 78 | |
| 79 | self.PlaceObserverPoles([0.3]) |
| 80 | |
| 81 | self.U_max = numpy.matrix([[12.0]]) |
| 82 | self.U_min = numpy.matrix([[-12.0]]) |
| 83 | |
| 84 | qff_vel = 8.0 |
| 85 | self.Qff = numpy.matrix([[1.0 / (qff_vel ** 2.0)]]) |
| 86 | |
| 87 | self.Kff = controls.TwoStateFeedForwards(self.B, self.Qff) |
| 88 | self.InitializeState() |
| 89 | |
| 90 | |
| 91 | class Indexer(VelocityIndexer): |
| 92 | def __init__(self, name='Indexer'): |
| 93 | super(Indexer, self).__init__(name) |
| 94 | |
| 95 | self.A_continuous_unaugmented = self.A_continuous |
| 96 | self.B_continuous_unaugmented = self.B_continuous |
| 97 | |
| 98 | self.A_continuous = numpy.matrix(numpy.zeros((2, 2))) |
| 99 | self.A_continuous[1:2, 1:2] = self.A_continuous_unaugmented |
| 100 | self.A_continuous[0, 1] = 1 |
| 101 | |
| 102 | self.B_continuous = numpy.matrix(numpy.zeros((2, 1))) |
| 103 | self.B_continuous[1:2, 0] = self.B_continuous_unaugmented |
| 104 | |
| 105 | # State feedback matrices |
| 106 | # [position, angular velocity] |
| 107 | self.C = numpy.matrix([[1, 0]]) |
| 108 | self.D = numpy.matrix([[0]]) |
| 109 | |
| 110 | self.A, self.B = self.ContinuousToDiscrete( |
| 111 | self.A_continuous, self.B_continuous, self.dt) |
| 112 | |
| 113 | self.rpl = .45 |
| 114 | self.ipl = 0.07 |
| 115 | self.PlaceObserverPoles([self.rpl + 1j * self.ipl, |
| 116 | self.rpl - 1j * self.ipl]) |
| 117 | |
| 118 | self.K_unaugmented = self.K |
| 119 | self.K = numpy.matrix(numpy.zeros((1, 2))) |
| 120 | self.K[0, 1:2] = self.K_unaugmented |
| 121 | self.Kff_unaugmented = self.Kff |
| 122 | self.Kff = numpy.matrix(numpy.zeros((1, 2))) |
| 123 | self.Kff[0, 1:2] = self.Kff_unaugmented |
| 124 | |
| 125 | self.InitializeState() |
| 126 | |
| 127 | |
| 128 | class IntegralIndexer(Indexer): |
| 129 | def __init__(self, name="IntegralIndexer"): |
| 130 | super(IntegralIndexer, self).__init__(name=name) |
| 131 | |
| 132 | self.A_continuous_unaugmented = self.A_continuous |
| 133 | self.B_continuous_unaugmented = self.B_continuous |
| 134 | |
| 135 | self.A_continuous = numpy.matrix(numpy.zeros((3, 3))) |
| 136 | self.A_continuous[0:2, 0:2] = self.A_continuous_unaugmented |
| 137 | self.A_continuous[0:2, 2] = self.B_continuous_unaugmented |
| 138 | |
| 139 | self.B_continuous = numpy.matrix(numpy.zeros((3, 1))) |
| 140 | self.B_continuous[0:2, 0] = self.B_continuous_unaugmented |
| 141 | |
| 142 | self.C_unaugmented = self.C |
| 143 | self.C = numpy.matrix(numpy.zeros((1, 3))) |
| 144 | self.C[0:1, 0:2] = self.C_unaugmented |
| 145 | |
| 146 | self.A, self.B = self.ContinuousToDiscrete( |
| 147 | self.A_continuous, self.B_continuous, self.dt) |
| 148 | |
| 149 | q_pos = 2.0 |
| 150 | q_vel = 0.001 |
| 151 | q_voltage = 10.0 |
| 152 | self.Q = numpy.matrix([[(q_pos ** 2.0), 0.0, 0.0], |
| 153 | [0.0, (q_vel ** 2.0), 0.0], |
| 154 | [0.0, 0.0, (q_voltage ** 2.0)]]) |
| 155 | |
| 156 | r_pos = 0.001 |
| 157 | self.R = numpy.matrix([[(r_pos ** 2.0)]]) |
| 158 | |
| 159 | self.KalmanGain, self.Q_steady = controls.kalman( |
| 160 | A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R) |
| 161 | self.L = self.A * self.KalmanGain |
| 162 | |
| 163 | self.K_unaugmented = self.K |
| 164 | self.K = numpy.matrix(numpy.zeros((1, 3))) |
| 165 | self.K[0, 0:2] = self.K_unaugmented |
| 166 | self.K[0, 2] = 1 |
| 167 | self.Kff_unaugmented = self.Kff |
| 168 | self.Kff = numpy.matrix(numpy.zeros((1, 3))) |
| 169 | self.Kff[0, 0:2] = self.Kff_unaugmented |
| 170 | |
| 171 | self.InitializeState() |
| 172 | |
| 173 | |
| 174 | class ScenarioPlotter(object): |
| 175 | def __init__(self): |
| 176 | # Various lists for graphing things. |
| 177 | self.t = [] |
| 178 | self.x = [] |
| 179 | self.v = [] |
| 180 | self.a = [] |
| 181 | self.x_hat = [] |
| 182 | self.u = [] |
| 183 | self.offset = [] |
| 184 | |
| 185 | def run_test(self, indexer, goal, iterations=200, controller_indexer=None, |
| 186 | observer_indexer=None): |
| 187 | """Runs the indexer plant with an initial condition and goal. |
| 188 | |
| 189 | Args: |
| 190 | indexer: Indexer object to use. |
| 191 | goal: goal state. |
| 192 | iterations: Number of timesteps to run the model for. |
| 193 | controller_indexer: Indexer object to get K from, or None if we should |
| 194 | use indexer. |
| 195 | observer_indexer: Indexer object to use for the observer, or None if we |
| 196 | should use the actual state. |
| 197 | """ |
| 198 | |
| 199 | if controller_indexer is None: |
| 200 | controller_indexer = indexer |
| 201 | |
| 202 | vbat = 12.0 |
| 203 | |
| 204 | if self.t: |
| 205 | initial_t = self.t[-1] + indexer.dt |
| 206 | else: |
| 207 | initial_t = 0 |
| 208 | |
| 209 | for i in xrange(iterations): |
| 210 | X_hat = indexer.X |
| 211 | |
| 212 | if observer_indexer is not None: |
| 213 | X_hat = observer_indexer.X_hat |
| 214 | self.x_hat.append(observer_indexer.X_hat[1, 0]) |
| 215 | |
| 216 | ff_U = controller_indexer.Kff * (goal - observer_indexer.A * goal) |
| 217 | |
| 218 | U = controller_indexer.K * (goal - X_hat) + ff_U |
| 219 | U[0, 0] = numpy.clip(U[0, 0], -vbat, vbat) |
| 220 | self.x.append(indexer.X[0, 0]) |
| 221 | |
| 222 | |
| 223 | if self.v: |
| 224 | last_v = self.v[-1] |
| 225 | else: |
| 226 | last_v = 0 |
| 227 | |
| 228 | self.v.append(indexer.X[1, 0]) |
| 229 | self.a.append((self.v[-1] - last_v) / indexer.dt) |
| 230 | |
| 231 | if observer_indexer is not None: |
| 232 | observer_indexer.Y = indexer.Y |
| 233 | observer_indexer.CorrectObserver(U) |
| 234 | self.offset.append(observer_indexer.X_hat[2, 0]) |
| 235 | |
| 236 | applied_U = U.copy() |
| 237 | if i > 30: |
| 238 | applied_U += 2 |
| 239 | indexer.Update(applied_U) |
| 240 | |
| 241 | if observer_indexer is not None: |
| 242 | observer_indexer.PredictObserver(U) |
| 243 | |
| 244 | self.t.append(initial_t + i * indexer.dt) |
| 245 | self.u.append(U[0, 0]) |
| 246 | |
| 247 | def Plot(self): |
| 248 | pylab.subplot(3, 1, 1) |
| 249 | pylab.plot(self.t, self.v, label='x') |
| 250 | pylab.plot(self.t, self.x_hat, label='x_hat') |
| 251 | pylab.legend() |
| 252 | |
| 253 | pylab.subplot(3, 1, 2) |
| 254 | pylab.plot(self.t, self.u, label='u') |
| 255 | pylab.plot(self.t, self.offset, label='voltage_offset') |
| 256 | pylab.legend() |
| 257 | |
| 258 | pylab.subplot(3, 1, 3) |
| 259 | pylab.plot(self.t, self.a, label='a') |
| 260 | pylab.legend() |
| 261 | |
| 262 | pylab.show() |
| 263 | |
| 264 | |
| 265 | def main(argv): |
| 266 | scenario_plotter = ScenarioPlotter() |
| 267 | |
| 268 | indexer = Indexer() |
| 269 | indexer_controller = IntegralIndexer() |
| 270 | observer_indexer = IntegralIndexer() |
| 271 | |
| 272 | initial_X = numpy.matrix([[0.0], [0.0]]) |
| 273 | R = numpy.matrix([[0.0], [20.0], [0.0]]) |
| 274 | scenario_plotter.run_test(indexer, goal=R, controller_indexer=indexer_controller, |
| 275 | observer_indexer=observer_indexer, iterations=200) |
| 276 | |
| 277 | if FLAGS.plot: |
| 278 | scenario_plotter.Plot() |
| 279 | |
| 280 | if len(argv) != 5: |
| 281 | glog.fatal('Expected .h file name and .cc file name') |
| 282 | else: |
| 283 | namespaces = ['y2017', 'control_loops', 'superstructure', 'indexer'] |
| 284 | indexer = Indexer('Indexer') |
| 285 | loop_writer = control_loop.ControlLoopWriter('Indexer', [indexer], |
| 286 | namespaces=namespaces) |
| 287 | loop_writer.Write(argv[1], argv[2]) |
| 288 | |
| 289 | integral_indexer = IntegralIndexer('IntegralIndexer') |
| 290 | integral_loop_writer = control_loop.ControlLoopWriter( |
| 291 | 'IntegralIndexer', [integral_indexer], namespaces=namespaces) |
| 292 | integral_loop_writer.Write(argv[3], argv[4]) |
| 293 | |
| 294 | |
| 295 | if __name__ == '__main__': |
| 296 | argv = FLAGS(sys.argv) |
| 297 | glog.init() |
| 298 | sys.exit(main(argv)) |