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