Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 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): |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [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 | ecc92a0 | 2019-01-20 17:42:19 -0800 | [diff] [blame] | 49 | q_pos: float, q position for a single speed. |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 50 | q_pos_low: float, q position low gear. |
| 51 | q_pos_high: float, q position high gear. |
Austin Schuh | ecc92a0 | 2019-01-20 17:42:19 -0800 | [diff] [blame] | 52 | q_vel: float, q velocity for a single speed |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 53 | q_vel_low: float, q velocity low gear. |
| 54 | q_vel_high: float, q velocity high gear. |
| 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. |
| 61 | controller_poles: array, An array of poles. (See control_loop.py) |
| 62 | observer_poles: array, An array of poles. (See control_loop.py) |
Michael Schuh | 95fbcc5 | 2018-03-10 20:57:20 -0800 | [diff] [blame] | 63 | 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] | 64 | """ |
Austin Schuh | ecc92a0 | 2019-01-20 17:42:19 -0800 | [diff] [blame] | 65 | if G is not None: |
| 66 | assert (G_high is None) |
| 67 | assert (G_low is None) |
| 68 | G_high = G |
| 69 | G_low = G |
| 70 | assert (G_high is not None) |
| 71 | assert (G_low is not None) |
| 72 | |
| 73 | if q_pos is not None: |
| 74 | assert (q_pos_low is None) |
| 75 | assert (q_pos_high is None) |
| 76 | q_pos_low = q_pos |
| 77 | q_pos_high = q_pos |
| 78 | assert (q_pos_low is not None) |
| 79 | assert (q_pos_high is not None) |
| 80 | |
| 81 | if q_vel is not None: |
| 82 | assert (q_vel_low is None) |
| 83 | assert (q_vel_high is None) |
| 84 | q_vel_low = q_vel |
| 85 | q_vel_high = q_vel |
| 86 | assert (q_vel_low is not None) |
| 87 | assert (q_vel_high is not None) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 88 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 89 | self.J = J |
| 90 | self.mass = mass |
| 91 | self.robot_radius = robot_radius |
Michael Schuh | 95fbcc5 | 2018-03-10 20:57:20 -0800 | [diff] [blame] | 92 | self.robot_cg_offset = robot_cg_offset |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 93 | self.wheel_radius = wheel_radius |
| 94 | self.G_high = G_high |
| 95 | self.G_low = G_low |
| 96 | self.dt = dt |
| 97 | self.q_pos_low = q_pos_low |
| 98 | self.q_pos_high = q_pos_high |
| 99 | self.q_vel_low = q_vel_low |
| 100 | self.q_vel_high = q_vel_high |
| 101 | self.efficiency = efficiency |
| 102 | self.has_imu = has_imu |
| 103 | self.kf_q_voltage = kf_q_voltage |
| 104 | self.motor_type = motor_type |
| 105 | self.force = force |
| 106 | self.num_motors = num_motors |
| 107 | self.controller_poles = controller_poles |
| 108 | self.observer_poles = observer_poles |
| 109 | |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 110 | |
| 111 | class Drivetrain(control_loop.ControlLoop): |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 112 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 113 | def __init__(self, |
| 114 | drivetrain_params, |
| 115 | name="Drivetrain", |
| 116 | left_low=True, |
| 117 | right_low=True): |
| 118 | """Defines a base drivetrain for a robot. |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 119 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 120 | Args: |
| 121 | drivetrain_params: DrivetrainParams, class of values defining the drivetrain. |
| 122 | name: string, Name of this drivetrain. |
| 123 | left_low: bool, Whether the left is in high gear. |
| 124 | right_low: bool, Whether the right is in high gear. |
| 125 | """ |
| 126 | super(Drivetrain, self).__init__(name) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 127 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 128 | # Moment of inertia of the drivetrain in kg m^2 |
| 129 | self.J = drivetrain_params.J |
| 130 | # Mass of the robot, in kg. |
| 131 | self.mass = drivetrain_params.mass |
| 132 | # Radius of the robot, in meters (requires tuning by hand) |
| 133 | self.robot_radius = drivetrain_params.robot_radius |
| 134 | # Radius of the wheels, in meters. |
| 135 | self.r = drivetrain_params.wheel_radius |
| 136 | self.has_imu = drivetrain_params.has_imu |
Michael Schuh | 95fbcc5 | 2018-03-10 20:57:20 -0800 | [diff] [blame] | 137 | # Offset in meters of the CG from the center of the robot to the left side |
| 138 | # of the robot. Since the arm is on the right side, the offset will |
| 139 | # likely be a negative number. |
| 140 | self.robot_cg_offset = drivetrain_params.robot_cg_offset |
| 141 | # Distance from the left side of the robot to the Center of Gravity |
| 142 | self.robot_radius_l = drivetrain_params.robot_radius - self.robot_cg_offset |
| 143 | # Distance from the right side of the robot to the Center of Gravity |
| 144 | self.robot_radius_r = drivetrain_params.robot_radius + self.robot_cg_offset |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 145 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 146 | # Gear ratios |
| 147 | self.G_low = drivetrain_params.G_low |
| 148 | self.G_high = drivetrain_params.G_high |
| 149 | if left_low: |
| 150 | self.Gl = self.G_low |
| 151 | else: |
| 152 | self.Gl = self.G_high |
| 153 | if right_low: |
| 154 | self.Gr = self.G_low |
| 155 | else: |
| 156 | self.Gr = self.G_high |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 157 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 158 | # Control loop time step |
| 159 | self.dt = drivetrain_params.dt |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 160 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 161 | self.efficiency = drivetrain_params.efficiency |
| 162 | self.force = drivetrain_params.force |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 163 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 164 | self.BuildDrivetrain(drivetrain_params.motor_type, |
| 165 | drivetrain_params.num_motors) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 166 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 167 | if left_low or right_low: |
| 168 | q_pos = drivetrain_params.q_pos_low |
| 169 | q_vel = drivetrain_params.q_vel_low |
| 170 | else: |
| 171 | q_pos = drivetrain_params.q_pos_high |
| 172 | q_vel = drivetrain_params.q_vel_high |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 173 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 174 | self.BuildDrivetrainController(q_pos, q_vel) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 175 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 176 | self.InitializeState() |
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 | def BuildDrivetrain(self, motor, num_motors_per_side): |
| 179 | self.motor = motor |
| 180 | # Number of motors per side |
| 181 | self.num_motors = num_motors_per_side |
| 182 | # Stall Torque in N m |
| 183 | self.stall_torque = motor.stall_torque * self.num_motors * self.efficiency |
| 184 | # Stall Current in Amps |
| 185 | self.stall_current = motor.stall_current * self.num_motors |
| 186 | # Free Speed in rad/s |
| 187 | self.free_speed = motor.free_speed |
| 188 | # Free Current in Amps |
| 189 | self.free_current = motor.free_current * self.num_motors |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 190 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 191 | # Effective motor resistance in ohms. |
| 192 | self.resistance = 12.0 / self.stall_current |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 193 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 194 | # Resistance of the motor, divided by the number of motors. |
| 195 | # Motor velocity constant |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 196 | self.Kv = ( |
| 197 | self.free_speed / (12.0 - self.resistance * self.free_current)) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 198 | # Torque constant |
| 199 | self.Kt = self.stall_torque / self.stall_current |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 200 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 201 | # These describe the way that a given side of a robot will be influenced |
| 202 | # by the other side. Units of 1 / kg. |
Michael Schuh | 95fbcc5 | 2018-03-10 20:57:20 -0800 | [diff] [blame] | 203 | self.mspl = 1.0 / self.mass + self.robot_radius_l * self.robot_radius_l / self.J |
| 204 | self.mspr = 1.0 / self.mass + self.robot_radius_r * self.robot_radius_r / self.J |
| 205 | self.msnl = self.robot_radius_r / ( self.robot_radius_l * self.mass ) - \ |
| 206 | self.robot_radius_l * self.robot_radius_r / self.J |
| 207 | self.msnr = self.robot_radius_l / ( self.robot_radius_r * self.mass ) - \ |
| 208 | self.robot_radius_l * self.robot_radius_r / self.J |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 209 | # The calculations which we will need for A and B. |
| 210 | self.tcl = self.Kt / self.Kv / ( |
| 211 | self.Gl * self.Gl * self.resistance * self.r * self.r) |
| 212 | self.tcr = self.Kt / self.Kv / ( |
| 213 | self.Gr * self.Gr * self.resistance * self.r * self.r) |
| 214 | self.mpl = self.Kt / (self.Gl * self.resistance * self.r) |
| 215 | self.mpr = self.Kt / (self.Gr * self.resistance * self.r) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 216 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 217 | # State feedback matrices |
| 218 | # X will be of the format |
Austin Schuh | d77c064 | 2020-12-30 21:38:54 -0800 | [diff] [blame] | 219 | # [[positionl], [velocityl], [positionr], [velocityr]] |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 220 | self.A_continuous = numpy.matrix( |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 221 | [[0, 1, 0, 0], [0, -self.mspl * self.tcl, 0, -self.msnr * self.tcr], |
| 222 | [0, 0, 0, 1], [0, -self.msnl * self.tcl, 0, |
| 223 | -self.mspr * self.tcr]]) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 224 | self.B_continuous = numpy.matrix( |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 225 | [[0, 0], [self.mspl * self.mpl, self.msnr * self.mpr], [0, 0], |
Michael Schuh | 95fbcc5 | 2018-03-10 20:57:20 -0800 | [diff] [blame] | 226 | [self.msnl * self.mpl, self.mspr * self.mpr]]) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 227 | self.C = numpy.matrix([[1, 0, 0, 0], [0, 0, 1, 0]]) |
| 228 | self.D = numpy.matrix([[0, 0], [0, 0]]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 229 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 230 | self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, |
| 231 | self.B_continuous, self.dt) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 232 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 233 | def BuildDrivetrainController(self, q_pos, q_vel): |
Austin Schuh | d77c064 | 2020-12-30 21:38:54 -0800 | [diff] [blame] | 234 | # We can solve for the max velocity by setting \dot(x) = Ax + Bu to 0 |
| 235 | max_voltage = 12 |
| 236 | glog.debug( |
| 237 | "Max speed %f m/s", |
| 238 | -(self.B_continuous[1, 1] + self.B_continuous[1, 0]) / |
| 239 | (self.A_continuous[1, 1] + self.A_continuous[1, 3]) * max_voltage) |
| 240 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 241 | # Tune the LQR controller |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 242 | self.Q = numpy.matrix([[(1.0 / (q_pos**2.0)), 0.0, 0.0, 0.0], |
| 243 | [0.0, (1.0 / (q_vel**2.0)), 0.0, 0.0], |
| 244 | [0.0, 0.0, (1.0 / (q_pos**2.0)), 0.0], |
| 245 | [0.0, 0.0, 0.0, (1.0 / (q_vel**2.0))]]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 246 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 247 | self.R = numpy.matrix([[(1.0 / (12.0**2.0)), 0.0], |
| 248 | [0.0, (1.0 / (12.0**2.0))]]) |
| 249 | self.K = controls.dlqr(self.A, self.B, self.Q, self.R) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 250 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 251 | glog.debug('DT q_pos %f q_vel %s %s', q_pos, q_vel, self._name) |
| 252 | glog.debug(str(numpy.linalg.eig(self.A - self.B * self.K)[0])) |
| 253 | glog.debug('K %s', repr(self.K)) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 254 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 255 | self.hlp = 0.3 |
| 256 | self.llp = 0.4 |
| 257 | self.PlaceObserverPoles([self.hlp, self.hlp, self.llp, self.llp]) |
| 258 | |
| 259 | self.U_max = numpy.matrix([[12.0], [12.0]]) |
| 260 | self.U_min = numpy.matrix([[-12.0], [-12.0]]) |
| 261 | |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 262 | |
| 263 | class KFDrivetrain(Drivetrain): |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 264 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 265 | def __init__(self, |
| 266 | drivetrain_params, |
| 267 | name="KFDrivetrain", |
| 268 | left_low=True, |
| 269 | right_low=True): |
| 270 | """Kalman filter values of a drivetrain. |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 271 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 272 | Args: |
| 273 | drivetrain_params: DrivetrainParams, class of values defining the drivetrain. |
| 274 | name: string, Name of this drivetrain. |
| 275 | left_low: bool, Whether the left is in high gear. |
| 276 | right_low: bool, Whether the right is in high gear. |
| 277 | """ |
| 278 | super(KFDrivetrain, self).__init__(drivetrain_params, name, left_low, |
| 279 | right_low) |
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 | self.unaugmented_A_continuous = self.A_continuous |
| 282 | self.unaugmented_B_continuous = self.B_continuous |
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 | # The practical voltage applied to the wheels is |
| 285 | # V_left = U_left + left_voltage_error |
| 286 | # |
| 287 | # The states are |
| 288 | # [left position, left velocity, right position, right velocity, |
| 289 | # left voltage error, right voltage error, angular_error] |
| 290 | # |
| 291 | # The left and right positions are filtered encoder positions and are not |
| 292 | # adjusted for heading error. |
| 293 | # The turn velocity as computed by the left and right velocities is |
| 294 | # adjusted by the gyro velocity. |
| 295 | # The angular_error is the angular velocity error between the wheel speed |
| 296 | # and the gyro speed. |
| 297 | self.A_continuous = numpy.matrix(numpy.zeros((7, 7))) |
| 298 | self.B_continuous = numpy.matrix(numpy.zeros((7, 2))) |
| 299 | self.A_continuous[0:4, 0:4] = self.unaugmented_A_continuous |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 300 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 301 | if self.force: |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 302 | self.A_continuous[0:4, 4:6] = numpy.matrix([[0.0, 0.0], |
| 303 | [self.mspl, self.msnl], |
| 304 | [0.0, 0.0], |
| 305 | [self.msnr, self.mspr]]) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 306 | q_voltage = drivetrain_params.kf_q_voltage * self.mpl |
| 307 | else: |
| 308 | self.A_continuous[0:4, 4:6] = self.unaugmented_B_continuous |
| 309 | q_voltage = drivetrain_params.kf_q_voltage |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 310 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 311 | self.B_continuous[0:4, 0:2] = self.unaugmented_B_continuous |
| 312 | self.A_continuous[0, 6] = 1 |
| 313 | self.A_continuous[2, 6] = -1 |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 314 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 315 | self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, |
| 316 | self.B_continuous, self.dt) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 317 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 318 | if self.has_imu: |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 319 | self.C = numpy.matrix([[1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], |
| 320 | [ |
| 321 | 0, -0.5 / drivetrain_params.robot_radius, |
| 322 | 0, 0.5 / drivetrain_params.robot_radius, |
| 323 | 0, 0, 0 |
| 324 | ], [0, 0, 0, 0, 0, 0, 0]]) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 325 | gravity = 9.8 |
| 326 | self.C[3, 0:6] = 0.5 * ( |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 327 | self.A_continuous[1, 0:6] + self.A_continuous[3, 0:6]) / gravity |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 328 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 329 | self.D = numpy.matrix( |
| 330 | [[0, 0], [0, 0], [0, 0], |
| 331 | [ |
| 332 | 0.5 * (self.B_continuous[1, 0] + self.B_continuous[3, 0]) / |
| 333 | gravity, |
| 334 | 0.5 * (self.B_continuous[1, 1] + self.B_continuous[3, 1]) / |
| 335 | gravity |
| 336 | ]]) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 337 | else: |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 338 | self.C = numpy.matrix([[1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], |
| 339 | [ |
| 340 | 0, -0.5 / drivetrain_params.robot_radius, |
| 341 | 0, 0.5 / drivetrain_params.robot_radius, |
| 342 | 0, 0, 0 |
| 343 | ]]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 344 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 345 | self.D = numpy.matrix([[0, 0], [0, 0], [0, 0]]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 346 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 347 | q_pos = 0.05 |
| 348 | q_vel = 1.00 |
| 349 | q_encoder_uncertainty = 2.00 |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 350 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 351 | self.Q = numpy.matrix( |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 352 | [[(q_pos**2.0), 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], |
| 353 | [0.0, (q_vel**2.0), 0.0, 0.0, 0.0, 0.0, 0.0], |
| 354 | [0.0, 0.0, (q_pos**2.0), 0.0, 0.0, 0.0, 0.0], |
| 355 | [0.0, 0.0, 0.0, (q_vel**2.0), 0.0, 0.0, 0.0], |
| 356 | [0.0, 0.0, 0.0, 0.0, (q_voltage**2.0), 0.0, 0.0], |
| 357 | [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] | 358 | [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] | 359 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 360 | r_pos = 0.0001 |
| 361 | r_gyro = 0.000001 |
| 362 | if self.has_imu: |
| 363 | r_accelerometer = 7.0 |
| 364 | self.R = numpy.matrix([[(r_pos**2.0), 0.0, 0.0, 0.0], |
| 365 | [0.0, (r_pos**2.0), 0.0, 0.0], |
| 366 | [0.0, 0.0, (r_gyro**2.0), 0.0], |
| 367 | [0.0, 0.0, 0.0, (r_accelerometer**2.0)]]) |
| 368 | else: |
| 369 | self.R = numpy.matrix([[(r_pos**2.0), 0.0, 0.0], |
| 370 | [0.0, (r_pos**2.0), 0.0], |
| 371 | [0.0, 0.0, (r_gyro**2.0)]]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 372 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 373 | # Solving for kf gains. |
| 374 | self.KalmanGain, self.Q_steady = controls.kalman( |
| 375 | A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 376 | |
James Kuszmaul | 4d752d5 | 2019-02-09 17:27:55 -0800 | [diff] [blame] | 377 | # If we don't have an IMU, pad various matrices with zeros so that |
| 378 | # we can still have 4 measurement outputs. |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 379 | if not self.has_imu: |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 380 | self.KalmanGain = numpy.hstack((self.KalmanGain, |
| 381 | numpy.matrix(numpy.zeros((7, 1))))) |
Austin Schuh | ecc92a0 | 2019-01-20 17:42:19 -0800 | [diff] [blame] | 382 | self.C = numpy.vstack((self.C, numpy.matrix(numpy.zeros((1, 7))))) |
| 383 | self.D = numpy.vstack((self.D, numpy.matrix(numpy.zeros((1, 2))))) |
James Kuszmaul | 4d752d5 | 2019-02-09 17:27:55 -0800 | [diff] [blame] | 384 | Rtmp = numpy.zeros((4, 4)) |
| 385 | Rtmp[0:3, 0:3] = self.R |
| 386 | self.R = Rtmp |
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 | self.L = self.A * self.KalmanGain |
| 389 | |
| 390 | unaug_K = self.K |
| 391 | |
| 392 | # Implement a nice closed loop controller for use by the closed loop |
| 393 | # controller. |
| 394 | self.K = numpy.matrix(numpy.zeros((self.B.shape[1], self.A.shape[0]))) |
| 395 | self.K[0:2, 0:4] = unaug_K |
| 396 | if self.force: |
| 397 | self.K[0, 4] = 1.0 / self.mpl |
| 398 | self.K[1, 5] = 1.0 / self.mpr |
| 399 | else: |
| 400 | self.K[0, 4] = 1.0 |
| 401 | self.K[1, 5] = 1.0 |
| 402 | |
| 403 | self.Qff = numpy.matrix(numpy.zeros((4, 4))) |
| 404 | qff_pos = 0.005 |
| 405 | qff_vel = 1.00 |
| 406 | self.Qff[0, 0] = 1.0 / qff_pos**2.0 |
| 407 | self.Qff[1, 1] = 1.0 / qff_vel**2.0 |
| 408 | self.Qff[2, 2] = 1.0 / qff_pos**2.0 |
| 409 | self.Qff[3, 3] = 1.0 / qff_vel**2.0 |
| 410 | self.Kff = numpy.matrix(numpy.zeros((2, 7))) |
| 411 | self.Kff[0:2, 0:4] = controls.TwoStateFeedForwards( |
| 412 | self.B[0:4, :], self.Qff) |
| 413 | |
| 414 | self.InitializeState() |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 415 | |
| 416 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 417 | def WriteDrivetrain(drivetrain_files, |
| 418 | kf_drivetrain_files, |
| 419 | year_namespace, |
| 420 | drivetrain_params, |
| 421 | scalar_type='double'): |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 422 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 423 | # Write the generated constants out to a file. |
| 424 | drivetrain_low_low = Drivetrain( |
| 425 | name="DrivetrainLowLow", |
| 426 | left_low=True, |
| 427 | right_low=True, |
| 428 | drivetrain_params=drivetrain_params) |
| 429 | drivetrain_low_high = Drivetrain( |
| 430 | name="DrivetrainLowHigh", |
| 431 | left_low=True, |
| 432 | right_low=False, |
| 433 | drivetrain_params=drivetrain_params) |
| 434 | drivetrain_high_low = Drivetrain( |
| 435 | name="DrivetrainHighLow", |
| 436 | left_low=False, |
| 437 | right_low=True, |
| 438 | drivetrain_params=drivetrain_params) |
| 439 | drivetrain_high_high = Drivetrain( |
| 440 | name="DrivetrainHighHigh", |
| 441 | left_low=False, |
| 442 | right_low=False, |
| 443 | drivetrain_params=drivetrain_params) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 444 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 445 | kf_drivetrain_low_low = KFDrivetrain( |
| 446 | name="KFDrivetrainLowLow", |
| 447 | left_low=True, |
| 448 | right_low=True, |
| 449 | drivetrain_params=drivetrain_params) |
| 450 | kf_drivetrain_low_high = KFDrivetrain( |
| 451 | name="KFDrivetrainLowHigh", |
| 452 | left_low=True, |
| 453 | right_low=False, |
| 454 | drivetrain_params=drivetrain_params) |
| 455 | kf_drivetrain_high_low = KFDrivetrain( |
| 456 | name="KFDrivetrainHighLow", |
| 457 | left_low=False, |
| 458 | right_low=True, |
| 459 | drivetrain_params=drivetrain_params) |
| 460 | kf_drivetrain_high_high = KFDrivetrain( |
| 461 | name="KFDrivetrainHighHigh", |
| 462 | left_low=False, |
| 463 | right_low=False, |
| 464 | drivetrain_params=drivetrain_params) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 465 | |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 466 | if isinstance(year_namespace, list): |
Austin Schuh | ecc92a0 | 2019-01-20 17:42:19 -0800 | [diff] [blame] | 467 | namespaces = year_namespace |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 468 | else: |
Austin Schuh | ecc92a0 | 2019-01-20 17:42:19 -0800 | [diff] [blame] | 469 | namespaces = [year_namespace, 'control_loops', 'drivetrain'] |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 470 | dog_loop_writer = control_loop.ControlLoopWriter( |
| 471 | "Drivetrain", [ |
| 472 | drivetrain_low_low, drivetrain_low_high, drivetrain_high_low, |
| 473 | drivetrain_high_high |
| 474 | ], |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 475 | namespaces=namespaces, |
| 476 | scalar_type=scalar_type) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 477 | dog_loop_writer.AddConstant( |
| 478 | control_loop.Constant("kDt", "%f", drivetrain_low_low.dt)) |
| 479 | dog_loop_writer.AddConstant( |
| 480 | control_loop.Constant("kStallTorque", "%f", |
| 481 | drivetrain_low_low.stall_torque)) |
| 482 | dog_loop_writer.AddConstant( |
| 483 | control_loop.Constant("kStallCurrent", "%f", |
| 484 | drivetrain_low_low.stall_current)) |
| 485 | dog_loop_writer.AddConstant( |
| 486 | control_loop.Constant("kFreeSpeed", "%f", |
| 487 | drivetrain_low_low.free_speed)) |
| 488 | dog_loop_writer.AddConstant( |
| 489 | control_loop.Constant("kFreeCurrent", "%f", |
| 490 | drivetrain_low_low.free_current)) |
| 491 | dog_loop_writer.AddConstant( |
| 492 | control_loop.Constant("kJ", "%f", drivetrain_low_low.J)) |
| 493 | dog_loop_writer.AddConstant( |
| 494 | control_loop.Constant("kMass", "%f", drivetrain_low_low.mass)) |
| 495 | dog_loop_writer.AddConstant( |
| 496 | control_loop.Constant("kRobotRadius", "%f", |
| 497 | drivetrain_low_low.robot_radius)) |
| 498 | dog_loop_writer.AddConstant( |
| 499 | control_loop.Constant("kWheelRadius", "%f", drivetrain_low_low.r)) |
| 500 | dog_loop_writer.AddConstant( |
| 501 | control_loop.Constant("kR", "%f", drivetrain_low_low.resistance)) |
| 502 | dog_loop_writer.AddConstant( |
| 503 | control_loop.Constant("kV", "%f", drivetrain_low_low.Kv)) |
| 504 | dog_loop_writer.AddConstant( |
| 505 | control_loop.Constant("kT", "%f", drivetrain_low_low.Kt)) |
| 506 | dog_loop_writer.AddConstant( |
| 507 | control_loop.Constant("kLowGearRatio", "%f", drivetrain_low_low.G_low)) |
| 508 | dog_loop_writer.AddConstant( |
| 509 | control_loop.Constant("kHighGearRatio", "%f", |
| 510 | drivetrain_high_high.G_high)) |
| 511 | dog_loop_writer.AddConstant( |
| 512 | control_loop.Constant( |
| 513 | "kHighOutputRatio", "%f", |
| 514 | drivetrain_high_high.G_high * drivetrain_high_high.r)) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 515 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 516 | dog_loop_writer.Write(drivetrain_files[0], drivetrain_files[1]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 517 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 518 | kf_loop_writer = control_loop.ControlLoopWriter( |
| 519 | "KFDrivetrain", [ |
| 520 | kf_drivetrain_low_low, kf_drivetrain_low_high, |
| 521 | kf_drivetrain_high_low, kf_drivetrain_high_high |
| 522 | ], |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 523 | namespaces=namespaces, |
| 524 | scalar_type=scalar_type) |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 525 | kf_loop_writer.Write(kf_drivetrain_files[0], kf_drivetrain_files[1]) |
| 526 | |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 527 | |
| 528 | def PlotDrivetrainMotions(drivetrain_params): |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 529 | # Simulate the response of the system to a step input. |
| 530 | drivetrain = Drivetrain( |
| 531 | left_low=False, right_low=False, drivetrain_params=drivetrain_params) |
| 532 | simulated_left = [] |
| 533 | simulated_right = [] |
| 534 | for _ in xrange(100): |
| 535 | drivetrain.Update(numpy.matrix([[12.0], [12.0]])) |
| 536 | simulated_left.append(drivetrain.X[0, 0]) |
| 537 | simulated_right.append(drivetrain.X[2, 0]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 538 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 539 | pylab.rc('lines', linewidth=4) |
| 540 | pylab.plot(range(100), simulated_left, label='left position') |
| 541 | pylab.plot(range(100), simulated_right, 'r--', label='right position') |
| 542 | pylab.suptitle('Acceleration Test\n12 Volt Step Input') |
| 543 | pylab.legend(loc='lower right') |
| 544 | pylab.show() |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 545 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 546 | # Simulate forwards motion. |
| 547 | drivetrain = Drivetrain( |
| 548 | left_low=False, right_low=False, drivetrain_params=drivetrain_params) |
| 549 | close_loop_left = [] |
| 550 | close_loop_right = [] |
| 551 | left_power = [] |
| 552 | right_power = [] |
| 553 | R = numpy.matrix([[1.0], [0.0], [1.0], [0.0]]) |
| 554 | for _ in xrange(300): |
| 555 | U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min, |
| 556 | drivetrain.U_max) |
| 557 | drivetrain.UpdateObserver(U) |
| 558 | drivetrain.Update(U) |
| 559 | close_loop_left.append(drivetrain.X[0, 0]) |
| 560 | close_loop_right.append(drivetrain.X[2, 0]) |
| 561 | left_power.append(U[0, 0]) |
| 562 | right_power.append(U[1, 0]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 563 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 564 | pylab.plot(range(300), close_loop_left, label='left position') |
| 565 | pylab.plot(range(300), close_loop_right, 'm--', label='right position') |
| 566 | pylab.plot(range(300), left_power, label='left power') |
| 567 | pylab.plot(range(300), right_power, '--', label='right power') |
| 568 | pylab.suptitle('Linear Move\nLeft and Right Position going to 1') |
| 569 | pylab.legend() |
| 570 | pylab.show() |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 571 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 572 | # Try turning in place |
| 573 | drivetrain = Drivetrain(drivetrain_params=drivetrain_params) |
| 574 | close_loop_left = [] |
| 575 | close_loop_right = [] |
| 576 | R = numpy.matrix([[-1.0], [0.0], [1.0], [0.0]]) |
| 577 | for _ in xrange(200): |
| 578 | U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min, |
| 579 | drivetrain.U_max) |
| 580 | drivetrain.UpdateObserver(U) |
| 581 | drivetrain.Update(U) |
| 582 | close_loop_left.append(drivetrain.X[0, 0]) |
| 583 | close_loop_right.append(drivetrain.X[2, 0]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 584 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 585 | pylab.plot(range(200), close_loop_left, label='left position') |
| 586 | pylab.plot(range(200), close_loop_right, label='right position') |
| 587 | pylab.suptitle( |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 588 | 'Angular Move\nLeft position going to -1 and right position going to 1') |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 589 | pylab.legend(loc='center right') |
| 590 | pylab.show() |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 591 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 592 | # Try turning just one side. |
| 593 | drivetrain = Drivetrain(drivetrain_params=drivetrain_params) |
| 594 | close_loop_left = [] |
| 595 | close_loop_right = [] |
| 596 | R = numpy.matrix([[0.0], [0.0], [1.0], [0.0]]) |
| 597 | for _ in xrange(300): |
| 598 | U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), drivetrain.U_min, |
| 599 | drivetrain.U_max) |
| 600 | drivetrain.UpdateObserver(U) |
| 601 | drivetrain.Update(U) |
| 602 | close_loop_left.append(drivetrain.X[0, 0]) |
| 603 | close_loop_right.append(drivetrain.X[2, 0]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 604 | |
Diana Burgess | d0180f1 | 2018-03-21 21:24:17 -0700 | [diff] [blame] | 605 | pylab.plot(range(300), close_loop_left, label='left position') |
| 606 | pylab.plot(range(300), close_loop_right, label='right position') |
| 607 | pylab.suptitle( |
| 608 | 'Pivot\nLeft position not changing and right position going to 1') |
| 609 | pylab.legend(loc='center right') |
| 610 | pylab.show() |