blob: f2dfdbef7ef4455116bf566486d847a952e55959 [file] [log] [blame]
Austin Schuh048fb602013-10-07 23:31:04 -07001#!/usr/bin/python
2
3import numpy
4import sys
5import polytope
6import drivetrain
Austin Schuh2054f5f2013-10-27 14:54:10 -07007import control_loop
Austin Schuh048fb602013-10-07 23:31:04 -07008import controls
9from matplotlib import pylab
10
11__author__ = 'Austin Schuh (austin.linux@gmail.com)'
12
13
14def 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 Schuh2054f5f2013-10-27 14:54:10 -070098class VelocityDrivetrainModel(control_loop.ControlLoop):
James Kuszmaule1755b32014-02-13 06:27:48 -080099 def __init__(self, left_low=True, right_low=True, name="VelocityDrivetrainModel"):
Austin Schuh2054f5f2013-10-27 14:54:10 -0700100 super(VelocityDrivetrainModel, self).__init__(name)
Austin Schuh03513cb2013-10-08 22:29:07 -0700101 self._drivetrain = drivetrain.Drivetrain(left_low=left_low,
James Kuszmaule1755b32014-02-13 06:27:48 -0800102 right_low=right_low)
Austin Schuh2054f5f2013-10-27 14:54:10 -0700103 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 Schuh03513cb2013-10-08 22:29:07 -0700107
Austin Schuh2054f5f2013-10-27 14:54:10 -0700108 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 Schuh03513cb2013-10-08 22:29:07 -0700116
117 # FF * X = U (steady state)
118 self.FF = self.B.I * (numpy.eye(2) - self.A)
119
Austin Schuh427b3702013-11-02 13:44:09 -0700120 self.PlaceControllerPoles([0.6, 0.6])
Austin Schuh2054f5f2013-10-27 14:54:10 -0700121 self.PlaceObserverPoles([0.02, 0.02])
Austin Schuh03513cb2013-10-08 22:29:07 -0700122
Austin Schuhe05d2c12013-10-12 00:08:31 -0700123 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 Schuh2054f5f2013-10-27 14:54:10 -0700130 self.U_max = self._drivetrain.U_max
131 self.U_min = self._drivetrain.U_min
132
Austin Schuh03513cb2013-10-08 22:29:07 -0700133
Austin Schuh048fb602013-10-07 23:31:04 -0700134class VelocityDrivetrain(object):
Austin Schuh427b3702013-11-02 13:44:09 -0700135 HIGH = 'high'
136 LOW = 'low'
137 SHIFTING_UP = 'up'
138 SHIFTING_DOWN = 'down'
139
James Kuszmaule1755b32014-02-13 06:27:48 -0800140 def __init__(self):
Austin Schuh427b3702013-11-02 13:44:09 -0700141 self.drivetrain_low_low = VelocityDrivetrainModel(
James Kuszmaule1755b32014-02-13 06:27:48 -0800142 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 Schuh048fb602013-10-07 23:31:04 -0700146
147 # X is [lvel, rvel]
148 self.X = numpy.matrix(
149 [[0.0],
150 [0.0]])
151
Austin Schuh048fb602013-10-07 23:31:04 -0700152 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 Schuh048fb602013-10-07 23:31:04 -0700169 self.dt = 0.01
170
171 self.R = numpy.matrix(
172 [[0.0],
173 [0.0]])
174
Austin Schuhe05d2c12013-10-12 00:08:31 -0700175 # 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 Schuh03513cb2013-10-08 22:29:07 -0700178 self.ttrust = 1.0
179
Austin Schuh427b3702013-11-02 13:44:09 -0700180 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 Schuh03513cb2013-10-08 22:29:07 -0700197
198 def CurrentDrivetrain(self):
Austin Schuh427b3702013-11-02 13:44:09 -0700199 if self.left_shifter_position > 0.5:
200 if self.right_shifter_position > 0.5:
Austin Schuh03513cb2013-10-08 22:29:07 -0700201 return self.drivetrain_high_high
202 else:
203 return self.drivetrain_high_low
204 else:
Austin Schuh427b3702013-11-02 13:44:09 -0700205 if self.right_shifter_position > 0.5:
Austin Schuh03513cb2013-10-08 22:29:07 -0700206 return self.drivetrain_low_high
207 else:
208 return self.drivetrain_low_low
Austin Schuh048fb602013-10-07 23:31:04 -0700209
Austin Schuh427b3702013-11-02 13:44:09 -0700210 def SimShifter(self, gear, shifter_position):
211 if gear is VelocityDrivetrain.HIGH or gear is VelocityDrivetrain.SHIFTING_UP:
Brian Silvermande8fd552013-11-03 15:53:42 -0800212 shifter_position = min(shifter_position + 0.5, 1.0)
Austin Schuh427b3702013-11-02 13:44:09 -0700213 else:
Brian Silvermande8fd552013-11-03 15:53:42 -0800214 shifter_position = max(shifter_position - 0.5, 0.0)
Austin Schuh427b3702013-11-02 13:44:09 -0700215
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 Schuhe05d2c12013-10-12 00:08:31 -0700223 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 Schuh427b3702013-11-02 13:44:09 -0700228 #print gear_name, "Motor Energy Difference.", 0.5 * 0.000140032647 * (low_omega * low_omega - high_omega * high_omega), "joules"
Austin Schuhe05d2c12013-10-12 00:08:31 -0700229 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 Schuh427b3702013-11-02 13:44:09 -0700235 #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 Schuhe05d2c12013-10-12 00:08:31 -0700239
Austin Schuh8afe35a2013-10-27 10:59:15 -0700240 # 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 Schuh427b3702013-11-02 13:44:09 -0700247 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 Schuhe05d2c12013-10-12 00:08:31 -0700264
Austin Schuhec00fc62013-10-12 00:31:49 -0700265 def FilterVelocity(self, throttle):
Austin Schuh048fb602013-10-07 23:31:04 -0700266 # 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 Schuhe05d2c12013-10-12 00:08:31 -0700268 # This math assumes that the left and right power and velocity are equal.
Austin Schuh048fb602013-10-07 23:31:04 -0700269
Austin Schuhe05d2c12013-10-12 00:08:31 -0700270 # 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 Schuh2054f5f2013-10-27 14:54:10 -0700275 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 Schuhe05d2c12013-10-12 00:08:31 -0700278 # Compute the FF sum for high gear.
Austin Schuh2054f5f2013-10-27 14:54:10 -0700279 high_min_FF_sum = self.drivetrain_high_high.FF[0, :].sum()
Austin Schuhe05d2c12013-10-12 00:08:31 -0700280
Austin Schuhec00fc62013-10-12 00:31:49 -0700281 # U = self.K[0, :].sum() * (R - x_avg) + self.FF[0, :].sum() * R
Austin Schuhe05d2c12013-10-12 00:08:31 -0700282 # throttle * 12.0 = (self.K[0, :].sum() + self.FF[0, :].sum()) * R
Austin Schuhec00fc62013-10-12 00:31:49 -0700283 # - self.K[0, :].sum() * x_avg
Austin Schuhe05d2c12013-10-12 00:08:31 -0700284
Austin Schuhec00fc62013-10-12 00:31:49 -0700285 # R = (throttle * 12.0 + self.K[0, :].sum() * x_avg) /
Austin Schuhe05d2c12013-10-12 00:08:31 -0700286 # (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 Schuh2054f5f2013-10-27 14:54:10 -0700291 # Scale throttle by min_FF_sum / high_min_FF_sum. This will make low gear
Austin Schuhe05d2c12013-10-12 00:08:31 -0700292 # 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 Schuh2054f5f2013-10-27 14:54:10 -0700294 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 Schuhec00fc62013-10-12 00:31:49 -0700297
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 Schuh427b3702013-11-02 13:44:09 -0700303 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 Schuhec00fc62013-10-12 00:31:49 -0700311
Austin Schuh427b3702013-11-02 13:44:09 -0700312 if self.IsInGear(self.right_gear):
313 self.right_cim.X[0, 0] = self.MotorRPM(self.right_shifter_position, self.X[0, 0])
Austin Schuhec00fc62013-10-12 00:31:49 -0700314
Austin Schuh427b3702013-11-02 13:44:09 -0700315 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 Schuhec00fc62013-10-12 00:31:49 -0700318
Austin Schuh427b3702013-11-02 13:44:09 -0700319 # 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 Schuh048fb602013-10-07 23:31:04 -0700323
Austin Schuh427b3702013-11-02 13:44:09 -0700324 # 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 Schuh048fb602013-10-07 23:31:04 -0700328
Austin Schuh427b3702013-11-02 13:44:09 -0700329 # (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 Schuh048fb602013-10-07 23:31:04 -0700336
Austin Schuh427b3702013-11-02 13:44:09 -0700337 equality_k = numpy.matrix(
338 [[1 + steering * numpy.sign(fvel), -(1 - steering * numpy.sign(fvel))]])
339 equality_w = 0.0
Austin Schuh048fb602013-10-07 23:31:04 -0700340
Austin Schuh427b3702013-11-02 13:44:09 -0700341 self.R[0, 0] = left_velocity
342 self.R[1, 0] = right_velocity
Austin Schuh048fb602013-10-07 23:31:04 -0700343
Austin Schuh427b3702013-11-02 13:44:09 -0700344 # 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 Schuh048fb602013-10-07 23:31:04 -0700352
Austin Schuh427b3702013-11-02 13:44:09 -0700353 # Limit R back inside the box.
354 self.boxed_R = CoerceGoal(R_poly, equality_k, equality_w, self.R)
Austin Schuh048fb602013-10-07 23:31:04 -0700355
Austin Schuh427b3702013-11-02 13:44:09 -0700356 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 Schuh048fb602013-10-07 23:31:04 -0700367
Austin Schuh427b3702013-11-02 13:44:09 -0700368 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 Schuh048fb602013-10-07 23:31:04 -0700375
376 self.U = numpy.clip(self.U_ideal, self.U_min, self.U_max)
Austin Schuh427b3702013-11-02 13:44:09 -0700377
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 Schuhe05d2c12013-10-12 00:08:31 -0700388 print "U is", self.U[0, 0], self.U[1, 0]
Austin Schuh427b3702013-11-02 13:44:09 -0700389 print "Left shifter", self.left_gear, self.left_shifter_position, "Right shifter", self.right_gear, self.right_shifter_position
Austin Schuh048fb602013-10-07 23:31:04 -0700390
391
392def main(argv):
James Kuszmaule1755b32014-02-13 06:27:48 -0800393 vdrivetrain = VelocityDrivetrain()
Austin Schuh048fb602013-10-07 23:31:04 -0700394
Brian Silverman2c590c32013-11-04 18:08:54 -0800395 if len(argv) != 7:
Austin Schuh2054f5f2013-10-27 14:54:10 -0700396 print "Expected .h file name and .cc file name"
397 else:
Brian Silverman2c590c32013-11-04 18:08:54 -0800398 dog_loop_writer = control_loop.ControlLoopWriter(
Austin Schuha25a0412014-03-09 00:50:04 -0800399 "VelocityDrivetrain", [vdrivetrain.drivetrain_low_low,
400 vdrivetrain.drivetrain_low_high,
401 vdrivetrain.drivetrain_high_low,
402 vdrivetrain.drivetrain_high_high])
Austin Schuh2054f5f2013-10-27 14:54:10 -0700403
404 if argv[1][-3:] == '.cc':
Brian Silverman2c590c32013-11-04 18:08:54 -0800405 dog_loop_writer.Write(argv[2], argv[1])
Austin Schuh2054f5f2013-10-27 14:54:10 -0700406 else:
Brian Silverman2c590c32013-11-04 18:08:54 -0800407 dog_loop_writer.Write(argv[1], argv[2])
408
Austin Schuh427b3702013-11-02 13:44:09 -0700409 cim_writer = control_loop.ControlLoopWriter(
410 "CIM", [drivetrain.CIM()])
411
Brian Silverman2c590c32013-11-04 18:08:54 -0800412 if argv[5][-3:] == '.cc':
413 cim_writer.Write(argv[6], argv[5])
Austin Schuh427b3702013-11-02 13:44:09 -0700414 else:
Brian Silverman2c590c32013-11-04 18:08:54 -0800415 cim_writer.Write(argv[5], argv[6])
Austin Schuh427b3702013-11-02 13:44:09 -0700416 return
Austin Schuh2054f5f2013-10-27 14:54:10 -0700417
Austin Schuh048fb602013-10-07 23:31:04 -0700418 vl_plot = []
419 vr_plot = []
420 ul_plot = []
421 ur_plot = []
422 radius_plot = []
423 t_plot = []
Austin Schuhe05d2c12013-10-12 00:08:31 -0700424 left_gear_plot = []
425 right_gear_plot = []
Austin Schuh427b3702013-11-02 13:44:09 -0700426 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 Schuh03513cb2013-10-08 22:29:07 -0700430
Austin Schuh8afe35a2013-10-27 10:59:15 -0700431 print "K is", vdrivetrain.CurrentDrivetrain().K
432
Austin Schuh427b3702013-11-02 13:44:09 -0700433 if vdrivetrain.left_gear is VelocityDrivetrain.HIGH:
Austin Schuhe05d2c12013-10-12 00:08:31 -0700434 print "Left is high"
435 else:
436 print "Left is low"
Austin Schuh427b3702013-11-02 13:44:09 -0700437 if vdrivetrain.right_gear is VelocityDrivetrain.HIGH:
Austin Schuhe05d2c12013-10-12 00:08:31 -0700438 print "Right is high"
439 else:
440 print "Right is low"
441
Austin Schuh427b3702013-11-02 13:44:09 -0700442 for t in numpy.arange(0, 4.0, vdrivetrain.dt):
Austin Schuhe05d2c12013-10-12 00:08:31 -0700443 if t < 1.0:
Brian Silvermande8fd552013-11-03 15:53:42 -0800444 vdrivetrain.Update(throttle=1.00, steering=0.0)
Austin Schuh427b3702013-11-02 13:44:09 -0700445 elif t < 1.2:
Brian Silvermande8fd552013-11-03 15:53:42 -0800446 vdrivetrain.Update(throttle=1.00, steering=0.0)
Austin Schuh048fb602013-10-07 23:31:04 -0700447 else:
Brian Silvermande8fd552013-11-03 15:53:42 -0800448 vdrivetrain.Update(throttle=1.00, steering=0.0)
Austin Schuh048fb602013-10-07 23:31:04 -0700449 t_plot.append(t)
Austin Schuh8afe35a2013-10-27 10:59:15 -0700450 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 Schuh427b3702013-11-02 13:44:09 -0700454 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 Schuh048fb602013-10-07 23:31:04 -0700456
Austin Schuh8afe35a2013-10-27 10:59:15 -0700457 fwd_velocity = (vdrivetrain.X[1, 0] + vdrivetrain.X[0, 0]) / 2
458 turn_velocity = (vdrivetrain.X[1, 0] - vdrivetrain.X[0, 0])
Austin Schuh427b3702013-11-02 13:44:09 -0700459 if abs(fwd_velocity) < 0.0000001:
Austin Schuh048fb602013-10-07 23:31:04 -0700460 radius_plot.append(turn_velocity)
461 else:
462 radius_plot.append(turn_velocity / fwd_velocity)
463
Austin Schuh8afe35a2013-10-27 10:59:15 -0700464 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 Schuh427b3702013-11-02 13:44:09 -0700475 #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 Schuh8afe35a2013-10-27 10:59:15 -0700479
Austin Schuh427b3702013-11-02 13:44:09 -0700480 # TODO(austin):
481 # Shifting compensation.
482
483 # Tighten the turn.
484 # Closed loop drive.
Austin Schuh8afe35a2013-10-27 10:59:15 -0700485
Austin Schuh048fb602013-10-07 23:31:04 -0700486 pylab.plot(t_plot, vl_plot, label='left velocity')
487 pylab.plot(t_plot, vr_plot, label='right velocity')
Austin Schuh8afe35a2013-10-27 10:59:15 -0700488 pylab.plot(t_plot, ul_plot, label='left voltage')
489 pylab.plot(t_plot, ur_plot, label='right voltage')
Austin Schuh048fb602013-10-07 23:31:04 -0700490 pylab.plot(t_plot, radius_plot, label='radius')
Austin Schuh8afe35a2013-10-27 10:59:15 -0700491 pylab.plot(t_plot, left_gear_plot, label='left gear high')
492 pylab.plot(t_plot, right_gear_plot, label='right gear high')
Austin Schuh048fb602013-10-07 23:31:04 -0700493 pylab.legend()
494 pylab.show()
495 return 0
496
497if __name__ == '__main__':
498 sys.exit(main(sys.argv))