blob: a9003aa3414ba29a289bfeeaa21e4cef62580793 [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
7from matplotlib import pylab
8
9
10class CIM(control_loop.ControlLoop):
11 def __init__(self):
12 super(CIM, self).__init__("CIM")
13 # Stall Torque in N m
14 self.stall_torque = 2.42
15 # Stall Current in Amps
16 self.stall_current = 133
17 # Free Speed in RPM
18 self.free_speed = 4650.0
19 # Free Current in Amps
20 self.free_current = 2.7
21 # Moment of inertia of the CIM in kg m^2
22 self.J = 0.0001
23 # Resistance of the motor, divided by 2 to account for the 2 motors
Austin Schuh96ce8ae2015-11-26 12:46:02 -080024 self.resistance = 12.0 / self.stall_current
Brian Silverman17f503e2015-08-02 18:17:18 -070025 # Motor velocity constant
26 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
Austin Schuh96ce8ae2015-11-26 12:46:02 -080027 (12.0 - self.resistance * self.free_current))
Brian Silverman17f503e2015-08-02 18:17:18 -070028 # Torque constant
29 self.Kt = self.stall_torque / self.stall_current
30 # Control loop time step
Austin Schuhadf2cde2015-11-08 20:35:16 -080031 self.dt = 0.005
Brian Silverman17f503e2015-08-02 18:17:18 -070032
33 # State feedback matrices
34 self.A_continuous = numpy.matrix(
Austin Schuh96ce8ae2015-11-26 12:46:02 -080035 [[-self.Kt / self.Kv / (self.J * self.resistance)]])
Brian Silverman17f503e2015-08-02 18:17:18 -070036 self.B_continuous = numpy.matrix(
Austin Schuh96ce8ae2015-11-26 12:46:02 -080037 [[self.Kt / (self.J * self.resistance)]])
Brian Silverman17f503e2015-08-02 18:17:18 -070038 self.C = numpy.matrix([[1]])
39 self.D = numpy.matrix([[0]])
40
41 self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
42 self.B_continuous, self.dt)
43
44 self.PlaceControllerPoles([0.01])
45 self.PlaceObserverPoles([0.01])
46
47 self.U_max = numpy.matrix([[12.0]])
48 self.U_min = numpy.matrix([[-12.0]])
49
50 self.InitializeState()
51
52
53class Drivetrain(control_loop.ControlLoop):
54 def __init__(self, name="Drivetrain", left_low=True, right_low=True):
55 super(Drivetrain, self).__init__(name)
Austin Schuh96ce8ae2015-11-26 12:46:02 -080056 # Number of motors per side
57 self.num_motors = 2
Brian Silverman17f503e2015-08-02 18:17:18 -070058 # Stall Torque in N m
Austin Schuh96ce8ae2015-11-26 12:46:02 -080059 self.stall_torque = 2.42 * self.num_motors
Brian Silverman17f503e2015-08-02 18:17:18 -070060 # Stall Current in Amps
Austin Schuh96ce8ae2015-11-26 12:46:02 -080061 self.stall_current = 133.0 * self.num_motors
Brian Silverman17f503e2015-08-02 18:17:18 -070062 # Free Speed in RPM. Used number from last year.
63 self.free_speed = 4650.0
64 # Free Current in Amps
Austin Schuh96ce8ae2015-11-26 12:46:02 -080065 self.free_current = 2.7 * self.num_motors
Brian Silverman17f503e2015-08-02 18:17:18 -070066 # Moment of inertia of the drivetrain in kg m^2
67 # Just borrowed from last year.
68 self.J = 4.5
69 # Mass of the robot, in kg.
70 self.m = 68
71 # Radius of the robot, in meters (from last year).
72 self.rb = 0.617998644 / 2.0
73 # Radius of the wheels, in meters.
74 self.r = .04445
75 # Resistance of the motor, divided by the number of motors.
Austin Schuh96ce8ae2015-11-26 12:46:02 -080076 self.resistance = 12.0 / self.stall_current
Brian Silverman17f503e2015-08-02 18:17:18 -070077 # Motor velocity constant
78 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
Austin Schuh96ce8ae2015-11-26 12:46:02 -080079 (12.0 - self.resistance * self.free_current))
Brian Silverman17f503e2015-08-02 18:17:18 -070080 # Torque constant
81 self.Kt = self.stall_torque / self.stall_current
82 # Gear ratios
83 self.G_low = 18.0 / 60.0 * 18.0 / 50.0
84 self.G_high = 28.0 / 50.0 * 18.0 / 50.0
85 if left_low:
86 self.Gl = self.G_low
87 else:
88 self.Gl = self.G_high
89 if right_low:
90 self.Gr = self.G_low
91 else:
92 self.Gr = self.G_high
93
94 # Control loop time step
Austin Schuhadf2cde2015-11-08 20:35:16 -080095 self.dt = 0.005
Brian Silverman17f503e2015-08-02 18:17:18 -070096
97 # These describe the way that a given side of a robot will be influenced
98 # by the other side. Units of 1 / kg.
99 self.msp = 1.0 / self.m + self.rb * self.rb / self.J
100 self.msn = 1.0 / self.m - self.rb * self.rb / self.J
101 # The calculations which we will need for A and B.
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800102 self.tcl = -self.Kt / self.Kv / (self.Gl * self.Gl * self.resistance * self.r * self.r)
103 self.tcr = -self.Kt / self.Kv / (self.Gr * self.Gr * self.resistance * self.r * self.r)
104 self.mpl = self.Kt / (self.Gl * self.resistance * self.r)
105 self.mpr = self.Kt / (self.Gr * self.resistance * self.r)
Brian Silverman17f503e2015-08-02 18:17:18 -0700106
107 # State feedback matrices
108 # X will be of the format
109 # [[positionl], [velocityl], [positionr], velocityr]]
110 self.A_continuous = numpy.matrix(
111 [[0, 1, 0, 0],
112 [0, self.msp * self.tcl, 0, self.msn * self.tcr],
113 [0, 0, 0, 1],
114 [0, self.msn * self.tcl, 0, self.msp * self.tcr]])
115 self.B_continuous = numpy.matrix(
116 [[0, 0],
117 [self.msp * self.mpl, self.msn * self.mpr],
118 [0, 0],
119 [self.msn * self.mpl, self.msp * self.mpr]])
120 self.C = numpy.matrix([[1, 0, 0, 0],
121 [0, 0, 1, 0]])
122 self.D = numpy.matrix([[0, 0],
123 [0, 0]])
124
Brian Silverman17f503e2015-08-02 18:17:18 -0700125 self.A, self.B = self.ContinuousToDiscrete(
126 self.A_continuous, self.B_continuous, self.dt)
127
128 # Poles from last year.
129 self.hp = 0.65
130 self.lp = 0.83
131 self.PlaceControllerPoles([self.hp, self.lp, self.hp, self.lp])
Austin Schuh572ff402015-11-08 12:17:50 -0800132 #print self.K
Austin Schuhadf2cde2015-11-08 20:35:16 -0800133 q_pos = 0.12
Brian Silverman17f503e2015-08-02 18:17:18 -0700134 q_vel = 1.0
135 self.Q = numpy.matrix([[(1.0 / (q_pos ** 2.0)), 0.0, 0.0, 0.0],
136 [0.0, (1.0 / (q_vel ** 2.0)), 0.0, 0.0],
137 [0.0, 0.0, (1.0 / (q_pos ** 2.0)), 0.0],
138 [0.0, 0.0, 0.0, (1.0 / (q_vel ** 2.0))]])
139
140 self.R = numpy.matrix([[(1.0 / (12.0 ** 2.0)), 0.0],
141 [0.0, (1.0 / (12.0 ** 2.0))]])
142 self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
Austin Schuh572ff402015-11-08 12:17:50 -0800143 #print self.A
144 #print self.B
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):
218 # Simulate the response of the system to a step input.
219 drivetrain = Drivetrain()
220 simulated_left = []
221 simulated_right = []
222 for _ in xrange(100):
223 drivetrain.Update(numpy.matrix([[12.0], [12.0]]))
224 simulated_left.append(drivetrain.X[0, 0])
225 simulated_right.append(drivetrain.X[2, 0])
226
227 #pylab.plot(range(100), simulated_left)
228 #pylab.plot(range(100), simulated_right)
229 #pylab.show()
230
231 # Simulate forwards motion.
232 drivetrain = Drivetrain()
233 close_loop_left = []
234 close_loop_right = []
235 R = numpy.matrix([[1.0], [0.0], [1.0], [0.0]])
236 for _ in xrange(100):
237 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
238 drivetrain.U_min, drivetrain.U_max)
239 drivetrain.UpdateObserver(U)
240 drivetrain.Update(U)
241 close_loop_left.append(drivetrain.X[0, 0])
242 close_loop_right.append(drivetrain.X[2, 0])
243
244 #pylab.plot(range(100), close_loop_left)
245 #pylab.plot(range(100), close_loop_right)
246 #pylab.show()
247
248 # Try turning in place
249 drivetrain = Drivetrain()
250 close_loop_left = []
251 close_loop_right = []
252 R = numpy.matrix([[-1.0], [0.0], [1.0], [0.0]])
253 for _ in xrange(100):
254 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
255 drivetrain.U_min, drivetrain.U_max)
256 drivetrain.UpdateObserver(U)
257 drivetrain.Update(U)
258 close_loop_left.append(drivetrain.X[0, 0])
259 close_loop_right.append(drivetrain.X[2, 0])
260
261 #pylab.plot(range(100), close_loop_left)
262 #pylab.plot(range(100), close_loop_right)
263 #pylab.show()
264
265 # Try turning just one side.
266 drivetrain = Drivetrain()
267 close_loop_left = []
268 close_loop_right = []
269 R = numpy.matrix([[0.0], [0.0], [1.0], [0.0]])
270 for _ in xrange(100):
271 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
272 drivetrain.U_min, drivetrain.U_max)
273 drivetrain.UpdateObserver(U)
274 drivetrain.Update(U)
275 close_loop_left.append(drivetrain.X[0, 0])
276 close_loop_right.append(drivetrain.X[2, 0])
277
278 #pylab.plot(range(100), close_loop_left)
279 #pylab.plot(range(100), close_loop_right)
280 #pylab.show()
281
282 # Write the generated constants out to a file.
Brian Silverman17f503e2015-08-02 18:17:18 -0700283 drivetrain_low_low = Drivetrain(name="DrivetrainLowLow", left_low=True, right_low=True)
284 drivetrain_low_high = Drivetrain(name="DrivetrainLowHigh", left_low=True, right_low=False)
285 drivetrain_high_low = Drivetrain(name="DrivetrainHighLow", left_low=False, right_low=True)
286 drivetrain_high_high = Drivetrain(name="DrivetrainHighHigh", left_low=False, right_low=False)
287
Austin Schuh572ff402015-11-08 12:17:50 -0800288 kf_drivetrain_low_low = KFDrivetrain(name="KFDrivetrainLowLow", left_low=True, right_low=True)
289 kf_drivetrain_low_high = KFDrivetrain(name="KFDrivetrainLowHigh", left_low=True, right_low=False)
290 kf_drivetrain_high_low = KFDrivetrain(name="KFDrivetrainHighLow", left_low=False, right_low=True)
291 kf_drivetrain_high_high = KFDrivetrain(name="KFDrivetrainHighHigh", left_low=False, right_low=False)
292
Brian Silverman17f503e2015-08-02 18:17:18 -0700293 if len(argv) != 5:
294 print "Expected .h file name and .cc file name"
295 else:
Austin Schuh572ff402015-11-08 12:17:50 -0800296 namespaces = ['y2014', 'control_loops', 'drivetrain']
Brian Silverman17f503e2015-08-02 18:17:18 -0700297 dog_loop_writer = control_loop.ControlLoopWriter(
298 "Drivetrain", [drivetrain_low_low, drivetrain_low_high,
Austin Schuh572ff402015-11-08 12:17:50 -0800299 drivetrain_high_low, drivetrain_high_high],
300 namespaces = namespaces)
Austin Schuh0e997732015-11-08 15:14:53 -0800301 dog_loop_writer.AddConstant(control_loop.Constant("kDt", "%f",
302 drivetrain_low_low.dt))
Austin Schuh96ce8ae2015-11-26 12:46:02 -0800303 dog_loop_writer.AddConstant(control_loop.Constant("kStallTorque", "%f",
304 drivetrain_low_low.stall_torque))
305 dog_loop_writer.AddConstant(control_loop.Constant("kStallCurrent", "%f",
306 drivetrain_low_low.stall_current))
307 dog_loop_writer.AddConstant(control_loop.Constant("kFreeSpeedRPM", "%f",
308 drivetrain_low_low.free_speed))
309 dog_loop_writer.AddConstant(control_loop.Constant("kFreeCurrent", "%f",
310 drivetrain_low_low.free_current))
311 dog_loop_writer.AddConstant(control_loop.Constant("kJ", "%f",
312 drivetrain_low_low.J))
313 dog_loop_writer.AddConstant(control_loop.Constant("kMass", "%f",
314 drivetrain_low_low.m))
315 dog_loop_writer.AddConstant(control_loop.Constant("kRobotRadius", "%f",
316 drivetrain_low_low.rb))
317 dog_loop_writer.AddConstant(control_loop.Constant("kWheelRadius", "%f",
318 drivetrain_low_low.r))
319 dog_loop_writer.AddConstant(control_loop.Constant("kR", "%f",
320 drivetrain_low_low.resistance))
321 dog_loop_writer.AddConstant(control_loop.Constant("kV", "%f",
322 drivetrain_low_low.Kv))
323 dog_loop_writer.AddConstant(control_loop.Constant("kT", "%f",
324 drivetrain_low_low.Kt))
325
Brian Silverman17f503e2015-08-02 18:17:18 -0700326 if argv[1][-3:] == '.cc':
327 dog_loop_writer.Write(argv[2], argv[1])
328 else:
329 dog_loop_writer.Write(argv[1], argv[2])
330
Austin Schuh572ff402015-11-08 12:17:50 -0800331 kf_loop_writer = control_loop.ControlLoopWriter(
332 "KFDrivetrain", [kf_drivetrain_low_low, kf_drivetrain_low_high,
333 kf_drivetrain_high_low, kf_drivetrain_high_high],
334 namespaces = namespaces)
335 if argv[3][-3:] == '.cc':
336 kf_loop_writer.Write(argv[4], argv[3])
337 else:
338 kf_loop_writer.Write(argv[3], argv[4])
339
Brian Silverman17f503e2015-08-02 18:17:18 -0700340if __name__ == '__main__':
341 sys.exit(main(sys.argv))