blob: 77fd905a89093814ceb1ed4986a19c671c916640 [file] [log] [blame]
Austin Schuh3c542312013-02-24 01:53:50 -08001import controls
2import numpy
Austin Schuh572ff402015-11-08 12:17:50 -08003import os
Austin Schuh3c542312013-02-24 01:53:50 -08004
Ben Fredrickson1b45f782014-02-23 07:44:36 +00005class Constant(object):
Austin Schuh1a387962015-01-31 16:36:20 -08006 def __init__ (self, name, formatt, value):
7 self.name = name
8 self.formatt = formatt
9 self.value = value
10 self.formatToType = {}
Brian Silverman4e55e582015-11-10 14:16:37 -050011 self.formatToType['%f'] = "double"
12 self.formatToType['%d'] = "int"
Austin Schuh1a387962015-01-31 16:36:20 -080013 def __str__ (self):
14 return str("\nstatic constexpr %s %s = "+ self.formatt +";\n") % \
15 (self.formatToType[self.formatt], self.name, self.value)
Ben Fredrickson1b45f782014-02-23 07:44:36 +000016
17
Austin Schuhe3490622013-03-13 01:24:30 -070018class ControlLoopWriter(object):
Ben Fredrickson1b45f782014-02-23 07:44:36 +000019 def __init__(self, gain_schedule_name, loops, namespaces=None, write_constants=False):
Austin Schuhe3490622013-03-13 01:24:30 -070020 """Constructs a control loop writer.
21
22 Args:
23 gain_schedule_name: string, Name of the overall controller.
24 loops: array[ControlLoop], a list of control loops to gain schedule
25 in order.
26 namespaces: array[string], a list of names of namespaces to nest in
27 order. If None, the default will be used.
28 """
29 self._gain_schedule_name = gain_schedule_name
30 self._loops = loops
31 if namespaces:
32 self._namespaces = namespaces
33 else:
34 self._namespaces = ['frc971', 'control_loops']
35
36 self._namespace_start = '\n'.join(
37 ['namespace %s {' % name for name in self._namespaces])
38
39 self._namespace_end = '\n'.join(
40 ['} // namespace %s' % name for name in reversed(self._namespaces)])
Austin Schuh86093ad2016-02-06 14:29:34 -080041
Ben Fredrickson1b45f782014-02-23 07:44:36 +000042 self._constant_list = []
Austin Schuh25933852014-02-23 02:04:13 -080043
44 def AddConstant(self, constant):
45 """Adds a constant to write.
46
47 Args:
48 constant: Constant, the constant to add to the header.
49 """
50 self._constant_list.append(constant)
Austin Schuhe3490622013-03-13 01:24:30 -070051
Brian Silvermane51ad632014-01-08 15:12:29 -080052 def _TopDirectory(self):
53 return self._namespaces[0]
54
Austin Schuhe3490622013-03-13 01:24:30 -070055 def _HeaderGuard(self, header_file):
Austin Schuh16cf47a2015-11-28 13:20:33 -080056 return ('_'.join([namespace.upper() for namespace in self._namespaces]) + '_' +
Austin Schuh572ff402015-11-08 12:17:50 -080057 os.path.basename(header_file).upper()
58 .replace('.', '_').replace('/', '_') + '_')
Austin Schuhe3490622013-03-13 01:24:30 -070059
60 def Write(self, header_file, cc_file):
61 """Writes the loops to the specified files."""
62 self.WriteHeader(header_file)
Austin Schuh572ff402015-11-08 12:17:50 -080063 self.WriteCC(os.path.basename(header_file), cc_file)
Austin Schuhe3490622013-03-13 01:24:30 -070064
65 def _GenericType(self, typename):
66 """Returns a loop template using typename for the type."""
67 num_states = self._loops[0].A.shape[0]
68 num_inputs = self._loops[0].B.shape[1]
69 num_outputs = self._loops[0].C.shape[0]
70 return '%s<%d, %d, %d>' % (
71 typename, num_states, num_inputs, num_outputs)
72
73 def _ControllerType(self):
Austin Schuh32501832017-02-25 18:32:56 -080074 """Returns a template name for StateFeedbackController."""
75 return self._GenericType('StateFeedbackController')
76
77 def _ObserverType(self):
78 """Returns a template name for StateFeedbackObserver."""
79 return self._GenericType('StateFeedbackObserver')
Austin Schuhe3490622013-03-13 01:24:30 -070080
81 def _LoopType(self):
82 """Returns a template name for StateFeedbackLoop."""
83 return self._GenericType('StateFeedbackLoop')
84
85 def _PlantType(self):
86 """Returns a template name for StateFeedbackPlant."""
87 return self._GenericType('StateFeedbackPlant')
88
Austin Schuh32501832017-02-25 18:32:56 -080089 def _PlantCoeffType(self):
Austin Schuhe3490622013-03-13 01:24:30 -070090 """Returns a template name for StateFeedbackPlantCoefficients."""
91 return self._GenericType('StateFeedbackPlantCoefficients')
92
Austin Schuh32501832017-02-25 18:32:56 -080093 def _ControllerCoeffType(self):
94 """Returns a template name for StateFeedbackControllerCoefficients."""
95 return self._GenericType('StateFeedbackControllerCoefficients')
96
97 def _ObserverCoeffType(self):
98 """Returns a template name for StateFeedbackObserverCoefficients."""
99 return self._GenericType('StateFeedbackObserverCoefficients')
100
James Kuszmaul0e866512014-02-21 13:12:52 -0800101 def WriteHeader(self, header_file, double_appendage=False, MoI_ratio=0.0):
102 """Writes the header file to the file named header_file.
103 Set double_appendage to true in order to include a ratio of
104 moments of inertia constant. Currently, only used for 2014 claw."""
Austin Schuhe3490622013-03-13 01:24:30 -0700105 with open(header_file, 'w') as fd:
106 header_guard = self._HeaderGuard(header_file)
107 fd.write('#ifndef %s\n'
108 '#define %s\n\n' % (header_guard, header_guard))
109 fd.write('#include \"frc971/control_loops/state_feedback_loop.h\"\n')
110 fd.write('\n')
111
112 fd.write(self._namespace_start)
Ben Fredrickson1b45f782014-02-23 07:44:36 +0000113
114 for const in self._constant_list:
115 fd.write(str(const))
116
Austin Schuhe3490622013-03-13 01:24:30 -0700117 fd.write('\n\n')
118 for loop in self._loops:
119 fd.write(loop.DumpPlantHeader())
120 fd.write('\n')
121 fd.write(loop.DumpControllerHeader())
122 fd.write('\n')
Austin Schuh32501832017-02-25 18:32:56 -0800123 fd.write(loop.DumpObserverHeader())
124 fd.write('\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700125
126 fd.write('%s Make%sPlant();\n\n' %
127 (self._PlantType(), self._gain_schedule_name))
128
Austin Schuh32501832017-02-25 18:32:56 -0800129 fd.write('%s Make%sController();\n\n' %
130 (self._ControllerType(), self._gain_schedule_name))
131
132 fd.write('%s Make%sObserver();\n\n' %
133 (self._ObserverType(), self._gain_schedule_name))
134
Austin Schuhe3490622013-03-13 01:24:30 -0700135 fd.write('%s Make%sLoop();\n\n' %
136 (self._LoopType(), self._gain_schedule_name))
137
138 fd.write(self._namespace_end)
139 fd.write('\n\n')
140 fd.write("#endif // %s\n" % header_guard)
141
142 def WriteCC(self, header_file_name, cc_file):
143 """Writes the cc file to the file named cc_file."""
144 with open(cc_file, 'w') as fd:
Austin Schuh572ff402015-11-08 12:17:50 -0800145 fd.write('#include \"%s/%s\"\n' %
146 (os.path.join(*self._namespaces), header_file_name))
Austin Schuhe3490622013-03-13 01:24:30 -0700147 fd.write('\n')
148 fd.write('#include <vector>\n')
149 fd.write('\n')
150 fd.write('#include \"frc971/control_loops/state_feedback_loop.h\"\n')
151 fd.write('\n')
152 fd.write(self._namespace_start)
153 fd.write('\n\n')
154 for loop in self._loops:
155 fd.write(loop.DumpPlant())
156 fd.write('\n')
157
158 for loop in self._loops:
159 fd.write(loop.DumpController())
160 fd.write('\n')
161
Austin Schuh32501832017-02-25 18:32:56 -0800162 for loop in self._loops:
163 fd.write(loop.DumpObserver())
164 fd.write('\n')
165
Austin Schuhe3490622013-03-13 01:24:30 -0700166 fd.write('%s Make%sPlant() {\n' %
167 (self._PlantType(), self._gain_schedule_name))
Austin Schuh1a387962015-01-31 16:36:20 -0800168 fd.write(' ::std::vector< ::std::unique_ptr<%s>> plants(%d);\n' % (
Austin Schuh32501832017-02-25 18:32:56 -0800169 self._PlantCoeffType(), len(self._loops)))
Austin Schuhe3490622013-03-13 01:24:30 -0700170 for index, loop in enumerate(self._loops):
Austin Schuh1a387962015-01-31 16:36:20 -0800171 fd.write(' plants[%d] = ::std::unique_ptr<%s>(new %s(%s));\n' %
Austin Schuh32501832017-02-25 18:32:56 -0800172 (index, self._PlantCoeffType(), self._PlantCoeffType(),
Austin Schuhe3490622013-03-13 01:24:30 -0700173 loop.PlantFunction()))
Austin Schuh1a387962015-01-31 16:36:20 -0800174 fd.write(' return %s(&plants);\n' % self._PlantType())
Austin Schuhe3490622013-03-13 01:24:30 -0700175 fd.write('}\n\n')
176
Austin Schuh32501832017-02-25 18:32:56 -0800177 fd.write('%s Make%sController() {\n' %
178 (self._ControllerType(), self._gain_schedule_name))
Austin Schuh1a387962015-01-31 16:36:20 -0800179 fd.write(' ::std::vector< ::std::unique_ptr<%s>> controllers(%d);\n' % (
Austin Schuh32501832017-02-25 18:32:56 -0800180 self._ControllerCoeffType(), len(self._loops)))
Austin Schuhe3490622013-03-13 01:24:30 -0700181 for index, loop in enumerate(self._loops):
Austin Schuh1a387962015-01-31 16:36:20 -0800182 fd.write(' controllers[%d] = ::std::unique_ptr<%s>(new %s(%s));\n' %
Austin Schuh32501832017-02-25 18:32:56 -0800183 (index, self._ControllerCoeffType(), self._ControllerCoeffType(),
Austin Schuhe3490622013-03-13 01:24:30 -0700184 loop.ControllerFunction()))
Austin Schuh32501832017-02-25 18:32:56 -0800185 fd.write(' return %s(&controllers);\n' % self._ControllerType())
186 fd.write('}\n\n')
187
188 fd.write('%s Make%sObserver() {\n' %
189 (self._ObserverType(), self._gain_schedule_name))
190 fd.write(' ::std::vector< ::std::unique_ptr<%s>> observers(%d);\n' % (
191 self._ObserverCoeffType(), len(self._loops)))
192 for index, loop in enumerate(self._loops):
193 fd.write(' observers[%d] = ::std::unique_ptr<%s>(new %s(%s));\n' %
194 (index, self._ObserverCoeffType(), self._ObserverCoeffType(),
195 loop.ObserverFunction()))
196 fd.write(' return %s(&observers);\n' % self._ObserverType())
197 fd.write('}\n\n')
198
199 fd.write('%s Make%sLoop() {\n' %
200 (self._LoopType(), self._gain_schedule_name))
201 fd.write(' return %s(Make%sPlant(), Make%sController(), Make%sObserver());\n' %
202 (self._LoopType(), self._gain_schedule_name,
203 self._gain_schedule_name, self._gain_schedule_name))
Austin Schuhe3490622013-03-13 01:24:30 -0700204 fd.write('}\n\n')
205
206 fd.write(self._namespace_end)
207 fd.write('\n')
208
209
Austin Schuh3c542312013-02-24 01:53:50 -0800210class ControlLoop(object):
211 def __init__(self, name):
212 """Constructs a control loop object.
213
214 Args:
215 name: string, The name of the loop to use when writing the C++ files.
216 """
217 self._name = name
218
Austin Schuhc1f68892013-03-16 17:06:27 -0700219 def ContinuousToDiscrete(self, A_continuous, B_continuous, dt):
220 """Calculates the discrete time values for A and B.
Austin Schuh3c542312013-02-24 01:53:50 -0800221
222 Args:
223 A_continuous: numpy.matrix, The continuous time A matrix
224 B_continuous: numpy.matrix, The continuous time B matrix
225 dt: float, The time step of the control loop
Austin Schuhc1f68892013-03-16 17:06:27 -0700226
227 Returns:
228 (A, B), numpy.matrix, the control matricies.
Austin Schuh3c542312013-02-24 01:53:50 -0800229 """
Austin Schuhc1f68892013-03-16 17:06:27 -0700230 return controls.c2d(A_continuous, B_continuous, dt)
231
232 def InitializeState(self):
233 """Sets X, Y, and X_hat to zero defaults."""
Austin Schuh3c542312013-02-24 01:53:50 -0800234 self.X = numpy.zeros((self.A.shape[0], 1))
Austin Schuhc1f68892013-03-16 17:06:27 -0700235 self.Y = self.C * self.X
Austin Schuh3c542312013-02-24 01:53:50 -0800236 self.X_hat = numpy.zeros((self.A.shape[0], 1))
237
238 def PlaceControllerPoles(self, poles):
239 """Places the controller poles.
240
241 Args:
242 poles: array, An array of poles. Must be complex conjegates if they have
243 any imaginary portions.
244 """
245 self.K = controls.dplace(self.A, self.B, poles)
246
247 def PlaceObserverPoles(self, poles):
248 """Places the observer poles.
249
250 Args:
251 poles: array, An array of poles. Must be complex conjegates if they have
252 any imaginary portions.
253 """
254 self.L = controls.dplace(self.A.T, self.C.T, poles).T
255
256 def Update(self, U):
257 """Simulates one time step with the provided U."""
Austin Schuh1d005732015-03-01 00:10:20 -0800258 #U = numpy.clip(U, self.U_min, self.U_max)
Austin Schuh3c542312013-02-24 01:53:50 -0800259 self.X = self.A * self.X + self.B * U
260 self.Y = self.C * self.X + self.D * U
261
Austin Schuh1a387962015-01-31 16:36:20 -0800262 def PredictObserver(self, U):
263 """Runs the predict step of the observer update."""
264 self.X_hat = (self.A * self.X_hat + self.B * U)
265
266 def CorrectObserver(self, U):
267 """Runs the correct step of the observer update."""
268 self.X_hat += numpy.linalg.inv(self.A) * self.L * (
269 self.Y - self.C * self.X_hat - self.D * U)
270
Austin Schuh3c542312013-02-24 01:53:50 -0800271 def UpdateObserver(self, U):
272 """Updates the observer given the provided U."""
273 self.X_hat = (self.A * self.X_hat + self.B * U +
274 self.L * (self.Y - self.C * self.X_hat - self.D * U))
275
276 def _DumpMatrix(self, matrix_name, matrix):
277 """Dumps the provided matrix into a variable called matrix_name.
278
279 Args:
280 matrix_name: string, The variable name to save the matrix to.
281 matrix: The matrix to dump.
282
283 Returns:
284 string, The C++ commands required to populate a variable named matrix_name
285 with the contents of matrix.
286 """
Austin Schuhe3490622013-03-13 01:24:30 -0700287 ans = [' Eigen::Matrix<double, %d, %d> %s;\n' % (
Austin Schuh3c542312013-02-24 01:53:50 -0800288 matrix.shape[0], matrix.shape[1], matrix_name)]
Brian Silverman0f637382013-03-03 17:44:46 -0800289 for x in xrange(matrix.shape[0]):
290 for y in xrange(matrix.shape[1]):
Austin Schuhdf79d112016-10-15 21:25:32 -0700291 ans.append(' %s(%d, %d) = %s;\n' % (matrix_name, x, y, repr(matrix[x, y])))
Austin Schuh3c542312013-02-24 01:53:50 -0800292
Austin Schuhe3490622013-03-13 01:24:30 -0700293 return ''.join(ans)
Austin Schuh3c542312013-02-24 01:53:50 -0800294
Austin Schuhe3490622013-03-13 01:24:30 -0700295 def DumpPlantHeader(self):
Austin Schuh3c542312013-02-24 01:53:50 -0800296 """Writes out a c++ header declaration which will create a Plant object.
297
Austin Schuh3c542312013-02-24 01:53:50 -0800298 Returns:
299 string, The header declaration for the function.
300 """
301 num_states = self.A.shape[0]
302 num_inputs = self.B.shape[1]
303 num_outputs = self.C.shape[0]
Austin Schuhe3490622013-03-13 01:24:30 -0700304 return 'StateFeedbackPlantCoefficients<%d, %d, %d> Make%sPlantCoefficients();\n' % (
305 num_states, num_inputs, num_outputs, self._name)
Austin Schuh3c542312013-02-24 01:53:50 -0800306
Austin Schuhe3490622013-03-13 01:24:30 -0700307 def DumpPlant(self):
308 """Writes out a c++ function which will create a PlantCoefficients object.
Austin Schuh3c542312013-02-24 01:53:50 -0800309
310 Returns:
311 string, The function which will create the object.
312 """
313 num_states = self.A.shape[0]
314 num_inputs = self.B.shape[1]
315 num_outputs = self.C.shape[0]
Austin Schuhe3490622013-03-13 01:24:30 -0700316 ans = ['StateFeedbackPlantCoefficients<%d, %d, %d>'
317 ' Make%sPlantCoefficients() {\n' % (
318 num_states, num_inputs, num_outputs, self._name)]
Austin Schuh3c542312013-02-24 01:53:50 -0800319
Austin Schuhe3490622013-03-13 01:24:30 -0700320 ans.append(self._DumpMatrix('A', self.A))
Austin Schuhc5fceb82017-02-25 16:24:12 -0800321 ans.append(self._DumpMatrix('A_inv', numpy.linalg.inv(self.A)))
Austin Schuh6c20f202017-02-18 22:31:44 -0800322 ans.append(self._DumpMatrix('A_continuous', self.A_continuous))
Austin Schuhe3490622013-03-13 01:24:30 -0700323 ans.append(self._DumpMatrix('B', self.B))
Austin Schuh6c20f202017-02-18 22:31:44 -0800324 ans.append(self._DumpMatrix('B_continuous', self.B_continuous))
Austin Schuhe3490622013-03-13 01:24:30 -0700325 ans.append(self._DumpMatrix('C', self.C))
326 ans.append(self._DumpMatrix('D', self.D))
327 ans.append(self._DumpMatrix('U_max', self.U_max))
328 ans.append(self._DumpMatrix('U_min', self.U_min))
Austin Schuh3c542312013-02-24 01:53:50 -0800329
Austin Schuhe3490622013-03-13 01:24:30 -0700330 ans.append(' return StateFeedbackPlantCoefficients<%d, %d, %d>'
Austin Schuhc5fceb82017-02-25 16:24:12 -0800331 '(A, A_inv, A_continuous, B, B_continuous, C, D, U_max, U_min);\n' % (
Austin Schuh6c20f202017-02-18 22:31:44 -0800332 num_states, num_inputs, num_outputs))
Austin Schuhe3490622013-03-13 01:24:30 -0700333 ans.append('}\n')
334 return ''.join(ans)
Austin Schuh3c542312013-02-24 01:53:50 -0800335
Austin Schuhe3490622013-03-13 01:24:30 -0700336 def PlantFunction(self):
337 """Returns the name of the plant coefficient function."""
338 return 'Make%sPlantCoefficients()' % self._name
Austin Schuh3c542312013-02-24 01:53:50 -0800339
Austin Schuhe3490622013-03-13 01:24:30 -0700340 def ControllerFunction(self):
341 """Returns the name of the controller function."""
Austin Schuh32501832017-02-25 18:32:56 -0800342 return 'Make%sControllerCoefficients()' % self._name
343
344 def ObserverFunction(self):
345 """Returns the name of the controller function."""
346 return 'Make%sObserverCoefficients()' % self._name
Austin Schuhe3490622013-03-13 01:24:30 -0700347
348 def DumpControllerHeader(self):
349 """Writes out a c++ header declaration which will create a Controller object.
Austin Schuh3c542312013-02-24 01:53:50 -0800350
351 Returns:
352 string, The header declaration for the function.
353 """
354 num_states = self.A.shape[0]
355 num_inputs = self.B.shape[1]
356 num_outputs = self.C.shape[0]
Austin Schuh32501832017-02-25 18:32:56 -0800357 return 'StateFeedbackControllerCoefficients<%d, %d, %d> %s;\n' % (
Austin Schuhe3490622013-03-13 01:24:30 -0700358 num_states, num_inputs, num_outputs, self.ControllerFunction())
Austin Schuh3c542312013-02-24 01:53:50 -0800359
Austin Schuhe3490622013-03-13 01:24:30 -0700360 def DumpController(self):
361 """Returns a c++ function which will create a Controller object.
Austin Schuh3c542312013-02-24 01:53:50 -0800362
363 Returns:
364 string, The function which will create the object.
365 """
366 num_states = self.A.shape[0]
367 num_inputs = self.B.shape[1]
368 num_outputs = self.C.shape[0]
Austin Schuh32501832017-02-25 18:32:56 -0800369 ans = ['StateFeedbackControllerCoefficients<%d, %d, %d> %s {\n' % (
Austin Schuhe3490622013-03-13 01:24:30 -0700370 num_states, num_inputs, num_outputs, self.ControllerFunction())]
Austin Schuh3c542312013-02-24 01:53:50 -0800371
Austin Schuhe3490622013-03-13 01:24:30 -0700372 ans.append(self._DumpMatrix('K', self.K))
Austin Schuh86093ad2016-02-06 14:29:34 -0800373 if not hasattr(self, 'Kff'):
374 self.Kff = numpy.matrix(numpy.zeros(self.K.shape))
375
376 ans.append(self._DumpMatrix('Kff', self.Kff))
Austin Schuh3c542312013-02-24 01:53:50 -0800377
Austin Schuh32501832017-02-25 18:32:56 -0800378 ans.append(' return StateFeedbackControllerCoefficients<%d, %d, %d>'
379 '(K, Kff);\n' % (
Austin Schuhc5fceb82017-02-25 16:24:12 -0800380 num_states, num_inputs, num_outputs))
Austin Schuhe3490622013-03-13 01:24:30 -0700381 ans.append('}\n')
382 return ''.join(ans)
Austin Schuh32501832017-02-25 18:32:56 -0800383
384 def DumpObserverHeader(self):
385 """Writes out a c++ header declaration which will create a Observer object.
386
387 Returns:
388 string, The header declaration for the function.
389 """
390 num_states = self.A.shape[0]
391 num_inputs = self.B.shape[1]
392 num_outputs = self.C.shape[0]
393 return 'StateFeedbackObserverCoefficients<%d, %d, %d> %s;\n' % (
394 num_states, num_inputs, num_outputs, self.ObserverFunction())
395
396 def DumpObserver(self):
397 """Returns a c++ function which will create a Observer object.
398
399 Returns:
400 string, The function which will create the object.
401 """
402 num_states = self.A.shape[0]
403 num_inputs = self.B.shape[1]
404 num_outputs = self.C.shape[0]
405 ans = ['StateFeedbackObserverCoefficients<%d, %d, %d> %s {\n' % (
406 num_states, num_inputs, num_outputs, self.ObserverFunction())]
407
408 ans.append(self._DumpMatrix('L', self.L))
409
410 ans.append(' return StateFeedbackObserverCoefficients<%d, %d, %d>'
411 '(L);\n' % (num_states, num_inputs, num_outputs))
412 ans.append('}\n')
413 return ''.join(ans)