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