blob: c3dc1a2be273b3d60febdd430c78f7f753e164fd [file] [log] [blame]
Austin Schuhce7e03d2020-11-20 22:32:44 -08001import frc971.control_loops.python.controls as controls
Austin Schuh3c542312013-02-24 01:53:50 -08002import 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
Austin Schuhe8ca06a2020-03-07 22:27:39 -08008 def __init__(self, name, formatt, value, comment=None):
Tyler Chatow6738c362019-02-16 14:12:30 -08009 self.name = name
10 self.formatt = formatt
11 self.value = value
12 self.formatToType = {}
13 self.formatToType['%f'] = "double"
14 self.formatToType['%d'] = "int"
Austin Schuhe8ca06a2020-03-07 22:27:39 -080015 if comment is None:
16 self.comment = ""
17 else:
18 self.comment = comment + "\n"
Tyler Chatow6738c362019-02-16 14:12:30 -080019
20 def Render(self, loop_type):
21 typestring = self.formatToType[self.formatt]
22 if loop_type == 'float' and typestring == 'double':
23 typestring = loop_type
Austin Schuhe8ca06a2020-03-07 22:27:39 -080024 return str("\n%sstatic constexpr %s %s = "+ self.formatt +";\n") % \
25 (self.comment, typestring, self.name, self.value)
Ben Fredrickson1b45f782014-02-23 07:44:36 +000026
27
Austin Schuhe3490622013-03-13 01:24:30 -070028class ControlLoopWriter(object):
Austin Schuhe3490622013-03-13 01:24:30 -070029
Tyler Chatow6738c362019-02-16 14:12:30 -080030 def __init__(self,
31 gain_schedule_name,
32 loops,
33 namespaces=None,
34 write_constants=False,
35 plant_type='StateFeedbackPlant',
36 observer_type='StateFeedbackObserver',
37 scalar_type='double'):
38 """Constructs a control loop writer.
Austin Schuhe3490622013-03-13 01:24:30 -070039
Tyler Chatow6738c362019-02-16 14:12:30 -080040 Args:
41 gain_schedule_name: string, Name of the overall controller.
42 loops: array[ControlLoop], a list of control loops to gain schedule
43 in order.
44 namespaces: array[string], a list of names of namespaces to nest in
45 order. If None, the default will be used.
46 plant_type: string, The C++ type of the plant.
47 observer_type: string, The C++ type of the observer.
48 scalar_type: string, The C++ type of the base scalar.
49 """
50 self._gain_schedule_name = gain_schedule_name
51 self._loops = loops
52 if namespaces:
53 self._namespaces = namespaces
54 else:
55 self._namespaces = ['frc971', 'control_loops']
Austin Schuhe3490622013-03-13 01:24:30 -070056
Tyler Chatow6738c362019-02-16 14:12:30 -080057 self._namespace_start = '\n'.join(
58 ['namespace %s {' % name for name in self._namespaces])
Austin Schuh86093ad2016-02-06 14:29:34 -080059
Tyler Chatow6738c362019-02-16 14:12:30 -080060 self._namespace_end = '\n'.join([
61 '} // namespace %s' % name for name in reversed(self._namespaces)
62 ])
Austin Schuh25933852014-02-23 02:04:13 -080063
Tyler Chatow6738c362019-02-16 14:12:30 -080064 self._constant_list = []
65 self._plant_type = plant_type
66 self._observer_type = observer_type
67 self._scalar_type = scalar_type
Austin Schuh25933852014-02-23 02:04:13 -080068
Tyler Chatow6738c362019-02-16 14:12:30 -080069 def AddConstant(self, constant):
70 """Adds a constant to write.
Austin Schuhe3490622013-03-13 01:24:30 -070071
Tyler Chatow6738c362019-02-16 14:12:30 -080072 Args:
73 constant: Constant, the constant to add to the header.
74 """
75 self._constant_list.append(constant)
Brian Silvermane51ad632014-01-08 15:12:29 -080076
Tyler Chatow6738c362019-02-16 14:12:30 -080077 def _TopDirectory(self):
78 return self._namespaces[0]
Austin Schuhe3490622013-03-13 01:24:30 -070079
Tyler Chatow6738c362019-02-16 14:12:30 -080080 def _HeaderGuard(self, header_file):
81 return ('_'.join([namespace.upper() for namespace in self._namespaces])
82 + '_' + os.path.basename(header_file).upper().replace(
83 '.', '_').replace('/', '_') + '_')
Austin Schuhe3490622013-03-13 01:24:30 -070084
Tyler Chatow6738c362019-02-16 14:12:30 -080085 def Write(self, header_file, cc_file):
86 """Writes the loops to the specified files."""
87 self.WriteHeader(header_file)
88 self.WriteCC(os.path.basename(header_file), cc_file)
Austin Schuhe3490622013-03-13 01:24:30 -070089
Tyler Chatow6738c362019-02-16 14:12:30 -080090 def _GenericType(self, typename, extra_args=None):
91 """Returns a loop template using typename for the type."""
92 num_states = self._loops[0].A.shape[0]
93 num_inputs = self._loops[0].B.shape[1]
94 num_outputs = self._loops[0].C.shape[0]
95 if extra_args is not None:
96 extra_args = ', ' + extra_args
97 else:
98 extra_args = ''
99 if self._scalar_type != 'double':
100 extra_args += ', ' + self._scalar_type
101 return '%s<%d, %d, %d%s>' % (typename, num_states, num_inputs,
102 num_outputs, extra_args)
Austin Schuh32501832017-02-25 18:32:56 -0800103
Tyler Chatow6738c362019-02-16 14:12:30 -0800104 def _ControllerType(self):
105 """Returns a template name for StateFeedbackController."""
106 return self._GenericType('StateFeedbackController')
Austin Schuhe3490622013-03-13 01:24:30 -0700107
Tyler Chatow6738c362019-02-16 14:12:30 -0800108 def _ObserverType(self):
109 """Returns a template name for StateFeedbackObserver."""
110 return self._GenericType(self._observer_type)
Austin Schuh20388b62017-11-23 22:40:46 -0800111
Tyler Chatow6738c362019-02-16 14:12:30 -0800112 def _LoopType(self):
113 """Returns a template name for StateFeedbackLoop."""
114 num_states = self._loops[0].A.shape[0]
115 num_inputs = self._loops[0].B.shape[1]
116 num_outputs = self._loops[0].C.shape[0]
Austin Schuh20388b62017-11-23 22:40:46 -0800117
Tyler Chatow6738c362019-02-16 14:12:30 -0800118 return 'StateFeedbackLoop<%d, %d, %d, %s, %s, %s>' % (
119 num_states, num_inputs, num_outputs, self._scalar_type,
120 self._PlantType(), self._ObserverType())
Austin Schuhe3490622013-03-13 01:24:30 -0700121
Tyler Chatow6738c362019-02-16 14:12:30 -0800122 def _PlantType(self):
123 """Returns a template name for StateFeedbackPlant."""
124 return self._GenericType(self._plant_type)
Austin Schuhe3490622013-03-13 01:24:30 -0700125
Tyler Chatow6738c362019-02-16 14:12:30 -0800126 def _PlantCoeffType(self):
127 """Returns a template name for StateFeedbackPlantCoefficients."""
128 return self._GenericType(self._plant_type + 'Coefficients')
Austin Schuhe3490622013-03-13 01:24:30 -0700129
Tyler Chatow6738c362019-02-16 14:12:30 -0800130 def _ControllerCoeffType(self):
131 """Returns a template name for StateFeedbackControllerCoefficients."""
132 return self._GenericType('StateFeedbackControllerCoefficients')
Austin Schuh32501832017-02-25 18:32:56 -0800133
Tyler Chatow6738c362019-02-16 14:12:30 -0800134 def _ObserverCoeffType(self):
135 """Returns a template name for StateFeedbackObserverCoefficients."""
136 return self._GenericType(self._observer_type + 'Coefficients')
Austin Schuh32501832017-02-25 18:32:56 -0800137
Tyler Chatow6738c362019-02-16 14:12:30 -0800138 def WriteHeader(self, header_file):
139 """Writes the header file to the file named header_file."""
140 with open(header_file, 'w') as fd:
141 header_guard = self._HeaderGuard(header_file)
142 fd.write('#ifndef %s\n'
143 '#define %s\n\n' % (header_guard, header_guard))
144 fd.write(
145 '#include \"frc971/control_loops/state_feedback_loop.h\"\n')
146 if (self._plant_type == 'StateFeedbackHybridPlant' or
147 self._observer_type == 'HybridKalman'):
148 fd.write(
149 '#include \"frc971/control_loops/hybrid_state_feedback_loop.h\"\n'
150 )
Austin Schuh4cc4fe22017-11-23 19:13:09 -0800151
Tyler Chatow6738c362019-02-16 14:12:30 -0800152 fd.write('\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700153
Tyler Chatow6738c362019-02-16 14:12:30 -0800154 fd.write(self._namespace_start)
Ben Fredrickson1b45f782014-02-23 07:44:36 +0000155
Tyler Chatow6738c362019-02-16 14:12:30 -0800156 for const in self._constant_list:
157 fd.write(const.Render(self._scalar_type))
Ben Fredrickson1b45f782014-02-23 07:44:36 +0000158
Tyler Chatow6738c362019-02-16 14:12:30 -0800159 fd.write('\n\n')
160 for loop in self._loops:
161 fd.write(loop.DumpPlantHeader(self._PlantCoeffType()))
162 fd.write('\n')
163 fd.write(loop.DumpControllerHeader(self._scalar_type))
164 fd.write('\n')
165 fd.write(loop.DumpObserverHeader(self._ObserverCoeffType()))
166 fd.write('\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700167
Tyler Chatow6738c362019-02-16 14:12:30 -0800168 fd.write('%s Make%sPlant();\n\n' % (self._PlantType(),
169 self._gain_schedule_name))
Austin Schuhe3490622013-03-13 01:24:30 -0700170
Tyler Chatow6738c362019-02-16 14:12:30 -0800171 fd.write('%s Make%sController();\n\n' % (self._ControllerType(),
172 self._gain_schedule_name))
Austin Schuh32501832017-02-25 18:32:56 -0800173
Tyler Chatow6738c362019-02-16 14:12:30 -0800174 fd.write('%s Make%sObserver();\n\n' % (self._ObserverType(),
175 self._gain_schedule_name))
Austin Schuh32501832017-02-25 18:32:56 -0800176
Tyler Chatow6738c362019-02-16 14:12:30 -0800177 fd.write('%s Make%sLoop();\n\n' % (self._LoopType(),
178 self._gain_schedule_name))
Austin Schuhe3490622013-03-13 01:24:30 -0700179
Tyler Chatow6738c362019-02-16 14:12:30 -0800180 fd.write(self._namespace_end)
181 fd.write('\n\n')
182 fd.write("#endif // %s\n" % header_guard)
Austin Schuhe3490622013-03-13 01:24:30 -0700183
Tyler Chatow6738c362019-02-16 14:12:30 -0800184 def WriteCC(self, header_file_name, cc_file):
185 """Writes the cc file to the file named cc_file."""
186 with open(cc_file, 'w') as fd:
187 fd.write('#include \"%s/%s\"\n' % (os.path.join(*self._namespaces),
188 header_file_name))
189 fd.write('\n')
James Kuszmaul03be1242020-02-21 14:52:04 -0800190 fd.write('#include <chrono>\n')
Tyler Chatow6738c362019-02-16 14:12:30 -0800191 fd.write('#include <vector>\n')
192 fd.write('\n')
193 fd.write(
194 '#include \"frc971/control_loops/state_feedback_loop.h\"\n')
195 fd.write('\n')
196 fd.write(self._namespace_start)
197 fd.write('\n\n')
198 for loop in self._loops:
199 fd.write(
200 loop.DumpPlant(self._PlantCoeffType(), 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(loop.DumpController(self._scalar_type))
205 fd.write('\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700206
Tyler Chatow6738c362019-02-16 14:12:30 -0800207 for loop in self._loops:
208 fd.write(
209 loop.DumpObserver(self._ObserverCoeffType(),
210 self._scalar_type))
211 fd.write('\n')
Austin Schuh32501832017-02-25 18:32:56 -0800212
Tyler Chatow6738c362019-02-16 14:12:30 -0800213 fd.write('%s Make%sPlant() {\n' % (self._PlantType(),
214 self._gain_schedule_name))
215 fd.write(' ::std::vector< ::std::unique_ptr<%s>> plants(%d);\n' %
216 (self._PlantCoeffType(), len(self._loops)))
217 for index, loop in enumerate(self._loops):
218 fd.write(' plants[%d] = ::std::unique_ptr<%s>(new %s(%s));\n' %
219 (index, self._PlantCoeffType(), self._PlantCoeffType(),
220 loop.PlantFunction()))
221 fd.write(' return %s(&plants);\n' % self._PlantType())
222 fd.write('}\n\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700223
Tyler Chatow6738c362019-02-16 14:12:30 -0800224 fd.write('%s Make%sController() {\n' % (self._ControllerType(),
225 self._gain_schedule_name))
226 fd.write(
227 ' ::std::vector< ::std::unique_ptr<%s>> controllers(%d);\n' %
228 (self._ControllerCoeffType(), len(self._loops)))
229 for index, loop in enumerate(self._loops):
230 fd.write(
231 ' controllers[%d] = ::std::unique_ptr<%s>(new %s(%s));\n' %
232 (index, self._ControllerCoeffType(),
233 self._ControllerCoeffType(), loop.ControllerFunction()))
234 fd.write(' return %s(&controllers);\n' % self._ControllerType())
235 fd.write('}\n\n')
Austin Schuh32501832017-02-25 18:32:56 -0800236
Tyler Chatow6738c362019-02-16 14:12:30 -0800237 fd.write('%s Make%sObserver() {\n' % (self._ObserverType(),
238 self._gain_schedule_name))
239 fd.write(' ::std::vector< ::std::unique_ptr<%s>> observers(%d);\n'
240 % (self._ObserverCoeffType(), len(self._loops)))
241 for index, loop in enumerate(self._loops):
242 fd.write(
243 ' observers[%d] = ::std::unique_ptr<%s>(new %s(%s));\n'
244 % (index, self._ObserverCoeffType(),
245 self._ObserverCoeffType(), loop.ObserverFunction()))
246 fd.write(' return %s(&observers);\n' % self._ObserverType())
247 fd.write('}\n\n')
Austin Schuh32501832017-02-25 18:32:56 -0800248
Tyler Chatow6738c362019-02-16 14:12:30 -0800249 fd.write('%s Make%sLoop() {\n' % (self._LoopType(),
250 self._gain_schedule_name))
251 fd.write(
252 ' return %s(Make%sPlant(), Make%sController(), Make%sObserver());\n'
253 % (self._LoopType(), self._gain_schedule_name,
254 self._gain_schedule_name, self._gain_schedule_name))
255 fd.write('}\n\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700256
Tyler Chatow6738c362019-02-16 14:12:30 -0800257 fd.write(self._namespace_end)
258 fd.write('\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700259
260
Austin Schuh3c542312013-02-24 01:53:50 -0800261class ControlLoop(object):
Austin Schuh3c542312013-02-24 01:53:50 -0800262
Tyler Chatow6738c362019-02-16 14:12:30 -0800263 def __init__(self, name):
264 """Constructs a control loop object.
Austin Schuh3c542312013-02-24 01:53:50 -0800265
Tyler Chatow6738c362019-02-16 14:12:30 -0800266 Args:
267 name: string, The name of the loop to use when writing the C++ files.
268 """
269 self._name = name
Austin Schuhb5d302f2019-01-20 20:51:19 -0800270
Tyler Chatow6738c362019-02-16 14:12:30 -0800271 @property
272 def name(self):
273 """Returns the name"""
274 return self._name
Austin Schuh3c542312013-02-24 01:53:50 -0800275
Tyler Chatow6738c362019-02-16 14:12:30 -0800276 def ContinuousToDiscrete(self, A_continuous, B_continuous, dt):
277 """Calculates the discrete time values for A and B.
Austin Schuhc1f68892013-03-16 17:06:27 -0700278
Tyler Chatow6738c362019-02-16 14:12:30 -0800279 Args:
280 A_continuous: numpy.matrix, The continuous time A matrix
281 B_continuous: numpy.matrix, The continuous time B matrix
282 dt: float, The time step of the control loop
Austin Schuhc1f68892013-03-16 17:06:27 -0700283
Tyler Chatow6738c362019-02-16 14:12:30 -0800284 Returns:
285 (A, B), numpy.matrix, the control matricies.
286 """
287 return controls.c2d(A_continuous, B_continuous, dt)
Austin Schuh3c542312013-02-24 01:53:50 -0800288
Tyler Chatow6738c362019-02-16 14:12:30 -0800289 def InitializeState(self):
290 """Sets X, Y, and X_hat to zero defaults."""
Austin Schuh43b9ae92020-02-29 23:08:38 -0800291 self.X = numpy.matrix(numpy.zeros((self.A.shape[0], 1)))
Tyler Chatow6738c362019-02-16 14:12:30 -0800292 self.Y = self.C * self.X
Austin Schuh43b9ae92020-02-29 23:08:38 -0800293 self.X_hat = numpy.matrix(numpy.zeros((self.A.shape[0], 1)))
Austin Schuh3c542312013-02-24 01:53:50 -0800294
Tyler Chatow6738c362019-02-16 14:12:30 -0800295 def PlaceControllerPoles(self, poles):
296 """Places the controller poles.
Austin Schuh3c542312013-02-24 01:53:50 -0800297
Tyler Chatow6738c362019-02-16 14:12:30 -0800298 Args:
299 poles: array, An array of poles. Must be complex conjegates if they have
300 any imaginary portions.
301 """
302 self.K = controls.dplace(self.A, self.B, poles)
Austin Schuh3c542312013-02-24 01:53:50 -0800303
Tyler Chatow6738c362019-02-16 14:12:30 -0800304 def PlaceObserverPoles(self, poles):
305 """Places the observer poles.
Austin Schuh3c542312013-02-24 01:53:50 -0800306
Tyler Chatow6738c362019-02-16 14:12:30 -0800307 Args:
308 poles: array, An array of poles. Must be complex conjegates if they have
309 any imaginary portions.
310 """
311 self.L = controls.dplace(self.A.T, self.C.T, poles).T
Sabina Davis3922dfa2018-02-10 23:10:05 -0800312
Tyler Chatow6738c362019-02-16 14:12:30 -0800313 def Update(self, U):
314 """Simulates one time step with the provided U."""
315 #U = numpy.clip(U, self.U_min, self.U_max)
316 self.X = self.A * self.X + self.B * U
317 self.Y = self.C * self.X + self.D * U
Austin Schuh3c542312013-02-24 01:53:50 -0800318
Tyler Chatow6738c362019-02-16 14:12:30 -0800319 def PredictObserver(self, U):
320 """Runs the predict step of the observer update."""
321 self.X_hat = (self.A * self.X_hat + self.B * U)
Austin Schuh1a387962015-01-31 16:36:20 -0800322
Tyler Chatow6738c362019-02-16 14:12:30 -0800323 def CorrectObserver(self, U):
324 """Runs the correct step of the observer update."""
325 if hasattr(self, 'KalmanGain'):
326 KalmanGain = self.KalmanGain
327 else:
328 KalmanGain = numpy.linalg.inv(self.A) * self.L
329 self.X_hat += KalmanGain * (self.Y - self.C * self.X_hat - self.D * U)
Austin Schuh1a387962015-01-31 16:36:20 -0800330
Tyler Chatow6738c362019-02-16 14:12:30 -0800331 def UpdateObserver(self, U):
332 """Updates the observer given the provided U."""
333 if hasattr(self, 'KalmanGain'):
334 KalmanGain = self.KalmanGain
335 else:
336 KalmanGain = numpy.linalg.inv(self.A) * self.L
337 self.X_hat = (self.A * self.X_hat + self.B * U + self.A * KalmanGain *
338 (self.Y - self.C * self.X_hat - self.D * U))
Austin Schuh3c542312013-02-24 01:53:50 -0800339
Tyler Chatow6738c362019-02-16 14:12:30 -0800340 def _DumpMatrix(self, matrix_name, matrix, scalar_type):
341 """Dumps the provided matrix into a variable called matrix_name.
Austin Schuh3c542312013-02-24 01:53:50 -0800342
Tyler Chatow6738c362019-02-16 14:12:30 -0800343 Args:
344 matrix_name: string, The variable name to save the matrix to.
345 matrix: The matrix to dump.
346 scalar_type: The C++ type to use for the scalar in the matrix.
Austin Schuh3c542312013-02-24 01:53:50 -0800347
Tyler Chatow6738c362019-02-16 14:12:30 -0800348 Returns:
349 string, The C++ commands required to populate a variable named matrix_name
350 with the contents of matrix.
351 """
352 ans = [
353 ' Eigen::Matrix<%s, %d, %d> %s;\n' % (scalar_type, matrix.shape[0],
354 matrix.shape[1], matrix_name)
355 ]
Austin Schuh5ea48472021-02-02 20:46:41 -0800356 for x in range(matrix.shape[0]):
357 for y in range(matrix.shape[1]):
Tyler Chatow6738c362019-02-16 14:12:30 -0800358 write_type = repr(matrix[x, y])
359 if scalar_type == 'float':
360 if '.' not in write_type:
361 write_type += '.0'
362 write_type += 'f'
363 ans.append(
364 ' %s(%d, %d) = %s;\n' % (matrix_name, x, y, write_type))
Austin Schuh3c542312013-02-24 01:53:50 -0800365
Tyler Chatow6738c362019-02-16 14:12:30 -0800366 return ''.join(ans)
Austin Schuh3c542312013-02-24 01:53:50 -0800367
Tyler Chatow6738c362019-02-16 14:12:30 -0800368 def DumpPlantHeader(self, plant_coefficient_type):
369 """Writes out a c++ header declaration which will create a Plant object.
Austin Schuh3c542312013-02-24 01:53:50 -0800370
Tyler Chatow6738c362019-02-16 14:12:30 -0800371 Returns:
372 string, The header declaration for the function.
373 """
374 return '%s Make%sPlantCoefficients();\n' % (plant_coefficient_type,
375 self._name)
Austin Schuh3c542312013-02-24 01:53:50 -0800376
Tyler Chatow6738c362019-02-16 14:12:30 -0800377 def DumpPlant(self, plant_coefficient_type, scalar_type):
378 """Writes out a c++ function which will create a PlantCoefficients object.
Austin Schuh3c542312013-02-24 01:53:50 -0800379
Tyler Chatow6738c362019-02-16 14:12:30 -0800380 Returns:
381 string, The function which will create the object.
382 """
383 ans = [
384 '%s Make%sPlantCoefficients() {\n' % (plant_coefficient_type,
385 self._name)
386 ]
Austin Schuh3c542312013-02-24 01:53:50 -0800387
Tyler Chatow6738c362019-02-16 14:12:30 -0800388 ans.append(self._DumpMatrix('C', self.C, scalar_type))
389 ans.append(self._DumpMatrix('D', self.D, scalar_type))
390 ans.append(self._DumpMatrix('U_max', self.U_max, scalar_type))
391 ans.append(self._DumpMatrix('U_min', self.U_min, scalar_type))
Austin Schuh3c542312013-02-24 01:53:50 -0800392
Tyler Chatow6738c362019-02-16 14:12:30 -0800393 if plant_coefficient_type.startswith('StateFeedbackPlant'):
394 ans.append(self._DumpMatrix('A', self.A, scalar_type))
395 ans.append(self._DumpMatrix('B', self.B, scalar_type))
396 ans.append(
James Kuszmaul03be1242020-02-21 14:52:04 -0800397 ' const std::chrono::nanoseconds dt(%d);\n' % (self.dt * 1e9))
398 ans.append(
Tyler Chatow6738c362019-02-16 14:12:30 -0800399 ' return %s'
James Kuszmaul03be1242020-02-21 14:52:04 -0800400 '(A, B, C, D, U_max, U_min, dt);\n' % (plant_coefficient_type))
Tyler Chatow6738c362019-02-16 14:12:30 -0800401 elif plant_coefficient_type.startswith('StateFeedbackHybridPlant'):
402 ans.append(
403 self._DumpMatrix('A_continuous', self.A_continuous,
404 scalar_type))
405 ans.append(
406 self._DumpMatrix('B_continuous', self.B_continuous,
407 scalar_type))
408 ans.append(' return %s'
409 '(A_continuous, B_continuous, C, D, U_max, U_min);\n' %
410 (plant_coefficient_type))
411 else:
412 glog.fatal('Unsupported plant type %s', plant_coefficient_type)
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800413
Tyler Chatow6738c362019-02-16 14:12:30 -0800414 ans.append('}\n')
415 return ''.join(ans)
Austin Schuh3c542312013-02-24 01:53:50 -0800416
Tyler Chatow6738c362019-02-16 14:12:30 -0800417 def PlantFunction(self):
418 """Returns the name of the plant coefficient function."""
419 return 'Make%sPlantCoefficients()' % self._name
Austin Schuh3c542312013-02-24 01:53:50 -0800420
Tyler Chatow6738c362019-02-16 14:12:30 -0800421 def ControllerFunction(self):
422 """Returns the name of the controller function."""
423 return 'Make%sControllerCoefficients()' % self._name
Austin Schuh32501832017-02-25 18:32:56 -0800424
Tyler Chatow6738c362019-02-16 14:12:30 -0800425 def ObserverFunction(self):
426 """Returns the name of the controller function."""
427 return 'Make%sObserverCoefficients()' % self._name
Austin Schuhe3490622013-03-13 01:24:30 -0700428
Tyler Chatow6738c362019-02-16 14:12:30 -0800429 def DumpControllerHeader(self, scalar_type):
430 """Writes out a c++ header declaration which will create a Controller object.
Austin Schuh3c542312013-02-24 01:53:50 -0800431
Tyler Chatow6738c362019-02-16 14:12:30 -0800432 Returns:
433 string, The header declaration for the function.
434 """
435 num_states = self.A.shape[0]
436 num_inputs = self.B.shape[1]
437 num_outputs = self.C.shape[0]
438 return 'StateFeedbackControllerCoefficients<%d, %d, %d, %s> %s;\n' % (
439 num_states, num_inputs, num_outputs, scalar_type,
440 self.ControllerFunction())
Austin Schuh3c542312013-02-24 01:53:50 -0800441
Tyler Chatow6738c362019-02-16 14:12:30 -0800442 def DumpController(self, scalar_type):
443 """Returns a c++ function which will create a Controller object.
Austin Schuh3c542312013-02-24 01:53:50 -0800444
Tyler Chatow6738c362019-02-16 14:12:30 -0800445 Returns:
446 string, The function which will create the object.
447 """
448 num_states = self.A.shape[0]
449 num_inputs = self.B.shape[1]
450 num_outputs = self.C.shape[0]
451 ans = [
452 'StateFeedbackControllerCoefficients<%d, %d, %d, %s> %s {\n' %
453 (num_states, num_inputs, num_outputs, scalar_type,
454 self.ControllerFunction())
455 ]
Austin Schuh3c542312013-02-24 01:53:50 -0800456
Tyler Chatow6738c362019-02-16 14:12:30 -0800457 ans.append(self._DumpMatrix('K', self.K, scalar_type))
458 if not hasattr(self, 'Kff'):
459 self.Kff = numpy.matrix(numpy.zeros(self.K.shape))
Austin Schuh86093ad2016-02-06 14:29:34 -0800460
Tyler Chatow6738c362019-02-16 14:12:30 -0800461 ans.append(self._DumpMatrix('Kff', self.Kff, scalar_type))
Austin Schuh3c542312013-02-24 01:53:50 -0800462
Tyler Chatow6738c362019-02-16 14:12:30 -0800463 ans.append(
464 ' return StateFeedbackControllerCoefficients<%d, %d, %d, %s>'
465 '(K, Kff);\n' % (num_states, num_inputs, num_outputs, scalar_type))
466 ans.append('}\n')
467 return ''.join(ans)
Austin Schuh32501832017-02-25 18:32:56 -0800468
Tyler Chatow6738c362019-02-16 14:12:30 -0800469 def DumpObserverHeader(self, observer_coefficient_type):
470 """Writes out a c++ header declaration which will create a Observer object.
Austin Schuh32501832017-02-25 18:32:56 -0800471
Tyler Chatow6738c362019-02-16 14:12:30 -0800472 Returns:
473 string, The header declaration for the function.
474 """
475 return '%s %s;\n' % (observer_coefficient_type, self.ObserverFunction())
Austin Schuh32501832017-02-25 18:32:56 -0800476
Tyler Chatow6738c362019-02-16 14:12:30 -0800477 def DumpObserver(self, observer_coefficient_type, scalar_type):
478 """Returns a c++ function which will create a Observer object.
Austin Schuh32501832017-02-25 18:32:56 -0800479
Tyler Chatow6738c362019-02-16 14:12:30 -0800480 Returns:
481 string, The function which will create the object.
482 """
483 ans = [
484 '%s %s {\n' % (observer_coefficient_type, self.ObserverFunction())
485 ]
Austin Schuh32501832017-02-25 18:32:56 -0800486
Tyler Chatow6738c362019-02-16 14:12:30 -0800487 if observer_coefficient_type.startswith('StateFeedbackObserver'):
488 if hasattr(self, 'KalmanGain'):
489 KalmanGain = self.KalmanGain
490 Q = self.Q
491 R = self.R
492 else:
493 KalmanGain = numpy.linalg.inv(self.A) * self.L
494 Q = numpy.zeros(self.A.shape)
495 R = numpy.zeros((self.C.shape[0], self.C.shape[0]))
496 ans.append(self._DumpMatrix('KalmanGain', KalmanGain, scalar_type))
497 ans.append(self._DumpMatrix('Q', Q, scalar_type))
498 ans.append(self._DumpMatrix('R', R, scalar_type))
499 ans.append(' return %s(KalmanGain, Q, R);\n' %
500 (observer_coefficient_type,))
Sabina Davis3922dfa2018-02-10 23:10:05 -0800501
Tyler Chatow6738c362019-02-16 14:12:30 -0800502 elif observer_coefficient_type.startswith('HybridKalman'):
503 ans.append(
504 self._DumpMatrix('Q_continuous', self.Q_continuous,
505 scalar_type))
506 ans.append(
507 self._DumpMatrix('R_continuous', self.R_continuous,
508 scalar_type))
509 ans.append(
510 self._DumpMatrix('P_steady_state', self.P_steady_state,
511 scalar_type))
512 ans.append(
513 ' return %s(Q_continuous, R_continuous, P_steady_state);\n' %
514 (observer_coefficient_type,))
515 else:
516 glog.fatal('Unsupported observer type %s',
517 observer_coefficient_type)
Austin Schuh32501832017-02-25 18:32:56 -0800518
Tyler Chatow6738c362019-02-16 14:12:30 -0800519 ans.append('}\n')
520 return ''.join(ans)
521
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800522
523class HybridControlLoop(ControlLoop):
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800524
Tyler Chatow6738c362019-02-16 14:12:30 -0800525 def __init__(self, name):
526 super(HybridControlLoop, self).__init__(name=name)
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800527
Tyler Chatow6738c362019-02-16 14:12:30 -0800528 def Discretize(self, dt):
529 [self.A, self.B, self.Q, self.R] = \
530 controls.kalmd(self.A_continuous, self.B_continuous,
531 self.Q_continuous, self.R_continuous, dt)
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800532
Tyler Chatow6738c362019-02-16 14:12:30 -0800533 def PredictHybridObserver(self, U, dt):
534 self.Discretize(dt)
535 self.X_hat = self.A * self.X_hat + self.B * U
536 self.P = (self.A * self.P * self.A.T + self.Q)
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800537
Tyler Chatow6738c362019-02-16 14:12:30 -0800538 def CorrectHybridObserver(self, U):
539 Y_bar = self.Y - self.C * self.X_hat
540 C_t = self.C.T
541 S = self.C * self.P * C_t + self.R
542 self.KalmanGain = self.P * C_t * numpy.linalg.inv(S)
543 self.X_hat = self.X_hat + self.KalmanGain * Y_bar
544 self.P = (numpy.eye(len(self.A)) - self.KalmanGain * self.C) * self.P
545
546 def InitializeState(self):
547 super(HybridControlLoop, self).InitializeState()
548 if hasattr(self, 'Q_steady_state'):
549 self.P = self.Q_steady_state
550 else:
551 self.P = numpy.matrix(
552 numpy.zeros((self.A.shape[0], self.A.shape[0])))
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800553
554
555class CIM(object):
Tyler Chatow6738c362019-02-16 14:12:30 -0800556
557 def __init__(self):
558 # Stall Torque in N m
559 self.stall_torque = 2.42
560 # Stall Current in Amps
561 self.stall_current = 133.0
562 # Free Speed in rad/s
563 self.free_speed = 5500.0 / 60.0 * 2.0 * numpy.pi
564 # Free Current in Amps
565 self.free_current = 4.7
566 # Resistance of the motor
567 self.resistance = 12.0 / self.stall_current
568 # Motor velocity constant
569 self.Kv = (
570 self.free_speed / (12.0 - self.resistance * self.free_current))
571 # Torque constant
572 self.Kt = self.stall_torque / self.stall_current
Lee Mracek97fc8af2018-01-13 04:38:52 -0500573
574
575class MiniCIM(object):
Tyler Chatow6738c362019-02-16 14:12:30 -0800576
577 def __init__(self):
578 # Stall Torque in N m
579 self.stall_torque = 1.41
580 # Stall Current in Amps
581 self.stall_current = 89.0
582 # Free Speed in rad/s
583 self.free_speed = 5840.0 / 60.0 * 2.0 * numpy.pi
584 # Free Current in Amps
585 self.free_current = 3.0
586 # Resistance of the motor
587 self.resistance = 12.0 / self.stall_current
588 # Motor velocity constant
589 self.Kv = (
590 self.free_speed / (12.0 - self.resistance * self.free_current))
591 # Torque constant
592 self.Kt = self.stall_torque / self.stall_current
Austin Schuhf173eb82018-01-20 23:32:30 -0800593
594
Austin Schuhb5d302f2019-01-20 20:51:19 -0800595class NMotor(object):
Tyler Chatow6738c362019-02-16 14:12:30 -0800596
Austin Schuhb5d302f2019-01-20 20:51:19 -0800597 def __init__(self, motor, n):
598 """Gangs together n motors."""
599 self.motor = motor
600 self.stall_torque = motor.stall_torque * n
601 self.stall_current = motor.stall_current * n
602 self.free_speed = motor.free_speed
603
604 self.free_current = motor.free_current * n
605 self.resistance = motor.resistance / n
606 self.Kv = motor.Kv
607 self.Kt = motor.Kt
Austin Schuh36bb8e32019-02-18 15:02:57 -0800608 self.motor_inertia = motor.motor_inertia * n
Austin Schuhb5d302f2019-01-20 20:51:19 -0800609
610
611class Vex775Pro(object):
Tyler Chatow6738c362019-02-16 14:12:30 -0800612
Austin Schuhb5d302f2019-01-20 20:51:19 -0800613 def __init__(self):
614 # Stall Torque in N m
615 self.stall_torque = 0.71
616 # Stall Current in Amps
617 self.stall_current = 134.0
618 # Free Speed in rad/s
619 self.free_speed = 18730.0 / 60.0 * 2.0 * numpy.pi
620 # Free Current in Amps
621 self.free_current = 0.7
622 # Resistance of the motor
623 self.resistance = 12.0 / self.stall_current
624 # Motor velocity constant
Tyler Chatow6738c362019-02-16 14:12:30 -0800625 self.Kv = (
626 self.free_speed / (12.0 - self.resistance * self.free_current))
Austin Schuhb5d302f2019-01-20 20:51:19 -0800627 # Torque constant
628 self.Kt = self.stall_torque / self.stall_current
629 # Motor inertia in kg m^2
630 self.motor_inertia = 0.00001187
631
632
Austin Schuhf173eb82018-01-20 23:32:30 -0800633class BAG(object):
Tyler Chatow6738c362019-02-16 14:12:30 -0800634 # BAG motor specs available at http://motors.vex.com/vexpro-motors/bag-motor
635 def __init__(self):
636 # Stall Torque in (N m)
637 self.stall_torque = 0.43
638 # Stall Current in (Amps)
639 self.stall_current = 53.0
640 # Free Speed in (rad/s)
641 self.free_speed = 13180.0 / 60.0 * 2.0 * numpy.pi
642 # Free Current in (Amps)
643 self.free_current = 1.8
644 # Resistance of the motor (Ohms)
645 self.resistance = 12.0 / self.stall_current
646 # Motor velocity constant (radians / (sec * volt))
647 self.Kv = (
648 self.free_speed / (12.0 - self.resistance * self.free_current))
649 # Torque constant (N * m / A)
650 self.Kt = self.stall_torque / self.stall_current
651 # Motor inertia in kg m^2
652 self.motor_inertia = 0.000006
653
Brian Silverman6260c092018-01-14 15:21:36 -0800654
655class MN3510(object):
Tyler Chatow6738c362019-02-16 14:12:30 -0800656
657 def __init__(self):
658 # http://www.robotshop.com/en/t-motor-navigator-mn3510-360kv-brushless-motor.html#Specifications
659 # Free Current in Amps
660 self.free_current = 0.0
661 # Resistance of the motor
662 self.resistance = 0.188
663 # Stall Current in Amps
664 self.stall_current = 14.0 / self.resistance
665 # Motor velocity constant
666 self.Kv = 360.0 / 60.0 * (2.0 * numpy.pi)
667 # Torque constant Nm / A
668 self.Kt = 1.0 / self.Kv
669 # Stall Torque in N m
670 self.stall_torque = self.Kt * self.stall_current
James Kuszmaulef0c18a2020-01-12 15:44:20 -0800671
672
673class Falcon(object):
674 """Class representing the VexPro Falcon 500 motor.
675
676 All numbers based on data from
677 https://www.vexrobotics.com/vexpro/falcon-500."""
678
679 def __init__(self):
680 # Stall Torque in N m
681 self.stall_torque = 4.69
682 # Stall Current in Amps
683 self.stall_current = 257.0
684 # Free Speed in rad / sec
685 self.free_speed = 6380.0 / 60.0 * 2.0 * numpy.pi
686 # Free Current in Amps
687 self.free_current = 1.5
688 # Resistance of the motor, divided by 2 to account for the 2 motors
689 self.resistance = 12.0 / self.stall_current
690 # Motor velocity constant
691 self.Kv = (self.free_speed /
692 (12.0 - self.resistance * self.free_current))
693 # Torque constant
694 self.Kt = self.stall_torque / self.stall_current
Austin Schuhc1c957a2020-02-20 17:47:58 -0800695 # Motor inertia in kg m^2
696 # Diameter of 1.9", weight of: 100 grams
697 # TODO(austin): Get a number from Scott Westbrook for the mass
698 self.motor_inertia = 0.1 * ((0.95 * 0.0254) ** 2.0)