blob: c59cbab6570d8c68a367dc4ea2fc0210663eba44 [file] [log] [blame]
Campbell Crowley33e0e3d2017-12-27 17:55:40 -08001#!/usr/bin/python
2
3from frc971.control_loops.python import control_loop
4from frc971.control_loops.python import controls
5import numpy
6import sys
7from matplotlib import pylab
8import glog
9
10class DrivetrainParams(object):
Diana Burgessd0180f12018-03-21 21:24:17 -070011 def __init__(self,
12 J,
13 mass,
14 robot_radius,
15 wheel_radius,
Austin Schuhecc92a02019-01-20 17:42:19 -080016 G=None,
17 G_high=None,
18 G_low=None,
19 q_pos=None,
20 q_pos_low=None,
21 q_pos_high=None,
22 q_vel=None,
23 q_vel_low=None,
24 q_vel_high=None,
Diana Burgessd0180f12018-03-21 21:24:17 -070025 efficiency=0.60,
26 has_imu=False,
27 force=False,
28 kf_q_voltage=10.0,
29 motor_type=control_loop.CIM(),
30 num_motors=2,
31 dt=0.00505,
32 controller_poles=[0.90, 0.90],
Michael Schuh95fbcc52018-03-10 20:57:20 -080033 observer_poles=[0.02, 0.02],
34 robot_cg_offset=0.0):
Diana Burgessd0180f12018-03-21 21:24:17 -070035 """Defines all constants of a drivetrain.
Campbell Crowley33e0e3d2017-12-27 17:55:40 -080036
Diana Burgessd0180f12018-03-21 21:24:17 -070037 Args:
38 J: float, Moment of inertia of drivetrain in kg m^2
39 mass: float, Mass of the robot in kg.
40 robot_radius: float, Radius of the robot, in meters (requires tuning by
41 hand).
42 wheel_radius: float, Radius of the wheels, in meters.
Austin Schuhecc92a02019-01-20 17:42:19 -080043 G: float, Gear ratio for a single speed.
Diana Burgessd0180f12018-03-21 21:24:17 -070044 G_high: float, Gear ratio for high gear.
45 G_low: float, Gear ratio for low gear.
46 dt: float, Control loop time step.
Austin Schuhecc92a02019-01-20 17:42:19 -080047 q_pos: float, q position for a single speed.
Diana Burgessd0180f12018-03-21 21:24:17 -070048 q_pos_low: float, q position low gear.
49 q_pos_high: float, q position high gear.
Austin Schuhecc92a02019-01-20 17:42:19 -080050 q_vel: float, q velocity for a single speed
Diana Burgessd0180f12018-03-21 21:24:17 -070051 q_vel_low: float, q velocity low gear.
52 q_vel_high: float, q velocity high gear.
53 efficiency: float, gear box effiency.
54 has_imu: bool, true if imu is present.
55 force: bool, true if force.
56 kf_q_voltage: float
57 motor_type: object, class of values defining the motor in drivetrain.
58 num_motors: int, number of motors on one side of drivetrain.
59 controller_poles: array, An array of poles. (See control_loop.py)
60 observer_poles: array, An array of poles. (See control_loop.py)
Michael Schuh95fbcc52018-03-10 20:57:20 -080061 robot_cg_offset: offset in meters of CG from robot center to left side
Diana Burgessd0180f12018-03-21 21:24:17 -070062 """
Austin Schuhecc92a02019-01-20 17:42:19 -080063 if G is not None:
64 assert (G_high is None)
65 assert (G_low is None)
66 G_high = G
67 G_low = G
68 assert (G_high is not None)
69 assert (G_low is not None)
70
71 if q_pos is not None:
72 assert (q_pos_low is None)
73 assert (q_pos_high is None)
74 q_pos_low = q_pos
75 q_pos_high = q_pos
76 assert (q_pos_low is not None)
77 assert (q_pos_high is not None)
78
79 if q_vel is not None:
80 assert (q_vel_low is None)
81 assert (q_vel_high is None)
82 q_vel_low = q_vel
83 q_vel_high = q_vel
84 assert (q_vel_low is not None)
85 assert (q_vel_high is not None)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -080086
Diana Burgessd0180f12018-03-21 21:24:17 -070087 self.J = J
88 self.mass = mass
89 self.robot_radius = robot_radius
Michael Schuh95fbcc52018-03-10 20:57:20 -080090 self.robot_cg_offset = robot_cg_offset
Diana Burgessd0180f12018-03-21 21:24:17 -070091 self.wheel_radius = wheel_radius
92 self.G_high = G_high
93 self.G_low = G_low
94 self.dt = dt
95 self.q_pos_low = q_pos_low
96 self.q_pos_high = q_pos_high
97 self.q_vel_low = q_vel_low
98 self.q_vel_high = q_vel_high
99 self.efficiency = efficiency
100 self.has_imu = has_imu
101 self.kf_q_voltage = kf_q_voltage
102 self.motor_type = motor_type
103 self.force = force
104 self.num_motors = num_motors
105 self.controller_poles = controller_poles
106 self.observer_poles = observer_poles
107
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800108
109class Drivetrain(control_loop.ControlLoop):
Diana Burgessd0180f12018-03-21 21:24:17 -0700110 def __init__(self,
111 drivetrain_params,
112 name="Drivetrain",
113 left_low=True,
114 right_low=True):
115 """Defines a base drivetrain for a robot.
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800116
Diana Burgessd0180f12018-03-21 21:24:17 -0700117 Args:
118 drivetrain_params: DrivetrainParams, class of values defining the drivetrain.
119 name: string, Name of this drivetrain.
120 left_low: bool, Whether the left is in high gear.
121 right_low: bool, Whether the right is in high gear.
122 """
123 super(Drivetrain, self).__init__(name)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800124
Diana Burgessd0180f12018-03-21 21:24:17 -0700125 # Moment of inertia of the drivetrain in kg m^2
126 self.J = drivetrain_params.J
127 # Mass of the robot, in kg.
128 self.mass = drivetrain_params.mass
129 # Radius of the robot, in meters (requires tuning by hand)
130 self.robot_radius = drivetrain_params.robot_radius
131 # Radius of the wheels, in meters.
132 self.r = drivetrain_params.wheel_radius
133 self.has_imu = drivetrain_params.has_imu
Michael Schuh95fbcc52018-03-10 20:57:20 -0800134 # Offset in meters of the CG from the center of the robot to the left side
135 # of the robot. Since the arm is on the right side, the offset will
136 # likely be a negative number.
137 self.robot_cg_offset = drivetrain_params.robot_cg_offset
138 # Distance from the left side of the robot to the Center of Gravity
139 self.robot_radius_l = drivetrain_params.robot_radius - self.robot_cg_offset
140 # Distance from the right side of the robot to the Center of Gravity
141 self.robot_radius_r = drivetrain_params.robot_radius + self.robot_cg_offset
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800142
Diana Burgessd0180f12018-03-21 21:24:17 -0700143 # Gear ratios
144 self.G_low = drivetrain_params.G_low
145 self.G_high = drivetrain_params.G_high
146 if left_low:
147 self.Gl = self.G_low
148 else:
149 self.Gl = self.G_high
150 if right_low:
151 self.Gr = self.G_low
152 else:
153 self.Gr = self.G_high
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800154
Diana Burgessd0180f12018-03-21 21:24:17 -0700155 # Control loop time step
156 self.dt = drivetrain_params.dt
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800157
Diana Burgessd0180f12018-03-21 21:24:17 -0700158 self.efficiency = drivetrain_params.efficiency
159 self.force = drivetrain_params.force
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800160
Diana Burgessd0180f12018-03-21 21:24:17 -0700161 self.BuildDrivetrain(drivetrain_params.motor_type,
162 drivetrain_params.num_motors)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800163
Diana Burgessd0180f12018-03-21 21:24:17 -0700164 if left_low or right_low:
165 q_pos = drivetrain_params.q_pos_low
166 q_vel = drivetrain_params.q_vel_low
167 else:
168 q_pos = drivetrain_params.q_pos_high
169 q_vel = drivetrain_params.q_vel_high
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800170
Diana Burgessd0180f12018-03-21 21:24:17 -0700171 self.BuildDrivetrainController(q_pos, q_vel)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800172
Diana Burgessd0180f12018-03-21 21:24:17 -0700173 self.InitializeState()
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800174
Diana Burgessd0180f12018-03-21 21:24:17 -0700175 def BuildDrivetrain(self, motor, num_motors_per_side):
176 self.motor = motor
177 # Number of motors per side
178 self.num_motors = num_motors_per_side
179 # Stall Torque in N m
180 self.stall_torque = motor.stall_torque * self.num_motors * self.efficiency
181 # Stall Current in Amps
182 self.stall_current = motor.stall_current * self.num_motors
183 # Free Speed in rad/s
184 self.free_speed = motor.free_speed
185 # Free Current in Amps
186 self.free_current = motor.free_current * self.num_motors
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800187
Diana Burgessd0180f12018-03-21 21:24:17 -0700188 # Effective motor resistance in ohms.
189 self.resistance = 12.0 / self.stall_current
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800190
Diana Burgessd0180f12018-03-21 21:24:17 -0700191 # Resistance of the motor, divided by the number of motors.
192 # Motor velocity constant
193 self.Kv = (self.free_speed /
194 (12.0 - self.resistance * self.free_current))
195 # Torque constant
196 self.Kt = self.stall_torque / self.stall_current
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800197
Diana Burgessd0180f12018-03-21 21:24:17 -0700198 # These describe the way that a given side of a robot will be influenced
199 # by the other side. Units of 1 / kg.
Michael Schuh95fbcc52018-03-10 20:57:20 -0800200 self.mspl = 1.0 / self.mass + self.robot_radius_l * self.robot_radius_l / self.J
201 self.mspr = 1.0 / self.mass + self.robot_radius_r * self.robot_radius_r / self.J
202 self.msnl = self.robot_radius_r / ( self.robot_radius_l * self.mass ) - \
203 self.robot_radius_l * self.robot_radius_r / self.J
204 self.msnr = self.robot_radius_l / ( self.robot_radius_r * self.mass ) - \
205 self.robot_radius_l * self.robot_radius_r / self.J
Diana Burgessd0180f12018-03-21 21:24:17 -0700206 # The calculations which we will need for A and B.
207 self.tcl = self.Kt / self.Kv / (
208 self.Gl * self.Gl * self.resistance * self.r * self.r)
209 self.tcr = self.Kt / self.Kv / (
210 self.Gr * self.Gr * self.resistance * self.r * self.r)
211 self.mpl = self.Kt / (self.Gl * self.resistance * self.r)
212 self.mpr = self.Kt / (self.Gr * self.resistance * self.r)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800213
Diana Burgessd0180f12018-03-21 21:24:17 -0700214 # State feedback matrices
215 # X will be of the format
216 # [[positionl], [velocityl], [positionr], velocityr]]
217 self.A_continuous = numpy.matrix(
Michael Schuh95fbcc52018-03-10 20:57:20 -0800218 [[0, 1, 0, 0],
219 [0, -self.mspl * self.tcl, 0, -self.msnr * self.tcr],
220 [0, 0, 0, 1],
221 [0, -self.msnl * self.tcl, 0, -self.mspr * self.tcr]])
Diana Burgessd0180f12018-03-21 21:24:17 -0700222 self.B_continuous = numpy.matrix(
Michael Schuh95fbcc52018-03-10 20:57:20 -0800223 [[0, 0],
224 [self.mspl * self.mpl, self.msnr * self.mpr],
225 [0, 0],
226 [self.msnl * self.mpl, self.mspr * self.mpr]])
Diana Burgessd0180f12018-03-21 21:24:17 -0700227 self.C = numpy.matrix([[1, 0, 0, 0], [0, 0, 1, 0]])
228 self.D = numpy.matrix([[0, 0], [0, 0]])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800229
Diana Burgessd0180f12018-03-21 21:24:17 -0700230 self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
231 self.B_continuous, self.dt)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800232
Diana Burgessd0180f12018-03-21 21:24:17 -0700233 def BuildDrivetrainController(self, q_pos, q_vel):
234 # Tune the LQR controller
235 self.Q = numpy.matrix([[(1.0 / (q_pos**2.0)), 0.0, 0.0,
236 0.0], [0.0, (1.0 / (q_vel**2.0)), 0.0, 0.0],
237 [0.0, 0.0, (1.0 / (q_pos**2.0)),
238 0.0], [0.0, 0.0, 0.0, (1.0 / (q_vel**2.0))]])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800239
Diana Burgessd0180f12018-03-21 21:24:17 -0700240 self.R = numpy.matrix([[(1.0 / (12.0**2.0)), 0.0],
241 [0.0, (1.0 / (12.0**2.0))]])
242 self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800243
Diana Burgessd0180f12018-03-21 21:24:17 -0700244 glog.debug('DT q_pos %f q_vel %s %s', q_pos, q_vel, self._name)
245 glog.debug(str(numpy.linalg.eig(self.A - self.B * self.K)[0]))
246 glog.debug('K %s', repr(self.K))
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800247
Diana Burgessd0180f12018-03-21 21:24:17 -0700248 self.hlp = 0.3
249 self.llp = 0.4
250 self.PlaceObserverPoles([self.hlp, self.hlp, self.llp, self.llp])
251
252 self.U_max = numpy.matrix([[12.0], [12.0]])
253 self.U_min = numpy.matrix([[-12.0], [-12.0]])
254
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800255
256class KFDrivetrain(Drivetrain):
Diana Burgessd0180f12018-03-21 21:24:17 -0700257 def __init__(self,
258 drivetrain_params,
259 name="KFDrivetrain",
260 left_low=True,
261 right_low=True):
262 """Kalman filter values of a drivetrain.
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800263
Diana Burgessd0180f12018-03-21 21:24:17 -0700264 Args:
265 drivetrain_params: DrivetrainParams, class of values defining the drivetrain.
266 name: string, Name of this drivetrain.
267 left_low: bool, Whether the left is in high gear.
268 right_low: bool, Whether the right is in high gear.
269 """
270 super(KFDrivetrain, self).__init__(drivetrain_params, name, left_low,
271 right_low)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800272
Diana Burgessd0180f12018-03-21 21:24:17 -0700273 self.unaugmented_A_continuous = self.A_continuous
274 self.unaugmented_B_continuous = self.B_continuous
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800275
Diana Burgessd0180f12018-03-21 21:24:17 -0700276 # The practical voltage applied to the wheels is
277 # V_left = U_left + left_voltage_error
278 #
279 # The states are
280 # [left position, left velocity, right position, right velocity,
281 # left voltage error, right voltage error, angular_error]
282 #
283 # The left and right positions are filtered encoder positions and are not
284 # adjusted for heading error.
285 # The turn velocity as computed by the left and right velocities is
286 # adjusted by the gyro velocity.
287 # The angular_error is the angular velocity error between the wheel speed
288 # and the gyro speed.
289 self.A_continuous = numpy.matrix(numpy.zeros((7, 7)))
290 self.B_continuous = numpy.matrix(numpy.zeros((7, 2)))
291 self.A_continuous[0:4, 0:4] = self.unaugmented_A_continuous
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800292
Diana Burgessd0180f12018-03-21 21:24:17 -0700293 if self.force:
294 self.A_continuous[0:4, 4:6] = numpy.matrix(
Michael Schuh95fbcc52018-03-10 20:57:20 -0800295 [[0.0, 0.0], [self.mspl, self.msnl], [0.0, 0.0],
296 [self.msnr, self.mspr]])
Diana Burgessd0180f12018-03-21 21:24:17 -0700297 q_voltage = drivetrain_params.kf_q_voltage * self.mpl
298 else:
299 self.A_continuous[0:4, 4:6] = self.unaugmented_B_continuous
300 q_voltage = drivetrain_params.kf_q_voltage
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800301
Diana Burgessd0180f12018-03-21 21:24:17 -0700302 self.B_continuous[0:4, 0:2] = self.unaugmented_B_continuous
303 self.A_continuous[0, 6] = 1
304 self.A_continuous[2, 6] = -1
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800305
Diana Burgessd0180f12018-03-21 21:24:17 -0700306 self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
307 self.B_continuous, self.dt)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800308
Diana Burgessd0180f12018-03-21 21:24:17 -0700309 if self.has_imu:
310 self.C = numpy.matrix(
311 [[1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [
312 0, -0.5 / drivetrain_params.robot_radius, 0,
313 0.5 / drivetrain_params.robot_radius, 0, 0, 0
314 ], [0, 0, 0, 0, 0, 0, 0]])
315 gravity = 9.8
316 self.C[3, 0:6] = 0.5 * (
317 self.A_continuous[1, 0:6] + self.A_continuous[3, 0:6]
318 ) / gravity
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800319
Diana Burgessd0180f12018-03-21 21:24:17 -0700320 self.D = numpy.matrix([[0, 0], [0, 0], [0, 0], [
321 0.5 *
322 (self.B_continuous[1, 0] + self.B_continuous[3, 0]) / gravity,
323 0.5 *
324 (self.B_continuous[1, 1] + self.B_continuous[3, 1]) / gravity
325 ]])
326 else:
327 self.C = numpy.matrix(
328 [[1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [
329 0, -0.5 / drivetrain_params.robot_radius, 0,
330 0.5 / drivetrain_params.robot_radius, 0, 0, 0
331 ]])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800332
Diana Burgessd0180f12018-03-21 21:24:17 -0700333 self.D = numpy.matrix([[0, 0], [0, 0], [0, 0]])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800334
Diana Burgessd0180f12018-03-21 21:24:17 -0700335 q_pos = 0.05
336 q_vel = 1.00
337 q_encoder_uncertainty = 2.00
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800338
Diana Burgessd0180f12018-03-21 21:24:17 -0700339 self.Q = numpy.matrix(
340 [[(q_pos**2.0), 0.0, 0.0, 0.0, 0.0, 0.0,
341 0.0], [0.0, (q_vel**2.0), 0.0, 0.0, 0.0, 0.0,
342 0.0], [0.0, 0.0, (q_pos**2.0), 0.0, 0.0, 0.0,
343 0.0], [0.0, 0.0, 0.0, (q_vel**2.0), 0.0, 0.0, 0.0],
344 [0.0, 0.0, 0.0, 0.0, (q_voltage**2.0), 0.0,
345 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, (q_voltage**2.0), 0.0],
346 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, (q_encoder_uncertainty**2.0)]])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800347
Diana Burgessd0180f12018-03-21 21:24:17 -0700348 r_pos = 0.0001
349 r_gyro = 0.000001
350 if self.has_imu:
351 r_accelerometer = 7.0
352 self.R = numpy.matrix([[(r_pos**2.0), 0.0, 0.0, 0.0],
353 [0.0, (r_pos**2.0), 0.0, 0.0],
354 [0.0, 0.0, (r_gyro**2.0), 0.0],
355 [0.0, 0.0, 0.0, (r_accelerometer**2.0)]])
356 else:
357 self.R = numpy.matrix([[(r_pos**2.0), 0.0, 0.0],
358 [0.0, (r_pos**2.0), 0.0],
359 [0.0, 0.0, (r_gyro**2.0)]])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800360
Diana Burgessd0180f12018-03-21 21:24:17 -0700361 # Solving for kf gains.
362 self.KalmanGain, self.Q_steady = controls.kalman(
363 A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800364
James Kuszmaul4d752d52019-02-09 17:27:55 -0800365 # If we don't have an IMU, pad various matrices with zeros so that
366 # we can still have 4 measurement outputs.
Diana Burgessd0180f12018-03-21 21:24:17 -0700367 if not self.has_imu:
Austin Schuhecc92a02019-01-20 17:42:19 -0800368 self.KalmanGain = numpy.hstack((self.KalmanGain, numpy.matrix(numpy.zeros((7, 1)))))
369 self.C = numpy.vstack((self.C, numpy.matrix(numpy.zeros((1, 7)))))
370 self.D = numpy.vstack((self.D, numpy.matrix(numpy.zeros((1, 2)))))
James Kuszmaul4d752d52019-02-09 17:27:55 -0800371 Rtmp = numpy.zeros((4, 4))
372 Rtmp[0:3, 0:3] = self.R
373 self.R = Rtmp
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800374
Diana Burgessd0180f12018-03-21 21:24:17 -0700375 self.L = self.A * self.KalmanGain
376
377 unaug_K = self.K
378
379 # Implement a nice closed loop controller for use by the closed loop
380 # controller.
381 self.K = numpy.matrix(numpy.zeros((self.B.shape[1], self.A.shape[0])))
382 self.K[0:2, 0:4] = unaug_K
383 if self.force:
384 self.K[0, 4] = 1.0 / self.mpl
385 self.K[1, 5] = 1.0 / self.mpr
386 else:
387 self.K[0, 4] = 1.0
388 self.K[1, 5] = 1.0
389
390 self.Qff = numpy.matrix(numpy.zeros((4, 4)))
391 qff_pos = 0.005
392 qff_vel = 1.00
393 self.Qff[0, 0] = 1.0 / qff_pos**2.0
394 self.Qff[1, 1] = 1.0 / qff_vel**2.0
395 self.Qff[2, 2] = 1.0 / qff_pos**2.0
396 self.Qff[3, 3] = 1.0 / qff_vel**2.0
397 self.Kff = numpy.matrix(numpy.zeros((2, 7)))
398 self.Kff[0:2, 0:4] = controls.TwoStateFeedForwards(
399 self.B[0:4, :], self.Qff)
400
401 self.InitializeState()
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800402
403
404def WriteDrivetrain(drivetrain_files, kf_drivetrain_files, year_namespace,
Austin Schuhbcce26a2018-03-26 23:41:24 -0700405 drivetrain_params, scalar_type='double'):
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800406
Diana Burgessd0180f12018-03-21 21:24:17 -0700407 # Write the generated constants out to a file.
408 drivetrain_low_low = Drivetrain(
409 name="DrivetrainLowLow",
410 left_low=True,
411 right_low=True,
412 drivetrain_params=drivetrain_params)
413 drivetrain_low_high = Drivetrain(
414 name="DrivetrainLowHigh",
415 left_low=True,
416 right_low=False,
417 drivetrain_params=drivetrain_params)
418 drivetrain_high_low = Drivetrain(
419 name="DrivetrainHighLow",
420 left_low=False,
421 right_low=True,
422 drivetrain_params=drivetrain_params)
423 drivetrain_high_high = Drivetrain(
424 name="DrivetrainHighHigh",
425 left_low=False,
426 right_low=False,
427 drivetrain_params=drivetrain_params)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800428
Diana Burgessd0180f12018-03-21 21:24:17 -0700429 kf_drivetrain_low_low = KFDrivetrain(
430 name="KFDrivetrainLowLow",
431 left_low=True,
432 right_low=True,
433 drivetrain_params=drivetrain_params)
434 kf_drivetrain_low_high = KFDrivetrain(
435 name="KFDrivetrainLowHigh",
436 left_low=True,
437 right_low=False,
438 drivetrain_params=drivetrain_params)
439 kf_drivetrain_high_low = KFDrivetrain(
440 name="KFDrivetrainHighLow",
441 left_low=False,
442 right_low=True,
443 drivetrain_params=drivetrain_params)
444 kf_drivetrain_high_high = KFDrivetrain(
445 name="KFDrivetrainHighHigh",
446 left_low=False,
447 right_low=False,
448 drivetrain_params=drivetrain_params)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800449
Austin Schuhbcce26a2018-03-26 23:41:24 -0700450 if isinstance(year_namespace, list):
Austin Schuhecc92a02019-01-20 17:42:19 -0800451 namespaces = year_namespace
Austin Schuhbcce26a2018-03-26 23:41:24 -0700452 else:
Austin Schuhecc92a02019-01-20 17:42:19 -0800453 namespaces = [year_namespace, 'control_loops', 'drivetrain']
Diana Burgessd0180f12018-03-21 21:24:17 -0700454 dog_loop_writer = control_loop.ControlLoopWriter(
455 "Drivetrain", [
456 drivetrain_low_low, drivetrain_low_high, drivetrain_high_low,
457 drivetrain_high_high
458 ],
Austin Schuhbcce26a2018-03-26 23:41:24 -0700459 namespaces=namespaces,
460 scalar_type=scalar_type)
Diana Burgessd0180f12018-03-21 21:24:17 -0700461 dog_loop_writer.AddConstant(
462 control_loop.Constant("kDt", "%f", drivetrain_low_low.dt))
463 dog_loop_writer.AddConstant(
464 control_loop.Constant("kStallTorque", "%f",
465 drivetrain_low_low.stall_torque))
466 dog_loop_writer.AddConstant(
467 control_loop.Constant("kStallCurrent", "%f",
468 drivetrain_low_low.stall_current))
469 dog_loop_writer.AddConstant(
470 control_loop.Constant("kFreeSpeed", "%f",
471 drivetrain_low_low.free_speed))
472 dog_loop_writer.AddConstant(
473 control_loop.Constant("kFreeCurrent", "%f",
474 drivetrain_low_low.free_current))
475 dog_loop_writer.AddConstant(
476 control_loop.Constant("kJ", "%f", drivetrain_low_low.J))
477 dog_loop_writer.AddConstant(
478 control_loop.Constant("kMass", "%f", drivetrain_low_low.mass))
479 dog_loop_writer.AddConstant(
480 control_loop.Constant("kRobotRadius", "%f",
481 drivetrain_low_low.robot_radius))
482 dog_loop_writer.AddConstant(
483 control_loop.Constant("kWheelRadius", "%f", drivetrain_low_low.r))
484 dog_loop_writer.AddConstant(
485 control_loop.Constant("kR", "%f", drivetrain_low_low.resistance))
486 dog_loop_writer.AddConstant(
487 control_loop.Constant("kV", "%f", drivetrain_low_low.Kv))
488 dog_loop_writer.AddConstant(
489 control_loop.Constant("kT", "%f", drivetrain_low_low.Kt))
490 dog_loop_writer.AddConstant(
491 control_loop.Constant("kLowGearRatio", "%f", drivetrain_low_low.G_low))
492 dog_loop_writer.AddConstant(
493 control_loop.Constant("kHighGearRatio", "%f",
494 drivetrain_high_high.G_high))
495 dog_loop_writer.AddConstant(
496 control_loop.Constant(
497 "kHighOutputRatio", "%f",
498 drivetrain_high_high.G_high * drivetrain_high_high.r))
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800499
Diana Burgessd0180f12018-03-21 21:24:17 -0700500 dog_loop_writer.Write(drivetrain_files[0], drivetrain_files[1])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800501
Diana Burgessd0180f12018-03-21 21:24:17 -0700502 kf_loop_writer = control_loop.ControlLoopWriter(
503 "KFDrivetrain", [
504 kf_drivetrain_low_low, kf_drivetrain_low_high,
505 kf_drivetrain_high_low, kf_drivetrain_high_high
506 ],
Austin Schuhbcce26a2018-03-26 23:41:24 -0700507 namespaces=namespaces,
508 scalar_type=scalar_type)
Diana Burgessd0180f12018-03-21 21:24:17 -0700509 kf_loop_writer.Write(kf_drivetrain_files[0], kf_drivetrain_files[1])
510
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800511
512def PlotDrivetrainMotions(drivetrain_params):
Diana Burgessd0180f12018-03-21 21:24:17 -0700513 # Simulate the response of the system to a step input.
514 drivetrain = Drivetrain(
515 left_low=False, right_low=False, drivetrain_params=drivetrain_params)
516 simulated_left = []
517 simulated_right = []
518 for _ in xrange(100):
519 drivetrain.Update(numpy.matrix([[12.0], [12.0]]))
520 simulated_left.append(drivetrain.X[0, 0])
521 simulated_right.append(drivetrain.X[2, 0])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800522
Diana Burgessd0180f12018-03-21 21:24:17 -0700523 pylab.rc('lines', linewidth=4)
524 pylab.plot(range(100), simulated_left, label='left position')
525 pylab.plot(range(100), simulated_right, 'r--', label='right position')
526 pylab.suptitle('Acceleration Test\n12 Volt Step Input')
527 pylab.legend(loc='lower right')
528 pylab.show()
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800529
Diana Burgessd0180f12018-03-21 21:24:17 -0700530 # Simulate forwards motion.
531 drivetrain = Drivetrain(
532 left_low=False, right_low=False, drivetrain_params=drivetrain_params)
533 close_loop_left = []
534 close_loop_right = []
535 left_power = []
536 right_power = []
537 R = numpy.matrix([[1.0], [0.0], [1.0], [0.0]])
538 for _ in xrange(300):
539 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min,
540 drivetrain.U_max)
541 drivetrain.UpdateObserver(U)
542 drivetrain.Update(U)
543 close_loop_left.append(drivetrain.X[0, 0])
544 close_loop_right.append(drivetrain.X[2, 0])
545 left_power.append(U[0, 0])
546 right_power.append(U[1, 0])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800547
Diana Burgessd0180f12018-03-21 21:24:17 -0700548 pylab.plot(range(300), close_loop_left, label='left position')
549 pylab.plot(range(300), close_loop_right, 'm--', label='right position')
550 pylab.plot(range(300), left_power, label='left power')
551 pylab.plot(range(300), right_power, '--', label='right power')
552 pylab.suptitle('Linear Move\nLeft and Right Position going to 1')
553 pylab.legend()
554 pylab.show()
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800555
Diana Burgessd0180f12018-03-21 21:24:17 -0700556 # Try turning in place
557 drivetrain = Drivetrain(drivetrain_params=drivetrain_params)
558 close_loop_left = []
559 close_loop_right = []
560 R = numpy.matrix([[-1.0], [0.0], [1.0], [0.0]])
561 for _ in xrange(200):
562 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min,
563 drivetrain.U_max)
564 drivetrain.UpdateObserver(U)
565 drivetrain.Update(U)
566 close_loop_left.append(drivetrain.X[0, 0])
567 close_loop_right.append(drivetrain.X[2, 0])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800568
Diana Burgessd0180f12018-03-21 21:24:17 -0700569 pylab.plot(range(200), close_loop_left, label='left position')
570 pylab.plot(range(200), close_loop_right, label='right position')
571 pylab.suptitle(
572 'Angular Move\nLeft position going to -1 and right position going to 1'
573 )
574 pylab.legend(loc='center right')
575 pylab.show()
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800576
Diana Burgessd0180f12018-03-21 21:24:17 -0700577 # Try turning just one side.
578 drivetrain = Drivetrain(drivetrain_params=drivetrain_params)
579 close_loop_left = []
580 close_loop_right = []
581 R = numpy.matrix([[0.0], [0.0], [1.0], [0.0]])
582 for _ in xrange(300):
583 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min,
584 drivetrain.U_max)
585 drivetrain.UpdateObserver(U)
586 drivetrain.Update(U)
587 close_loop_left.append(drivetrain.X[0, 0])
588 close_loop_right.append(drivetrain.X[2, 0])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800589
Diana Burgessd0180f12018-03-21 21:24:17 -0700590 pylab.plot(range(300), close_loop_left, label='left position')
591 pylab.plot(range(300), close_loop_right, label='right position')
592 pylab.suptitle(
593 'Pivot\nLeft position not changing and right position going to 1')
594 pylab.legend(loc='center right')
595 pylab.show()