blob: 1aec45972b7eec69bc1894c61ccadf1fc38c824c [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 Schuh1542ea32021-01-23 20:19:50 -080049 q_pos: float, q position for a single speed LQR controller.
50 q_pos_low: float, q position low gear LQR controller.
51 q_pos_high: float, q position high gear LQR controller.
52 q_vel: float, q velocity for a single speed LQR controller.
53 q_vel_low: float, q velocity low gear LQR controller.
54 q_vel_high: float, q velocity high gear LQR controller.
Diana Burgessd0180f12018-03-21 21:24:17 -070055 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.
Austin Schuh1542ea32021-01-23 20:19:50 -080061 controller_poles: array, An array of poles for the polydrivetrain
62 controller. (See control_loop.py)
Diana Burgessd0180f12018-03-21 21:24:17 -070063 observer_poles: array, An array of poles. (See control_loop.py)
Michael Schuh95fbcc52018-03-10 20:57:20 -080064 robot_cg_offset: offset in meters of CG from robot center to left side
Diana Burgessd0180f12018-03-21 21:24:17 -070065 """
Austin Schuhecc92a02019-01-20 17:42:19 -080066 if G is not None:
67 assert (G_high is None)
68 assert (G_low is None)
69 G_high = G
70 G_low = G
71 assert (G_high is not None)
72 assert (G_low is not None)
73
74 if q_pos is not None:
75 assert (q_pos_low is None)
76 assert (q_pos_high is None)
77 q_pos_low = q_pos
78 q_pos_high = q_pos
79 assert (q_pos_low is not None)
80 assert (q_pos_high is not None)
81
82 if q_vel is not None:
83 assert (q_vel_low is None)
84 assert (q_vel_high is None)
85 q_vel_low = q_vel
86 q_vel_high = q_vel
87 assert (q_vel_low is not None)
88 assert (q_vel_high is not None)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -080089
Diana Burgessd0180f12018-03-21 21:24:17 -070090 self.J = J
91 self.mass = mass
92 self.robot_radius = robot_radius
Michael Schuh95fbcc52018-03-10 20:57:20 -080093 self.robot_cg_offset = robot_cg_offset
Diana Burgessd0180f12018-03-21 21:24:17 -070094 self.wheel_radius = wheel_radius
95 self.G_high = G_high
96 self.G_low = G_low
97 self.dt = dt
98 self.q_pos_low = q_pos_low
99 self.q_pos_high = q_pos_high
100 self.q_vel_low = q_vel_low
101 self.q_vel_high = q_vel_high
102 self.efficiency = efficiency
103 self.has_imu = has_imu
104 self.kf_q_voltage = kf_q_voltage
105 self.motor_type = motor_type
106 self.force = force
107 self.num_motors = num_motors
108 self.controller_poles = controller_poles
109 self.observer_poles = observer_poles
110
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800111
112class Drivetrain(control_loop.ControlLoop):
Tyler Chatow6738c362019-02-16 14:12:30 -0800113
Diana Burgessd0180f12018-03-21 21:24:17 -0700114 def __init__(self,
115 drivetrain_params,
116 name="Drivetrain",
117 left_low=True,
118 right_low=True):
119 """Defines a base drivetrain for a robot.
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800120
Diana Burgessd0180f12018-03-21 21:24:17 -0700121 Args:
122 drivetrain_params: DrivetrainParams, class of values defining the drivetrain.
123 name: string, Name of this drivetrain.
124 left_low: bool, Whether the left is in high gear.
125 right_low: bool, Whether the right is in high gear.
126 """
127 super(Drivetrain, self).__init__(name)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800128
Diana Burgessd0180f12018-03-21 21:24:17 -0700129 # Moment of inertia of the drivetrain in kg m^2
130 self.J = drivetrain_params.J
131 # Mass of the robot, in kg.
132 self.mass = drivetrain_params.mass
133 # Radius of the robot, in meters (requires tuning by hand)
134 self.robot_radius = drivetrain_params.robot_radius
135 # Radius of the wheels, in meters.
136 self.r = drivetrain_params.wheel_radius
137 self.has_imu = drivetrain_params.has_imu
Michael Schuh95fbcc52018-03-10 20:57:20 -0800138 # Offset in meters of the CG from the center of the robot to the left side
139 # of the robot. Since the arm is on the right side, the offset will
140 # likely be a negative number.
141 self.robot_cg_offset = drivetrain_params.robot_cg_offset
142 # Distance from the left side of the robot to the Center of Gravity
143 self.robot_radius_l = drivetrain_params.robot_radius - self.robot_cg_offset
144 # Distance from the right side of the robot to the Center of Gravity
145 self.robot_radius_r = drivetrain_params.robot_radius + self.robot_cg_offset
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800146
Diana Burgessd0180f12018-03-21 21:24:17 -0700147 # Gear ratios
148 self.G_low = drivetrain_params.G_low
149 self.G_high = drivetrain_params.G_high
150 if left_low:
151 self.Gl = self.G_low
152 else:
153 self.Gl = self.G_high
154 if right_low:
155 self.Gr = self.G_low
156 else:
157 self.Gr = self.G_high
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800158
Diana Burgessd0180f12018-03-21 21:24:17 -0700159 # Control loop time step
160 self.dt = drivetrain_params.dt
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800161
Diana Burgessd0180f12018-03-21 21:24:17 -0700162 self.efficiency = drivetrain_params.efficiency
163 self.force = drivetrain_params.force
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800164
Diana Burgessd0180f12018-03-21 21:24:17 -0700165 self.BuildDrivetrain(drivetrain_params.motor_type,
166 drivetrain_params.num_motors)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800167
Diana Burgessd0180f12018-03-21 21:24:17 -0700168 if left_low or right_low:
169 q_pos = drivetrain_params.q_pos_low
170 q_vel = drivetrain_params.q_vel_low
171 else:
172 q_pos = drivetrain_params.q_pos_high
173 q_vel = drivetrain_params.q_vel_high
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800174
Diana Burgessd0180f12018-03-21 21:24:17 -0700175 self.BuildDrivetrainController(q_pos, q_vel)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800176
Diana Burgessd0180f12018-03-21 21:24:17 -0700177 self.InitializeState()
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800178
Diana Burgessd0180f12018-03-21 21:24:17 -0700179 def BuildDrivetrain(self, motor, num_motors_per_side):
180 self.motor = motor
181 # Number of motors per side
182 self.num_motors = num_motors_per_side
183 # Stall Torque in N m
184 self.stall_torque = motor.stall_torque * self.num_motors * self.efficiency
185 # Stall Current in Amps
186 self.stall_current = motor.stall_current * self.num_motors
187 # Free Speed in rad/s
188 self.free_speed = motor.free_speed
189 # Free Current in Amps
190 self.free_current = motor.free_current * self.num_motors
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800191
Diana Burgessd0180f12018-03-21 21:24:17 -0700192 # Effective motor resistance in ohms.
193 self.resistance = 12.0 / self.stall_current
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800194
Diana Burgessd0180f12018-03-21 21:24:17 -0700195 # Resistance of the motor, divided by the number of motors.
196 # Motor velocity constant
Tyler Chatow6738c362019-02-16 14:12:30 -0800197 self.Kv = (
198 self.free_speed / (12.0 - self.resistance * self.free_current))
Diana Burgessd0180f12018-03-21 21:24:17 -0700199 # Torque constant
200 self.Kt = self.stall_torque / self.stall_current
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800201
Diana Burgessd0180f12018-03-21 21:24:17 -0700202 # These describe the way that a given side of a robot will be influenced
203 # by the other side. Units of 1 / kg.
Michael Schuh95fbcc52018-03-10 20:57:20 -0800204 self.mspl = 1.0 / self.mass + self.robot_radius_l * self.robot_radius_l / self.J
205 self.mspr = 1.0 / self.mass + self.robot_radius_r * self.robot_radius_r / self.J
206 self.msnl = self.robot_radius_r / ( self.robot_radius_l * self.mass ) - \
207 self.robot_radius_l * self.robot_radius_r / self.J
208 self.msnr = self.robot_radius_l / ( self.robot_radius_r * self.mass ) - \
209 self.robot_radius_l * self.robot_radius_r / self.J
Diana Burgessd0180f12018-03-21 21:24:17 -0700210 # The calculations which we will need for A and B.
211 self.tcl = self.Kt / self.Kv / (
212 self.Gl * self.Gl * self.resistance * self.r * self.r)
213 self.tcr = self.Kt / self.Kv / (
214 self.Gr * self.Gr * self.resistance * self.r * self.r)
215 self.mpl = self.Kt / (self.Gl * self.resistance * self.r)
216 self.mpr = self.Kt / (self.Gr * self.resistance * self.r)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800217
Diana Burgessd0180f12018-03-21 21:24:17 -0700218 # State feedback matrices
219 # X will be of the format
Austin Schuhd77c0642020-12-30 21:38:54 -0800220 # [[positionl], [velocityl], [positionr], [velocityr]]
Diana Burgessd0180f12018-03-21 21:24:17 -0700221 self.A_continuous = numpy.matrix(
Tyler Chatow6738c362019-02-16 14:12:30 -0800222 [[0, 1, 0, 0], [0, -self.mspl * self.tcl, 0, -self.msnr * self.tcr],
223 [0, 0, 0, 1], [0, -self.msnl * self.tcl, 0,
224 -self.mspr * self.tcr]])
Diana Burgessd0180f12018-03-21 21:24:17 -0700225 self.B_continuous = numpy.matrix(
Tyler Chatow6738c362019-02-16 14:12:30 -0800226 [[0, 0], [self.mspl * self.mpl, self.msnr * self.mpr], [0, 0],
Michael Schuh95fbcc52018-03-10 20:57:20 -0800227 [self.msnl * self.mpl, self.mspr * self.mpr]])
Diana Burgessd0180f12018-03-21 21:24:17 -0700228 self.C = numpy.matrix([[1, 0, 0, 0], [0, 0, 1, 0]])
229 self.D = numpy.matrix([[0, 0], [0, 0]])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800230
Diana Burgessd0180f12018-03-21 21:24:17 -0700231 self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
232 self.B_continuous, self.dt)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800233
Diana Burgessd0180f12018-03-21 21:24:17 -0700234 def BuildDrivetrainController(self, q_pos, q_vel):
Austin Schuhd77c0642020-12-30 21:38:54 -0800235 # We can solve for the max velocity by setting \dot(x) = Ax + Bu to 0
236 max_voltage = 12
237 glog.debug(
238 "Max speed %f m/s",
239 -(self.B_continuous[1, 1] + self.B_continuous[1, 0]) /
240 (self.A_continuous[1, 1] + self.A_continuous[1, 3]) * max_voltage)
241
Diana Burgessd0180f12018-03-21 21:24:17 -0700242 # Tune the LQR controller
Tyler Chatow6738c362019-02-16 14:12:30 -0800243 self.Q = numpy.matrix([[(1.0 / (q_pos**2.0)), 0.0, 0.0, 0.0],
244 [0.0, (1.0 / (q_vel**2.0)), 0.0, 0.0],
245 [0.0, 0.0, (1.0 / (q_pos**2.0)), 0.0],
246 [0.0, 0.0, 0.0, (1.0 / (q_vel**2.0))]])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800247
Diana Burgessd0180f12018-03-21 21:24:17 -0700248 self.R = numpy.matrix([[(1.0 / (12.0**2.0)), 0.0],
249 [0.0, (1.0 / (12.0**2.0))]])
250 self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800251
Diana Burgessd0180f12018-03-21 21:24:17 -0700252 glog.debug('DT q_pos %f q_vel %s %s', q_pos, q_vel, self._name)
Austin Schuh1542ea32021-01-23 20:19:50 -0800253 glog.debug('Poles: %s', str(numpy.linalg.eig(self.A - self.B * self.K)[0]))
254 glog.debug('Time constants: %s hz',
255 str([
256 numpy.log(x) / -self.dt
257 for x in numpy.linalg.eig(self.A - self.B * self.K)[0]
258 ]))
Diana Burgessd0180f12018-03-21 21:24:17 -0700259 glog.debug('K %s', repr(self.K))
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800260
Diana Burgessd0180f12018-03-21 21:24:17 -0700261 self.hlp = 0.3
262 self.llp = 0.4
263 self.PlaceObserverPoles([self.hlp, self.hlp, self.llp, self.llp])
264
265 self.U_max = numpy.matrix([[12.0], [12.0]])
266 self.U_min = numpy.matrix([[-12.0], [-12.0]])
267
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800268
269class KFDrivetrain(Drivetrain):
Tyler Chatow6738c362019-02-16 14:12:30 -0800270
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],
311 [self.msnr, self.mspr]])
Diana Burgessd0180f12018-03-21 21:24:17 -0700312 q_voltage = drivetrain_params.kf_q_voltage * self.mpl
313 else:
314 self.A_continuous[0:4, 4:6] = self.unaugmented_B_continuous
315 q_voltage = drivetrain_params.kf_q_voltage
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800316
Diana Burgessd0180f12018-03-21 21:24:17 -0700317 self.B_continuous[0:4, 0:2] = self.unaugmented_B_continuous
318 self.A_continuous[0, 6] = 1
319 self.A_continuous[2, 6] = -1
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800320
Diana Burgessd0180f12018-03-21 21:24:17 -0700321 self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
322 self.B_continuous, self.dt)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800323
Diana Burgessd0180f12018-03-21 21:24:17 -0700324 if self.has_imu:
Tyler Chatow6738c362019-02-16 14:12:30 -0800325 self.C = numpy.matrix([[1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0],
326 [
327 0, -0.5 / drivetrain_params.robot_radius,
328 0, 0.5 / drivetrain_params.robot_radius,
329 0, 0, 0
330 ], [0, 0, 0, 0, 0, 0, 0]])
Diana Burgessd0180f12018-03-21 21:24:17 -0700331 gravity = 9.8
332 self.C[3, 0:6] = 0.5 * (
Tyler Chatow6738c362019-02-16 14:12:30 -0800333 self.A_continuous[1, 0:6] + self.A_continuous[3, 0:6]) / gravity
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800334
Tyler Chatow6738c362019-02-16 14:12:30 -0800335 self.D = numpy.matrix(
336 [[0, 0], [0, 0], [0, 0],
337 [
338 0.5 * (self.B_continuous[1, 0] + self.B_continuous[3, 0]) /
339 gravity,
340 0.5 * (self.B_continuous[1, 1] + self.B_continuous[3, 1]) /
341 gravity
342 ]])
Diana Burgessd0180f12018-03-21 21:24:17 -0700343 else:
Tyler Chatow6738c362019-02-16 14:12:30 -0800344 self.C = numpy.matrix([[1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0],
345 [
346 0, -0.5 / drivetrain_params.robot_radius,
347 0, 0.5 / drivetrain_params.robot_radius,
348 0, 0, 0
349 ]])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800350
Diana Burgessd0180f12018-03-21 21:24:17 -0700351 self.D = numpy.matrix([[0, 0], [0, 0], [0, 0]])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800352
Diana Burgessd0180f12018-03-21 21:24:17 -0700353 q_pos = 0.05
354 q_vel = 1.00
355 q_encoder_uncertainty = 2.00
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800356
Diana Burgessd0180f12018-03-21 21:24:17 -0700357 self.Q = numpy.matrix(
Tyler Chatow6738c362019-02-16 14:12:30 -0800358 [[(q_pos**2.0), 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
359 [0.0, (q_vel**2.0), 0.0, 0.0, 0.0, 0.0, 0.0],
360 [0.0, 0.0, (q_pos**2.0), 0.0, 0.0, 0.0, 0.0],
361 [0.0, 0.0, 0.0, (q_vel**2.0), 0.0, 0.0, 0.0],
362 [0.0, 0.0, 0.0, 0.0, (q_voltage**2.0), 0.0, 0.0],
363 [0.0, 0.0, 0.0, 0.0, 0.0, (q_voltage**2.0), 0.0],
Diana Burgessd0180f12018-03-21 21:24:17 -0700364 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, (q_encoder_uncertainty**2.0)]])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800365
Diana Burgessd0180f12018-03-21 21:24:17 -0700366 r_pos = 0.0001
367 r_gyro = 0.000001
368 if self.has_imu:
369 r_accelerometer = 7.0
370 self.R = numpy.matrix([[(r_pos**2.0), 0.0, 0.0, 0.0],
371 [0.0, (r_pos**2.0), 0.0, 0.0],
372 [0.0, 0.0, (r_gyro**2.0), 0.0],
373 [0.0, 0.0, 0.0, (r_accelerometer**2.0)]])
374 else:
375 self.R = numpy.matrix([[(r_pos**2.0), 0.0, 0.0],
376 [0.0, (r_pos**2.0), 0.0],
377 [0.0, 0.0, (r_gyro**2.0)]])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800378
Diana Burgessd0180f12018-03-21 21:24:17 -0700379 # Solving for kf gains.
380 self.KalmanGain, self.Q_steady = controls.kalman(
381 A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800382
James Kuszmaul4d752d52019-02-09 17:27:55 -0800383 # If we don't have an IMU, pad various matrices with zeros so that
384 # we can still have 4 measurement outputs.
Diana Burgessd0180f12018-03-21 21:24:17 -0700385 if not self.has_imu:
Tyler Chatow6738c362019-02-16 14:12:30 -0800386 self.KalmanGain = numpy.hstack((self.KalmanGain,
387 numpy.matrix(numpy.zeros((7, 1)))))
Austin Schuhecc92a02019-01-20 17:42:19 -0800388 self.C = numpy.vstack((self.C, numpy.matrix(numpy.zeros((1, 7)))))
389 self.D = numpy.vstack((self.D, numpy.matrix(numpy.zeros((1, 2)))))
James Kuszmaul4d752d52019-02-09 17:27:55 -0800390 Rtmp = numpy.zeros((4, 4))
391 Rtmp[0:3, 0:3] = self.R
392 self.R = Rtmp
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800393
Diana Burgessd0180f12018-03-21 21:24:17 -0700394 self.L = self.A * self.KalmanGain
395
396 unaug_K = self.K
397
398 # Implement a nice closed loop controller for use by the closed loop
399 # controller.
400 self.K = numpy.matrix(numpy.zeros((self.B.shape[1], self.A.shape[0])))
401 self.K[0:2, 0:4] = unaug_K
402 if self.force:
403 self.K[0, 4] = 1.0 / self.mpl
404 self.K[1, 5] = 1.0 / self.mpr
405 else:
406 self.K[0, 4] = 1.0
407 self.K[1, 5] = 1.0
408
409 self.Qff = numpy.matrix(numpy.zeros((4, 4)))
410 qff_pos = 0.005
411 qff_vel = 1.00
412 self.Qff[0, 0] = 1.0 / qff_pos**2.0
413 self.Qff[1, 1] = 1.0 / qff_vel**2.0
414 self.Qff[2, 2] = 1.0 / qff_pos**2.0
415 self.Qff[3, 3] = 1.0 / qff_vel**2.0
416 self.Kff = numpy.matrix(numpy.zeros((2, 7)))
417 self.Kff[0:2, 0:4] = controls.TwoStateFeedForwards(
418 self.B[0:4, :], self.Qff)
419
420 self.InitializeState()
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800421
422
Tyler Chatow6738c362019-02-16 14:12:30 -0800423def WriteDrivetrain(drivetrain_files,
424 kf_drivetrain_files,
425 year_namespace,
426 drivetrain_params,
427 scalar_type='double'):
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800428
Diana Burgessd0180f12018-03-21 21:24:17 -0700429 # Write the generated constants out to a file.
430 drivetrain_low_low = Drivetrain(
431 name="DrivetrainLowLow",
432 left_low=True,
433 right_low=True,
434 drivetrain_params=drivetrain_params)
435 drivetrain_low_high = Drivetrain(
436 name="DrivetrainLowHigh",
437 left_low=True,
438 right_low=False,
439 drivetrain_params=drivetrain_params)
440 drivetrain_high_low = Drivetrain(
441 name="DrivetrainHighLow",
442 left_low=False,
443 right_low=True,
444 drivetrain_params=drivetrain_params)
445 drivetrain_high_high = Drivetrain(
446 name="DrivetrainHighHigh",
447 left_low=False,
448 right_low=False,
449 drivetrain_params=drivetrain_params)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800450
Diana Burgessd0180f12018-03-21 21:24:17 -0700451 kf_drivetrain_low_low = KFDrivetrain(
452 name="KFDrivetrainLowLow",
453 left_low=True,
454 right_low=True,
455 drivetrain_params=drivetrain_params)
456 kf_drivetrain_low_high = KFDrivetrain(
457 name="KFDrivetrainLowHigh",
458 left_low=True,
459 right_low=False,
460 drivetrain_params=drivetrain_params)
461 kf_drivetrain_high_low = KFDrivetrain(
462 name="KFDrivetrainHighLow",
463 left_low=False,
464 right_low=True,
465 drivetrain_params=drivetrain_params)
466 kf_drivetrain_high_high = KFDrivetrain(
467 name="KFDrivetrainHighHigh",
468 left_low=False,
469 right_low=False,
470 drivetrain_params=drivetrain_params)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800471
Austin Schuhbcce26a2018-03-26 23:41:24 -0700472 if isinstance(year_namespace, list):
Austin Schuhecc92a02019-01-20 17:42:19 -0800473 namespaces = year_namespace
Austin Schuhbcce26a2018-03-26 23:41:24 -0700474 else:
Austin Schuhecc92a02019-01-20 17:42:19 -0800475 namespaces = [year_namespace, 'control_loops', 'drivetrain']
Diana Burgessd0180f12018-03-21 21:24:17 -0700476 dog_loop_writer = control_loop.ControlLoopWriter(
477 "Drivetrain", [
478 drivetrain_low_low, drivetrain_low_high, drivetrain_high_low,
479 drivetrain_high_high
480 ],
Austin Schuhbcce26a2018-03-26 23:41:24 -0700481 namespaces=namespaces,
482 scalar_type=scalar_type)
Diana Burgessd0180f12018-03-21 21:24:17 -0700483 dog_loop_writer.AddConstant(
484 control_loop.Constant("kDt", "%f", drivetrain_low_low.dt))
485 dog_loop_writer.AddConstant(
486 control_loop.Constant("kStallTorque", "%f",
487 drivetrain_low_low.stall_torque))
488 dog_loop_writer.AddConstant(
489 control_loop.Constant("kStallCurrent", "%f",
490 drivetrain_low_low.stall_current))
491 dog_loop_writer.AddConstant(
492 control_loop.Constant("kFreeSpeed", "%f",
493 drivetrain_low_low.free_speed))
494 dog_loop_writer.AddConstant(
495 control_loop.Constant("kFreeCurrent", "%f",
496 drivetrain_low_low.free_current))
497 dog_loop_writer.AddConstant(
498 control_loop.Constant("kJ", "%f", drivetrain_low_low.J))
499 dog_loop_writer.AddConstant(
500 control_loop.Constant("kMass", "%f", drivetrain_low_low.mass))
501 dog_loop_writer.AddConstant(
502 control_loop.Constant("kRobotRadius", "%f",
503 drivetrain_low_low.robot_radius))
504 dog_loop_writer.AddConstant(
505 control_loop.Constant("kWheelRadius", "%f", drivetrain_low_low.r))
506 dog_loop_writer.AddConstant(
507 control_loop.Constant("kR", "%f", drivetrain_low_low.resistance))
508 dog_loop_writer.AddConstant(
509 control_loop.Constant("kV", "%f", drivetrain_low_low.Kv))
510 dog_loop_writer.AddConstant(
511 control_loop.Constant("kT", "%f", drivetrain_low_low.Kt))
512 dog_loop_writer.AddConstant(
513 control_loop.Constant("kLowGearRatio", "%f", drivetrain_low_low.G_low))
514 dog_loop_writer.AddConstant(
515 control_loop.Constant("kHighGearRatio", "%f",
516 drivetrain_high_high.G_high))
517 dog_loop_writer.AddConstant(
518 control_loop.Constant(
519 "kHighOutputRatio", "%f",
520 drivetrain_high_high.G_high * drivetrain_high_high.r))
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800521
Diana Burgessd0180f12018-03-21 21:24:17 -0700522 dog_loop_writer.Write(drivetrain_files[0], drivetrain_files[1])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800523
Diana Burgessd0180f12018-03-21 21:24:17 -0700524 kf_loop_writer = control_loop.ControlLoopWriter(
525 "KFDrivetrain", [
526 kf_drivetrain_low_low, kf_drivetrain_low_high,
527 kf_drivetrain_high_low, kf_drivetrain_high_high
528 ],
Austin Schuhbcce26a2018-03-26 23:41:24 -0700529 namespaces=namespaces,
530 scalar_type=scalar_type)
Diana Burgessd0180f12018-03-21 21:24:17 -0700531 kf_loop_writer.Write(kf_drivetrain_files[0], kf_drivetrain_files[1])
532
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800533
534def PlotDrivetrainMotions(drivetrain_params):
Austin Schuh1542ea32021-01-23 20:19:50 -0800535 # Test out the voltage error.
536 drivetrain = KFDrivetrain(
537 left_low=False, right_low=False, drivetrain_params=drivetrain_params)
538 close_loop_left = []
539 close_loop_right = []
540 left_power = []
541 right_power = []
542 R = numpy.matrix([[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]])
543 for _ in xrange(300):
544 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min,
545 drivetrain.U_max)
546 drivetrain.UpdateObserver(U)
547 drivetrain.Update(U + numpy.matrix([[1.0], [1.0]]))
548 close_loop_left.append(drivetrain.X[0, 0])
549 close_loop_right.append(drivetrain.X[2, 0])
550 left_power.append(U[0, 0])
551 right_power.append(U[1, 0])
552
553 t = [drivetrain.dt * x for x in range(300)]
554 pylab.plot(t, close_loop_left, label='left position')
555 pylab.plot(t, close_loop_right, 'm--', label='right position')
556 pylab.plot(t, left_power, label='left power')
557 pylab.plot(t, right_power, '--', label='right power')
558 pylab.suptitle('Voltage error')
559 pylab.legend()
560 pylab.show()
561
Diana Burgessd0180f12018-03-21 21:24:17 -0700562 # Simulate the response of the system to a step input.
Austin Schuh1542ea32021-01-23 20:19:50 -0800563 drivetrain = KFDrivetrain(
Diana Burgessd0180f12018-03-21 21:24:17 -0700564 left_low=False, right_low=False, drivetrain_params=drivetrain_params)
565 simulated_left = []
566 simulated_right = []
567 for _ in xrange(100):
568 drivetrain.Update(numpy.matrix([[12.0], [12.0]]))
569 simulated_left.append(drivetrain.X[0, 0])
570 simulated_right.append(drivetrain.X[2, 0])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800571
Diana Burgessd0180f12018-03-21 21:24:17 -0700572 pylab.rc('lines', linewidth=4)
573 pylab.plot(range(100), simulated_left, label='left position')
574 pylab.plot(range(100), simulated_right, 'r--', label='right position')
575 pylab.suptitle('Acceleration Test\n12 Volt Step Input')
576 pylab.legend(loc='lower right')
577 pylab.show()
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800578
Diana Burgessd0180f12018-03-21 21:24:17 -0700579 # Simulate forwards motion.
Austin Schuh1542ea32021-01-23 20:19:50 -0800580 drivetrain = KFDrivetrain(
Diana Burgessd0180f12018-03-21 21:24:17 -0700581 left_low=False, right_low=False, drivetrain_params=drivetrain_params)
582 close_loop_left = []
583 close_loop_right = []
584 left_power = []
585 right_power = []
Austin Schuh1542ea32021-01-23 20:19:50 -0800586 R = numpy.matrix([[1.0], [0.0], [1.0], [0.0], [0.0], [0.0], [0.0]])
Diana Burgessd0180f12018-03-21 21:24:17 -0700587 for _ in xrange(300):
588 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min,
589 drivetrain.U_max)
590 drivetrain.UpdateObserver(U)
591 drivetrain.Update(U)
592 close_loop_left.append(drivetrain.X[0, 0])
593 close_loop_right.append(drivetrain.X[2, 0])
594 left_power.append(U[0, 0])
595 right_power.append(U[1, 0])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800596
Austin Schuh1542ea32021-01-23 20:19:50 -0800597 t = [drivetrain.dt * x for x in range(300)]
598 pylab.plot(t, close_loop_left, label='left position')
599 pylab.plot(t, close_loop_right, 'm--', label='right position')
600 pylab.plot(t, left_power, label='left power')
601 pylab.plot(t, right_power, '--', label='right power')
Diana Burgessd0180f12018-03-21 21:24:17 -0700602 pylab.suptitle('Linear Move\nLeft and Right Position going to 1')
603 pylab.legend()
604 pylab.show()
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800605
Diana Burgessd0180f12018-03-21 21:24:17 -0700606 # Try turning in place
Austin Schuh1542ea32021-01-23 20:19:50 -0800607 drivetrain = KFDrivetrain(drivetrain_params=drivetrain_params)
Diana Burgessd0180f12018-03-21 21:24:17 -0700608 close_loop_left = []
609 close_loop_right = []
Austin Schuh1542ea32021-01-23 20:19:50 -0800610 R = numpy.matrix([[-1.0], [0.0], [1.0], [0.0], [0.0], [0.0], [0.0]])
Diana Burgessd0180f12018-03-21 21:24:17 -0700611 for _ in xrange(200):
612 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min,
613 drivetrain.U_max)
614 drivetrain.UpdateObserver(U)
615 drivetrain.Update(U)
616 close_loop_left.append(drivetrain.X[0, 0])
617 close_loop_right.append(drivetrain.X[2, 0])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800618
Diana Burgessd0180f12018-03-21 21:24:17 -0700619 pylab.plot(range(200), close_loop_left, label='left position')
620 pylab.plot(range(200), close_loop_right, label='right position')
621 pylab.suptitle(
Tyler Chatow6738c362019-02-16 14:12:30 -0800622 'Angular Move\nLeft position going to -1 and right position going to 1')
Diana Burgessd0180f12018-03-21 21:24:17 -0700623 pylab.legend(loc='center right')
624 pylab.show()
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800625
Diana Burgessd0180f12018-03-21 21:24:17 -0700626 # Try turning just one side.
Austin Schuh1542ea32021-01-23 20:19:50 -0800627 drivetrain = KFDrivetrain(drivetrain_params=drivetrain_params)
Diana Burgessd0180f12018-03-21 21:24:17 -0700628 close_loop_left = []
629 close_loop_right = []
Austin Schuh1542ea32021-01-23 20:19:50 -0800630 R = numpy.matrix([[0.0], [0.0], [1.0], [0.0], [0.0], [0.0], [0.0]])
Diana Burgessd0180f12018-03-21 21:24:17 -0700631 for _ in xrange(300):
632 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min,
633 drivetrain.U_max)
634 drivetrain.UpdateObserver(U)
635 drivetrain.Update(U)
636 close_loop_left.append(drivetrain.X[0, 0])
637 close_loop_right.append(drivetrain.X[2, 0])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800638
Diana Burgessd0180f12018-03-21 21:24:17 -0700639 pylab.plot(range(300), close_loop_left, label='left position')
640 pylab.plot(range(300), close_loop_right, label='right position')
641 pylab.suptitle(
642 'Pivot\nLeft position not changing and right position going to 1')
643 pylab.legend(loc='center right')
644 pylab.show()