James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 3 | from frc971.control_loops.python import control_loop |
| 4 | from frc971.control_loops.python import controls |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 5 | import numpy |
| 6 | import sys |
| 7 | from matplotlib import pylab |
| 8 | |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 9 | import gflags |
| 10 | import glog |
| 11 | |
| 12 | FLAGS = gflags.FLAGS |
| 13 | |
| 14 | gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.') |
Austin Schuh | 8afe35a | 2013-10-27 10:59:15 -0700 | [diff] [blame] | 15 | |
| 16 | class CIM(control_loop.ControlLoop): |
| 17 | def __init__(self): |
| 18 | super(CIM, self).__init__("CIM") |
| 19 | # Stall Torque in N m |
| 20 | self.stall_torque = 2.42 |
| 21 | # Stall Current in Amps |
| 22 | self.stall_current = 133 |
| 23 | # Free Speed in RPM |
| 24 | self.free_speed = 4650.0 |
| 25 | # Free Current in Amps |
| 26 | self.free_current = 2.7 |
| 27 | # Moment of inertia of the CIM in kg m^2 |
| 28 | self.J = 0.0001 |
| 29 | # Resistance of the motor, divided by 2 to account for the 2 motors |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 30 | self.resistance = 12.0 / self.stall_current |
Austin Schuh | 8afe35a | 2013-10-27 10:59:15 -0700 | [diff] [blame] | 31 | # Motor velocity constant |
| 32 | self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) / |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 33 | (12.0 - self.resistance * self.free_current)) |
Austin Schuh | 8afe35a | 2013-10-27 10:59:15 -0700 | [diff] [blame] | 34 | # Torque constant |
| 35 | self.Kt = self.stall_torque / self.stall_current |
| 36 | # Control loop time step |
Comran Morshed | 1c91be4 | 2015-02-11 21:16:01 +0000 | [diff] [blame] | 37 | self.dt = 0.005 |
Austin Schuh | 8afe35a | 2013-10-27 10:59:15 -0700 | [diff] [blame] | 38 | |
| 39 | # State feedback matrices |
| 40 | self.A_continuous = numpy.matrix( |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 41 | [[-self.Kt / self.Kv / (self.J * self.resistance)]]) |
Austin Schuh | 8afe35a | 2013-10-27 10:59:15 -0700 | [diff] [blame] | 42 | self.B_continuous = numpy.matrix( |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 43 | [[self.Kt / (self.J * self.resistance)]]) |
Austin Schuh | 8afe35a | 2013-10-27 10:59:15 -0700 | [diff] [blame] | 44 | self.C = numpy.matrix([[1]]) |
| 45 | self.D = numpy.matrix([[0]]) |
| 46 | |
| 47 | self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, |
| 48 | self.B_continuous, self.dt) |
| 49 | |
| 50 | self.PlaceControllerPoles([0.01]) |
Austin Schuh | 427b370 | 2013-11-02 13:44:09 -0700 | [diff] [blame] | 51 | self.PlaceObserverPoles([0.01]) |
Austin Schuh | 8afe35a | 2013-10-27 10:59:15 -0700 | [diff] [blame] | 52 | |
| 53 | self.U_max = numpy.matrix([[12.0]]) |
| 54 | self.U_min = numpy.matrix([[-12.0]]) |
| 55 | |
| 56 | self.InitializeState() |
| 57 | |
| 58 | |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 59 | class Drivetrain(control_loop.ControlLoop): |
Brian Silverman | 176303a | 2014-04-10 10:54:55 -0700 | [diff] [blame] | 60 | def __init__(self, name="Drivetrain", left_low=True, right_low=True): |
| 61 | super(Drivetrain, self).__init__(name) |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 62 | # Number of motors per side |
| 63 | self.num_motors = 1 |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 64 | # Stall Torque in N m |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 65 | self.stall_torque = 2.42 * self.num_motors * 0.60 |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 66 | # Stall Current in Amps |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 67 | self.stall_current = 133.0 * self.num_motors |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 68 | # Free Speed in RPM. Used number from last year. |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 69 | self.free_speed = 5500.0 |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 70 | # Free Current in Amps |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 71 | self.free_current = 4.7 * self.num_motors |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 72 | # Moment of inertia of the drivetrain in kg m^2 |
| 73 | # Just borrowed from last year. |
Brian Silverman | 4b36e2e | 2015-03-16 15:46:53 -0700 | [diff] [blame] | 74 | self.J = 10 |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 75 | # Mass of the robot, in kg. |
| 76 | self.m = 68 |
| 77 | # Radius of the robot, in meters (from last year). |
Austin Schuh | 0b69d0c | 2015-03-15 16:38:45 -0700 | [diff] [blame] | 78 | self.rb = 0.9603 / 2.0 |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 79 | # Radius of the wheels, in meters. |
Comran Morshed | 1c91be4 | 2015-02-11 21:16:01 +0000 | [diff] [blame] | 80 | self.r = .0515938 |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 81 | # Resistance of the motor, divided by the number of motors. |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 82 | self.resistance = 12.0 / self.stall_current |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 83 | # Motor velocity constant |
| 84 | self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) / |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 85 | (12.0 - self.resistance * self.free_current)) |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 86 | # Torque constant |
| 87 | self.Kt = self.stall_torque / self.stall_current |
| 88 | # Gear ratios |
Comran Morshed | 1c91be4 | 2015-02-11 21:16:01 +0000 | [diff] [blame] | 89 | self.G_const = 28.0 / 50.0 * 20.0 / 64.0 |
| 90 | |
| 91 | self.G_low = self.G_const |
| 92 | self.G_high = self.G_const |
| 93 | |
Austin Schuh | de4d7fe | 2013-10-08 22:22:45 -0700 | [diff] [blame] | 94 | if left_low: |
| 95 | self.Gl = self.G_low |
| 96 | else: |
| 97 | self.Gl = self.G_high |
| 98 | if right_low: |
| 99 | self.Gr = self.G_low |
| 100 | else: |
| 101 | self.Gr = self.G_high |
Comran Morshed | 1c91be4 | 2015-02-11 21:16:01 +0000 | [diff] [blame] | 102 | |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 103 | # Control loop time step |
Comran Morshed | 1c91be4 | 2015-02-11 21:16:01 +0000 | [diff] [blame] | 104 | self.dt = 0.005 |
| 105 | |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 106 | # These describe the way that a given side of a robot will be influenced |
| 107 | # by the other side. Units of 1 / kg. |
| 108 | self.msp = 1.0 / self.m + self.rb * self.rb / self.J |
| 109 | self.msn = 1.0 / self.m - self.rb * self.rb / self.J |
| 110 | # The calculations which we will need for A and B. |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 111 | self.tcl = -self.Kt / self.Kv / (self.Gl * self.Gl * self.resistance * self.r * self.r) |
| 112 | self.tcr = -self.Kt / self.Kv / (self.Gr * self.Gr * self.resistance * self.r * self.r) |
| 113 | self.mpl = self.Kt / (self.Gl * self.resistance * self.r) |
| 114 | self.mpr = self.Kt / (self.Gr * self.resistance * self.r) |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 115 | |
| 116 | # State feedback matrices |
| 117 | # X will be of the format |
Austin Schuh | de4d7fe | 2013-10-08 22:22:45 -0700 | [diff] [blame] | 118 | # [[positionl], [velocityl], [positionr], velocityr]] |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 119 | self.A_continuous = numpy.matrix( |
| 120 | [[0, 1, 0, 0], |
Austin Schuh | de4d7fe | 2013-10-08 22:22:45 -0700 | [diff] [blame] | 121 | [0, self.msp * self.tcl, 0, self.msn * self.tcr], |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 122 | [0, 0, 0, 1], |
Austin Schuh | de4d7fe | 2013-10-08 22:22:45 -0700 | [diff] [blame] | 123 | [0, self.msn * self.tcl, 0, self.msp * self.tcr]]) |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 124 | self.B_continuous = numpy.matrix( |
| 125 | [[0, 0], |
Austin Schuh | de4d7fe | 2013-10-08 22:22:45 -0700 | [diff] [blame] | 126 | [self.msp * self.mpl, self.msn * self.mpr], |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 127 | [0, 0], |
Austin Schuh | de4d7fe | 2013-10-08 22:22:45 -0700 | [diff] [blame] | 128 | [self.msn * self.mpl, self.msp * self.mpr]]) |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 129 | self.C = numpy.matrix([[1, 0, 0, 0], |
| 130 | [0, 0, 1, 0]]) |
| 131 | self.D = numpy.matrix([[0, 0], |
| 132 | [0, 0]]) |
| 133 | |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 134 | self.A, self.B = self.ContinuousToDiscrete( |
| 135 | self.A_continuous, self.B_continuous, self.dt) |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 136 | |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 137 | if left_low or right_low: |
| 138 | q_pos = 0.12 |
| 139 | q_vel = 1.0 |
| 140 | else: |
| 141 | q_pos = 0.14 |
| 142 | q_vel = 0.95 |
| 143 | |
| 144 | # Tune the LQR controller |
Austin Schuh | a25a041 | 2014-03-09 00:50:04 -0800 | [diff] [blame] | 145 | self.Q = numpy.matrix([[(1.0 / (q_pos ** 2.0)), 0.0, 0.0, 0.0], |
| 146 | [0.0, (1.0 / (q_vel ** 2.0)), 0.0, 0.0], |
| 147 | [0.0, 0.0, (1.0 / (q_pos ** 2.0)), 0.0], |
| 148 | [0.0, 0.0, 0.0, (1.0 / (q_vel ** 2.0))]]) |
| 149 | |
| 150 | self.R = numpy.matrix([[(1.0 / (12.0 ** 2.0)), 0.0], |
| 151 | [0.0, (1.0 / (12.0 ** 2.0))]]) |
| 152 | self.K = controls.dlqr(self.A, self.B, self.Q, self.R) |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 153 | |
| 154 | glog.debug('DT q_pos %f q_vel %s %s', q_pos, q_vel, name) |
| 155 | glog.debug(str(numpy.linalg.eig(self.A - self.B * self.K)[0])) |
| 156 | glog.debug('K %s', repr(self.K)) |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 157 | |
Brian Silverman | 38ea9bf | 2014-04-19 22:57:54 -0700 | [diff] [blame] | 158 | self.hlp = 0.3 |
| 159 | self.llp = 0.4 |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 160 | self.PlaceObserverPoles([self.hlp, self.hlp, self.llp, self.llp]) |
| 161 | |
| 162 | self.U_max = numpy.matrix([[12.0], [12.0]]) |
| 163 | self.U_min = numpy.matrix([[-12.0], [-12.0]]) |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 164 | |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 165 | self.InitializeState() |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 166 | |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 167 | |
| 168 | class KFDrivetrain(Drivetrain): |
| 169 | def __init__(self, name="KFDrivetrain", left_low=True, right_low=True): |
| 170 | super(KFDrivetrain, self).__init__(name, left_low, right_low) |
| 171 | |
| 172 | self.unaugmented_A_continuous = self.A_continuous |
| 173 | self.unaugmented_B_continuous = self.B_continuous |
| 174 | |
| 175 | # The practical voltage applied to the wheels is |
| 176 | # V_left = U_left + left_voltage_error |
| 177 | # |
| 178 | # The states are |
| 179 | # [left position, left velocity, right position, right velocity, |
| 180 | # left voltage error, right voltage error, angular_error] |
| 181 | # |
| 182 | # The left and right positions are filtered encoder positions and are not |
| 183 | # adjusted for heading error. |
| 184 | # The turn velocity as computed by the left and right velocities is |
| 185 | # adjusted by the gyro velocity. |
| 186 | # The angular_error is the angular velocity error between the wheel speed |
| 187 | # and the gyro speed. |
| 188 | self.A_continuous = numpy.matrix(numpy.zeros((7, 7))) |
| 189 | self.B_continuous = numpy.matrix(numpy.zeros((7, 2))) |
| 190 | self.A_continuous[0:4,0:4] = self.unaugmented_A_continuous |
| 191 | self.A_continuous[0:4,4:6] = self.unaugmented_B_continuous |
| 192 | self.B_continuous[0:4,0:2] = self.unaugmented_B_continuous |
| 193 | self.A_continuous[0,6] = 1 |
| 194 | self.A_continuous[2,6] = -1 |
| 195 | |
| 196 | self.A, self.B = self.ContinuousToDiscrete( |
| 197 | self.A_continuous, self.B_continuous, self.dt) |
| 198 | |
| 199 | self.C = numpy.matrix([[1, 0, 0, 0, 0, 0, 0], |
| 200 | [0, 0, 1, 0, 0, 0, 0], |
| 201 | [0, -0.5 / self.rb, 0, 0.5 / self.rb, 0, 0, 0]]) |
| 202 | |
| 203 | self.D = numpy.matrix([[0, 0], |
| 204 | [0, 0], |
| 205 | [0, 0]]) |
| 206 | |
| 207 | q_pos = 0.05 |
| 208 | q_vel = 1.00 |
| 209 | q_voltage = 10.0 |
| 210 | q_encoder_uncertainty = 2.00 |
| 211 | |
| 212 | self.Q = numpy.matrix([[(q_pos ** 2.0), 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], |
| 213 | [0.0, (q_vel ** 2.0), 0.0, 0.0, 0.0, 0.0, 0.0], |
| 214 | [0.0, 0.0, (q_pos ** 2.0), 0.0, 0.0, 0.0, 0.0], |
| 215 | [0.0, 0.0, 0.0, (q_vel ** 2.0), 0.0, 0.0, 0.0], |
| 216 | [0.0, 0.0, 0.0, 0.0, (q_voltage ** 2.0), 0.0, 0.0], |
| 217 | [0.0, 0.0, 0.0, 0.0, 0.0, (q_voltage ** 2.0), 0.0], |
| 218 | [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, (q_encoder_uncertainty ** 2.0)]]) |
| 219 | |
| 220 | r_pos = 0.0001 |
| 221 | r_gyro = 0.000001 |
| 222 | self.R = numpy.matrix([[(r_pos ** 2.0), 0.0, 0.0], |
| 223 | [0.0, (r_pos ** 2.0), 0.0], |
| 224 | [0.0, 0.0, (r_gyro ** 2.0)]]) |
| 225 | |
| 226 | # Solving for kf gains. |
| 227 | self.KalmanGain, self.Q_steady = controls.kalman( |
| 228 | A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R) |
| 229 | |
| 230 | self.L = self.A * self.KalmanGain |
| 231 | |
| 232 | unaug_K = self.K |
| 233 | |
| 234 | # Implement a nice closed loop controller for use by the closed loop |
| 235 | # controller. |
| 236 | self.K = numpy.matrix(numpy.zeros((self.B.shape[1], self.A.shape[0]))) |
| 237 | self.K[0:2, 0:4] = unaug_K |
| 238 | self.K[0, 4] = 1.0 |
| 239 | self.K[1, 5] = 1.0 |
| 240 | |
| 241 | self.Qff = numpy.matrix(numpy.zeros((4, 4))) |
| 242 | qff_pos = 0.005 |
| 243 | qff_vel = 1.00 |
| 244 | self.Qff[0, 0] = 1.0 / qff_pos ** 2.0 |
| 245 | self.Qff[1, 1] = 1.0 / qff_vel ** 2.0 |
| 246 | self.Qff[2, 2] = 1.0 / qff_pos ** 2.0 |
| 247 | self.Qff[3, 3] = 1.0 / qff_vel ** 2.0 |
| 248 | self.Kff = numpy.matrix(numpy.zeros((2, 7))) |
| 249 | self.Kff[0:2, 0:4] = controls.TwoStateFeedForwards(self.B[0:4,:], self.Qff) |
| 250 | |
| 251 | self.InitializeState() |
| 252 | |
| 253 | |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 254 | def main(argv): |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 255 | argv = FLAGS(argv) |
| 256 | glog.init() |
| 257 | |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 258 | # Simulate the response of the system to a step input. |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 259 | drivetrain = Drivetrain(left_low=False, right_low=False) |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 260 | simulated_left = [] |
| 261 | simulated_right = [] |
| 262 | for _ in xrange(100): |
| 263 | drivetrain.Update(numpy.matrix([[12.0], [12.0]])) |
| 264 | simulated_left.append(drivetrain.X[0, 0]) |
| 265 | simulated_right.append(drivetrain.X[2, 0]) |
| 266 | |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 267 | if FLAGS.plot: |
| 268 | pylab.plot(range(100), simulated_left) |
| 269 | pylab.plot(range(100), simulated_right) |
| 270 | pylab.suptitle('Acceleration Test') |
| 271 | pylab.show() |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 272 | |
| 273 | # Simulate forwards motion. |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 274 | drivetrain = Drivetrain(left_low=False, right_low=False) |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 275 | close_loop_left = [] |
| 276 | close_loop_right = [] |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 277 | left_power = [] |
| 278 | right_power = [] |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 279 | R = numpy.matrix([[1.0], [0.0], [1.0], [0.0]]) |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 280 | for _ in xrange(300): |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 281 | U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), |
| 282 | drivetrain.U_min, drivetrain.U_max) |
| 283 | drivetrain.UpdateObserver(U) |
| 284 | drivetrain.Update(U) |
| 285 | close_loop_left.append(drivetrain.X[0, 0]) |
| 286 | close_loop_right.append(drivetrain.X[2, 0]) |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 287 | left_power.append(U[0, 0]) |
| 288 | right_power.append(U[1, 0]) |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 289 | |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 290 | if FLAGS.plot: |
| 291 | pylab.plot(range(300), close_loop_left, label='left position') |
| 292 | pylab.plot(range(300), close_loop_right, label='right position') |
| 293 | pylab.plot(range(300), left_power, label='left power') |
| 294 | pylab.plot(range(300), right_power, label='right power') |
| 295 | pylab.suptitle('Linear Move') |
| 296 | pylab.legend() |
| 297 | pylab.show() |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 298 | |
| 299 | # Try turning in place |
| 300 | drivetrain = Drivetrain() |
| 301 | close_loop_left = [] |
| 302 | close_loop_right = [] |
| 303 | R = numpy.matrix([[-1.0], [0.0], [1.0], [0.0]]) |
| 304 | for _ in xrange(100): |
| 305 | U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), |
| 306 | drivetrain.U_min, drivetrain.U_max) |
| 307 | drivetrain.UpdateObserver(U) |
| 308 | drivetrain.Update(U) |
| 309 | close_loop_left.append(drivetrain.X[0, 0]) |
| 310 | close_loop_right.append(drivetrain.X[2, 0]) |
| 311 | |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 312 | if FLAGS.plot: |
| 313 | pylab.plot(range(100), close_loop_left) |
| 314 | pylab.plot(range(100), close_loop_right) |
| 315 | pylab.suptitle('Angular Move') |
| 316 | pylab.show() |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 317 | |
| 318 | # Try turning just one side. |
| 319 | drivetrain = Drivetrain() |
| 320 | close_loop_left = [] |
| 321 | close_loop_right = [] |
| 322 | R = numpy.matrix([[0.0], [0.0], [1.0], [0.0]]) |
| 323 | for _ in xrange(100): |
| 324 | U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), |
| 325 | drivetrain.U_min, drivetrain.U_max) |
| 326 | drivetrain.UpdateObserver(U) |
| 327 | drivetrain.Update(U) |
| 328 | close_loop_left.append(drivetrain.X[0, 0]) |
| 329 | close_loop_right.append(drivetrain.X[2, 0]) |
| 330 | |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 331 | if FLAGS.plot: |
| 332 | pylab.plot(range(100), close_loop_left) |
| 333 | pylab.plot(range(100), close_loop_right) |
| 334 | pylab.suptitle('Pivot') |
| 335 | pylab.show() |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 336 | |
| 337 | # Write the generated constants out to a file. |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 338 | drivetrain_low_low = Drivetrain( |
| 339 | name="DrivetrainLowLow", left_low=True, right_low=True) |
| 340 | drivetrain_low_high = Drivetrain( |
| 341 | name="DrivetrainLowHigh", left_low=True, right_low=False) |
| 342 | drivetrain_high_low = Drivetrain( |
| 343 | name="DrivetrainHighLow", left_low=False, right_low=True) |
| 344 | drivetrain_high_high = Drivetrain( |
| 345 | name="DrivetrainHighHigh", left_low=False, right_low=False) |
| 346 | |
| 347 | kf_drivetrain_low_low = KFDrivetrain( |
| 348 | name="KFDrivetrainLowLow", left_low=True, right_low=True) |
| 349 | kf_drivetrain_low_high = KFDrivetrain( |
| 350 | name="KFDrivetrainLowHigh", left_low=True, right_low=False) |
| 351 | kf_drivetrain_high_low = KFDrivetrain( |
| 352 | name="KFDrivetrainHighLow", left_low=False, right_low=True) |
| 353 | kf_drivetrain_high_high = KFDrivetrain( |
| 354 | name="KFDrivetrainHighHigh", left_low=False, right_low=False) |
Brian Silverman | 2c590c3 | 2013-11-04 18:08:54 -0800 | [diff] [blame] | 355 | |
| 356 | if len(argv) != 5: |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 357 | print "Expected .h file name and .cc file name" |
| 358 | else: |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 359 | namespaces = ['y2015', 'control_loops', 'drivetrain'] |
Brian Silverman | 176303a | 2014-04-10 10:54:55 -0700 | [diff] [blame] | 360 | dog_loop_writer = control_loop.ControlLoopWriter( |
| 361 | "Drivetrain", [drivetrain_low_low, drivetrain_low_high, |
Austin Schuh | 70810b9 | 2016-11-26 14:55:34 -0800 | [diff] [blame^] | 362 | drivetrain_high_low, drivetrain_high_high], |
| 363 | namespaces = namespaces) |
| 364 | dog_loop_writer.AddConstant(control_loop.Constant("kDt", "%f", |
| 365 | drivetrain_low_low.dt)) |
| 366 | dog_loop_writer.AddConstant(control_loop.Constant("kStallTorque", "%f", |
| 367 | drivetrain_low_low.stall_torque)) |
| 368 | dog_loop_writer.AddConstant(control_loop.Constant("kStallCurrent", "%f", |
| 369 | drivetrain_low_low.stall_current)) |
| 370 | dog_loop_writer.AddConstant(control_loop.Constant("kFreeSpeedRPM", "%f", |
| 371 | drivetrain_low_low.free_speed)) |
| 372 | dog_loop_writer.AddConstant(control_loop.Constant("kFreeCurrent", "%f", |
| 373 | drivetrain_low_low.free_current)) |
| 374 | dog_loop_writer.AddConstant(control_loop.Constant("kJ", "%f", |
| 375 | drivetrain_low_low.J)) |
| 376 | dog_loop_writer.AddConstant(control_loop.Constant("kMass", "%f", |
| 377 | drivetrain_low_low.m)) |
| 378 | dog_loop_writer.AddConstant(control_loop.Constant("kRobotRadius", "%f", |
| 379 | drivetrain_low_low.rb)) |
| 380 | dog_loop_writer.AddConstant(control_loop.Constant("kWheelRadius", "%f", |
| 381 | drivetrain_low_low.r)) |
| 382 | dog_loop_writer.AddConstant(control_loop.Constant("kR", "%f", |
| 383 | drivetrain_low_low.resistance)) |
| 384 | dog_loop_writer.AddConstant(control_loop.Constant("kV", "%f", |
| 385 | drivetrain_low_low.Kv)) |
| 386 | dog_loop_writer.AddConstant(control_loop.Constant("kT", "%f", |
| 387 | drivetrain_low_low.Kt)) |
| 388 | dog_loop_writer.AddConstant(control_loop.Constant("kLowGearRatio", "%f", |
| 389 | drivetrain_low_low.G_low)) |
| 390 | dog_loop_writer.AddConstant(control_loop.Constant("kHighGearRatio", "%f", |
| 391 | drivetrain_high_high.G_high)) |
| 392 | |
| 393 | dog_loop_writer.Write(argv[1], argv[2]) |
| 394 | |
| 395 | kf_loop_writer = control_loop.ControlLoopWriter( |
| 396 | "KFDrivetrain", [kf_drivetrain_low_low, kf_drivetrain_low_high, |
| 397 | kf_drivetrain_high_low, kf_drivetrain_high_high], |
| 398 | namespaces = namespaces) |
| 399 | kf_loop_writer.Write(argv[3], argv[4]) |
Brian Silverman | 2c590c3 | 2013-11-04 18:08:54 -0800 | [diff] [blame] | 400 | |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 401 | if __name__ == '__main__': |
| 402 | sys.exit(main(sys.argv)) |