blob: 1da85bc1cd51475e5bb15ec713aa9bc18455efc5 [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
Tyler Chatow6738c362019-02-16 14:12:30 -080010
Campbell Crowley33e0e3d2017-12-27 17:55:40 -080011class DrivetrainParams(object):
Tyler Chatow6738c362019-02-16 14:12:30 -080012
Diana Burgessd0180f12018-03-21 21:24:17 -070013 def __init__(self,
14 J,
15 mass,
16 robot_radius,
17 wheel_radius,
Austin Schuhecc92a02019-01-20 17:42:19 -080018 G=None,
19 G_high=None,
20 G_low=None,
21 q_pos=None,
22 q_pos_low=None,
23 q_pos_high=None,
24 q_vel=None,
25 q_vel_low=None,
26 q_vel_high=None,
Diana Burgessd0180f12018-03-21 21:24:17 -070027 efficiency=0.60,
28 has_imu=False,
29 force=False,
30 kf_q_voltage=10.0,
31 motor_type=control_loop.CIM(),
32 num_motors=2,
33 dt=0.00505,
34 controller_poles=[0.90, 0.90],
Michael Schuh95fbcc52018-03-10 20:57:20 -080035 observer_poles=[0.02, 0.02],
36 robot_cg_offset=0.0):
Diana Burgessd0180f12018-03-21 21:24:17 -070037 """Defines all constants of a drivetrain.
Campbell Crowley33e0e3d2017-12-27 17:55:40 -080038
Diana Burgessd0180f12018-03-21 21:24:17 -070039 Args:
40 J: float, Moment of inertia of drivetrain in kg m^2
41 mass: float, Mass of the robot in kg.
42 robot_radius: float, Radius of the robot, in meters (requires tuning by
43 hand).
44 wheel_radius: float, Radius of the wheels, in meters.
Austin Schuhecc92a02019-01-20 17:42:19 -080045 G: float, Gear ratio for a single speed.
Diana Burgessd0180f12018-03-21 21:24:17 -070046 G_high: float, Gear ratio for high gear.
47 G_low: float, Gear ratio for low gear.
48 dt: float, Control loop time step.
Austin Schuhecc92a02019-01-20 17:42:19 -080049 q_pos: float, q position for a single speed.
Diana Burgessd0180f12018-03-21 21:24:17 -070050 q_pos_low: float, q position low gear.
51 q_pos_high: float, q position high gear.
Austin Schuhecc92a02019-01-20 17:42:19 -080052 q_vel: float, q velocity for a single speed
Diana Burgessd0180f12018-03-21 21:24:17 -070053 q_vel_low: float, q velocity low gear.
54 q_vel_high: float, q velocity high gear.
55 efficiency: float, gear box effiency.
56 has_imu: bool, true if imu is present.
57 force: bool, true if force.
58 kf_q_voltage: float
59 motor_type: object, class of values defining the motor in drivetrain.
60 num_motors: int, number of motors on one side of drivetrain.
61 controller_poles: array, An array of poles. (See control_loop.py)
62 observer_poles: array, An array of poles. (See control_loop.py)
Michael Schuh95fbcc52018-03-10 20:57:20 -080063 robot_cg_offset: offset in meters of CG from robot center to left side
Diana Burgessd0180f12018-03-21 21:24:17 -070064 """
Austin Schuhecc92a02019-01-20 17:42:19 -080065 if G is not None:
66 assert (G_high is None)
67 assert (G_low is None)
68 G_high = G
69 G_low = G
70 assert (G_high is not None)
71 assert (G_low is not None)
72
73 if q_pos is not None:
74 assert (q_pos_low is None)
75 assert (q_pos_high is None)
76 q_pos_low = q_pos
77 q_pos_high = q_pos
78 assert (q_pos_low is not None)
79 assert (q_pos_high is not None)
80
81 if q_vel is not None:
82 assert (q_vel_low is None)
83 assert (q_vel_high is None)
84 q_vel_low = q_vel
85 q_vel_high = q_vel
86 assert (q_vel_low is not None)
87 assert (q_vel_high is not None)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -080088
Diana Burgessd0180f12018-03-21 21:24:17 -070089 self.J = J
90 self.mass = mass
91 self.robot_radius = robot_radius
Michael Schuh95fbcc52018-03-10 20:57:20 -080092 self.robot_cg_offset = robot_cg_offset
Diana Burgessd0180f12018-03-21 21:24:17 -070093 self.wheel_radius = wheel_radius
94 self.G_high = G_high
95 self.G_low = G_low
96 self.dt = dt
97 self.q_pos_low = q_pos_low
98 self.q_pos_high = q_pos_high
99 self.q_vel_low = q_vel_low
100 self.q_vel_high = q_vel_high
101 self.efficiency = efficiency
102 self.has_imu = has_imu
103 self.kf_q_voltage = kf_q_voltage
104 self.motor_type = motor_type
105 self.force = force
106 self.num_motors = num_motors
107 self.controller_poles = controller_poles
108 self.observer_poles = observer_poles
109
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800110
111class Drivetrain(control_loop.ControlLoop):
Tyler Chatow6738c362019-02-16 14:12:30 -0800112
Diana Burgessd0180f12018-03-21 21:24:17 -0700113 def __init__(self,
114 drivetrain_params,
115 name="Drivetrain",
116 left_low=True,
117 right_low=True):
118 """Defines a base drivetrain for a robot.
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800119
Diana Burgessd0180f12018-03-21 21:24:17 -0700120 Args:
121 drivetrain_params: DrivetrainParams, class of values defining the drivetrain.
122 name: string, Name of this drivetrain.
123 left_low: bool, Whether the left is in high gear.
124 right_low: bool, Whether the right is in high gear.
125 """
126 super(Drivetrain, self).__init__(name)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800127
Diana Burgessd0180f12018-03-21 21:24:17 -0700128 # Moment of inertia of the drivetrain in kg m^2
129 self.J = drivetrain_params.J
130 # Mass of the robot, in kg.
131 self.mass = drivetrain_params.mass
132 # Radius of the robot, in meters (requires tuning by hand)
133 self.robot_radius = drivetrain_params.robot_radius
134 # Radius of the wheels, in meters.
135 self.r = drivetrain_params.wheel_radius
136 self.has_imu = drivetrain_params.has_imu
Michael Schuh95fbcc52018-03-10 20:57:20 -0800137 # Offset in meters of the CG from the center of the robot to the left side
138 # of the robot. Since the arm is on the right side, the offset will
139 # likely be a negative number.
140 self.robot_cg_offset = drivetrain_params.robot_cg_offset
141 # Distance from the left side of the robot to the Center of Gravity
142 self.robot_radius_l = drivetrain_params.robot_radius - self.robot_cg_offset
143 # Distance from the right side of the robot to the Center of Gravity
144 self.robot_radius_r = drivetrain_params.robot_radius + self.robot_cg_offset
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800145
Diana Burgessd0180f12018-03-21 21:24:17 -0700146 # Gear ratios
147 self.G_low = drivetrain_params.G_low
148 self.G_high = drivetrain_params.G_high
149 if left_low:
150 self.Gl = self.G_low
151 else:
152 self.Gl = self.G_high
153 if right_low:
154 self.Gr = self.G_low
155 else:
156 self.Gr = self.G_high
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800157
Diana Burgessd0180f12018-03-21 21:24:17 -0700158 # Control loop time step
159 self.dt = drivetrain_params.dt
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800160
Diana Burgessd0180f12018-03-21 21:24:17 -0700161 self.efficiency = drivetrain_params.efficiency
162 self.force = drivetrain_params.force
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800163
Diana Burgessd0180f12018-03-21 21:24:17 -0700164 self.BuildDrivetrain(drivetrain_params.motor_type,
165 drivetrain_params.num_motors)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800166
Diana Burgessd0180f12018-03-21 21:24:17 -0700167 if left_low or right_low:
168 q_pos = drivetrain_params.q_pos_low
169 q_vel = drivetrain_params.q_vel_low
170 else:
171 q_pos = drivetrain_params.q_pos_high
172 q_vel = drivetrain_params.q_vel_high
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800173
Diana Burgessd0180f12018-03-21 21:24:17 -0700174 self.BuildDrivetrainController(q_pos, q_vel)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800175
Diana Burgessd0180f12018-03-21 21:24:17 -0700176 self.InitializeState()
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800177
Diana Burgessd0180f12018-03-21 21:24:17 -0700178 def BuildDrivetrain(self, motor, num_motors_per_side):
179 self.motor = motor
180 # Number of motors per side
181 self.num_motors = num_motors_per_side
182 # Stall Torque in N m
183 self.stall_torque = motor.stall_torque * self.num_motors * self.efficiency
184 # Stall Current in Amps
185 self.stall_current = motor.stall_current * self.num_motors
186 # Free Speed in rad/s
187 self.free_speed = motor.free_speed
188 # Free Current in Amps
189 self.free_current = motor.free_current * self.num_motors
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800190
Diana Burgessd0180f12018-03-21 21:24:17 -0700191 # Effective motor resistance in ohms.
192 self.resistance = 12.0 / self.stall_current
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800193
Diana Burgessd0180f12018-03-21 21:24:17 -0700194 # Resistance of the motor, divided by the number of motors.
195 # Motor velocity constant
Tyler Chatow6738c362019-02-16 14:12:30 -0800196 self.Kv = (
197 self.free_speed / (12.0 - self.resistance * self.free_current))
Diana Burgessd0180f12018-03-21 21:24:17 -0700198 # Torque constant
199 self.Kt = self.stall_torque / self.stall_current
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800200
Diana Burgessd0180f12018-03-21 21:24:17 -0700201 # These describe the way that a given side of a robot will be influenced
202 # by the other side. Units of 1 / kg.
Michael Schuh95fbcc52018-03-10 20:57:20 -0800203 self.mspl = 1.0 / self.mass + self.robot_radius_l * self.robot_radius_l / self.J
204 self.mspr = 1.0 / self.mass + self.robot_radius_r * self.robot_radius_r / self.J
205 self.msnl = self.robot_radius_r / ( self.robot_radius_l * self.mass ) - \
206 self.robot_radius_l * self.robot_radius_r / self.J
207 self.msnr = self.robot_radius_l / ( self.robot_radius_r * self.mass ) - \
208 self.robot_radius_l * self.robot_radius_r / self.J
Diana Burgessd0180f12018-03-21 21:24:17 -0700209 # The calculations which we will need for A and B.
210 self.tcl = self.Kt / self.Kv / (
211 self.Gl * self.Gl * self.resistance * self.r * self.r)
212 self.tcr = self.Kt / self.Kv / (
213 self.Gr * self.Gr * self.resistance * self.r * self.r)
214 self.mpl = self.Kt / (self.Gl * self.resistance * self.r)
215 self.mpr = self.Kt / (self.Gr * self.resistance * self.r)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800216
Diana Burgessd0180f12018-03-21 21:24:17 -0700217 # State feedback matrices
218 # X will be of the format
219 # [[positionl], [velocityl], [positionr], velocityr]]
220 self.A_continuous = numpy.matrix(
Tyler Chatow6738c362019-02-16 14:12:30 -0800221 [[0, 1, 0, 0], [0, -self.mspl * self.tcl, 0, -self.msnr * self.tcr],
222 [0, 0, 0, 1], [0, -self.msnl * self.tcl, 0,
223 -self.mspr * self.tcr]])
Diana Burgessd0180f12018-03-21 21:24:17 -0700224 self.B_continuous = numpy.matrix(
Tyler Chatow6738c362019-02-16 14:12:30 -0800225 [[0, 0], [self.mspl * self.mpl, self.msnr * self.mpr], [0, 0],
Michael Schuh95fbcc52018-03-10 20:57:20 -0800226 [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
Tyler Chatow6738c362019-02-16 14:12:30 -0800235 self.Q = numpy.matrix([[(1.0 / (q_pos**2.0)), 0.0, 0.0, 0.0],
236 [0.0, (1.0 / (q_vel**2.0)), 0.0, 0.0],
237 [0.0, 0.0, (1.0 / (q_pos**2.0)), 0.0],
238 [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):
Tyler Chatow6738c362019-02-16 14:12:30 -0800257
Diana Burgessd0180f12018-03-21 21:24:17 -0700258 def __init__(self,
259 drivetrain_params,
260 name="KFDrivetrain",
261 left_low=True,
262 right_low=True):
263 """Kalman filter values of a drivetrain.
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800264
Diana Burgessd0180f12018-03-21 21:24:17 -0700265 Args:
266 drivetrain_params: DrivetrainParams, class of values defining the drivetrain.
267 name: string, Name of this drivetrain.
268 left_low: bool, Whether the left is in high gear.
269 right_low: bool, Whether the right is in high gear.
270 """
271 super(KFDrivetrain, self).__init__(drivetrain_params, name, left_low,
272 right_low)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800273
Diana Burgessd0180f12018-03-21 21:24:17 -0700274 self.unaugmented_A_continuous = self.A_continuous
275 self.unaugmented_B_continuous = self.B_continuous
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800276
Diana Burgessd0180f12018-03-21 21:24:17 -0700277 # The practical voltage applied to the wheels is
278 # V_left = U_left + left_voltage_error
279 #
280 # The states are
281 # [left position, left velocity, right position, right velocity,
282 # left voltage error, right voltage error, angular_error]
283 #
284 # The left and right positions are filtered encoder positions and are not
285 # adjusted for heading error.
286 # The turn velocity as computed by the left and right velocities is
287 # adjusted by the gyro velocity.
288 # The angular_error is the angular velocity error between the wheel speed
289 # and the gyro speed.
290 self.A_continuous = numpy.matrix(numpy.zeros((7, 7)))
291 self.B_continuous = numpy.matrix(numpy.zeros((7, 2)))
292 self.A_continuous[0:4, 0:4] = self.unaugmented_A_continuous
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800293
Diana Burgessd0180f12018-03-21 21:24:17 -0700294 if self.force:
Tyler Chatow6738c362019-02-16 14:12:30 -0800295 self.A_continuous[0:4, 4:6] = numpy.matrix([[0.0, 0.0],
296 [self.mspl, self.msnl],
297 [0.0, 0.0],
298 [self.msnr, self.mspr]])
Diana Burgessd0180f12018-03-21 21:24:17 -0700299 q_voltage = drivetrain_params.kf_q_voltage * self.mpl
300 else:
301 self.A_continuous[0:4, 4:6] = self.unaugmented_B_continuous
302 q_voltage = drivetrain_params.kf_q_voltage
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800303
Diana Burgessd0180f12018-03-21 21:24:17 -0700304 self.B_continuous[0:4, 0:2] = self.unaugmented_B_continuous
305 self.A_continuous[0, 6] = 1
306 self.A_continuous[2, 6] = -1
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800307
Diana Burgessd0180f12018-03-21 21:24:17 -0700308 self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
309 self.B_continuous, self.dt)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800310
Diana Burgessd0180f12018-03-21 21:24:17 -0700311 if self.has_imu:
Tyler Chatow6738c362019-02-16 14:12:30 -0800312 self.C = numpy.matrix([[1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0],
313 [
314 0, -0.5 / drivetrain_params.robot_radius,
315 0, 0.5 / drivetrain_params.robot_radius,
316 0, 0, 0
317 ], [0, 0, 0, 0, 0, 0, 0]])
Diana Burgessd0180f12018-03-21 21:24:17 -0700318 gravity = 9.8
319 self.C[3, 0:6] = 0.5 * (
Tyler Chatow6738c362019-02-16 14:12:30 -0800320 self.A_continuous[1, 0:6] + self.A_continuous[3, 0:6]) / gravity
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800321
Tyler Chatow6738c362019-02-16 14:12:30 -0800322 self.D = numpy.matrix(
323 [[0, 0], [0, 0], [0, 0],
324 [
325 0.5 * (self.B_continuous[1, 0] + self.B_continuous[3, 0]) /
326 gravity,
327 0.5 * (self.B_continuous[1, 1] + self.B_continuous[3, 1]) /
328 gravity
329 ]])
Diana Burgessd0180f12018-03-21 21:24:17 -0700330 else:
Tyler Chatow6738c362019-02-16 14:12:30 -0800331 self.C = numpy.matrix([[1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0],
332 [
333 0, -0.5 / drivetrain_params.robot_radius,
334 0, 0.5 / drivetrain_params.robot_radius,
335 0, 0, 0
336 ]])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800337
Diana Burgessd0180f12018-03-21 21:24:17 -0700338 self.D = numpy.matrix([[0, 0], [0, 0], [0, 0]])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800339
Diana Burgessd0180f12018-03-21 21:24:17 -0700340 q_pos = 0.05
341 q_vel = 1.00
342 q_encoder_uncertainty = 2.00
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800343
Diana Burgessd0180f12018-03-21 21:24:17 -0700344 self.Q = numpy.matrix(
Tyler Chatow6738c362019-02-16 14:12:30 -0800345 [[(q_pos**2.0), 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
346 [0.0, (q_vel**2.0), 0.0, 0.0, 0.0, 0.0, 0.0],
347 [0.0, 0.0, (q_pos**2.0), 0.0, 0.0, 0.0, 0.0],
348 [0.0, 0.0, 0.0, (q_vel**2.0), 0.0, 0.0, 0.0],
349 [0.0, 0.0, 0.0, 0.0, (q_voltage**2.0), 0.0, 0.0],
350 [0.0, 0.0, 0.0, 0.0, 0.0, (q_voltage**2.0), 0.0],
Diana Burgessd0180f12018-03-21 21:24:17 -0700351 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, (q_encoder_uncertainty**2.0)]])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800352
Diana Burgessd0180f12018-03-21 21:24:17 -0700353 r_pos = 0.0001
354 r_gyro = 0.000001
355 if self.has_imu:
356 r_accelerometer = 7.0
357 self.R = numpy.matrix([[(r_pos**2.0), 0.0, 0.0, 0.0],
358 [0.0, (r_pos**2.0), 0.0, 0.0],
359 [0.0, 0.0, (r_gyro**2.0), 0.0],
360 [0.0, 0.0, 0.0, (r_accelerometer**2.0)]])
361 else:
362 self.R = numpy.matrix([[(r_pos**2.0), 0.0, 0.0],
363 [0.0, (r_pos**2.0), 0.0],
364 [0.0, 0.0, (r_gyro**2.0)]])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800365
Diana Burgessd0180f12018-03-21 21:24:17 -0700366 # Solving for kf gains.
367 self.KalmanGain, self.Q_steady = controls.kalman(
368 A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800369
James Kuszmaul4d752d52019-02-09 17:27:55 -0800370 # If we don't have an IMU, pad various matrices with zeros so that
371 # we can still have 4 measurement outputs.
Diana Burgessd0180f12018-03-21 21:24:17 -0700372 if not self.has_imu:
Tyler Chatow6738c362019-02-16 14:12:30 -0800373 self.KalmanGain = numpy.hstack((self.KalmanGain,
374 numpy.matrix(numpy.zeros((7, 1)))))
Austin Schuhecc92a02019-01-20 17:42:19 -0800375 self.C = numpy.vstack((self.C, numpy.matrix(numpy.zeros((1, 7)))))
376 self.D = numpy.vstack((self.D, numpy.matrix(numpy.zeros((1, 2)))))
James Kuszmaul4d752d52019-02-09 17:27:55 -0800377 Rtmp = numpy.zeros((4, 4))
378 Rtmp[0:3, 0:3] = self.R
379 self.R = Rtmp
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800380
Diana Burgessd0180f12018-03-21 21:24:17 -0700381 self.L = self.A * self.KalmanGain
382
383 unaug_K = self.K
384
385 # Implement a nice closed loop controller for use by the closed loop
386 # controller.
387 self.K = numpy.matrix(numpy.zeros((self.B.shape[1], self.A.shape[0])))
388 self.K[0:2, 0:4] = unaug_K
389 if self.force:
390 self.K[0, 4] = 1.0 / self.mpl
391 self.K[1, 5] = 1.0 / self.mpr
392 else:
393 self.K[0, 4] = 1.0
394 self.K[1, 5] = 1.0
395
396 self.Qff = numpy.matrix(numpy.zeros((4, 4)))
397 qff_pos = 0.005
398 qff_vel = 1.00
399 self.Qff[0, 0] = 1.0 / qff_pos**2.0
400 self.Qff[1, 1] = 1.0 / qff_vel**2.0
401 self.Qff[2, 2] = 1.0 / qff_pos**2.0
402 self.Qff[3, 3] = 1.0 / qff_vel**2.0
403 self.Kff = numpy.matrix(numpy.zeros((2, 7)))
404 self.Kff[0:2, 0:4] = controls.TwoStateFeedForwards(
405 self.B[0:4, :], self.Qff)
406
407 self.InitializeState()
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800408
409
Tyler Chatow6738c362019-02-16 14:12:30 -0800410def WriteDrivetrain(drivetrain_files,
411 kf_drivetrain_files,
412 year_namespace,
413 drivetrain_params,
414 scalar_type='double'):
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800415
Diana Burgessd0180f12018-03-21 21:24:17 -0700416 # Write the generated constants out to a file.
417 drivetrain_low_low = Drivetrain(
418 name="DrivetrainLowLow",
419 left_low=True,
420 right_low=True,
421 drivetrain_params=drivetrain_params)
422 drivetrain_low_high = Drivetrain(
423 name="DrivetrainLowHigh",
424 left_low=True,
425 right_low=False,
426 drivetrain_params=drivetrain_params)
427 drivetrain_high_low = Drivetrain(
428 name="DrivetrainHighLow",
429 left_low=False,
430 right_low=True,
431 drivetrain_params=drivetrain_params)
432 drivetrain_high_high = Drivetrain(
433 name="DrivetrainHighHigh",
434 left_low=False,
435 right_low=False,
436 drivetrain_params=drivetrain_params)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800437
Diana Burgessd0180f12018-03-21 21:24:17 -0700438 kf_drivetrain_low_low = KFDrivetrain(
439 name="KFDrivetrainLowLow",
440 left_low=True,
441 right_low=True,
442 drivetrain_params=drivetrain_params)
443 kf_drivetrain_low_high = KFDrivetrain(
444 name="KFDrivetrainLowHigh",
445 left_low=True,
446 right_low=False,
447 drivetrain_params=drivetrain_params)
448 kf_drivetrain_high_low = KFDrivetrain(
449 name="KFDrivetrainHighLow",
450 left_low=False,
451 right_low=True,
452 drivetrain_params=drivetrain_params)
453 kf_drivetrain_high_high = KFDrivetrain(
454 name="KFDrivetrainHighHigh",
455 left_low=False,
456 right_low=False,
457 drivetrain_params=drivetrain_params)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800458
Austin Schuhbcce26a2018-03-26 23:41:24 -0700459 if isinstance(year_namespace, list):
Austin Schuhecc92a02019-01-20 17:42:19 -0800460 namespaces = year_namespace
Austin Schuhbcce26a2018-03-26 23:41:24 -0700461 else:
Austin Schuhecc92a02019-01-20 17:42:19 -0800462 namespaces = [year_namespace, 'control_loops', 'drivetrain']
Diana Burgessd0180f12018-03-21 21:24:17 -0700463 dog_loop_writer = control_loop.ControlLoopWriter(
464 "Drivetrain", [
465 drivetrain_low_low, drivetrain_low_high, drivetrain_high_low,
466 drivetrain_high_high
467 ],
Austin Schuhbcce26a2018-03-26 23:41:24 -0700468 namespaces=namespaces,
469 scalar_type=scalar_type)
Diana Burgessd0180f12018-03-21 21:24:17 -0700470 dog_loop_writer.AddConstant(
471 control_loop.Constant("kDt", "%f", drivetrain_low_low.dt))
472 dog_loop_writer.AddConstant(
473 control_loop.Constant("kStallTorque", "%f",
474 drivetrain_low_low.stall_torque))
475 dog_loop_writer.AddConstant(
476 control_loop.Constant("kStallCurrent", "%f",
477 drivetrain_low_low.stall_current))
478 dog_loop_writer.AddConstant(
479 control_loop.Constant("kFreeSpeed", "%f",
480 drivetrain_low_low.free_speed))
481 dog_loop_writer.AddConstant(
482 control_loop.Constant("kFreeCurrent", "%f",
483 drivetrain_low_low.free_current))
484 dog_loop_writer.AddConstant(
485 control_loop.Constant("kJ", "%f", drivetrain_low_low.J))
486 dog_loop_writer.AddConstant(
487 control_loop.Constant("kMass", "%f", drivetrain_low_low.mass))
488 dog_loop_writer.AddConstant(
489 control_loop.Constant("kRobotRadius", "%f",
490 drivetrain_low_low.robot_radius))
491 dog_loop_writer.AddConstant(
492 control_loop.Constant("kWheelRadius", "%f", drivetrain_low_low.r))
493 dog_loop_writer.AddConstant(
494 control_loop.Constant("kR", "%f", drivetrain_low_low.resistance))
495 dog_loop_writer.AddConstant(
496 control_loop.Constant("kV", "%f", drivetrain_low_low.Kv))
497 dog_loop_writer.AddConstant(
498 control_loop.Constant("kT", "%f", drivetrain_low_low.Kt))
499 dog_loop_writer.AddConstant(
500 control_loop.Constant("kLowGearRatio", "%f", drivetrain_low_low.G_low))
501 dog_loop_writer.AddConstant(
502 control_loop.Constant("kHighGearRatio", "%f",
503 drivetrain_high_high.G_high))
504 dog_loop_writer.AddConstant(
505 control_loop.Constant(
506 "kHighOutputRatio", "%f",
507 drivetrain_high_high.G_high * drivetrain_high_high.r))
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800508
Diana Burgessd0180f12018-03-21 21:24:17 -0700509 dog_loop_writer.Write(drivetrain_files[0], drivetrain_files[1])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800510
Diana Burgessd0180f12018-03-21 21:24:17 -0700511 kf_loop_writer = control_loop.ControlLoopWriter(
512 "KFDrivetrain", [
513 kf_drivetrain_low_low, kf_drivetrain_low_high,
514 kf_drivetrain_high_low, kf_drivetrain_high_high
515 ],
Austin Schuhbcce26a2018-03-26 23:41:24 -0700516 namespaces=namespaces,
517 scalar_type=scalar_type)
Diana Burgessd0180f12018-03-21 21:24:17 -0700518 kf_loop_writer.Write(kf_drivetrain_files[0], kf_drivetrain_files[1])
519
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800520
521def PlotDrivetrainMotions(drivetrain_params):
Diana Burgessd0180f12018-03-21 21:24:17 -0700522 # Simulate the response of the system to a step input.
523 drivetrain = Drivetrain(
524 left_low=False, right_low=False, drivetrain_params=drivetrain_params)
525 simulated_left = []
526 simulated_right = []
527 for _ in xrange(100):
528 drivetrain.Update(numpy.matrix([[12.0], [12.0]]))
529 simulated_left.append(drivetrain.X[0, 0])
530 simulated_right.append(drivetrain.X[2, 0])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800531
Diana Burgessd0180f12018-03-21 21:24:17 -0700532 pylab.rc('lines', linewidth=4)
533 pylab.plot(range(100), simulated_left, label='left position')
534 pylab.plot(range(100), simulated_right, 'r--', label='right position')
535 pylab.suptitle('Acceleration Test\n12 Volt Step Input')
536 pylab.legend(loc='lower right')
537 pylab.show()
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800538
Diana Burgessd0180f12018-03-21 21:24:17 -0700539 # Simulate forwards motion.
540 drivetrain = Drivetrain(
541 left_low=False, right_low=False, drivetrain_params=drivetrain_params)
542 close_loop_left = []
543 close_loop_right = []
544 left_power = []
545 right_power = []
546 R = numpy.matrix([[1.0], [0.0], [1.0], [0.0]])
547 for _ in xrange(300):
548 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min,
549 drivetrain.U_max)
550 drivetrain.UpdateObserver(U)
551 drivetrain.Update(U)
552 close_loop_left.append(drivetrain.X[0, 0])
553 close_loop_right.append(drivetrain.X[2, 0])
554 left_power.append(U[0, 0])
555 right_power.append(U[1, 0])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800556
Diana Burgessd0180f12018-03-21 21:24:17 -0700557 pylab.plot(range(300), close_loop_left, label='left position')
558 pylab.plot(range(300), close_loop_right, 'm--', label='right position')
559 pylab.plot(range(300), left_power, label='left power')
560 pylab.plot(range(300), right_power, '--', label='right power')
561 pylab.suptitle('Linear Move\nLeft and Right Position going to 1')
562 pylab.legend()
563 pylab.show()
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800564
Diana Burgessd0180f12018-03-21 21:24:17 -0700565 # Try turning in place
566 drivetrain = Drivetrain(drivetrain_params=drivetrain_params)
567 close_loop_left = []
568 close_loop_right = []
569 R = numpy.matrix([[-1.0], [0.0], [1.0], [0.0]])
570 for _ in xrange(200):
571 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min,
572 drivetrain.U_max)
573 drivetrain.UpdateObserver(U)
574 drivetrain.Update(U)
575 close_loop_left.append(drivetrain.X[0, 0])
576 close_loop_right.append(drivetrain.X[2, 0])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800577
Diana Burgessd0180f12018-03-21 21:24:17 -0700578 pylab.plot(range(200), close_loop_left, label='left position')
579 pylab.plot(range(200), close_loop_right, label='right position')
580 pylab.suptitle(
Tyler Chatow6738c362019-02-16 14:12:30 -0800581 'Angular Move\nLeft position going to -1 and right position going to 1')
Diana Burgessd0180f12018-03-21 21:24:17 -0700582 pylab.legend(loc='center right')
583 pylab.show()
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800584
Diana Burgessd0180f12018-03-21 21:24:17 -0700585 # Try turning just one side.
586 drivetrain = Drivetrain(drivetrain_params=drivetrain_params)
587 close_loop_left = []
588 close_loop_right = []
589 R = numpy.matrix([[0.0], [0.0], [1.0], [0.0]])
590 for _ in xrange(300):
591 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min,
592 drivetrain.U_max)
593 drivetrain.UpdateObserver(U)
594 drivetrain.Update(U)
595 close_loop_left.append(drivetrain.X[0, 0])
596 close_loop_right.append(drivetrain.X[2, 0])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800597
Diana Burgessd0180f12018-03-21 21:24:17 -0700598 pylab.plot(range(300), close_loop_left, label='left position')
599 pylab.plot(range(300), close_loop_right, label='right position')
600 pylab.suptitle(
601 'Pivot\nLeft position not changing and right position going to 1')
602 pylab.legend(loc='center right')
603 pylab.show()