blob: e047c117e412b780687f5bf451064d0ce3f8e3f0 [file] [log] [blame]
Comran Morshede9b12922015-11-04 19:46:48 +00001#!/usr/bin/python
2
3import numpy
4import sys
Adam Snaider83eae562016-09-10 16:47:33 -07005from frc971.control_loops.python import polytope
6from y2014_bot3.control_loops.python import drivetrain
7from frc971.control_loops.python import control_loop
8from frc971.control_loops.python import controls
Comran Morshede9b12922015-11-04 19:46:48 +00009from matplotlib import pylab
10
Adam Snaider83eae562016-09-10 16:47:33 -070011import gflags
12import glog
13
Comran Morshede9b12922015-11-04 19:46:48 +000014__author__ = 'Austin Schuh (austin.linux@gmail.com)'
15
Adam Snaider83eae562016-09-10 16:47:33 -070016FLAGS = gflags.FLAGS
17
18try:
19 gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.')
20except gflags.DuplicateFlagError:
21 pass
Comran Morshede9b12922015-11-04 19:46:48 +000022
23def 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
42def 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
109class 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)
Comran Morshed41ed7c22015-11-04 21:03:37 +0000114 self.dt = 0.005
Comran Morshede9b12922015-11-04 19:46:48 +0000115 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]]])
Adam Snaider83eae562016-09-10 16:47:33 -0700122 self.C = numpy.matrix(numpy.eye(2))
123 self.D = numpy.matrix(numpy.zeros((2, 2)))
Comran Morshede9b12922015-11-04 19:46:48 +0000124
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
Adam Snaider83eae562016-09-10 16:47:33 -0700131 self.PlaceControllerPoles([0.67, 0.67])
Comran Morshede9b12922015-11-04 19:46:48 +0000132 self.PlaceObserverPoles([0.02, 0.02])
133
134 self.G_high = self._drivetrain.G_high
135 self.G_low = self._drivetrain.G_low
Adam Snaider83eae562016-09-10 16:47:33 -0700136 self.resistance = self._drivetrain.resistance
Comran Morshede9b12922015-11-04 19:46:48 +0000137 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
145class 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
Comran Morshed41ed7c22015-11-04 21:03:37 +0000180 self.dt = 0.005
Comran Morshede9b12922015-11-04 19:46:48 +0000181
182 self.R = numpy.matrix(
183 [[0.0],
184 [0.0]])
185
Adam Snaider83eae562016-09-10 16:47:33 -0700186 self.U_ideal = numpy.matrix(
187 [[0.0],
188 [0.0]])
189
Comran Morshede9b12922015-11-04 19:46:48 +0000190 # ttrust is the comprimise between having full throttle negative inertia,
191 # and having no throttle negative inertia. A value of 0 is full throttle
192 # inertia. A value of 1 is no throttle negative inertia.
Adam Snaider83eae562016-09-10 16:47:33 -0700193 self.ttrust = 1.0
Comran Morshede9b12922015-11-04 19:46:48 +0000194
195 self.left_gear = VelocityDrivetrain.LOW
196 self.right_gear = VelocityDrivetrain.LOW
197 self.left_shifter_position = 0.0
198 self.right_shifter_position = 0.0
199 self.left_cim = drivetrain.CIM()
200 self.right_cim = drivetrain.CIM()
201
202 def IsInGear(self, gear):
203 return gear is VelocityDrivetrain.HIGH or gear is VelocityDrivetrain.LOW
204
205 def MotorRPM(self, shifter_position, velocity):
206 if shifter_position > 0.5:
207 return (velocity / self.CurrentDrivetrain().G_high /
208 self.CurrentDrivetrain().r)
209 else:
210 return (velocity / self.CurrentDrivetrain().G_low /
211 self.CurrentDrivetrain().r)
212
213 def CurrentDrivetrain(self):
214 if self.left_shifter_position > 0.5:
215 if self.right_shifter_position > 0.5:
216 return self.drivetrain_high_high
217 else:
218 return self.drivetrain_high_low
219 else:
220 if self.right_shifter_position > 0.5:
221 return self.drivetrain_low_high
222 else:
223 return self.drivetrain_low_low
224
225 def SimShifter(self, gear, shifter_position):
226 if gear is VelocityDrivetrain.HIGH or gear is VelocityDrivetrain.SHIFTING_UP:
227 shifter_position = min(shifter_position + 0.5, 1.0)
228 else:
229 shifter_position = max(shifter_position - 0.5, 0.0)
230
231 if shifter_position == 1.0:
232 gear = VelocityDrivetrain.HIGH
233 elif shifter_position == 0.0:
234 gear = VelocityDrivetrain.LOW
235
236 return gear, shifter_position
237
238 def ComputeGear(self, wheel_velocity, should_print=False, current_gear=False, gear_name=None):
239 high_omega = (wheel_velocity / self.CurrentDrivetrain().G_high /
240 self.CurrentDrivetrain().r)
241 low_omega = (wheel_velocity / self.CurrentDrivetrain().G_low /
242 self.CurrentDrivetrain().r)
243 #print gear_name, "Motor Energy Difference.", 0.5 * 0.000140032647 * (low_omega * low_omega - high_omega * high_omega), "joules"
244 high_torque = ((12.0 - high_omega / self.CurrentDrivetrain().Kv) *
Adam Snaider83eae562016-09-10 16:47:33 -0700245 self.CurrentDrivetrain().Kt / self.CurrentDrivetrain().resistance)
Comran Morshede9b12922015-11-04 19:46:48 +0000246 low_torque = ((12.0 - low_omega / self.CurrentDrivetrain().Kv) *
Adam Snaider83eae562016-09-10 16:47:33 -0700247 self.CurrentDrivetrain().Kt / self.CurrentDrivetrain().resistance)
Comran Morshede9b12922015-11-04 19:46:48 +0000248 high_power = high_torque * high_omega
249 low_power = low_torque * low_omega
250 #if should_print:
251 # print gear_name, "High omega", high_omega, "Low omega", low_omega
252 # print gear_name, "High torque", high_torque, "Low torque", low_torque
253 # print gear_name, "High power", high_power, "Low power", low_power
254
255 # Shift algorithm improvements.
256 # TODO(aschuh):
257 # It takes time to shift. Shifting down for 1 cycle doesn't make sense
258 # because you will end up slower than without shifting. Figure out how
259 # to include that info.
260 # If the driver is still in high gear, but isn't asking for the extra power
261 # from low gear, don't shift until he asks for it.
262 goal_gear_is_high = high_power > low_power
263 #goal_gear_is_high = True
264
265 if not self.IsInGear(current_gear):
Adam Snaider83eae562016-09-10 16:47:33 -0700266 glog.debug('%s Not in gear.', gear_name)
Comran Morshede9b12922015-11-04 19:46:48 +0000267 return current_gear
268 else:
269 is_high = current_gear is VelocityDrivetrain.HIGH
270 if is_high != goal_gear_is_high:
271 if goal_gear_is_high:
Adam Snaider83eae562016-09-10 16:47:33 -0700272 glog.debug('%s Shifting up.', gear_name)
Comran Morshede9b12922015-11-04 19:46:48 +0000273 return VelocityDrivetrain.SHIFTING_UP
274 else:
Adam Snaider83eae562016-09-10 16:47:33 -0700275 glog.debug('%s Shifting down.', gear_name)
Comran Morshede9b12922015-11-04 19:46:48 +0000276 return VelocityDrivetrain.SHIFTING_DOWN
277 else:
278 return current_gear
279
280 def FilterVelocity(self, throttle):
281 # Invert the plant to figure out how the velocity filter would have to work
282 # out in order to filter out the forwards negative inertia.
283 # This math assumes that the left and right power and velocity are equal.
284
285 # The throttle filter should filter such that the motor in the highest gear
286 # should be controlling the time constant.
287 # Do this by finding the index of FF that has the lowest value, and computing
288 # the sums using that index.
289 FF_sum = self.CurrentDrivetrain().FF.sum(axis=1)
290 min_FF_sum_index = numpy.argmin(FF_sum)
291 min_FF_sum = FF_sum[min_FF_sum_index, 0]
292 min_K_sum = self.CurrentDrivetrain().K[min_FF_sum_index, :].sum()
293 # Compute the FF sum for high gear.
294 high_min_FF_sum = self.drivetrain_high_high.FF[0, :].sum()
295
296 # U = self.K[0, :].sum() * (R - x_avg) + self.FF[0, :].sum() * R
297 # throttle * 12.0 = (self.K[0, :].sum() + self.FF[0, :].sum()) * R
298 # - self.K[0, :].sum() * x_avg
299
300 # R = (throttle * 12.0 + self.K[0, :].sum() * x_avg) /
301 # (self.K[0, :].sum() + self.FF[0, :].sum())
302
303 # U = (K + FF) * R - K * X
304 # (K + FF) ^-1 * (U + K * X) = R
305
306 # Scale throttle by min_FF_sum / high_min_FF_sum. This will make low gear
307 # have the same velocity goal as high gear, and so that the robot will hold
308 # the same speed for the same throttle for all gears.
309 adjusted_ff_voltage = numpy.clip(throttle * 12.0 * min_FF_sum / high_min_FF_sum, -12.0, 12.0)
310 return ((adjusted_ff_voltage + self.ttrust * min_K_sum * (self.X[0, 0] + self.X[1, 0]) / 2.0)
311 / (self.ttrust * min_K_sum + min_FF_sum))
312
313 def Update(self, throttle, steering):
314 # Shift into the gear which sends the most power to the floor.
315 # This is the same as sending the most torque down to the floor at the
316 # wheel.
317
Comran Morshed41ed7c22015-11-04 21:03:37 +0000318 self.left_gear = self.right_gear = True
Adam Snaider83eae562016-09-10 16:47:33 -0700319 if True:
Comran Morshed41ed7c22015-11-04 21:03:37 +0000320 self.left_gear = self.ComputeGear(self.X[0, 0], should_print=True,
321 current_gear=self.left_gear,
322 gear_name="left")
323 self.right_gear = self.ComputeGear(self.X[1, 0], should_print=True,
324 current_gear=self.right_gear,
325 gear_name="right")
326 if self.IsInGear(self.left_gear):
327 self.left_cim.X[0, 0] = self.MotorRPM(self.left_shifter_position, self.X[0, 0])
Comran Morshede9b12922015-11-04 19:46:48 +0000328
Comran Morshed41ed7c22015-11-04 21:03:37 +0000329 if self.IsInGear(self.right_gear):
330 self.right_cim.X[0, 0] = self.MotorRPM(self.right_shifter_position, self.X[0, 0])
Comran Morshede9b12922015-11-04 19:46:48 +0000331
Adam Snaider83eae562016-09-10 16:47:33 -0700332 if self.IsInGear(self.left_gear) and self.IsInGear(self.right_gear):
Comran Morshede9b12922015-11-04 19:46:48 +0000333 # Filter the throttle to provide a nicer response.
334 fvel = self.FilterVelocity(throttle)
335
336 # Constant radius means that angualar_velocity / linear_velocity = constant.
337 # Compute the left and right velocities.
Comran Morshed41ed7c22015-11-04 21:03:37 +0000338 steering_velocity = numpy.abs(fvel) * steering
339 left_velocity = fvel - steering_velocity
340 right_velocity = fvel + steering_velocity
Comran Morshede9b12922015-11-04 19:46:48 +0000341
342 # Write this constraint in the form of K * R = w
343 # angular velocity / linear velocity = constant
344 # (left - right) / (left + right) = constant
345 # left - right = constant * left + constant * right
346
347 # (fvel - steering * numpy.abs(fvel) - fvel - steering * numpy.abs(fvel)) /
348 # (fvel - steering * numpy.abs(fvel) + fvel + steering * numpy.abs(fvel)) =
349 # constant
350 # (- 2 * steering * numpy.abs(fvel)) / (2 * fvel) = constant
351 # (-steering * sign(fvel)) = constant
352 # (-steering * sign(fvel)) * (left + right) = left - right
353 # (steering * sign(fvel) + 1) * left + (steering * sign(fvel) - 1) * right = 0
354
355 equality_k = numpy.matrix(
356 [[1 + steering * numpy.sign(fvel), -(1 - steering * numpy.sign(fvel))]])
357 equality_w = 0.0
358
359 self.R[0, 0] = left_velocity
360 self.R[1, 0] = right_velocity
361
362 # Construct a constraint on R by manipulating the constraint on U
363 # Start out with H * U <= k
364 # U = FF * R + K * (R - X)
365 # H * (FF * R + K * R - K * X) <= k
366 # H * (FF + K) * R <= k + H * K * X
367 R_poly = polytope.HPolytope(
368 self.U_poly.H * (self.CurrentDrivetrain().K + self.CurrentDrivetrain().FF),
369 self.U_poly.k + self.U_poly.H * self.CurrentDrivetrain().K * self.X)
370
371 # Limit R back inside the box.
372 self.boxed_R = CoerceGoal(R_poly, equality_k, equality_w, self.R)
373
374 FF_volts = self.CurrentDrivetrain().FF * self.boxed_R
375 self.U_ideal = self.CurrentDrivetrain().K * (self.boxed_R - self.X) + FF_volts
376 else:
Adam Snaider83eae562016-09-10 16:47:33 -0700377 glog.debug('Not all in gear')
Comran Morshede9b12922015-11-04 19:46:48 +0000378 if not self.IsInGear(self.left_gear) and not self.IsInGear(self.right_gear):
379 # TODO(austin): Use battery volts here.
380 R_left = self.MotorRPM(self.left_shifter_position, self.X[0, 0])
381 self.U_ideal[0, 0] = numpy.clip(
382 self.left_cim.K * (R_left - self.left_cim.X) + R_left / self.left_cim.Kv,
383 self.left_cim.U_min, self.left_cim.U_max)
384 self.left_cim.Update(self.U_ideal[0, 0])
385
386 R_right = self.MotorRPM(self.right_shifter_position, self.X[1, 0])
387 self.U_ideal[1, 0] = numpy.clip(
388 self.right_cim.K * (R_right - self.right_cim.X) + R_right / self.right_cim.Kv,
389 self.right_cim.U_min, self.right_cim.U_max)
390 self.right_cim.Update(self.U_ideal[1, 0])
391 else:
392 assert False
393
394 self.U = numpy.clip(self.U_ideal, self.U_min, self.U_max)
395
396 # TODO(austin): Model the robot as not accelerating when you shift...
397 # This hack only works when you shift at the same time.
Adam Snaider83eae562016-09-10 16:47:33 -0700398 if self.IsInGear(self.left_gear) and self.IsInGear(self.right_gear):
Comran Morshede9b12922015-11-04 19:46:48 +0000399 self.X = self.CurrentDrivetrain().A * self.X + self.CurrentDrivetrain().B * self.U
400
401 self.left_gear, self.left_shifter_position = self.SimShifter(
402 self.left_gear, self.left_shifter_position)
403 self.right_gear, self.right_shifter_position = self.SimShifter(
404 self.right_gear, self.right_shifter_position)
405
Adam Snaider83eae562016-09-10 16:47:33 -0700406 glog.debug('U is %s %s', str(self.U[0, 0]), str(self.U[1, 0]))
407 glog.debug('Left shifter %s %d Right shifter %s %d',
408 self.left_gear, self.left_shifter_position,
409 self.right_gear, self.right_shifter_position)
Comran Morshede9b12922015-11-04 19:46:48 +0000410
411
412def main(argv):
Adam Snaider83eae562016-09-10 16:47:33 -0700413 argv = FLAGS(argv)
414
Comran Morshede9b12922015-11-04 19:46:48 +0000415 vdrivetrain = VelocityDrivetrain()
416
Adam Snaider83eae562016-09-10 16:47:33 -0700417 if not FLAGS.plot:
418 if len(argv) != 5:
419 glog.fatal('Expected .h file name and .cc file name')
Comran Morshede9b12922015-11-04 19:46:48 +0000420 else:
Adam Snaider83eae562016-09-10 16:47:33 -0700421 namespaces = ['y2014_bot3', 'control_loops', 'drivetrain']
422 dog_loop_writer = control_loop.ControlLoopWriter(
423 "VelocityDrivetrain", [vdrivetrain.drivetrain_low_low,
424 vdrivetrain.drivetrain_low_high,
425 vdrivetrain.drivetrain_high_low,
426 vdrivetrain.drivetrain_high_high],
427 namespaces=namespaces)
428
Comran Morshede9b12922015-11-04 19:46:48 +0000429 dog_loop_writer.Write(argv[1], argv[2])
430
Adam Snaider83eae562016-09-10 16:47:33 -0700431 cim_writer = control_loop.ControlLoopWriter(
432 "CIM", [drivetrain.CIM()])
Comran Morshede9b12922015-11-04 19:46:48 +0000433
Adam Snaider83eae562016-09-10 16:47:33 -0700434 cim_writer.Write(argv[3], argv[4])
435 return
Comran Morshede9b12922015-11-04 19:46:48 +0000436
437 vl_plot = []
438 vr_plot = []
439 ul_plot = []
440 ur_plot = []
441 radius_plot = []
442 t_plot = []
443 left_gear_plot = []
444 right_gear_plot = []
445 vdrivetrain.left_shifter_position = 0.0
446 vdrivetrain.right_shifter_position = 0.0
447 vdrivetrain.left_gear = VelocityDrivetrain.LOW
448 vdrivetrain.right_gear = VelocityDrivetrain.LOW
449
Adam Snaider83eae562016-09-10 16:47:33 -0700450 glog.debug('K is %s', str(vdrivetrain.CurrentDrivetrain().K))
Comran Morshede9b12922015-11-04 19:46:48 +0000451
452 if vdrivetrain.left_gear is VelocityDrivetrain.HIGH:
Adam Snaider83eae562016-09-10 16:47:33 -0700453 glog.debug('Left is high')
Comran Morshede9b12922015-11-04 19:46:48 +0000454 else:
Adam Snaider83eae562016-09-10 16:47:33 -0700455 glog.debug('Left is low')
Comran Morshede9b12922015-11-04 19:46:48 +0000456 if vdrivetrain.right_gear is VelocityDrivetrain.HIGH:
Adam Snaider83eae562016-09-10 16:47:33 -0700457 glog.debug('Right is high')
Comran Morshede9b12922015-11-04 19:46:48 +0000458 else:
Adam Snaider83eae562016-09-10 16:47:33 -0700459 glog.debug('Right is low')
Comran Morshede9b12922015-11-04 19:46:48 +0000460
Comran Morshed41ed7c22015-11-04 21:03:37 +0000461 for t in numpy.arange(0, 1.7, vdrivetrain.dt):
462 if t < 0.5:
463 vdrivetrain.Update(throttle=0.00, steering=1.0)
Comran Morshede9b12922015-11-04 19:46:48 +0000464 elif t < 1.2:
Comran Morshed41ed7c22015-11-04 21:03:37 +0000465 vdrivetrain.Update(throttle=0.5, steering=1.0)
Comran Morshede9b12922015-11-04 19:46:48 +0000466 else:
Comran Morshed41ed7c22015-11-04 21:03:37 +0000467 vdrivetrain.Update(throttle=0.00, steering=1.0)
Comran Morshede9b12922015-11-04 19:46:48 +0000468 t_plot.append(t)
469 vl_plot.append(vdrivetrain.X[0, 0])
470 vr_plot.append(vdrivetrain.X[1, 0])
471 ul_plot.append(vdrivetrain.U[0, 0])
472 ur_plot.append(vdrivetrain.U[1, 0])
473 left_gear_plot.append((vdrivetrain.left_gear is VelocityDrivetrain.HIGH) * 2.0 - 10.0)
474 right_gear_plot.append((vdrivetrain.right_gear is VelocityDrivetrain.HIGH) * 2.0 - 10.0)
475
476 fwd_velocity = (vdrivetrain.X[1, 0] + vdrivetrain.X[0, 0]) / 2
477 turn_velocity = (vdrivetrain.X[1, 0] - vdrivetrain.X[0, 0])
478 if abs(fwd_velocity) < 0.0000001:
479 radius_plot.append(turn_velocity)
480 else:
481 radius_plot.append(turn_velocity / fwd_velocity)
482
Comran Morshede9b12922015-11-04 19:46:48 +0000483 # TODO(austin):
484 # Shifting compensation.
485
486 # Tighten the turn.
487 # Closed loop drive.
488
489 pylab.plot(t_plot, vl_plot, label='left velocity')
490 pylab.plot(t_plot, vr_plot, label='right velocity')
491 pylab.plot(t_plot, ul_plot, label='left voltage')
492 pylab.plot(t_plot, ur_plot, label='right voltage')
493 pylab.plot(t_plot, radius_plot, label='radius')
494 pylab.plot(t_plot, left_gear_plot, label='left gear high')
495 pylab.plot(t_plot, right_gear_plot, label='right gear high')
496 pylab.legend()
497 pylab.show()
498 return 0
499
500if __name__ == '__main__':
501 sys.exit(main(sys.argv))