blob: 9381eb2f41cba7f5503c7d430d9911417011ce15 [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
Comran Morshed9a9948c2016-01-16 15:58:04 +000016
17class Drivetrain(control_loop.ControlLoop):
18 def __init__(self, name="Drivetrain", left_low=True, right_low=True):
19 super(Drivetrain, self).__init__(name)
20 # Number of motors per side
21 self.num_motors = 2
22 # Stall Torque in N m
23 self.stall_torque = 2.42 * self.num_motors * 0.60
24 # Stall Current in Amps
25 self.stall_current = 133.0 * self.num_motors
26 # Free Speed in RPM. Used number from last year.
27 self.free_speed = 5500.0
28 # Free Current in Amps
29 self.free_current = 4.7 * self.num_motors
30 # Moment of inertia of the drivetrain in kg m^2
Austin Schuh126924c2016-02-28 21:56:17 -080031 self.J = 2.0
Comran Morshed9a9948c2016-01-16 15:58:04 +000032 # Mass of the robot, in kg.
33 self.m = 68
Comran Morshed001c7c32016-02-15 21:04:55 +000034 # Radius of the robot, in meters (requires tuning by hand)
35 self.rb = 0.601 / 2.0
Comran Morshed9a9948c2016-01-16 15:58:04 +000036 # Radius of the wheels, in meters.
Austin Schuhceb23cc2016-04-20 20:12:18 -070037 self.r = 0.097155 * 0.9811158901447808 / 118.0 * 115.75
Comran Morshed9a9948c2016-01-16 15:58:04 +000038 # Resistance of the motor, divided by the number of motors.
39 self.resistance = 12.0 / self.stall_current
40 # Motor velocity constant
41 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
42 (12.0 - self.resistance * self.free_current))
43 # Torque constant
44 self.Kt = self.stall_torque / self.stall_current
45 # Gear ratios
Comran Morshed001c7c32016-02-15 21:04:55 +000046 self.G_low = 14.0 / 48.0 * 18.0 / 60.0 * 18.0 / 36.0
47 self.G_high = 14.0 / 48.0 * 28.0 / 50.0 * 18.0 / 36.0
Comran Morshed9a9948c2016-01-16 15:58:04 +000048 if left_low:
49 self.Gl = self.G_low
50 else:
51 self.Gl = self.G_high
52 if right_low:
53 self.Gr = self.G_low
54 else:
55 self.Gr = self.G_high
56
57 # Control loop time step
58 self.dt = 0.005
59
60 # These describe the way that a given side of a robot will be influenced
61 # by the other side. Units of 1 / kg.
62 self.msp = 1.0 / self.m + self.rb * self.rb / self.J
63 self.msn = 1.0 / self.m - self.rb * self.rb / self.J
64 # The calculations which we will need for A and B.
65 self.tcl = -self.Kt / self.Kv / (self.Gl * self.Gl * self.resistance * self.r * self.r)
66 self.tcr = -self.Kt / self.Kv / (self.Gr * self.Gr * self.resistance * self.r * self.r)
67 self.mpl = self.Kt / (self.Gl * self.resistance * self.r)
68 self.mpr = self.Kt / (self.Gr * self.resistance * self.r)
69
70 # State feedback matrices
71 # X will be of the format
72 # [[positionl], [velocityl], [positionr], velocityr]]
73 self.A_continuous = numpy.matrix(
74 [[0, 1, 0, 0],
75 [0, self.msp * self.tcl, 0, self.msn * self.tcr],
76 [0, 0, 0, 1],
77 [0, self.msn * self.tcl, 0, self.msp * self.tcr]])
78 self.B_continuous = numpy.matrix(
79 [[0, 0],
80 [self.msp * self.mpl, self.msn * self.mpr],
81 [0, 0],
82 [self.msn * self.mpl, self.msp * self.mpr]])
83 self.C = numpy.matrix([[1, 0, 0, 0],
84 [0, 0, 1, 0]])
85 self.D = numpy.matrix([[0, 0],
86 [0, 0]])
87
88 self.A, self.B = self.ContinuousToDiscrete(
89 self.A_continuous, self.B_continuous, self.dt)
90
Austin Schuh093535c2016-03-05 23:21:00 -080091 if left_low or right_low:
92 q_pos = 0.12
93 q_vel = 1.0
94 else:
95 q_pos = 0.14
96 q_vel = 0.95
97
Austin Schuh2a671df2016-11-26 15:00:06 -080098 # Tune the LQR controller
Comran Morshed9a9948c2016-01-16 15:58:04 +000099 self.Q = numpy.matrix([[(1.0 / (q_pos ** 2.0)), 0.0, 0.0, 0.0],
100 [0.0, (1.0 / (q_vel ** 2.0)), 0.0, 0.0],
101 [0.0, 0.0, (1.0 / (q_pos ** 2.0)), 0.0],
102 [0.0, 0.0, 0.0, (1.0 / (q_vel ** 2.0))]])
103
104 self.R = numpy.matrix([[(1.0 / (12.0 ** 2.0)), 0.0],
105 [0.0, (1.0 / (12.0 ** 2.0))]])
106 self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
107
Austin Schuh093535c2016-03-05 23:21:00 -0800108 glog.debug('DT q_pos %f q_vel %s %s', q_pos, q_vel, name)
Comran Morshed9a9948c2016-01-16 15:58:04 +0000109 glog.debug(str(numpy.linalg.eig(self.A - self.B * self.K)[0]))
Austin Schuh093535c2016-03-05 23:21:00 -0800110 glog.debug('K %s', repr(self.K))
Comran Morshed9a9948c2016-01-16 15:58:04 +0000111
112 self.hlp = 0.3
113 self.llp = 0.4
114 self.PlaceObserverPoles([self.hlp, self.hlp, self.llp, self.llp])
115
116 self.U_max = numpy.matrix([[12.0], [12.0]])
117 self.U_min = numpy.matrix([[-12.0], [-12.0]])
Austin Schuh093535c2016-03-05 23:21:00 -0800118
Comran Morshed9a9948c2016-01-16 15:58:04 +0000119 self.InitializeState()
120
121
122class KFDrivetrain(Drivetrain):
123 def __init__(self, name="KFDrivetrain", left_low=True, right_low=True):
124 super(KFDrivetrain, self).__init__(name, left_low, right_low)
125
126 self.unaugmented_A_continuous = self.A_continuous
127 self.unaugmented_B_continuous = self.B_continuous
128
Comran Morshed9a9948c2016-01-16 15:58:04 +0000129 # The practical voltage applied to the wheels is
130 # V_left = U_left + left_voltage_error
131 #
Austin Schuh2a671df2016-11-26 15:00:06 -0800132 # The states are
Comran Morshed9a9948c2016-01-16 15:58:04 +0000133 # [left position, left velocity, right position, right velocity,
134 # left voltage error, right voltage error, angular_error]
Austin Schuh093535c2016-03-05 23:21:00 -0800135 #
136 # The left and right positions are filtered encoder positions and are not
137 # adjusted for heading error.
138 # The turn velocity as computed by the left and right velocities is
139 # adjusted by the gyro velocity.
140 # The angular_error is the angular velocity error between the wheel speed
141 # and the gyro speed.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000142 self.A_continuous = numpy.matrix(numpy.zeros((7, 7)))
143 self.B_continuous = numpy.matrix(numpy.zeros((7, 2)))
144 self.A_continuous[0:4,0:4] = self.unaugmented_A_continuous
145 self.A_continuous[0:4,4:6] = self.unaugmented_B_continuous
146 self.B_continuous[0:4,0:2] = self.unaugmented_B_continuous
147 self.A_continuous[0,6] = 1
148 self.A_continuous[2,6] = -1
149
150 self.A, self.B = self.ContinuousToDiscrete(
151 self.A_continuous, self.B_continuous, self.dt)
152
153 self.C = numpy.matrix([[1, 0, 0, 0, 0, 0, 0],
154 [0, 0, 1, 0, 0, 0, 0],
155 [0, -0.5 / self.rb, 0, 0.5 / self.rb, 0, 0, 0]])
156
157 self.D = numpy.matrix([[0, 0],
158 [0, 0],
159 [0, 0]])
160
161 q_pos = 0.05
162 q_vel = 1.00
163 q_voltage = 10.0
164 q_encoder_uncertainty = 2.00
165
166 self.Q = numpy.matrix([[(q_pos ** 2.0), 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
167 [0.0, (q_vel ** 2.0), 0.0, 0.0, 0.0, 0.0, 0.0],
168 [0.0, 0.0, (q_pos ** 2.0), 0.0, 0.0, 0.0, 0.0],
169 [0.0, 0.0, 0.0, (q_vel ** 2.0), 0.0, 0.0, 0.0],
170 [0.0, 0.0, 0.0, 0.0, (q_voltage ** 2.0), 0.0, 0.0],
171 [0.0, 0.0, 0.0, 0.0, 0.0, (q_voltage ** 2.0), 0.0],
172 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, (q_encoder_uncertainty ** 2.0)]])
173
174 r_pos = 0.0001
175 r_gyro = 0.000001
176 self.R = numpy.matrix([[(r_pos ** 2.0), 0.0, 0.0],
177 [0.0, (r_pos ** 2.0), 0.0],
178 [0.0, 0.0, (r_gyro ** 2.0)]])
179
180 # Solving for kf gains.
181 self.KalmanGain, self.Q_steady = controls.kalman(
182 A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R)
183
184 self.L = self.A * self.KalmanGain
185
Austin Schuh093535c2016-03-05 23:21:00 -0800186 unaug_K = self.K
187
188 # Implement a nice closed loop controller for use by the closed loop
189 # controller.
Comran Morshed9a9948c2016-01-16 15:58:04 +0000190 self.K = numpy.matrix(numpy.zeros((self.B.shape[1], self.A.shape[0])))
Austin Schuh093535c2016-03-05 23:21:00 -0800191 self.K[0:2, 0:4] = unaug_K
192 self.K[0, 4] = 1.0
193 self.K[1, 5] = 1.0
194
195 self.Qff = numpy.matrix(numpy.zeros((4, 4)))
196 qff_pos = 0.005
197 qff_vel = 1.00
198 self.Qff[0, 0] = 1.0 / qff_pos ** 2.0
199 self.Qff[1, 1] = 1.0 / qff_vel ** 2.0
200 self.Qff[2, 2] = 1.0 / qff_pos ** 2.0
201 self.Qff[3, 3] = 1.0 / qff_vel ** 2.0
202 self.Kff = numpy.matrix(numpy.zeros((2, 7)))
203 self.Kff[0:2, 0:4] = controls.TwoStateFeedForwards(self.B[0:4,:], self.Qff)
204
205 self.InitializeState()
Comran Morshed9a9948c2016-01-16 15:58:04 +0000206
207
208def main(argv):
209 argv = FLAGS(argv)
Austin Schuh289ee4f2016-02-05 00:20:52 -0800210 glog.init()
Comran Morshed9a9948c2016-01-16 15:58:04 +0000211
212 # Simulate the response of the system to a step input.
Austin Schuh093535c2016-03-05 23:21:00 -0800213 drivetrain = Drivetrain(left_low=False, right_low=False)
Comran Morshed9a9948c2016-01-16 15:58:04 +0000214 simulated_left = []
215 simulated_right = []
216 for _ in xrange(100):
217 drivetrain.Update(numpy.matrix([[12.0], [12.0]]))
218 simulated_left.append(drivetrain.X[0, 0])
219 simulated_right.append(drivetrain.X[2, 0])
220
221 if FLAGS.plot:
222 pylab.plot(range(100), simulated_left)
223 pylab.plot(range(100), simulated_right)
Austin Schuh093535c2016-03-05 23:21:00 -0800224 pylab.suptitle('Acceleration Test')
Comran Morshed9a9948c2016-01-16 15:58:04 +0000225 pylab.show()
226
227 # Simulate forwards motion.
Austin Schuh093535c2016-03-05 23:21:00 -0800228 drivetrain = Drivetrain(left_low=False, right_low=False)
Comran Morshed9a9948c2016-01-16 15:58:04 +0000229 close_loop_left = []
230 close_loop_right = []
Austin Schuh093535c2016-03-05 23:21:00 -0800231 left_power = []
232 right_power = []
Comran Morshed9a9948c2016-01-16 15:58:04 +0000233 R = numpy.matrix([[1.0], [0.0], [1.0], [0.0]])
Austin Schuh093535c2016-03-05 23:21:00 -0800234 for _ in xrange(300):
Comran Morshed9a9948c2016-01-16 15:58:04 +0000235 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
236 drivetrain.U_min, drivetrain.U_max)
237 drivetrain.UpdateObserver(U)
238 drivetrain.Update(U)
239 close_loop_left.append(drivetrain.X[0, 0])
240 close_loop_right.append(drivetrain.X[2, 0])
Austin Schuh093535c2016-03-05 23:21:00 -0800241 left_power.append(U[0, 0])
242 right_power.append(U[1, 0])
Comran Morshed9a9948c2016-01-16 15:58:04 +0000243
244 if FLAGS.plot:
Austin Schuh093535c2016-03-05 23:21:00 -0800245 pylab.plot(range(300), close_loop_left, label='left position')
246 pylab.plot(range(300), close_loop_right, label='right position')
247 pylab.plot(range(300), left_power, label='left power')
248 pylab.plot(range(300), right_power, label='right power')
249 pylab.suptitle('Linear Move')
250 pylab.legend()
Comran Morshed9a9948c2016-01-16 15:58:04 +0000251 pylab.show()
252
253 # Try turning in place
254 drivetrain = Drivetrain()
255 close_loop_left = []
256 close_loop_right = []
257 R = numpy.matrix([[-1.0], [0.0], [1.0], [0.0]])
258 for _ in xrange(100):
259 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
260 drivetrain.U_min, drivetrain.U_max)
261 drivetrain.UpdateObserver(U)
262 drivetrain.Update(U)
263 close_loop_left.append(drivetrain.X[0, 0])
264 close_loop_right.append(drivetrain.X[2, 0])
265
266 if FLAGS.plot:
267 pylab.plot(range(100), close_loop_left)
268 pylab.plot(range(100), close_loop_right)
Austin Schuh093535c2016-03-05 23:21:00 -0800269 pylab.suptitle('Angular Move')
Comran Morshed9a9948c2016-01-16 15:58:04 +0000270 pylab.show()
271
272 # Try turning just one side.
273 drivetrain = Drivetrain()
274 close_loop_left = []
275 close_loop_right = []
276 R = numpy.matrix([[0.0], [0.0], [1.0], [0.0]])
277 for _ in xrange(100):
278 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
279 drivetrain.U_min, drivetrain.U_max)
280 drivetrain.UpdateObserver(U)
281 drivetrain.Update(U)
282 close_loop_left.append(drivetrain.X[0, 0])
283 close_loop_right.append(drivetrain.X[2, 0])
284
285 if FLAGS.plot:
286 pylab.plot(range(100), close_loop_left)
287 pylab.plot(range(100), close_loop_right)
Austin Schuh093535c2016-03-05 23:21:00 -0800288 pylab.suptitle('Pivot')
Comran Morshed9a9948c2016-01-16 15:58:04 +0000289 pylab.show()
290
291 # Write the generated constants out to a file.
292 drivetrain_low_low = Drivetrain(
293 name="DrivetrainLowLow", left_low=True, right_low=True)
294 drivetrain_low_high = Drivetrain(
295 name="DrivetrainLowHigh", left_low=True, right_low=False)
296 drivetrain_high_low = Drivetrain(
297 name="DrivetrainHighLow", left_low=False, right_low=True)
298 drivetrain_high_high = Drivetrain(
299 name="DrivetrainHighHigh", left_low=False, right_low=False)
300
301 kf_drivetrain_low_low = KFDrivetrain(
302 name="KFDrivetrainLowLow", left_low=True, right_low=True)
303 kf_drivetrain_low_high = KFDrivetrain(
304 name="KFDrivetrainLowHigh", left_low=True, right_low=False)
305 kf_drivetrain_high_low = KFDrivetrain(
306 name="KFDrivetrainHighLow", left_low=False, right_low=True)
307 kf_drivetrain_high_high = KFDrivetrain(
308 name="KFDrivetrainHighHigh", left_low=False, right_low=False)
309
310 if len(argv) != 5:
311 print "Expected .h file name and .cc file name"
312 else:
Comran Morshed6c6a0a92016-01-17 12:45:16 +0000313 namespaces = ['y2016', 'control_loops', 'drivetrain']
Comran Morshed9a9948c2016-01-16 15:58:04 +0000314 dog_loop_writer = control_loop.ControlLoopWriter(
315 "Drivetrain", [drivetrain_low_low, drivetrain_low_high,
316 drivetrain_high_low, drivetrain_high_high],
317 namespaces = namespaces)
318 dog_loop_writer.AddConstant(control_loop.Constant("kDt", "%f",
319 drivetrain_low_low.dt))
320 dog_loop_writer.AddConstant(control_loop.Constant("kStallTorque", "%f",
321 drivetrain_low_low.stall_torque))
322 dog_loop_writer.AddConstant(control_loop.Constant("kStallCurrent", "%f",
323 drivetrain_low_low.stall_current))
324 dog_loop_writer.AddConstant(control_loop.Constant("kFreeSpeedRPM", "%f",
325 drivetrain_low_low.free_speed))
326 dog_loop_writer.AddConstant(control_loop.Constant("kFreeCurrent", "%f",
327 drivetrain_low_low.free_current))
328 dog_loop_writer.AddConstant(control_loop.Constant("kJ", "%f",
329 drivetrain_low_low.J))
330 dog_loop_writer.AddConstant(control_loop.Constant("kMass", "%f",
331 drivetrain_low_low.m))
332 dog_loop_writer.AddConstant(control_loop.Constant("kRobotRadius", "%f",
333 drivetrain_low_low.rb))
334 dog_loop_writer.AddConstant(control_loop.Constant("kWheelRadius", "%f",
335 drivetrain_low_low.r))
336 dog_loop_writer.AddConstant(control_loop.Constant("kR", "%f",
337 drivetrain_low_low.resistance))
338 dog_loop_writer.AddConstant(control_loop.Constant("kV", "%f",
339 drivetrain_low_low.Kv))
340 dog_loop_writer.AddConstant(control_loop.Constant("kT", "%f",
341 drivetrain_low_low.Kt))
Austin Schuh33bc6842016-02-17 00:38:51 -0800342 dog_loop_writer.AddConstant(control_loop.Constant("kLowGearRatio", "%f",
343 drivetrain_low_low.G_low))
344 dog_loop_writer.AddConstant(control_loop.Constant("kHighGearRatio", "%f",
345 drivetrain_high_high.G_high))
Comran Morshed9a9948c2016-01-16 15:58:04 +0000346
347 dog_loop_writer.Write(argv[1], argv[2])
348
349 kf_loop_writer = control_loop.ControlLoopWriter(
350 "KFDrivetrain", [kf_drivetrain_low_low, kf_drivetrain_low_high,
351 kf_drivetrain_high_low, kf_drivetrain_high_high],
352 namespaces = namespaces)
353 kf_loop_writer.Write(argv[3], argv[4])
354
355if __name__ == '__main__':
356 sys.exit(main(sys.argv))