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 | e05d2c1 | 2013-10-12 00:08:31 -0700 | [diff] [blame] | 157 | # ttrust is the comprimise between having full throttle negative inertia, |
| 158 | # and having no throttle negative inertia. A value of 0 is full throttle |
| 159 | # inertia. A value of 1 is no throttle negative inertia. |
Austin Schuh | 03513cb | 2013-10-08 22:29:07 -0700 | [diff] [blame] | 160 | self.ttrust = 1.0 |
| 161 | |
| 162 | self.left_high = False |
| 163 | self.right_high = False |
| 164 | |
| 165 | def CurrentDrivetrain(self): |
| 166 | if self.left_high: |
| 167 | if self.right_high: |
| 168 | return self.drivetrain_high_high |
| 169 | else: |
| 170 | return self.drivetrain_high_low |
| 171 | else: |
| 172 | if self.right_high: |
| 173 | return self.drivetrain_low_high |
| 174 | else: |
| 175 | return self.drivetrain_low_low |
Austin Schuh | 048fb60 | 2013-10-07 23:31:04 -0700 | [diff] [blame] | 176 | |
Austin Schuh | e05d2c1 | 2013-10-12 00:08:31 -0700 | [diff] [blame] | 177 | def ComputeGear(self, wheel_velocity, should_print=False, current_gear=False, gear_name=None): |
| 178 | high_omega = (wheel_velocity / self.CurrentDrivetrain().G_high / |
| 179 | self.CurrentDrivetrain().r) |
| 180 | low_omega = (wheel_velocity / self.CurrentDrivetrain().G_low / |
| 181 | self.CurrentDrivetrain().r) |
Austin Schuh | 8afe35a | 2013-10-27 10:59:15 -0700 | [diff] [blame^] | 182 | 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] | 183 | high_torque = ((12.0 - high_omega / self.CurrentDrivetrain().Kv) * |
| 184 | self.CurrentDrivetrain().Kt / self.CurrentDrivetrain().R) |
| 185 | low_torque = ((12.0 - low_omega / self.CurrentDrivetrain().Kv) * |
| 186 | self.CurrentDrivetrain().Kt / self.CurrentDrivetrain().R) |
| 187 | high_power = high_torque * high_omega |
| 188 | low_power = low_torque * low_omega |
| 189 | if should_print: |
| 190 | print gear_name, "High omega", high_omega, "Low omega", low_omega |
| 191 | print gear_name, "High torque", high_torque, "Low torque", low_torque |
| 192 | print gear_name, "High power", high_power, "Low power", low_power |
| 193 | if (high_power > low_power) != current_gear: |
| 194 | if high_power > low_power: |
| 195 | print gear_name, "Shifting to high" |
| 196 | else: |
| 197 | print gear_name, "Shifting to low" |
| 198 | |
Austin Schuh | 8afe35a | 2013-10-27 10:59:15 -0700 | [diff] [blame^] | 199 | # Shift algorithm improvements. |
| 200 | # TODO(aschuh): |
| 201 | # It takes time to shift. Shifting down for 1 cycle doesn't make sense |
| 202 | # because you will end up slower than without shifting. Figure out how |
| 203 | # to include that info. |
| 204 | # If the driver is still in high gear, but isn't asking for the extra power |
| 205 | # from low gear, don't shift until he asks for it. |
Austin Schuh | e05d2c1 | 2013-10-12 00:08:31 -0700 | [diff] [blame] | 206 | return high_power > low_power |
| 207 | |
Austin Schuh | ec00fc6 | 2013-10-12 00:31:49 -0700 | [diff] [blame] | 208 | def FilterVelocity(self, throttle): |
Austin Schuh | 048fb60 | 2013-10-07 23:31:04 -0700 | [diff] [blame] | 209 | # Invert the plant to figure out how the velocity filter would have to work |
| 210 | # out in order to filter out the forwards negative inertia. |
Austin Schuh | e05d2c1 | 2013-10-12 00:08:31 -0700 | [diff] [blame] | 211 | # 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] | 212 | |
Austin Schuh | e05d2c1 | 2013-10-12 00:08:31 -0700 | [diff] [blame] | 213 | # The throttle filter should filter such that the motor in the highest gear |
| 214 | # should be controlling the time constant. |
| 215 | # Do this by finding the index of FF that has the lowest value, and computing |
| 216 | # the sums using that index. |
| 217 | FF_sum = self.CurrentDrivetrain().FF.sum(axis=1) |
Austin Schuh | 936838f | 2013-10-12 00:36:43 -0700 | [diff] [blame] | 218 | max_FF_sum_index = numpy.argmin(FF_sum) |
Austin Schuh | e05d2c1 | 2013-10-12 00:08:31 -0700 | [diff] [blame] | 219 | max_FF_sum = FF_sum[max_FF_sum_index, 0] |
| 220 | max_K_sum = self.CurrentDrivetrain().K[max_FF_sum_index, :].sum() |
| 221 | max_A_sum = self.CurrentDrivetrain().A[max_FF_sum_index, :].sum() |
| 222 | max_B_sum = self.CurrentDrivetrain().B[max_FF_sum_index, :].sum() |
| 223 | # Compute the FF sum for high gear. |
| 224 | high_max_FF_sum = self.drivetrain_high_high.FF[0, :].sum() |
| 225 | |
Austin Schuh | ec00fc6 | 2013-10-12 00:31:49 -0700 | [diff] [blame] | 226 | # 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] | 227 | # throttle * 12.0 = (self.K[0, :].sum() + self.FF[0, :].sum()) * R |
Austin Schuh | ec00fc6 | 2013-10-12 00:31:49 -0700 | [diff] [blame] | 228 | # - self.K[0, :].sum() * x_avg |
Austin Schuh | e05d2c1 | 2013-10-12 00:08:31 -0700 | [diff] [blame] | 229 | |
Austin Schuh | ec00fc6 | 2013-10-12 00:31:49 -0700 | [diff] [blame] | 230 | # R = (throttle * 12.0 + self.K[0, :].sum() * x_avg) / |
Austin Schuh | e05d2c1 | 2013-10-12 00:08:31 -0700 | [diff] [blame] | 231 | # (self.K[0, :].sum() + self.FF[0, :].sum()) |
| 232 | |
| 233 | # U = (K + FF) * R - K * X |
| 234 | # (K + FF) ^-1 * (U + K * X) = R |
| 235 | |
Austin Schuh | e05d2c1 | 2013-10-12 00:08:31 -0700 | [diff] [blame] | 236 | # Scale throttle by max_FF_sum / high_max_FF_sum. This will make low gear |
| 237 | # have the same velocity goal as high gear, and so that the robot will hold |
| 238 | # the same speed for the same throttle for all gears. |
| 239 | adjusted_ff_voltage = numpy.clip(throttle * 12.0 * max_FF_sum / high_max_FF_sum, -12.0, 12.0) |
Austin Schuh | ec00fc6 | 2013-10-12 00:31:49 -0700 | [diff] [blame] | 240 | return ((adjusted_ff_voltage + self.ttrust * max_K_sum * (self.X[0, 0] + self.X[1, 0]) / 2.0) |
Austin Schuh | e05d2c1 | 2013-10-12 00:08:31 -0700 | [diff] [blame] | 241 | / (self.ttrust * max_K_sum + max_FF_sum)) |
Austin Schuh | ec00fc6 | 2013-10-12 00:31:49 -0700 | [diff] [blame] | 242 | |
| 243 | def Update(self, throttle, steering): |
| 244 | # Shift into the gear which sends the most power to the floor. |
| 245 | # This is the same as sending the most torque down to the floor at the |
| 246 | # wheel. |
| 247 | |
| 248 | self.left_high = self.ComputeGear(self.X[0, 0], should_print=True, current_gear=self.left_high, gear_name="left") |
| 249 | self.right_high = self.ComputeGear(self.X[1, 0], should_print=True, current_gear=self.right_high, gear_name="right") |
| 250 | |
| 251 | FF_sum = self.CurrentDrivetrain().FF.sum(axis=1) |
| 252 | |
| 253 | # Filter the throttle to provide a nicer response. |
| 254 | |
| 255 | # TODO(austin): fn |
| 256 | fvel = self.FilterVelocity(throttle) |
Austin Schuh | 048fb60 | 2013-10-07 23:31:04 -0700 | [diff] [blame] | 257 | |
| 258 | # Constant radius means that angualar_velocity / linear_velocity = constant. |
| 259 | # Compute the left and right velocities. |
| 260 | left_velocity = fvel - steering * numpy.abs(fvel) |
| 261 | right_velocity = fvel + steering * numpy.abs(fvel) |
| 262 | |
| 263 | # Write this constraint in the form of K * R = w |
| 264 | # angular velocity / linear velocity = constant |
| 265 | # (left - right) / (left + right) = constant |
| 266 | # left - right = constant * left + constant * right |
| 267 | |
| 268 | # (fvel - steering * numpy.abs(fvel) - fvel - steering * numpy.abs(fvel)) / |
| 269 | # (fvel - steering * numpy.abs(fvel) + fvel + steering * numpy.abs(fvel)) = |
| 270 | # constant |
| 271 | # (- 2 * steering * numpy.abs(fvel)) / (2 * fvel) = constant |
| 272 | # (-steering * sign(fvel)) = constant |
| 273 | # (-steering * sign(fvel)) * (left + right) = left - right |
| 274 | # (steering * sign(fvel) + 1) * left + (steering * sign(fvel) - 1) * right = 0 |
| 275 | |
| 276 | equality_k = numpy.matrix( |
| 277 | [[1 + steering * numpy.sign(fvel), -(1 - steering * numpy.sign(fvel))]]) |
| 278 | equality_w = 0.0 |
| 279 | |
| 280 | self.R[0, 0] = left_velocity |
| 281 | self.R[1, 0] = right_velocity |
| 282 | |
| 283 | # Construct a constraint on R by manipulating the constraint on U |
| 284 | # Start out with H * U <= k |
| 285 | # U = FF * R + K * (R - X) |
| 286 | # H * (FF * R + K * R - K * X) <= k |
| 287 | # H * (FF + K) * R <= k + H * K * X |
| 288 | R_poly = polytope.HPolytope( |
Austin Schuh | 03513cb | 2013-10-08 22:29:07 -0700 | [diff] [blame] | 289 | self.U_poly.H * (self.CurrentDrivetrain().K + self.CurrentDrivetrain().FF), |
| 290 | 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] | 291 | |
| 292 | # Limit R back inside the box. |
| 293 | self.boxed_R = CoerceGoal(R_poly, equality_k, equality_w, self.R) |
| 294 | |
Austin Schuh | 03513cb | 2013-10-08 22:29:07 -0700 | [diff] [blame] | 295 | FF_volts = self.CurrentDrivetrain().FF * self.boxed_R |
| 296 | 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] | 297 | |
| 298 | 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] | 299 | self.X = self.CurrentDrivetrain().A * self.X + self.CurrentDrivetrain().B * self.U |
Austin Schuh | e05d2c1 | 2013-10-12 00:08:31 -0700 | [diff] [blame] | 300 | print "U is", self.U[0, 0], self.U[1, 0] |
Austin Schuh | 048fb60 | 2013-10-07 23:31:04 -0700 | [diff] [blame] | 301 | |
| 302 | |
| 303 | def main(argv): |
Austin Schuh | 8afe35a | 2013-10-27 10:59:15 -0700 | [diff] [blame^] | 304 | vdrivetrain = VelocityDrivetrain() |
Austin Schuh | 048fb60 | 2013-10-07 23:31:04 -0700 | [diff] [blame] | 305 | |
| 306 | vl_plot = [] |
| 307 | vr_plot = [] |
| 308 | ul_plot = [] |
| 309 | ur_plot = [] |
| 310 | radius_plot = [] |
| 311 | t_plot = [] |
Austin Schuh | e05d2c1 | 2013-10-12 00:08:31 -0700 | [diff] [blame] | 312 | left_gear_plot = [] |
| 313 | right_gear_plot = [] |
Austin Schuh | 8afe35a | 2013-10-27 10:59:15 -0700 | [diff] [blame^] | 314 | vdrivetrain.left_high = True |
| 315 | vdrivetrain.right_high = True |
Austin Schuh | 03513cb | 2013-10-08 22:29:07 -0700 | [diff] [blame] | 316 | |
Austin Schuh | 8afe35a | 2013-10-27 10:59:15 -0700 | [diff] [blame^] | 317 | print "K is", vdrivetrain.CurrentDrivetrain().K |
| 318 | |
| 319 | if vdrivetrain.left_high: |
Austin Schuh | e05d2c1 | 2013-10-12 00:08:31 -0700 | [diff] [blame] | 320 | print "Left is high" |
| 321 | else: |
| 322 | print "Left is low" |
Austin Schuh | 8afe35a | 2013-10-27 10:59:15 -0700 | [diff] [blame^] | 323 | if vdrivetrain.right_high: |
Austin Schuh | e05d2c1 | 2013-10-12 00:08:31 -0700 | [diff] [blame] | 324 | print "Right is high" |
| 325 | else: |
| 326 | print "Right is low" |
| 327 | |
Austin Schuh | 8afe35a | 2013-10-27 10:59:15 -0700 | [diff] [blame^] | 328 | for t in numpy.arange(0, 2.0, vdrivetrain.dt): |
Austin Schuh | e05d2c1 | 2013-10-12 00:08:31 -0700 | [diff] [blame] | 329 | if t < 1.0: |
Austin Schuh | 8afe35a | 2013-10-27 10:59:15 -0700 | [diff] [blame^] | 330 | vdrivetrain.Update(throttle=0.60, steering=0.3) |
Austin Schuh | e05d2c1 | 2013-10-12 00:08:31 -0700 | [diff] [blame] | 331 | elif t < 1.5: |
Austin Schuh | 8afe35a | 2013-10-27 10:59:15 -0700 | [diff] [blame^] | 332 | vdrivetrain.Update(throttle=0.60, steering=-0.3) |
Austin Schuh | 048fb60 | 2013-10-07 23:31:04 -0700 | [diff] [blame] | 333 | else: |
Austin Schuh | 8afe35a | 2013-10-27 10:59:15 -0700 | [diff] [blame^] | 334 | vdrivetrain.Update(throttle=0.0, steering=0.3) |
Austin Schuh | 048fb60 | 2013-10-07 23:31:04 -0700 | [diff] [blame] | 335 | t_plot.append(t) |
Austin Schuh | 8afe35a | 2013-10-27 10:59:15 -0700 | [diff] [blame^] | 336 | vl_plot.append(vdrivetrain.X[0, 0]) |
| 337 | vr_plot.append(vdrivetrain.X[1, 0]) |
| 338 | ul_plot.append(vdrivetrain.U[0, 0]) |
| 339 | ur_plot.append(vdrivetrain.U[1, 0]) |
| 340 | left_gear_plot.append(vdrivetrain.left_high * 2.0 - 10.0) |
| 341 | right_gear_plot.append(vdrivetrain.right_high * 2.0 - 10.0) |
Austin Schuh | 048fb60 | 2013-10-07 23:31:04 -0700 | [diff] [blame] | 342 | |
Austin Schuh | 8afe35a | 2013-10-27 10:59:15 -0700 | [diff] [blame^] | 343 | fwd_velocity = (vdrivetrain.X[1, 0] + vdrivetrain.X[0, 0]) / 2 |
| 344 | turn_velocity = (vdrivetrain.X[1, 0] - vdrivetrain.X[0, 0]) |
Austin Schuh | 048fb60 | 2013-10-07 23:31:04 -0700 | [diff] [blame] | 345 | if fwd_velocity < 0.0000001: |
| 346 | radius_plot.append(turn_velocity) |
| 347 | else: |
| 348 | radius_plot.append(turn_velocity / fwd_velocity) |
| 349 | |
Austin Schuh | 8afe35a | 2013-10-27 10:59:15 -0700 | [diff] [blame^] | 350 | cim_velocity_plot = [] |
| 351 | cim_voltage_plot = [] |
| 352 | cim_time = [] |
| 353 | cim = drivetrain.CIM() |
| 354 | R = numpy.matrix([[300]]) |
| 355 | for t in numpy.arange(0, 0.5, cim.dt): |
| 356 | U = numpy.clip(cim.K * (R - cim.X) + R / cim.Kv, cim.U_min, cim.U_max) |
| 357 | cim.Update(U) |
| 358 | cim_velocity_plot.append(cim.X[0, 0]) |
| 359 | cim_voltage_plot.append(U[0, 0] * 10) |
| 360 | cim_time.append(t) |
| 361 | pylab.plot(cim_time, cim_velocity_plot, label='cim spinup') |
| 362 | pylab.plot(cim_time, cim_voltage_plot, label='cim voltage') |
| 363 | pylab.legend() |
| 364 | pylab.show() |
| 365 | |
| 366 | |
Austin Schuh | 048fb60 | 2013-10-07 23:31:04 -0700 | [diff] [blame] | 367 | pylab.plot(t_plot, vl_plot, label='left velocity') |
| 368 | pylab.plot(t_plot, vr_plot, label='right velocity') |
Austin Schuh | 8afe35a | 2013-10-27 10:59:15 -0700 | [diff] [blame^] | 369 | pylab.plot(t_plot, ul_plot, label='left voltage') |
| 370 | pylab.plot(t_plot, ur_plot, label='right voltage') |
Austin Schuh | 048fb60 | 2013-10-07 23:31:04 -0700 | [diff] [blame] | 371 | pylab.plot(t_plot, radius_plot, label='radius') |
Austin Schuh | 8afe35a | 2013-10-27 10:59:15 -0700 | [diff] [blame^] | 372 | pylab.plot(t_plot, left_gear_plot, label='left gear high') |
| 373 | pylab.plot(t_plot, right_gear_plot, label='right gear high') |
Austin Schuh | 048fb60 | 2013-10-07 23:31:04 -0700 | [diff] [blame] | 374 | pylab.legend() |
| 375 | pylab.show() |
| 376 | return 0 |
| 377 | |
| 378 | if __name__ == '__main__': |
| 379 | sys.exit(main(sys.argv)) |