blob: 4b44115b420ee5c06c17f99937284b5897761664 [file] [log] [blame]
Comran Morshed0d6cf9b2015-06-17 19:29:57 +00001#!/usr/bin/python
2
Campbell Crowley9c3ecfd2015-12-31 17:04:30 -08003from frc971.control_loops.python import control_loop
4from frc971.control_loops.python import controls
Comran Morshed0d6cf9b2015-06-17 19:29:57 +00005import numpy
6import sys
7from matplotlib import pylab
8
Austin Schuh178d5152016-11-26 14:58:40 -08009import gflags
Campbell Crowley9c3ecfd2015-12-31 17:04:30 -080010import glog
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000011
Austin Schuh178d5152016-11-26 14:58:40 -080012FLAGS = gflags.FLAGS
13
14gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.')
15
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000016
17class Drivetrain(control_loop.ControlLoop):
18 def __init__(self, name="Drivetrain", left_low=True, right_low=True):
19 super(Drivetrain, self).__init__(name)
Austin Schuh178d5152016-11-26 14:58:40 -080020 # Number of motors per side
21 self.num_motors = 1
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000022 # Stall Torque in N m
Austin Schuh178d5152016-11-26 14:58:40 -080023 self.stall_torque = 2.42 * self.num_motors * 0.60
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000024 # Stall Current in Amps
Austin Schuh178d5152016-11-26 14:58:40 -080025 self.stall_current = 133.0 * self.num_motors
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000026 # Free Speed in RPM. Used number from last year.
Austin Schuh178d5152016-11-26 14:58:40 -080027 self.free_speed = 5500.0
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000028 # Free Current in Amps
Austin Schuh178d5152016-11-26 14:58:40 -080029 self.free_current = 4.7 * self.num_motors
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000030 # Moment of inertia of the drivetrain in kg m^2
31 # Just borrowed from last year.
Austin Schuh4c6a84d2015-09-14 21:34:08 +000032 self.J = 10
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000033 # Mass of the robot, in kg.
34 self.m = 68
35 # Radius of the robot, in meters (from last year).
36 self.rb = 0.9603 / 2.0
37 # Radius of the wheels, in meters.
Austin Schuh511a67b2015-09-12 13:47:12 -070038 self.r = 0.0508
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000039 # Resistance of the motor, divided by the number of motors.
Austin Schuh178d5152016-11-26 14:58:40 -080040 self.resistance = 12.0 / self.stall_current
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000041 # Motor velocity constant
42 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
Austin Schuh178d5152016-11-26 14:58:40 -080043 (12.0 - self.resistance * self.free_current))
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000044 # Torque constant
45 self.Kt = self.stall_torque / self.stall_current
46 # Gear ratios
Austin Schuh511a67b2015-09-12 13:47:12 -070047 self.G_const = 18.0 / 44.0 * 18.0 / 60.0
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000048
49 self.G_low = self.G_const
50 self.G_high = self.G_const
51
52 if left_low:
53 self.Gl = self.G_low
54 else:
55 self.Gl = self.G_high
56 if right_low:
57 self.Gr = self.G_low
58 else:
59 self.Gr = self.G_high
60
61 # Control loop time step
62 self.dt = 0.005
63
64 # These describe the way that a given side of a robot will be influenced
65 # by the other side. Units of 1 / kg.
66 self.msp = 1.0 / self.m + self.rb * self.rb / self.J
67 self.msn = 1.0 / self.m - self.rb * self.rb / self.J
68 # The calculations which we will need for A and B.
Austin Schuh178d5152016-11-26 14:58:40 -080069 self.tcl = -self.Kt / self.Kv / (self.Gl * self.Gl * self.resistance * self.r * self.r)
70 self.tcr = -self.Kt / self.Kv / (self.Gr * self.Gr * self.resistance * self.r * self.r)
71 self.mpl = self.Kt / (self.Gl * self.resistance * self.r)
72 self.mpr = self.Kt / (self.Gr * self.resistance * self.r)
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000073
74 # State feedback matrices
75 # X will be of the format
76 # [[positionl], [velocityl], [positionr], velocityr]]
77 self.A_continuous = numpy.matrix(
78 [[0, 1, 0, 0],
79 [0, self.msp * self.tcl, 0, self.msn * self.tcr],
80 [0, 0, 0, 1],
81 [0, self.msn * self.tcl, 0, self.msp * self.tcr]])
82 self.B_continuous = numpy.matrix(
83 [[0, 0],
84 [self.msp * self.mpl, self.msn * self.mpr],
85 [0, 0],
86 [self.msn * self.mpl, self.msp * self.mpr]])
87 self.C = numpy.matrix([[1, 0, 0, 0],
88 [0, 0, 1, 0]])
89 self.D = numpy.matrix([[0, 0],
90 [0, 0]])
91
Comran Morshed0d6cf9b2015-06-17 19:29:57 +000092 self.A, self.B = self.ContinuousToDiscrete(
93 self.A_continuous, self.B_continuous, self.dt)
94
Austin Schuh178d5152016-11-26 14:58:40 -080095 if left_low or right_low:
96 q_pos = 0.12
97 q_vel = 1.0
98 else:
99 q_pos = 0.14
100 q_vel = 0.95
101
102 # Tune the LQR controller
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000103 self.Q = numpy.matrix([[(1.0 / (q_pos ** 2.0)), 0.0, 0.0, 0.0],
104 [0.0, (1.0 / (q_vel ** 2.0)), 0.0, 0.0],
105 [0.0, 0.0, (1.0 / (q_pos ** 2.0)), 0.0],
106 [0.0, 0.0, 0.0, (1.0 / (q_vel ** 2.0))]])
107
108 self.R = numpy.matrix([[(1.0 / (12.0 ** 2.0)), 0.0],
109 [0.0, (1.0 / (12.0 ** 2.0))]])
110 self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
Austin Schuh178d5152016-11-26 14:58:40 -0800111
112 glog.debug('DT q_pos %f q_vel %s %s', q_pos, q_vel, name)
113 glog.debug(str(numpy.linalg.eig(self.A - self.B * self.K)[0]))
114 glog.debug('K %s', repr(self.K))
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000115
116 self.hlp = 0.3
117 self.llp = 0.4
118 self.PlaceObserverPoles([self.hlp, self.hlp, self.llp, self.llp])
119
120 self.U_max = numpy.matrix([[12.0], [12.0]])
121 self.U_min = numpy.matrix([[-12.0], [-12.0]])
Austin Schuh178d5152016-11-26 14:58:40 -0800122
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000123 self.InitializeState()
124
Austin Schuh178d5152016-11-26 14:58:40 -0800125
126class KFDrivetrain(Drivetrain):
127 def __init__(self, name="KFDrivetrain", left_low=True, right_low=True):
128 super(KFDrivetrain, self).__init__(name, left_low, right_low)
129
130 self.unaugmented_A_continuous = self.A_continuous
131 self.unaugmented_B_continuous = self.B_continuous
132
133 # The practical voltage applied to the wheels is
134 # V_left = U_left + left_voltage_error
135 #
136 # The states are
137 # [left position, left velocity, right position, right velocity,
138 # left voltage error, right voltage error, angular_error]
139 #
140 # The left and right positions are filtered encoder positions and are not
141 # adjusted for heading error.
142 # The turn velocity as computed by the left and right velocities is
143 # adjusted by the gyro velocity.
144 # The angular_error is the angular velocity error between the wheel speed
145 # and the gyro speed.
146 self.A_continuous = numpy.matrix(numpy.zeros((7, 7)))
147 self.B_continuous = numpy.matrix(numpy.zeros((7, 2)))
148 self.A_continuous[0:4,0:4] = self.unaugmented_A_continuous
149 self.A_continuous[0:4,4:6] = self.unaugmented_B_continuous
150 self.B_continuous[0:4,0:2] = self.unaugmented_B_continuous
151 self.A_continuous[0,6] = 1
152 self.A_continuous[2,6] = -1
153
154 self.A, self.B = self.ContinuousToDiscrete(
155 self.A_continuous, self.B_continuous, self.dt)
156
157 self.C = numpy.matrix([[1, 0, 0, 0, 0, 0, 0],
158 [0, 0, 1, 0, 0, 0, 0],
159 [0, -0.5 / self.rb, 0, 0.5 / self.rb, 0, 0, 0]])
160
161 self.D = numpy.matrix([[0, 0],
162 [0, 0],
163 [0, 0]])
164
165 q_pos = 0.05
166 q_vel = 1.00
167 q_voltage = 10.0
168 q_encoder_uncertainty = 2.00
169
170 self.Q = numpy.matrix([[(q_pos ** 2.0), 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
171 [0.0, (q_vel ** 2.0), 0.0, 0.0, 0.0, 0.0, 0.0],
172 [0.0, 0.0, (q_pos ** 2.0), 0.0, 0.0, 0.0, 0.0],
173 [0.0, 0.0, 0.0, (q_vel ** 2.0), 0.0, 0.0, 0.0],
174 [0.0, 0.0, 0.0, 0.0, (q_voltage ** 2.0), 0.0, 0.0],
175 [0.0, 0.0, 0.0, 0.0, 0.0, (q_voltage ** 2.0), 0.0],
176 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, (q_encoder_uncertainty ** 2.0)]])
177
178 r_pos = 0.0001
179 r_gyro = 0.000001
180 self.R = numpy.matrix([[(r_pos ** 2.0), 0.0, 0.0],
181 [0.0, (r_pos ** 2.0), 0.0],
182 [0.0, 0.0, (r_gyro ** 2.0)]])
183
184 # Solving for kf gains.
185 self.KalmanGain, self.Q_steady = controls.kalman(
186 A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R)
187
188 self.L = self.A * self.KalmanGain
189
190 unaug_K = self.K
191
192 # Implement a nice closed loop controller for use by the closed loop
193 # controller.
194 self.K = numpy.matrix(numpy.zeros((self.B.shape[1], self.A.shape[0])))
195 self.K[0:2, 0:4] = unaug_K
196 self.K[0, 4] = 1.0
197 self.K[1, 5] = 1.0
198
199 self.Qff = numpy.matrix(numpy.zeros((4, 4)))
200 qff_pos = 0.005
201 qff_vel = 1.00
202 self.Qff[0, 0] = 1.0 / qff_pos ** 2.0
203 self.Qff[1, 1] = 1.0 / qff_vel ** 2.0
204 self.Qff[2, 2] = 1.0 / qff_pos ** 2.0
205 self.Qff[3, 3] = 1.0 / qff_vel ** 2.0
206 self.Kff = numpy.matrix(numpy.zeros((2, 7)))
207 self.Kff[0:2, 0:4] = controls.TwoStateFeedForwards(self.B[0:4,:], self.Qff)
208
209 self.InitializeState()
210
211
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000212def main(argv):
Austin Schuh178d5152016-11-26 14:58:40 -0800213 argv = FLAGS(argv)
214 glog.init()
215
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000216 # Simulate the response of the system to a step input.
Austin Schuh178d5152016-11-26 14:58:40 -0800217 drivetrain = Drivetrain(left_low=False, right_low=False)
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000218 simulated_left = []
219 simulated_right = []
220 for _ in xrange(100):
221 drivetrain.Update(numpy.matrix([[12.0], [12.0]]))
222 simulated_left.append(drivetrain.X[0, 0])
223 simulated_right.append(drivetrain.X[2, 0])
224
Austin Schuh178d5152016-11-26 14:58:40 -0800225 if FLAGS.plot:
226 pylab.plot(range(100), simulated_left)
227 pylab.plot(range(100), simulated_right)
228 pylab.suptitle('Acceleration Test')
229 pylab.show()
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000230
231 # Simulate forwards motion.
Austin Schuh178d5152016-11-26 14:58:40 -0800232 drivetrain = Drivetrain(left_low=False, right_low=False)
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000233 close_loop_left = []
234 close_loop_right = []
Austin Schuh178d5152016-11-26 14:58:40 -0800235 left_power = []
236 right_power = []
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000237 R = numpy.matrix([[1.0], [0.0], [1.0], [0.0]])
Austin Schuh178d5152016-11-26 14:58:40 -0800238 for _ in xrange(300):
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000239 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
240 drivetrain.U_min, drivetrain.U_max)
241 drivetrain.UpdateObserver(U)
242 drivetrain.Update(U)
243 close_loop_left.append(drivetrain.X[0, 0])
244 close_loop_right.append(drivetrain.X[2, 0])
Austin Schuh178d5152016-11-26 14:58:40 -0800245 left_power.append(U[0, 0])
246 right_power.append(U[1, 0])
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000247
Austin Schuh178d5152016-11-26 14:58:40 -0800248 if FLAGS.plot:
249 pylab.plot(range(300), close_loop_left, label='left position')
250 pylab.plot(range(300), close_loop_right, label='right position')
251 pylab.plot(range(300), left_power, label='left power')
252 pylab.plot(range(300), right_power, label='right power')
253 pylab.suptitle('Linear Move')
254 pylab.legend()
255 pylab.show()
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000256
257 # Try turning in place
258 drivetrain = Drivetrain()
259 close_loop_left = []
260 close_loop_right = []
261 R = numpy.matrix([[-1.0], [0.0], [1.0], [0.0]])
262 for _ in xrange(100):
263 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
264 drivetrain.U_min, drivetrain.U_max)
265 drivetrain.UpdateObserver(U)
266 drivetrain.Update(U)
267 close_loop_left.append(drivetrain.X[0, 0])
268 close_loop_right.append(drivetrain.X[2, 0])
269
Austin Schuh178d5152016-11-26 14:58:40 -0800270 if FLAGS.plot:
271 pylab.plot(range(100), close_loop_left)
272 pylab.plot(range(100), close_loop_right)
273 pylab.suptitle('Angular Move')
274 pylab.show()
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000275
276 # Try turning just one side.
277 drivetrain = Drivetrain()
278 close_loop_left = []
279 close_loop_right = []
280 R = numpy.matrix([[0.0], [0.0], [1.0], [0.0]])
281 for _ in xrange(100):
282 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
283 drivetrain.U_min, drivetrain.U_max)
284 drivetrain.UpdateObserver(U)
285 drivetrain.Update(U)
286 close_loop_left.append(drivetrain.X[0, 0])
287 close_loop_right.append(drivetrain.X[2, 0])
288
Austin Schuh178d5152016-11-26 14:58:40 -0800289 if FLAGS.plot:
290 pylab.plot(range(100), close_loop_left)
291 pylab.plot(range(100), close_loop_right)
292 pylab.suptitle('Pivot')
293 pylab.show()
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000294
295 # Write the generated constants out to a file.
Austin Schuh178d5152016-11-26 14:58:40 -0800296 drivetrain_low_low = Drivetrain(
297 name="DrivetrainLowLow", left_low=True, right_low=True)
298 drivetrain_low_high = Drivetrain(
299 name="DrivetrainLowHigh", left_low=True, right_low=False)
300 drivetrain_high_low = Drivetrain(
301 name="DrivetrainHighLow", left_low=False, right_low=True)
302 drivetrain_high_high = Drivetrain(
303 name="DrivetrainHighHigh", left_low=False, right_low=False)
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000304
Austin Schuh178d5152016-11-26 14:58:40 -0800305 kf_drivetrain_low_low = KFDrivetrain(
306 name="KFDrivetrainLowLow", left_low=True, right_low=True)
307 kf_drivetrain_low_high = KFDrivetrain(
308 name="KFDrivetrainLowHigh", left_low=True, right_low=False)
309 kf_drivetrain_high_low = KFDrivetrain(
310 name="KFDrivetrainHighLow", left_low=False, right_low=True)
311 kf_drivetrain_high_high = KFDrivetrain(
312 name="KFDrivetrainHighHigh", left_low=False, right_low=False)
313
314 if len(argv) != 5:
315 print "Expected .h file name and .cc file name"
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000316 else:
Austin Schuh178d5152016-11-26 14:58:40 -0800317 namespaces = ['y2015_bot3', 'control_loops', 'drivetrain']
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000318 dog_loop_writer = control_loop.ControlLoopWriter(
319 "Drivetrain", [drivetrain_low_low, drivetrain_low_high,
Jasmine Zhoudde7a772015-09-11 23:08:52 -0700320 drivetrain_high_low, drivetrain_high_high],
Austin Schuh178d5152016-11-26 14:58:40 -0800321 namespaces = namespaces)
322 dog_loop_writer.AddConstant(control_loop.Constant("kDt", "%f",
323 drivetrain_low_low.dt))
324 dog_loop_writer.AddConstant(control_loop.Constant("kStallTorque", "%f",
325 drivetrain_low_low.stall_torque))
326 dog_loop_writer.AddConstant(control_loop.Constant("kStallCurrent", "%f",
327 drivetrain_low_low.stall_current))
328 dog_loop_writer.AddConstant(control_loop.Constant("kFreeSpeedRPM", "%f",
329 drivetrain_low_low.free_speed))
330 dog_loop_writer.AddConstant(control_loop.Constant("kFreeCurrent", "%f",
331 drivetrain_low_low.free_current))
332 dog_loop_writer.AddConstant(control_loop.Constant("kJ", "%f",
333 drivetrain_low_low.J))
334 dog_loop_writer.AddConstant(control_loop.Constant("kMass", "%f",
335 drivetrain_low_low.m))
336 dog_loop_writer.AddConstant(control_loop.Constant("kRobotRadius", "%f",
337 drivetrain_low_low.rb))
338 dog_loop_writer.AddConstant(control_loop.Constant("kWheelRadius", "%f",
339 drivetrain_low_low.r))
340 dog_loop_writer.AddConstant(control_loop.Constant("kR", "%f",
341 drivetrain_low_low.resistance))
342 dog_loop_writer.AddConstant(control_loop.Constant("kV", "%f",
343 drivetrain_low_low.Kv))
344 dog_loop_writer.AddConstant(control_loop.Constant("kT", "%f",
345 drivetrain_low_low.Kt))
346 dog_loop_writer.AddConstant(control_loop.Constant("kLowGearRatio", "%f",
347 drivetrain_low_low.G_low))
348 dog_loop_writer.AddConstant(control_loop.Constant("kHighGearRatio", "%f",
349 drivetrain_high_high.G_high))
350
Campbell Crowley9c3ecfd2015-12-31 17:04:30 -0800351 dog_loop_writer.Write(argv[1], argv[2])
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000352
Austin Schuh178d5152016-11-26 14:58:40 -0800353 kf_loop_writer = control_loop.ControlLoopWriter(
354 "KFDrivetrain", [kf_drivetrain_low_low, kf_drivetrain_low_high,
355 kf_drivetrain_high_low, kf_drivetrain_high_high],
356 namespaces = namespaces)
357 kf_loop_writer.Write(argv[3], argv[4])
358
Comran Morshed0d6cf9b2015-06-17 19:29:57 +0000359if __name__ == '__main__':
360 sys.exit(main(sys.argv))