Austin Schuh | 085eab9 | 2020-11-26 13:54:51 -0800 | [diff] [blame] | 1 | #!/usr/bin/python3 |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 2 | |
| 3 | from frc971.control_loops.python import control_loop |
| 4 | from frc971.control_loops.python import controls |
| 5 | import numpy |
| 6 | import sys |
| 7 | from matplotlib import pylab |
| 8 | import glog |
| 9 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 10 | |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 11 | class DrivetrainParams(object): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 12 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 13 | def __init__(self, |
| 14 | J, |
| 15 | mass, |
| 16 | robot_radius, |
| 17 | wheel_radius, |
Austin Schuh | ecc92a0 | 2019-01-20 17:42:19 -0800 | [diff] [blame] | 18 | 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 Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 27 | 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 Schuh | 95fbcc5 | 2018-03-10 20:57:20 -0800 | [diff] [blame] | 35 | observer_poles=[0.02, 0.02], |
| 36 | robot_cg_offset=0.0): |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 37 | """Defines all constants of a drivetrain. |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 38 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 39 | 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 Schuh | ecc92a0 | 2019-01-20 17:42:19 -0800 | [diff] [blame] | 45 | G: float, Gear ratio for a single speed. |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 46 | 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 Schuh | 1542ea3 | 2021-01-23 20:19:50 -0800 | [diff] [blame] | 49 | 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 Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 55 | efficiency: float, gear box effiency. |
| 56 | has_imu: bool, true if imu is present. |
| 57 | force: bool, true if force. |
| 58 | kf_q_voltage: float |
| 59 | motor_type: object, class of values defining the motor in drivetrain. |
| 60 | num_motors: int, number of motors on one side of drivetrain. |
Austin Schuh | 1542ea3 | 2021-01-23 20:19:50 -0800 | [diff] [blame] | 61 | controller_poles: array, An array of poles for the polydrivetrain |
| 62 | controller. (See control_loop.py) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 63 | observer_poles: array, An array of poles. (See control_loop.py) |
Michael Schuh | 95fbcc5 | 2018-03-10 20:57:20 -0800 | [diff] [blame] | 64 | robot_cg_offset: offset in meters of CG from robot center to left side |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 65 | """ |
Austin Schuh | ecc92a0 | 2019-01-20 17:42:19 -0800 | [diff] [blame] | 66 | 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 Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 89 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 90 | self.J = J |
| 91 | self.mass = mass |
| 92 | self.robot_radius = robot_radius |
Michael Schuh | 95fbcc5 | 2018-03-10 20:57:20 -0800 | [diff] [blame] | 93 | self.robot_cg_offset = robot_cg_offset |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 94 | 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 Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 111 | |
| 112 | class Drivetrain(control_loop.ControlLoop): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 113 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 114 | 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 Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 120 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 121 | 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 Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 128 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 129 | # 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 Schuh | 95fbcc5 | 2018-03-10 20:57:20 -0800 | [diff] [blame] | 138 | # 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 Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 146 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 147 | # 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 Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 158 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 159 | # Control loop time step |
| 160 | self.dt = drivetrain_params.dt |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 161 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 162 | self.efficiency = drivetrain_params.efficiency |
| 163 | self.force = drivetrain_params.force |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 164 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 165 | self.BuildDrivetrain(drivetrain_params.motor_type, |
| 166 | drivetrain_params.num_motors) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 167 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 168 | 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 Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 174 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 175 | self.BuildDrivetrainController(q_pos, q_vel) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 176 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 177 | self.InitializeState() |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 178 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 179 | 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 Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 191 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 192 | # Effective motor resistance in ohms. |
| 193 | self.resistance = 12.0 / self.stall_current |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 194 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 195 | # Resistance of the motor, divided by the number of motors. |
| 196 | # Motor velocity constant |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 197 | self.Kv = (self.free_speed / |
| 198 | (12.0 - self.resistance * self.free_current)) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 199 | # Torque constant |
| 200 | self.Kt = self.stall_torque / self.stall_current |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 201 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 202 | # These describe the way that a given side of a robot will be influenced |
| 203 | # by the other side. Units of 1 / kg. |
Michael Schuh | 95fbcc5 | 2018-03-10 20:57:20 -0800 | [diff] [blame] | 204 | 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 Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 210 | # The calculations which we will need for A and B. |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 211 | self.tcl = self.Kt / self.Kv / (self.Gl * self.Gl * self.resistance * |
| 212 | self.r * self.r) |
| 213 | self.tcr = self.Kt / self.Kv / (self.Gr * self.Gr * self.resistance * |
| 214 | self.r * self.r) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 215 | self.mpl = self.Kt / (self.Gl * self.resistance * self.r) |
| 216 | self.mpr = self.Kt / (self.Gr * self.resistance * self.r) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 217 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 218 | # State feedback matrices |
| 219 | # X will be of the format |
Austin Schuh | d77c064 | 2020-12-30 21:38:54 -0800 | [diff] [blame] | 220 | # [[positionl], [velocityl], [positionr], [velocityr]] |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 221 | self.A_continuous = numpy.matrix( |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 222 | [[0, 1, 0, |
| 223 | 0], [0, -self.mspl * self.tcl, 0, -self.msnr * self.tcr], |
| 224 | [0, 0, 0, 1], |
| 225 | [0, -self.msnl * self.tcl, 0, -self.mspr * self.tcr]]) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 226 | self.B_continuous = numpy.matrix( |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 227 | [[0, 0], [self.mspl * self.mpl, self.msnr * self.mpr], [0, 0], |
Michael Schuh | 95fbcc5 | 2018-03-10 20:57:20 -0800 | [diff] [blame] | 228 | [self.msnl * self.mpl, self.mspr * self.mpr]]) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 229 | self.C = numpy.matrix([[1, 0, 0, 0], [0, 0, 1, 0]]) |
| 230 | self.D = numpy.matrix([[0, 0], [0, 0]]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 231 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 232 | self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, |
| 233 | self.B_continuous, self.dt) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 234 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 235 | def BuildDrivetrainController(self, q_pos, q_vel): |
Austin Schuh | d77c064 | 2020-12-30 21:38:54 -0800 | [diff] [blame] | 236 | # We can solve for the max velocity by setting \dot(x) = Ax + Bu to 0 |
| 237 | max_voltage = 12 |
| 238 | glog.debug( |
| 239 | "Max speed %f m/s", |
| 240 | -(self.B_continuous[1, 1] + self.B_continuous[1, 0]) / |
| 241 | (self.A_continuous[1, 1] + self.A_continuous[1, 3]) * max_voltage) |
| 242 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 243 | # Tune the LQR controller |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 244 | self.Q = numpy.matrix([[(1.0 / (q_pos**2.0)), 0.0, 0.0, 0.0], |
| 245 | [0.0, (1.0 / (q_vel**2.0)), 0.0, 0.0], |
| 246 | [0.0, 0.0, (1.0 / (q_pos**2.0)), 0.0], |
| 247 | [0.0, 0.0, 0.0, (1.0 / (q_vel**2.0))]]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 248 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 249 | self.R = numpy.matrix([[(1.0 / (12.0**2.0)), 0.0], |
| 250 | [0.0, (1.0 / (12.0**2.0))]]) |
| 251 | self.K = controls.dlqr(self.A, self.B, self.Q, self.R) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 252 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 253 | glog.debug('DT q_pos %f q_vel %s %s', q_pos, q_vel, self._name) |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 254 | glog.debug('Poles: %s', |
| 255 | str(numpy.linalg.eig(self.A - self.B * self.K)[0])) |
| 256 | glog.debug( |
| 257 | 'Time constants: %s hz', |
| 258 | str([ |
| 259 | numpy.log(x) / -self.dt |
| 260 | for x in numpy.linalg.eig(self.A - self.B * self.K)[0] |
| 261 | ])) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 262 | glog.debug('K %s', repr(self.K)) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 263 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 264 | self.hlp = 0.3 |
| 265 | self.llp = 0.4 |
| 266 | self.PlaceObserverPoles([self.hlp, self.hlp, self.llp, self.llp]) |
| 267 | |
| 268 | self.U_max = numpy.matrix([[12.0], [12.0]]) |
| 269 | self.U_min = numpy.matrix([[-12.0], [-12.0]]) |
| 270 | |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 271 | |
| 272 | class KFDrivetrain(Drivetrain): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 273 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 274 | def __init__(self, |
| 275 | drivetrain_params, |
| 276 | name="KFDrivetrain", |
| 277 | left_low=True, |
| 278 | right_low=True): |
| 279 | """Kalman filter values of a drivetrain. |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 280 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 281 | Args: |
| 282 | drivetrain_params: DrivetrainParams, class of values defining the drivetrain. |
| 283 | name: string, Name of this drivetrain. |
| 284 | left_low: bool, Whether the left is in high gear. |
| 285 | right_low: bool, Whether the right is in high gear. |
| 286 | """ |
| 287 | super(KFDrivetrain, self).__init__(drivetrain_params, name, left_low, |
| 288 | right_low) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 289 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 290 | self.unaugmented_A_continuous = self.A_continuous |
| 291 | self.unaugmented_B_continuous = self.B_continuous |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 292 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 293 | # The practical voltage applied to the wheels is |
| 294 | # V_left = U_left + left_voltage_error |
| 295 | # |
| 296 | # The states are |
| 297 | # [left position, left velocity, right position, right velocity, |
| 298 | # left voltage error, right voltage error, angular_error] |
| 299 | # |
| 300 | # The left and right positions are filtered encoder positions and are not |
| 301 | # adjusted for heading error. |
| 302 | # The turn velocity as computed by the left and right velocities is |
| 303 | # adjusted by the gyro velocity. |
| 304 | # The angular_error is the angular velocity error between the wheel speed |
| 305 | # and the gyro speed. |
| 306 | self.A_continuous = numpy.matrix(numpy.zeros((7, 7))) |
| 307 | self.B_continuous = numpy.matrix(numpy.zeros((7, 2))) |
| 308 | self.A_continuous[0:4, 0:4] = self.unaugmented_A_continuous |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 309 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 310 | if self.force: |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 311 | self.A_continuous[0:4, 4:6] = numpy.matrix([[0.0, 0.0], |
| 312 | [self.mspl, self.msnl], |
| 313 | [0.0, 0.0], |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 314 | [self.msnr, |
| 315 | self.mspr]]) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 316 | q_voltage = drivetrain_params.kf_q_voltage * self.mpl |
| 317 | else: |
| 318 | self.A_continuous[0:4, 4:6] = self.unaugmented_B_continuous |
| 319 | q_voltage = drivetrain_params.kf_q_voltage |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 320 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 321 | self.B_continuous[0:4, 0:2] = self.unaugmented_B_continuous |
| 322 | self.A_continuous[0, 6] = 1 |
| 323 | self.A_continuous[2, 6] = -1 |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 324 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 325 | self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, |
| 326 | self.B_continuous, self.dt) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 327 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 328 | if self.has_imu: |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 329 | self.C = numpy.matrix([[1, 0, 0, 0, 0, 0, 0], |
| 330 | [0, 0, 1, 0, 0, 0, 0], |
| 331 | [ |
| 332 | 0, |
| 333 | -0.5 / drivetrain_params.robot_radius, |
| 334 | 0, 0.5 / drivetrain_params.robot_radius, |
| 335 | 0, 0, 0 |
| 336 | ], [0, 0, 0, 0, 0, 0, 0]]) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 337 | gravity = 9.8 |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 338 | self.C[3, 0:6] = 0.5 * (self.A_continuous[1, 0:6] + |
| 339 | self.A_continuous[3, 0:6]) / gravity |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 340 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 341 | self.D = numpy.matrix([ |
| 342 | [0, 0], [0, 0], [0, 0], |
| 343 | [ |
| 344 | 0.5 * (self.B_continuous[1, 0] + self.B_continuous[3, 0]) / |
| 345 | gravity, |
| 346 | 0.5 * (self.B_continuous[1, 1] + self.B_continuous[3, 1]) / |
| 347 | gravity |
| 348 | ] |
| 349 | ]) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 350 | else: |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 351 | self.C = numpy.matrix([[1, 0, 0, 0, 0, 0, 0], |
| 352 | [0, 0, 1, 0, 0, 0, 0], |
| 353 | [ |
| 354 | 0, |
| 355 | -0.5 / drivetrain_params.robot_radius, |
| 356 | 0, 0.5 / drivetrain_params.robot_radius, |
| 357 | 0, 0, 0 |
| 358 | ]]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 359 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 360 | self.D = numpy.matrix([[0, 0], [0, 0], [0, 0]]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 361 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 362 | q_pos = 0.05 |
| 363 | q_vel = 1.00 |
| 364 | q_encoder_uncertainty = 2.00 |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 365 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 366 | self.Q = numpy.matrix( |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 367 | [[(q_pos**2.0), 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], |
| 368 | [0.0, (q_vel**2.0), 0.0, 0.0, 0.0, 0.0, 0.0], |
| 369 | [0.0, 0.0, (q_pos**2.0), 0.0, 0.0, 0.0, 0.0], |
| 370 | [0.0, 0.0, 0.0, (q_vel**2.0), 0.0, 0.0, 0.0], |
| 371 | [0.0, 0.0, 0.0, 0.0, (q_voltage**2.0), 0.0, 0.0], |
| 372 | [0.0, 0.0, 0.0, 0.0, 0.0, (q_voltage**2.0), 0.0], |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 373 | [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, (q_encoder_uncertainty**2.0)]]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 374 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 375 | r_pos = 0.0001 |
| 376 | r_gyro = 0.000001 |
| 377 | if self.has_imu: |
| 378 | r_accelerometer = 7.0 |
| 379 | self.R = numpy.matrix([[(r_pos**2.0), 0.0, 0.0, 0.0], |
| 380 | [0.0, (r_pos**2.0), 0.0, 0.0], |
| 381 | [0.0, 0.0, (r_gyro**2.0), 0.0], |
| 382 | [0.0, 0.0, 0.0, (r_accelerometer**2.0)]]) |
| 383 | else: |
| 384 | self.R = numpy.matrix([[(r_pos**2.0), 0.0, 0.0], |
| 385 | [0.0, (r_pos**2.0), 0.0], |
| 386 | [0.0, 0.0, (r_gyro**2.0)]]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 387 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 388 | # Solving for kf gains. |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 389 | self.KalmanGain, self.Q_steady = controls.kalman(A=self.A, |
| 390 | B=self.B, |
| 391 | C=self.C, |
| 392 | Q=self.Q, |
| 393 | R=self.R) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 394 | |
James Kuszmaul | 4d752d5 | 2019-02-09 17:27:55 -0800 | [diff] [blame] | 395 | # If we don't have an IMU, pad various matrices with zeros so that |
| 396 | # we can still have 4 measurement outputs. |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 397 | if not self.has_imu: |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 398 | self.KalmanGain = numpy.hstack( |
| 399 | (self.KalmanGain, numpy.matrix(numpy.zeros((7, 1))))) |
Austin Schuh | ecc92a0 | 2019-01-20 17:42:19 -0800 | [diff] [blame] | 400 | self.C = numpy.vstack((self.C, numpy.matrix(numpy.zeros((1, 7))))) |
| 401 | self.D = numpy.vstack((self.D, numpy.matrix(numpy.zeros((1, 2))))) |
James Kuszmaul | 4d752d5 | 2019-02-09 17:27:55 -0800 | [diff] [blame] | 402 | Rtmp = numpy.zeros((4, 4)) |
| 403 | Rtmp[0:3, 0:3] = self.R |
| 404 | self.R = Rtmp |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 405 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 406 | self.L = self.A * self.KalmanGain |
| 407 | |
| 408 | unaug_K = self.K |
| 409 | |
| 410 | # Implement a nice closed loop controller for use by the closed loop |
| 411 | # controller. |
| 412 | self.K = numpy.matrix(numpy.zeros((self.B.shape[1], self.A.shape[0]))) |
| 413 | self.K[0:2, 0:4] = unaug_K |
| 414 | if self.force: |
| 415 | self.K[0, 4] = 1.0 / self.mpl |
| 416 | self.K[1, 5] = 1.0 / self.mpr |
| 417 | else: |
| 418 | self.K[0, 4] = 1.0 |
| 419 | self.K[1, 5] = 1.0 |
| 420 | |
| 421 | self.Qff = numpy.matrix(numpy.zeros((4, 4))) |
| 422 | qff_pos = 0.005 |
| 423 | qff_vel = 1.00 |
| 424 | self.Qff[0, 0] = 1.0 / qff_pos**2.0 |
| 425 | self.Qff[1, 1] = 1.0 / qff_vel**2.0 |
| 426 | self.Qff[2, 2] = 1.0 / qff_pos**2.0 |
| 427 | self.Qff[3, 3] = 1.0 / qff_vel**2.0 |
| 428 | self.Kff = numpy.matrix(numpy.zeros((2, 7))) |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 429 | self.Kff[0:2, |
| 430 | 0:4] = controls.TwoStateFeedForwards(self.B[0:4, :], self.Qff) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 431 | |
| 432 | self.InitializeState() |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 433 | |
| 434 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 435 | def WriteDrivetrain(drivetrain_files, |
| 436 | kf_drivetrain_files, |
| 437 | year_namespace, |
| 438 | drivetrain_params, |
| 439 | scalar_type='double'): |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 440 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 441 | # Write the generated constants out to a file. |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 442 | drivetrain_low_low = Drivetrain(name="DrivetrainLowLow", |
| 443 | left_low=True, |
| 444 | right_low=True, |
| 445 | drivetrain_params=drivetrain_params) |
| 446 | drivetrain_low_high = Drivetrain(name="DrivetrainLowHigh", |
| 447 | left_low=True, |
| 448 | right_low=False, |
| 449 | drivetrain_params=drivetrain_params) |
| 450 | drivetrain_high_low = Drivetrain(name="DrivetrainHighLow", |
| 451 | left_low=False, |
| 452 | right_low=True, |
| 453 | drivetrain_params=drivetrain_params) |
| 454 | drivetrain_high_high = Drivetrain(name="DrivetrainHighHigh", |
| 455 | left_low=False, |
| 456 | right_low=False, |
| 457 | drivetrain_params=drivetrain_params) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 458 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 459 | kf_drivetrain_low_low = KFDrivetrain(name="KFDrivetrainLowLow", |
| 460 | left_low=True, |
| 461 | right_low=True, |
| 462 | drivetrain_params=drivetrain_params) |
| 463 | kf_drivetrain_low_high = KFDrivetrain(name="KFDrivetrainLowHigh", |
| 464 | left_low=True, |
| 465 | right_low=False, |
| 466 | drivetrain_params=drivetrain_params) |
| 467 | kf_drivetrain_high_low = KFDrivetrain(name="KFDrivetrainHighLow", |
| 468 | left_low=False, |
| 469 | right_low=True, |
| 470 | drivetrain_params=drivetrain_params) |
| 471 | kf_drivetrain_high_high = KFDrivetrain(name="KFDrivetrainHighHigh", |
| 472 | left_low=False, |
| 473 | right_low=False, |
| 474 | drivetrain_params=drivetrain_params) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 475 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 476 | if isinstance(year_namespace, list): |
Austin Schuh | ecc92a0 | 2019-01-20 17:42:19 -0800 | [diff] [blame] | 477 | namespaces = year_namespace |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 478 | else: |
Austin Schuh | ecc92a0 | 2019-01-20 17:42:19 -0800 | [diff] [blame] | 479 | namespaces = [year_namespace, 'control_loops', 'drivetrain'] |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 480 | dog_loop_writer = control_loop.ControlLoopWriter("Drivetrain", [ |
| 481 | drivetrain_low_low, drivetrain_low_high, drivetrain_high_low, |
| 482 | drivetrain_high_high |
| 483 | ], |
| 484 | namespaces=namespaces, |
| 485 | scalar_type=scalar_type) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 486 | dog_loop_writer.AddConstant( |
| 487 | control_loop.Constant("kDt", "%f", drivetrain_low_low.dt)) |
| 488 | dog_loop_writer.AddConstant( |
| 489 | control_loop.Constant("kStallTorque", "%f", |
| 490 | drivetrain_low_low.stall_torque)) |
| 491 | dog_loop_writer.AddConstant( |
| 492 | control_loop.Constant("kStallCurrent", "%f", |
| 493 | drivetrain_low_low.stall_current)) |
| 494 | dog_loop_writer.AddConstant( |
| 495 | control_loop.Constant("kFreeSpeed", "%f", |
| 496 | drivetrain_low_low.free_speed)) |
| 497 | dog_loop_writer.AddConstant( |
| 498 | control_loop.Constant("kFreeCurrent", "%f", |
| 499 | drivetrain_low_low.free_current)) |
| 500 | dog_loop_writer.AddConstant( |
| 501 | control_loop.Constant("kJ", "%f", drivetrain_low_low.J)) |
| 502 | dog_loop_writer.AddConstant( |
| 503 | control_loop.Constant("kMass", "%f", drivetrain_low_low.mass)) |
| 504 | dog_loop_writer.AddConstant( |
| 505 | control_loop.Constant("kRobotRadius", "%f", |
| 506 | drivetrain_low_low.robot_radius)) |
| 507 | dog_loop_writer.AddConstant( |
| 508 | control_loop.Constant("kWheelRadius", "%f", drivetrain_low_low.r)) |
| 509 | dog_loop_writer.AddConstant( |
| 510 | control_loop.Constant("kR", "%f", drivetrain_low_low.resistance)) |
| 511 | dog_loop_writer.AddConstant( |
| 512 | control_loop.Constant("kV", "%f", drivetrain_low_low.Kv)) |
| 513 | dog_loop_writer.AddConstant( |
| 514 | control_loop.Constant("kT", "%f", drivetrain_low_low.Kt)) |
| 515 | dog_loop_writer.AddConstant( |
| 516 | control_loop.Constant("kLowGearRatio", "%f", drivetrain_low_low.G_low)) |
| 517 | dog_loop_writer.AddConstant( |
| 518 | control_loop.Constant("kHighGearRatio", "%f", |
| 519 | drivetrain_high_high.G_high)) |
| 520 | dog_loop_writer.AddConstant( |
| 521 | control_loop.Constant( |
| 522 | "kHighOutputRatio", "%f", |
| 523 | drivetrain_high_high.G_high * drivetrain_high_high.r)) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 524 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 525 | dog_loop_writer.Write(drivetrain_files[0], drivetrain_files[1]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 526 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 527 | kf_loop_writer = control_loop.ControlLoopWriter("KFDrivetrain", [ |
| 528 | kf_drivetrain_low_low, kf_drivetrain_low_high, kf_drivetrain_high_low, |
| 529 | kf_drivetrain_high_high |
| 530 | ], |
| 531 | namespaces=namespaces, |
| 532 | scalar_type=scalar_type) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 533 | kf_loop_writer.Write(kf_drivetrain_files[0], kf_drivetrain_files[1]) |
| 534 | |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 535 | |
| 536 | def PlotDrivetrainMotions(drivetrain_params): |
Austin Schuh | 1542ea3 | 2021-01-23 20:19:50 -0800 | [diff] [blame] | 537 | # Test out the voltage error. |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 538 | drivetrain = KFDrivetrain(left_low=False, |
| 539 | right_low=False, |
| 540 | drivetrain_params=drivetrain_params) |
Austin Schuh | 1542ea3 | 2021-01-23 20:19:50 -0800 | [diff] [blame] | 541 | close_loop_left = [] |
| 542 | close_loop_right = [] |
| 543 | left_power = [] |
| 544 | right_power = [] |
| 545 | R = numpy.matrix([[0.0], [0.0], [0.0], [0.0], [0.0], [0.0], [0.0]]) |
Austin Schuh | 5ea4847 | 2021-02-02 20:46:41 -0800 | [diff] [blame] | 546 | for _ in range(300): |
Austin Schuh | 1542ea3 | 2021-01-23 20:19:50 -0800 | [diff] [blame] | 547 | U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min, |
| 548 | drivetrain.U_max) |
Austin Schuh | 64433f1 | 2022-02-21 19:40:38 -0800 | [diff] [blame] | 549 | drivetrain.CorrectObserver(U) |
| 550 | drivetrain.PredictObserver(U) |
Austin Schuh | 1542ea3 | 2021-01-23 20:19:50 -0800 | [diff] [blame] | 551 | drivetrain.Update(U + numpy.matrix([[1.0], [1.0]])) |
| 552 | close_loop_left.append(drivetrain.X[0, 0]) |
| 553 | close_loop_right.append(drivetrain.X[2, 0]) |
| 554 | left_power.append(U[0, 0]) |
| 555 | right_power.append(U[1, 0]) |
| 556 | |
| 557 | t = [drivetrain.dt * x for x in range(300)] |
| 558 | pylab.plot(t, close_loop_left, label='left position') |
| 559 | pylab.plot(t, close_loop_right, 'm--', label='right position') |
| 560 | pylab.plot(t, left_power, label='left power') |
| 561 | pylab.plot(t, right_power, '--', label='right power') |
| 562 | pylab.suptitle('Voltage error') |
| 563 | pylab.legend() |
| 564 | pylab.show() |
| 565 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 566 | # Simulate the response of the system to a step input. |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 567 | drivetrain = KFDrivetrain(left_low=False, |
| 568 | right_low=False, |
| 569 | drivetrain_params=drivetrain_params) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 570 | simulated_left = [] |
| 571 | simulated_right = [] |
Austin Schuh | 5ea4847 | 2021-02-02 20:46:41 -0800 | [diff] [blame] | 572 | for _ in range(100): |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 573 | drivetrain.Update(numpy.matrix([[12.0], [12.0]])) |
| 574 | simulated_left.append(drivetrain.X[0, 0]) |
| 575 | simulated_right.append(drivetrain.X[2, 0]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 576 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 577 | pylab.rc('lines', linewidth=4) |
| 578 | pylab.plot(range(100), simulated_left, label='left position') |
| 579 | pylab.plot(range(100), simulated_right, 'r--', label='right position') |
| 580 | pylab.suptitle('Acceleration Test\n12 Volt Step Input') |
| 581 | pylab.legend(loc='lower right') |
| 582 | pylab.show() |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 583 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 584 | # Simulate forwards motion. |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame^] | 585 | drivetrain = KFDrivetrain(left_low=False, |
| 586 | right_low=False, |
| 587 | drivetrain_params=drivetrain_params) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 588 | close_loop_left = [] |
| 589 | close_loop_right = [] |
| 590 | left_power = [] |
| 591 | right_power = [] |
Austin Schuh | 1542ea3 | 2021-01-23 20:19:50 -0800 | [diff] [blame] | 592 | R = numpy.matrix([[1.0], [0.0], [1.0], [0.0], [0.0], [0.0], [0.0]]) |
Austin Schuh | 5ea4847 | 2021-02-02 20:46:41 -0800 | [diff] [blame] | 593 | for _ in range(300): |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 594 | U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min, |
| 595 | drivetrain.U_max) |
Austin Schuh | 64433f1 | 2022-02-21 19:40:38 -0800 | [diff] [blame] | 596 | drivetrain.CorrectObserver(U) |
| 597 | drivetrain.PredictObserver(U) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 598 | drivetrain.Update(U) |
| 599 | close_loop_left.append(drivetrain.X[0, 0]) |
| 600 | close_loop_right.append(drivetrain.X[2, 0]) |
| 601 | left_power.append(U[0, 0]) |
| 602 | right_power.append(U[1, 0]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 603 | |
Austin Schuh | 1542ea3 | 2021-01-23 20:19:50 -0800 | [diff] [blame] | 604 | t = [drivetrain.dt * x for x in range(300)] |
| 605 | pylab.plot(t, close_loop_left, label='left position') |
| 606 | pylab.plot(t, close_loop_right, 'm--', label='right position') |
| 607 | pylab.plot(t, left_power, label='left power') |
| 608 | pylab.plot(t, right_power, '--', label='right power') |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 609 | pylab.suptitle('Linear Move\nLeft and Right Position going to 1') |
| 610 | pylab.legend() |
| 611 | pylab.show() |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 612 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 613 | # Try turning in place |
Austin Schuh | 1542ea3 | 2021-01-23 20:19:50 -0800 | [diff] [blame] | 614 | drivetrain = KFDrivetrain(drivetrain_params=drivetrain_params) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 615 | close_loop_left = [] |
| 616 | close_loop_right = [] |
Austin Schuh | 1542ea3 | 2021-01-23 20:19:50 -0800 | [diff] [blame] | 617 | R = numpy.matrix([[-1.0], [0.0], [1.0], [0.0], [0.0], [0.0], [0.0]]) |
Austin Schuh | 5ea4847 | 2021-02-02 20:46:41 -0800 | [diff] [blame] | 618 | for _ in range(200): |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 619 | U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min, |
| 620 | drivetrain.U_max) |
Austin Schuh | 64433f1 | 2022-02-21 19:40:38 -0800 | [diff] [blame] | 621 | drivetrain.CorrectObserver(U) |
| 622 | drivetrain.PredictObserver(U) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 623 | drivetrain.Update(U) |
| 624 | close_loop_left.append(drivetrain.X[0, 0]) |
| 625 | close_loop_right.append(drivetrain.X[2, 0]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 626 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 627 | pylab.plot(range(200), close_loop_left, label='left position') |
| 628 | pylab.plot(range(200), close_loop_right, label='right position') |
| 629 | pylab.suptitle( |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 630 | 'Angular Move\nLeft position going to -1 and right position going to 1' |
| 631 | ) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 632 | pylab.legend(loc='center right') |
| 633 | pylab.show() |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 634 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 635 | # Try turning just one side. |
Austin Schuh | 1542ea3 | 2021-01-23 20:19:50 -0800 | [diff] [blame] | 636 | drivetrain = KFDrivetrain(drivetrain_params=drivetrain_params) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 637 | close_loop_left = [] |
| 638 | close_loop_right = [] |
Austin Schuh | 1542ea3 | 2021-01-23 20:19:50 -0800 | [diff] [blame] | 639 | R = numpy.matrix([[0.0], [0.0], [1.0], [0.0], [0.0], [0.0], [0.0]]) |
Austin Schuh | 5ea4847 | 2021-02-02 20:46:41 -0800 | [diff] [blame] | 640 | for _ in range(300): |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 641 | U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min, |
| 642 | drivetrain.U_max) |
Austin Schuh | 64433f1 | 2022-02-21 19:40:38 -0800 | [diff] [blame] | 643 | drivetrain.CorrectObserver(U) |
| 644 | drivetrain.PredictObserver(U) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 645 | drivetrain.Update(U) |
| 646 | close_loop_left.append(drivetrain.X[0, 0]) |
| 647 | close_loop_right.append(drivetrain.X[2, 0]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 648 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 649 | pylab.plot(range(300), close_loop_left, label='left position') |
| 650 | pylab.plot(range(300), close_loop_right, label='right position') |
| 651 | pylab.suptitle( |
| 652 | 'Pivot\nLeft position not changing and right position going to 1') |
| 653 | pylab.legend(loc='center right') |
| 654 | pylab.show() |