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