blob: 4069036e284ce7f0f4b84a529bdeed8f0156bbce [file] [log] [blame]
Austin Schuh085eab92020-11-26 13:54:51 -08001#!/usr/bin/python3
Campbell Crowley33e0e3d2017-12-27 17:55:40 -08002
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):
Diana Burgessd0180f12018-03-21 21:24:17 -070012 def __init__(self,
13 J,
14 mass,
15 robot_radius,
16 wheel_radius,
Austin Schuhecc92a02019-01-20 17:42:19 -080017 G=None,
18 G_high=None,
19 G_low=None,
20 q_pos=None,
21 q_pos_low=None,
22 q_pos_high=None,
23 q_vel=None,
24 q_vel_low=None,
25 q_vel_high=None,
Diana Burgessd0180f12018-03-21 21:24:17 -070026 efficiency=0.60,
27 has_imu=False,
28 force=False,
29 kf_q_voltage=10.0,
30 motor_type=control_loop.CIM(),
31 num_motors=2,
32 dt=0.00505,
33 controller_poles=[0.90, 0.90],
Michael Schuh95fbcc52018-03-10 20:57:20 -080034 observer_poles=[0.02, 0.02],
35 robot_cg_offset=0.0):
Diana Burgessd0180f12018-03-21 21:24:17 -070036 """Defines all constants of a drivetrain.
Campbell Crowley33e0e3d2017-12-27 17:55:40 -080037
Diana Burgessd0180f12018-03-21 21:24:17 -070038 Args:
39 J: float, Moment of inertia of drivetrain in kg m^2
40 mass: float, Mass of the robot in kg.
41 robot_radius: float, Radius of the robot, in meters (requires tuning by
42 hand).
43 wheel_radius: float, Radius of the wheels, in meters.
Austin Schuhecc92a02019-01-20 17:42:19 -080044 G: float, Gear ratio for a single speed.
Diana Burgessd0180f12018-03-21 21:24:17 -070045 G_high: float, Gear ratio for high gear.
46 G_low: float, Gear ratio for low gear.
47 dt: float, Control loop time step.
Austin Schuh1542ea32021-01-23 20:19:50 -080048 q_pos: float, q position for a single speed LQR controller.
49 q_pos_low: float, q position low gear LQR controller.
50 q_pos_high: float, q position high gear LQR controller.
51 q_vel: float, q velocity for a single speed LQR controller.
52 q_vel_low: float, q velocity low gear LQR controller.
53 q_vel_high: float, q velocity high gear LQR controller.
Diana Burgessd0180f12018-03-21 21:24:17 -070054 efficiency: float, gear box effiency.
55 has_imu: bool, true if imu is present.
56 force: bool, true if force.
57 kf_q_voltage: float
58 motor_type: object, class of values defining the motor in drivetrain.
59 num_motors: int, number of motors on one side of drivetrain.
Austin Schuh1542ea32021-01-23 20:19:50 -080060 controller_poles: array, An array of poles for the polydrivetrain
61 controller. (See control_loop.py)
Diana Burgessd0180f12018-03-21 21:24:17 -070062 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):
Diana Burgessd0180f12018-03-21 21:24:17 -0700112 def __init__(self,
113 drivetrain_params,
114 name="Drivetrain",
115 left_low=True,
116 right_low=True):
117 """Defines a base drivetrain for a robot.
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800118
Diana Burgessd0180f12018-03-21 21:24:17 -0700119 Args:
120 drivetrain_params: DrivetrainParams, class of values defining the drivetrain.
121 name: string, Name of this drivetrain.
122 left_low: bool, Whether the left is in high gear.
123 right_low: bool, Whether the right is in high gear.
124 """
125 super(Drivetrain, self).__init__(name)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800126
Diana Burgessd0180f12018-03-21 21:24:17 -0700127 # Moment of inertia of the drivetrain in kg m^2
128 self.J = drivetrain_params.J
129 # Mass of the robot, in kg.
130 self.mass = drivetrain_params.mass
131 # Radius of the robot, in meters (requires tuning by hand)
132 self.robot_radius = drivetrain_params.robot_radius
133 # Radius of the wheels, in meters.
134 self.r = drivetrain_params.wheel_radius
135 self.has_imu = drivetrain_params.has_imu
Michael Schuh95fbcc52018-03-10 20:57:20 -0800136 # Offset in meters of the CG from the center of the robot to the left side
137 # of the robot. Since the arm is on the right side, the offset will
138 # likely be a negative number.
139 self.robot_cg_offset = drivetrain_params.robot_cg_offset
140 # Distance from the left side of the robot to the Center of Gravity
141 self.robot_radius_l = drivetrain_params.robot_radius - self.robot_cg_offset
142 # Distance from the right side of the robot to the Center of Gravity
143 self.robot_radius_r = drivetrain_params.robot_radius + self.robot_cg_offset
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800144
Diana Burgessd0180f12018-03-21 21:24:17 -0700145 # Gear ratios
146 self.G_low = drivetrain_params.G_low
147 self.G_high = drivetrain_params.G_high
148 if left_low:
149 self.Gl = self.G_low
150 else:
151 self.Gl = self.G_high
152 if right_low:
153 self.Gr = self.G_low
154 else:
155 self.Gr = self.G_high
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800156
Diana Burgessd0180f12018-03-21 21:24:17 -0700157 # Control loop time step
158 self.dt = drivetrain_params.dt
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800159
Diana Burgessd0180f12018-03-21 21:24:17 -0700160 self.efficiency = drivetrain_params.efficiency
161 self.force = drivetrain_params.force
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800162
Diana Burgessd0180f12018-03-21 21:24:17 -0700163 self.BuildDrivetrain(drivetrain_params.motor_type,
164 drivetrain_params.num_motors)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800165
Diana Burgessd0180f12018-03-21 21:24:17 -0700166 if left_low or right_low:
167 q_pos = drivetrain_params.q_pos_low
168 q_vel = drivetrain_params.q_vel_low
169 else:
170 q_pos = drivetrain_params.q_pos_high
171 q_vel = drivetrain_params.q_vel_high
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800172
Diana Burgessd0180f12018-03-21 21:24:17 -0700173 self.BuildDrivetrainController(q_pos, q_vel)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800174
Diana Burgessd0180f12018-03-21 21:24:17 -0700175 self.InitializeState()
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800176
Diana Burgessd0180f12018-03-21 21:24:17 -0700177 def BuildDrivetrain(self, motor, num_motors_per_side):
178 self.motor = motor
179 # Number of motors per side
180 self.num_motors = num_motors_per_side
181 # Stall Torque in N m
182 self.stall_torque = motor.stall_torque * self.num_motors * self.efficiency
183 # Stall Current in Amps
184 self.stall_current = motor.stall_current * self.num_motors
185 # Free Speed in rad/s
186 self.free_speed = motor.free_speed
187 # Free Current in Amps
188 self.free_current = motor.free_current * self.num_motors
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800189
Diana Burgessd0180f12018-03-21 21:24:17 -0700190 # Effective motor resistance in ohms.
191 self.resistance = 12.0 / self.stall_current
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800192
Diana Burgessd0180f12018-03-21 21:24:17 -0700193 # Resistance of the motor, divided by the number of motors.
194 # Motor velocity constant
Tyler Chatow6738c362019-02-16 14:12:30 -0800195 self.Kv = (
196 self.free_speed / (12.0 - self.resistance * self.free_current))
Diana Burgessd0180f12018-03-21 21:24:17 -0700197 # Torque constant
198 self.Kt = self.stall_torque / self.stall_current
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800199
Diana Burgessd0180f12018-03-21 21:24:17 -0700200 # These describe the way that a given side of a robot will be influenced
201 # by the other side. Units of 1 / kg.
Michael Schuh95fbcc52018-03-10 20:57:20 -0800202 self.mspl = 1.0 / self.mass + self.robot_radius_l * self.robot_radius_l / self.J
203 self.mspr = 1.0 / self.mass + self.robot_radius_r * self.robot_radius_r / self.J
204 self.msnl = self.robot_radius_r / ( self.robot_radius_l * self.mass ) - \
205 self.robot_radius_l * self.robot_radius_r / self.J
206 self.msnr = self.robot_radius_l / ( self.robot_radius_r * self.mass ) - \
207 self.robot_radius_l * self.robot_radius_r / self.J
Diana Burgessd0180f12018-03-21 21:24:17 -0700208 # The calculations which we will need for A and B.
209 self.tcl = self.Kt / self.Kv / (
210 self.Gl * self.Gl * self.resistance * self.r * self.r)
211 self.tcr = self.Kt / self.Kv / (
212 self.Gr * self.Gr * self.resistance * self.r * self.r)
213 self.mpl = self.Kt / (self.Gl * self.resistance * self.r)
214 self.mpr = self.Kt / (self.Gr * self.resistance * self.r)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800215
Diana Burgessd0180f12018-03-21 21:24:17 -0700216 # State feedback matrices
217 # X will be of the format
Austin Schuhd77c0642020-12-30 21:38:54 -0800218 # [[positionl], [velocityl], [positionr], [velocityr]]
Diana Burgessd0180f12018-03-21 21:24:17 -0700219 self.A_continuous = numpy.matrix(
Ravago Jones26f7ad02021-02-05 15:45:59 -0800220 [[0, 1, 0,
221 0], [0, -self.mspl * self.tcl, 0, -self.msnr * self.tcr],
222 [0, 0, 0, 1],
223 [0, -self.msnl * self.tcl, 0, -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):
Austin Schuhd77c0642020-12-30 21:38:54 -0800234 # We can solve for the max velocity by setting \dot(x) = Ax + Bu to 0
235 max_voltage = 12
236 glog.debug(
237 "Max speed %f m/s",
238 -(self.B_continuous[1, 1] + self.B_continuous[1, 0]) /
239 (self.A_continuous[1, 1] + self.A_continuous[1, 3]) * max_voltage)
240
Diana Burgessd0180f12018-03-21 21:24:17 -0700241 # Tune the LQR controller
Tyler Chatow6738c362019-02-16 14:12:30 -0800242 self.Q = numpy.matrix([[(1.0 / (q_pos**2.0)), 0.0, 0.0, 0.0],
243 [0.0, (1.0 / (q_vel**2.0)), 0.0, 0.0],
244 [0.0, 0.0, (1.0 / (q_pos**2.0)), 0.0],
245 [0.0, 0.0, 0.0, (1.0 / (q_vel**2.0))]])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800246
Diana Burgessd0180f12018-03-21 21:24:17 -0700247 self.R = numpy.matrix([[(1.0 / (12.0**2.0)), 0.0],
248 [0.0, (1.0 / (12.0**2.0))]])
249 self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800250
Diana Burgessd0180f12018-03-21 21:24:17 -0700251 glog.debug('DT q_pos %f q_vel %s %s', q_pos, q_vel, self._name)
Ravago Jones26f7ad02021-02-05 15:45:59 -0800252 glog.debug('Poles: %s',
253 str(numpy.linalg.eig(self.A - self.B * self.K)[0]))
254 glog.debug(
255 'Time constants: %s hz',
256 str([
257 numpy.log(x) / -self.dt
258 for x in numpy.linalg.eig(self.A - self.B * self.K)[0]
259 ]))
Diana Burgessd0180f12018-03-21 21:24:17 -0700260 glog.debug('K %s', repr(self.K))
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800261
Diana Burgessd0180f12018-03-21 21:24:17 -0700262 self.hlp = 0.3
263 self.llp = 0.4
264 self.PlaceObserverPoles([self.hlp, self.hlp, self.llp, self.llp])
265
266 self.U_max = numpy.matrix([[12.0], [12.0]])
267 self.U_min = numpy.matrix([[-12.0], [-12.0]])
268
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800269
270class KFDrivetrain(Drivetrain):
Diana Burgessd0180f12018-03-21 21:24:17 -0700271 def __init__(self,
272 drivetrain_params,
273 name="KFDrivetrain",
274 left_low=True,
275 right_low=True):
276 """Kalman filter values of a drivetrain.
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800277
Diana Burgessd0180f12018-03-21 21:24:17 -0700278 Args:
279 drivetrain_params: DrivetrainParams, class of values defining the drivetrain.
280 name: string, Name of this drivetrain.
281 left_low: bool, Whether the left is in high gear.
282 right_low: bool, Whether the right is in high gear.
283 """
284 super(KFDrivetrain, self).__init__(drivetrain_params, name, left_low,
285 right_low)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800286
Diana Burgessd0180f12018-03-21 21:24:17 -0700287 self.unaugmented_A_continuous = self.A_continuous
288 self.unaugmented_B_continuous = self.B_continuous
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800289
Diana Burgessd0180f12018-03-21 21:24:17 -0700290 # The practical voltage applied to the wheels is
291 # V_left = U_left + left_voltage_error
292 #
293 # The states are
294 # [left position, left velocity, right position, right velocity,
295 # left voltage error, right voltage error, angular_error]
296 #
297 # The left and right positions are filtered encoder positions and are not
298 # adjusted for heading error.
299 # The turn velocity as computed by the left and right velocities is
300 # adjusted by the gyro velocity.
301 # The angular_error is the angular velocity error between the wheel speed
302 # and the gyro speed.
303 self.A_continuous = numpy.matrix(numpy.zeros((7, 7)))
304 self.B_continuous = numpy.matrix(numpy.zeros((7, 2)))
305 self.A_continuous[0:4, 0:4] = self.unaugmented_A_continuous
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800306
Diana Burgessd0180f12018-03-21 21:24:17 -0700307 if self.force:
Tyler Chatow6738c362019-02-16 14:12:30 -0800308 self.A_continuous[0:4, 4:6] = numpy.matrix([[0.0, 0.0],
309 [self.mspl, self.msnl],
310 [0.0, 0.0],
Ravago Jones26f7ad02021-02-05 15:45:59 -0800311 [self.msnr,
312 self.mspr]])
Diana Burgessd0180f12018-03-21 21:24:17 -0700313 q_voltage = drivetrain_params.kf_q_voltage * self.mpl
314 else:
315 self.A_continuous[0:4, 4:6] = self.unaugmented_B_continuous
316 q_voltage = drivetrain_params.kf_q_voltage
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800317
Diana Burgessd0180f12018-03-21 21:24:17 -0700318 self.B_continuous[0:4, 0:2] = self.unaugmented_B_continuous
319 self.A_continuous[0, 6] = 1
320 self.A_continuous[2, 6] = -1
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800321
Diana Burgessd0180f12018-03-21 21:24:17 -0700322 self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
323 self.B_continuous, self.dt)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800324
Diana Burgessd0180f12018-03-21 21:24:17 -0700325 if self.has_imu:
Ravago Jones26f7ad02021-02-05 15:45:59 -0800326 self.C = numpy.matrix(
327 [[1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0],
328 [
329 0, -0.5 / drivetrain_params.robot_radius, 0,
330 0.5 / drivetrain_params.robot_radius, 0, 0, 0
331 ], [0, 0, 0, 0, 0, 0, 0]])
Diana Burgessd0180f12018-03-21 21:24:17 -0700332 gravity = 9.8
Ravago Jones26f7ad02021-02-05 15:45:59 -0800333 self.C[3, 0:6] = 0.5 * (self.A_continuous[1, 0:6] +
334 self.A_continuous[3, 0:6]) / gravity
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800335
Tyler Chatow6738c362019-02-16 14:12:30 -0800336 self.D = numpy.matrix(
337 [[0, 0], [0, 0], [0, 0],
338 [
Ravago Jones26f7ad02021-02-05 15:45:59 -0800339 0.5 * (self.B_continuous[1, 0] + self.B_continuous[3, 0])
340 / gravity,
341 0.5 * (self.B_continuous[1, 1] + self.B_continuous[3, 1])
342 / gravity
Tyler Chatow6738c362019-02-16 14:12:30 -0800343 ]])
Diana Burgessd0180f12018-03-21 21:24:17 -0700344 else:
Ravago Jones26f7ad02021-02-05 15:45:59 -0800345 self.C = numpy.matrix(
346 [[1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0],
347 [
348 0, -0.5 / drivetrain_params.robot_radius, 0,
349 0.5 / drivetrain_params.robot_radius, 0, 0, 0
350 ]])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800351
Diana Burgessd0180f12018-03-21 21:24:17 -0700352 self.D = numpy.matrix([[0, 0], [0, 0], [0, 0]])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800353
Diana Burgessd0180f12018-03-21 21:24:17 -0700354 q_pos = 0.05
355 q_vel = 1.00
356 q_encoder_uncertainty = 2.00
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800357
Diana Burgessd0180f12018-03-21 21:24:17 -0700358 self.Q = numpy.matrix(
Tyler Chatow6738c362019-02-16 14:12:30 -0800359 [[(q_pos**2.0), 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
360 [0.0, (q_vel**2.0), 0.0, 0.0, 0.0, 0.0, 0.0],
361 [0.0, 0.0, (q_pos**2.0), 0.0, 0.0, 0.0, 0.0],
362 [0.0, 0.0, 0.0, (q_vel**2.0), 0.0, 0.0, 0.0],
363 [0.0, 0.0, 0.0, 0.0, (q_voltage**2.0), 0.0, 0.0],
364 [0.0, 0.0, 0.0, 0.0, 0.0, (q_voltage**2.0), 0.0],
Diana Burgessd0180f12018-03-21 21:24:17 -0700365 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, (q_encoder_uncertainty**2.0)]])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800366
Diana Burgessd0180f12018-03-21 21:24:17 -0700367 r_pos = 0.0001
368 r_gyro = 0.000001
369 if self.has_imu:
370 r_accelerometer = 7.0
371 self.R = numpy.matrix([[(r_pos**2.0), 0.0, 0.0, 0.0],
372 [0.0, (r_pos**2.0), 0.0, 0.0],
373 [0.0, 0.0, (r_gyro**2.0), 0.0],
374 [0.0, 0.0, 0.0, (r_accelerometer**2.0)]])
375 else:
376 self.R = numpy.matrix([[(r_pos**2.0), 0.0, 0.0],
377 [0.0, (r_pos**2.0), 0.0],
378 [0.0, 0.0, (r_gyro**2.0)]])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800379
Diana Burgessd0180f12018-03-21 21:24:17 -0700380 # Solving for kf gains.
381 self.KalmanGain, self.Q_steady = controls.kalman(
382 A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800383
James Kuszmaul4d752d52019-02-09 17:27:55 -0800384 # If we don't have an IMU, pad various matrices with zeros so that
385 # we can still have 4 measurement outputs.
Diana Burgessd0180f12018-03-21 21:24:17 -0700386 if not self.has_imu:
Tyler Chatow6738c362019-02-16 14:12:30 -0800387 self.KalmanGain = numpy.hstack((self.KalmanGain,
388 numpy.matrix(numpy.zeros((7, 1)))))
Austin Schuhecc92a02019-01-20 17:42:19 -0800389 self.C = numpy.vstack((self.C, numpy.matrix(numpy.zeros((1, 7)))))
390 self.D = numpy.vstack((self.D, numpy.matrix(numpy.zeros((1, 2)))))
James Kuszmaul4d752d52019-02-09 17:27:55 -0800391 Rtmp = numpy.zeros((4, 4))
392 Rtmp[0:3, 0:3] = self.R
393 self.R = Rtmp
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800394
Diana Burgessd0180f12018-03-21 21:24:17 -0700395 self.L = self.A * self.KalmanGain
396
397 unaug_K = self.K
398
399 # Implement a nice closed loop controller for use by the closed loop
400 # controller.
401 self.K = numpy.matrix(numpy.zeros((self.B.shape[1], self.A.shape[0])))
402 self.K[0:2, 0:4] = unaug_K
403 if self.force:
404 self.K[0, 4] = 1.0 / self.mpl
405 self.K[1, 5] = 1.0 / self.mpr
406 else:
407 self.K[0, 4] = 1.0
408 self.K[1, 5] = 1.0
409
410 self.Qff = numpy.matrix(numpy.zeros((4, 4)))
411 qff_pos = 0.005
412 qff_vel = 1.00
413 self.Qff[0, 0] = 1.0 / qff_pos**2.0
414 self.Qff[1, 1] = 1.0 / qff_vel**2.0
415 self.Qff[2, 2] = 1.0 / qff_pos**2.0
416 self.Qff[3, 3] = 1.0 / qff_vel**2.0
417 self.Kff = numpy.matrix(numpy.zeros((2, 7)))
418 self.Kff[0:2, 0:4] = controls.TwoStateFeedForwards(
419 self.B[0:4, :], self.Qff)
420
421 self.InitializeState()
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800422
423
Tyler Chatow6738c362019-02-16 14:12:30 -0800424def WriteDrivetrain(drivetrain_files,
425 kf_drivetrain_files,
426 year_namespace,
427 drivetrain_params,
428 scalar_type='double'):
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800429
Diana Burgessd0180f12018-03-21 21:24:17 -0700430 # Write the generated constants out to a file.
431 drivetrain_low_low = Drivetrain(
432 name="DrivetrainLowLow",
433 left_low=True,
434 right_low=True,
435 drivetrain_params=drivetrain_params)
436 drivetrain_low_high = Drivetrain(
437 name="DrivetrainLowHigh",
438 left_low=True,
439 right_low=False,
440 drivetrain_params=drivetrain_params)
441 drivetrain_high_low = Drivetrain(
442 name="DrivetrainHighLow",
443 left_low=False,
444 right_low=True,
445 drivetrain_params=drivetrain_params)
446 drivetrain_high_high = Drivetrain(
447 name="DrivetrainHighHigh",
448 left_low=False,
449 right_low=False,
450 drivetrain_params=drivetrain_params)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800451
Diana Burgessd0180f12018-03-21 21:24:17 -0700452 kf_drivetrain_low_low = KFDrivetrain(
453 name="KFDrivetrainLowLow",
454 left_low=True,
455 right_low=True,
456 drivetrain_params=drivetrain_params)
457 kf_drivetrain_low_high = KFDrivetrain(
458 name="KFDrivetrainLowHigh",
459 left_low=True,
460 right_low=False,
461 drivetrain_params=drivetrain_params)
462 kf_drivetrain_high_low = KFDrivetrain(
463 name="KFDrivetrainHighLow",
464 left_low=False,
465 right_low=True,
466 drivetrain_params=drivetrain_params)
467 kf_drivetrain_high_high = KFDrivetrain(
468 name="KFDrivetrainHighHigh",
469 left_low=False,
470 right_low=False,
471 drivetrain_params=drivetrain_params)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800472
Austin Schuhbcce26a2018-03-26 23:41:24 -0700473 if isinstance(year_namespace, list):
Austin Schuhecc92a02019-01-20 17:42:19 -0800474 namespaces = year_namespace
Austin Schuhbcce26a2018-03-26 23:41:24 -0700475 else:
Austin Schuhecc92a02019-01-20 17:42:19 -0800476 namespaces = [year_namespace, 'control_loops', 'drivetrain']
Diana Burgessd0180f12018-03-21 21:24:17 -0700477 dog_loop_writer = control_loop.ControlLoopWriter(
478 "Drivetrain", [
479 drivetrain_low_low, drivetrain_low_high, drivetrain_high_low,
480 drivetrain_high_high
481 ],
Austin Schuhbcce26a2018-03-26 23:41:24 -0700482 namespaces=namespaces,
483 scalar_type=scalar_type)
Diana Burgessd0180f12018-03-21 21:24:17 -0700484 dog_loop_writer.AddConstant(
485 control_loop.Constant("kDt", "%f", drivetrain_low_low.dt))
486 dog_loop_writer.AddConstant(
487 control_loop.Constant("kStallTorque", "%f",
488 drivetrain_low_low.stall_torque))
489 dog_loop_writer.AddConstant(
490 control_loop.Constant("kStallCurrent", "%f",
491 drivetrain_low_low.stall_current))
492 dog_loop_writer.AddConstant(
493 control_loop.Constant("kFreeSpeed", "%f",
494 drivetrain_low_low.free_speed))
495 dog_loop_writer.AddConstant(
496 control_loop.Constant("kFreeCurrent", "%f",
497 drivetrain_low_low.free_current))
498 dog_loop_writer.AddConstant(
499 control_loop.Constant("kJ", "%f", drivetrain_low_low.J))
500 dog_loop_writer.AddConstant(
501 control_loop.Constant("kMass", "%f", drivetrain_low_low.mass))
502 dog_loop_writer.AddConstant(
503 control_loop.Constant("kRobotRadius", "%f",
504 drivetrain_low_low.robot_radius))
505 dog_loop_writer.AddConstant(
506 control_loop.Constant("kWheelRadius", "%f", drivetrain_low_low.r))
507 dog_loop_writer.AddConstant(
508 control_loop.Constant("kR", "%f", drivetrain_low_low.resistance))
509 dog_loop_writer.AddConstant(
510 control_loop.Constant("kV", "%f", drivetrain_low_low.Kv))
511 dog_loop_writer.AddConstant(
512 control_loop.Constant("kT", "%f", drivetrain_low_low.Kt))
513 dog_loop_writer.AddConstant(
514 control_loop.Constant("kLowGearRatio", "%f", drivetrain_low_low.G_low))
515 dog_loop_writer.AddConstant(
516 control_loop.Constant("kHighGearRatio", "%f",
517 drivetrain_high_high.G_high))
518 dog_loop_writer.AddConstant(
519 control_loop.Constant(
520 "kHighOutputRatio", "%f",
521 drivetrain_high_high.G_high * drivetrain_high_high.r))
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800522
Diana Burgessd0180f12018-03-21 21:24:17 -0700523 dog_loop_writer.Write(drivetrain_files[0], drivetrain_files[1])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800524
Diana Burgessd0180f12018-03-21 21:24:17 -0700525 kf_loop_writer = control_loop.ControlLoopWriter(
526 "KFDrivetrain", [
527 kf_drivetrain_low_low, kf_drivetrain_low_high,
528 kf_drivetrain_high_low, kf_drivetrain_high_high
529 ],
Austin Schuhbcce26a2018-03-26 23:41:24 -0700530 namespaces=namespaces,
531 scalar_type=scalar_type)
Diana Burgessd0180f12018-03-21 21:24:17 -0700532 kf_loop_writer.Write(kf_drivetrain_files[0], kf_drivetrain_files[1])
533
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800534
535def PlotDrivetrainMotions(drivetrain_params):
Austin Schuh1542ea32021-01-23 20:19:50 -0800536 # Test out the voltage error.
537 drivetrain = KFDrivetrain(
538 left_low=False, right_low=False, drivetrain_params=drivetrain_params)
539 close_loop_left = []
540 close_loop_right = []
541 left_power = []
542 right_power = []
543 R = numpy.matrix([[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]])
Austin Schuh5ea48472021-02-02 20:46:41 -0800544 for _ in range(300):
Austin Schuh1542ea32021-01-23 20:19:50 -0800545 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min,
546 drivetrain.U_max)
547 drivetrain.UpdateObserver(U)
548 drivetrain.Update(U + numpy.matrix([[1.0], [1.0]]))
549 close_loop_left.append(drivetrain.X[0, 0])
550 close_loop_right.append(drivetrain.X[2, 0])
551 left_power.append(U[0, 0])
552 right_power.append(U[1, 0])
553
554 t = [drivetrain.dt * x for x in range(300)]
555 pylab.plot(t, close_loop_left, label='left position')
556 pylab.plot(t, close_loop_right, 'm--', label='right position')
557 pylab.plot(t, left_power, label='left power')
558 pylab.plot(t, right_power, '--', label='right power')
559 pylab.suptitle('Voltage error')
560 pylab.legend()
561 pylab.show()
562
Diana Burgessd0180f12018-03-21 21:24:17 -0700563 # Simulate the response of the system to a step input.
Austin Schuh1542ea32021-01-23 20:19:50 -0800564 drivetrain = KFDrivetrain(
Diana Burgessd0180f12018-03-21 21:24:17 -0700565 left_low=False, right_low=False, drivetrain_params=drivetrain_params)
566 simulated_left = []
567 simulated_right = []
Austin Schuh5ea48472021-02-02 20:46:41 -0800568 for _ in range(100):
Diana Burgessd0180f12018-03-21 21:24:17 -0700569 drivetrain.Update(numpy.matrix([[12.0], [12.0]]))
570 simulated_left.append(drivetrain.X[0, 0])
571 simulated_right.append(drivetrain.X[2, 0])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800572
Diana Burgessd0180f12018-03-21 21:24:17 -0700573 pylab.rc('lines', linewidth=4)
574 pylab.plot(range(100), simulated_left, label='left position')
575 pylab.plot(range(100), simulated_right, 'r--', label='right position')
576 pylab.suptitle('Acceleration Test\n12 Volt Step Input')
577 pylab.legend(loc='lower right')
578 pylab.show()
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800579
Diana Burgessd0180f12018-03-21 21:24:17 -0700580 # Simulate forwards motion.
Austin Schuh1542ea32021-01-23 20:19:50 -0800581 drivetrain = KFDrivetrain(
Diana Burgessd0180f12018-03-21 21:24:17 -0700582 left_low=False, right_low=False, drivetrain_params=drivetrain_params)
583 close_loop_left = []
584 close_loop_right = []
585 left_power = []
586 right_power = []
Austin Schuh1542ea32021-01-23 20:19:50 -0800587 R = numpy.matrix([[1.0], [0.0], [1.0], [0.0], [0.0], [0.0], [0.0]])
Austin Schuh5ea48472021-02-02 20:46:41 -0800588 for _ in range(300):
Diana Burgessd0180f12018-03-21 21:24:17 -0700589 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min,
590 drivetrain.U_max)
591 drivetrain.UpdateObserver(U)
592 drivetrain.Update(U)
593 close_loop_left.append(drivetrain.X[0, 0])
594 close_loop_right.append(drivetrain.X[2, 0])
595 left_power.append(U[0, 0])
596 right_power.append(U[1, 0])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800597
Austin Schuh1542ea32021-01-23 20:19:50 -0800598 t = [drivetrain.dt * x for x in range(300)]
599 pylab.plot(t, close_loop_left, label='left position')
600 pylab.plot(t, close_loop_right, 'm--', label='right position')
601 pylab.plot(t, left_power, label='left power')
602 pylab.plot(t, right_power, '--', label='right power')
Diana Burgessd0180f12018-03-21 21:24:17 -0700603 pylab.suptitle('Linear Move\nLeft and Right Position going to 1')
604 pylab.legend()
605 pylab.show()
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800606
Diana Burgessd0180f12018-03-21 21:24:17 -0700607 # Try turning in place
Austin Schuh1542ea32021-01-23 20:19:50 -0800608 drivetrain = KFDrivetrain(drivetrain_params=drivetrain_params)
Diana Burgessd0180f12018-03-21 21:24:17 -0700609 close_loop_left = []
610 close_loop_right = []
Austin Schuh1542ea32021-01-23 20:19:50 -0800611 R = numpy.matrix([[-1.0], [0.0], [1.0], [0.0], [0.0], [0.0], [0.0]])
Austin Schuh5ea48472021-02-02 20:46:41 -0800612 for _ in range(200):
Diana Burgessd0180f12018-03-21 21:24:17 -0700613 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min,
614 drivetrain.U_max)
615 drivetrain.UpdateObserver(U)
616 drivetrain.Update(U)
617 close_loop_left.append(drivetrain.X[0, 0])
618 close_loop_right.append(drivetrain.X[2, 0])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800619
Diana Burgessd0180f12018-03-21 21:24:17 -0700620 pylab.plot(range(200), close_loop_left, label='left position')
621 pylab.plot(range(200), close_loop_right, label='right position')
622 pylab.suptitle(
Ravago Jones26f7ad02021-02-05 15:45:59 -0800623 'Angular Move\nLeft position going to -1 and right position going to 1'
624 )
Diana Burgessd0180f12018-03-21 21:24:17 -0700625 pylab.legend(loc='center right')
626 pylab.show()
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800627
Diana Burgessd0180f12018-03-21 21:24:17 -0700628 # Try turning just one side.
Austin Schuh1542ea32021-01-23 20:19:50 -0800629 drivetrain = KFDrivetrain(drivetrain_params=drivetrain_params)
Diana Burgessd0180f12018-03-21 21:24:17 -0700630 close_loop_left = []
631 close_loop_right = []
Austin Schuh1542ea32021-01-23 20:19:50 -0800632 R = numpy.matrix([[0.0], [0.0], [1.0], [0.0], [0.0], [0.0], [0.0]])
Austin Schuh5ea48472021-02-02 20:46:41 -0800633 for _ in range(300):
Diana Burgessd0180f12018-03-21 21:24:17 -0700634 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min,
635 drivetrain.U_max)
636 drivetrain.UpdateObserver(U)
637 drivetrain.Update(U)
638 close_loop_left.append(drivetrain.X[0, 0])
639 close_loop_right.append(drivetrain.X[2, 0])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800640
Diana Burgessd0180f12018-03-21 21:24:17 -0700641 pylab.plot(range(300), close_loop_left, label='left position')
642 pylab.plot(range(300), close_loop_right, label='right position')
643 pylab.suptitle(
644 'Pivot\nLeft position not changing and right position going to 1')
645 pylab.legend(loc='center right')
646 pylab.show()