blob: db8388bd09cd8efa06397fc695b5b8bd35815f9c [file] [log] [blame]
Comran Morshed9a9948c2016-01-16 15:58:04 +00001#!/usr/bin/python
2
3from frc971.control_loops.python import control_loop
4from frc971.control_loops.python import controls
5import numpy
6import sys
Comran Morshed9a9948c2016-01-16 15:58:04 +00007from matplotlib import pylab
8
9import gflags
10import glog
11
12FLAGS = gflags.FLAGS
13
14gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.')
15
16class 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
30 self.resistance = 12.0 / self.stall_current
31 # Motor velocity constant
32 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
33 (12.0 - self.resistance * self.free_current))
34 # Torque constant
35 self.Kt = self.stall_torque / self.stall_current
36 # Control loop time step
37 self.dt = 0.005
38
39 # State feedback matrices
40 self.A_continuous = numpy.matrix(
41 [[-self.Kt / self.Kv / (self.J * self.resistance)]])
42 self.B_continuous = numpy.matrix(
43 [[self.Kt / (self.J * self.resistance)]])
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])
51 self.PlaceObserverPoles([0.01])
52
53 self.U_max = numpy.matrix([[12.0]])
54 self.U_min = numpy.matrix([[-12.0]])
55
56 self.InitializeState()
57
58
59class Drivetrain(control_loop.ControlLoop):
60 def __init__(self, name="Drivetrain", left_low=True, right_low=True):
61 super(Drivetrain, self).__init__(name)
62 # Number of motors per side
63 self.num_motors = 2
64 # Stall Torque in N m
65 self.stall_torque = 2.42 * self.num_motors * 0.60
66 # Stall Current in Amps
67 self.stall_current = 133.0 * self.num_motors
68 # Free Speed in RPM. Used number from last year.
69 self.free_speed = 5500.0
70 # Free Current in Amps
71 self.free_current = 4.7 * self.num_motors
72 # Moment of inertia of the drivetrain in kg m^2
Austin Schuh126924c2016-02-28 21:56:17 -080073 self.J = 2.0
Comran Morshed9a9948c2016-01-16 15:58:04 +000074 # Mass of the robot, in kg.
75 self.m = 68
Comran Morshed001c7c32016-02-15 21:04:55 +000076 # Radius of the robot, in meters (requires tuning by hand)
77 self.rb = 0.601 / 2.0
Comran Morshed9a9948c2016-01-16 15:58:04 +000078 # Radius of the wheels, in meters.
Austin Schuhceb23cc2016-04-20 20:12:18 -070079 self.r = 0.097155 * 0.9811158901447808 / 118.0 * 115.75
Comran Morshed9a9948c2016-01-16 15:58:04 +000080 # Resistance of the motor, divided by the number of motors.
81 self.resistance = 12.0 / self.stall_current
82 # Motor velocity constant
83 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
84 (12.0 - self.resistance * self.free_current))
85 # Torque constant
86 self.Kt = self.stall_torque / self.stall_current
87 # Gear ratios
Comran Morshed001c7c32016-02-15 21:04:55 +000088 self.G_low = 14.0 / 48.0 * 18.0 / 60.0 * 18.0 / 36.0
89 self.G_high = 14.0 / 48.0 * 28.0 / 50.0 * 18.0 / 36.0
Comran Morshed9a9948c2016-01-16 15:58:04 +000090 if left_low:
91 self.Gl = self.G_low
92 else:
93 self.Gl = self.G_high
94 if right_low:
95 self.Gr = self.G_low
96 else:
97 self.Gr = self.G_high
98
99 # Control loop time step
100 self.dt = 0.005
101
102 # These describe the way that a given side of a robot will be influenced
103 # by the other side. Units of 1 / kg.
104 self.msp = 1.0 / self.m + self.rb * self.rb / self.J
105 self.msn = 1.0 / self.m - self.rb * self.rb / self.J
106 # The calculations which we will need for A and B.
107 self.tcl = -self.Kt / self.Kv / (self.Gl * self.Gl * self.resistance * self.r * self.r)
108 self.tcr = -self.Kt / self.Kv / (self.Gr * self.Gr * self.resistance * self.r * self.r)
109 self.mpl = self.Kt / (self.Gl * self.resistance * self.r)
110 self.mpr = self.Kt / (self.Gr * self.resistance * self.r)
111
112 # State feedback matrices
113 # X will be of the format
114 # [[positionl], [velocityl], [positionr], velocityr]]
115 self.A_continuous = numpy.matrix(
116 [[0, 1, 0, 0],
117 [0, self.msp * self.tcl, 0, self.msn * self.tcr],
118 [0, 0, 0, 1],
119 [0, self.msn * self.tcl, 0, self.msp * self.tcr]])
120 self.B_continuous = numpy.matrix(
121 [[0, 0],
122 [self.msp * self.mpl, self.msn * self.mpr],
123 [0, 0],
124 [self.msn * self.mpl, self.msp * self.mpr]])
125 self.C = numpy.matrix([[1, 0, 0, 0],
126 [0, 0, 1, 0]])
127 self.D = numpy.matrix([[0, 0],
128 [0, 0]])
129
130 self.A, self.B = self.ContinuousToDiscrete(
131 self.A_continuous, self.B_continuous, self.dt)
132
Austin Schuh093535c2016-03-05 23:21:00 -0800133 if left_low or right_low:
134 q_pos = 0.12
135 q_vel = 1.0
136 else:
137 q_pos = 0.14
138 q_vel = 0.95
139
Comran Morshed9a9948c2016-01-16 15:58:04 +0000140 self.Q = numpy.matrix([[(1.0 / (q_pos ** 2.0)), 0.0, 0.0, 0.0],
141 [0.0, (1.0 / (q_vel ** 2.0)), 0.0, 0.0],
142 [0.0, 0.0, (1.0 / (q_pos ** 2.0)), 0.0],
143 [0.0, 0.0, 0.0, (1.0 / (q_vel ** 2.0))]])
144
145 self.R = numpy.matrix([[(1.0 / (12.0 ** 2.0)), 0.0],
146 [0.0, (1.0 / (12.0 ** 2.0))]])
147 self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
148
Austin Schuh093535c2016-03-05 23:21:00 -0800149 glog.debug('DT q_pos %f q_vel %s %s', q_pos, q_vel, name)
Comran Morshed9a9948c2016-01-16 15:58:04 +0000150 glog.debug(str(numpy.linalg.eig(self.A - self.B * self.K)[0]))
Austin Schuh093535c2016-03-05 23:21:00 -0800151 glog.debug('K %s', repr(self.K))
Comran Morshed9a9948c2016-01-16 15:58:04 +0000152
153 self.hlp = 0.3
154 self.llp = 0.4
155 self.PlaceObserverPoles([self.hlp, self.hlp, self.llp, self.llp])
156
157 self.U_max = numpy.matrix([[12.0], [12.0]])
158 self.U_min = numpy.matrix([[-12.0], [-12.0]])
Austin Schuh093535c2016-03-05 23:21:00 -0800159
Comran Morshed9a9948c2016-01-16 15:58:04 +0000160 self.InitializeState()
161
162
163class KFDrivetrain(Drivetrain):
164 def __init__(self, name="KFDrivetrain", left_low=True, right_low=True):
165 super(KFDrivetrain, self).__init__(name, left_low, right_low)
166
167 self.unaugmented_A_continuous = self.A_continuous
168 self.unaugmented_B_continuous = self.B_continuous
169
170 # The states are
171 # The practical voltage applied to the wheels is
172 # V_left = U_left + left_voltage_error
173 #
174 # [left position, left velocity, right position, right velocity,
175 # left voltage error, right voltage error, angular_error]
Austin Schuh093535c2016-03-05 23:21:00 -0800176 #
177 # The left and right positions are filtered encoder positions and are not
178 # adjusted for heading error.
179 # The turn velocity as computed by the left and right velocities is
180 # adjusted by the gyro velocity.
181 # The angular_error is the angular velocity error between the wheel speed
182 # and the gyro speed.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000183 self.A_continuous = numpy.matrix(numpy.zeros((7, 7)))
184 self.B_continuous = numpy.matrix(numpy.zeros((7, 2)))
185 self.A_continuous[0:4,0:4] = self.unaugmented_A_continuous
186 self.A_continuous[0:4,4:6] = self.unaugmented_B_continuous
187 self.B_continuous[0:4,0:2] = self.unaugmented_B_continuous
188 self.A_continuous[0,6] = 1
189 self.A_continuous[2,6] = -1
190
191 self.A, self.B = self.ContinuousToDiscrete(
192 self.A_continuous, self.B_continuous, self.dt)
193
194 self.C = numpy.matrix([[1, 0, 0, 0, 0, 0, 0],
195 [0, 0, 1, 0, 0, 0, 0],
196 [0, -0.5 / self.rb, 0, 0.5 / self.rb, 0, 0, 0]])
197
198 self.D = numpy.matrix([[0, 0],
199 [0, 0],
200 [0, 0]])
201
202 q_pos = 0.05
203 q_vel = 1.00
204 q_voltage = 10.0
205 q_encoder_uncertainty = 2.00
206
207 self.Q = numpy.matrix([[(q_pos ** 2.0), 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
208 [0.0, (q_vel ** 2.0), 0.0, 0.0, 0.0, 0.0, 0.0],
209 [0.0, 0.0, (q_pos ** 2.0), 0.0, 0.0, 0.0, 0.0],
210 [0.0, 0.0, 0.0, (q_vel ** 2.0), 0.0, 0.0, 0.0],
211 [0.0, 0.0, 0.0, 0.0, (q_voltage ** 2.0), 0.0, 0.0],
212 [0.0, 0.0, 0.0, 0.0, 0.0, (q_voltage ** 2.0), 0.0],
213 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, (q_encoder_uncertainty ** 2.0)]])
214
215 r_pos = 0.0001
216 r_gyro = 0.000001
217 self.R = numpy.matrix([[(r_pos ** 2.0), 0.0, 0.0],
218 [0.0, (r_pos ** 2.0), 0.0],
219 [0.0, 0.0, (r_gyro ** 2.0)]])
220
221 # Solving for kf gains.
222 self.KalmanGain, self.Q_steady = controls.kalman(
223 A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R)
224
225 self.L = self.A * self.KalmanGain
226
Austin Schuh093535c2016-03-05 23:21:00 -0800227 unaug_K = self.K
228
229 # Implement a nice closed loop controller for use by the closed loop
230 # controller.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000231 self.K = numpy.matrix(numpy.zeros((self.B.shape[1], self.A.shape[0])))
Austin Schuh093535c2016-03-05 23:21:00 -0800232 self.K[0:2, 0:4] = unaug_K
233 self.K[0, 4] = 1.0
234 self.K[1, 5] = 1.0
235
236 self.Qff = numpy.matrix(numpy.zeros((4, 4)))
237 qff_pos = 0.005
238 qff_vel = 1.00
239 self.Qff[0, 0] = 1.0 / qff_pos ** 2.0
240 self.Qff[1, 1] = 1.0 / qff_vel ** 2.0
241 self.Qff[2, 2] = 1.0 / qff_pos ** 2.0
242 self.Qff[3, 3] = 1.0 / qff_vel ** 2.0
243 self.Kff = numpy.matrix(numpy.zeros((2, 7)))
244 self.Kff[0:2, 0:4] = controls.TwoStateFeedForwards(self.B[0:4,:], self.Qff)
245
246 self.InitializeState()
Comran Morshed9a9948c2016-01-16 15:58:04 +0000247
248
249def main(argv):
250 argv = FLAGS(argv)
Austin Schuh289ee4f2016-02-05 00:20:52 -0800251 glog.init()
Comran Morshed9a9948c2016-01-16 15:58:04 +0000252
253 # Simulate the response of the system to a step input.
Austin Schuh093535c2016-03-05 23:21:00 -0800254 drivetrain = Drivetrain(left_low=False, right_low=False)
Comran Morshed9a9948c2016-01-16 15:58:04 +0000255 simulated_left = []
256 simulated_right = []
257 for _ in xrange(100):
258 drivetrain.Update(numpy.matrix([[12.0], [12.0]]))
259 simulated_left.append(drivetrain.X[0, 0])
260 simulated_right.append(drivetrain.X[2, 0])
261
262 if FLAGS.plot:
263 pylab.plot(range(100), simulated_left)
264 pylab.plot(range(100), simulated_right)
Austin Schuh093535c2016-03-05 23:21:00 -0800265 pylab.suptitle('Acceleration Test')
Comran Morshed9a9948c2016-01-16 15:58:04 +0000266 pylab.show()
267
268 # Simulate forwards motion.
Austin Schuh093535c2016-03-05 23:21:00 -0800269 drivetrain = Drivetrain(left_low=False, right_low=False)
Comran Morshed9a9948c2016-01-16 15:58:04 +0000270 close_loop_left = []
271 close_loop_right = []
Austin Schuh093535c2016-03-05 23:21:00 -0800272 left_power = []
273 right_power = []
Comran Morshed9a9948c2016-01-16 15:58:04 +0000274 R = numpy.matrix([[1.0], [0.0], [1.0], [0.0]])
Austin Schuh093535c2016-03-05 23:21:00 -0800275 for _ in xrange(300):
Comran Morshed9a9948c2016-01-16 15:58:04 +0000276 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
277 drivetrain.U_min, drivetrain.U_max)
278 drivetrain.UpdateObserver(U)
279 drivetrain.Update(U)
280 close_loop_left.append(drivetrain.X[0, 0])
281 close_loop_right.append(drivetrain.X[2, 0])
Austin Schuh093535c2016-03-05 23:21:00 -0800282 left_power.append(U[0, 0])
283 right_power.append(U[1, 0])
Comran Morshed9a9948c2016-01-16 15:58:04 +0000284
285 if FLAGS.plot:
Austin Schuh093535c2016-03-05 23:21:00 -0800286 pylab.plot(range(300), close_loop_left, label='left position')
287 pylab.plot(range(300), close_loop_right, label='right position')
288 pylab.plot(range(300), left_power, label='left power')
289 pylab.plot(range(300), right_power, label='right power')
290 pylab.suptitle('Linear Move')
291 pylab.legend()
Comran Morshed9a9948c2016-01-16 15:58:04 +0000292 pylab.show()
293
294 # Try turning in place
295 drivetrain = Drivetrain()
296 close_loop_left = []
297 close_loop_right = []
298 R = numpy.matrix([[-1.0], [0.0], [1.0], [0.0]])
299 for _ in xrange(100):
300 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
301 drivetrain.U_min, drivetrain.U_max)
302 drivetrain.UpdateObserver(U)
303 drivetrain.Update(U)
304 close_loop_left.append(drivetrain.X[0, 0])
305 close_loop_right.append(drivetrain.X[2, 0])
306
307 if FLAGS.plot:
308 pylab.plot(range(100), close_loop_left)
309 pylab.plot(range(100), close_loop_right)
Austin Schuh093535c2016-03-05 23:21:00 -0800310 pylab.suptitle('Angular Move')
Comran Morshed9a9948c2016-01-16 15:58:04 +0000311 pylab.show()
312
313 # Try turning just one side.
314 drivetrain = Drivetrain()
315 close_loop_left = []
316 close_loop_right = []
317 R = numpy.matrix([[0.0], [0.0], [1.0], [0.0]])
318 for _ in xrange(100):
319 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
320 drivetrain.U_min, drivetrain.U_max)
321 drivetrain.UpdateObserver(U)
322 drivetrain.Update(U)
323 close_loop_left.append(drivetrain.X[0, 0])
324 close_loop_right.append(drivetrain.X[2, 0])
325
326 if FLAGS.plot:
327 pylab.plot(range(100), close_loop_left)
328 pylab.plot(range(100), close_loop_right)
Austin Schuh093535c2016-03-05 23:21:00 -0800329 pylab.suptitle('Pivot')
Comran Morshed9a9948c2016-01-16 15:58:04 +0000330 pylab.show()
331
332 # Write the generated constants out to a file.
333 drivetrain_low_low = Drivetrain(
334 name="DrivetrainLowLow", left_low=True, right_low=True)
335 drivetrain_low_high = Drivetrain(
336 name="DrivetrainLowHigh", left_low=True, right_low=False)
337 drivetrain_high_low = Drivetrain(
338 name="DrivetrainHighLow", left_low=False, right_low=True)
339 drivetrain_high_high = Drivetrain(
340 name="DrivetrainHighHigh", left_low=False, right_low=False)
341
342 kf_drivetrain_low_low = KFDrivetrain(
343 name="KFDrivetrainLowLow", left_low=True, right_low=True)
344 kf_drivetrain_low_high = KFDrivetrain(
345 name="KFDrivetrainLowHigh", left_low=True, right_low=False)
346 kf_drivetrain_high_low = KFDrivetrain(
347 name="KFDrivetrainHighLow", left_low=False, right_low=True)
348 kf_drivetrain_high_high = KFDrivetrain(
349 name="KFDrivetrainHighHigh", left_low=False, right_low=False)
350
351 if len(argv) != 5:
352 print "Expected .h file name and .cc file name"
353 else:
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000354 namespaces = ['y2016', 'control_loops', 'drivetrain']
Comran Morshed9a9948c2016-01-16 15:58:04 +0000355 dog_loop_writer = control_loop.ControlLoopWriter(
356 "Drivetrain", [drivetrain_low_low, drivetrain_low_high,
357 drivetrain_high_low, drivetrain_high_high],
358 namespaces = namespaces)
359 dog_loop_writer.AddConstant(control_loop.Constant("kDt", "%f",
360 drivetrain_low_low.dt))
361 dog_loop_writer.AddConstant(control_loop.Constant("kStallTorque", "%f",
362 drivetrain_low_low.stall_torque))
363 dog_loop_writer.AddConstant(control_loop.Constant("kStallCurrent", "%f",
364 drivetrain_low_low.stall_current))
365 dog_loop_writer.AddConstant(control_loop.Constant("kFreeSpeedRPM", "%f",
366 drivetrain_low_low.free_speed))
367 dog_loop_writer.AddConstant(control_loop.Constant("kFreeCurrent", "%f",
368 drivetrain_low_low.free_current))
369 dog_loop_writer.AddConstant(control_loop.Constant("kJ", "%f",
370 drivetrain_low_low.J))
371 dog_loop_writer.AddConstant(control_loop.Constant("kMass", "%f",
372 drivetrain_low_low.m))
373 dog_loop_writer.AddConstant(control_loop.Constant("kRobotRadius", "%f",
374 drivetrain_low_low.rb))
375 dog_loop_writer.AddConstant(control_loop.Constant("kWheelRadius", "%f",
376 drivetrain_low_low.r))
377 dog_loop_writer.AddConstant(control_loop.Constant("kR", "%f",
378 drivetrain_low_low.resistance))
379 dog_loop_writer.AddConstant(control_loop.Constant("kV", "%f",
380 drivetrain_low_low.Kv))
381 dog_loop_writer.AddConstant(control_loop.Constant("kT", "%f",
382 drivetrain_low_low.Kt))
Austin Schuh33bc6842016-02-17 00:38:51 -0800383 dog_loop_writer.AddConstant(control_loop.Constant("kLowGearRatio", "%f",
384 drivetrain_low_low.G_low))
385 dog_loop_writer.AddConstant(control_loop.Constant("kHighGearRatio", "%f",
386 drivetrain_high_high.G_high))
Comran Morshed9a9948c2016-01-16 15:58:04 +0000387
388 dog_loop_writer.Write(argv[1], argv[2])
389
390 kf_loop_writer = control_loop.ControlLoopWriter(
391 "KFDrivetrain", [kf_drivetrain_low_low, kf_drivetrain_low_high,
392 kf_drivetrain_high_low, kf_drivetrain_high_high],
393 namespaces = namespaces)
394 kf_loop_writer.Write(argv[3], argv[4])
395
396if __name__ == '__main__':
397 sys.exit(main(sys.argv))