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 |
Austin Schuh | 566c82b | 2023-01-27 20:49:08 -0800 | [diff] [blame] | 6 | import math |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 7 | import sys |
| 8 | from matplotlib import pylab |
| 9 | import glog |
| 10 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 11 | |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 12 | class DrivetrainParams(object): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 13 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 14 | def __init__(self, |
| 15 | J, |
| 16 | mass, |
| 17 | robot_radius, |
| 18 | wheel_radius, |
Austin Schuh | ecc92a0 | 2019-01-20 17:42:19 -0800 | [diff] [blame] | 19 | 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 Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 28 | 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 Schuh | 95fbcc5 | 2018-03-10 20:57:20 -0800 | [diff] [blame] | 36 | observer_poles=[0.02, 0.02], |
Austin Schuh | 566c82b | 2023-01-27 20:49:08 -0800 | [diff] [blame] | 37 | robot_cg_offset=0.0, |
| 38 | coefficient_of_friction=1.0): |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 39 | """Defines all constants of a drivetrain. |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 40 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 41 | 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 Schuh | ecc92a0 | 2019-01-20 17:42:19 -0800 | [diff] [blame] | 47 | G: float, Gear ratio for a single speed. |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 48 | 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 Schuh | 1542ea3 | 2021-01-23 20:19:50 -0800 | [diff] [blame] | 51 | 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 Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 57 | 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 Schuh | 1542ea3 | 2021-01-23 20:19:50 -0800 | [diff] [blame] | 63 | controller_poles: array, An array of poles for the polydrivetrain |
| 64 | controller. (See control_loop.py) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 65 | observer_poles: array, An array of poles. (See control_loop.py) |
Michael Schuh | 95fbcc5 | 2018-03-10 20:57:20 -0800 | [diff] [blame] | 66 | 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] | 67 | """ |
Austin Schuh | ecc92a0 | 2019-01-20 17:42:19 -0800 | [diff] [blame] | 68 | 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 Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 91 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 92 | self.J = J |
| 93 | self.mass = mass |
| 94 | self.robot_radius = robot_radius |
Michael Schuh | 95fbcc5 | 2018-03-10 20:57:20 -0800 | [diff] [blame] | 95 | self.robot_cg_offset = robot_cg_offset |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 96 | 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 Schuh | 566c82b | 2023-01-27 20:49:08 -0800 | [diff] [blame] | 112 | self.coefficient_of_friction = coefficient_of_friction |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 113 | |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 114 | |
| 115 | class Drivetrain(control_loop.ControlLoop): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 116 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 117 | 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 Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 123 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 124 | 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 Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 131 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 132 | # 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 Schuh | 95fbcc5 | 2018-03-10 20:57:20 -0800 | [diff] [blame] | 141 | # 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 Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 149 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 150 | # 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 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 | # Control loop time step |
| 163 | self.dt = drivetrain_params.dt |
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.efficiency = drivetrain_params.efficiency |
| 166 | self.force = drivetrain_params.force |
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 | self.BuildDrivetrain(drivetrain_params.motor_type, |
| 169 | drivetrain_params.num_motors) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 170 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 171 | 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 Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 177 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 178 | self.BuildDrivetrainController(q_pos, q_vel) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 179 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 180 | self.InitializeState() |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 181 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 182 | 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 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 | # Effective motor resistance in ohms. |
| 196 | self.resistance = 12.0 / self.stall_current |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 197 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 198 | # Resistance of the motor, divided by the number of motors. |
| 199 | # Motor velocity constant |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 200 | self.Kv = (self.free_speed / |
| 201 | (12.0 - self.resistance * self.free_current)) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 202 | # Torque constant |
| 203 | self.Kt = self.stall_torque / self.stall_current |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 204 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 205 | # These describe the way that a given side of a robot will be influenced |
| 206 | # by the other side. Units of 1 / kg. |
Michael Schuh | 95fbcc5 | 2018-03-10 20:57:20 -0800 | [diff] [blame] | 207 | 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 Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 213 | # The calculations which we will need for A and B. |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 214 | 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 Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 218 | self.mpl = self.Kt / (self.Gl * self.resistance * self.r) |
| 219 | self.mpr = self.Kt / (self.Gr * self.resistance * self.r) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 220 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 221 | # State feedback matrices |
| 222 | # X will be of the format |
Austin Schuh | d77c064 | 2020-12-30 21:38:54 -0800 | [diff] [blame] | 223 | # [[positionl], [velocityl], [positionr], [velocityr]] |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 224 | self.A_continuous = numpy.matrix( |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 225 | [[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 Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 229 | self.B_continuous = numpy.matrix( |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 230 | [[0, 0], [self.mspl * self.mpl, self.msnr * self.mpr], [0, 0], |
Michael Schuh | 95fbcc5 | 2018-03-10 20:57:20 -0800 | [diff] [blame] | 231 | [self.msnl * self.mpl, self.mspr * self.mpr]]) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 232 | self.C = numpy.matrix([[1, 0, 0, 0], [0, 0, 1, 0]]) |
| 233 | self.D = numpy.matrix([[0, 0], [0, 0]]) |
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 | self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, |
| 236 | self.B_continuous, self.dt) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 237 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 238 | def BuildDrivetrainController(self, q_pos, q_vel): |
Austin Schuh | d77c064 | 2020-12-30 21:38:54 -0800 | [diff] [blame] | 239 | # 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 Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 246 | # Tune the LQR controller |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 247 | 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 Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 251 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 252 | 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 Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 255 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 256 | 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] | 257 | 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 Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 265 | glog.debug('K %s', repr(self.K)) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 266 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 267 | 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 Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 274 | |
| 275 | class KFDrivetrain(Drivetrain): |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 276 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 277 | 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 Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 283 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 284 | 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 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 | self.unaugmented_A_continuous = self.A_continuous |
| 294 | self.unaugmented_B_continuous = self.B_continuous |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 295 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 296 | # 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 Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 312 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 313 | if self.force: |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 314 | self.A_continuous[0:4, 4:6] = numpy.matrix([[0.0, 0.0], |
| 315 | [self.mspl, self.msnl], |
| 316 | [0.0, 0.0], |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 317 | [self.msnr, |
| 318 | self.mspr]]) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 319 | 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 Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 323 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 324 | 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 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 | self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, |
| 329 | self.B_continuous, self.dt) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 330 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 331 | if self.has_imu: |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 332 | 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 Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 340 | gravity = 9.8 |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 341 | self.C[3, 0:6] = 0.5 * (self.A_continuous[1, 0:6] + |
| 342 | self.A_continuous[3, 0:6]) / gravity |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 343 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 344 | 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 Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 353 | else: |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 354 | 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 Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 362 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 363 | self.D = numpy.matrix([[0, 0], [0, 0], [0, 0]]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 364 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 365 | q_pos = 0.05 |
| 366 | q_vel = 1.00 |
| 367 | q_encoder_uncertainty = 2.00 |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 368 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 369 | self.Q = numpy.matrix( |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 370 | [[(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 Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 376 | [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] | 377 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 378 | 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 Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 390 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 391 | # Solving for kf gains. |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 392 | 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 Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 397 | |
James Kuszmaul | 4d752d5 | 2019-02-09 17:27:55 -0800 | [diff] [blame] | 398 | # If we don't have an IMU, pad various matrices with zeros so that |
| 399 | # we can still have 4 measurement outputs. |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 400 | if not self.has_imu: |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 401 | self.KalmanGain = numpy.hstack( |
| 402 | (self.KalmanGain, numpy.matrix(numpy.zeros((7, 1))))) |
Austin Schuh | ecc92a0 | 2019-01-20 17:42:19 -0800 | [diff] [blame] | 403 | 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 Kuszmaul | 4d752d5 | 2019-02-09 17:27:55 -0800 | [diff] [blame] | 405 | Rtmp = numpy.zeros((4, 4)) |
| 406 | Rtmp[0:3, 0:3] = self.R |
| 407 | self.R = Rtmp |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 408 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 409 | 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 Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 432 | self.Kff[0:2, |
| 433 | 0:4] = controls.TwoStateFeedForwards(self.B[0:4, :], self.Qff) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 434 | |
| 435 | self.InitializeState() |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 436 | |
| 437 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 438 | def WriteDrivetrain(drivetrain_files, |
| 439 | kf_drivetrain_files, |
| 440 | year_namespace, |
| 441 | drivetrain_params, |
| 442 | scalar_type='double'): |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 443 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 444 | # Write the generated constants out to a file. |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 445 | 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 Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 461 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 462 | 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 Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 478 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 479 | if isinstance(year_namespace, list): |
Austin Schuh | ecc92a0 | 2019-01-20 17:42:19 -0800 | [diff] [blame] | 480 | namespaces = year_namespace |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 481 | else: |
Austin Schuh | ecc92a0 | 2019-01-20 17:42:19 -0800 | [diff] [blame] | 482 | namespaces = [year_namespace, 'control_loops', 'drivetrain'] |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 483 | 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 Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 489 | 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 Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 527 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 528 | dog_loop_writer.Write(drivetrain_files[0], drivetrain_files[1]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 529 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 530 | 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 Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 536 | kf_loop_writer.Write(kf_drivetrain_files[0], kf_drivetrain_files[1]) |
| 537 | |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 538 | |
Austin Schuh | 566c82b | 2023-01-27 20:49:08 -0800 | [diff] [blame] | 539 | def 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 | |
Austin Schuh | caed736 | 2024-01-13 15:29:05 -0800 | [diff] [blame^] | 569 | kMaxBreakerCurrent = 220 |
| 570 | |
Austin Schuh | 566c82b | 2023-01-27 20:49:08 -0800 | [diff] [blame] | 571 | vbat = 12.6 |
| 572 | # Measured resistance of the battery, pd board, and breakers. |
| 573 | Rw = 0.023 |
| 574 | top_speed = drivetrain.free_speed * (drivetrain.Gr + |
| 575 | drivetrain.Gl) / 2.0 * drivetrain.r |
| 576 | |
| 577 | passed_distance = False |
| 578 | max_breaker_current = 0 |
| 579 | heat_energy_usage = 0.0 |
| 580 | for index in range(800): |
| 581 | # Current per side |
| 582 | left_traction_current = (drivetrain.mass / 2.0 * |
| 583 | drivetrain_params.coefficient_of_friction * |
| 584 | 9.81 * drivetrain.r * drivetrain.Gl / |
| 585 | drivetrain.Kt) |
| 586 | right_traction_current = (drivetrain.mass / 2.0 * |
| 587 | drivetrain_params.coefficient_of_friction * |
| 588 | 9.81 * drivetrain.r * drivetrain.Gr / |
| 589 | drivetrain.Kt) |
| 590 | |
| 591 | # Detect if we've traveled over the sprint distance and report stats. |
| 592 | if (drivetrain.X[0, 0] + drivetrain.X[2, 0]) / 2.0 > kSprintDistance: |
| 593 | if not passed_distance: |
| 594 | velocity = (drivetrain.X[1, 0] + drivetrain.X[3, 0]) / 2.0 |
| 595 | print("Took", index * drivetrain.dt, |
| 596 | "to pass 1/2 field, going", velocity, "m/s,", |
| 597 | velocity / 0.0254 / 12.0, "Traction limit current", |
| 598 | left_traction_current / drivetrain_params.num_motors, |
| 599 | "max breaker current", max_breaker_current, "top speed", |
| 600 | top_speed, "m/s", top_speed / 0.0254 / 12.0, |
| 601 | "fps, gear ratio", drivetrain.Gl, "heat energy", |
| 602 | heat_energy_usage) |
| 603 | passed_distance = True |
| 604 | |
| 605 | bemf_left = drivetrain.X[ |
| 606 | 1, 0] / drivetrain.r / drivetrain.Gl / drivetrain.Kv |
| 607 | bemf_right = drivetrain.X[ |
| 608 | 3, 0] / drivetrain.r / drivetrain.Gr / drivetrain.Kv |
| 609 | |
| 610 | # Max current we could push through the motors is what we would get if |
| 611 | # we short the battery through the battery resistance into the motor. |
Austin Schuh | caed736 | 2024-01-13 15:29:05 -0800 | [diff] [blame^] | 612 | bemf = (bemf_left + bemf_right) / 2.0 |
| 613 | max_motor_current = (vbat - bemf) / (Rw + drivetrain.resistance / 2.0) |
Austin Schuh | 566c82b | 2023-01-27 20:49:08 -0800 | [diff] [blame] | 614 | |
| 615 | max_motor_currents.append(max_motor_current / |
| 616 | (drivetrain_params.num_motors * 2)) |
| 617 | |
| 618 | # From this current, we can compute the voltage we can apply. |
| 619 | # This is either the traction limit or the current limit. |
Austin Schuh | caed736 | 2024-01-13 15:29:05 -0800 | [diff] [blame^] | 620 | max_current_request_left = min(max_motor_current / 2, |
| 621 | left_traction_current) |
| 622 | max_current_request_right = min(max_motor_current / 2, |
| 623 | right_traction_current) |
| 624 | max_voltage_left = (bemf_left + |
| 625 | max_current_request_left * drivetrain.resistance) |
| 626 | max_voltage_right = (bemf_right + |
| 627 | max_current_request_right * drivetrain.resistance) |
| 628 | |
| 629 | # Now, make sure we don't pull more power out of the battery than the |
| 630 | # breakers will let us pull. Do this by comparing the max power we can |
| 631 | # pull out of the battery with the requested power. |
| 632 | # |
| 633 | # TODO(austin): This all assumes the robot is symetric... |
| 634 | max_battery_wattage = kMaxBreakerCurrent * (vbat - |
| 635 | kMaxBreakerCurrent * Rw) |
| 636 | if (max_current_request_left * max_voltage_left + |
| 637 | max_current_request_right * max_voltage_right > |
| 638 | max_battery_wattage): |
| 639 | # Now solve the quadratic equation to figure out what the overall |
| 640 | # motor current can be which puts us at the max battery wattage. |
| 641 | max_motor_current = ( |
| 642 | -bemf + math.sqrt(bemf * bemf + 4 * drivetrain.resistance / |
| 643 | 2.0 * max_battery_wattage)) / ( |
| 644 | 2.0 * drivetrain.resistance / 2.0) |
| 645 | # Clip each side's currents to 1/2 of the max motor current since |
| 646 | # we know we are limited. |
| 647 | max_current_request_left = min(max_motor_current / 2.0, |
| 648 | max_current_request_left) |
| 649 | max_current_request_right = min(max_motor_current / 2.0, |
| 650 | max_current_request_right) |
| 651 | # And then update the voltages. |
| 652 | max_voltage_left = ( |
| 653 | bemf_left + max_current_request_left * drivetrain.resistance) |
| 654 | max_voltage_right = ( |
| 655 | bemf_right + max_current_request_right * drivetrain.resistance) |
Austin Schuh | 566c82b | 2023-01-27 20:49:08 -0800 | [diff] [blame] | 656 | |
| 657 | simulated_left_position.append(drivetrain.X[0, 0]) |
| 658 | simulated_left_velocity.append(drivetrain.X[1, 0]) |
| 659 | simulated_right_position.append(drivetrain.X[2, 0]) |
| 660 | simulated_right_velocity.append(drivetrain.X[3, 0]) |
| 661 | |
| 662 | U = numpy.matrix([[min(max_voltage_left, vbat)], |
| 663 | [min(max_voltage_right, vbat)]]) |
| 664 | |
| 665 | # Stator current |
| 666 | simulated_left_motor_current = (U[0, 0] - |
| 667 | bemf_left) / drivetrain.resistance |
| 668 | simulated_right_motor_current = (U[1, 0] - |
| 669 | bemf_right) / drivetrain.resistance |
| 670 | |
| 671 | # And this gives us the power pushed into the motors. |
| 672 | power = (U[0, 0] * simulated_left_motor_current + |
| 673 | U[1, 0] * simulated_right_motor_current) |
| 674 | |
| 675 | simulated_wattage.append(power) |
| 676 | |
| 677 | # Solve for the voltage we'd have to supply to the input of the motor |
| 678 | # controller to generate the power required. |
| 679 | motor_inverter_voltage = ( |
| 680 | vbat + numpy.sqrt(vbat * vbat - 4.0 * power * Rw)) / 2.0 |
| 681 | |
| 682 | overall_current = (vbat - motor_inverter_voltage) / Rw |
| 683 | overall_currents.append(overall_current) |
| 684 | |
| 685 | motor_inverter_voltages.append(motor_inverter_voltage) |
| 686 | |
| 687 | # Overall left and right currents at the breaker |
| 688 | simulated_left_breaker_current = ( |
| 689 | simulated_left_motor_current / |
| 690 | drivetrain_params.num_motors) * U[0, 0] / motor_inverter_voltage |
| 691 | simulated_right_breaker_current = ( |
| 692 | simulated_right_motor_current / |
| 693 | drivetrain_params.num_motors) * U[1, 0] / motor_inverter_voltage |
| 694 | |
| 695 | simulated_left_motor_currents.append(simulated_left_motor_current / |
| 696 | drivetrain_params.num_motors) |
| 697 | simulated_left_breaker_currents.append(simulated_left_breaker_current) |
| 698 | simulated_right_motor_currents.append(simulated_right_motor_current / |
| 699 | drivetrain_params.num_motors) |
| 700 | simulated_right_breaker_currents.append( |
| 701 | simulated_right_breaker_current) |
| 702 | |
| 703 | # Save out the peak battery current observed. |
| 704 | max_breaker_current = max( |
| 705 | max_breaker_current, |
| 706 | max(simulated_left_breaker_current, |
| 707 | simulated_right_breaker_current)) |
| 708 | |
| 709 | # Compute the heat burned in the battery |
| 710 | simulated_battery_heat_wattage = math.pow( |
| 711 | vbat - motor_inverter_voltage, 2.0) / Rw |
| 712 | simulated_battery_heat_wattages.append(simulated_battery_heat_wattage) |
| 713 | |
| 714 | motor_heat_wattage = (math.pow(simulated_left_motor_current, 2.0) * |
| 715 | drivetrain.resistance + |
| 716 | math.pow(simulated_right_motor_current, 2.0) * |
| 717 | drivetrain.resistance) |
| 718 | simulated_motor_heat_wattages.append(motor_heat_wattage) |
| 719 | |
| 720 | simulated_motor_wattage.append(simulated_left_motor_current * U[0, 0] + |
| 721 | simulated_right_motor_current * U[1, 0]) |
| 722 | |
| 723 | simulated_battery_wattage.append(vbat * overall_current) |
| 724 | |
| 725 | # And then the overall energy outputted by the battery. |
| 726 | heat_energy_usage += (motor_heat_wattage + |
| 727 | simulated_battery_heat_wattage) * drivetrain.dt |
| 728 | |
| 729 | voltage_left.append(U[0, 0]) |
| 730 | voltage_right.append(U[1, 0]) |
| 731 | |
| 732 | drivetrain.Update(U) |
| 733 | |
| 734 | t = [drivetrain.dt * x for x in range(len(simulated_left_position))] |
| 735 | pylab.rc('lines', linewidth=4) |
| 736 | pylab.subplot(3, 1, 1) |
| 737 | pylab.plot(t, simulated_left_position, label='left position') |
| 738 | pylab.plot(t, simulated_right_position, 'r--', label='right position') |
| 739 | pylab.plot(t, simulated_left_velocity, label='left velocity') |
| 740 | pylab.plot(t, simulated_right_velocity, label='right velocity') |
| 741 | |
| 742 | pylab.suptitle('Acceleration Test\n12 Volt Step Input') |
| 743 | pylab.legend(loc='lower right') |
| 744 | |
| 745 | pylab.subplot(3, 1, 2) |
| 746 | |
| 747 | pylab.plot(t, simulated_left_motor_currents, label='left rotor current') |
| 748 | pylab.plot(t, |
| 749 | simulated_right_motor_currents, |
| 750 | 'r--', |
| 751 | label='right rotor current') |
| 752 | pylab.plot(t, |
| 753 | simulated_left_breaker_currents, |
| 754 | label='left breaker current') |
| 755 | pylab.plot(t, |
| 756 | simulated_right_breaker_currents, |
| 757 | 'r--', |
| 758 | label='right breaker current') |
| 759 | pylab.plot(t, motor_inverter_voltages, label='motor inverter voltage') |
| 760 | pylab.plot(t, voltage_left, label='left voltage') |
| 761 | pylab.plot(t, voltage_right, label='right voltage') |
| 762 | pylab.plot(t, max_motor_currents, label='max_currents') |
| 763 | pylab.legend(loc='lower right') |
| 764 | |
| 765 | wattage_axis = pylab.subplot(3, 1, 3) |
| 766 | wattage_axis.plot(t, simulated_wattage, label='wattage') |
| 767 | wattage_axis.plot(t, |
| 768 | simulated_battery_heat_wattages, |
| 769 | label='battery wattage') |
| 770 | wattage_axis.plot(t, |
| 771 | simulated_motor_heat_wattages, |
| 772 | label='motor heat wattage') |
| 773 | wattage_axis.plot(t, simulated_motor_wattage, label='motor wattage') |
| 774 | wattage_axis.plot(t, simulated_battery_wattage, label='overall wattage') |
| 775 | pylab.legend(loc='upper left') |
| 776 | overall_current_axis = wattage_axis.twinx() |
| 777 | overall_current_axis.plot(t, overall_currents, 'c--', label='current') |
| 778 | |
| 779 | pylab.legend(loc='lower right') |
| 780 | |
| 781 | pylab.suptitle('Acceleration Test\n12 Volt Step Input\n%f fps' % |
| 782 | (top_speed / 0.0254 / 12.0, )) |
| 783 | pylab.show() |
| 784 | |
| 785 | |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 786 | def PlotDrivetrainMotions(drivetrain_params): |
Austin Schuh | 1542ea3 | 2021-01-23 20:19:50 -0800 | [diff] [blame] | 787 | # Test out the voltage error. |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 788 | drivetrain = KFDrivetrain(left_low=False, |
| 789 | right_low=False, |
| 790 | drivetrain_params=drivetrain_params) |
Austin Schuh | 1542ea3 | 2021-01-23 20:19:50 -0800 | [diff] [blame] | 791 | close_loop_left = [] |
| 792 | close_loop_right = [] |
| 793 | left_power = [] |
| 794 | right_power = [] |
| 795 | 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] | 796 | for _ in range(300): |
Austin Schuh | 1542ea3 | 2021-01-23 20:19:50 -0800 | [diff] [blame] | 797 | U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min, |
| 798 | drivetrain.U_max) |
Austin Schuh | 64433f1 | 2022-02-21 19:40:38 -0800 | [diff] [blame] | 799 | drivetrain.CorrectObserver(U) |
| 800 | drivetrain.PredictObserver(U) |
Austin Schuh | 1542ea3 | 2021-01-23 20:19:50 -0800 | [diff] [blame] | 801 | drivetrain.Update(U + numpy.matrix([[1.0], [1.0]])) |
| 802 | close_loop_left.append(drivetrain.X[0, 0]) |
| 803 | close_loop_right.append(drivetrain.X[2, 0]) |
| 804 | left_power.append(U[0, 0]) |
| 805 | right_power.append(U[1, 0]) |
| 806 | |
| 807 | t = [drivetrain.dt * x for x in range(300)] |
| 808 | pylab.plot(t, close_loop_left, label='left position') |
| 809 | pylab.plot(t, close_loop_right, 'm--', label='right position') |
| 810 | pylab.plot(t, left_power, label='left power') |
| 811 | pylab.plot(t, right_power, '--', label='right power') |
| 812 | pylab.suptitle('Voltage error') |
| 813 | pylab.legend() |
| 814 | pylab.show() |
| 815 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 816 | # Simulate the response of the system to a step input. |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 817 | drivetrain = KFDrivetrain(left_low=False, |
| 818 | right_low=False, |
| 819 | drivetrain_params=drivetrain_params) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 820 | simulated_left = [] |
| 821 | simulated_right = [] |
Austin Schuh | 5ea4847 | 2021-02-02 20:46:41 -0800 | [diff] [blame] | 822 | for _ in range(100): |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 823 | drivetrain.Update(numpy.matrix([[12.0], [12.0]])) |
| 824 | simulated_left.append(drivetrain.X[0, 0]) |
| 825 | simulated_right.append(drivetrain.X[2, 0]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 826 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 827 | pylab.rc('lines', linewidth=4) |
| 828 | pylab.plot(range(100), simulated_left, label='left position') |
| 829 | pylab.plot(range(100), simulated_right, 'r--', label='right position') |
| 830 | pylab.suptitle('Acceleration Test\n12 Volt Step Input') |
| 831 | pylab.legend(loc='lower right') |
| 832 | pylab.show() |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 833 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 834 | # Simulate forwards motion. |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 835 | drivetrain = KFDrivetrain(left_low=False, |
| 836 | right_low=False, |
| 837 | drivetrain_params=drivetrain_params) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 838 | close_loop_left = [] |
| 839 | close_loop_right = [] |
| 840 | left_power = [] |
| 841 | right_power = [] |
Austin Schuh | 1542ea3 | 2021-01-23 20:19:50 -0800 | [diff] [blame] | 842 | 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] | 843 | for _ in range(300): |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 844 | U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min, |
| 845 | drivetrain.U_max) |
Austin Schuh | 64433f1 | 2022-02-21 19:40:38 -0800 | [diff] [blame] | 846 | drivetrain.CorrectObserver(U) |
| 847 | drivetrain.PredictObserver(U) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 848 | drivetrain.Update(U) |
| 849 | close_loop_left.append(drivetrain.X[0, 0]) |
| 850 | close_loop_right.append(drivetrain.X[2, 0]) |
| 851 | left_power.append(U[0, 0]) |
| 852 | right_power.append(U[1, 0]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 853 | |
Austin Schuh | 1542ea3 | 2021-01-23 20:19:50 -0800 | [diff] [blame] | 854 | t = [drivetrain.dt * x for x in range(300)] |
| 855 | pylab.plot(t, close_loop_left, label='left position') |
| 856 | pylab.plot(t, close_loop_right, 'm--', label='right position') |
| 857 | pylab.plot(t, left_power, label='left power') |
| 858 | pylab.plot(t, right_power, '--', label='right power') |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 859 | pylab.suptitle('Linear Move\nLeft and Right Position going to 1') |
| 860 | pylab.legend() |
| 861 | pylab.show() |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 862 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 863 | # Try turning in place |
Austin Schuh | 1542ea3 | 2021-01-23 20:19:50 -0800 | [diff] [blame] | 864 | drivetrain = KFDrivetrain(drivetrain_params=drivetrain_params) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 865 | close_loop_left = [] |
| 866 | close_loop_right = [] |
Austin Schuh | 1542ea3 | 2021-01-23 20:19:50 -0800 | [diff] [blame] | 867 | 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] | 868 | for _ in range(200): |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 869 | U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min, |
| 870 | drivetrain.U_max) |
Austin Schuh | 64433f1 | 2022-02-21 19:40:38 -0800 | [diff] [blame] | 871 | drivetrain.CorrectObserver(U) |
| 872 | drivetrain.PredictObserver(U) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 873 | drivetrain.Update(U) |
| 874 | close_loop_left.append(drivetrain.X[0, 0]) |
| 875 | close_loop_right.append(drivetrain.X[2, 0]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 876 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 877 | pylab.plot(range(200), close_loop_left, label='left position') |
| 878 | pylab.plot(range(200), close_loop_right, label='right position') |
| 879 | pylab.suptitle( |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 880 | 'Angular Move\nLeft position going to -1 and right position going to 1' |
| 881 | ) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 882 | pylab.legend(loc='center right') |
| 883 | pylab.show() |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 884 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 885 | # Try turning just one side. |
Austin Schuh | 1542ea3 | 2021-01-23 20:19:50 -0800 | [diff] [blame] | 886 | drivetrain = KFDrivetrain(drivetrain_params=drivetrain_params) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 887 | close_loop_left = [] |
| 888 | close_loop_right = [] |
Austin Schuh | 1542ea3 | 2021-01-23 20:19:50 -0800 | [diff] [blame] | 889 | 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] | 890 | for _ in range(300): |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 891 | U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min, |
| 892 | drivetrain.U_max) |
Austin Schuh | 64433f1 | 2022-02-21 19:40:38 -0800 | [diff] [blame] | 893 | drivetrain.CorrectObserver(U) |
| 894 | drivetrain.PredictObserver(U) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 895 | drivetrain.Update(U) |
| 896 | close_loop_left.append(drivetrain.X[0, 0]) |
| 897 | close_loop_right.append(drivetrain.X[2, 0]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 898 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 899 | pylab.plot(range(300), close_loop_left, label='left position') |
| 900 | pylab.plot(range(300), close_loop_right, label='right position') |
| 901 | pylab.suptitle( |
| 902 | 'Pivot\nLeft position not changing and right position going to 1') |
| 903 | pylab.legend(loc='center right') |
| 904 | pylab.show() |