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