Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
Austin Schuh | 572ff40 | 2015-11-08 12:17:50 -0800 | [diff] [blame] | 3 | from frc971.control_loops.python import control_loop |
| 4 | from frc971.control_loops.python import controls |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 5 | import numpy |
| 6 | import sys |
| 7 | from matplotlib import pylab |
| 8 | |
| 9 | |
| 10 | class CIM(control_loop.ControlLoop): |
| 11 | def __init__(self): |
| 12 | super(CIM, self).__init__("CIM") |
| 13 | # Stall Torque in N m |
| 14 | self.stall_torque = 2.42 |
| 15 | # Stall Current in Amps |
| 16 | self.stall_current = 133 |
| 17 | # Free Speed in RPM |
| 18 | self.free_speed = 4650.0 |
| 19 | # Free Current in Amps |
| 20 | self.free_current = 2.7 |
| 21 | # Moment of inertia of the CIM in kg m^2 |
| 22 | self.J = 0.0001 |
| 23 | # Resistance of the motor, divided by 2 to account for the 2 motors |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 24 | self.resistance = 12.0 / self.stall_current |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 25 | # Motor velocity constant |
| 26 | self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) / |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 27 | (12.0 - self.resistance * self.free_current)) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 28 | # Torque constant |
| 29 | self.Kt = self.stall_torque / self.stall_current |
| 30 | # Control loop time step |
Austin Schuh | adf2cde | 2015-11-08 20:35:16 -0800 | [diff] [blame] | 31 | self.dt = 0.005 |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 32 | |
| 33 | # State feedback matrices |
| 34 | self.A_continuous = numpy.matrix( |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 35 | [[-self.Kt / self.Kv / (self.J * self.resistance)]]) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 36 | self.B_continuous = numpy.matrix( |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 37 | [[self.Kt / (self.J * self.resistance)]]) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 38 | self.C = numpy.matrix([[1]]) |
| 39 | self.D = numpy.matrix([[0]]) |
| 40 | |
| 41 | self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, |
| 42 | self.B_continuous, self.dt) |
| 43 | |
| 44 | self.PlaceControllerPoles([0.01]) |
| 45 | self.PlaceObserverPoles([0.01]) |
| 46 | |
| 47 | self.U_max = numpy.matrix([[12.0]]) |
| 48 | self.U_min = numpy.matrix([[-12.0]]) |
| 49 | |
| 50 | self.InitializeState() |
| 51 | |
| 52 | |
| 53 | class Drivetrain(control_loop.ControlLoop): |
| 54 | def __init__(self, name="Drivetrain", left_low=True, right_low=True): |
| 55 | super(Drivetrain, self).__init__(name) |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 56 | # Number of motors per side |
| 57 | self.num_motors = 2 |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 58 | # Stall Torque in N m |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 59 | self.stall_torque = 2.42 * self.num_motors |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 60 | # Stall Current in Amps |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 61 | self.stall_current = 133.0 * self.num_motors |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 62 | # Free Speed in RPM. Used number from last year. |
| 63 | self.free_speed = 4650.0 |
| 64 | # Free Current in Amps |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 65 | self.free_current = 2.7 * self.num_motors |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 66 | # Moment of inertia of the drivetrain in kg m^2 |
| 67 | # Just borrowed from last year. |
| 68 | self.J = 4.5 |
| 69 | # Mass of the robot, in kg. |
| 70 | self.m = 68 |
| 71 | # Radius of the robot, in meters (from last year). |
| 72 | self.rb = 0.617998644 / 2.0 |
| 73 | # Radius of the wheels, in meters. |
| 74 | self.r = .04445 |
| 75 | # Resistance of the motor, divided by the number of motors. |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 76 | self.resistance = 12.0 / self.stall_current |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 77 | # Motor velocity constant |
| 78 | self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) / |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 79 | (12.0 - self.resistance * self.free_current)) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 80 | # Torque constant |
| 81 | self.Kt = self.stall_torque / self.stall_current |
| 82 | # Gear ratios |
| 83 | self.G_low = 18.0 / 60.0 * 18.0 / 50.0 |
| 84 | self.G_high = 28.0 / 50.0 * 18.0 / 50.0 |
| 85 | if left_low: |
| 86 | self.Gl = self.G_low |
| 87 | else: |
| 88 | self.Gl = self.G_high |
| 89 | if right_low: |
| 90 | self.Gr = self.G_low |
| 91 | else: |
| 92 | self.Gr = self.G_high |
| 93 | |
| 94 | # Control loop time step |
Austin Schuh | adf2cde | 2015-11-08 20:35:16 -0800 | [diff] [blame] | 95 | self.dt = 0.005 |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 96 | |
| 97 | # These describe the way that a given side of a robot will be influenced |
| 98 | # by the other side. Units of 1 / kg. |
| 99 | self.msp = 1.0 / self.m + self.rb * self.rb / self.J |
| 100 | self.msn = 1.0 / self.m - self.rb * self.rb / self.J |
| 101 | # The calculations which we will need for A and B. |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 102 | self.tcl = -self.Kt / self.Kv / (self.Gl * self.Gl * self.resistance * self.r * self.r) |
| 103 | self.tcr = -self.Kt / self.Kv / (self.Gr * self.Gr * self.resistance * self.r * self.r) |
| 104 | self.mpl = self.Kt / (self.Gl * self.resistance * self.r) |
| 105 | self.mpr = self.Kt / (self.Gr * self.resistance * self.r) |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 106 | |
| 107 | # State feedback matrices |
| 108 | # X will be of the format |
| 109 | # [[positionl], [velocityl], [positionr], velocityr]] |
| 110 | self.A_continuous = numpy.matrix( |
| 111 | [[0, 1, 0, 0], |
| 112 | [0, self.msp * self.tcl, 0, self.msn * self.tcr], |
| 113 | [0, 0, 0, 1], |
| 114 | [0, self.msn * self.tcl, 0, self.msp * self.tcr]]) |
| 115 | self.B_continuous = numpy.matrix( |
| 116 | [[0, 0], |
| 117 | [self.msp * self.mpl, self.msn * self.mpr], |
| 118 | [0, 0], |
| 119 | [self.msn * self.mpl, self.msp * self.mpr]]) |
| 120 | self.C = numpy.matrix([[1, 0, 0, 0], |
| 121 | [0, 0, 1, 0]]) |
| 122 | self.D = numpy.matrix([[0, 0], |
| 123 | [0, 0]]) |
| 124 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 125 | self.A, self.B = self.ContinuousToDiscrete( |
| 126 | self.A_continuous, self.B_continuous, self.dt) |
| 127 | |
| 128 | # Poles from last year. |
| 129 | self.hp = 0.65 |
| 130 | self.lp = 0.83 |
| 131 | self.PlaceControllerPoles([self.hp, self.lp, self.hp, self.lp]) |
Austin Schuh | 572ff40 | 2015-11-08 12:17:50 -0800 | [diff] [blame] | 132 | #print self.K |
Austin Schuh | adf2cde | 2015-11-08 20:35:16 -0800 | [diff] [blame] | 133 | q_pos = 0.12 |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 134 | q_vel = 1.0 |
| 135 | self.Q = numpy.matrix([[(1.0 / (q_pos ** 2.0)), 0.0, 0.0, 0.0], |
| 136 | [0.0, (1.0 / (q_vel ** 2.0)), 0.0, 0.0], |
| 137 | [0.0, 0.0, (1.0 / (q_pos ** 2.0)), 0.0], |
| 138 | [0.0, 0.0, 0.0, (1.0 / (q_vel ** 2.0))]]) |
| 139 | |
| 140 | self.R = numpy.matrix([[(1.0 / (12.0 ** 2.0)), 0.0], |
| 141 | [0.0, (1.0 / (12.0 ** 2.0))]]) |
| 142 | self.K = controls.dlqr(self.A, self.B, self.Q, self.R) |
Austin Schuh | 572ff40 | 2015-11-08 12:17:50 -0800 | [diff] [blame] | 143 | #print self.A |
| 144 | #print self.B |
Austin Schuh | adf2cde | 2015-11-08 20:35:16 -0800 | [diff] [blame] | 145 | print "DT K", name |
| 146 | print self.K |
| 147 | print numpy.linalg.eig(self.A - self.B * self.K)[0] |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 148 | |
| 149 | self.hlp = 0.3 |
| 150 | self.llp = 0.4 |
| 151 | self.PlaceObserverPoles([self.hlp, self.hlp, self.llp, self.llp]) |
| 152 | |
| 153 | self.U_max = numpy.matrix([[12.0], [12.0]]) |
| 154 | self.U_min = numpy.matrix([[-12.0], [-12.0]]) |
| 155 | self.InitializeState() |
| 156 | |
Austin Schuh | 572ff40 | 2015-11-08 12:17:50 -0800 | [diff] [blame] | 157 | |
| 158 | class KFDrivetrain(Drivetrain): |
| 159 | def __init__(self, name="KFDrivetrain", left_low=True, right_low=True): |
| 160 | super(KFDrivetrain, self).__init__(name, left_low, right_low) |
| 161 | |
| 162 | self.unaugmented_A_continuous = self.A_continuous |
| 163 | self.unaugmented_B_continuous = self.B_continuous |
| 164 | |
| 165 | # The states are |
| 166 | # The practical voltage applied to the wheels is |
| 167 | # V_left = U_left + left_voltage_error |
| 168 | # |
| 169 | # [left position, left velocity, right position, right velocity, |
| 170 | # left voltage error, right voltage error, angular_error] |
| 171 | self.A_continuous = numpy.matrix(numpy.zeros((7, 7))) |
| 172 | self.B_continuous = numpy.matrix(numpy.zeros((7, 2))) |
| 173 | self.A_continuous[0:4,0:4] = self.unaugmented_A_continuous |
| 174 | self.A_continuous[0:4,4:6] = self.unaugmented_B_continuous |
| 175 | self.B_continuous[0:4,0:2] = self.unaugmented_B_continuous |
| 176 | |
| 177 | self.A, self.B = self.ContinuousToDiscrete( |
| 178 | self.A_continuous, self.B_continuous, self.dt) |
| 179 | |
| 180 | self.C = numpy.matrix([[1, 0, 0, 0, 0, 0, self.rb], |
| 181 | [0, 0, 1, 0, 0, 0, -self.rb], |
| 182 | [0, -2.0 / self.rb, 0, 2.0 / self.rb, 0, 0, 0]]) |
| 183 | |
| 184 | self.D = numpy.matrix([[0, 0], |
| 185 | [0, 0], |
| 186 | [0, 0]]) |
| 187 | |
| 188 | q_pos = 0.08 |
| 189 | q_vel = 0.40 |
| 190 | q_voltage = 6.0 |
| 191 | q_gyro = 0.1 |
| 192 | |
| 193 | self.Q = numpy.matrix([[(q_pos ** 2.0), 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], |
| 194 | [0.0, (q_vel ** 2.0), 0.0, 0.0, 0.0, 0.0, 0.0], |
| 195 | [0.0, 0.0, (q_pos ** 2.0), 0.0, 0.0, 0.0, 0.0], |
| 196 | [0.0, 0.0, 0.0, (q_vel ** 2.0), 0.0, 0.0, 0.0], |
| 197 | [0.0, 0.0, 0.0, 0.0, (q_voltage ** 2.0), 0.0, 0.0], |
| 198 | [0.0, 0.0, 0.0, 0.0, 0.0, (q_voltage ** 2.0), 0.0], |
| 199 | [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, (q_gyro ** 2.0)]]) |
| 200 | |
| 201 | r_pos = 0.05 |
| 202 | r_gyro = 0.001 |
| 203 | self.R = numpy.matrix([[(r_pos ** 2.0), 0.0, 0.0], |
| 204 | [0.0, (r_pos ** 2.0), 0.0], |
| 205 | [0.0, 0.0, (r_gyro ** 2.0)]]) |
| 206 | |
| 207 | # Solving for kf gains. |
| 208 | self.KalmanGain, self.Q_steady = controls.kalman( |
| 209 | A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R) |
| 210 | |
| 211 | self.L = self.A * self.KalmanGain |
| 212 | |
| 213 | # We need a nothing controller for the autogen code to be happy. |
| 214 | self.K = numpy.matrix(numpy.zeros((self.B.shape[1], self.A.shape[0]))) |
| 215 | |
| 216 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 217 | def main(argv): |
| 218 | # Simulate the response of the system to a step input. |
| 219 | drivetrain = Drivetrain() |
| 220 | simulated_left = [] |
| 221 | simulated_right = [] |
| 222 | for _ in xrange(100): |
| 223 | drivetrain.Update(numpy.matrix([[12.0], [12.0]])) |
| 224 | simulated_left.append(drivetrain.X[0, 0]) |
| 225 | simulated_right.append(drivetrain.X[2, 0]) |
| 226 | |
| 227 | #pylab.plot(range(100), simulated_left) |
| 228 | #pylab.plot(range(100), simulated_right) |
| 229 | #pylab.show() |
| 230 | |
| 231 | # Simulate forwards motion. |
| 232 | drivetrain = Drivetrain() |
| 233 | close_loop_left = [] |
| 234 | close_loop_right = [] |
| 235 | R = numpy.matrix([[1.0], [0.0], [1.0], [0.0]]) |
| 236 | for _ in xrange(100): |
| 237 | U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), |
| 238 | drivetrain.U_min, drivetrain.U_max) |
| 239 | drivetrain.UpdateObserver(U) |
| 240 | drivetrain.Update(U) |
| 241 | close_loop_left.append(drivetrain.X[0, 0]) |
| 242 | close_loop_right.append(drivetrain.X[2, 0]) |
| 243 | |
| 244 | #pylab.plot(range(100), close_loop_left) |
| 245 | #pylab.plot(range(100), close_loop_right) |
| 246 | #pylab.show() |
| 247 | |
| 248 | # Try turning in place |
| 249 | drivetrain = Drivetrain() |
| 250 | close_loop_left = [] |
| 251 | close_loop_right = [] |
| 252 | R = numpy.matrix([[-1.0], [0.0], [1.0], [0.0]]) |
| 253 | for _ in xrange(100): |
| 254 | U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), |
| 255 | drivetrain.U_min, drivetrain.U_max) |
| 256 | drivetrain.UpdateObserver(U) |
| 257 | drivetrain.Update(U) |
| 258 | close_loop_left.append(drivetrain.X[0, 0]) |
| 259 | close_loop_right.append(drivetrain.X[2, 0]) |
| 260 | |
| 261 | #pylab.plot(range(100), close_loop_left) |
| 262 | #pylab.plot(range(100), close_loop_right) |
| 263 | #pylab.show() |
| 264 | |
| 265 | # Try turning just one side. |
| 266 | drivetrain = Drivetrain() |
| 267 | close_loop_left = [] |
| 268 | close_loop_right = [] |
| 269 | R = numpy.matrix([[0.0], [0.0], [1.0], [0.0]]) |
| 270 | for _ in xrange(100): |
| 271 | U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), |
| 272 | drivetrain.U_min, drivetrain.U_max) |
| 273 | drivetrain.UpdateObserver(U) |
| 274 | drivetrain.Update(U) |
| 275 | close_loop_left.append(drivetrain.X[0, 0]) |
| 276 | close_loop_right.append(drivetrain.X[2, 0]) |
| 277 | |
| 278 | #pylab.plot(range(100), close_loop_left) |
| 279 | #pylab.plot(range(100), close_loop_right) |
| 280 | #pylab.show() |
| 281 | |
| 282 | # Write the generated constants out to a file. |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 283 | drivetrain_low_low = Drivetrain(name="DrivetrainLowLow", left_low=True, right_low=True) |
| 284 | drivetrain_low_high = Drivetrain(name="DrivetrainLowHigh", left_low=True, right_low=False) |
| 285 | drivetrain_high_low = Drivetrain(name="DrivetrainHighLow", left_low=False, right_low=True) |
| 286 | drivetrain_high_high = Drivetrain(name="DrivetrainHighHigh", left_low=False, right_low=False) |
| 287 | |
Austin Schuh | 572ff40 | 2015-11-08 12:17:50 -0800 | [diff] [blame] | 288 | kf_drivetrain_low_low = KFDrivetrain(name="KFDrivetrainLowLow", left_low=True, right_low=True) |
| 289 | kf_drivetrain_low_high = KFDrivetrain(name="KFDrivetrainLowHigh", left_low=True, right_low=False) |
| 290 | kf_drivetrain_high_low = KFDrivetrain(name="KFDrivetrainHighLow", left_low=False, right_low=True) |
| 291 | kf_drivetrain_high_high = KFDrivetrain(name="KFDrivetrainHighHigh", left_low=False, right_low=False) |
| 292 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 293 | if len(argv) != 5: |
| 294 | print "Expected .h file name and .cc file name" |
| 295 | else: |
Austin Schuh | 572ff40 | 2015-11-08 12:17:50 -0800 | [diff] [blame] | 296 | namespaces = ['y2014', 'control_loops', 'drivetrain'] |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 297 | dog_loop_writer = control_loop.ControlLoopWriter( |
| 298 | "Drivetrain", [drivetrain_low_low, drivetrain_low_high, |
Austin Schuh | 572ff40 | 2015-11-08 12:17:50 -0800 | [diff] [blame] | 299 | drivetrain_high_low, drivetrain_high_high], |
| 300 | namespaces = namespaces) |
Austin Schuh | 0e99773 | 2015-11-08 15:14:53 -0800 | [diff] [blame] | 301 | dog_loop_writer.AddConstant(control_loop.Constant("kDt", "%f", |
| 302 | drivetrain_low_low.dt)) |
Austin Schuh | 96ce8ae | 2015-11-26 12:46:02 -0800 | [diff] [blame] | 303 | dog_loop_writer.AddConstant(control_loop.Constant("kStallTorque", "%f", |
| 304 | drivetrain_low_low.stall_torque)) |
| 305 | dog_loop_writer.AddConstant(control_loop.Constant("kStallCurrent", "%f", |
| 306 | drivetrain_low_low.stall_current)) |
| 307 | dog_loop_writer.AddConstant(control_loop.Constant("kFreeSpeedRPM", "%f", |
| 308 | drivetrain_low_low.free_speed)) |
| 309 | dog_loop_writer.AddConstant(control_loop.Constant("kFreeCurrent", "%f", |
| 310 | drivetrain_low_low.free_current)) |
| 311 | dog_loop_writer.AddConstant(control_loop.Constant("kJ", "%f", |
| 312 | drivetrain_low_low.J)) |
| 313 | dog_loop_writer.AddConstant(control_loop.Constant("kMass", "%f", |
| 314 | drivetrain_low_low.m)) |
| 315 | dog_loop_writer.AddConstant(control_loop.Constant("kRobotRadius", "%f", |
| 316 | drivetrain_low_low.rb)) |
| 317 | dog_loop_writer.AddConstant(control_loop.Constant("kWheelRadius", "%f", |
| 318 | drivetrain_low_low.r)) |
| 319 | dog_loop_writer.AddConstant(control_loop.Constant("kR", "%f", |
| 320 | drivetrain_low_low.resistance)) |
| 321 | dog_loop_writer.AddConstant(control_loop.Constant("kV", "%f", |
| 322 | drivetrain_low_low.Kv)) |
| 323 | dog_loop_writer.AddConstant(control_loop.Constant("kT", "%f", |
| 324 | drivetrain_low_low.Kt)) |
| 325 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 326 | if argv[1][-3:] == '.cc': |
| 327 | dog_loop_writer.Write(argv[2], argv[1]) |
| 328 | else: |
| 329 | dog_loop_writer.Write(argv[1], argv[2]) |
| 330 | |
Austin Schuh | 572ff40 | 2015-11-08 12:17:50 -0800 | [diff] [blame] | 331 | kf_loop_writer = control_loop.ControlLoopWriter( |
| 332 | "KFDrivetrain", [kf_drivetrain_low_low, kf_drivetrain_low_high, |
| 333 | kf_drivetrain_high_low, kf_drivetrain_high_high], |
| 334 | namespaces = namespaces) |
| 335 | if argv[3][-3:] == '.cc': |
| 336 | kf_loop_writer.Write(argv[4], argv[3]) |
| 337 | else: |
| 338 | kf_loop_writer.Write(argv[3], argv[4]) |
| 339 | |
Brian Silverman | 17f503e | 2015-08-02 18:17:18 -0700 | [diff] [blame] | 340 | if __name__ == '__main__': |
| 341 | sys.exit(main(sys.argv)) |