blob: 5320145c5280642cb2c2f206e690086977a1dab3 [file] [log] [blame]
Austin Schuh048fb602013-10-07 23:31:04 -07001#!/usr/bin/python
2
3import numpy
4import sys
Austin Schuh70810b92016-11-26 14:55:34 -08005from frc971.control_loops.python import polytope
6from y2015.control_loops.python import drivetrain
7from frc971.control_loops.python import control_loop
8from frc971.control_loops.python import controls
Austin Schuh048fb602013-10-07 23:31:04 -07009from matplotlib import pylab
10
Austin Schuh70810b92016-11-26 14:55:34 -080011import gflags
12import glog
13
Austin Schuh048fb602013-10-07 23:31:04 -070014__author__ = 'Austin Schuh (austin.linux@gmail.com)'
15
Austin Schuh70810b92016-11-26 14:55:34 -080016FLAGS = gflags.FLAGS
17
18try:
19 gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.')
20except gflags.DuplicateFlagError:
21 pass
Austin Schuh048fb602013-10-07 23:31:04 -070022
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 """
Brian Silverman6dd2c532014-03-29 23:34:39 -070040 return DoCoerceGoal(region, K, w, R)[0]
Austin Schuh048fb602013-10-07 23:31:04 -070041
Brian Silverman6dd2c532014-03-29 23:34:39 -070042def DoCoerceGoal(region, K, w, R):
Austin Schuh048fb602013-10-07 23:31:04 -070043 if region.IsInside(R):
Brian Silverman6dd2c532014-03-29 23:34:39 -070044 return (R, True)
Austin Schuh048fb602013-10-07 23:31:04 -070045
46 perpendicular_vector = K.T / numpy.linalg.norm(K)
47 parallel_vector = numpy.matrix([[perpendicular_vector[1, 0]],
48 [-perpendicular_vector[0, 0]]])
Austin Schuh70810b92016-11-26 14:55:34 -080049
Austin Schuh048fb602013-10-07 23:31:04 -070050 # 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
Brian Silverman6dd2c532014-03-29 23:34:39 -070093 return (closest_point, True)
Austin Schuh048fb602013-10-07 23:31:04 -070094 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
Brian Silverman6dd2c532014-03-29 23:34:39 -0700106 return (closest_point, False)
Austin Schuh048fb602013-10-07 23:31:04 -0700107
108
Austin Schuh2054f5f2013-10-27 14:54:10 -0700109class VelocityDrivetrainModel(control_loop.ControlLoop):
James Kuszmaule1755b32014-02-13 06:27:48 -0800110 def __init__(self, left_low=True, right_low=True, name="VelocityDrivetrainModel"):
Austin Schuh2054f5f2013-10-27 14:54:10 -0700111 super(VelocityDrivetrainModel, self).__init__(name)
Austin Schuh03513cb2013-10-08 22:29:07 -0700112 self._drivetrain = drivetrain.Drivetrain(left_low=left_low,
James Kuszmaule1755b32014-02-13 06:27:48 -0800113 right_low=right_low)
Austin Schuh2054f5f2013-10-27 14:54:10 -0700114 self.dt = 0.01
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]]])
Austin Schuh03513cb2013-10-08 22:29:07 -0700118
Austin Schuh2054f5f2013-10-27 14:54:10 -0700119 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 Silverman4e55e582015-11-10 14:16:37 -0500122 self.C = numpy.matrix(numpy.eye(2))
123 self.D = numpy.matrix(numpy.zeros((2, 2)))
Austin Schuh2054f5f2013-10-27 14:54:10 -0700124
125 self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
126 self.B_continuous, self.dt)
Austin Schuh03513cb2013-10-08 22:29:07 -0700127
128 # FF * X = U (steady state)
129 self.FF = self.B.I * (numpy.eye(2) - self.A)
130
Austin Schuh427b3702013-11-02 13:44:09 -0700131 self.PlaceControllerPoles([0.6, 0.6])
Austin Schuh2054f5f2013-10-27 14:54:10 -0700132 self.PlaceObserverPoles([0.02, 0.02])
Austin Schuh03513cb2013-10-08 22:29:07 -0700133
Austin Schuhe05d2c12013-10-12 00:08:31 -0700134 self.G_high = self._drivetrain.G_high
135 self.G_low = self._drivetrain.G_low
Austin Schuh70810b92016-11-26 14:55:34 -0800136 self.resistance = self._drivetrain.resistance
Austin Schuhe05d2c12013-10-12 00:08:31 -0700137 self.r = self._drivetrain.r
138 self.Kv = self._drivetrain.Kv
139 self.Kt = self._drivetrain.Kt
140
Austin Schuh2054f5f2013-10-27 14:54:10 -0700141 self.U_max = self._drivetrain.U_max
142 self.U_min = self._drivetrain.U_min
143
Austin Schuh03513cb2013-10-08 22:29:07 -0700144
Austin Schuh048fb602013-10-07 23:31:04 -0700145class VelocityDrivetrain(object):
Austin Schuh427b3702013-11-02 13:44:09 -0700146 HIGH = 'high'
147 LOW = 'low'
148 SHIFTING_UP = 'up'
149 SHIFTING_DOWN = 'down'
150
James Kuszmaule1755b32014-02-13 06:27:48 -0800151 def __init__(self):
Austin Schuh427b3702013-11-02 13:44:09 -0700152 self.drivetrain_low_low = VelocityDrivetrainModel(
James Kuszmaule1755b32014-02-13 06:27:48 -0800153 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')
Austin Schuh048fb602013-10-07 23:31:04 -0700157
158 # X is [lvel, rvel]
159 self.X = numpy.matrix(
160 [[0.0],
161 [0.0]])
162
Austin Schuh048fb602013-10-07 23:31:04 -0700163 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 Schuh048fb602013-10-07 23:31:04 -0700180 self.dt = 0.01
181
182 self.R = numpy.matrix(
183 [[0.0],
184 [0.0]])
185
Austin Schuh70810b92016-11-26 14:55:34 -0800186 self.U_ideal = numpy.matrix(
187 [[0.0],
188 [0.0]])
189
Austin Schuhe05d2c12013-10-12 00:08:31 -0700190 # 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.
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700193 self.ttrust = 1.1
Austin Schuh03513cb2013-10-08 22:29:07 -0700194
Austin Schuh427b3702013-11-02 13:44:09 -0700195 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)
Austin Schuh03513cb2013-10-08 22:29:07 -0700212
213 def CurrentDrivetrain(self):
Austin Schuh427b3702013-11-02 13:44:09 -0700214 if self.left_shifter_position > 0.5:
215 if self.right_shifter_position > 0.5:
Austin Schuh03513cb2013-10-08 22:29:07 -0700216 return self.drivetrain_high_high
217 else:
218 return self.drivetrain_high_low
219 else:
Austin Schuh427b3702013-11-02 13:44:09 -0700220 if self.right_shifter_position > 0.5:
Austin Schuh03513cb2013-10-08 22:29:07 -0700221 return self.drivetrain_low_high
222 else:
223 return self.drivetrain_low_low
Austin Schuh048fb602013-10-07 23:31:04 -0700224
Austin Schuh427b3702013-11-02 13:44:09 -0700225 def SimShifter(self, gear, shifter_position):
226 if gear is VelocityDrivetrain.HIGH or gear is VelocityDrivetrain.SHIFTING_UP:
Brian Silvermande8fd552013-11-03 15:53:42 -0800227 shifter_position = min(shifter_position + 0.5, 1.0)
Austin Schuh427b3702013-11-02 13:44:09 -0700228 else:
Brian Silvermande8fd552013-11-03 15:53:42 -0800229 shifter_position = max(shifter_position - 0.5, 0.0)
Austin Schuh427b3702013-11-02 13:44:09 -0700230
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
Austin Schuhe05d2c12013-10-12 00:08:31 -0700238 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)
Austin Schuh427b3702013-11-02 13:44:09 -0700243 #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 -0700244 high_torque = ((12.0 - high_omega / self.CurrentDrivetrain().Kv) *
Austin Schuh70810b92016-11-26 14:55:34 -0800245 self.CurrentDrivetrain().Kt / self.CurrentDrivetrain().resistance)
Austin Schuhe05d2c12013-10-12 00:08:31 -0700246 low_torque = ((12.0 - low_omega / self.CurrentDrivetrain().Kv) *
Austin Schuh70810b92016-11-26 14:55:34 -0800247 self.CurrentDrivetrain().Kt / self.CurrentDrivetrain().resistance)
Austin Schuhe05d2c12013-10-12 00:08:31 -0700248 high_power = high_torque * high_omega
249 low_power = low_torque * low_omega
Austin Schuh427b3702013-11-02 13:44:09 -0700250 #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
Austin Schuhe05d2c12013-10-12 00:08:31 -0700254
Austin Schuh8afe35a2013-10-27 10:59:15 -0700255 # 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.
Austin Schuh427b3702013-11-02 13:44:09 -0700262 goal_gear_is_high = high_power > low_power
263 #goal_gear_is_high = True
264
265 if not self.IsInGear(current_gear):
Austin Schuh70810b92016-11-26 14:55:34 -0800266 glog.debug('%s Not in gear.', gear_name)
Austin Schuh427b3702013-11-02 13:44:09 -0700267 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:
Austin Schuh70810b92016-11-26 14:55:34 -0800272 glog.debug('%s Shifting up.', gear_name)
Austin Schuh427b3702013-11-02 13:44:09 -0700273 return VelocityDrivetrain.SHIFTING_UP
274 else:
Austin Schuh70810b92016-11-26 14:55:34 -0800275 glog.debug('%s Shifting down.', gear_name)
Austin Schuh427b3702013-11-02 13:44:09 -0700276 return VelocityDrivetrain.SHIFTING_DOWN
277 else:
278 return current_gear
Austin Schuhe05d2c12013-10-12 00:08:31 -0700279
Austin Schuhec00fc62013-10-12 00:31:49 -0700280 def FilterVelocity(self, throttle):
Austin Schuh048fb602013-10-07 23:31:04 -0700281 # 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.
Austin Schuhe05d2c12013-10-12 00:08:31 -0700283 # This math assumes that the left and right power and velocity are equal.
Austin Schuh048fb602013-10-07 23:31:04 -0700284
Austin Schuhe05d2c12013-10-12 00:08:31 -0700285 # 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)
Austin Schuh2054f5f2013-10-27 14:54:10 -0700290 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()
Austin Schuhe05d2c12013-10-12 00:08:31 -0700293 # Compute the FF sum for high gear.
Austin Schuh2054f5f2013-10-27 14:54:10 -0700294 high_min_FF_sum = self.drivetrain_high_high.FF[0, :].sum()
Austin Schuhe05d2c12013-10-12 00:08:31 -0700295
Austin Schuhec00fc62013-10-12 00:31:49 -0700296 # U = self.K[0, :].sum() * (R - x_avg) + self.FF[0, :].sum() * R
Austin Schuhe05d2c12013-10-12 00:08:31 -0700297 # throttle * 12.0 = (self.K[0, :].sum() + self.FF[0, :].sum()) * R
Austin Schuhec00fc62013-10-12 00:31:49 -0700298 # - self.K[0, :].sum() * x_avg
Austin Schuhe05d2c12013-10-12 00:08:31 -0700299
Austin Schuhec00fc62013-10-12 00:31:49 -0700300 # R = (throttle * 12.0 + self.K[0, :].sum() * x_avg) /
Austin Schuhe05d2c12013-10-12 00:08:31 -0700301 # (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
Austin Schuh2054f5f2013-10-27 14:54:10 -0700306 # Scale throttle by min_FF_sum / high_min_FF_sum. This will make low gear
Austin Schuhe05d2c12013-10-12 00:08:31 -0700307 # 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.
Austin Schuh2054f5f2013-10-27 14:54:10 -0700309 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))
Austin Schuhec00fc62013-10-12 00:31:49 -0700312
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
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700318 self.left_gear = self.right_gear = True
319 if False:
320 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])
Austin Schuhec00fc62013-10-12 00:31:49 -0700328
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700329 if self.IsInGear(self.right_gear):
330 self.right_cim.X[0, 0] = self.MotorRPM(self.right_shifter_position, self.X[0, 0])
Austin Schuhec00fc62013-10-12 00:31:49 -0700331
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700332 steering *= 2.3
333 if True or self.IsInGear(self.left_gear) and self.IsInGear(self.right_gear):
Austin Schuh427b3702013-11-02 13:44:09 -0700334 # Filter the throttle to provide a nicer response.
335 fvel = self.FilterVelocity(throttle)
Austin Schuhec00fc62013-10-12 00:31:49 -0700336
Austin Schuh427b3702013-11-02 13:44:09 -0700337 # Constant radius means that angualar_velocity / linear_velocity = constant.
338 # Compute the left and right velocities.
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700339 steering_velocity = numpy.abs(fvel) * steering
340 left_velocity = fvel - steering_velocity
341 right_velocity = fvel + steering_velocity
Austin Schuh048fb602013-10-07 23:31:04 -0700342
Austin Schuh427b3702013-11-02 13:44:09 -0700343 # Write this constraint in the form of K * R = w
344 # angular velocity / linear velocity = constant
345 # (left - right) / (left + right) = constant
346 # left - right = constant * left + constant * right
Austin Schuh048fb602013-10-07 23:31:04 -0700347
Austin Schuh427b3702013-11-02 13:44:09 -0700348 # (fvel - steering * numpy.abs(fvel) - fvel - steering * numpy.abs(fvel)) /
349 # (fvel - steering * numpy.abs(fvel) + fvel + steering * numpy.abs(fvel)) =
350 # constant
351 # (- 2 * steering * numpy.abs(fvel)) / (2 * fvel) = constant
352 # (-steering * sign(fvel)) = constant
353 # (-steering * sign(fvel)) * (left + right) = left - right
354 # (steering * sign(fvel) + 1) * left + (steering * sign(fvel) - 1) * right = 0
Austin Schuh048fb602013-10-07 23:31:04 -0700355
Austin Schuh427b3702013-11-02 13:44:09 -0700356 equality_k = numpy.matrix(
357 [[1 + steering * numpy.sign(fvel), -(1 - steering * numpy.sign(fvel))]])
358 equality_w = 0.0
Austin Schuh048fb602013-10-07 23:31:04 -0700359
Austin Schuh427b3702013-11-02 13:44:09 -0700360 self.R[0, 0] = left_velocity
361 self.R[1, 0] = right_velocity
Austin Schuh048fb602013-10-07 23:31:04 -0700362
Austin Schuh427b3702013-11-02 13:44:09 -0700363 # Construct a constraint on R by manipulating the constraint on U
364 # Start out with H * U <= k
365 # U = FF * R + K * (R - X)
366 # H * (FF * R + K * R - K * X) <= k
367 # H * (FF + K) * R <= k + H * K * X
368 R_poly = polytope.HPolytope(
369 self.U_poly.H * (self.CurrentDrivetrain().K + self.CurrentDrivetrain().FF),
370 self.U_poly.k + self.U_poly.H * self.CurrentDrivetrain().K * self.X)
Austin Schuh048fb602013-10-07 23:31:04 -0700371
Austin Schuh427b3702013-11-02 13:44:09 -0700372 # Limit R back inside the box.
373 self.boxed_R = CoerceGoal(R_poly, equality_k, equality_w, self.R)
Austin Schuh048fb602013-10-07 23:31:04 -0700374
Austin Schuh427b3702013-11-02 13:44:09 -0700375 FF_volts = self.CurrentDrivetrain().FF * self.boxed_R
376 self.U_ideal = self.CurrentDrivetrain().K * (self.boxed_R - self.X) + FF_volts
377 else:
Austin Schuh70810b92016-11-26 14:55:34 -0800378 glog.debug('Not all in gear')
Austin Schuh427b3702013-11-02 13:44:09 -0700379 if not self.IsInGear(self.left_gear) and not self.IsInGear(self.right_gear):
380 # TODO(austin): Use battery volts here.
381 R_left = self.MotorRPM(self.left_shifter_position, self.X[0, 0])
382 self.U_ideal[0, 0] = numpy.clip(
383 self.left_cim.K * (R_left - self.left_cim.X) + R_left / self.left_cim.Kv,
384 self.left_cim.U_min, self.left_cim.U_max)
385 self.left_cim.Update(self.U_ideal[0, 0])
Austin Schuh048fb602013-10-07 23:31:04 -0700386
Austin Schuh427b3702013-11-02 13:44:09 -0700387 R_right = self.MotorRPM(self.right_shifter_position, self.X[1, 0])
388 self.U_ideal[1, 0] = numpy.clip(
389 self.right_cim.K * (R_right - self.right_cim.X) + R_right / self.right_cim.Kv,
390 self.right_cim.U_min, self.right_cim.U_max)
391 self.right_cim.Update(self.U_ideal[1, 0])
392 else:
393 assert False
Austin Schuh048fb602013-10-07 23:31:04 -0700394
395 self.U = numpy.clip(self.U_ideal, self.U_min, self.U_max)
Austin Schuh427b3702013-11-02 13:44:09 -0700396
397 # TODO(austin): Model the robot as not accelerating when you shift...
398 # This hack only works when you shift at the same time.
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700399 if True or self.IsInGear(self.left_gear) and self.IsInGear(self.right_gear):
Austin Schuh427b3702013-11-02 13:44:09 -0700400 self.X = self.CurrentDrivetrain().A * self.X + self.CurrentDrivetrain().B * self.U
401
402 self.left_gear, self.left_shifter_position = self.SimShifter(
403 self.left_gear, self.left_shifter_position)
404 self.right_gear, self.right_shifter_position = self.SimShifter(
405 self.right_gear, self.right_shifter_position)
406
Austin Schuh70810b92016-11-26 14:55:34 -0800407 glog.debug('U is %s %s', str(self.U[0, 0]), str(self.U[1, 0]))
408 glog.debug('Left shifter %s %d Right shifter %s %d',
409 self.left_gear, self.left_shifter_position,
410 self.right_gear, self.right_shifter_position)
Austin Schuh048fb602013-10-07 23:31:04 -0700411
412
413def main(argv):
Austin Schuh70810b92016-11-26 14:55:34 -0800414 argv = FLAGS(argv)
415
James Kuszmaule1755b32014-02-13 06:27:48 -0800416 vdrivetrain = VelocityDrivetrain()
Austin Schuh048fb602013-10-07 23:31:04 -0700417
Austin Schuh70810b92016-11-26 14:55:34 -0800418 if not FLAGS.plot:
419 if len(argv) != 5:
420 glog.fatal('Expected .h file name and .cc file name')
Austin Schuh2054f5f2013-10-27 14:54:10 -0700421 else:
Austin Schuh70810b92016-11-26 14:55:34 -0800422 namespaces = ['y2015', 'control_loops', 'drivetrain']
423 dog_loop_writer = control_loop.ControlLoopWriter(
424 "VelocityDrivetrain", [vdrivetrain.drivetrain_low_low,
425 vdrivetrain.drivetrain_low_high,
426 vdrivetrain.drivetrain_high_low,
427 vdrivetrain.drivetrain_high_high],
428 namespaces=namespaces)
429
Brian Silverman2c590c32013-11-04 18:08:54 -0800430 dog_loop_writer.Write(argv[1], argv[2])
431
Austin Schuh70810b92016-11-26 14:55:34 -0800432 cim_writer = control_loop.ControlLoopWriter(
433 "CIM", [drivetrain.CIM()])
Austin Schuh427b3702013-11-02 13:44:09 -0700434
Austin Schuh70810b92016-11-26 14:55:34 -0800435 cim_writer.Write(argv[3], argv[4])
436 return
Austin Schuh2054f5f2013-10-27 14:54:10 -0700437
Austin Schuh048fb602013-10-07 23:31:04 -0700438 vl_plot = []
439 vr_plot = []
440 ul_plot = []
441 ur_plot = []
442 radius_plot = []
443 t_plot = []
Austin Schuhe05d2c12013-10-12 00:08:31 -0700444 left_gear_plot = []
445 right_gear_plot = []
Austin Schuh427b3702013-11-02 13:44:09 -0700446 vdrivetrain.left_shifter_position = 0.0
447 vdrivetrain.right_shifter_position = 0.0
448 vdrivetrain.left_gear = VelocityDrivetrain.LOW
449 vdrivetrain.right_gear = VelocityDrivetrain.LOW
Austin Schuh03513cb2013-10-08 22:29:07 -0700450
Austin Schuh70810b92016-11-26 14:55:34 -0800451 glog.debug('K is %s', str(vdrivetrain.CurrentDrivetrain().K))
Austin Schuh8afe35a2013-10-27 10:59:15 -0700452
Austin Schuh427b3702013-11-02 13:44:09 -0700453 if vdrivetrain.left_gear is VelocityDrivetrain.HIGH:
Austin Schuh70810b92016-11-26 14:55:34 -0800454 glog.debug('Left is high')
Austin Schuhe05d2c12013-10-12 00:08:31 -0700455 else:
Austin Schuh70810b92016-11-26 14:55:34 -0800456 glog.debug('Left is low')
Austin Schuh427b3702013-11-02 13:44:09 -0700457 if vdrivetrain.right_gear is VelocityDrivetrain.HIGH:
Austin Schuh70810b92016-11-26 14:55:34 -0800458 glog.debug('Right is high')
Austin Schuhe05d2c12013-10-12 00:08:31 -0700459 else:
Austin Schuh70810b92016-11-26 14:55:34 -0800460 glog.debug('Right is low')
Austin Schuhe05d2c12013-10-12 00:08:31 -0700461
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700462 for t in numpy.arange(0, 1.7, vdrivetrain.dt):
463 if t < 0.5:
464 vdrivetrain.Update(throttle=0.00, steering=1.0)
Austin Schuh427b3702013-11-02 13:44:09 -0700465 elif t < 1.2:
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700466 vdrivetrain.Update(throttle=0.5, steering=1.0)
Austin Schuh048fb602013-10-07 23:31:04 -0700467 else:
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700468 vdrivetrain.Update(throttle=0.00, steering=1.0)
Austin Schuh048fb602013-10-07 23:31:04 -0700469 t_plot.append(t)
Austin Schuh8afe35a2013-10-27 10:59:15 -0700470 vl_plot.append(vdrivetrain.X[0, 0])
471 vr_plot.append(vdrivetrain.X[1, 0])
472 ul_plot.append(vdrivetrain.U[0, 0])
473 ur_plot.append(vdrivetrain.U[1, 0])
Austin Schuh427b3702013-11-02 13:44:09 -0700474 left_gear_plot.append((vdrivetrain.left_gear is VelocityDrivetrain.HIGH) * 2.0 - 10.0)
475 right_gear_plot.append((vdrivetrain.right_gear is VelocityDrivetrain.HIGH) * 2.0 - 10.0)
Austin Schuh048fb602013-10-07 23:31:04 -0700476
Austin Schuh8afe35a2013-10-27 10:59:15 -0700477 fwd_velocity = (vdrivetrain.X[1, 0] + vdrivetrain.X[0, 0]) / 2
478 turn_velocity = (vdrivetrain.X[1, 0] - vdrivetrain.X[0, 0])
Austin Schuh427b3702013-11-02 13:44:09 -0700479 if abs(fwd_velocity) < 0.0000001:
Austin Schuh048fb602013-10-07 23:31:04 -0700480 radius_plot.append(turn_velocity)
481 else:
482 radius_plot.append(turn_velocity / fwd_velocity)
483
Austin Schuh8afe35a2013-10-27 10:59:15 -0700484 cim_velocity_plot = []
485 cim_voltage_plot = []
486 cim_time = []
487 cim = drivetrain.CIM()
488 R = numpy.matrix([[300]])
489 for t in numpy.arange(0, 0.5, cim.dt):
490 U = numpy.clip(cim.K * (R - cim.X) + R / cim.Kv, cim.U_min, cim.U_max)
491 cim.Update(U)
492 cim_velocity_plot.append(cim.X[0, 0])
493 cim_voltage_plot.append(U[0, 0] * 10)
494 cim_time.append(t)
Brian Silverman4b36e2e2015-03-16 15:46:53 -0700495 pylab.plot(cim_time, cim_velocity_plot, label='cim spinup')
496 pylab.plot(cim_time, cim_voltage_plot, label='cim voltage')
497 pylab.legend()
498 pylab.show()
Austin Schuh8afe35a2013-10-27 10:59:15 -0700499
Austin Schuh427b3702013-11-02 13:44:09 -0700500 # TODO(austin):
501 # Shifting compensation.
502
503 # Tighten the turn.
504 # Closed loop drive.
Austin Schuh8afe35a2013-10-27 10:59:15 -0700505
Austin Schuh048fb602013-10-07 23:31:04 -0700506 pylab.plot(t_plot, vl_plot, label='left velocity')
507 pylab.plot(t_plot, vr_plot, label='right velocity')
Austin Schuh8afe35a2013-10-27 10:59:15 -0700508 pylab.plot(t_plot, ul_plot, label='left voltage')
509 pylab.plot(t_plot, ur_plot, label='right voltage')
Austin Schuh048fb602013-10-07 23:31:04 -0700510 pylab.plot(t_plot, radius_plot, label='radius')
Austin Schuh8afe35a2013-10-27 10:59:15 -0700511 pylab.plot(t_plot, left_gear_plot, label='left gear high')
512 pylab.plot(t_plot, right_gear_plot, label='right gear high')
Austin Schuh048fb602013-10-07 23:31:04 -0700513 pylab.legend()
514 pylab.show()
515 return 0
516
517if __name__ == '__main__':
518 sys.exit(main(sys.argv))