blob: 00b86b591d24a76c36b6b4e7aeb380a9c1437992 [file] [log] [blame]
Austin Schuh048fb602013-10-07 23:31:04 -07001#!/usr/bin/python
2
3import numpy
4import sys
5import polytope
6import drivetrain
7import controls
8from matplotlib import pylab
9
10__author__ = 'Austin Schuh (austin.linux@gmail.com)'
11
12
13def 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 Schuh03513cb2013-10-08 22:29:07 -070097class 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
114
Austin Schuh048fb602013-10-07 23:31:04 -0700115class VelocityDrivetrain(object):
116 def __init__(self):
Austin Schuh03513cb2013-10-08 22:29:07 -0700117 self.drivetrain_low_low = VelocityDrivetrainModel(left_low=True, right_low=True)
118 self.drivetrain_low_high = VelocityDrivetrainModel(left_low=True, right_low=False)
119 self.drivetrain_high_low = VelocityDrivetrainModel(left_low=False, right_low=True)
120 self.drivetrain_high_high = VelocityDrivetrainModel(left_low=False, right_low=False)
Austin Schuh048fb602013-10-07 23:31:04 -0700121
122 # X is [lvel, rvel]
123 self.X = numpy.matrix(
124 [[0.0],
125 [0.0]])
126
Austin Schuh048fb602013-10-07 23:31:04 -0700127 self.U_poly = polytope.HPolytope(
128 numpy.matrix([[1, 0],
129 [-1, 0],
130 [0, 1],
131 [0, -1]]),
132 numpy.matrix([[12],
133 [12],
134 [12],
135 [12]]))
136
137 self.U_max = numpy.matrix(
138 [[12.0],
139 [12.0]])
140 self.U_min = numpy.matrix(
141 [[-12.0000000000],
142 [-12.0000000000]])
143
Austin Schuh048fb602013-10-07 23:31:04 -0700144 self.dt = 0.01
145
146 self.R = numpy.matrix(
147 [[0.0],
148 [0.0]])
149
Austin Schuh048fb602013-10-07 23:31:04 -0700150 self.xfiltered = 0.0
151
152 # U = self.K[0, :].sum() * (R - xfiltered) + self.FF[0, :].sum() * R
153 # throttle * 12.0 = (self.K[0, :].sum() + self.FF[0, :].sum()) * R
154 # - self.K[0, :].sum() * xfiltered
155
156 # R = (throttle * 12.0 + self.K[0, :].sum() * xfiltered) /
157 # (self.K[0, :].sum() + self.FF[0, :].sum())
158
159 # U = (K + FF) * R - K * X
160 # (K + FF) ^-1 * (U + K * X) = R
161
162 # (K + FF) ^-1 * (throttle * 12.0 + K * throttle * self.vmax) = R
163 # Xn+1 = A * X + B * (throttle * 12.0)
164
165 # xfiltered = self.A[0, :].sum() + B[0, :].sum() * throttle * 12.0
Austin Schuh03513cb2013-10-08 22:29:07 -0700166 self.ttrust = 1.0
167
168 self.left_high = False
169 self.right_high = False
170
171 def CurrentDrivetrain(self):
172 if self.left_high:
173 if self.right_high:
174 return self.drivetrain_high_high
175 else:
176 return self.drivetrain_high_low
177 else:
178 if self.right_high:
179 return self.drivetrain_low_high
180 else:
181 return self.drivetrain_low_low
Austin Schuh048fb602013-10-07 23:31:04 -0700182
183 def Update(self, throttle, steering):
184 # Invert the plant to figure out how the velocity filter would have to work
185 # out in order to filter out the forwards negative inertia.
186 # This math assumes that the left and right power and velocity are equals,
187 # and that the plant is the same on the left and right.
Austin Schuh048fb602013-10-07 23:31:04 -0700188
Austin Schuh03513cb2013-10-08 22:29:07 -0700189 # TODO(aschuh): Rederive this assuming G_left != G_right.
190 # TODO(aschuh): Figure out the correct throttle if we start in low gear and
191 # then let it shift up. This isn't it.
192 fvel = ((throttle * 12.0 + self.ttrust * self.CurrentDrivetrain().K[0, :].sum() * self.xfiltered)
193 / (self.ttrust * self.CurrentDrivetrain().K[0, :].sum() + self.CurrentDrivetrain().FF[0, :].sum()))
194 self.xfiltered = (self.CurrentDrivetrain().A[0, :].sum() * self.xfiltered +
195 self.CurrentDrivetrain().B[0, :].sum() * throttle * 12.0)
Austin Schuh048fb602013-10-07 23:31:04 -0700196
197 # Constant radius means that angualar_velocity / linear_velocity = constant.
198 # Compute the left and right velocities.
199 left_velocity = fvel - steering * numpy.abs(fvel)
200 right_velocity = fvel + steering * numpy.abs(fvel)
201
202 # Write this constraint in the form of K * R = w
203 # angular velocity / linear velocity = constant
204 # (left - right) / (left + right) = constant
205 # left - right = constant * left + constant * right
206
207 # (fvel - steering * numpy.abs(fvel) - fvel - steering * numpy.abs(fvel)) /
208 # (fvel - steering * numpy.abs(fvel) + fvel + steering * numpy.abs(fvel)) =
209 # constant
210 # (- 2 * steering * numpy.abs(fvel)) / (2 * fvel) = constant
211 # (-steering * sign(fvel)) = constant
212 # (-steering * sign(fvel)) * (left + right) = left - right
213 # (steering * sign(fvel) + 1) * left + (steering * sign(fvel) - 1) * right = 0
214
215 equality_k = numpy.matrix(
216 [[1 + steering * numpy.sign(fvel), -(1 - steering * numpy.sign(fvel))]])
217 equality_w = 0.0
218
219 self.R[0, 0] = left_velocity
220 self.R[1, 0] = right_velocity
221
222 # Construct a constraint on R by manipulating the constraint on U
223 # Start out with H * U <= k
224 # U = FF * R + K * (R - X)
225 # H * (FF * R + K * R - K * X) <= k
226 # H * (FF + K) * R <= k + H * K * X
227 R_poly = polytope.HPolytope(
Austin Schuh03513cb2013-10-08 22:29:07 -0700228 self.U_poly.H * (self.CurrentDrivetrain().K + self.CurrentDrivetrain().FF),
229 self.U_poly.k + self.U_poly.H * self.CurrentDrivetrain().K * self.X)
Austin Schuh048fb602013-10-07 23:31:04 -0700230
231 # Limit R back inside the box.
232 self.boxed_R = CoerceGoal(R_poly, equality_k, equality_w, self.R)
233
Austin Schuh03513cb2013-10-08 22:29:07 -0700234 FF_volts = self.CurrentDrivetrain().FF * self.boxed_R
235 self.U_ideal = self.CurrentDrivetrain().K * (self.boxed_R - self.X) + FF_volts
Austin Schuh048fb602013-10-07 23:31:04 -0700236
237 self.U = numpy.clip(self.U_ideal, self.U_min, self.U_max)
Austin Schuh03513cb2013-10-08 22:29:07 -0700238 self.X = self.CurrentDrivetrain().A * self.X + self.CurrentDrivetrain().B * self.U
Austin Schuh048fb602013-10-07 23:31:04 -0700239
240
241def main(argv):
242 drivetrain = VelocityDrivetrain()
243
244 vl_plot = []
245 vr_plot = []
246 ul_plot = []
247 ur_plot = []
248 radius_plot = []
249 t_plot = []
250 for t in numpy.arange(0, 1.5, drivetrain.dt):
251 if t < 0.5:
252 drivetrain.Update(throttle=0.60, steering=0.3)
253 elif t < 1.0:
Austin Schuh03513cb2013-10-08 22:29:07 -0700254 if t > 0.7:
255 drivetrain.left_high = False
256 drivetrain.right_high = True
257
Austin Schuh048fb602013-10-07 23:31:04 -0700258 drivetrain.Update(throttle=0.60, steering=-0.3)
259 else:
260 drivetrain.Update(throttle=0.00, steering=0.3)
261 t_plot.append(t)
262 vl_plot.append(drivetrain.X[0, 0])
263 vr_plot.append(drivetrain.X[1, 0])
264 ul_plot.append(drivetrain.U[0, 0])
265 ur_plot.append(drivetrain.U[1, 0])
266
267 fwd_velocity = (drivetrain.X[1, 0] + drivetrain.X[0, 0]) / 2
268 turn_velocity = (drivetrain.X[1, 0] - drivetrain.X[0, 0])
269 if fwd_velocity < 0.0000001:
270 radius_plot.append(turn_velocity)
271 else:
272 radius_plot.append(turn_velocity / fwd_velocity)
273
274 pylab.plot(t_plot, vl_plot, label='left velocity')
275 pylab.plot(t_plot, vr_plot, label='right velocity')
276 pylab.plot(t_plot, ul_plot, label='left power')
277 pylab.plot(t_plot, ur_plot, label='right power')
278 pylab.plot(t_plot, radius_plot, label='radius')
279 pylab.legend()
280 pylab.show()
281 return 0
282
283if __name__ == '__main__':
284 sys.exit(main(sys.argv))