blob: 75425bab7bbdf4dde9e4325f3b1a2ddb5336e83d [file] [log] [blame]
James Kuszmaulf254c1a2013-03-10 16:31:26 -07001#!/usr/bin/python
2
Austin Schuh70810b92016-11-26 14:55:34 -08003from frc971.control_loops.python import control_loop
4from frc971.control_loops.python import controls
James Kuszmaulf254c1a2013-03-10 16:31:26 -07005import numpy
6import sys
7from matplotlib import pylab
8
Austin Schuh70810b92016-11-26 14:55:34 -08009import gflags
10import glog
11
12FLAGS = gflags.FLAGS
13
14gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.')
Austin Schuh8afe35a2013-10-27 10:59:15 -070015
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
Austin Schuh70810b92016-11-26 14:55:34 -080030 self.resistance = 12.0 / self.stall_current
Austin Schuh8afe35a2013-10-27 10:59:15 -070031 # Motor velocity constant
32 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
Austin Schuh70810b92016-11-26 14:55:34 -080033 (12.0 - self.resistance * self.free_current))
Austin Schuh8afe35a2013-10-27 10:59:15 -070034 # Torque constant
35 self.Kt = self.stall_torque / self.stall_current
36 # Control loop time step
Comran Morshed1c91be42015-02-11 21:16:01 +000037 self.dt = 0.005
Austin Schuh8afe35a2013-10-27 10:59:15 -070038
39 # State feedback matrices
40 self.A_continuous = numpy.matrix(
Austin Schuh70810b92016-11-26 14:55:34 -080041 [[-self.Kt / self.Kv / (self.J * self.resistance)]])
Austin Schuh8afe35a2013-10-27 10:59:15 -070042 self.B_continuous = numpy.matrix(
Austin Schuh70810b92016-11-26 14:55:34 -080043 [[self.Kt / (self.J * self.resistance)]])
Austin Schuh8afe35a2013-10-27 10:59:15 -070044 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 Schuh427b3702013-11-02 13:44:09 -070051 self.PlaceObserverPoles([0.01])
Austin Schuh8afe35a2013-10-27 10:59:15 -070052
53 self.U_max = numpy.matrix([[12.0]])
54 self.U_min = numpy.matrix([[-12.0]])
55
56 self.InitializeState()
57
58
James Kuszmaulf254c1a2013-03-10 16:31:26 -070059class Drivetrain(control_loop.ControlLoop):
Brian Silverman176303a2014-04-10 10:54:55 -070060 def __init__(self, name="Drivetrain", left_low=True, right_low=True):
61 super(Drivetrain, self).__init__(name)
Austin Schuh70810b92016-11-26 14:55:34 -080062 # Number of motors per side
63 self.num_motors = 1
James Kuszmaulf254c1a2013-03-10 16:31:26 -070064 # Stall Torque in N m
Austin Schuh70810b92016-11-26 14:55:34 -080065 self.stall_torque = 2.42 * self.num_motors * 0.60
James Kuszmaulf254c1a2013-03-10 16:31:26 -070066 # Stall Current in Amps
Austin Schuh70810b92016-11-26 14:55:34 -080067 self.stall_current = 133.0 * self.num_motors
James Kuszmaulf254c1a2013-03-10 16:31:26 -070068 # Free Speed in RPM. Used number from last year.
Austin Schuh70810b92016-11-26 14:55:34 -080069 self.free_speed = 5500.0
James Kuszmaulf254c1a2013-03-10 16:31:26 -070070 # Free Current in Amps
Austin Schuh70810b92016-11-26 14:55:34 -080071 self.free_current = 4.7 * self.num_motors
James Kuszmaulf254c1a2013-03-10 16:31:26 -070072 # Moment of inertia of the drivetrain in kg m^2
73 # Just borrowed from last year.
Brian Silverman4b36e2e2015-03-16 15:46:53 -070074 self.J = 10
James Kuszmaulf254c1a2013-03-10 16:31:26 -070075 # Mass of the robot, in kg.
76 self.m = 68
77 # Radius of the robot, in meters (from last year).
Austin Schuh0b69d0c2015-03-15 16:38:45 -070078 self.rb = 0.9603 / 2.0
James Kuszmaulf254c1a2013-03-10 16:31:26 -070079 # Radius of the wheels, in meters.
Comran Morshed1c91be42015-02-11 21:16:01 +000080 self.r = .0515938
James Kuszmaulf254c1a2013-03-10 16:31:26 -070081 # Resistance of the motor, divided by the number of motors.
Austin Schuh70810b92016-11-26 14:55:34 -080082 self.resistance = 12.0 / self.stall_current
James Kuszmaulf254c1a2013-03-10 16:31:26 -070083 # Motor velocity constant
84 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
Austin Schuh70810b92016-11-26 14:55:34 -080085 (12.0 - self.resistance * self.free_current))
James Kuszmaulf254c1a2013-03-10 16:31:26 -070086 # Torque constant
87 self.Kt = self.stall_torque / self.stall_current
88 # Gear ratios
Comran Morshed1c91be42015-02-11 21:16:01 +000089 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 Schuhde4d7fe2013-10-08 22:22:45 -070094 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 Morshed1c91be42015-02-11 21:16:01 +0000102
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700103 # Control loop time step
Comran Morshed1c91be42015-02-11 21:16:01 +0000104 self.dt = 0.005
105
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700106 # 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 Schuh70810b92016-11-26 14:55:34 -0800111 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 Kuszmaulf254c1a2013-03-10 16:31:26 -0700115
116 # State feedback matrices
117 # X will be of the format
Austin Schuhde4d7fe2013-10-08 22:22:45 -0700118 # [[positionl], [velocityl], [positionr], velocityr]]
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700119 self.A_continuous = numpy.matrix(
120 [[0, 1, 0, 0],
Austin Schuhde4d7fe2013-10-08 22:22:45 -0700121 [0, self.msp * self.tcl, 0, self.msn * self.tcr],
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700122 [0, 0, 0, 1],
Austin Schuhde4d7fe2013-10-08 22:22:45 -0700123 [0, self.msn * self.tcl, 0, self.msp * self.tcr]])
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700124 self.B_continuous = numpy.matrix(
125 [[0, 0],
Austin Schuhde4d7fe2013-10-08 22:22:45 -0700126 [self.msp * self.mpl, self.msn * self.mpr],
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700127 [0, 0],
Austin Schuhde4d7fe2013-10-08 22:22:45 -0700128 [self.msn * self.mpl, self.msp * self.mpr]])
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700129 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 Schuh4352ac62013-03-19 06:23:16 +0000134 self.A, self.B = self.ContinuousToDiscrete(
135 self.A_continuous, self.B_continuous, self.dt)
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700136
Austin Schuh70810b92016-11-26 14:55:34 -0800137 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 Schuha25a0412014-03-09 00:50:04 -0800145 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 Schuh70810b92016-11-26 14:55:34 -0800153
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 Kuszmaulf254c1a2013-03-10 16:31:26 -0700157
Brian Silverman38ea9bf2014-04-19 22:57:54 -0700158 self.hlp = 0.3
159 self.llp = 0.4
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700160 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 Schuh70810b92016-11-26 14:55:34 -0800164
Austin Schuh4352ac62013-03-19 06:23:16 +0000165 self.InitializeState()
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700166
Austin Schuh70810b92016-11-26 14:55:34 -0800167
168class 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 Kuszmaulf254c1a2013-03-10 16:31:26 -0700254def main(argv):
Austin Schuh70810b92016-11-26 14:55:34 -0800255 argv = FLAGS(argv)
256 glog.init()
257
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700258 # Simulate the response of the system to a step input.
Austin Schuh70810b92016-11-26 14:55:34 -0800259 drivetrain = Drivetrain(left_low=False, right_low=False)
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700260 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 Schuh70810b92016-11-26 14:55:34 -0800267 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 Kuszmaulf254c1a2013-03-10 16:31:26 -0700272
273 # Simulate forwards motion.
Austin Schuh70810b92016-11-26 14:55:34 -0800274 drivetrain = Drivetrain(left_low=False, right_low=False)
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700275 close_loop_left = []
276 close_loop_right = []
Austin Schuh70810b92016-11-26 14:55:34 -0800277 left_power = []
278 right_power = []
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700279 R = numpy.matrix([[1.0], [0.0], [1.0], [0.0]])
Austin Schuh70810b92016-11-26 14:55:34 -0800280 for _ in xrange(300):
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700281 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 Schuh70810b92016-11-26 14:55:34 -0800287 left_power.append(U[0, 0])
288 right_power.append(U[1, 0])
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700289
Austin Schuh70810b92016-11-26 14:55:34 -0800290 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 Kuszmaulf254c1a2013-03-10 16:31:26 -0700298
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 Schuh70810b92016-11-26 14:55:34 -0800312 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 Kuszmaulf254c1a2013-03-10 16:31:26 -0700317
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 Schuh70810b92016-11-26 14:55:34 -0800331 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 Kuszmaulf254c1a2013-03-10 16:31:26 -0700336
337 # Write the generated constants out to a file.
Austin Schuh70810b92016-11-26 14:55:34 -0800338 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 Silverman2c590c32013-11-04 18:08:54 -0800355
356 if len(argv) != 5:
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700357 print "Expected .h file name and .cc file name"
358 else:
Austin Schuh70810b92016-11-26 14:55:34 -0800359 namespaces = ['y2015', 'control_loops', 'drivetrain']
Brian Silverman176303a2014-04-10 10:54:55 -0700360 dog_loop_writer = control_loop.ControlLoopWriter(
361 "Drivetrain", [drivetrain_low_low, drivetrain_low_high,
Austin Schuh70810b92016-11-26 14:55:34 -0800362 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 Silverman2c590c32013-11-04 18:08:54 -0800400
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700401if __name__ == '__main__':
402 sys.exit(main(sys.argv))