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