blob: 2fed5f0230edbae87ebf5ff18e81c7c378897eaf [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
Austin Schuh566c82b2023-01-27 20:49:08 -08006import math
Campbell Crowley33e0e3d2017-12-27 17:55:40 -08007import sys
8from matplotlib import pylab
9import glog
10
Tyler Chatow6738c362019-02-16 14:12:30 -080011
Campbell Crowley33e0e3d2017-12-27 17:55:40 -080012class DrivetrainParams(object):
Ravago Jones5127ccc2022-07-31 16:32:45 -070013
Diana Burgessd0180f12018-03-21 21:24:17 -070014 def __init__(self,
15 J,
16 mass,
17 robot_radius,
18 wheel_radius,
Austin Schuhecc92a02019-01-20 17:42:19 -080019 G=None,
20 G_high=None,
21 G_low=None,
22 q_pos=None,
23 q_pos_low=None,
24 q_pos_high=None,
25 q_vel=None,
26 q_vel_low=None,
27 q_vel_high=None,
Diana Burgessd0180f12018-03-21 21:24:17 -070028 efficiency=0.60,
29 has_imu=False,
30 force=False,
31 kf_q_voltage=10.0,
32 motor_type=control_loop.CIM(),
33 num_motors=2,
34 dt=0.00505,
35 controller_poles=[0.90, 0.90],
Michael Schuh95fbcc52018-03-10 20:57:20 -080036 observer_poles=[0.02, 0.02],
Austin Schuh566c82b2023-01-27 20:49:08 -080037 robot_cg_offset=0.0,
38 coefficient_of_friction=1.0):
Diana Burgessd0180f12018-03-21 21:24:17 -070039 """Defines all constants of a drivetrain.
Campbell Crowley33e0e3d2017-12-27 17:55:40 -080040
Diana Burgessd0180f12018-03-21 21:24:17 -070041 Args:
42 J: float, Moment of inertia of drivetrain in kg m^2
43 mass: float, Mass of the robot in kg.
44 robot_radius: float, Radius of the robot, in meters (requires tuning by
45 hand).
46 wheel_radius: float, Radius of the wheels, in meters.
Austin Schuhecc92a02019-01-20 17:42:19 -080047 G: float, Gear ratio for a single speed.
Diana Burgessd0180f12018-03-21 21:24:17 -070048 G_high: float, Gear ratio for high gear.
49 G_low: float, Gear ratio for low gear.
50 dt: float, Control loop time step.
Austin Schuh1542ea32021-01-23 20:19:50 -080051 q_pos: float, q position for a single speed LQR controller.
52 q_pos_low: float, q position low gear LQR controller.
53 q_pos_high: float, q position high gear LQR controller.
54 q_vel: float, q velocity for a single speed LQR controller.
55 q_vel_low: float, q velocity low gear LQR controller.
56 q_vel_high: float, q velocity high gear LQR controller.
Diana Burgessd0180f12018-03-21 21:24:17 -070057 efficiency: float, gear box effiency.
58 has_imu: bool, true if imu is present.
59 force: bool, true if force.
60 kf_q_voltage: float
61 motor_type: object, class of values defining the motor in drivetrain.
62 num_motors: int, number of motors on one side of drivetrain.
Austin Schuh1542ea32021-01-23 20:19:50 -080063 controller_poles: array, An array of poles for the polydrivetrain
64 controller. (See control_loop.py)
Diana Burgessd0180f12018-03-21 21:24:17 -070065 observer_poles: array, An array of poles. (See control_loop.py)
Michael Schuh95fbcc52018-03-10 20:57:20 -080066 robot_cg_offset: offset in meters of CG from robot center to left side
Diana Burgessd0180f12018-03-21 21:24:17 -070067 """
Austin Schuhecc92a02019-01-20 17:42:19 -080068 if G is not None:
69 assert (G_high is None)
70 assert (G_low is None)
71 G_high = G
72 G_low = G
73 assert (G_high is not None)
74 assert (G_low is not None)
75
76 if q_pos is not None:
77 assert (q_pos_low is None)
78 assert (q_pos_high is None)
79 q_pos_low = q_pos
80 q_pos_high = q_pos
81 assert (q_pos_low is not None)
82 assert (q_pos_high is not None)
83
84 if q_vel is not None:
85 assert (q_vel_low is None)
86 assert (q_vel_high is None)
87 q_vel_low = q_vel
88 q_vel_high = q_vel
89 assert (q_vel_low is not None)
90 assert (q_vel_high is not None)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -080091
Diana Burgessd0180f12018-03-21 21:24:17 -070092 self.J = J
93 self.mass = mass
94 self.robot_radius = robot_radius
Michael Schuh95fbcc52018-03-10 20:57:20 -080095 self.robot_cg_offset = robot_cg_offset
Diana Burgessd0180f12018-03-21 21:24:17 -070096 self.wheel_radius = wheel_radius
97 self.G_high = G_high
98 self.G_low = G_low
99 self.dt = dt
100 self.q_pos_low = q_pos_low
101 self.q_pos_high = q_pos_high
102 self.q_vel_low = q_vel_low
103 self.q_vel_high = q_vel_high
104 self.efficiency = efficiency
105 self.has_imu = has_imu
106 self.kf_q_voltage = kf_q_voltage
107 self.motor_type = motor_type
108 self.force = force
109 self.num_motors = num_motors
110 self.controller_poles = controller_poles
111 self.observer_poles = observer_poles
Austin Schuh566c82b2023-01-27 20:49:08 -0800112 self.coefficient_of_friction = coefficient_of_friction
Diana Burgessd0180f12018-03-21 21:24:17 -0700113
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800114
115class Drivetrain(control_loop.ControlLoop):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700116
Diana Burgessd0180f12018-03-21 21:24:17 -0700117 def __init__(self,
118 drivetrain_params,
119 name="Drivetrain",
120 left_low=True,
121 right_low=True):
122 """Defines a base drivetrain for a robot.
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800123
Diana Burgessd0180f12018-03-21 21:24:17 -0700124 Args:
125 drivetrain_params: DrivetrainParams, class of values defining the drivetrain.
126 name: string, Name of this drivetrain.
127 left_low: bool, Whether the left is in high gear.
128 right_low: bool, Whether the right is in high gear.
129 """
130 super(Drivetrain, self).__init__(name)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800131
Diana Burgessd0180f12018-03-21 21:24:17 -0700132 # Moment of inertia of the drivetrain in kg m^2
133 self.J = drivetrain_params.J
134 # Mass of the robot, in kg.
135 self.mass = drivetrain_params.mass
136 # Radius of the robot, in meters (requires tuning by hand)
137 self.robot_radius = drivetrain_params.robot_radius
138 # Radius of the wheels, in meters.
139 self.r = drivetrain_params.wheel_radius
140 self.has_imu = drivetrain_params.has_imu
Michael Schuh95fbcc52018-03-10 20:57:20 -0800141 # Offset in meters of the CG from the center of the robot to the left side
142 # of the robot. Since the arm is on the right side, the offset will
143 # likely be a negative number.
144 self.robot_cg_offset = drivetrain_params.robot_cg_offset
145 # Distance from the left side of the robot to the Center of Gravity
146 self.robot_radius_l = drivetrain_params.robot_radius - self.robot_cg_offset
147 # Distance from the right side of the robot to the Center of Gravity
148 self.robot_radius_r = drivetrain_params.robot_radius + self.robot_cg_offset
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800149
Diana Burgessd0180f12018-03-21 21:24:17 -0700150 # Gear ratios
151 self.G_low = drivetrain_params.G_low
152 self.G_high = drivetrain_params.G_high
153 if left_low:
154 self.Gl = self.G_low
155 else:
156 self.Gl = self.G_high
157 if right_low:
158 self.Gr = self.G_low
159 else:
160 self.Gr = self.G_high
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800161
Diana Burgessd0180f12018-03-21 21:24:17 -0700162 # Control loop time step
163 self.dt = drivetrain_params.dt
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800164
Diana Burgessd0180f12018-03-21 21:24:17 -0700165 self.efficiency = drivetrain_params.efficiency
166 self.force = drivetrain_params.force
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800167
Diana Burgessd0180f12018-03-21 21:24:17 -0700168 self.BuildDrivetrain(drivetrain_params.motor_type,
169 drivetrain_params.num_motors)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800170
Diana Burgessd0180f12018-03-21 21:24:17 -0700171 if left_low or right_low:
172 q_pos = drivetrain_params.q_pos_low
173 q_vel = drivetrain_params.q_vel_low
174 else:
175 q_pos = drivetrain_params.q_pos_high
176 q_vel = drivetrain_params.q_vel_high
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800177
Diana Burgessd0180f12018-03-21 21:24:17 -0700178 self.BuildDrivetrainController(q_pos, q_vel)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800179
Diana Burgessd0180f12018-03-21 21:24:17 -0700180 self.InitializeState()
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800181
Diana Burgessd0180f12018-03-21 21:24:17 -0700182 def BuildDrivetrain(self, motor, num_motors_per_side):
183 self.motor = motor
184 # Number of motors per side
185 self.num_motors = num_motors_per_side
186 # Stall Torque in N m
187 self.stall_torque = motor.stall_torque * self.num_motors * self.efficiency
188 # Stall Current in Amps
189 self.stall_current = motor.stall_current * self.num_motors
190 # Free Speed in rad/s
191 self.free_speed = motor.free_speed
192 # Free Current in Amps
193 self.free_current = motor.free_current * self.num_motors
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800194
Diana Burgessd0180f12018-03-21 21:24:17 -0700195 # Effective motor resistance in ohms.
196 self.resistance = 12.0 / self.stall_current
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800197
Diana Burgessd0180f12018-03-21 21:24:17 -0700198 # Resistance of the motor, divided by the number of motors.
199 # Motor velocity constant
Ravago Jones5127ccc2022-07-31 16:32:45 -0700200 self.Kv = (self.free_speed /
201 (12.0 - self.resistance * self.free_current))
Diana Burgessd0180f12018-03-21 21:24:17 -0700202 # Torque constant
203 self.Kt = self.stall_torque / self.stall_current
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800204
Diana Burgessd0180f12018-03-21 21:24:17 -0700205 # These describe the way that a given side of a robot will be influenced
206 # by the other side. Units of 1 / kg.
Michael Schuh95fbcc52018-03-10 20:57:20 -0800207 self.mspl = 1.0 / self.mass + self.robot_radius_l * self.robot_radius_l / self.J
208 self.mspr = 1.0 / self.mass + self.robot_radius_r * self.robot_radius_r / self.J
209 self.msnl = self.robot_radius_r / ( self.robot_radius_l * self.mass ) - \
210 self.robot_radius_l * self.robot_radius_r / self.J
211 self.msnr = self.robot_radius_l / ( self.robot_radius_r * self.mass ) - \
212 self.robot_radius_l * self.robot_radius_r / self.J
Diana Burgessd0180f12018-03-21 21:24:17 -0700213 # The calculations which we will need for A and B.
Ravago Jones5127ccc2022-07-31 16:32:45 -0700214 self.tcl = self.Kt / self.Kv / (self.Gl * self.Gl * self.resistance *
215 self.r * self.r)
216 self.tcr = self.Kt / self.Kv / (self.Gr * self.Gr * self.resistance *
217 self.r * self.r)
Diana Burgessd0180f12018-03-21 21:24:17 -0700218 self.mpl = self.Kt / (self.Gl * self.resistance * self.r)
219 self.mpr = self.Kt / (self.Gr * self.resistance * self.r)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800220
Diana Burgessd0180f12018-03-21 21:24:17 -0700221 # State feedback matrices
222 # X will be of the format
Austin Schuhd77c0642020-12-30 21:38:54 -0800223 # [[positionl], [velocityl], [positionr], [velocityr]]
Diana Burgessd0180f12018-03-21 21:24:17 -0700224 self.A_continuous = numpy.matrix(
Ravago Jones26f7ad02021-02-05 15:45:59 -0800225 [[0, 1, 0,
226 0], [0, -self.mspl * self.tcl, 0, -self.msnr * self.tcr],
227 [0, 0, 0, 1],
228 [0, -self.msnl * self.tcl, 0, -self.mspr * self.tcr]])
Diana Burgessd0180f12018-03-21 21:24:17 -0700229 self.B_continuous = numpy.matrix(
Tyler Chatow6738c362019-02-16 14:12:30 -0800230 [[0, 0], [self.mspl * self.mpl, self.msnr * self.mpr], [0, 0],
Michael Schuh95fbcc52018-03-10 20:57:20 -0800231 [self.msnl * self.mpl, self.mspr * self.mpr]])
Diana Burgessd0180f12018-03-21 21:24:17 -0700232 self.C = numpy.matrix([[1, 0, 0, 0], [0, 0, 1, 0]])
233 self.D = numpy.matrix([[0, 0], [0, 0]])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800234
Diana Burgessd0180f12018-03-21 21:24:17 -0700235 self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
236 self.B_continuous, self.dt)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800237
Diana Burgessd0180f12018-03-21 21:24:17 -0700238 def BuildDrivetrainController(self, q_pos, q_vel):
Austin Schuhd77c0642020-12-30 21:38:54 -0800239 # We can solve for the max velocity by setting \dot(x) = Ax + Bu to 0
240 max_voltage = 12
241 glog.debug(
242 "Max speed %f m/s",
243 -(self.B_continuous[1, 1] + self.B_continuous[1, 0]) /
244 (self.A_continuous[1, 1] + self.A_continuous[1, 3]) * max_voltage)
245
Diana Burgessd0180f12018-03-21 21:24:17 -0700246 # Tune the LQR controller
Tyler Chatow6738c362019-02-16 14:12:30 -0800247 self.Q = numpy.matrix([[(1.0 / (q_pos**2.0)), 0.0, 0.0, 0.0],
248 [0.0, (1.0 / (q_vel**2.0)), 0.0, 0.0],
249 [0.0, 0.0, (1.0 / (q_pos**2.0)), 0.0],
250 [0.0, 0.0, 0.0, (1.0 / (q_vel**2.0))]])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800251
Diana Burgessd0180f12018-03-21 21:24:17 -0700252 self.R = numpy.matrix([[(1.0 / (12.0**2.0)), 0.0],
253 [0.0, (1.0 / (12.0**2.0))]])
254 self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800255
Diana Burgessd0180f12018-03-21 21:24:17 -0700256 glog.debug('DT q_pos %f q_vel %s %s', q_pos, q_vel, self._name)
Ravago Jones26f7ad02021-02-05 15:45:59 -0800257 glog.debug('Poles: %s',
258 str(numpy.linalg.eig(self.A - self.B * self.K)[0]))
259 glog.debug(
260 'Time constants: %s hz',
261 str([
262 numpy.log(x) / -self.dt
263 for x in numpy.linalg.eig(self.A - self.B * self.K)[0]
264 ]))
Diana Burgessd0180f12018-03-21 21:24:17 -0700265 glog.debug('K %s', repr(self.K))
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800266
Diana Burgessd0180f12018-03-21 21:24:17 -0700267 self.hlp = 0.3
268 self.llp = 0.4
269 self.PlaceObserverPoles([self.hlp, self.hlp, self.llp, self.llp])
270
271 self.U_max = numpy.matrix([[12.0], [12.0]])
272 self.U_min = numpy.matrix([[-12.0], [-12.0]])
273
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800274
275class KFDrivetrain(Drivetrain):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700276
Diana Burgessd0180f12018-03-21 21:24:17 -0700277 def __init__(self,
278 drivetrain_params,
279 name="KFDrivetrain",
280 left_low=True,
281 right_low=True):
282 """Kalman filter values of a drivetrain.
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800283
Diana Burgessd0180f12018-03-21 21:24:17 -0700284 Args:
285 drivetrain_params: DrivetrainParams, class of values defining the drivetrain.
286 name: string, Name of this drivetrain.
287 left_low: bool, Whether the left is in high gear.
288 right_low: bool, Whether the right is in high gear.
289 """
290 super(KFDrivetrain, self).__init__(drivetrain_params, name, left_low,
291 right_low)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800292
Diana Burgessd0180f12018-03-21 21:24:17 -0700293 self.unaugmented_A_continuous = self.A_continuous
294 self.unaugmented_B_continuous = self.B_continuous
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800295
Diana Burgessd0180f12018-03-21 21:24:17 -0700296 # The practical voltage applied to the wheels is
297 # V_left = U_left + left_voltage_error
298 #
299 # The states are
300 # [left position, left velocity, right position, right velocity,
301 # left voltage error, right voltage error, angular_error]
302 #
303 # The left and right positions are filtered encoder positions and are not
304 # adjusted for heading error.
305 # The turn velocity as computed by the left and right velocities is
306 # adjusted by the gyro velocity.
307 # The angular_error is the angular velocity error between the wheel speed
308 # and the gyro speed.
309 self.A_continuous = numpy.matrix(numpy.zeros((7, 7)))
310 self.B_continuous = numpy.matrix(numpy.zeros((7, 2)))
311 self.A_continuous[0:4, 0:4] = self.unaugmented_A_continuous
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800312
Diana Burgessd0180f12018-03-21 21:24:17 -0700313 if self.force:
Tyler Chatow6738c362019-02-16 14:12:30 -0800314 self.A_continuous[0:4, 4:6] = numpy.matrix([[0.0, 0.0],
315 [self.mspl, self.msnl],
316 [0.0, 0.0],
Ravago Jones26f7ad02021-02-05 15:45:59 -0800317 [self.msnr,
318 self.mspr]])
Diana Burgessd0180f12018-03-21 21:24:17 -0700319 q_voltage = drivetrain_params.kf_q_voltage * self.mpl
320 else:
321 self.A_continuous[0:4, 4:6] = self.unaugmented_B_continuous
322 q_voltage = drivetrain_params.kf_q_voltage
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800323
Diana Burgessd0180f12018-03-21 21:24:17 -0700324 self.B_continuous[0:4, 0:2] = self.unaugmented_B_continuous
325 self.A_continuous[0, 6] = 1
326 self.A_continuous[2, 6] = -1
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800327
Diana Burgessd0180f12018-03-21 21:24:17 -0700328 self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
329 self.B_continuous, self.dt)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800330
Diana Burgessd0180f12018-03-21 21:24:17 -0700331 if self.has_imu:
Ravago Jones5127ccc2022-07-31 16:32:45 -0700332 self.C = numpy.matrix([[1, 0, 0, 0, 0, 0, 0],
333 [0, 0, 1, 0, 0, 0, 0],
334 [
335 0,
336 -0.5 / drivetrain_params.robot_radius,
337 0, 0.5 / drivetrain_params.robot_radius,
338 0, 0, 0
339 ], [0, 0, 0, 0, 0, 0, 0]])
Diana Burgessd0180f12018-03-21 21:24:17 -0700340 gravity = 9.8
Ravago Jones26f7ad02021-02-05 15:45:59 -0800341 self.C[3, 0:6] = 0.5 * (self.A_continuous[1, 0:6] +
342 self.A_continuous[3, 0:6]) / gravity
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800343
Ravago Jones5127ccc2022-07-31 16:32:45 -0700344 self.D = numpy.matrix([
345 [0, 0], [0, 0], [0, 0],
346 [
347 0.5 * (self.B_continuous[1, 0] + self.B_continuous[3, 0]) /
348 gravity,
349 0.5 * (self.B_continuous[1, 1] + self.B_continuous[3, 1]) /
350 gravity
351 ]
352 ])
Diana Burgessd0180f12018-03-21 21:24:17 -0700353 else:
Ravago Jones5127ccc2022-07-31 16:32:45 -0700354 self.C = numpy.matrix([[1, 0, 0, 0, 0, 0, 0],
355 [0, 0, 1, 0, 0, 0, 0],
356 [
357 0,
358 -0.5 / drivetrain_params.robot_radius,
359 0, 0.5 / drivetrain_params.robot_radius,
360 0, 0, 0
361 ]])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800362
Diana Burgessd0180f12018-03-21 21:24:17 -0700363 self.D = numpy.matrix([[0, 0], [0, 0], [0, 0]])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800364
Diana Burgessd0180f12018-03-21 21:24:17 -0700365 q_pos = 0.05
366 q_vel = 1.00
367 q_encoder_uncertainty = 2.00
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800368
Diana Burgessd0180f12018-03-21 21:24:17 -0700369 self.Q = numpy.matrix(
Tyler Chatow6738c362019-02-16 14:12:30 -0800370 [[(q_pos**2.0), 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
371 [0.0, (q_vel**2.0), 0.0, 0.0, 0.0, 0.0, 0.0],
372 [0.0, 0.0, (q_pos**2.0), 0.0, 0.0, 0.0, 0.0],
373 [0.0, 0.0, 0.0, (q_vel**2.0), 0.0, 0.0, 0.0],
374 [0.0, 0.0, 0.0, 0.0, (q_voltage**2.0), 0.0, 0.0],
375 [0.0, 0.0, 0.0, 0.0, 0.0, (q_voltage**2.0), 0.0],
Diana Burgessd0180f12018-03-21 21:24:17 -0700376 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, (q_encoder_uncertainty**2.0)]])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800377
Diana Burgessd0180f12018-03-21 21:24:17 -0700378 r_pos = 0.0001
379 r_gyro = 0.000001
380 if self.has_imu:
381 r_accelerometer = 7.0
382 self.R = numpy.matrix([[(r_pos**2.0), 0.0, 0.0, 0.0],
383 [0.0, (r_pos**2.0), 0.0, 0.0],
384 [0.0, 0.0, (r_gyro**2.0), 0.0],
385 [0.0, 0.0, 0.0, (r_accelerometer**2.0)]])
386 else:
387 self.R = numpy.matrix([[(r_pos**2.0), 0.0, 0.0],
388 [0.0, (r_pos**2.0), 0.0],
389 [0.0, 0.0, (r_gyro**2.0)]])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800390
Diana Burgessd0180f12018-03-21 21:24:17 -0700391 # Solving for kf gains.
Ravago Jones5127ccc2022-07-31 16:32:45 -0700392 self.KalmanGain, self.Q_steady = controls.kalman(A=self.A,
393 B=self.B,
394 C=self.C,
395 Q=self.Q,
396 R=self.R)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800397
James Kuszmaul4d752d52019-02-09 17:27:55 -0800398 # If we don't have an IMU, pad various matrices with zeros so that
399 # we can still have 4 measurement outputs.
Diana Burgessd0180f12018-03-21 21:24:17 -0700400 if not self.has_imu:
Ravago Jones5127ccc2022-07-31 16:32:45 -0700401 self.KalmanGain = numpy.hstack(
402 (self.KalmanGain, numpy.matrix(numpy.zeros((7, 1)))))
Austin Schuhecc92a02019-01-20 17:42:19 -0800403 self.C = numpy.vstack((self.C, numpy.matrix(numpy.zeros((1, 7)))))
404 self.D = numpy.vstack((self.D, numpy.matrix(numpy.zeros((1, 2)))))
James Kuszmaul4d752d52019-02-09 17:27:55 -0800405 Rtmp = numpy.zeros((4, 4))
406 Rtmp[0:3, 0:3] = self.R
407 self.R = Rtmp
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800408
Diana Burgessd0180f12018-03-21 21:24:17 -0700409 self.L = self.A * self.KalmanGain
410
411 unaug_K = self.K
412
413 # Implement a nice closed loop controller for use by the closed loop
414 # controller.
415 self.K = numpy.matrix(numpy.zeros((self.B.shape[1], self.A.shape[0])))
416 self.K[0:2, 0:4] = unaug_K
417 if self.force:
418 self.K[0, 4] = 1.0 / self.mpl
419 self.K[1, 5] = 1.0 / self.mpr
420 else:
421 self.K[0, 4] = 1.0
422 self.K[1, 5] = 1.0
423
424 self.Qff = numpy.matrix(numpy.zeros((4, 4)))
425 qff_pos = 0.005
426 qff_vel = 1.00
427 self.Qff[0, 0] = 1.0 / qff_pos**2.0
428 self.Qff[1, 1] = 1.0 / qff_vel**2.0
429 self.Qff[2, 2] = 1.0 / qff_pos**2.0
430 self.Qff[3, 3] = 1.0 / qff_vel**2.0
431 self.Kff = numpy.matrix(numpy.zeros((2, 7)))
Ravago Jones5127ccc2022-07-31 16:32:45 -0700432 self.Kff[0:2,
433 0:4] = controls.TwoStateFeedForwards(self.B[0:4, :], self.Qff)
Diana Burgessd0180f12018-03-21 21:24:17 -0700434
435 self.InitializeState()
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800436
437
Tyler Chatow6738c362019-02-16 14:12:30 -0800438def WriteDrivetrain(drivetrain_files,
439 kf_drivetrain_files,
440 year_namespace,
441 drivetrain_params,
442 scalar_type='double'):
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800443
Diana Burgessd0180f12018-03-21 21:24:17 -0700444 # Write the generated constants out to a file.
Ravago Jones5127ccc2022-07-31 16:32:45 -0700445 drivetrain_low_low = Drivetrain(name="DrivetrainLowLow",
446 left_low=True,
447 right_low=True,
448 drivetrain_params=drivetrain_params)
449 drivetrain_low_high = Drivetrain(name="DrivetrainLowHigh",
450 left_low=True,
451 right_low=False,
452 drivetrain_params=drivetrain_params)
453 drivetrain_high_low = Drivetrain(name="DrivetrainHighLow",
454 left_low=False,
455 right_low=True,
456 drivetrain_params=drivetrain_params)
457 drivetrain_high_high = Drivetrain(name="DrivetrainHighHigh",
458 left_low=False,
459 right_low=False,
460 drivetrain_params=drivetrain_params)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800461
Ravago Jones5127ccc2022-07-31 16:32:45 -0700462 kf_drivetrain_low_low = KFDrivetrain(name="KFDrivetrainLowLow",
463 left_low=True,
464 right_low=True,
465 drivetrain_params=drivetrain_params)
466 kf_drivetrain_low_high = KFDrivetrain(name="KFDrivetrainLowHigh",
467 left_low=True,
468 right_low=False,
469 drivetrain_params=drivetrain_params)
470 kf_drivetrain_high_low = KFDrivetrain(name="KFDrivetrainHighLow",
471 left_low=False,
472 right_low=True,
473 drivetrain_params=drivetrain_params)
474 kf_drivetrain_high_high = KFDrivetrain(name="KFDrivetrainHighHigh",
475 left_low=False,
476 right_low=False,
477 drivetrain_params=drivetrain_params)
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800478
Austin Schuhbcce26a2018-03-26 23:41:24 -0700479 if isinstance(year_namespace, list):
Austin Schuhecc92a02019-01-20 17:42:19 -0800480 namespaces = year_namespace
Austin Schuhbcce26a2018-03-26 23:41:24 -0700481 else:
Austin Schuhecc92a02019-01-20 17:42:19 -0800482 namespaces = [year_namespace, 'control_loops', 'drivetrain']
Ravago Jones5127ccc2022-07-31 16:32:45 -0700483 dog_loop_writer = control_loop.ControlLoopWriter("Drivetrain", [
484 drivetrain_low_low, drivetrain_low_high, drivetrain_high_low,
485 drivetrain_high_high
486 ],
487 namespaces=namespaces,
488 scalar_type=scalar_type)
Diana Burgessd0180f12018-03-21 21:24:17 -0700489 dog_loop_writer.AddConstant(
490 control_loop.Constant("kDt", "%f", drivetrain_low_low.dt))
491 dog_loop_writer.AddConstant(
492 control_loop.Constant("kStallTorque", "%f",
493 drivetrain_low_low.stall_torque))
494 dog_loop_writer.AddConstant(
495 control_loop.Constant("kStallCurrent", "%f",
496 drivetrain_low_low.stall_current))
497 dog_loop_writer.AddConstant(
498 control_loop.Constant("kFreeSpeed", "%f",
499 drivetrain_low_low.free_speed))
500 dog_loop_writer.AddConstant(
501 control_loop.Constant("kFreeCurrent", "%f",
502 drivetrain_low_low.free_current))
503 dog_loop_writer.AddConstant(
504 control_loop.Constant("kJ", "%f", drivetrain_low_low.J))
505 dog_loop_writer.AddConstant(
506 control_loop.Constant("kMass", "%f", drivetrain_low_low.mass))
507 dog_loop_writer.AddConstant(
508 control_loop.Constant("kRobotRadius", "%f",
509 drivetrain_low_low.robot_radius))
510 dog_loop_writer.AddConstant(
511 control_loop.Constant("kWheelRadius", "%f", drivetrain_low_low.r))
512 dog_loop_writer.AddConstant(
513 control_loop.Constant("kR", "%f", drivetrain_low_low.resistance))
514 dog_loop_writer.AddConstant(
515 control_loop.Constant("kV", "%f", drivetrain_low_low.Kv))
516 dog_loop_writer.AddConstant(
517 control_loop.Constant("kT", "%f", drivetrain_low_low.Kt))
518 dog_loop_writer.AddConstant(
519 control_loop.Constant("kLowGearRatio", "%f", drivetrain_low_low.G_low))
520 dog_loop_writer.AddConstant(
521 control_loop.Constant("kHighGearRatio", "%f",
522 drivetrain_high_high.G_high))
523 dog_loop_writer.AddConstant(
524 control_loop.Constant(
525 "kHighOutputRatio", "%f",
526 drivetrain_high_high.G_high * drivetrain_high_high.r))
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800527
Diana Burgessd0180f12018-03-21 21:24:17 -0700528 dog_loop_writer.Write(drivetrain_files[0], drivetrain_files[1])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800529
Ravago Jones5127ccc2022-07-31 16:32:45 -0700530 kf_loop_writer = control_loop.ControlLoopWriter("KFDrivetrain", [
531 kf_drivetrain_low_low, kf_drivetrain_low_high, kf_drivetrain_high_low,
532 kf_drivetrain_high_high
533 ],
534 namespaces=namespaces,
535 scalar_type=scalar_type)
Diana Burgessd0180f12018-03-21 21:24:17 -0700536 kf_loop_writer.Write(kf_drivetrain_files[0], kf_drivetrain_files[1])
537
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800538
Austin Schuh566c82b2023-01-27 20:49:08 -0800539def PlotDrivetrainSprint(drivetrain_params):
540 # Simulate the response of the system to a step input.
541 drivetrain = KFDrivetrain(left_low=False,
542 right_low=False,
543 drivetrain_params=drivetrain_params)
544 simulated_left_position = []
545 simulated_right_position = []
546 simulated_left_velocity = []
547 simulated_right_velocity = []
548
549 simulated_left_motor_currents = []
550 simulated_left_breaker_currents = []
551 simulated_right_motor_currents = []
552 simulated_right_breaker_currents = []
553
554 simulated_battery_heat_wattages = []
555 simulated_wattage = []
556 motor_inverter_voltages = []
557 voltage_left = []
558 voltage_right = []
559 simulated_motor_heat_wattages = []
560 simulated_motor_wattage = []
561
562 max_motor_currents = []
563 overall_currents = []
564 simulated_battery_wattage = []
565
566 # Distance in meters to call 1/2 field.
567 kSprintDistance = 8.0
568
569 vbat = 12.6
570 # Measured resistance of the battery, pd board, and breakers.
571 Rw = 0.023
572 top_speed = drivetrain.free_speed * (drivetrain.Gr +
573 drivetrain.Gl) / 2.0 * drivetrain.r
574
575 passed_distance = False
576 max_breaker_current = 0
577 heat_energy_usage = 0.0
578 for index in range(800):
579 # Current per side
580 left_traction_current = (drivetrain.mass / 2.0 *
581 drivetrain_params.coefficient_of_friction *
582 9.81 * drivetrain.r * drivetrain.Gl /
583 drivetrain.Kt)
584 right_traction_current = (drivetrain.mass / 2.0 *
585 drivetrain_params.coefficient_of_friction *
586 9.81 * drivetrain.r * drivetrain.Gr /
587 drivetrain.Kt)
588
589 # Detect if we've traveled over the sprint distance and report stats.
590 if (drivetrain.X[0, 0] + drivetrain.X[2, 0]) / 2.0 > kSprintDistance:
591 if not passed_distance:
592 velocity = (drivetrain.X[1, 0] + drivetrain.X[3, 0]) / 2.0
593 print("Took", index * drivetrain.dt,
594 "to pass 1/2 field, going", velocity, "m/s,",
595 velocity / 0.0254 / 12.0, "Traction limit current",
596 left_traction_current / drivetrain_params.num_motors,
597 "max breaker current", max_breaker_current, "top speed",
598 top_speed, "m/s", top_speed / 0.0254 / 12.0,
599 "fps, gear ratio", drivetrain.Gl, "heat energy",
600 heat_energy_usage)
601 passed_distance = True
602
603 bemf_left = drivetrain.X[
604 1, 0] / drivetrain.r / drivetrain.Gl / drivetrain.Kv
605 bemf_right = drivetrain.X[
606 3, 0] / drivetrain.r / drivetrain.Gr / drivetrain.Kv
607
608 # Max current we could push through the motors is what we would get if
609 # we short the battery through the battery resistance into the motor.
610 max_motor_current = (vbat - (bemf_left + bemf_right) / 2.0) / (
611 Rw + drivetrain.resistance / 2.0)
612
613 max_motor_currents.append(max_motor_current /
614 (drivetrain_params.num_motors * 2))
615
616 # From this current, we can compute the voltage we can apply.
617 # This is either the traction limit or the current limit.
618 max_voltage_left = bemf_left + min(
619 max_motor_current / 2,
620 left_traction_current) * drivetrain.resistance
621 max_voltage_right = bemf_right + min(
622 max_motor_current / 2,
623 right_traction_current) * drivetrain.resistance
624
625 simulated_left_position.append(drivetrain.X[0, 0])
626 simulated_left_velocity.append(drivetrain.X[1, 0])
627 simulated_right_position.append(drivetrain.X[2, 0])
628 simulated_right_velocity.append(drivetrain.X[3, 0])
629
630 U = numpy.matrix([[min(max_voltage_left, vbat)],
631 [min(max_voltage_right, vbat)]])
632
633 # Stator current
634 simulated_left_motor_current = (U[0, 0] -
635 bemf_left) / drivetrain.resistance
636 simulated_right_motor_current = (U[1, 0] -
637 bemf_right) / drivetrain.resistance
638
639 # And this gives us the power pushed into the motors.
640 power = (U[0, 0] * simulated_left_motor_current +
641 U[1, 0] * simulated_right_motor_current)
642
643 simulated_wattage.append(power)
644
645 # Solve for the voltage we'd have to supply to the input of the motor
646 # controller to generate the power required.
647 motor_inverter_voltage = (
648 vbat + numpy.sqrt(vbat * vbat - 4.0 * power * Rw)) / 2.0
649
650 overall_current = (vbat - motor_inverter_voltage) / Rw
651 overall_currents.append(overall_current)
652
653 motor_inverter_voltages.append(motor_inverter_voltage)
654
655 # Overall left and right currents at the breaker
656 simulated_left_breaker_current = (
657 simulated_left_motor_current /
658 drivetrain_params.num_motors) * U[0, 0] / motor_inverter_voltage
659 simulated_right_breaker_current = (
660 simulated_right_motor_current /
661 drivetrain_params.num_motors) * U[1, 0] / motor_inverter_voltage
662
663 simulated_left_motor_currents.append(simulated_left_motor_current /
664 drivetrain_params.num_motors)
665 simulated_left_breaker_currents.append(simulated_left_breaker_current)
666 simulated_right_motor_currents.append(simulated_right_motor_current /
667 drivetrain_params.num_motors)
668 simulated_right_breaker_currents.append(
669 simulated_right_breaker_current)
670
671 # Save out the peak battery current observed.
672 max_breaker_current = max(
673 max_breaker_current,
674 max(simulated_left_breaker_current,
675 simulated_right_breaker_current))
676
677 # Compute the heat burned in the battery
678 simulated_battery_heat_wattage = math.pow(
679 vbat - motor_inverter_voltage, 2.0) / Rw
680 simulated_battery_heat_wattages.append(simulated_battery_heat_wattage)
681
682 motor_heat_wattage = (math.pow(simulated_left_motor_current, 2.0) *
683 drivetrain.resistance +
684 math.pow(simulated_right_motor_current, 2.0) *
685 drivetrain.resistance)
686 simulated_motor_heat_wattages.append(motor_heat_wattage)
687
688 simulated_motor_wattage.append(simulated_left_motor_current * U[0, 0] +
689 simulated_right_motor_current * U[1, 0])
690
691 simulated_battery_wattage.append(vbat * overall_current)
692
693 # And then the overall energy outputted by the battery.
694 heat_energy_usage += (motor_heat_wattage +
695 simulated_battery_heat_wattage) * drivetrain.dt
696
697 voltage_left.append(U[0, 0])
698 voltage_right.append(U[1, 0])
699
700 drivetrain.Update(U)
701
702 t = [drivetrain.dt * x for x in range(len(simulated_left_position))]
703 pylab.rc('lines', linewidth=4)
704 pylab.subplot(3, 1, 1)
705 pylab.plot(t, simulated_left_position, label='left position')
706 pylab.plot(t, simulated_right_position, 'r--', label='right position')
707 pylab.plot(t, simulated_left_velocity, label='left velocity')
708 pylab.plot(t, simulated_right_velocity, label='right velocity')
709
710 pylab.suptitle('Acceleration Test\n12 Volt Step Input')
711 pylab.legend(loc='lower right')
712
713 pylab.subplot(3, 1, 2)
714
715 pylab.plot(t, simulated_left_motor_currents, label='left rotor current')
716 pylab.plot(t,
717 simulated_right_motor_currents,
718 'r--',
719 label='right rotor current')
720 pylab.plot(t,
721 simulated_left_breaker_currents,
722 label='left breaker current')
723 pylab.plot(t,
724 simulated_right_breaker_currents,
725 'r--',
726 label='right breaker current')
727 pylab.plot(t, motor_inverter_voltages, label='motor inverter voltage')
728 pylab.plot(t, voltage_left, label='left voltage')
729 pylab.plot(t, voltage_right, label='right voltage')
730 pylab.plot(t, max_motor_currents, label='max_currents')
731 pylab.legend(loc='lower right')
732
733 wattage_axis = pylab.subplot(3, 1, 3)
734 wattage_axis.plot(t, simulated_wattage, label='wattage')
735 wattage_axis.plot(t,
736 simulated_battery_heat_wattages,
737 label='battery wattage')
738 wattage_axis.plot(t,
739 simulated_motor_heat_wattages,
740 label='motor heat wattage')
741 wattage_axis.plot(t, simulated_motor_wattage, label='motor wattage')
742 wattage_axis.plot(t, simulated_battery_wattage, label='overall wattage')
743 pylab.legend(loc='upper left')
744 overall_current_axis = wattage_axis.twinx()
745 overall_current_axis.plot(t, overall_currents, 'c--', label='current')
746
747 pylab.legend(loc='lower right')
748
749 pylab.suptitle('Acceleration Test\n12 Volt Step Input\n%f fps' %
750 (top_speed / 0.0254 / 12.0, ))
751 pylab.show()
752
753
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800754def PlotDrivetrainMotions(drivetrain_params):
Austin Schuh1542ea32021-01-23 20:19:50 -0800755 # Test out the voltage error.
Ravago Jones5127ccc2022-07-31 16:32:45 -0700756 drivetrain = KFDrivetrain(left_low=False,
757 right_low=False,
758 drivetrain_params=drivetrain_params)
Austin Schuh1542ea32021-01-23 20:19:50 -0800759 close_loop_left = []
760 close_loop_right = []
761 left_power = []
762 right_power = []
763 R = numpy.matrix([[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]])
Austin Schuh5ea48472021-02-02 20:46:41 -0800764 for _ in range(300):
Austin Schuh1542ea32021-01-23 20:19:50 -0800765 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min,
766 drivetrain.U_max)
Austin Schuh64433f12022-02-21 19:40:38 -0800767 drivetrain.CorrectObserver(U)
768 drivetrain.PredictObserver(U)
Austin Schuh1542ea32021-01-23 20:19:50 -0800769 drivetrain.Update(U + numpy.matrix([[1.0], [1.0]]))
770 close_loop_left.append(drivetrain.X[0, 0])
771 close_loop_right.append(drivetrain.X[2, 0])
772 left_power.append(U[0, 0])
773 right_power.append(U[1, 0])
774
775 t = [drivetrain.dt * x for x in range(300)]
776 pylab.plot(t, close_loop_left, label='left position')
777 pylab.plot(t, close_loop_right, 'm--', label='right position')
778 pylab.plot(t, left_power, label='left power')
779 pylab.plot(t, right_power, '--', label='right power')
780 pylab.suptitle('Voltage error')
781 pylab.legend()
782 pylab.show()
783
Diana Burgessd0180f12018-03-21 21:24:17 -0700784 # Simulate the response of the system to a step input.
Ravago Jones5127ccc2022-07-31 16:32:45 -0700785 drivetrain = KFDrivetrain(left_low=False,
786 right_low=False,
787 drivetrain_params=drivetrain_params)
Diana Burgessd0180f12018-03-21 21:24:17 -0700788 simulated_left = []
789 simulated_right = []
Austin Schuh5ea48472021-02-02 20:46:41 -0800790 for _ in range(100):
Diana Burgessd0180f12018-03-21 21:24:17 -0700791 drivetrain.Update(numpy.matrix([[12.0], [12.0]]))
792 simulated_left.append(drivetrain.X[0, 0])
793 simulated_right.append(drivetrain.X[2, 0])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800794
Diana Burgessd0180f12018-03-21 21:24:17 -0700795 pylab.rc('lines', linewidth=4)
796 pylab.plot(range(100), simulated_left, label='left position')
797 pylab.plot(range(100), simulated_right, 'r--', label='right position')
798 pylab.suptitle('Acceleration Test\n12 Volt Step Input')
799 pylab.legend(loc='lower right')
800 pylab.show()
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800801
Diana Burgessd0180f12018-03-21 21:24:17 -0700802 # Simulate forwards motion.
Ravago Jones5127ccc2022-07-31 16:32:45 -0700803 drivetrain = KFDrivetrain(left_low=False,
804 right_low=False,
805 drivetrain_params=drivetrain_params)
Diana Burgessd0180f12018-03-21 21:24:17 -0700806 close_loop_left = []
807 close_loop_right = []
808 left_power = []
809 right_power = []
Austin Schuh1542ea32021-01-23 20:19:50 -0800810 R = numpy.matrix([[1.0], [0.0], [1.0], [0.0], [0.0], [0.0], [0.0]])
Austin Schuh5ea48472021-02-02 20:46:41 -0800811 for _ in range(300):
Diana Burgessd0180f12018-03-21 21:24:17 -0700812 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min,
813 drivetrain.U_max)
Austin Schuh64433f12022-02-21 19:40:38 -0800814 drivetrain.CorrectObserver(U)
815 drivetrain.PredictObserver(U)
Diana Burgessd0180f12018-03-21 21:24:17 -0700816 drivetrain.Update(U)
817 close_loop_left.append(drivetrain.X[0, 0])
818 close_loop_right.append(drivetrain.X[2, 0])
819 left_power.append(U[0, 0])
820 right_power.append(U[1, 0])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800821
Austin Schuh1542ea32021-01-23 20:19:50 -0800822 t = [drivetrain.dt * x for x in range(300)]
823 pylab.plot(t, close_loop_left, label='left position')
824 pylab.plot(t, close_loop_right, 'm--', label='right position')
825 pylab.plot(t, left_power, label='left power')
826 pylab.plot(t, right_power, '--', label='right power')
Diana Burgessd0180f12018-03-21 21:24:17 -0700827 pylab.suptitle('Linear Move\nLeft and Right Position going to 1')
828 pylab.legend()
829 pylab.show()
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800830
Diana Burgessd0180f12018-03-21 21:24:17 -0700831 # Try turning in place
Austin Schuh1542ea32021-01-23 20:19:50 -0800832 drivetrain = KFDrivetrain(drivetrain_params=drivetrain_params)
Diana Burgessd0180f12018-03-21 21:24:17 -0700833 close_loop_left = []
834 close_loop_right = []
Austin Schuh1542ea32021-01-23 20:19:50 -0800835 R = numpy.matrix([[-1.0], [0.0], [1.0], [0.0], [0.0], [0.0], [0.0]])
Austin Schuh5ea48472021-02-02 20:46:41 -0800836 for _ in range(200):
Diana Burgessd0180f12018-03-21 21:24:17 -0700837 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min,
838 drivetrain.U_max)
Austin Schuh64433f12022-02-21 19:40:38 -0800839 drivetrain.CorrectObserver(U)
840 drivetrain.PredictObserver(U)
Diana Burgessd0180f12018-03-21 21:24:17 -0700841 drivetrain.Update(U)
842 close_loop_left.append(drivetrain.X[0, 0])
843 close_loop_right.append(drivetrain.X[2, 0])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800844
Diana Burgessd0180f12018-03-21 21:24:17 -0700845 pylab.plot(range(200), close_loop_left, label='left position')
846 pylab.plot(range(200), close_loop_right, label='right position')
847 pylab.suptitle(
Ravago Jones26f7ad02021-02-05 15:45:59 -0800848 'Angular Move\nLeft position going to -1 and right position going to 1'
849 )
Diana Burgessd0180f12018-03-21 21:24:17 -0700850 pylab.legend(loc='center right')
851 pylab.show()
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800852
Diana Burgessd0180f12018-03-21 21:24:17 -0700853 # Try turning just one side.
Austin Schuh1542ea32021-01-23 20:19:50 -0800854 drivetrain = KFDrivetrain(drivetrain_params=drivetrain_params)
Diana Burgessd0180f12018-03-21 21:24:17 -0700855 close_loop_left = []
856 close_loop_right = []
Austin Schuh1542ea32021-01-23 20:19:50 -0800857 R = numpy.matrix([[0.0], [0.0], [1.0], [0.0], [0.0], [0.0], [0.0]])
Austin Schuh5ea48472021-02-02 20:46:41 -0800858 for _ in range(300):
Diana Burgessd0180f12018-03-21 21:24:17 -0700859 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min,
860 drivetrain.U_max)
Austin Schuh64433f12022-02-21 19:40:38 -0800861 drivetrain.CorrectObserver(U)
862 drivetrain.PredictObserver(U)
Diana Burgessd0180f12018-03-21 21:24:17 -0700863 drivetrain.Update(U)
864 close_loop_left.append(drivetrain.X[0, 0])
865 close_loop_right.append(drivetrain.X[2, 0])
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800866
Diana Burgessd0180f12018-03-21 21:24:17 -0700867 pylab.plot(range(300), close_loop_left, label='left position')
868 pylab.plot(range(300), close_loop_right, label='right position')
869 pylab.suptitle(
870 'Pivot\nLeft position not changing and right position going to 1')
871 pylab.legend(loc='center right')
872 pylab.show()