blob: 50e431457b6af3958da16ea475698ba4cbe66762 [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):
Austin Schuh3ad5ed82017-02-25 21:36:19 -080019 def __init__(self, gain_schedule_name, loops, namespaces=None,
20 write_constants=False, plant_type='StateFeedbackPlant',
21 observer_type='StateFeedbackObserver'):
Austin Schuhe3490622013-03-13 01:24:30 -070022 """Constructs a control loop writer.
23
24 Args:
25 gain_schedule_name: string, Name of the overall controller.
26 loops: array[ControlLoop], a list of control loops to gain schedule
27 in order.
28 namespaces: array[string], a list of names of namespaces to nest in
29 order. If None, the default will be used.
Austin Schuh3ad5ed82017-02-25 21:36:19 -080030 plant_type: string, The C++ type of the plant.
31 observer_type: string, The C++ type of the observer.
Austin Schuhe3490622013-03-13 01:24:30 -070032 """
33 self._gain_schedule_name = gain_schedule_name
34 self._loops = loops
35 if namespaces:
36 self._namespaces = namespaces
37 else:
38 self._namespaces = ['frc971', 'control_loops']
39
40 self._namespace_start = '\n'.join(
41 ['namespace %s {' % name for name in self._namespaces])
42
43 self._namespace_end = '\n'.join(
44 ['} // namespace %s' % name for name in reversed(self._namespaces)])
Austin Schuh86093ad2016-02-06 14:29:34 -080045
Ben Fredrickson1b45f782014-02-23 07:44:36 +000046 self._constant_list = []
Austin Schuh3ad5ed82017-02-25 21:36:19 -080047 self._plant_type = plant_type
48 self._observer_type = observer_type
Austin Schuh25933852014-02-23 02:04:13 -080049
50 def AddConstant(self, constant):
51 """Adds a constant to write.
52
53 Args:
54 constant: Constant, the constant to add to the header.
55 """
56 self._constant_list.append(constant)
Austin Schuhe3490622013-03-13 01:24:30 -070057
Brian Silvermane51ad632014-01-08 15:12:29 -080058 def _TopDirectory(self):
59 return self._namespaces[0]
60
Austin Schuhe3490622013-03-13 01:24:30 -070061 def _HeaderGuard(self, header_file):
Austin Schuh16cf47a2015-11-28 13:20:33 -080062 return ('_'.join([namespace.upper() for namespace in self._namespaces]) + '_' +
Austin Schuh572ff402015-11-08 12:17:50 -080063 os.path.basename(header_file).upper()
64 .replace('.', '_').replace('/', '_') + '_')
Austin Schuhe3490622013-03-13 01:24:30 -070065
66 def Write(self, header_file, cc_file):
67 """Writes the loops to the specified files."""
68 self.WriteHeader(header_file)
Austin Schuh572ff402015-11-08 12:17:50 -080069 self.WriteCC(os.path.basename(header_file), cc_file)
Austin Schuhe3490622013-03-13 01:24:30 -070070
Austin Schuh3ad5ed82017-02-25 21:36:19 -080071 def _GenericType(self, typename, extra_args=None):
Austin Schuhe3490622013-03-13 01:24:30 -070072 """Returns a loop template using typename for the type."""
73 num_states = self._loops[0].A.shape[0]
74 num_inputs = self._loops[0].B.shape[1]
75 num_outputs = self._loops[0].C.shape[0]
Austin Schuh3ad5ed82017-02-25 21:36:19 -080076 if extra_args is not None:
77 extra_args = ', ' + extra_args
78 else:
79 extra_args = ''
80 return '%s<%d, %d, %d%s>' % (
81 typename, num_states, num_inputs, num_outputs, extra_args)
Austin Schuhe3490622013-03-13 01:24:30 -070082
83 def _ControllerType(self):
Austin Schuh32501832017-02-25 18:32:56 -080084 """Returns a template name for StateFeedbackController."""
85 return self._GenericType('StateFeedbackController')
86
87 def _ObserverType(self):
88 """Returns a template name for StateFeedbackObserver."""
Austin Schuh3ad5ed82017-02-25 21:36:19 -080089 return self._GenericType(self._observer_type)
Austin Schuhe3490622013-03-13 01:24:30 -070090
91 def _LoopType(self):
92 """Returns a template name for StateFeedbackLoop."""
Austin Schuh3ad5ed82017-02-25 21:36:19 -080093 extra_args = '%s, %s' % (self._PlantType(), self._ObserverType())
94 return self._GenericType('StateFeedbackLoop', extra_args)
Austin Schuhe3490622013-03-13 01:24:30 -070095
96 def _PlantType(self):
97 """Returns a template name for StateFeedbackPlant."""
Austin Schuh3ad5ed82017-02-25 21:36:19 -080098 return self._GenericType(self._plant_type)
Austin Schuhe3490622013-03-13 01:24:30 -070099
Austin Schuh32501832017-02-25 18:32:56 -0800100 def _PlantCoeffType(self):
Austin Schuhe3490622013-03-13 01:24:30 -0700101 """Returns a template name for StateFeedbackPlantCoefficients."""
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800102 return self._GenericType(self._plant_type + 'Coefficients')
Austin Schuhe3490622013-03-13 01:24:30 -0700103
Austin Schuh32501832017-02-25 18:32:56 -0800104 def _ControllerCoeffType(self):
105 """Returns a template name for StateFeedbackControllerCoefficients."""
106 return self._GenericType('StateFeedbackControllerCoefficients')
107
108 def _ObserverCoeffType(self):
109 """Returns a template name for StateFeedbackObserverCoefficients."""
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800110 return self._GenericType(self._observer_type + 'Coefficients')
Austin Schuh32501832017-02-25 18:32:56 -0800111
James Kuszmaul0e866512014-02-21 13:12:52 -0800112 def WriteHeader(self, header_file, double_appendage=False, MoI_ratio=0.0):
113 """Writes the header file to the file named header_file.
114 Set double_appendage to true in order to include a ratio of
115 moments of inertia constant. Currently, only used for 2014 claw."""
Austin Schuhe3490622013-03-13 01:24:30 -0700116 with open(header_file, 'w') as fd:
117 header_guard = self._HeaderGuard(header_file)
118 fd.write('#ifndef %s\n'
119 '#define %s\n\n' % (header_guard, header_guard))
120 fd.write('#include \"frc971/control_loops/state_feedback_loop.h\"\n')
Austin Schuh4cc4fe22017-11-23 19:13:09 -0800121 if (self._plant_type == 'StateFeedbackHybridPlant' or
122 self._observer_type == 'HybridKalman'):
123 fd.write('#include \"frc971/control_loops/hybrid_state_feedback_loop.h\"\n')
124
Austin Schuhe3490622013-03-13 01:24:30 -0700125 fd.write('\n')
126
127 fd.write(self._namespace_start)
Ben Fredrickson1b45f782014-02-23 07:44:36 +0000128
129 for const in self._constant_list:
130 fd.write(str(const))
131
Austin Schuhe3490622013-03-13 01:24:30 -0700132 fd.write('\n\n')
133 for loop in self._loops:
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800134 fd.write(loop.DumpPlantHeader(self._PlantCoeffType()))
Austin Schuhe3490622013-03-13 01:24:30 -0700135 fd.write('\n')
136 fd.write(loop.DumpControllerHeader())
137 fd.write('\n')
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800138 fd.write(loop.DumpObserverHeader(self._ObserverCoeffType()))
Austin Schuh32501832017-02-25 18:32:56 -0800139 fd.write('\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700140
141 fd.write('%s Make%sPlant();\n\n' %
142 (self._PlantType(), self._gain_schedule_name))
143
Austin Schuh32501832017-02-25 18:32:56 -0800144 fd.write('%s Make%sController();\n\n' %
145 (self._ControllerType(), self._gain_schedule_name))
146
147 fd.write('%s Make%sObserver();\n\n' %
148 (self._ObserverType(), self._gain_schedule_name))
149
Austin Schuhe3490622013-03-13 01:24:30 -0700150 fd.write('%s Make%sLoop();\n\n' %
151 (self._LoopType(), self._gain_schedule_name))
152
153 fd.write(self._namespace_end)
154 fd.write('\n\n')
155 fd.write("#endif // %s\n" % header_guard)
156
157 def WriteCC(self, header_file_name, cc_file):
158 """Writes the cc file to the file named cc_file."""
159 with open(cc_file, 'w') as fd:
Austin Schuh572ff402015-11-08 12:17:50 -0800160 fd.write('#include \"%s/%s\"\n' %
161 (os.path.join(*self._namespaces), header_file_name))
Austin Schuhe3490622013-03-13 01:24:30 -0700162 fd.write('\n')
163 fd.write('#include <vector>\n')
164 fd.write('\n')
165 fd.write('#include \"frc971/control_loops/state_feedback_loop.h\"\n')
166 fd.write('\n')
167 fd.write(self._namespace_start)
168 fd.write('\n\n')
169 for loop in self._loops:
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800170 fd.write(loop.DumpPlant(self._PlantCoeffType()))
Austin Schuhe3490622013-03-13 01:24:30 -0700171 fd.write('\n')
172
173 for loop in self._loops:
174 fd.write(loop.DumpController())
175 fd.write('\n')
176
Austin Schuh32501832017-02-25 18:32:56 -0800177 for loop in self._loops:
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800178 fd.write(loop.DumpObserver(self._ObserverCoeffType()))
Austin Schuh32501832017-02-25 18:32:56 -0800179 fd.write('\n')
180
Austin Schuhe3490622013-03-13 01:24:30 -0700181 fd.write('%s Make%sPlant() {\n' %
182 (self._PlantType(), self._gain_schedule_name))
Austin Schuh1a387962015-01-31 16:36:20 -0800183 fd.write(' ::std::vector< ::std::unique_ptr<%s>> plants(%d);\n' % (
Austin Schuh32501832017-02-25 18:32:56 -0800184 self._PlantCoeffType(), len(self._loops)))
Austin Schuhe3490622013-03-13 01:24:30 -0700185 for index, loop in enumerate(self._loops):
Austin Schuh1a387962015-01-31 16:36:20 -0800186 fd.write(' plants[%d] = ::std::unique_ptr<%s>(new %s(%s));\n' %
Austin Schuh32501832017-02-25 18:32:56 -0800187 (index, self._PlantCoeffType(), self._PlantCoeffType(),
Austin Schuhe3490622013-03-13 01:24:30 -0700188 loop.PlantFunction()))
Austin Schuh1a387962015-01-31 16:36:20 -0800189 fd.write(' return %s(&plants);\n' % self._PlantType())
Austin Schuhe3490622013-03-13 01:24:30 -0700190 fd.write('}\n\n')
191
Austin Schuh32501832017-02-25 18:32:56 -0800192 fd.write('%s Make%sController() {\n' %
193 (self._ControllerType(), self._gain_schedule_name))
Austin Schuh1a387962015-01-31 16:36:20 -0800194 fd.write(' ::std::vector< ::std::unique_ptr<%s>> controllers(%d);\n' % (
Austin Schuh32501832017-02-25 18:32:56 -0800195 self._ControllerCoeffType(), len(self._loops)))
Austin Schuhe3490622013-03-13 01:24:30 -0700196 for index, loop in enumerate(self._loops):
Austin Schuh1a387962015-01-31 16:36:20 -0800197 fd.write(' controllers[%d] = ::std::unique_ptr<%s>(new %s(%s));\n' %
Austin Schuh32501832017-02-25 18:32:56 -0800198 (index, self._ControllerCoeffType(), self._ControllerCoeffType(),
Austin Schuhe3490622013-03-13 01:24:30 -0700199 loop.ControllerFunction()))
Austin Schuh32501832017-02-25 18:32:56 -0800200 fd.write(' return %s(&controllers);\n' % self._ControllerType())
201 fd.write('}\n\n')
202
203 fd.write('%s Make%sObserver() {\n' %
204 (self._ObserverType(), self._gain_schedule_name))
205 fd.write(' ::std::vector< ::std::unique_ptr<%s>> observers(%d);\n' % (
206 self._ObserverCoeffType(), len(self._loops)))
207 for index, loop in enumerate(self._loops):
208 fd.write(' observers[%d] = ::std::unique_ptr<%s>(new %s(%s));\n' %
209 (index, self._ObserverCoeffType(), self._ObserverCoeffType(),
210 loop.ObserverFunction()))
211 fd.write(' return %s(&observers);\n' % self._ObserverType())
212 fd.write('}\n\n')
213
214 fd.write('%s Make%sLoop() {\n' %
215 (self._LoopType(), self._gain_schedule_name))
216 fd.write(' return %s(Make%sPlant(), Make%sController(), Make%sObserver());\n' %
217 (self._LoopType(), self._gain_schedule_name,
218 self._gain_schedule_name, self._gain_schedule_name))
Austin Schuhe3490622013-03-13 01:24:30 -0700219 fd.write('}\n\n')
220
221 fd.write(self._namespace_end)
222 fd.write('\n')
223
224
Austin Schuh3c542312013-02-24 01:53:50 -0800225class ControlLoop(object):
226 def __init__(self, name):
227 """Constructs a control loop object.
228
229 Args:
230 name: string, The name of the loop to use when writing the C++ files.
231 """
232 self._name = name
233
Austin Schuhc1f68892013-03-16 17:06:27 -0700234 def ContinuousToDiscrete(self, A_continuous, B_continuous, dt):
235 """Calculates the discrete time values for A and B.
Austin Schuh3c542312013-02-24 01:53:50 -0800236
237 Args:
238 A_continuous: numpy.matrix, The continuous time A matrix
239 B_continuous: numpy.matrix, The continuous time B matrix
240 dt: float, The time step of the control loop
Austin Schuhc1f68892013-03-16 17:06:27 -0700241
242 Returns:
243 (A, B), numpy.matrix, the control matricies.
Austin Schuh3c542312013-02-24 01:53:50 -0800244 """
Austin Schuhc1f68892013-03-16 17:06:27 -0700245 return controls.c2d(A_continuous, B_continuous, dt)
246
247 def InitializeState(self):
248 """Sets X, Y, and X_hat to zero defaults."""
Austin Schuh3c542312013-02-24 01:53:50 -0800249 self.X = numpy.zeros((self.A.shape[0], 1))
Austin Schuhc1f68892013-03-16 17:06:27 -0700250 self.Y = self.C * self.X
Austin Schuh3c542312013-02-24 01:53:50 -0800251 self.X_hat = numpy.zeros((self.A.shape[0], 1))
252
253 def PlaceControllerPoles(self, poles):
254 """Places the controller poles.
255
256 Args:
257 poles: array, An array of poles. Must be complex conjegates if they have
258 any imaginary portions.
259 """
260 self.K = controls.dplace(self.A, self.B, poles)
261
262 def PlaceObserverPoles(self, poles):
263 """Places the observer poles.
264
265 Args:
266 poles: array, An array of poles. Must be complex conjegates if they have
267 any imaginary portions.
268 """
269 self.L = controls.dplace(self.A.T, self.C.T, poles).T
270
271 def Update(self, U):
272 """Simulates one time step with the provided U."""
Austin Schuh1d005732015-03-01 00:10:20 -0800273 #U = numpy.clip(U, self.U_min, self.U_max)
Austin Schuh3c542312013-02-24 01:53:50 -0800274 self.X = self.A * self.X + self.B * U
275 self.Y = self.C * self.X + self.D * U
276
Austin Schuh1a387962015-01-31 16:36:20 -0800277 def PredictObserver(self, U):
278 """Runs the predict step of the observer update."""
279 self.X_hat = (self.A * self.X_hat + self.B * U)
280
281 def CorrectObserver(self, U):
282 """Runs the correct step of the observer update."""
283 self.X_hat += numpy.linalg.inv(self.A) * self.L * (
284 self.Y - self.C * self.X_hat - self.D * U)
285
Austin Schuh3c542312013-02-24 01:53:50 -0800286 def UpdateObserver(self, U):
287 """Updates the observer given the provided U."""
288 self.X_hat = (self.A * self.X_hat + self.B * U +
289 self.L * (self.Y - self.C * self.X_hat - self.D * U))
290
291 def _DumpMatrix(self, matrix_name, matrix):
292 """Dumps the provided matrix into a variable called matrix_name.
293
294 Args:
295 matrix_name: string, The variable name to save the matrix to.
296 matrix: The matrix to dump.
297
298 Returns:
299 string, The C++ commands required to populate a variable named matrix_name
300 with the contents of matrix.
301 """
Austin Schuhe3490622013-03-13 01:24:30 -0700302 ans = [' Eigen::Matrix<double, %d, %d> %s;\n' % (
Austin Schuh3c542312013-02-24 01:53:50 -0800303 matrix.shape[0], matrix.shape[1], matrix_name)]
Brian Silverman0f637382013-03-03 17:44:46 -0800304 for x in xrange(matrix.shape[0]):
305 for y in xrange(matrix.shape[1]):
Austin Schuhdf79d112016-10-15 21:25:32 -0700306 ans.append(' %s(%d, %d) = %s;\n' % (matrix_name, x, y, repr(matrix[x, y])))
Austin Schuh3c542312013-02-24 01:53:50 -0800307
Austin Schuhe3490622013-03-13 01:24:30 -0700308 return ''.join(ans)
Austin Schuh3c542312013-02-24 01:53:50 -0800309
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800310 def DumpPlantHeader(self, plant_coefficient_type):
Austin Schuh3c542312013-02-24 01:53:50 -0800311 """Writes out a c++ header declaration which will create a Plant object.
312
Austin Schuh3c542312013-02-24 01:53:50 -0800313 Returns:
314 string, The header declaration for the function.
315 """
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800316 return '%s Make%sPlantCoefficients();\n' % (
317 plant_coefficient_type, self._name)
Austin Schuh3c542312013-02-24 01:53:50 -0800318
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800319 def DumpPlant(self, plant_coefficient_type):
Austin Schuhe3490622013-03-13 01:24:30 -0700320 """Writes out a c++ function which will create a PlantCoefficients object.
Austin Schuh3c542312013-02-24 01:53:50 -0800321
322 Returns:
323 string, The function which will create the object.
324 """
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800325 ans = ['%s Make%sPlantCoefficients() {\n' % (
326 plant_coefficient_type, self._name)]
Austin Schuh3c542312013-02-24 01:53:50 -0800327
Austin Schuhe3490622013-03-13 01:24:30 -0700328 ans.append(self._DumpMatrix('C', self.C))
329 ans.append(self._DumpMatrix('D', self.D))
330 ans.append(self._DumpMatrix('U_max', self.U_max))
331 ans.append(self._DumpMatrix('U_min', self.U_min))
Austin Schuh3c542312013-02-24 01:53:50 -0800332
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800333 if plant_coefficient_type.startswith('StateFeedbackPlant'):
334 ans.append(self._DumpMatrix('A', self.A))
335 ans.append(self._DumpMatrix('A_inv', numpy.linalg.inv(self.A)))
336 ans.append(self._DumpMatrix('B', self.B))
337 ans.append(' return %s'
338 '(A, A_inv, B, C, D, U_max, U_min);\n' % (
339 plant_coefficient_type))
340 elif plant_coefficient_type.startswith('StateFeedbackHybridPlant'):
341 ans.append(self._DumpMatrix('A_continuous', self.A_continuous))
342 ans.append(self._DumpMatrix('B_continuous', self.B_continuous))
343 ans.append(' return %s'
344 '(A_continuous, B_continuous, C, D, U_max, U_min);\n' % (
345 plant_coefficient_type))
346 else:
347 glog.fatal('Unsupported plant type %s', plant_coefficient_type)
348
Austin Schuhe3490622013-03-13 01:24:30 -0700349 ans.append('}\n')
350 return ''.join(ans)
Austin Schuh3c542312013-02-24 01:53:50 -0800351
Austin Schuhe3490622013-03-13 01:24:30 -0700352 def PlantFunction(self):
353 """Returns the name of the plant coefficient function."""
354 return 'Make%sPlantCoefficients()' % self._name
Austin Schuh3c542312013-02-24 01:53:50 -0800355
Austin Schuhe3490622013-03-13 01:24:30 -0700356 def ControllerFunction(self):
357 """Returns the name of the controller function."""
Austin Schuh32501832017-02-25 18:32:56 -0800358 return 'Make%sControllerCoefficients()' % self._name
359
360 def ObserverFunction(self):
361 """Returns the name of the controller function."""
362 return 'Make%sObserverCoefficients()' % self._name
Austin Schuhe3490622013-03-13 01:24:30 -0700363
364 def DumpControllerHeader(self):
365 """Writes out a c++ header declaration which will create a Controller object.
Austin Schuh3c542312013-02-24 01:53:50 -0800366
367 Returns:
368 string, The header declaration for the function.
369 """
370 num_states = self.A.shape[0]
371 num_inputs = self.B.shape[1]
372 num_outputs = self.C.shape[0]
Austin Schuh32501832017-02-25 18:32:56 -0800373 return 'StateFeedbackControllerCoefficients<%d, %d, %d> %s;\n' % (
Austin Schuhe3490622013-03-13 01:24:30 -0700374 num_states, num_inputs, num_outputs, self.ControllerFunction())
Austin Schuh3c542312013-02-24 01:53:50 -0800375
Austin Schuhe3490622013-03-13 01:24:30 -0700376 def DumpController(self):
377 """Returns a c++ function which will create a Controller object.
Austin Schuh3c542312013-02-24 01:53:50 -0800378
379 Returns:
380 string, The function which will create the object.
381 """
382 num_states = self.A.shape[0]
383 num_inputs = self.B.shape[1]
384 num_outputs = self.C.shape[0]
Austin Schuh32501832017-02-25 18:32:56 -0800385 ans = ['StateFeedbackControllerCoefficients<%d, %d, %d> %s {\n' % (
Austin Schuhe3490622013-03-13 01:24:30 -0700386 num_states, num_inputs, num_outputs, self.ControllerFunction())]
Austin Schuh3c542312013-02-24 01:53:50 -0800387
Austin Schuhe3490622013-03-13 01:24:30 -0700388 ans.append(self._DumpMatrix('K', self.K))
Austin Schuh86093ad2016-02-06 14:29:34 -0800389 if not hasattr(self, 'Kff'):
390 self.Kff = numpy.matrix(numpy.zeros(self.K.shape))
391
392 ans.append(self._DumpMatrix('Kff', self.Kff))
Austin Schuh3c542312013-02-24 01:53:50 -0800393
Austin Schuh32501832017-02-25 18:32:56 -0800394 ans.append(' return StateFeedbackControllerCoefficients<%d, %d, %d>'
395 '(K, Kff);\n' % (
Austin Schuhc5fceb82017-02-25 16:24:12 -0800396 num_states, num_inputs, num_outputs))
Austin Schuhe3490622013-03-13 01:24:30 -0700397 ans.append('}\n')
398 return ''.join(ans)
Austin Schuh32501832017-02-25 18:32:56 -0800399
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800400 def DumpObserverHeader(self, observer_coefficient_type):
Austin Schuh32501832017-02-25 18:32:56 -0800401 """Writes out a c++ header declaration which will create a Observer object.
402
403 Returns:
404 string, The header declaration for the function.
405 """
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800406 return '%s %s;\n' % (
407 observer_coefficient_type, self.ObserverFunction())
Austin Schuh32501832017-02-25 18:32:56 -0800408
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800409 def DumpObserver(self, observer_coefficient_type):
Austin Schuh32501832017-02-25 18:32:56 -0800410 """Returns a c++ function which will create a Observer object.
411
412 Returns:
413 string, The function which will create the object.
414 """
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800415 ans = ['%s %s {\n' % (
416 observer_coefficient_type, self.ObserverFunction())]
Austin Schuh32501832017-02-25 18:32:56 -0800417
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800418 if observer_coefficient_type.startswith('StateFeedbackObserver'):
419 ans.append(self._DumpMatrix('L', self.L))
420 ans.append(' return %s(L);\n' % (observer_coefficient_type,))
421 elif observer_coefficient_type.startswith('HybridKalman'):
422 ans.append(self._DumpMatrix('Q_continuous', self.Q_continuous))
423 ans.append(self._DumpMatrix('R_continuous', self.R_continuous))
424 ans.append(self._DumpMatrix('P_steady_state', self.P_steady_state))
425 ans.append(' return %s(Q_continuous, R_continuous, P_steady_state);\n' % (
426 observer_coefficient_type,))
Brian Silvermana3a20cc2017-03-05 18:35:20 -0800427 else:
428 glog.fatal('Unsupported observer type %s', observer_coefficient_type)
Austin Schuh32501832017-02-25 18:32:56 -0800429
Austin Schuh32501832017-02-25 18:32:56 -0800430 ans.append('}\n')
431 return ''.join(ans)
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800432
433class HybridControlLoop(ControlLoop):
434 def __init__(self, name):
435 super(HybridControlLoop, self).__init__(name=name)
436
Brian Silverman59c829a2017-03-05 18:36:54 -0800437 def Discretize(self, dt):
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800438 [self.A, self.B, self.Q, self.R] = \
439 controls.kalmd(self.A_continuous, self.B_continuous,
440 self.Q_continuous, self.R_continuous, dt)
441
442 def PredictHybridObserver(self, U, dt):
Brian Silverman59c829a2017-03-05 18:36:54 -0800443 self.Discretize(dt)
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800444 self.X_hat = self.A * self.X_hat + self.B * U
445 self.P = (self.A * self.P * self.A.T + self.Q)
446
447 def CorrectHybridObserver(self, U):
448 Y_bar = self.Y - self.C * self.X_hat
449 C_t = self.C.T
450 S = self.C * self.P * C_t + self.R
451 self.KalmanGain = self.P * C_t * numpy.linalg.inv(S)
452 self.X_hat = self.X_hat + self.KalmanGain * Y_bar
453 self.P = (numpy.eye(len(self.A)) - self.KalmanGain * self.C) * self.P
454
455 def InitializeState(self):
456 super(HybridControlLoop, self).InitializeState()
457 if hasattr(self, 'Q_steady_state'):
458 self.P = self.Q_steady_state
459 else:
460 self.P = numpy.matrix(numpy.zeros((self.A.shape[0], self.A.shape[0])))