blob: 347e1cd4f4f7062e0ff4802407077f0469701d6f [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
Austin Schuhbcce26a2018-03-26 23:41:24 -07005
Tyler Chatow6738c362019-02-16 14:12:30 -08006class Constant(object):
7
8 def __init__(self, name, formatt, value):
9 self.name = name
10 self.formatt = formatt
11 self.value = value
12 self.formatToType = {}
13 self.formatToType['%f'] = "double"
14 self.formatToType['%d'] = "int"
15
16 def Render(self, loop_type):
17 typestring = self.formatToType[self.formatt]
18 if loop_type == 'float' and typestring == 'double':
19 typestring = loop_type
20 return str("\nstatic constexpr %s %s = "+ self.formatt +";\n") % \
21 (typestring, self.name, self.value)
Ben Fredrickson1b45f782014-02-23 07:44:36 +000022
23
Austin Schuhe3490622013-03-13 01:24:30 -070024class ControlLoopWriter(object):
Austin Schuhe3490622013-03-13 01:24:30 -070025
Tyler Chatow6738c362019-02-16 14:12:30 -080026 def __init__(self,
27 gain_schedule_name,
28 loops,
29 namespaces=None,
30 write_constants=False,
31 plant_type='StateFeedbackPlant',
32 observer_type='StateFeedbackObserver',
33 scalar_type='double'):
34 """Constructs a control loop writer.
Austin Schuhe3490622013-03-13 01:24:30 -070035
Tyler Chatow6738c362019-02-16 14:12:30 -080036 Args:
37 gain_schedule_name: string, Name of the overall controller.
38 loops: array[ControlLoop], a list of control loops to gain schedule
39 in order.
40 namespaces: array[string], a list of names of namespaces to nest in
41 order. If None, the default will be used.
42 plant_type: string, The C++ type of the plant.
43 observer_type: string, The C++ type of the observer.
44 scalar_type: string, The C++ type of the base scalar.
45 """
46 self._gain_schedule_name = gain_schedule_name
47 self._loops = loops
48 if namespaces:
49 self._namespaces = namespaces
50 else:
51 self._namespaces = ['frc971', 'control_loops']
Austin Schuhe3490622013-03-13 01:24:30 -070052
Tyler Chatow6738c362019-02-16 14:12:30 -080053 self._namespace_start = '\n'.join(
54 ['namespace %s {' % name for name in self._namespaces])
Austin Schuh86093ad2016-02-06 14:29:34 -080055
Tyler Chatow6738c362019-02-16 14:12:30 -080056 self._namespace_end = '\n'.join([
57 '} // namespace %s' % name for name in reversed(self._namespaces)
58 ])
Austin Schuh25933852014-02-23 02:04:13 -080059
Tyler Chatow6738c362019-02-16 14:12:30 -080060 self._constant_list = []
61 self._plant_type = plant_type
62 self._observer_type = observer_type
63 self._scalar_type = scalar_type
Austin Schuh25933852014-02-23 02:04:13 -080064
Tyler Chatow6738c362019-02-16 14:12:30 -080065 def AddConstant(self, constant):
66 """Adds a constant to write.
Austin Schuhe3490622013-03-13 01:24:30 -070067
Tyler Chatow6738c362019-02-16 14:12:30 -080068 Args:
69 constant: Constant, the constant to add to the header.
70 """
71 self._constant_list.append(constant)
Brian Silvermane51ad632014-01-08 15:12:29 -080072
Tyler Chatow6738c362019-02-16 14:12:30 -080073 def _TopDirectory(self):
74 return self._namespaces[0]
Austin Schuhe3490622013-03-13 01:24:30 -070075
Tyler Chatow6738c362019-02-16 14:12:30 -080076 def _HeaderGuard(self, header_file):
77 return ('_'.join([namespace.upper() for namespace in self._namespaces])
78 + '_' + os.path.basename(header_file).upper().replace(
79 '.', '_').replace('/', '_') + '_')
Austin Schuhe3490622013-03-13 01:24:30 -070080
Tyler Chatow6738c362019-02-16 14:12:30 -080081 def Write(self, header_file, cc_file):
82 """Writes the loops to the specified files."""
83 self.WriteHeader(header_file)
84 self.WriteCC(os.path.basename(header_file), cc_file)
Austin Schuhe3490622013-03-13 01:24:30 -070085
Tyler Chatow6738c362019-02-16 14:12:30 -080086 def _GenericType(self, typename, extra_args=None):
87 """Returns a loop template using typename for the type."""
88 num_states = self._loops[0].A.shape[0]
89 num_inputs = self._loops[0].B.shape[1]
90 num_outputs = self._loops[0].C.shape[0]
91 if extra_args is not None:
92 extra_args = ', ' + extra_args
93 else:
94 extra_args = ''
95 if self._scalar_type != 'double':
96 extra_args += ', ' + self._scalar_type
97 return '%s<%d, %d, %d%s>' % (typename, num_states, num_inputs,
98 num_outputs, extra_args)
Austin Schuh32501832017-02-25 18:32:56 -080099
Tyler Chatow6738c362019-02-16 14:12:30 -0800100 def _ControllerType(self):
101 """Returns a template name for StateFeedbackController."""
102 return self._GenericType('StateFeedbackController')
Austin Schuhe3490622013-03-13 01:24:30 -0700103
Tyler Chatow6738c362019-02-16 14:12:30 -0800104 def _ObserverType(self):
105 """Returns a template name for StateFeedbackObserver."""
106 return self._GenericType(self._observer_type)
Austin Schuh20388b62017-11-23 22:40:46 -0800107
Tyler Chatow6738c362019-02-16 14:12:30 -0800108 def _LoopType(self):
109 """Returns a template name for StateFeedbackLoop."""
110 num_states = self._loops[0].A.shape[0]
111 num_inputs = self._loops[0].B.shape[1]
112 num_outputs = self._loops[0].C.shape[0]
Austin Schuh20388b62017-11-23 22:40:46 -0800113
Tyler Chatow6738c362019-02-16 14:12:30 -0800114 return 'StateFeedbackLoop<%d, %d, %d, %s, %s, %s>' % (
115 num_states, num_inputs, num_outputs, self._scalar_type,
116 self._PlantType(), self._ObserverType())
Austin Schuhe3490622013-03-13 01:24:30 -0700117
Tyler Chatow6738c362019-02-16 14:12:30 -0800118 def _PlantType(self):
119 """Returns a template name for StateFeedbackPlant."""
120 return self._GenericType(self._plant_type)
Austin Schuhe3490622013-03-13 01:24:30 -0700121
Tyler Chatow6738c362019-02-16 14:12:30 -0800122 def _PlantCoeffType(self):
123 """Returns a template name for StateFeedbackPlantCoefficients."""
124 return self._GenericType(self._plant_type + 'Coefficients')
Austin Schuhe3490622013-03-13 01:24:30 -0700125
Tyler Chatow6738c362019-02-16 14:12:30 -0800126 def _ControllerCoeffType(self):
127 """Returns a template name for StateFeedbackControllerCoefficients."""
128 return self._GenericType('StateFeedbackControllerCoefficients')
Austin Schuh32501832017-02-25 18:32:56 -0800129
Tyler Chatow6738c362019-02-16 14:12:30 -0800130 def _ObserverCoeffType(self):
131 """Returns a template name for StateFeedbackObserverCoefficients."""
132 return self._GenericType(self._observer_type + 'Coefficients')
Austin Schuh32501832017-02-25 18:32:56 -0800133
Tyler Chatow6738c362019-02-16 14:12:30 -0800134 def WriteHeader(self, header_file):
135 """Writes the header file to the file named header_file."""
136 with open(header_file, 'w') as fd:
137 header_guard = self._HeaderGuard(header_file)
138 fd.write('#ifndef %s\n'
139 '#define %s\n\n' % (header_guard, header_guard))
140 fd.write(
141 '#include \"frc971/control_loops/state_feedback_loop.h\"\n')
142 if (self._plant_type == 'StateFeedbackHybridPlant' or
143 self._observer_type == 'HybridKalman'):
144 fd.write(
145 '#include \"frc971/control_loops/hybrid_state_feedback_loop.h\"\n'
146 )
Austin Schuh4cc4fe22017-11-23 19:13:09 -0800147
Tyler Chatow6738c362019-02-16 14:12:30 -0800148 fd.write('\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700149
Tyler Chatow6738c362019-02-16 14:12:30 -0800150 fd.write(self._namespace_start)
Ben Fredrickson1b45f782014-02-23 07:44:36 +0000151
Tyler Chatow6738c362019-02-16 14:12:30 -0800152 for const in self._constant_list:
153 fd.write(const.Render(self._scalar_type))
Ben Fredrickson1b45f782014-02-23 07:44:36 +0000154
Tyler Chatow6738c362019-02-16 14:12:30 -0800155 fd.write('\n\n')
156 for loop in self._loops:
157 fd.write(loop.DumpPlantHeader(self._PlantCoeffType()))
158 fd.write('\n')
159 fd.write(loop.DumpControllerHeader(self._scalar_type))
160 fd.write('\n')
161 fd.write(loop.DumpObserverHeader(self._ObserverCoeffType()))
162 fd.write('\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700163
Tyler Chatow6738c362019-02-16 14:12:30 -0800164 fd.write('%s Make%sPlant();\n\n' % (self._PlantType(),
165 self._gain_schedule_name))
Austin Schuhe3490622013-03-13 01:24:30 -0700166
Tyler Chatow6738c362019-02-16 14:12:30 -0800167 fd.write('%s Make%sController();\n\n' % (self._ControllerType(),
168 self._gain_schedule_name))
Austin Schuh32501832017-02-25 18:32:56 -0800169
Tyler Chatow6738c362019-02-16 14:12:30 -0800170 fd.write('%s Make%sObserver();\n\n' % (self._ObserverType(),
171 self._gain_schedule_name))
Austin Schuh32501832017-02-25 18:32:56 -0800172
Tyler Chatow6738c362019-02-16 14:12:30 -0800173 fd.write('%s Make%sLoop();\n\n' % (self._LoopType(),
174 self._gain_schedule_name))
Austin Schuhe3490622013-03-13 01:24:30 -0700175
Tyler Chatow6738c362019-02-16 14:12:30 -0800176 fd.write(self._namespace_end)
177 fd.write('\n\n')
178 fd.write("#endif // %s\n" % header_guard)
Austin Schuhe3490622013-03-13 01:24:30 -0700179
Tyler Chatow6738c362019-02-16 14:12:30 -0800180 def WriteCC(self, header_file_name, cc_file):
181 """Writes the cc file to the file named cc_file."""
182 with open(cc_file, 'w') as fd:
183 fd.write('#include \"%s/%s\"\n' % (os.path.join(*self._namespaces),
184 header_file_name))
185 fd.write('\n')
James Kuszmaul03be1242020-02-21 14:52:04 -0800186 fd.write('#include <chrono>\n')
Tyler Chatow6738c362019-02-16 14:12:30 -0800187 fd.write('#include <vector>\n')
188 fd.write('\n')
189 fd.write(
190 '#include \"frc971/control_loops/state_feedback_loop.h\"\n')
191 fd.write('\n')
192 fd.write(self._namespace_start)
193 fd.write('\n\n')
194 for loop in self._loops:
195 fd.write(
196 loop.DumpPlant(self._PlantCoeffType(), self._scalar_type))
197 fd.write('\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700198
Tyler Chatow6738c362019-02-16 14:12:30 -0800199 for loop in self._loops:
200 fd.write(loop.DumpController(self._scalar_type))
201 fd.write('\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700202
Tyler Chatow6738c362019-02-16 14:12:30 -0800203 for loop in self._loops:
204 fd.write(
205 loop.DumpObserver(self._ObserverCoeffType(),
206 self._scalar_type))
207 fd.write('\n')
Austin Schuh32501832017-02-25 18:32:56 -0800208
Tyler Chatow6738c362019-02-16 14:12:30 -0800209 fd.write('%s Make%sPlant() {\n' % (self._PlantType(),
210 self._gain_schedule_name))
211 fd.write(' ::std::vector< ::std::unique_ptr<%s>> plants(%d);\n' %
212 (self._PlantCoeffType(), len(self._loops)))
213 for index, loop in enumerate(self._loops):
214 fd.write(' plants[%d] = ::std::unique_ptr<%s>(new %s(%s));\n' %
215 (index, self._PlantCoeffType(), self._PlantCoeffType(),
216 loop.PlantFunction()))
217 fd.write(' return %s(&plants);\n' % self._PlantType())
218 fd.write('}\n\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700219
Tyler Chatow6738c362019-02-16 14:12:30 -0800220 fd.write('%s Make%sController() {\n' % (self._ControllerType(),
221 self._gain_schedule_name))
222 fd.write(
223 ' ::std::vector< ::std::unique_ptr<%s>> controllers(%d);\n' %
224 (self._ControllerCoeffType(), len(self._loops)))
225 for index, loop in enumerate(self._loops):
226 fd.write(
227 ' controllers[%d] = ::std::unique_ptr<%s>(new %s(%s));\n' %
228 (index, self._ControllerCoeffType(),
229 self._ControllerCoeffType(), loop.ControllerFunction()))
230 fd.write(' return %s(&controllers);\n' % self._ControllerType())
231 fd.write('}\n\n')
Austin Schuh32501832017-02-25 18:32:56 -0800232
Tyler Chatow6738c362019-02-16 14:12:30 -0800233 fd.write('%s Make%sObserver() {\n' % (self._ObserverType(),
234 self._gain_schedule_name))
235 fd.write(' ::std::vector< ::std::unique_ptr<%s>> observers(%d);\n'
236 % (self._ObserverCoeffType(), len(self._loops)))
237 for index, loop in enumerate(self._loops):
238 fd.write(
239 ' observers[%d] = ::std::unique_ptr<%s>(new %s(%s));\n'
240 % (index, self._ObserverCoeffType(),
241 self._ObserverCoeffType(), loop.ObserverFunction()))
242 fd.write(' return %s(&observers);\n' % self._ObserverType())
243 fd.write('}\n\n')
Austin Schuh32501832017-02-25 18:32:56 -0800244
Tyler Chatow6738c362019-02-16 14:12:30 -0800245 fd.write('%s Make%sLoop() {\n' % (self._LoopType(),
246 self._gain_schedule_name))
247 fd.write(
248 ' return %s(Make%sPlant(), Make%sController(), Make%sObserver());\n'
249 % (self._LoopType(), self._gain_schedule_name,
250 self._gain_schedule_name, self._gain_schedule_name))
251 fd.write('}\n\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700252
Tyler Chatow6738c362019-02-16 14:12:30 -0800253 fd.write(self._namespace_end)
254 fd.write('\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700255
256
Austin Schuh3c542312013-02-24 01:53:50 -0800257class ControlLoop(object):
Austin Schuh3c542312013-02-24 01:53:50 -0800258
Tyler Chatow6738c362019-02-16 14:12:30 -0800259 def __init__(self, name):
260 """Constructs a control loop object.
Austin Schuh3c542312013-02-24 01:53:50 -0800261
Tyler Chatow6738c362019-02-16 14:12:30 -0800262 Args:
263 name: string, The name of the loop to use when writing the C++ files.
264 """
265 self._name = name
Austin Schuhb5d302f2019-01-20 20:51:19 -0800266
Tyler Chatow6738c362019-02-16 14:12:30 -0800267 @property
268 def name(self):
269 """Returns the name"""
270 return self._name
Austin Schuh3c542312013-02-24 01:53:50 -0800271
Tyler Chatow6738c362019-02-16 14:12:30 -0800272 def ContinuousToDiscrete(self, A_continuous, B_continuous, dt):
273 """Calculates the discrete time values for A and B.
Austin Schuhc1f68892013-03-16 17:06:27 -0700274
Tyler Chatow6738c362019-02-16 14:12:30 -0800275 Args:
276 A_continuous: numpy.matrix, The continuous time A matrix
277 B_continuous: numpy.matrix, The continuous time B matrix
278 dt: float, The time step of the control loop
Austin Schuhc1f68892013-03-16 17:06:27 -0700279
Tyler Chatow6738c362019-02-16 14:12:30 -0800280 Returns:
281 (A, B), numpy.matrix, the control matricies.
282 """
283 return controls.c2d(A_continuous, B_continuous, dt)
Austin Schuh3c542312013-02-24 01:53:50 -0800284
Tyler Chatow6738c362019-02-16 14:12:30 -0800285 def InitializeState(self):
286 """Sets X, Y, and X_hat to zero defaults."""
287 self.X = numpy.zeros((self.A.shape[0], 1))
288 self.Y = self.C * self.X
289 self.X_hat = numpy.zeros((self.A.shape[0], 1))
Austin Schuh3c542312013-02-24 01:53:50 -0800290
Tyler Chatow6738c362019-02-16 14:12:30 -0800291 def PlaceControllerPoles(self, poles):
292 """Places the controller poles.
Austin Schuh3c542312013-02-24 01:53:50 -0800293
Tyler Chatow6738c362019-02-16 14:12:30 -0800294 Args:
295 poles: array, An array of poles. Must be complex conjegates if they have
296 any imaginary portions.
297 """
298 self.K = controls.dplace(self.A, self.B, poles)
Austin Schuh3c542312013-02-24 01:53:50 -0800299
Tyler Chatow6738c362019-02-16 14:12:30 -0800300 def PlaceObserverPoles(self, poles):
301 """Places the observer poles.
Austin Schuh3c542312013-02-24 01:53:50 -0800302
Tyler Chatow6738c362019-02-16 14:12:30 -0800303 Args:
304 poles: array, An array of poles. Must be complex conjegates if they have
305 any imaginary portions.
306 """
307 self.L = controls.dplace(self.A.T, self.C.T, poles).T
Sabina Davis3922dfa2018-02-10 23:10:05 -0800308
Tyler Chatow6738c362019-02-16 14:12:30 -0800309 def Update(self, U):
310 """Simulates one time step with the provided U."""
311 #U = numpy.clip(U, self.U_min, self.U_max)
312 self.X = self.A * self.X + self.B * U
313 self.Y = self.C * self.X + self.D * U
Austin Schuh3c542312013-02-24 01:53:50 -0800314
Tyler Chatow6738c362019-02-16 14:12:30 -0800315 def PredictObserver(self, U):
316 """Runs the predict step of the observer update."""
317 self.X_hat = (self.A * self.X_hat + self.B * U)
Austin Schuh1a387962015-01-31 16:36:20 -0800318
Tyler Chatow6738c362019-02-16 14:12:30 -0800319 def CorrectObserver(self, U):
320 """Runs the correct step of the observer update."""
321 if hasattr(self, 'KalmanGain'):
322 KalmanGain = self.KalmanGain
323 else:
324 KalmanGain = numpy.linalg.inv(self.A) * self.L
325 self.X_hat += KalmanGain * (self.Y - self.C * self.X_hat - self.D * U)
Austin Schuh1a387962015-01-31 16:36:20 -0800326
Tyler Chatow6738c362019-02-16 14:12:30 -0800327 def UpdateObserver(self, U):
328 """Updates the observer given the provided U."""
329 if hasattr(self, 'KalmanGain'):
330 KalmanGain = self.KalmanGain
331 else:
332 KalmanGain = numpy.linalg.inv(self.A) * self.L
333 self.X_hat = (self.A * self.X_hat + self.B * U + self.A * KalmanGain *
334 (self.Y - self.C * self.X_hat - self.D * U))
Austin Schuh3c542312013-02-24 01:53:50 -0800335
Tyler Chatow6738c362019-02-16 14:12:30 -0800336 def _DumpMatrix(self, matrix_name, matrix, scalar_type):
337 """Dumps the provided matrix into a variable called matrix_name.
Austin Schuh3c542312013-02-24 01:53:50 -0800338
Tyler Chatow6738c362019-02-16 14:12:30 -0800339 Args:
340 matrix_name: string, The variable name to save the matrix to.
341 matrix: The matrix to dump.
342 scalar_type: The C++ type to use for the scalar in the matrix.
Austin Schuh3c542312013-02-24 01:53:50 -0800343
Tyler Chatow6738c362019-02-16 14:12:30 -0800344 Returns:
345 string, The C++ commands required to populate a variable named matrix_name
346 with the contents of matrix.
347 """
348 ans = [
349 ' Eigen::Matrix<%s, %d, %d> %s;\n' % (scalar_type, matrix.shape[0],
350 matrix.shape[1], matrix_name)
351 ]
352 for x in xrange(matrix.shape[0]):
353 for y in xrange(matrix.shape[1]):
354 write_type = repr(matrix[x, y])
355 if scalar_type == 'float':
356 if '.' not in write_type:
357 write_type += '.0'
358 write_type += 'f'
359 ans.append(
360 ' %s(%d, %d) = %s;\n' % (matrix_name, x, y, write_type))
Austin Schuh3c542312013-02-24 01:53:50 -0800361
Tyler Chatow6738c362019-02-16 14:12:30 -0800362 return ''.join(ans)
Austin Schuh3c542312013-02-24 01:53:50 -0800363
Tyler Chatow6738c362019-02-16 14:12:30 -0800364 def DumpPlantHeader(self, plant_coefficient_type):
365 """Writes out a c++ header declaration which will create a Plant object.
Austin Schuh3c542312013-02-24 01:53:50 -0800366
Tyler Chatow6738c362019-02-16 14:12:30 -0800367 Returns:
368 string, The header declaration for the function.
369 """
370 return '%s Make%sPlantCoefficients();\n' % (plant_coefficient_type,
371 self._name)
Austin Schuh3c542312013-02-24 01:53:50 -0800372
Tyler Chatow6738c362019-02-16 14:12:30 -0800373 def DumpPlant(self, plant_coefficient_type, scalar_type):
374 """Writes out a c++ function which will create a PlantCoefficients object.
Austin Schuh3c542312013-02-24 01:53:50 -0800375
Tyler Chatow6738c362019-02-16 14:12:30 -0800376 Returns:
377 string, The function which will create the object.
378 """
379 ans = [
380 '%s Make%sPlantCoefficients() {\n' % (plant_coefficient_type,
381 self._name)
382 ]
Austin Schuh3c542312013-02-24 01:53:50 -0800383
Tyler Chatow6738c362019-02-16 14:12:30 -0800384 ans.append(self._DumpMatrix('C', self.C, scalar_type))
385 ans.append(self._DumpMatrix('D', self.D, scalar_type))
386 ans.append(self._DumpMatrix('U_max', self.U_max, scalar_type))
387 ans.append(self._DumpMatrix('U_min', self.U_min, scalar_type))
Austin Schuh3c542312013-02-24 01:53:50 -0800388
Tyler Chatow6738c362019-02-16 14:12:30 -0800389 if plant_coefficient_type.startswith('StateFeedbackPlant'):
390 ans.append(self._DumpMatrix('A', self.A, scalar_type))
391 ans.append(self._DumpMatrix('B', self.B, scalar_type))
392 ans.append(
James Kuszmaul03be1242020-02-21 14:52:04 -0800393 ' const std::chrono::nanoseconds dt(%d);\n' % (self.dt * 1e9))
394 ans.append(
Tyler Chatow6738c362019-02-16 14:12:30 -0800395 ' return %s'
James Kuszmaul03be1242020-02-21 14:52:04 -0800396 '(A, B, C, D, U_max, U_min, dt);\n' % (plant_coefficient_type))
Tyler Chatow6738c362019-02-16 14:12:30 -0800397 elif plant_coefficient_type.startswith('StateFeedbackHybridPlant'):
398 ans.append(
399 self._DumpMatrix('A_continuous', self.A_continuous,
400 scalar_type))
401 ans.append(
402 self._DumpMatrix('B_continuous', self.B_continuous,
403 scalar_type))
404 ans.append(' return %s'
405 '(A_continuous, B_continuous, C, D, U_max, U_min);\n' %
406 (plant_coefficient_type))
407 else:
408 glog.fatal('Unsupported plant type %s', plant_coefficient_type)
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800409
Tyler Chatow6738c362019-02-16 14:12:30 -0800410 ans.append('}\n')
411 return ''.join(ans)
Austin Schuh3c542312013-02-24 01:53:50 -0800412
Tyler Chatow6738c362019-02-16 14:12:30 -0800413 def PlantFunction(self):
414 """Returns the name of the plant coefficient function."""
415 return 'Make%sPlantCoefficients()' % self._name
Austin Schuh3c542312013-02-24 01:53:50 -0800416
Tyler Chatow6738c362019-02-16 14:12:30 -0800417 def ControllerFunction(self):
418 """Returns the name of the controller function."""
419 return 'Make%sControllerCoefficients()' % self._name
Austin Schuh32501832017-02-25 18:32:56 -0800420
Tyler Chatow6738c362019-02-16 14:12:30 -0800421 def ObserverFunction(self):
422 """Returns the name of the controller function."""
423 return 'Make%sObserverCoefficients()' % self._name
Austin Schuhe3490622013-03-13 01:24:30 -0700424
Tyler Chatow6738c362019-02-16 14:12:30 -0800425 def DumpControllerHeader(self, scalar_type):
426 """Writes out a c++ header declaration which will create a Controller object.
Austin Schuh3c542312013-02-24 01:53:50 -0800427
Tyler Chatow6738c362019-02-16 14:12:30 -0800428 Returns:
429 string, The header declaration for the function.
430 """
431 num_states = self.A.shape[0]
432 num_inputs = self.B.shape[1]
433 num_outputs = self.C.shape[0]
434 return 'StateFeedbackControllerCoefficients<%d, %d, %d, %s> %s;\n' % (
435 num_states, num_inputs, num_outputs, scalar_type,
436 self.ControllerFunction())
Austin Schuh3c542312013-02-24 01:53:50 -0800437
Tyler Chatow6738c362019-02-16 14:12:30 -0800438 def DumpController(self, scalar_type):
439 """Returns a c++ function which will create a Controller object.
Austin Schuh3c542312013-02-24 01:53:50 -0800440
Tyler Chatow6738c362019-02-16 14:12:30 -0800441 Returns:
442 string, The function which will create the object.
443 """
444 num_states = self.A.shape[0]
445 num_inputs = self.B.shape[1]
446 num_outputs = self.C.shape[0]
447 ans = [
448 'StateFeedbackControllerCoefficients<%d, %d, %d, %s> %s {\n' %
449 (num_states, num_inputs, num_outputs, scalar_type,
450 self.ControllerFunction())
451 ]
Austin Schuh3c542312013-02-24 01:53:50 -0800452
Tyler Chatow6738c362019-02-16 14:12:30 -0800453 ans.append(self._DumpMatrix('K', self.K, scalar_type))
454 if not hasattr(self, 'Kff'):
455 self.Kff = numpy.matrix(numpy.zeros(self.K.shape))
Austin Schuh86093ad2016-02-06 14:29:34 -0800456
Tyler Chatow6738c362019-02-16 14:12:30 -0800457 ans.append(self._DumpMatrix('Kff', self.Kff, scalar_type))
Austin Schuh3c542312013-02-24 01:53:50 -0800458
Tyler Chatow6738c362019-02-16 14:12:30 -0800459 ans.append(
460 ' return StateFeedbackControllerCoefficients<%d, %d, %d, %s>'
461 '(K, Kff);\n' % (num_states, num_inputs, num_outputs, scalar_type))
462 ans.append('}\n')
463 return ''.join(ans)
Austin Schuh32501832017-02-25 18:32:56 -0800464
Tyler Chatow6738c362019-02-16 14:12:30 -0800465 def DumpObserverHeader(self, observer_coefficient_type):
466 """Writes out a c++ header declaration which will create a Observer object.
Austin Schuh32501832017-02-25 18:32:56 -0800467
Tyler Chatow6738c362019-02-16 14:12:30 -0800468 Returns:
469 string, The header declaration for the function.
470 """
471 return '%s %s;\n' % (observer_coefficient_type, self.ObserverFunction())
Austin Schuh32501832017-02-25 18:32:56 -0800472
Tyler Chatow6738c362019-02-16 14:12:30 -0800473 def DumpObserver(self, observer_coefficient_type, scalar_type):
474 """Returns a c++ function which will create a Observer object.
Austin Schuh32501832017-02-25 18:32:56 -0800475
Tyler Chatow6738c362019-02-16 14:12:30 -0800476 Returns:
477 string, The function which will create the object.
478 """
479 ans = [
480 '%s %s {\n' % (observer_coefficient_type, self.ObserverFunction())
481 ]
Austin Schuh32501832017-02-25 18:32:56 -0800482
Tyler Chatow6738c362019-02-16 14:12:30 -0800483 if observer_coefficient_type.startswith('StateFeedbackObserver'):
484 if hasattr(self, 'KalmanGain'):
485 KalmanGain = self.KalmanGain
486 Q = self.Q
487 R = self.R
488 else:
489 KalmanGain = numpy.linalg.inv(self.A) * self.L
490 Q = numpy.zeros(self.A.shape)
491 R = numpy.zeros((self.C.shape[0], self.C.shape[0]))
492 ans.append(self._DumpMatrix('KalmanGain', KalmanGain, scalar_type))
493 ans.append(self._DumpMatrix('Q', Q, scalar_type))
494 ans.append(self._DumpMatrix('R', R, scalar_type))
495 ans.append(' return %s(KalmanGain, Q, R);\n' %
496 (observer_coefficient_type,))
Sabina Davis3922dfa2018-02-10 23:10:05 -0800497
Tyler Chatow6738c362019-02-16 14:12:30 -0800498 elif observer_coefficient_type.startswith('HybridKalman'):
499 ans.append(
500 self._DumpMatrix('Q_continuous', self.Q_continuous,
501 scalar_type))
502 ans.append(
503 self._DumpMatrix('R_continuous', self.R_continuous,
504 scalar_type))
505 ans.append(
506 self._DumpMatrix('P_steady_state', self.P_steady_state,
507 scalar_type))
508 ans.append(
509 ' return %s(Q_continuous, R_continuous, P_steady_state);\n' %
510 (observer_coefficient_type,))
511 else:
512 glog.fatal('Unsupported observer type %s',
513 observer_coefficient_type)
Austin Schuh32501832017-02-25 18:32:56 -0800514
Tyler Chatow6738c362019-02-16 14:12:30 -0800515 ans.append('}\n')
516 return ''.join(ans)
517
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800518
519class HybridControlLoop(ControlLoop):
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800520
Tyler Chatow6738c362019-02-16 14:12:30 -0800521 def __init__(self, name):
522 super(HybridControlLoop, self).__init__(name=name)
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800523
Tyler Chatow6738c362019-02-16 14:12:30 -0800524 def Discretize(self, dt):
525 [self.A, self.B, self.Q, self.R] = \
526 controls.kalmd(self.A_continuous, self.B_continuous,
527 self.Q_continuous, self.R_continuous, dt)
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800528
Tyler Chatow6738c362019-02-16 14:12:30 -0800529 def PredictHybridObserver(self, U, dt):
530 self.Discretize(dt)
531 self.X_hat = self.A * self.X_hat + self.B * U
532 self.P = (self.A * self.P * self.A.T + self.Q)
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800533
Tyler Chatow6738c362019-02-16 14:12:30 -0800534 def CorrectHybridObserver(self, U):
535 Y_bar = self.Y - self.C * self.X_hat
536 C_t = self.C.T
537 S = self.C * self.P * C_t + self.R
538 self.KalmanGain = self.P * C_t * numpy.linalg.inv(S)
539 self.X_hat = self.X_hat + self.KalmanGain * Y_bar
540 self.P = (numpy.eye(len(self.A)) - self.KalmanGain * self.C) * self.P
541
542 def InitializeState(self):
543 super(HybridControlLoop, self).InitializeState()
544 if hasattr(self, 'Q_steady_state'):
545 self.P = self.Q_steady_state
546 else:
547 self.P = numpy.matrix(
548 numpy.zeros((self.A.shape[0], self.A.shape[0])))
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800549
550
551class CIM(object):
Tyler Chatow6738c362019-02-16 14:12:30 -0800552
553 def __init__(self):
554 # Stall Torque in N m
555 self.stall_torque = 2.42
556 # Stall Current in Amps
557 self.stall_current = 133.0
558 # Free Speed in rad/s
559 self.free_speed = 5500.0 / 60.0 * 2.0 * numpy.pi
560 # Free Current in Amps
561 self.free_current = 4.7
562 # Resistance of the motor
563 self.resistance = 12.0 / self.stall_current
564 # Motor velocity constant
565 self.Kv = (
566 self.free_speed / (12.0 - self.resistance * self.free_current))
567 # Torque constant
568 self.Kt = self.stall_torque / self.stall_current
Lee Mracek97fc8af2018-01-13 04:38:52 -0500569
570
571class MiniCIM(object):
Tyler Chatow6738c362019-02-16 14:12:30 -0800572
573 def __init__(self):
574 # Stall Torque in N m
575 self.stall_torque = 1.41
576 # Stall Current in Amps
577 self.stall_current = 89.0
578 # Free Speed in rad/s
579 self.free_speed = 5840.0 / 60.0 * 2.0 * numpy.pi
580 # Free Current in Amps
581 self.free_current = 3.0
582 # Resistance of the motor
583 self.resistance = 12.0 / self.stall_current
584 # Motor velocity constant
585 self.Kv = (
586 self.free_speed / (12.0 - self.resistance * self.free_current))
587 # Torque constant
588 self.Kt = self.stall_torque / self.stall_current
Austin Schuhf173eb82018-01-20 23:32:30 -0800589
590
Austin Schuhb5d302f2019-01-20 20:51:19 -0800591class NMotor(object):
Tyler Chatow6738c362019-02-16 14:12:30 -0800592
Austin Schuhb5d302f2019-01-20 20:51:19 -0800593 def __init__(self, motor, n):
594 """Gangs together n motors."""
595 self.motor = motor
596 self.stall_torque = motor.stall_torque * n
597 self.stall_current = motor.stall_current * n
598 self.free_speed = motor.free_speed
599
600 self.free_current = motor.free_current * n
601 self.resistance = motor.resistance / n
602 self.Kv = motor.Kv
603 self.Kt = motor.Kt
Austin Schuh36bb8e32019-02-18 15:02:57 -0800604 self.motor_inertia = motor.motor_inertia * n
Austin Schuhb5d302f2019-01-20 20:51:19 -0800605
606
607class Vex775Pro(object):
Tyler Chatow6738c362019-02-16 14:12:30 -0800608
Austin Schuhb5d302f2019-01-20 20:51:19 -0800609 def __init__(self):
610 # Stall Torque in N m
611 self.stall_torque = 0.71
612 # Stall Current in Amps
613 self.stall_current = 134.0
614 # Free Speed in rad/s
615 self.free_speed = 18730.0 / 60.0 * 2.0 * numpy.pi
616 # Free Current in Amps
617 self.free_current = 0.7
618 # Resistance of the motor
619 self.resistance = 12.0 / self.stall_current
620 # Motor velocity constant
Tyler Chatow6738c362019-02-16 14:12:30 -0800621 self.Kv = (
622 self.free_speed / (12.0 - self.resistance * self.free_current))
Austin Schuhb5d302f2019-01-20 20:51:19 -0800623 # Torque constant
624 self.Kt = self.stall_torque / self.stall_current
625 # Motor inertia in kg m^2
626 self.motor_inertia = 0.00001187
627
628
Austin Schuhf173eb82018-01-20 23:32:30 -0800629class BAG(object):
Tyler Chatow6738c362019-02-16 14:12:30 -0800630 # BAG motor specs available at http://motors.vex.com/vexpro-motors/bag-motor
631 def __init__(self):
632 # Stall Torque in (N m)
633 self.stall_torque = 0.43
634 # Stall Current in (Amps)
635 self.stall_current = 53.0
636 # Free Speed in (rad/s)
637 self.free_speed = 13180.0 / 60.0 * 2.0 * numpy.pi
638 # Free Current in (Amps)
639 self.free_current = 1.8
640 # Resistance of the motor (Ohms)
641 self.resistance = 12.0 / self.stall_current
642 # Motor velocity constant (radians / (sec * volt))
643 self.Kv = (
644 self.free_speed / (12.0 - self.resistance * self.free_current))
645 # Torque constant (N * m / A)
646 self.Kt = self.stall_torque / self.stall_current
647 # Motor inertia in kg m^2
648 self.motor_inertia = 0.000006
649
Brian Silverman6260c092018-01-14 15:21:36 -0800650
651class MN3510(object):
Tyler Chatow6738c362019-02-16 14:12:30 -0800652
653 def __init__(self):
654 # http://www.robotshop.com/en/t-motor-navigator-mn3510-360kv-brushless-motor.html#Specifications
655 # Free Current in Amps
656 self.free_current = 0.0
657 # Resistance of the motor
658 self.resistance = 0.188
659 # Stall Current in Amps
660 self.stall_current = 14.0 / self.resistance
661 # Motor velocity constant
662 self.Kv = 360.0 / 60.0 * (2.0 * numpy.pi)
663 # Torque constant Nm / A
664 self.Kt = 1.0 / self.Kv
665 # Stall Torque in N m
666 self.stall_torque = self.Kt * self.stall_current
James Kuszmaulef0c18a2020-01-12 15:44:20 -0800667
668
669class Falcon(object):
670 """Class representing the VexPro Falcon 500 motor.
671
672 All numbers based on data from
673 https://www.vexrobotics.com/vexpro/falcon-500."""
674
675 def __init__(self):
676 # Stall Torque in N m
677 self.stall_torque = 4.69
678 # Stall Current in Amps
679 self.stall_current = 257.0
680 # Free Speed in rad / sec
681 self.free_speed = 6380.0 / 60.0 * 2.0 * numpy.pi
682 # Free Current in Amps
683 self.free_current = 1.5
684 # Resistance of the motor, divided by 2 to account for the 2 motors
685 self.resistance = 12.0 / self.stall_current
686 # Motor velocity constant
687 self.Kv = (self.free_speed /
688 (12.0 - self.resistance * self.free_current))
689 # Torque constant
690 self.Kt = self.stall_torque / self.stall_current