Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import numpy |
| 4 | from frc971.control_loops.python import polytope |
| 5 | import frc971.control_loops.python.drivetrain |
| 6 | from frc971.control_loops.python import control_loop |
| 7 | from frc971.control_loops.python import controls |
| 8 | from frc971.control_loops.python.cim import CIM |
| 9 | from matplotlib import pylab |
| 10 | |
| 11 | import glog |
| 12 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 13 | |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 14 | def CoerceGoal(region, K, w, R): |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 15 | """Intersects a line with a region, and finds the closest point to R. |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 16 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 17 | Finds a point that is closest to R inside the region, and on the line |
| 18 | defined by K X = w. If it is not possible to find a point on the line, |
| 19 | finds a point that is inside the region and closest to the line. This |
| 20 | function assumes that |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 21 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 22 | Args: |
| 23 | region: HPolytope, the valid goal region. |
| 24 | K: numpy.matrix (2 x 1), the matrix for the equation [K1, K2] [x1; x2] = w |
| 25 | w: float, the offset in the equation above. |
| 26 | R: numpy.matrix (2 x 1), the point to be closest to. |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 27 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 28 | Returns: |
| 29 | numpy.matrix (2 x 1), the point. |
| 30 | """ |
| 31 | return DoCoerceGoal(region, K, w, R)[0] |
| 32 | |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 33 | |
| 34 | def DoCoerceGoal(region, K, w, R): |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 35 | if region.IsInside(R): |
| 36 | return (R, True) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 37 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 38 | perpendicular_vector = K.T / numpy.linalg.norm(K) |
| 39 | parallel_vector = numpy.matrix([[perpendicular_vector[1, 0]], |
| 40 | [-perpendicular_vector[0, 0]]]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 41 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 42 | # We want to impose the constraint K * X = w on the polytope H * X <= k. |
| 43 | # We do this by breaking X up into parallel and perpendicular components to |
| 44 | # the half plane. This gives us the following equation. |
| 45 | # |
| 46 | # parallel * (parallel.T \dot X) + perpendicular * (perpendicular \dot X)) = X |
| 47 | # |
| 48 | # Then, substitute this into the polytope. |
| 49 | # |
| 50 | # H * (parallel * (parallel.T \dot X) + perpendicular * (perpendicular \dot X)) <= k |
| 51 | # |
| 52 | # Substitute K * X = w |
| 53 | # |
| 54 | # H * parallel * (parallel.T \dot X) + H * perpendicular * w <= k |
| 55 | # |
| 56 | # Move all the knowns to the right side. |
| 57 | # |
| 58 | # H * parallel * ([parallel1 parallel2] * X) <= k - H * perpendicular * w |
| 59 | # |
| 60 | # Let t = parallel.T \dot X, the component parallel to the surface. |
| 61 | # |
| 62 | # H * parallel * t <= k - H * perpendicular * w |
| 63 | # |
| 64 | # This is a polytope which we can solve, and use to figure out the range of X |
| 65 | # that we care about! |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 66 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 67 | t_poly = polytope.HPolytope(region.H * parallel_vector, |
| 68 | region.k - region.H * perpendicular_vector * w) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 69 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 70 | vertices = t_poly.Vertices() |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 71 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 72 | if vertices.shape[0]: |
| 73 | # The region exists! |
| 74 | # Find the closest vertex |
| 75 | min_distance = numpy.infty |
| 76 | closest_point = None |
| 77 | for vertex in vertices: |
| 78 | point = parallel_vector * vertex + perpendicular_vector * w |
| 79 | length = numpy.linalg.norm(R - point) |
| 80 | if length < min_distance: |
| 81 | min_distance = length |
| 82 | closest_point = point |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 83 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 84 | return (closest_point, True) |
| 85 | else: |
| 86 | # Find the vertex of the space that is closest to the line. |
| 87 | region_vertices = region.Vertices() |
| 88 | min_distance = numpy.infty |
| 89 | closest_point = None |
| 90 | for vertex in region_vertices: |
| 91 | point = vertex.T |
| 92 | length = numpy.abs((perpendicular_vector.T * point)[0, 0]) |
| 93 | if length < min_distance: |
| 94 | min_distance = length |
| 95 | closest_point = point |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 96 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 97 | return (closest_point, False) |
| 98 | |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 99 | |
| 100 | class VelocityDrivetrainModel(control_loop.ControlLoop): |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 101 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 102 | def __init__(self, |
| 103 | drivetrain_params, |
| 104 | left_low=True, |
| 105 | right_low=True, |
| 106 | name="VelocityDrivetrainModel"): |
| 107 | super(VelocityDrivetrainModel, self).__init__(name) |
| 108 | self._drivetrain = frc971.control_loops.python.drivetrain.Drivetrain( |
| 109 | left_low=left_low, |
| 110 | right_low=right_low, |
| 111 | drivetrain_params=drivetrain_params) |
| 112 | self.dt = drivetrain_params.dt |
| 113 | self.A_continuous = numpy.matrix( |
| 114 | [[ |
| 115 | self._drivetrain.A_continuous[1, 1], |
| 116 | self._drivetrain.A_continuous[1, 3] |
| 117 | ], |
| 118 | [ |
| 119 | self._drivetrain.A_continuous[3, 1], |
| 120 | self._drivetrain.A_continuous[3, 3] |
| 121 | ]]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 122 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 123 | self.B_continuous = numpy.matrix( |
| 124 | [[ |
| 125 | self._drivetrain.B_continuous[1, 0], |
| 126 | self._drivetrain.B_continuous[1, 1] |
| 127 | ], |
| 128 | [ |
| 129 | self._drivetrain.B_continuous[3, 0], |
| 130 | self._drivetrain.B_continuous[3, 1] |
| 131 | ]]) |
| 132 | self.C = numpy.matrix(numpy.eye(2)) |
| 133 | self.D = numpy.matrix(numpy.zeros((2, 2))) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 134 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 135 | self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, |
| 136 | self.B_continuous, self.dt) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 137 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 138 | # FF * X = U (steady state) |
| 139 | self.FF = self.B.I * (numpy.eye(2) - self.A) |
Austin Schuh | 7442515 | 2018-12-21 11:37:14 +1100 | [diff] [blame] | 140 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 141 | self.PlaceControllerPoles(drivetrain_params.controller_poles) |
| 142 | # Build a kalman filter for the velocity. We don't care what the gains |
| 143 | # are, but the hybrid kalman filter that we want to write to disk to get |
| 144 | # access to A_continuous and B_continuous needs this for completeness. |
| 145 | self.Q_continuous = numpy.matrix([[(0.5**2.0), 0.0], [0.0, (0.5**2.0)]]) |
| 146 | self.R_continuous = numpy.matrix([[(0.1**2.0), 0.0], [0.0, (0.1**2.0)]]) |
| 147 | self.PlaceObserverPoles(drivetrain_params.observer_poles) |
| 148 | _, _, self.Q, self.R = controls.kalmd( |
| 149 | A_continuous=self.A_continuous, |
| 150 | B_continuous=self.B_continuous, |
| 151 | Q_continuous=self.Q_continuous, |
| 152 | R_continuous=self.R_continuous, |
| 153 | dt=self.dt) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 154 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 155 | self.KalmanGain, self.P_steady_state = controls.kalman( |
| 156 | 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] | 157 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 158 | self.G_high = self._drivetrain.G_high |
| 159 | self.G_low = self._drivetrain.G_low |
| 160 | self.resistance = self._drivetrain.resistance |
| 161 | self.r = self._drivetrain.r |
| 162 | self.Kv = self._drivetrain.Kv |
| 163 | self.Kt = self._drivetrain.Kt |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 164 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 165 | self.U_max = self._drivetrain.U_max |
| 166 | self.U_min = self._drivetrain.U_min |
| 167 | |
| 168 | @property |
| 169 | def robot_radius_l(self): |
| 170 | return self._drivetrain.robot_radius_l |
| 171 | |
| 172 | @property |
| 173 | def robot_radius_r(self): |
| 174 | return self._drivetrain.robot_radius_r |
| 175 | |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 176 | |
| 177 | class VelocityDrivetrain(object): |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 178 | HIGH = 'high' |
| 179 | LOW = 'low' |
| 180 | SHIFTING_UP = 'up' |
| 181 | SHIFTING_DOWN = 'down' |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 182 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 183 | def __init__(self, drivetrain_params, name='VelocityDrivetrain'): |
| 184 | self.drivetrain_low_low = VelocityDrivetrainModel( |
| 185 | left_low=True, |
| 186 | right_low=True, |
| 187 | name=name + 'LowLow', |
| 188 | drivetrain_params=drivetrain_params) |
| 189 | self.drivetrain_low_high = VelocityDrivetrainModel( |
| 190 | left_low=True, |
| 191 | right_low=False, |
| 192 | name=name + 'LowHigh', |
| 193 | drivetrain_params=drivetrain_params) |
| 194 | self.drivetrain_high_low = VelocityDrivetrainModel( |
| 195 | left_low=False, |
| 196 | right_low=True, |
| 197 | name=name + 'HighLow', |
| 198 | drivetrain_params=drivetrain_params) |
| 199 | self.drivetrain_high_high = VelocityDrivetrainModel( |
| 200 | left_low=False, |
| 201 | right_low=False, |
| 202 | name=name + 'HighHigh', |
| 203 | drivetrain_params=drivetrain_params) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 204 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 205 | # X is [lvel, rvel] |
| 206 | self.X = numpy.matrix([[0.0], [0.0]]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 207 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 208 | self.U_poly = polytope.HPolytope( |
| 209 | numpy.matrix([[1, 0], [-1, 0], [0, 1], [0, -1]]), |
| 210 | numpy.matrix([[12], [12], [12], [12]])) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 211 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 212 | self.U_max = numpy.matrix([[12.0], [12.0]]) |
| 213 | self.U_min = numpy.matrix([[-12.0000000000], [-12.0000000000]]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 214 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 215 | self.dt = 0.00505 |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 216 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 217 | self.R = numpy.matrix([[0.0], [0.0]]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 218 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 219 | self.U_ideal = numpy.matrix([[0.0], [0.0]]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 220 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 221 | # ttrust is the comprimise between having full throttle negative inertia, |
| 222 | # and having no throttle negative inertia. A value of 0 is full throttle |
| 223 | # inertia. A value of 1 is no throttle negative inertia. |
| 224 | self.ttrust = 1.0 |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 225 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 226 | self.left_gear = VelocityDrivetrain.LOW |
| 227 | self.right_gear = VelocityDrivetrain.LOW |
| 228 | self.left_shifter_position = 0.0 |
| 229 | self.right_shifter_position = 0.0 |
| 230 | self.left_cim = CIM() |
| 231 | self.right_cim = CIM() |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 232 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 233 | def IsInGear(self, gear): |
| 234 | return gear is VelocityDrivetrain.HIGH or gear is VelocityDrivetrain.LOW |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 235 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 236 | def MotorRPM(self, shifter_position, velocity): |
| 237 | if shifter_position > 0.5: |
| 238 | return (velocity / self.CurrentDrivetrain().G_high / |
| 239 | self.CurrentDrivetrain().r) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 240 | else: |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 241 | return (velocity / self.CurrentDrivetrain().G_low / |
| 242 | self.CurrentDrivetrain().r) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 243 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 244 | def CurrentDrivetrain(self): |
| 245 | if self.left_shifter_position > 0.5: |
| 246 | if self.right_shifter_position > 0.5: |
| 247 | return self.drivetrain_high_high |
| 248 | else: |
| 249 | return self.drivetrain_high_low |
| 250 | else: |
| 251 | if self.right_shifter_position > 0.5: |
| 252 | return self.drivetrain_low_high |
| 253 | else: |
| 254 | return self.drivetrain_low_low |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 255 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 256 | def SimShifter(self, gear, shifter_position): |
| 257 | if gear is VelocityDrivetrain.HIGH or gear is VelocityDrivetrain.SHIFTING_UP: |
| 258 | shifter_position = min(shifter_position + 0.5, 1.0) |
| 259 | else: |
| 260 | shifter_position = max(shifter_position - 0.5, 0.0) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 261 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 262 | if shifter_position == 1.0: |
| 263 | gear = VelocityDrivetrain.HIGH |
| 264 | elif shifter_position == 0.0: |
| 265 | gear = VelocityDrivetrain.LOW |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 266 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 267 | return gear, shifter_position |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 268 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 269 | def ComputeGear(self, |
| 270 | wheel_velocity, |
| 271 | should_print=False, |
| 272 | current_gear=False, |
| 273 | gear_name=None): |
| 274 | high_omega = (wheel_velocity / self.CurrentDrivetrain().G_high / |
| 275 | self.CurrentDrivetrain().r) |
| 276 | low_omega = (wheel_velocity / self.CurrentDrivetrain().G_low / |
| 277 | self.CurrentDrivetrain().r) |
| 278 | #print gear_name, "Motor Energy Difference.", 0.5 * 0.000140032647 * (low_omega * low_omega - high_omega * high_omega), "joules" |
| 279 | high_torque = ( |
| 280 | (12.0 - high_omega / self.CurrentDrivetrain().Kv) * |
| 281 | self.CurrentDrivetrain().Kt / self.CurrentDrivetrain().resistance) |
| 282 | low_torque = ( |
| 283 | (12.0 - low_omega / self.CurrentDrivetrain().Kv) * |
| 284 | self.CurrentDrivetrain().Kt / self.CurrentDrivetrain().resistance) |
| 285 | high_power = high_torque * high_omega |
| 286 | low_power = low_torque * low_omega |
| 287 | #if should_print: |
| 288 | # print gear_name, "High omega", high_omega, "Low omega", low_omega |
| 289 | # print gear_name, "High torque", high_torque, "Low torque", low_torque |
| 290 | # print gear_name, "High power", high_power, "Low power", low_power |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 291 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 292 | # Shift algorithm improvements. |
| 293 | # TODO(aschuh): |
| 294 | # It takes time to shift. Shifting down for 1 cycle doesn't make sense |
| 295 | # because you will end up slower than without shifting. Figure out how |
| 296 | # to include that info. |
| 297 | # If the driver is still in high gear, but isn't asking for the extra power |
| 298 | # from low gear, don't shift until he asks for it. |
| 299 | goal_gear_is_high = high_power > low_power |
| 300 | #goal_gear_is_high = True |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 301 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 302 | if not self.IsInGear(current_gear): |
| 303 | glog.debug('%s Not in gear.', gear_name) |
| 304 | return current_gear |
| 305 | else: |
| 306 | is_high = current_gear is VelocityDrivetrain.HIGH |
| 307 | if is_high != goal_gear_is_high: |
| 308 | if goal_gear_is_high: |
| 309 | glog.debug('%s Shifting up.', gear_name) |
| 310 | return VelocityDrivetrain.SHIFTING_UP |
| 311 | else: |
| 312 | glog.debug('%s Shifting down.', gear_name) |
| 313 | return VelocityDrivetrain.SHIFTING_DOWN |
| 314 | else: |
| 315 | return current_gear |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 316 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 317 | def FilterVelocity(self, throttle): |
| 318 | # Invert the plant to figure out how the velocity filter would have to work |
| 319 | # out in order to filter out the forwards negative inertia. |
| 320 | # This math assumes that the left and right power and velocity are equal. |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 321 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 322 | # The throttle filter should filter such that the motor in the highest gear |
| 323 | # should be controlling the time constant. |
| 324 | # Do this by finding the index of FF that has the lowest value, and computing |
| 325 | # the sums using that index. |
| 326 | FF_sum = self.CurrentDrivetrain().FF.sum(axis=1) |
| 327 | min_FF_sum_index = numpy.argmin(FF_sum) |
| 328 | min_FF_sum = FF_sum[min_FF_sum_index, 0] |
| 329 | min_K_sum = self.CurrentDrivetrain().K[min_FF_sum_index, :].sum() |
| 330 | # Compute the FF sum for high gear. |
| 331 | high_min_FF_sum = self.drivetrain_high_high.FF[0, :].sum() |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 332 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 333 | # U = self.K[0, :].sum() * (R - x_avg) + self.FF[0, :].sum() * R |
| 334 | # throttle * 12.0 = (self.K[0, :].sum() + self.FF[0, :].sum()) * R |
| 335 | # - self.K[0, :].sum() * x_avg |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 336 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 337 | # R = (throttle * 12.0 + self.K[0, :].sum() * x_avg) / |
| 338 | # (self.K[0, :].sum() + self.FF[0, :].sum()) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 339 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 340 | # U = (K + FF) * R - K * X |
| 341 | # (K + FF) ^-1 * (U + K * X) = R |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 342 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 343 | # Scale throttle by min_FF_sum / high_min_FF_sum. This will make low gear |
| 344 | # have the same velocity goal as high gear, and so that the robot will hold |
| 345 | # the same speed for the same throttle for all gears. |
| 346 | adjusted_ff_voltage = numpy.clip( |
| 347 | throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0) |
| 348 | return ((adjusted_ff_voltage + self.ttrust * min_K_sum * |
| 349 | (self.X[0, 0] + self.X[1, 0]) / 2.0) / |
| 350 | (self.ttrust * min_K_sum + min_FF_sum)) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 351 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 352 | def Update(self, throttle, steering): |
| 353 | # Shift into the gear which sends the most power to the floor. |
| 354 | # This is the same as sending the most torque down to the floor at the |
| 355 | # wheel. |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 356 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 357 | self.left_gear = self.right_gear = True |
| 358 | if True: |
| 359 | self.left_gear = self.ComputeGear( |
| 360 | self.X[0, 0], |
| 361 | should_print=True, |
| 362 | current_gear=self.left_gear, |
| 363 | gear_name="left") |
| 364 | self.right_gear = self.ComputeGear( |
| 365 | self.X[1, 0], |
| 366 | should_print=True, |
| 367 | current_gear=self.right_gear, |
| 368 | gear_name="right") |
| 369 | if self.IsInGear(self.left_gear): |
| 370 | self.left_cim.X[0, 0] = self.MotorRPM( |
| 371 | self.left_shifter_position, self.X[0, 0]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 372 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 373 | if self.IsInGear(self.right_gear): |
| 374 | self.right_cim.X[0, 0] = self.MotorRPM( |
| 375 | self.right_shifter_position, self.X[0, 0]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 376 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 377 | if self.IsInGear(self.left_gear) and self.IsInGear(self.right_gear): |
| 378 | # Filter the throttle to provide a nicer response. |
| 379 | fvel = self.FilterVelocity(throttle) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 380 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 381 | # Constant radius means that angualar_velocity / linear_velocity = constant. |
| 382 | # Compute the left and right velocities. |
| 383 | steering_velocity = numpy.abs(fvel) * steering |
| 384 | left_velocity = fvel - steering_velocity |
| 385 | right_velocity = fvel + steering_velocity |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 386 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 387 | # Write this constraint in the form of K * R = w |
| 388 | # angular velocity / linear velocity = constant |
| 389 | # (left - right) / (left + right) = constant |
| 390 | # left - right = constant * left + constant * right |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 391 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 392 | # (fvel - steering * numpy.abs(fvel) - fvel - steering * numpy.abs(fvel)) / |
| 393 | # (fvel - steering * numpy.abs(fvel) + fvel + steering * numpy.abs(fvel)) = |
| 394 | # constant |
| 395 | # (- 2 * steering * numpy.abs(fvel)) / (2 * fvel) = constant |
| 396 | # (-steering * sign(fvel)) = constant |
| 397 | # (-steering * sign(fvel)) * (left + right) = left - right |
| 398 | # (steering * sign(fvel) + 1) * left + (steering * sign(fvel) - 1) * right = 0 |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 399 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 400 | equality_k = numpy.matrix([[ |
| 401 | 1 + steering * numpy.sign(fvel), |
| 402 | -(1 - steering * numpy.sign(fvel)) |
| 403 | ]]) |
| 404 | equality_w = 0.0 |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 405 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 406 | self.R[0, 0] = left_velocity |
| 407 | self.R[1, 0] = right_velocity |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 408 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 409 | # Construct a constraint on R by manipulating the constraint on U |
| 410 | # Start out with H * U <= k |
| 411 | # U = FF * R + K * (R - X) |
| 412 | # H * (FF * R + K * R - K * X) <= k |
| 413 | # H * (FF + K) * R <= k + H * K * X |
| 414 | R_poly = polytope.HPolytope( |
| 415 | self.U_poly.H * |
| 416 | (self.CurrentDrivetrain().K + self.CurrentDrivetrain().FF), |
| 417 | self.U_poly.k + |
| 418 | self.U_poly.H * self.CurrentDrivetrain().K * self.X) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 419 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 420 | # Limit R back inside the box. |
| 421 | self.boxed_R = CoerceGoal(R_poly, equality_k, equality_w, self.R) |
| 422 | |
| 423 | FF_volts = self.CurrentDrivetrain().FF * self.boxed_R |
| 424 | self.U_ideal = self.CurrentDrivetrain().K * ( |
| 425 | self.boxed_R - self.X) + FF_volts |
| 426 | else: |
| 427 | glog.debug('Not all in gear') |
| 428 | if not self.IsInGear(self.left_gear) and not self.IsInGear( |
| 429 | self.right_gear): |
| 430 | # TODO(austin): Use battery volts here. |
| 431 | R_left = self.MotorRPM(self.left_shifter_position, self.X[0, 0]) |
| 432 | self.U_ideal[0, 0] = numpy.clip( |
| 433 | self.left_cim.K * (R_left - self.left_cim.X) + |
| 434 | R_left / self.left_cim.Kv, self.left_cim.U_min, |
| 435 | self.left_cim.U_max) |
| 436 | self.left_cim.Update(self.U_ideal[0, 0]) |
| 437 | |
| 438 | R_right = self.MotorRPM(self.right_shifter_position, |
| 439 | self.X[1, 0]) |
| 440 | self.U_ideal[1, 0] = numpy.clip( |
| 441 | self.right_cim.K * (R_right - self.right_cim.X) + |
| 442 | R_right / self.right_cim.Kv, self.right_cim.U_min, |
| 443 | self.right_cim.U_max) |
| 444 | self.right_cim.Update(self.U_ideal[1, 0]) |
| 445 | else: |
| 446 | assert False |
| 447 | |
| 448 | self.U = numpy.clip(self.U_ideal, self.U_min, self.U_max) |
| 449 | |
| 450 | # TODO(austin): Model the robot as not accelerating when you shift... |
| 451 | # This hack only works when you shift at the same time. |
| 452 | if self.IsInGear(self.left_gear) and self.IsInGear(self.right_gear): |
| 453 | self.X = self.CurrentDrivetrain( |
| 454 | ).A * self.X + self.CurrentDrivetrain().B * self.U |
| 455 | |
| 456 | self.left_gear, self.left_shifter_position = self.SimShifter( |
| 457 | self.left_gear, self.left_shifter_position) |
| 458 | self.right_gear, self.right_shifter_position = self.SimShifter( |
| 459 | self.right_gear, self.right_shifter_position) |
| 460 | |
| 461 | glog.debug('U is %s %s', str(self.U[0, 0]), str(self.U[1, 0])) |
| 462 | glog.debug('Left shifter %s %d Right shifter %s %d', self.left_gear, |
| 463 | self.left_shifter_position, self.right_gear, |
| 464 | self.right_shifter_position) |
| 465 | |
| 466 | |
| 467 | def WritePolyDrivetrain(drivetrain_files, |
| 468 | motor_files, |
| 469 | hybrid_files, |
| 470 | year_namespace, |
| 471 | drivetrain_params, |
Austin Schuh | 7442515 | 2018-12-21 11:37:14 +1100 | [diff] [blame] | 472 | scalar_type='double'): |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 473 | vdrivetrain = VelocityDrivetrain(drivetrain_params) |
| 474 | hybrid_vdrivetrain = VelocityDrivetrain( |
| 475 | drivetrain_params, name="HybridVelocityDrivetrain") |
| 476 | if isinstance(year_namespace, list): |
| 477 | namespaces = year_namespace |
| 478 | else: |
| 479 | namespaces = [year_namespace, 'control_loops', 'drivetrain'] |
| 480 | dog_loop_writer = control_loop.ControlLoopWriter( |
| 481 | "VelocityDrivetrain", [ |
| 482 | vdrivetrain.drivetrain_low_low, vdrivetrain.drivetrain_low_high, |
| 483 | vdrivetrain.drivetrain_high_low, vdrivetrain.drivetrain_high_high |
| 484 | ], |
| 485 | namespaces=namespaces, |
| 486 | scalar_type=scalar_type) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 487 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 488 | dog_loop_writer.Write(drivetrain_files[0], drivetrain_files[1]) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 489 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 490 | hybrid_loop_writer = control_loop.ControlLoopWriter( |
| 491 | "HybridVelocityDrivetrain", [ |
| 492 | hybrid_vdrivetrain.drivetrain_low_low, |
| 493 | hybrid_vdrivetrain.drivetrain_low_high, |
| 494 | hybrid_vdrivetrain.drivetrain_high_low, |
| 495 | hybrid_vdrivetrain.drivetrain_high_high |
| 496 | ], |
| 497 | namespaces=namespaces, |
| 498 | scalar_type=scalar_type, |
| 499 | plant_type='StateFeedbackHybridPlant', |
| 500 | observer_type='HybridKalman') |
Austin Schuh | 7442515 | 2018-12-21 11:37:14 +1100 | [diff] [blame] | 501 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 502 | hybrid_loop_writer.Write(hybrid_files[0], hybrid_files[1]) |
Austin Schuh | 7442515 | 2018-12-21 11:37:14 +1100 | [diff] [blame] | 503 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 504 | cim_writer = control_loop.ControlLoopWriter( |
| 505 | "CIM", [CIM()], scalar_type=scalar_type) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 506 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 507 | cim_writer.Write(motor_files[0], motor_files[1]) |
| 508 | |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 509 | |
| 510 | def PlotPolyDrivetrainMotions(drivetrain_params): |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 511 | vdrivetrain = VelocityDrivetrain(drivetrain_params) |
| 512 | vl_plot = [] |
| 513 | vr_plot = [] |
| 514 | ul_plot = [] |
| 515 | ur_plot = [] |
| 516 | radius_plot = [] |
| 517 | t_plot = [] |
| 518 | left_gear_plot = [] |
| 519 | right_gear_plot = [] |
| 520 | vdrivetrain.left_shifter_position = 0.0 |
| 521 | vdrivetrain.right_shifter_position = 0.0 |
| 522 | vdrivetrain.left_gear = VelocityDrivetrain.LOW |
| 523 | vdrivetrain.right_gear = VelocityDrivetrain.LOW |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 524 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 525 | glog.debug('K is %s', str(vdrivetrain.CurrentDrivetrain().K)) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 526 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 527 | if vdrivetrain.left_gear is VelocityDrivetrain.HIGH: |
| 528 | glog.debug('Left is high') |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 529 | else: |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 530 | glog.debug('Left is low') |
| 531 | if vdrivetrain.right_gear is VelocityDrivetrain.HIGH: |
| 532 | glog.debug('Right is high') |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 533 | else: |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 534 | glog.debug('Right is low') |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 535 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 536 | for t in numpy.arange(0, 1.7, vdrivetrain.dt): |
| 537 | if t < 0.5: |
| 538 | vdrivetrain.Update(throttle=0.00, steering=1.0) |
| 539 | elif t < 1.2: |
| 540 | vdrivetrain.Update(throttle=0.5, steering=1.0) |
| 541 | else: |
| 542 | vdrivetrain.Update(throttle=0.00, steering=1.0) |
| 543 | t_plot.append(t) |
| 544 | vl_plot.append(vdrivetrain.X[0, 0]) |
| 545 | vr_plot.append(vdrivetrain.X[1, 0]) |
| 546 | ul_plot.append(vdrivetrain.U[0, 0]) |
| 547 | ur_plot.append(vdrivetrain.U[1, 0]) |
| 548 | left_gear_plot.append( |
| 549 | (vdrivetrain.left_gear is VelocityDrivetrain.HIGH) * 2.0 - 10.0) |
| 550 | right_gear_plot.append( |
| 551 | (vdrivetrain.right_gear is VelocityDrivetrain.HIGH) * 2.0 - 10.0) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 552 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 553 | fwd_velocity = (vdrivetrain.X[1, 0] + vdrivetrain.X[0, 0]) / 2 |
| 554 | turn_velocity = (vdrivetrain.X[1, 0] - vdrivetrain.X[0, 0]) |
| 555 | if abs(fwd_velocity) < 0.0000001: |
| 556 | radius_plot.append(turn_velocity) |
| 557 | else: |
| 558 | radius_plot.append(turn_velocity / fwd_velocity) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 559 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame^] | 560 | # TODO(austin): |
| 561 | # Shifting compensation. |
| 562 | |
| 563 | # Tighten the turn. |
| 564 | # Closed loop drive. |
| 565 | |
| 566 | pylab.plot(t_plot, vl_plot, label='left velocity') |
| 567 | pylab.plot(t_plot, vr_plot, label='right velocity') |
| 568 | pylab.plot(t_plot, ul_plot, label='left voltage') |
| 569 | pylab.plot(t_plot, ur_plot, label='right voltage') |
| 570 | pylab.plot(t_plot, radius_plot, label='radius') |
| 571 | pylab.plot(t_plot, left_gear_plot, label='left gear high') |
| 572 | pylab.plot(t_plot, right_gear_plot, label='right gear high') |
| 573 | pylab.legend() |
| 574 | pylab.show() |