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 |
| 7 | import controls |
| 8 | from matplotlib import pylab |
| 9 | |
| 10 | __author__ = 'Austin Schuh (austin.linux@gmail.com)' |
| 11 | |
| 12 | |
| 13 | def CoerceGoal(region, K, w, R): |
| 14 | """Intersects a line with a region, and finds the closest point to R. |
| 15 | |
| 16 | Finds a point that is closest to R inside the region, and on the line |
| 17 | defined by K X = w. If it is not possible to find a point on the line, |
| 18 | finds a point that is inside the region and closest to the line. This |
| 19 | function assumes that |
| 20 | |
| 21 | Args: |
| 22 | region: HPolytope, the valid goal region. |
| 23 | K: numpy.matrix (2 x 1), the matrix for the equation [K1, K2] [x1; x2] = w |
| 24 | w: float, the offset in the equation above. |
| 25 | R: numpy.matrix (2 x 1), the point to be closest to. |
| 26 | |
| 27 | Returns: |
| 28 | numpy.matrix (2 x 1), the point. |
| 29 | """ |
| 30 | |
| 31 | if region.IsInside(R): |
| 32 | return R |
| 33 | |
| 34 | perpendicular_vector = K.T / numpy.linalg.norm(K) |
| 35 | parallel_vector = numpy.matrix([[perpendicular_vector[1, 0]], |
| 36 | [-perpendicular_vector[0, 0]]]) |
| 37 | |
| 38 | # We want to impose the constraint K * X = w on the polytope H * X <= k. |
| 39 | # We do this by breaking X up into parallel and perpendicular components to |
| 40 | # the half plane. This gives us the following equation. |
| 41 | # |
| 42 | # parallel * (parallel.T \dot X) + perpendicular * (perpendicular \dot X)) = X |
| 43 | # |
| 44 | # Then, substitute this into the polytope. |
| 45 | # |
| 46 | # H * (parallel * (parallel.T \dot X) + perpendicular * (perpendicular \dot X)) <= k |
| 47 | # |
| 48 | # Substitute K * X = w |
| 49 | # |
| 50 | # H * parallel * (parallel.T \dot X) + H * perpendicular * w <= k |
| 51 | # |
| 52 | # Move all the knowns to the right side. |
| 53 | # |
| 54 | # H * parallel * ([parallel1 parallel2] * X) <= k - H * perpendicular * w |
| 55 | # |
| 56 | # Let t = parallel.T \dot X, the component parallel to the surface. |
| 57 | # |
| 58 | # H * parallel * t <= k - H * perpendicular * w |
| 59 | # |
| 60 | # This is a polytope which we can solve, and use to figure out the range of X |
| 61 | # that we care about! |
| 62 | |
| 63 | t_poly = polytope.HPolytope( |
| 64 | region.H * parallel_vector, |
| 65 | region.k - region.H * perpendicular_vector * w) |
| 66 | |
| 67 | vertices = t_poly.Vertices() |
| 68 | |
| 69 | if vertices.shape[0]: |
| 70 | # The region exists! |
| 71 | # Find the closest vertex |
| 72 | min_distance = numpy.infty |
| 73 | closest_point = None |
| 74 | for vertex in vertices: |
| 75 | point = parallel_vector * vertex + perpendicular_vector * w |
| 76 | length = numpy.linalg.norm(R - point) |
| 77 | if length < min_distance: |
| 78 | min_distance = length |
| 79 | closest_point = point |
| 80 | |
| 81 | return closest_point |
| 82 | else: |
| 83 | # Find the vertex of the space that is closest to the line. |
| 84 | region_vertices = region.Vertices() |
| 85 | min_distance = numpy.infty |
| 86 | closest_point = None |
| 87 | for vertex in region_vertices: |
| 88 | point = vertex.T |
| 89 | length = numpy.abs((perpendicular_vector.T * point)[0, 0]) |
| 90 | if length < min_distance: |
| 91 | min_distance = length |
| 92 | closest_point = point |
| 93 | |
| 94 | return closest_point |
| 95 | |
| 96 | |
Austin Schuh | 03513cb | 2013-10-08 22:29:07 -0700 | [diff] [blame] | 97 | class VelocityDrivetrainModel(object): |
| 98 | def __init__(self, left_low=True, right_low=True): |
| 99 | self._drivetrain = drivetrain.Drivetrain(left_low=left_low, |
| 100 | right_low=right_low) |
| 101 | self.A = numpy.matrix( |
| 102 | [[self._drivetrain.A[1, 1], self._drivetrain.A[1, 3]], |
| 103 | [self._drivetrain.A[3, 1], self._drivetrain.A[3, 3]]]) |
| 104 | |
| 105 | self.B = numpy.matrix( |
| 106 | [[self._drivetrain.B[1, 0], self._drivetrain.B[1, 1]], |
| 107 | [self._drivetrain.B[3, 0], self._drivetrain.B[3, 1]]]) |
| 108 | |
| 109 | # FF * X = U (steady state) |
| 110 | self.FF = self.B.I * (numpy.eye(2) - self.A) |
| 111 | |
| 112 | self.K = controls.dplace(self.A, self.B, [0.3, 0.3]) |
| 113 | |
Austin Schuh | e05d2c1 | 2013-10-12 00:08:31 -0700 | [diff] [blame^] | 114 | self.G_high = self._drivetrain.G_high |
| 115 | self.G_low = self._drivetrain.G_low |
| 116 | self.R = self._drivetrain.R |
| 117 | self.r = self._drivetrain.r |
| 118 | self.Kv = self._drivetrain.Kv |
| 119 | self.Kt = self._drivetrain.Kt |
| 120 | |
Austin Schuh | 03513cb | 2013-10-08 22:29:07 -0700 | [diff] [blame] | 121 | |
Austin Schuh | 048fb60 | 2013-10-07 23:31:04 -0700 | [diff] [blame] | 122 | class VelocityDrivetrain(object): |
| 123 | def __init__(self): |
Austin Schuh | 03513cb | 2013-10-08 22:29:07 -0700 | [diff] [blame] | 124 | self.drivetrain_low_low = VelocityDrivetrainModel(left_low=True, right_low=True) |
| 125 | self.drivetrain_low_high = VelocityDrivetrainModel(left_low=True, right_low=False) |
| 126 | self.drivetrain_high_low = VelocityDrivetrainModel(left_low=False, right_low=True) |
| 127 | self.drivetrain_high_high = VelocityDrivetrainModel(left_low=False, right_low=False) |
Austin Schuh | 048fb60 | 2013-10-07 23:31:04 -0700 | [diff] [blame] | 128 | |
| 129 | # X is [lvel, rvel] |
| 130 | self.X = numpy.matrix( |
| 131 | [[0.0], |
| 132 | [0.0]]) |
| 133 | |
Austin Schuh | 048fb60 | 2013-10-07 23:31:04 -0700 | [diff] [blame] | 134 | self.U_poly = polytope.HPolytope( |
| 135 | numpy.matrix([[1, 0], |
| 136 | [-1, 0], |
| 137 | [0, 1], |
| 138 | [0, -1]]), |
| 139 | numpy.matrix([[12], |
| 140 | [12], |
| 141 | [12], |
| 142 | [12]])) |
| 143 | |
| 144 | self.U_max = numpy.matrix( |
| 145 | [[12.0], |
| 146 | [12.0]]) |
| 147 | self.U_min = numpy.matrix( |
| 148 | [[-12.0000000000], |
| 149 | [-12.0000000000]]) |
| 150 | |
Austin Schuh | 048fb60 | 2013-10-07 23:31:04 -0700 | [diff] [blame] | 151 | self.dt = 0.01 |
| 152 | |
| 153 | self.R = numpy.matrix( |
| 154 | [[0.0], |
| 155 | [0.0]]) |
| 156 | |
Austin Schuh | 048fb60 | 2013-10-07 23:31:04 -0700 | [diff] [blame] | 157 | self.xfiltered = 0.0 |
| 158 | |
Austin Schuh | e05d2c1 | 2013-10-12 00:08:31 -0700 | [diff] [blame^] | 159 | # ttrust is the comprimise between having full throttle negative inertia, |
| 160 | # and having no throttle negative inertia. A value of 0 is full throttle |
| 161 | # inertia. A value of 1 is no throttle negative inertia. |
Austin Schuh | 03513cb | 2013-10-08 22:29:07 -0700 | [diff] [blame] | 162 | self.ttrust = 1.0 |
| 163 | |
| 164 | self.left_high = False |
| 165 | self.right_high = False |
| 166 | |
| 167 | def CurrentDrivetrain(self): |
| 168 | if self.left_high: |
| 169 | if self.right_high: |
| 170 | return self.drivetrain_high_high |
| 171 | else: |
| 172 | return self.drivetrain_high_low |
| 173 | else: |
| 174 | if self.right_high: |
| 175 | return self.drivetrain_low_high |
| 176 | else: |
| 177 | return self.drivetrain_low_low |
Austin Schuh | 048fb60 | 2013-10-07 23:31:04 -0700 | [diff] [blame] | 178 | |
Austin Schuh | e05d2c1 | 2013-10-12 00:08:31 -0700 | [diff] [blame^] | 179 | def ComputeGear(self, wheel_velocity, should_print=False, current_gear=False, gear_name=None): |
| 180 | high_omega = (wheel_velocity / self.CurrentDrivetrain().G_high / |
| 181 | self.CurrentDrivetrain().r) |
| 182 | low_omega = (wheel_velocity / self.CurrentDrivetrain().G_low / |
| 183 | self.CurrentDrivetrain().r) |
| 184 | high_torque = ((12.0 - high_omega / self.CurrentDrivetrain().Kv) * |
| 185 | self.CurrentDrivetrain().Kt / self.CurrentDrivetrain().R) |
| 186 | low_torque = ((12.0 - low_omega / self.CurrentDrivetrain().Kv) * |
| 187 | self.CurrentDrivetrain().Kt / self.CurrentDrivetrain().R) |
| 188 | high_power = high_torque * high_omega |
| 189 | low_power = low_torque * low_omega |
| 190 | if should_print: |
| 191 | print gear_name, "High omega", high_omega, "Low omega", low_omega |
| 192 | print gear_name, "High torque", high_torque, "Low torque", low_torque |
| 193 | print gear_name, "High power", high_power, "Low power", low_power |
| 194 | if (high_power > low_power) != current_gear: |
| 195 | if high_power > low_power: |
| 196 | print gear_name, "Shifting to high" |
| 197 | else: |
| 198 | print gear_name, "Shifting to low" |
| 199 | |
| 200 | return high_power > low_power |
| 201 | |
Austin Schuh | 048fb60 | 2013-10-07 23:31:04 -0700 | [diff] [blame] | 202 | def Update(self, throttle, steering): |
Austin Schuh | e05d2c1 | 2013-10-12 00:08:31 -0700 | [diff] [blame^] | 203 | # Shift into the gear which sends the most power to the floor. |
| 204 | # This is the same as sending the most torque down to the floor at the |
| 205 | # wheel. |
| 206 | |
| 207 | self.left_high = self.ComputeGear(self.X[0, 0], should_print=True, current_gear=self.left_high, gear_name="left") |
| 208 | self.right_high = self.ComputeGear(self.X[1, 0], should_print=True, current_gear=self.right_high, gear_name="right") |
| 209 | |
| 210 | FF_sum = self.CurrentDrivetrain().FF.sum(axis=1) |
| 211 | |
| 212 | # Filter the throttle to provide a nicer response. |
| 213 | |
| 214 | # TODO(austin): fn |
Austin Schuh | 048fb60 | 2013-10-07 23:31:04 -0700 | [diff] [blame] | 215 | # Invert the plant to figure out how the velocity filter would have to work |
| 216 | # out in order to filter out the forwards negative inertia. |
Austin Schuh | e05d2c1 | 2013-10-12 00:08:31 -0700 | [diff] [blame^] | 217 | # 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] | 218 | |
Austin Schuh | e05d2c1 | 2013-10-12 00:08:31 -0700 | [diff] [blame^] | 219 | # The throttle filter should filter such that the motor in the highest gear |
| 220 | # should be controlling the time constant. |
| 221 | # Do this by finding the index of FF that has the lowest value, and computing |
| 222 | # the sums using that index. |
| 223 | FF_sum = self.CurrentDrivetrain().FF.sum(axis=1) |
| 224 | max_FF_sum_index = numpy.argmax(FF_sum) |
| 225 | max_FF_sum = FF_sum[max_FF_sum_index, 0] |
| 226 | max_K_sum = self.CurrentDrivetrain().K[max_FF_sum_index, :].sum() |
| 227 | max_A_sum = self.CurrentDrivetrain().A[max_FF_sum_index, :].sum() |
| 228 | max_B_sum = self.CurrentDrivetrain().B[max_FF_sum_index, :].sum() |
| 229 | # Compute the FF sum for high gear. |
| 230 | high_max_FF_sum = self.drivetrain_high_high.FF[0, :].sum() |
| 231 | |
| 232 | # U = self.K[0, :].sum() * (R - xfiltered) + self.FF[0, :].sum() * R |
| 233 | # throttle * 12.0 = (self.K[0, :].sum() + self.FF[0, :].sum()) * R |
| 234 | # - self.K[0, :].sum() * xfiltered |
| 235 | |
| 236 | # R = (throttle * 12.0 + self.K[0, :].sum() * xfiltered) / |
| 237 | # (self.K[0, :].sum() + self.FF[0, :].sum()) |
| 238 | |
| 239 | # U = (K + FF) * R - K * X |
| 240 | # (K + FF) ^-1 * (U + K * X) = R |
| 241 | |
| 242 | # Xn+1 = A * X + B * (throttle * 12.0) |
| 243 | # xfiltered = self.A[0, :].sum() + B[0, :].sum() * throttle * 12.0 |
| 244 | |
| 245 | # Scale throttle by max_FF_sum / high_max_FF_sum. This will make low gear |
| 246 | # have the same velocity goal as high gear, and so that the robot will hold |
| 247 | # the same speed for the same throttle for all gears. |
| 248 | adjusted_ff_voltage = numpy.clip(throttle * 12.0 * max_FF_sum / high_max_FF_sum, -12.0, 12.0) |
| 249 | fvel = ((adjusted_ff_voltage + self.ttrust * max_K_sum * self.xfiltered) |
| 250 | / (self.ttrust * max_K_sum + max_FF_sum)) |
| 251 | self.xfiltered = (max_A_sum * self.xfiltered + max_B_sum * adjusted_ff_voltage) |
Austin Schuh | 048fb60 | 2013-10-07 23:31:04 -0700 | [diff] [blame] | 252 | |
| 253 | # Constant radius means that angualar_velocity / linear_velocity = constant. |
| 254 | # Compute the left and right velocities. |
| 255 | left_velocity = fvel - steering * numpy.abs(fvel) |
| 256 | right_velocity = fvel + steering * numpy.abs(fvel) |
| 257 | |
| 258 | # Write this constraint in the form of K * R = w |
| 259 | # angular velocity / linear velocity = constant |
| 260 | # (left - right) / (left + right) = constant |
| 261 | # left - right = constant * left + constant * right |
| 262 | |
| 263 | # (fvel - steering * numpy.abs(fvel) - fvel - steering * numpy.abs(fvel)) / |
| 264 | # (fvel - steering * numpy.abs(fvel) + fvel + steering * numpy.abs(fvel)) = |
| 265 | # constant |
| 266 | # (- 2 * steering * numpy.abs(fvel)) / (2 * fvel) = constant |
| 267 | # (-steering * sign(fvel)) = constant |
| 268 | # (-steering * sign(fvel)) * (left + right) = left - right |
| 269 | # (steering * sign(fvel) + 1) * left + (steering * sign(fvel) - 1) * right = 0 |
| 270 | |
| 271 | equality_k = numpy.matrix( |
| 272 | [[1 + steering * numpy.sign(fvel), -(1 - steering * numpy.sign(fvel))]]) |
| 273 | equality_w = 0.0 |
| 274 | |
| 275 | self.R[0, 0] = left_velocity |
| 276 | self.R[1, 0] = right_velocity |
| 277 | |
| 278 | # Construct a constraint on R by manipulating the constraint on U |
| 279 | # Start out with H * U <= k |
| 280 | # U = FF * R + K * (R - X) |
| 281 | # H * (FF * R + K * R - K * X) <= k |
| 282 | # H * (FF + K) * R <= k + H * K * X |
| 283 | R_poly = polytope.HPolytope( |
Austin Schuh | 03513cb | 2013-10-08 22:29:07 -0700 | [diff] [blame] | 284 | self.U_poly.H * (self.CurrentDrivetrain().K + self.CurrentDrivetrain().FF), |
| 285 | 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] | 286 | |
| 287 | # Limit R back inside the box. |
| 288 | self.boxed_R = CoerceGoal(R_poly, equality_k, equality_w, self.R) |
| 289 | |
Austin Schuh | 03513cb | 2013-10-08 22:29:07 -0700 | [diff] [blame] | 290 | FF_volts = self.CurrentDrivetrain().FF * self.boxed_R |
| 291 | self.U_ideal = self.CurrentDrivetrain().K * (self.boxed_R - self.X) + FF_volts |
Austin Schuh | 048fb60 | 2013-10-07 23:31:04 -0700 | [diff] [blame] | 292 | |
| 293 | self.U = numpy.clip(self.U_ideal, self.U_min, self.U_max) |
Austin Schuh | 03513cb | 2013-10-08 22:29:07 -0700 | [diff] [blame] | 294 | self.X = self.CurrentDrivetrain().A * self.X + self.CurrentDrivetrain().B * self.U |
Austin Schuh | e05d2c1 | 2013-10-12 00:08:31 -0700 | [diff] [blame^] | 295 | print "U is", self.U[0, 0], self.U[1, 0] |
Austin Schuh | 048fb60 | 2013-10-07 23:31:04 -0700 | [diff] [blame] | 296 | |
| 297 | |
| 298 | def main(argv): |
| 299 | drivetrain = VelocityDrivetrain() |
| 300 | |
| 301 | vl_plot = [] |
| 302 | vr_plot = [] |
| 303 | ul_plot = [] |
| 304 | ur_plot = [] |
| 305 | radius_plot = [] |
| 306 | t_plot = [] |
Austin Schuh | e05d2c1 | 2013-10-12 00:08:31 -0700 | [diff] [blame^] | 307 | left_gear_plot = [] |
| 308 | right_gear_plot = [] |
| 309 | drivetrain.left_high = True |
| 310 | drivetrain.right_high = True |
Austin Schuh | 03513cb | 2013-10-08 22:29:07 -0700 | [diff] [blame] | 311 | |
Austin Schuh | e05d2c1 | 2013-10-12 00:08:31 -0700 | [diff] [blame^] | 312 | if drivetrain.left_high: |
| 313 | print "Left is high" |
| 314 | else: |
| 315 | print "Left is low" |
| 316 | if drivetrain.right_high: |
| 317 | print "Right is high" |
| 318 | else: |
| 319 | print "Right is low" |
| 320 | |
| 321 | for t in numpy.arange(0, 2.0, drivetrain.dt): |
| 322 | if t < 1.0: |
| 323 | drivetrain.Update(throttle=0.60, steering=0.3) |
| 324 | elif t < 1.5: |
Austin Schuh | 048fb60 | 2013-10-07 23:31:04 -0700 | [diff] [blame] | 325 | drivetrain.Update(throttle=0.60, steering=-0.3) |
| 326 | else: |
Austin Schuh | e05d2c1 | 2013-10-12 00:08:31 -0700 | [diff] [blame^] | 327 | drivetrain.Update(throttle=0.60, steering=0.3) |
Austin Schuh | 048fb60 | 2013-10-07 23:31:04 -0700 | [diff] [blame] | 328 | t_plot.append(t) |
| 329 | vl_plot.append(drivetrain.X[0, 0]) |
| 330 | vr_plot.append(drivetrain.X[1, 0]) |
| 331 | ul_plot.append(drivetrain.U[0, 0]) |
| 332 | ur_plot.append(drivetrain.U[1, 0]) |
Austin Schuh | e05d2c1 | 2013-10-12 00:08:31 -0700 | [diff] [blame^] | 333 | left_gear_plot.append(drivetrain.left_high * 2.0 - 10.0) |
| 334 | right_gear_plot.append(drivetrain.right_high * 2.0 - 10.0) |
Austin Schuh | 048fb60 | 2013-10-07 23:31:04 -0700 | [diff] [blame] | 335 | |
| 336 | fwd_velocity = (drivetrain.X[1, 0] + drivetrain.X[0, 0]) / 2 |
| 337 | turn_velocity = (drivetrain.X[1, 0] - drivetrain.X[0, 0]) |
| 338 | if fwd_velocity < 0.0000001: |
| 339 | radius_plot.append(turn_velocity) |
| 340 | else: |
| 341 | radius_plot.append(turn_velocity / fwd_velocity) |
| 342 | |
| 343 | pylab.plot(t_plot, vl_plot, label='left velocity') |
| 344 | pylab.plot(t_plot, vr_plot, label='right velocity') |
| 345 | pylab.plot(t_plot, ul_plot, label='left power') |
| 346 | pylab.plot(t_plot, ur_plot, label='right power') |
| 347 | pylab.plot(t_plot, radius_plot, label='radius') |
Austin Schuh | e05d2c1 | 2013-10-12 00:08:31 -0700 | [diff] [blame^] | 348 | pylab.plot(t_plot, left_gear_plot, label='left_gear') |
| 349 | pylab.plot(t_plot, right_gear_plot, label='right_gear') |
Austin Schuh | 048fb60 | 2013-10-07 23:31:04 -0700 | [diff] [blame] | 350 | pylab.legend() |
| 351 | pylab.show() |
| 352 | return 0 |
| 353 | |
| 354 | if __name__ == '__main__': |
| 355 | sys.exit(main(sys.argv)) |