blob: 8b178535030cc9695d50444bfccd77d6e0f4faa4 [file] [log] [blame]
Brian Silverman17f503e2015-08-02 18:17:18 -07001#!/usr/bin/python
2
Austin Schuh572ff402015-11-08 12:17:50 -08003from frc971.control_loops.python import control_loop
4from frc971.control_loops.python import controls
Brian Silverman17f503e2015-08-02 18:17:18 -07005import numpy
6import sys
Austin Schuh29659232015-11-26 13:55:30 -08007import argparse
Brian Silverman17f503e2015-08-02 18:17:18 -07008from matplotlib import pylab
9
10
11class CIM(control_loop.ControlLoop):
12 def __init__(self):
13 super(CIM, self).__init__("CIM")
14 # Stall Torque in N m
15 self.stall_torque = 2.42
16 # Stall Current in Amps
17 self.stall_current = 133
18 # Free Speed in RPM
19 self.free_speed = 4650.0
20 # Free Current in Amps
21 self.free_current = 2.7
22 # Moment of inertia of the CIM in kg m^2
23 self.J = 0.0001
24 # Resistance of the motor, divided by 2 to account for the 2 motors
Austin Schuh96ce8ae2015-11-26 12:46:02 -080025 self.resistance = 12.0 / self.stall_current
Brian Silverman17f503e2015-08-02 18:17:18 -070026 # Motor velocity constant
27 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
Austin Schuh96ce8ae2015-11-26 12:46:02 -080028 (12.0 - self.resistance * self.free_current))
Brian Silverman17f503e2015-08-02 18:17:18 -070029 # Torque constant
30 self.Kt = self.stall_torque / self.stall_current
31 # Control loop time step
Austin Schuhadf2cde2015-11-08 20:35:16 -080032 self.dt = 0.005
Brian Silverman17f503e2015-08-02 18:17:18 -070033
34 # State feedback matrices
35 self.A_continuous = numpy.matrix(
Austin Schuh96ce8ae2015-11-26 12:46:02 -080036 [[-self.Kt / self.Kv / (self.J * self.resistance)]])
Brian Silverman17f503e2015-08-02 18:17:18 -070037 self.B_continuous = numpy.matrix(
Austin Schuh96ce8ae2015-11-26 12:46:02 -080038 [[self.Kt / (self.J * self.resistance)]])
Brian Silverman17f503e2015-08-02 18:17:18 -070039 self.C = numpy.matrix([[1]])
40 self.D = numpy.matrix([[0]])
41
42 self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
43 self.B_continuous, self.dt)
44
45 self.PlaceControllerPoles([0.01])
46 self.PlaceObserverPoles([0.01])
47
48 self.U_max = numpy.matrix([[12.0]])
49 self.U_min = numpy.matrix([[-12.0]])
50
51 self.InitializeState()
52
53
54class Drivetrain(control_loop.ControlLoop):
55 def __init__(self, name="Drivetrain", left_low=True, right_low=True):
56 super(Drivetrain, self).__init__(name)
Austin Schuh96ce8ae2015-11-26 12:46:02 -080057 # Number of motors per side
58 self.num_motors = 2
Brian Silverman17f503e2015-08-02 18:17:18 -070059 # Stall Torque in N m
Austin Schuh96ce8ae2015-11-26 12:46:02 -080060 self.stall_torque = 2.42 * self.num_motors
Brian Silverman17f503e2015-08-02 18:17:18 -070061 # Stall Current in Amps
Austin Schuh96ce8ae2015-11-26 12:46:02 -080062 self.stall_current = 133.0 * self.num_motors
Brian Silverman17f503e2015-08-02 18:17:18 -070063 # Free Speed in RPM. Used number from last year.
64 self.free_speed = 4650.0
65 # Free Current in Amps
Austin Schuh96ce8ae2015-11-26 12:46:02 -080066 self.free_current = 2.7 * self.num_motors
Brian Silverman17f503e2015-08-02 18:17:18 -070067 # Moment of inertia of the drivetrain in kg m^2
68 # Just borrowed from last year.
69 self.J = 4.5
70 # Mass of the robot, in kg.
71 self.m = 68
72 # Radius of the robot, in meters (from last year).
73 self.rb = 0.617998644 / 2.0
74 # Radius of the wheels, in meters.
75 self.r = .04445
76 # Resistance of the motor, divided by the number of motors.
Austin Schuh96ce8ae2015-11-26 12:46:02 -080077 self.resistance = 12.0 / self.stall_current
Brian Silverman17f503e2015-08-02 18:17:18 -070078 # Motor velocity constant
79 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
Austin Schuh96ce8ae2015-11-26 12:46:02 -080080 (12.0 - self.resistance * self.free_current))
Brian Silverman17f503e2015-08-02 18:17:18 -070081 # Torque constant
82 self.Kt = self.stall_torque / self.stall_current
83 # Gear ratios
84 self.G_low = 18.0 / 60.0 * 18.0 / 50.0
85 self.G_high = 28.0 / 50.0 * 18.0 / 50.0
86 if left_low:
87 self.Gl = self.G_low
88 else:
89 self.Gl = self.G_high
90 if right_low:
91 self.Gr = self.G_low
92 else:
93 self.Gr = self.G_high
94
95 # Control loop time step
Austin Schuhadf2cde2015-11-08 20:35:16 -080096 self.dt = 0.005
Brian Silverman17f503e2015-08-02 18:17:18 -070097
98 # These describe the way that a given side of a robot will be influenced
99 # by the other side. Units of 1 / kg.
100 self.msp = 1.0 / self.m + self.rb * self.rb / self.J
101 self.msn = 1.0 / self.m - self.rb * self.rb / self.J
102 # The calculations which we will need for A and B.
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800103 self.tcl = -self.Kt / self.Kv / (self.Gl * self.Gl * self.resistance * self.r * self.r)
104 self.tcr = -self.Kt / self.Kv / (self.Gr * self.Gr * self.resistance * self.r * self.r)
105 self.mpl = self.Kt / (self.Gl * self.resistance * self.r)
106 self.mpr = self.Kt / (self.Gr * self.resistance * self.r)
Brian Silverman17f503e2015-08-02 18:17:18 -0700107
108 # State feedback matrices
109 # X will be of the format
110 # [[positionl], [velocityl], [positionr], velocityr]]
111 self.A_continuous = numpy.matrix(
112 [[0, 1, 0, 0],
113 [0, self.msp * self.tcl, 0, self.msn * self.tcr],
114 [0, 0, 0, 1],
115 [0, self.msn * self.tcl, 0, self.msp * self.tcr]])
116 self.B_continuous = numpy.matrix(
117 [[0, 0],
118 [self.msp * self.mpl, self.msn * self.mpr],
119 [0, 0],
120 [self.msn * self.mpl, self.msp * self.mpr]])
121 self.C = numpy.matrix([[1, 0, 0, 0],
122 [0, 0, 1, 0]])
123 self.D = numpy.matrix([[0, 0],
124 [0, 0]])
125
Brian Silverman17f503e2015-08-02 18:17:18 -0700126 self.A, self.B = self.ContinuousToDiscrete(
127 self.A_continuous, self.B_continuous, self.dt)
128
129 # Poles from last year.
130 self.hp = 0.65
131 self.lp = 0.83
132 self.PlaceControllerPoles([self.hp, self.lp, self.hp, self.lp])
Austin Schuh29659232015-11-26 13:55:30 -0800133
Austin Schuhadf2cde2015-11-08 20:35:16 -0800134 q_pos = 0.12
Brian Silverman17f503e2015-08-02 18:17:18 -0700135 q_vel = 1.0
136 self.Q = numpy.matrix([[(1.0 / (q_pos ** 2.0)), 0.0, 0.0, 0.0],
137 [0.0, (1.0 / (q_vel ** 2.0)), 0.0, 0.0],
138 [0.0, 0.0, (1.0 / (q_pos ** 2.0)), 0.0],
139 [0.0, 0.0, 0.0, (1.0 / (q_vel ** 2.0))]])
140
141 self.R = numpy.matrix([[(1.0 / (12.0 ** 2.0)), 0.0],
142 [0.0, (1.0 / (12.0 ** 2.0))]])
143 self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
Austin Schuh29659232015-11-26 13:55:30 -0800144
Austin Schuhadf2cde2015-11-08 20:35:16 -0800145 print "DT K", name
146 print self.K
147 print numpy.linalg.eig(self.A - self.B * self.K)[0]
Brian Silverman17f503e2015-08-02 18:17:18 -0700148
149 self.hlp = 0.3
150 self.llp = 0.4
151 self.PlaceObserverPoles([self.hlp, self.hlp, self.llp, self.llp])
152
153 self.U_max = numpy.matrix([[12.0], [12.0]])
154 self.U_min = numpy.matrix([[-12.0], [-12.0]])
155 self.InitializeState()
156
Austin Schuh572ff402015-11-08 12:17:50 -0800157
158class KFDrivetrain(Drivetrain):
159 def __init__(self, name="KFDrivetrain", left_low=True, right_low=True):
160 super(KFDrivetrain, self).__init__(name, left_low, right_low)
161
162 self.unaugmented_A_continuous = self.A_continuous
163 self.unaugmented_B_continuous = self.B_continuous
164
165 # The states are
166 # The practical voltage applied to the wheels is
167 # V_left = U_left + left_voltage_error
168 #
169 # [left position, left velocity, right position, right velocity,
170 # left voltage error, right voltage error, angular_error]
171 self.A_continuous = numpy.matrix(numpy.zeros((7, 7)))
172 self.B_continuous = numpy.matrix(numpy.zeros((7, 2)))
173 self.A_continuous[0:4,0:4] = self.unaugmented_A_continuous
174 self.A_continuous[0:4,4:6] = self.unaugmented_B_continuous
175 self.B_continuous[0:4,0:2] = self.unaugmented_B_continuous
176
177 self.A, self.B = self.ContinuousToDiscrete(
178 self.A_continuous, self.B_continuous, self.dt)
179
180 self.C = numpy.matrix([[1, 0, 0, 0, 0, 0, self.rb],
181 [0, 0, 1, 0, 0, 0, -self.rb],
182 [0, -2.0 / self.rb, 0, 2.0 / self.rb, 0, 0, 0]])
183
184 self.D = numpy.matrix([[0, 0],
185 [0, 0],
186 [0, 0]])
187
188 q_pos = 0.08
189 q_vel = 0.40
190 q_voltage = 6.0
191 q_gyro = 0.1
192
193 self.Q = numpy.matrix([[(q_pos ** 2.0), 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
194 [0.0, (q_vel ** 2.0), 0.0, 0.0, 0.0, 0.0, 0.0],
195 [0.0, 0.0, (q_pos ** 2.0), 0.0, 0.0, 0.0, 0.0],
196 [0.0, 0.0, 0.0, (q_vel ** 2.0), 0.0, 0.0, 0.0],
197 [0.0, 0.0, 0.0, 0.0, (q_voltage ** 2.0), 0.0, 0.0],
198 [0.0, 0.0, 0.0, 0.0, 0.0, (q_voltage ** 2.0), 0.0],
199 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, (q_gyro ** 2.0)]])
200
201 r_pos = 0.05
202 r_gyro = 0.001
203 self.R = numpy.matrix([[(r_pos ** 2.0), 0.0, 0.0],
204 [0.0, (r_pos ** 2.0), 0.0],
205 [0.0, 0.0, (r_gyro ** 2.0)]])
206
207 # Solving for kf gains.
208 self.KalmanGain, self.Q_steady = controls.kalman(
209 A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R)
210
211 self.L = self.A * self.KalmanGain
212
213 # We need a nothing controller for the autogen code to be happy.
214 self.K = numpy.matrix(numpy.zeros((self.B.shape[1], self.A.shape[0])))
215
216
Brian Silverman17f503e2015-08-02 18:17:18 -0700217def main(argv):
Austin Schuh29659232015-11-26 13:55:30 -0800218 parser = argparse.ArgumentParser(description='Calculate drivetrain.')
219 parser.add_argument('--plot', action='store_true', default=False, help='If true, plot')
220 parser.add_argument('drivetrainh')
221 parser.add_argument('drivetraincc')
222 parser.add_argument('kalman_drivetrainh')
223 parser.add_argument('kalman_drivetraincc')
224
225 args = parser.parse_args(argv[1:])
226
Brian Silverman17f503e2015-08-02 18:17:18 -0700227 # Simulate the response of the system to a step input.
228 drivetrain = Drivetrain()
229 simulated_left = []
230 simulated_right = []
231 for _ in xrange(100):
232 drivetrain.Update(numpy.matrix([[12.0], [12.0]]))
233 simulated_left.append(drivetrain.X[0, 0])
234 simulated_right.append(drivetrain.X[2, 0])
235
Austin Schuh29659232015-11-26 13:55:30 -0800236 if args.plot:
237 pylab.plot(range(100), simulated_left)
238 pylab.plot(range(100), simulated_right)
239 pylab.show()
Brian Silverman17f503e2015-08-02 18:17:18 -0700240
241 # Simulate forwards motion.
242 drivetrain = Drivetrain()
243 close_loop_left = []
244 close_loop_right = []
245 R = numpy.matrix([[1.0], [0.0], [1.0], [0.0]])
246 for _ in xrange(100):
247 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
248 drivetrain.U_min, drivetrain.U_max)
249 drivetrain.UpdateObserver(U)
250 drivetrain.Update(U)
251 close_loop_left.append(drivetrain.X[0, 0])
252 close_loop_right.append(drivetrain.X[2, 0])
253
Austin Schuh29659232015-11-26 13:55:30 -0800254 if args.plot:
255 pylab.plot(range(100), close_loop_left)
256 pylab.plot(range(100), close_loop_right)
257 pylab.show()
Brian Silverman17f503e2015-08-02 18:17:18 -0700258
259 # Try turning in place
260 drivetrain = Drivetrain()
261 close_loop_left = []
262 close_loop_right = []
263 R = numpy.matrix([[-1.0], [0.0], [1.0], [0.0]])
264 for _ in xrange(100):
265 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
266 drivetrain.U_min, drivetrain.U_max)
267 drivetrain.UpdateObserver(U)
268 drivetrain.Update(U)
269 close_loop_left.append(drivetrain.X[0, 0])
270 close_loop_right.append(drivetrain.X[2, 0])
271
Austin Schuh29659232015-11-26 13:55:30 -0800272 if args.plot:
273 pylab.plot(range(100), close_loop_left)
274 pylab.plot(range(100), close_loop_right)
275 pylab.show()
Brian Silverman17f503e2015-08-02 18:17:18 -0700276
277 # Try turning just one side.
278 drivetrain = Drivetrain()
279 close_loop_left = []
280 close_loop_right = []
281 R = numpy.matrix([[0.0], [0.0], [1.0], [0.0]])
282 for _ in xrange(100):
283 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
284 drivetrain.U_min, drivetrain.U_max)
285 drivetrain.UpdateObserver(U)
286 drivetrain.Update(U)
287 close_loop_left.append(drivetrain.X[0, 0])
288 close_loop_right.append(drivetrain.X[2, 0])
289
Austin Schuh29659232015-11-26 13:55:30 -0800290 if args.plot:
291 pylab.plot(range(100), close_loop_left)
292 pylab.plot(range(100), close_loop_right)
293 pylab.show()
Brian Silverman17f503e2015-08-02 18:17:18 -0700294
295 # Write the generated constants out to a file.
Austin Schuh29659232015-11-26 13:55:30 -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)
Brian Silverman17f503e2015-08-02 18:17:18 -0700304
Austin Schuh29659232015-11-26 13:55:30 -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)
Austin Schuh572ff402015-11-08 12:17:50 -0800313
Brian Silverman17f503e2015-08-02 18:17:18 -0700314 if len(argv) != 5:
315 print "Expected .h file name and .cc file name"
316 else:
Austin Schuh572ff402015-11-08 12:17:50 -0800317 namespaces = ['y2014', 'control_loops', 'drivetrain']
Brian Silverman17f503e2015-08-02 18:17:18 -0700318 dog_loop_writer = control_loop.ControlLoopWriter(
319 "Drivetrain", [drivetrain_low_low, drivetrain_low_high,
Austin Schuh572ff402015-11-08 12:17:50 -0800320 drivetrain_high_low, drivetrain_high_high],
321 namespaces = namespaces)
Austin Schuh0e997732015-11-08 15:14:53 -0800322 dog_loop_writer.AddConstant(control_loop.Constant("kDt", "%f",
323 drivetrain_low_low.dt))
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800324 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
Austin Schuh29659232015-11-26 13:55:30 -0800347 dog_loop_writer.Write(args.drivetrainh, args.drivetraincc)
Brian Silverman17f503e2015-08-02 18:17:18 -0700348
Austin Schuh572ff402015-11-08 12:17:50 -0800349 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)
Austin Schuh29659232015-11-26 13:55:30 -0800353 kf_loop_writer.Write(args.kalman_drivetrainh, args.kalman_drivetraincc)
Austin Schuh572ff402015-11-08 12:17:50 -0800354
Brian Silverman17f503e2015-08-02 18:17:18 -0700355if __name__ == '__main__':
356 sys.exit(main(sys.argv))