blob: e836d1ab7c40eaff718f09b50097034e088210e8 [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):
Ravago Jones5127ccc2022-07-31 16:32:45 -07007
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):
Ravago Jones5127ccc2022-07-31 16:32:45 -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):
Ravago Jones5127ccc2022-07-31 16:32:45 -070081 return ('_'.join([namespace.upper()
82 for namespace in self._namespaces]) + '_' +
83 os.path.basename(header_file).upper().replace(
Tyler Chatow6738c362019-02-16 14:12:30 -080084 '.', '_').replace('/', '_') + '_')
Austin Schuhe3490622013-03-13 01:24:30 -070085
Tyler Chatow6738c362019-02-16 14:12:30 -080086 def Write(self, header_file, cc_file):
87 """Writes the loops to the specified files."""
88 self.WriteHeader(header_file)
89 self.WriteCC(os.path.basename(header_file), cc_file)
Austin Schuhe3490622013-03-13 01:24:30 -070090
Tyler Chatow6738c362019-02-16 14:12:30 -080091 def _GenericType(self, typename, extra_args=None):
92 """Returns a loop template using typename for the type."""
93 num_states = self._loops[0].A.shape[0]
94 num_inputs = self._loops[0].B.shape[1]
95 num_outputs = self._loops[0].C.shape[0]
96 if extra_args is not None:
97 extra_args = ', ' + extra_args
98 else:
99 extra_args = ''
100 if self._scalar_type != 'double':
101 extra_args += ', ' + self._scalar_type
102 return '%s<%d, %d, %d%s>' % (typename, num_states, num_inputs,
103 num_outputs, extra_args)
Austin Schuh32501832017-02-25 18:32:56 -0800104
Tyler Chatow6738c362019-02-16 14:12:30 -0800105 def _ControllerType(self):
106 """Returns a template name for StateFeedbackController."""
107 return self._GenericType('StateFeedbackController')
Austin Schuhe3490622013-03-13 01:24:30 -0700108
Tyler Chatow6738c362019-02-16 14:12:30 -0800109 def _ObserverType(self):
110 """Returns a template name for StateFeedbackObserver."""
111 return self._GenericType(self._observer_type)
Austin Schuh20388b62017-11-23 22:40:46 -0800112
Tyler Chatow6738c362019-02-16 14:12:30 -0800113 def _LoopType(self):
114 """Returns a template name for StateFeedbackLoop."""
115 num_states = self._loops[0].A.shape[0]
116 num_inputs = self._loops[0].B.shape[1]
117 num_outputs = self._loops[0].C.shape[0]
Austin Schuh20388b62017-11-23 22:40:46 -0800118
Tyler Chatow6738c362019-02-16 14:12:30 -0800119 return 'StateFeedbackLoop<%d, %d, %d, %s, %s, %s>' % (
120 num_states, num_inputs, num_outputs, self._scalar_type,
121 self._PlantType(), self._ObserverType())
Austin Schuhe3490622013-03-13 01:24:30 -0700122
Tyler Chatow6738c362019-02-16 14:12:30 -0800123 def _PlantType(self):
124 """Returns a template name for StateFeedbackPlant."""
125 return self._GenericType(self._plant_type)
Austin Schuhe3490622013-03-13 01:24:30 -0700126
Tyler Chatow6738c362019-02-16 14:12:30 -0800127 def _PlantCoeffType(self):
128 """Returns a template name for StateFeedbackPlantCoefficients."""
129 return self._GenericType(self._plant_type + 'Coefficients')
Austin Schuhe3490622013-03-13 01:24:30 -0700130
Tyler Chatow6738c362019-02-16 14:12:30 -0800131 def _ControllerCoeffType(self):
132 """Returns a template name for StateFeedbackControllerCoefficients."""
133 return self._GenericType('StateFeedbackControllerCoefficients')
Austin Schuh32501832017-02-25 18:32:56 -0800134
Tyler Chatow6738c362019-02-16 14:12:30 -0800135 def _ObserverCoeffType(self):
136 """Returns a template name for StateFeedbackObserverCoefficients."""
137 return self._GenericType(self._observer_type + 'Coefficients')
Austin Schuh32501832017-02-25 18:32:56 -0800138
Tyler Chatow6738c362019-02-16 14:12:30 -0800139 def WriteHeader(self, header_file):
140 """Writes the header file to the file named header_file."""
141 with open(header_file, 'w') as fd:
142 header_guard = self._HeaderGuard(header_file)
143 fd.write('#ifndef %s\n'
144 '#define %s\n\n' % (header_guard, header_guard))
145 fd.write(
146 '#include \"frc971/control_loops/state_feedback_loop.h\"\n')
Ravago Jones26f7ad02021-02-05 15:45:59 -0800147 if (self._plant_type == 'StateFeedbackHybridPlant'
148 or self._observer_type == 'HybridKalman'):
Tyler Chatow6738c362019-02-16 14:12:30 -0800149 fd.write(
150 '#include \"frc971/control_loops/hybrid_state_feedback_loop.h\"\n'
151 )
Austin Schuh4cc4fe22017-11-23 19:13:09 -0800152
Tyler Chatow6738c362019-02-16 14:12:30 -0800153 fd.write('\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700154
Tyler Chatow6738c362019-02-16 14:12:30 -0800155 fd.write(self._namespace_start)
Ben Fredrickson1b45f782014-02-23 07:44:36 +0000156
Tyler Chatow6738c362019-02-16 14:12:30 -0800157 for const in self._constant_list:
158 fd.write(const.Render(self._scalar_type))
Ben Fredrickson1b45f782014-02-23 07:44:36 +0000159
Tyler Chatow6738c362019-02-16 14:12:30 -0800160 fd.write('\n\n')
161 for loop in self._loops:
162 fd.write(loop.DumpPlantHeader(self._PlantCoeffType()))
163 fd.write('\n')
164 fd.write(loop.DumpControllerHeader(self._scalar_type))
165 fd.write('\n')
166 fd.write(loop.DumpObserverHeader(self._ObserverCoeffType()))
167 fd.write('\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700168
Ravago Jones5127ccc2022-07-31 16:32:45 -0700169 fd.write('%s Make%sPlant();\n\n' %
170 (self._PlantType(), self._gain_schedule_name))
Austin Schuhe3490622013-03-13 01:24:30 -0700171
Ravago Jones5127ccc2022-07-31 16:32:45 -0700172 fd.write('%s Make%sController();\n\n' %
173 (self._ControllerType(), self._gain_schedule_name))
Austin Schuh32501832017-02-25 18:32:56 -0800174
Ravago Jones5127ccc2022-07-31 16:32:45 -0700175 fd.write('%s Make%sObserver();\n\n' %
176 (self._ObserverType(), self._gain_schedule_name))
Austin Schuh32501832017-02-25 18:32:56 -0800177
Ravago Jones5127ccc2022-07-31 16:32:45 -0700178 fd.write('%s Make%sLoop();\n\n' %
179 (self._LoopType(), self._gain_schedule_name))
Austin Schuhe3490622013-03-13 01:24:30 -0700180
Tyler Chatow6738c362019-02-16 14:12:30 -0800181 fd.write(self._namespace_end)
182 fd.write('\n\n')
183 fd.write("#endif // %s\n" % header_guard)
Austin Schuhe3490622013-03-13 01:24:30 -0700184
Tyler Chatow6738c362019-02-16 14:12:30 -0800185 def WriteCC(self, header_file_name, cc_file):
186 """Writes the cc file to the file named cc_file."""
187 with open(cc_file, 'w') as fd:
Ravago Jones5127ccc2022-07-31 16:32:45 -0700188 fd.write('#include \"%s/%s\"\n' %
189 (os.path.join(*self._namespaces), header_file_name))
Tyler Chatow6738c362019-02-16 14:12:30 -0800190 fd.write('\n')
James Kuszmaul03be1242020-02-21 14:52:04 -0800191 fd.write('#include <chrono>\n')
Tyler Chatow6738c362019-02-16 14:12:30 -0800192 fd.write('#include <vector>\n')
193 fd.write('\n')
194 fd.write(
195 '#include \"frc971/control_loops/state_feedback_loop.h\"\n')
196 fd.write('\n')
197 fd.write(self._namespace_start)
198 fd.write('\n\n')
199 for loop in self._loops:
200 fd.write(
201 loop.DumpPlant(self._PlantCoeffType(), self._scalar_type))
202 fd.write('\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700203
Tyler Chatow6738c362019-02-16 14:12:30 -0800204 for loop in self._loops:
205 fd.write(loop.DumpController(self._scalar_type))
206 fd.write('\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700207
Tyler Chatow6738c362019-02-16 14:12:30 -0800208 for loop in self._loops:
209 fd.write(
210 loop.DumpObserver(self._ObserverCoeffType(),
211 self._scalar_type))
212 fd.write('\n')
Austin Schuh32501832017-02-25 18:32:56 -0800213
Ravago Jones5127ccc2022-07-31 16:32:45 -0700214 fd.write('%s Make%sPlant() {\n' %
215 (self._PlantType(), self._gain_schedule_name))
Tyler Chatow6738c362019-02-16 14:12:30 -0800216 fd.write(' ::std::vector< ::std::unique_ptr<%s>> plants(%d);\n' %
217 (self._PlantCoeffType(), len(self._loops)))
218 for index, loop in enumerate(self._loops):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700219 fd.write(
220 ' plants[%d] = ::std::unique_ptr<%s>(new %s(%s));\n' %
221 (index, self._PlantCoeffType(), self._PlantCoeffType(),
222 loop.PlantFunction()))
Austin Schuhb02bf5b2021-07-31 21:28:21 -0700223 fd.write(' return %s(std::move(plants));\n' % self._PlantType())
Tyler Chatow6738c362019-02-16 14:12:30 -0800224 fd.write('}\n\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700225
Ravago Jones5127ccc2022-07-31 16:32:45 -0700226 fd.write('%s Make%sController() {\n' %
227 (self._ControllerType(), self._gain_schedule_name))
Tyler Chatow6738c362019-02-16 14:12:30 -0800228 fd.write(
229 ' ::std::vector< ::std::unique_ptr<%s>> controllers(%d);\n' %
230 (self._ControllerCoeffType(), len(self._loops)))
231 for index, loop in enumerate(self._loops):
232 fd.write(
Ravago Jones26f7ad02021-02-05 15:45:59 -0800233 ' controllers[%d] = ::std::unique_ptr<%s>(new %s(%s));\n'
234 % (index, self._ControllerCoeffType(),
235 self._ControllerCoeffType(), loop.ControllerFunction()))
Austin Schuhb02bf5b2021-07-31 21:28:21 -0700236 fd.write(' return %s(std::move(controllers));\n' %
237 self._ControllerType())
Tyler Chatow6738c362019-02-16 14:12:30 -0800238 fd.write('}\n\n')
Austin Schuh32501832017-02-25 18:32:56 -0800239
Ravago Jones5127ccc2022-07-31 16:32:45 -0700240 fd.write('%s Make%sObserver() {\n' %
241 (self._ObserverType(), self._gain_schedule_name))
242 fd.write(
243 ' ::std::vector< ::std::unique_ptr<%s>> observers(%d);\n' %
244 (self._ObserverCoeffType(), len(self._loops)))
Tyler Chatow6738c362019-02-16 14:12:30 -0800245 for index, loop in enumerate(self._loops):
246 fd.write(
Ravago Jones5127ccc2022-07-31 16:32:45 -0700247 ' observers[%d] = ::std::unique_ptr<%s>(new %s(%s));\n' %
248 (index, self._ObserverCoeffType(),
249 self._ObserverCoeffType(), loop.ObserverFunction()))
250 fd.write(' return %s(std::move(observers));\n' %
251 self._ObserverType())
Tyler Chatow6738c362019-02-16 14:12:30 -0800252 fd.write('}\n\n')
Austin Schuh32501832017-02-25 18:32:56 -0800253
Ravago Jones5127ccc2022-07-31 16:32:45 -0700254 fd.write('%s Make%sLoop() {\n' %
255 (self._LoopType(), self._gain_schedule_name))
Tyler Chatow6738c362019-02-16 14:12:30 -0800256 fd.write(
257 ' return %s(Make%sPlant(), Make%sController(), Make%sObserver());\n'
258 % (self._LoopType(), self._gain_schedule_name,
259 self._gain_schedule_name, self._gain_schedule_name))
260 fd.write('}\n\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700261
Tyler Chatow6738c362019-02-16 14:12:30 -0800262 fd.write(self._namespace_end)
263 fd.write('\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700264
265
Austin Schuh3c542312013-02-24 01:53:50 -0800266class ControlLoop(object):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700267
Tyler Chatow6738c362019-02-16 14:12:30 -0800268 def __init__(self, name):
269 """Constructs a control loop object.
Austin Schuh3c542312013-02-24 01:53:50 -0800270
Tyler Chatow6738c362019-02-16 14:12:30 -0800271 Args:
272 name: string, The name of the loop to use when writing the C++ files.
273 """
274 self._name = name
Austin Schuhb39f4522022-03-27 13:29:42 -0700275 self.delayed_u = 0
Austin Schuhb5d302f2019-01-20 20:51:19 -0800276
Tyler Chatow6738c362019-02-16 14:12:30 -0800277 @property
278 def name(self):
279 """Returns the name"""
280 return self._name
Austin Schuh3c542312013-02-24 01:53:50 -0800281
Tyler Chatow6738c362019-02-16 14:12:30 -0800282 def ContinuousToDiscrete(self, A_continuous, B_continuous, dt):
283 """Calculates the discrete time values for A and B.
Austin Schuhc1f68892013-03-16 17:06:27 -0700284
Tyler Chatow6738c362019-02-16 14:12:30 -0800285 Args:
286 A_continuous: numpy.matrix, The continuous time A matrix
287 B_continuous: numpy.matrix, The continuous time B matrix
288 dt: float, The time step of the control loop
Austin Schuhc1f68892013-03-16 17:06:27 -0700289
Tyler Chatow6738c362019-02-16 14:12:30 -0800290 Returns:
291 (A, B), numpy.matrix, the control matricies.
292 """
293 return controls.c2d(A_continuous, B_continuous, dt)
Austin Schuh3c542312013-02-24 01:53:50 -0800294
Tyler Chatow6738c362019-02-16 14:12:30 -0800295 def InitializeState(self):
296 """Sets X, Y, and X_hat to zero defaults."""
Austin Schuh43b9ae92020-02-29 23:08:38 -0800297 self.X = numpy.matrix(numpy.zeros((self.A.shape[0], 1)))
Tyler Chatow6738c362019-02-16 14:12:30 -0800298 self.Y = self.C * self.X
Austin Schuh43b9ae92020-02-29 23:08:38 -0800299 self.X_hat = numpy.matrix(numpy.zeros((self.A.shape[0], 1)))
Ravago Jones5127ccc2022-07-31 16:32:45 -0700300 self.last_U = numpy.matrix(
301 numpy.zeros((self.B.shape[1], max(1, self.delayed_u))))
Austin Schuh3c542312013-02-24 01:53:50 -0800302
Tyler Chatow6738c362019-02-16 14:12:30 -0800303 def PlaceControllerPoles(self, poles):
304 """Places the controller poles.
Austin Schuh3c542312013-02-24 01:53:50 -0800305
Tyler Chatow6738c362019-02-16 14:12:30 -0800306 Args:
307 poles: array, An array of poles. Must be complex conjegates if they have
308 any imaginary portions.
309 """
310 self.K = controls.dplace(self.A, self.B, poles)
Austin Schuh3c542312013-02-24 01:53:50 -0800311
Tyler Chatow6738c362019-02-16 14:12:30 -0800312 def PlaceObserverPoles(self, poles):
313 """Places the observer poles.
Austin Schuh3c542312013-02-24 01:53:50 -0800314
Tyler Chatow6738c362019-02-16 14:12:30 -0800315 Args:
316 poles: array, An array of poles. Must be complex conjegates if they have
317 any imaginary portions.
318 """
319 self.L = controls.dplace(self.A.T, self.C.T, poles).T
Sabina Davis3922dfa2018-02-10 23:10:05 -0800320
Tyler Chatow6738c362019-02-16 14:12:30 -0800321 def Update(self, U):
322 """Simulates one time step with the provided U."""
323 #U = numpy.clip(U, self.U_min, self.U_max)
Austin Schuhb39f4522022-03-27 13:29:42 -0700324 if self.delayed_u > 0:
325 self.X = self.A * self.X + self.B * self.last_U[:, -1]
326 self.Y = self.C * self.X + self.D * self.last_U[:, -1]
327 self.last_U[:, 1:] = self.last_U[:, 0:-1]
328 self.last_U[:, 0] = U.copy()
Austin Schuh64433f12022-02-21 19:40:38 -0800329 else:
330 self.X = self.A * self.X + self.B * U
331 self.Y = self.C * self.X + self.D * U
Austin Schuh3c542312013-02-24 01:53:50 -0800332
Tyler Chatow6738c362019-02-16 14:12:30 -0800333 def PredictObserver(self, U):
334 """Runs the predict step of the observer update."""
Austin Schuhb39f4522022-03-27 13:29:42 -0700335 if self.delayed_u > 0:
336 self.X_hat = (self.A * self.X_hat + self.B * self.last_U[:, -1])
337 self.last_U[:, 1:] = self.last_U[:, 0:-1]
338 self.last_U[:, 0] = U.copy()
Austin Schuh64433f12022-02-21 19:40:38 -0800339 else:
340 self.X_hat = (self.A * self.X_hat + self.B * U)
Austin Schuh1a387962015-01-31 16:36:20 -0800341
Tyler Chatow6738c362019-02-16 14:12:30 -0800342 def CorrectObserver(self, U):
343 """Runs the correct step of the observer update."""
344 if hasattr(self, 'KalmanGain'):
345 KalmanGain = self.KalmanGain
346 else:
347 KalmanGain = numpy.linalg.inv(self.A) * self.L
Austin Schuhb39f4522022-03-27 13:29:42 -0700348 if self.delayed_u > 0:
Austin Schuh64433f12022-02-21 19:40:38 -0800349 self.X_hat += KalmanGain * (self.Y - self.C * self.X_hat -
Austin Schuhb39f4522022-03-27 13:29:42 -0700350 self.D * self.last_U[:, -1])
Tyler Chatow6738c362019-02-16 14:12:30 -0800351 else:
Austin Schuh64433f12022-02-21 19:40:38 -0800352 self.X_hat += KalmanGain * (self.Y - self.C * self.X_hat -
353 self.D * U)
Austin Schuh3c542312013-02-24 01:53:50 -0800354
Tyler Chatow6738c362019-02-16 14:12:30 -0800355 def _DumpMatrix(self, matrix_name, matrix, scalar_type):
356 """Dumps the provided matrix into a variable called matrix_name.
Austin Schuh3c542312013-02-24 01:53:50 -0800357
Tyler Chatow6738c362019-02-16 14:12:30 -0800358 Args:
359 matrix_name: string, The variable name to save the matrix to.
360 matrix: The matrix to dump.
361 scalar_type: The C++ type to use for the scalar in the matrix.
Austin Schuh3c542312013-02-24 01:53:50 -0800362
Tyler Chatow6738c362019-02-16 14:12:30 -0800363 Returns:
364 string, The C++ commands required to populate a variable named matrix_name
365 with the contents of matrix.
366 """
367 ans = [
Ravago Jones26f7ad02021-02-05 15:45:59 -0800368 ' Eigen::Matrix<%s, %d, %d> %s;\n' %
369 (scalar_type, matrix.shape[0], matrix.shape[1], matrix_name)
Tyler Chatow6738c362019-02-16 14:12:30 -0800370 ]
Austin Schuh5ea48472021-02-02 20:46:41 -0800371 for x in range(matrix.shape[0]):
372 for y in range(matrix.shape[1]):
Tyler Chatow6738c362019-02-16 14:12:30 -0800373 write_type = repr(matrix[x, y])
374 if scalar_type == 'float':
Austin Schuh085eab92020-11-26 13:54:51 -0800375 if '.' not in write_type and 'e' not in write_type:
Tyler Chatow6738c362019-02-16 14:12:30 -0800376 write_type += '.0'
377 write_type += 'f'
Ravago Jones5127ccc2022-07-31 16:32:45 -0700378 ans.append(' %s(%d, %d) = %s;\n' %
379 (matrix_name, x, y, write_type))
Austin Schuh3c542312013-02-24 01:53:50 -0800380
Tyler Chatow6738c362019-02-16 14:12:30 -0800381 return ''.join(ans)
Austin Schuh3c542312013-02-24 01:53:50 -0800382
Tyler Chatow6738c362019-02-16 14:12:30 -0800383 def DumpPlantHeader(self, plant_coefficient_type):
384 """Writes out a c++ header declaration which will create a Plant object.
Austin Schuh3c542312013-02-24 01:53:50 -0800385
Tyler Chatow6738c362019-02-16 14:12:30 -0800386 Returns:
387 string, The header declaration for the function.
388 """
389 return '%s Make%sPlantCoefficients();\n' % (plant_coefficient_type,
390 self._name)
Austin Schuh3c542312013-02-24 01:53:50 -0800391
Tyler Chatow6738c362019-02-16 14:12:30 -0800392 def DumpPlant(self, plant_coefficient_type, scalar_type):
393 """Writes out a c++ function which will create a PlantCoefficients object.
Austin Schuh3c542312013-02-24 01:53:50 -0800394
Tyler Chatow6738c362019-02-16 14:12:30 -0800395 Returns:
396 string, The function which will create the object.
397 """
398 ans = [
Ravago Jones5127ccc2022-07-31 16:32:45 -0700399 '%s Make%sPlantCoefficients() {\n' %
400 (plant_coefficient_type, self._name)
Tyler Chatow6738c362019-02-16 14:12:30 -0800401 ]
Austin Schuh3c542312013-02-24 01:53:50 -0800402
Tyler Chatow6738c362019-02-16 14:12:30 -0800403 ans.append(self._DumpMatrix('C', self.C, scalar_type))
404 ans.append(self._DumpMatrix('D', self.D, scalar_type))
405 ans.append(self._DumpMatrix('U_max', self.U_max, scalar_type))
406 ans.append(self._DumpMatrix('U_min', self.U_min, scalar_type))
Austin Schuh3c542312013-02-24 01:53:50 -0800407
Austin Schuhb39f4522022-03-27 13:29:42 -0700408 delayed_u_string = str(self.delayed_u)
Tyler Chatow6738c362019-02-16 14:12:30 -0800409 if plant_coefficient_type.startswith('StateFeedbackPlant'):
410 ans.append(self._DumpMatrix('A', self.A, scalar_type))
411 ans.append(self._DumpMatrix('B', self.B, scalar_type))
Ravago Jones5127ccc2022-07-31 16:32:45 -0700412 ans.append(' const std::chrono::nanoseconds dt(%d);\n' %
413 (self.dt * 1e9))
414 ans.append(' return %s'
415 '(A, B, C, D, U_max, U_min, dt, %s);\n' %
416 (plant_coefficient_type, delayed_u_string))
Tyler Chatow6738c362019-02-16 14:12:30 -0800417 elif plant_coefficient_type.startswith('StateFeedbackHybridPlant'):
418 ans.append(
419 self._DumpMatrix('A_continuous', self.A_continuous,
420 scalar_type))
421 ans.append(
422 self._DumpMatrix('B_continuous', self.B_continuous,
423 scalar_type))
Ravago Jones5127ccc2022-07-31 16:32:45 -0700424 ans.append(
425 ' return %s'
426 '(A_continuous, B_continuous, C, D, U_max, U_min, %s);\n' %
427 (plant_coefficient_type, delayed_u_string))
Tyler Chatow6738c362019-02-16 14:12:30 -0800428 else:
429 glog.fatal('Unsupported plant type %s', plant_coefficient_type)
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800430
Tyler Chatow6738c362019-02-16 14:12:30 -0800431 ans.append('}\n')
432 return ''.join(ans)
Austin Schuh3c542312013-02-24 01:53:50 -0800433
Tyler Chatow6738c362019-02-16 14:12:30 -0800434 def PlantFunction(self):
435 """Returns the name of the plant coefficient function."""
436 return 'Make%sPlantCoefficients()' % self._name
Austin Schuh3c542312013-02-24 01:53:50 -0800437
Tyler Chatow6738c362019-02-16 14:12:30 -0800438 def ControllerFunction(self):
439 """Returns the name of the controller function."""
440 return 'Make%sControllerCoefficients()' % self._name
Austin Schuh32501832017-02-25 18:32:56 -0800441
Tyler Chatow6738c362019-02-16 14:12:30 -0800442 def ObserverFunction(self):
443 """Returns the name of the controller function."""
444 return 'Make%sObserverCoefficients()' % self._name
Austin Schuhe3490622013-03-13 01:24:30 -0700445
Tyler Chatow6738c362019-02-16 14:12:30 -0800446 def DumpControllerHeader(self, scalar_type):
447 """Writes out a c++ header declaration which will create a Controller object.
Austin Schuh3c542312013-02-24 01:53:50 -0800448
Tyler Chatow6738c362019-02-16 14:12:30 -0800449 Returns:
450 string, The header declaration for the function.
451 """
452 num_states = self.A.shape[0]
453 num_inputs = self.B.shape[1]
454 num_outputs = self.C.shape[0]
455 return 'StateFeedbackControllerCoefficients<%d, %d, %d, %s> %s;\n' % (
456 num_states, num_inputs, num_outputs, scalar_type,
457 self.ControllerFunction())
Austin Schuh3c542312013-02-24 01:53:50 -0800458
Tyler Chatow6738c362019-02-16 14:12:30 -0800459 def DumpController(self, scalar_type):
460 """Returns a c++ function which will create a Controller object.
Austin Schuh3c542312013-02-24 01:53:50 -0800461
Tyler Chatow6738c362019-02-16 14:12:30 -0800462 Returns:
463 string, The function which will create the object.
464 """
465 num_states = self.A.shape[0]
466 num_inputs = self.B.shape[1]
467 num_outputs = self.C.shape[0]
468 ans = [
469 'StateFeedbackControllerCoefficients<%d, %d, %d, %s> %s {\n' %
470 (num_states, num_inputs, num_outputs, scalar_type,
471 self.ControllerFunction())
472 ]
Austin Schuh3c542312013-02-24 01:53:50 -0800473
Tyler Chatow6738c362019-02-16 14:12:30 -0800474 ans.append(self._DumpMatrix('K', self.K, scalar_type))
475 if not hasattr(self, 'Kff'):
476 self.Kff = numpy.matrix(numpy.zeros(self.K.shape))
Austin Schuh86093ad2016-02-06 14:29:34 -0800477
Tyler Chatow6738c362019-02-16 14:12:30 -0800478 ans.append(self._DumpMatrix('Kff', self.Kff, scalar_type))
Austin Schuh3c542312013-02-24 01:53:50 -0800479
Tyler Chatow6738c362019-02-16 14:12:30 -0800480 ans.append(
481 ' return StateFeedbackControllerCoefficients<%d, %d, %d, %s>'
482 '(K, Kff);\n' % (num_states, num_inputs, num_outputs, scalar_type))
483 ans.append('}\n')
484 return ''.join(ans)
Austin Schuh32501832017-02-25 18:32:56 -0800485
Tyler Chatow6738c362019-02-16 14:12:30 -0800486 def DumpObserverHeader(self, observer_coefficient_type):
487 """Writes out a c++ header declaration which will create a Observer object.
Austin Schuh32501832017-02-25 18:32:56 -0800488
Tyler Chatow6738c362019-02-16 14:12:30 -0800489 Returns:
490 string, The header declaration for the function.
491 """
Ravago Jones26f7ad02021-02-05 15:45:59 -0800492 return '%s %s;\n' % (observer_coefficient_type,
493 self.ObserverFunction())
Austin Schuh32501832017-02-25 18:32:56 -0800494
Tyler Chatow6738c362019-02-16 14:12:30 -0800495 def DumpObserver(self, observer_coefficient_type, scalar_type):
496 """Returns a c++ function which will create a Observer object.
Austin Schuh32501832017-02-25 18:32:56 -0800497
Tyler Chatow6738c362019-02-16 14:12:30 -0800498 Returns:
499 string, The function which will create the object.
500 """
501 ans = [
502 '%s %s {\n' % (observer_coefficient_type, self.ObserverFunction())
503 ]
Austin Schuh32501832017-02-25 18:32:56 -0800504
Austin Schuhb39f4522022-03-27 13:29:42 -0700505 delayed_u_string = str(self.delayed_u)
Tyler Chatow6738c362019-02-16 14:12:30 -0800506 if observer_coefficient_type.startswith('StateFeedbackObserver'):
507 if hasattr(self, 'KalmanGain'):
508 KalmanGain = self.KalmanGain
509 Q = self.Q
510 R = self.R
511 else:
512 KalmanGain = numpy.linalg.inv(self.A) * self.L
513 Q = numpy.zeros(self.A.shape)
514 R = numpy.zeros((self.C.shape[0], self.C.shape[0]))
515 ans.append(self._DumpMatrix('KalmanGain', KalmanGain, scalar_type))
516 ans.append(self._DumpMatrix('Q', Q, scalar_type))
517 ans.append(self._DumpMatrix('R', R, scalar_type))
Austin Schuh64433f12022-02-21 19:40:38 -0800518 ans.append(' return %s(KalmanGain, Q, R, %s);\n' %
519 (observer_coefficient_type, delayed_u_string))
Sabina Davis3922dfa2018-02-10 23:10:05 -0800520
Tyler Chatow6738c362019-02-16 14:12:30 -0800521 elif observer_coefficient_type.startswith('HybridKalman'):
522 ans.append(
523 self._DumpMatrix('Q_continuous', self.Q_continuous,
524 scalar_type))
525 ans.append(
526 self._DumpMatrix('R_continuous', self.R_continuous,
527 scalar_type))
528 ans.append(
529 self._DumpMatrix('P_steady_state', self.P_steady_state,
530 scalar_type))
531 ans.append(
Ravago Jones5127ccc2022-07-31 16:32:45 -0700532 ' return %s(Q_continuous, R_continuous, P_steady_state, %s);\n'
533 % (observer_coefficient_type, delayed_u_string))
Tyler Chatow6738c362019-02-16 14:12:30 -0800534 else:
535 glog.fatal('Unsupported observer type %s',
536 observer_coefficient_type)
Austin Schuh32501832017-02-25 18:32:56 -0800537
Tyler Chatow6738c362019-02-16 14:12:30 -0800538 ans.append('}\n')
539 return ''.join(ans)
540
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800541
542class HybridControlLoop(ControlLoop):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700543
Tyler Chatow6738c362019-02-16 14:12:30 -0800544 def __init__(self, name):
545 super(HybridControlLoop, self).__init__(name=name)
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800546
Tyler Chatow6738c362019-02-16 14:12:30 -0800547 def Discretize(self, dt):
548 [self.A, self.B, self.Q, self.R] = \
549 controls.kalmd(self.A_continuous, self.B_continuous,
550 self.Q_continuous, self.R_continuous, dt)
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800551
Tyler Chatow6738c362019-02-16 14:12:30 -0800552 def PredictHybridObserver(self, U, dt):
553 self.Discretize(dt)
Austin Schuhb39f4522022-03-27 13:29:42 -0700554 if self.delayed_u > 0:
555 self.X_hat = self.A * self.X_hat + self.B * self.last_U[:, -1]
556 self.last_U[:, 1:] = self.last_U[:, 0:-1]
557 self.last_U[:, 0] = U.copy()
Austin Schuh64433f12022-02-21 19:40:38 -0800558 else:
559 self.X_hat = self.A * self.X_hat + self.B * U
560
Tyler Chatow6738c362019-02-16 14:12:30 -0800561 self.P = (self.A * self.P * self.A.T + self.Q)
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800562
Tyler Chatow6738c362019-02-16 14:12:30 -0800563 def CorrectHybridObserver(self, U):
564 Y_bar = self.Y - self.C * self.X_hat
565 C_t = self.C.T
566 S = self.C * self.P * C_t + self.R
567 self.KalmanGain = self.P * C_t * numpy.linalg.inv(S)
568 self.X_hat = self.X_hat + self.KalmanGain * Y_bar
569 self.P = (numpy.eye(len(self.A)) - self.KalmanGain * self.C) * self.P
570
571 def InitializeState(self):
572 super(HybridControlLoop, self).InitializeState()
573 if hasattr(self, 'Q_steady_state'):
574 self.P = self.Q_steady_state
575 else:
576 self.P = numpy.matrix(
577 numpy.zeros((self.A.shape[0], self.A.shape[0])))
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800578
579
580class CIM(object):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700581
Tyler Chatow6738c362019-02-16 14:12:30 -0800582 def __init__(self):
583 # Stall Torque in N m
584 self.stall_torque = 2.42
585 # Stall Current in Amps
586 self.stall_current = 133.0
587 # Free Speed in rad/s
588 self.free_speed = 5500.0 / 60.0 * 2.0 * numpy.pi
589 # Free Current in Amps
590 self.free_current = 4.7
591 # Resistance of the motor
592 self.resistance = 12.0 / self.stall_current
593 # Motor velocity constant
Ravago Jones5127ccc2022-07-31 16:32:45 -0700594 self.Kv = (self.free_speed /
595 (12.0 - self.resistance * self.free_current))
Tyler Chatow6738c362019-02-16 14:12:30 -0800596 # Torque constant
597 self.Kt = self.stall_torque / self.stall_current
Lee Mracek97fc8af2018-01-13 04:38:52 -0500598
599
600class MiniCIM(object):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700601
Tyler Chatow6738c362019-02-16 14:12:30 -0800602 def __init__(self):
603 # Stall Torque in N m
604 self.stall_torque = 1.41
605 # Stall Current in Amps
606 self.stall_current = 89.0
607 # Free Speed in rad/s
608 self.free_speed = 5840.0 / 60.0 * 2.0 * numpy.pi
609 # Free Current in Amps
610 self.free_current = 3.0
611 # Resistance of the motor
612 self.resistance = 12.0 / self.stall_current
613 # Motor velocity constant
Ravago Jones5127ccc2022-07-31 16:32:45 -0700614 self.Kv = (self.free_speed /
615 (12.0 - self.resistance * self.free_current))
Tyler Chatow6738c362019-02-16 14:12:30 -0800616 # Torque constant
617 self.Kt = self.stall_torque / self.stall_current
Austin Schuhf173eb82018-01-20 23:32:30 -0800618
milind-uf70e8e12021-10-02 12:36:00 -0700619 # Motor inertia in kg m^2
620 self.motor_inertia = 0.0001634
621
Austin Schuhf173eb82018-01-20 23:32:30 -0800622
Austin Schuhb5d302f2019-01-20 20:51:19 -0800623class NMotor(object):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700624
Austin Schuhb5d302f2019-01-20 20:51:19 -0800625 def __init__(self, motor, n):
626 """Gangs together n motors."""
627 self.motor = motor
628 self.stall_torque = motor.stall_torque * n
629 self.stall_current = motor.stall_current * n
630 self.free_speed = motor.free_speed
631
632 self.free_current = motor.free_current * n
633 self.resistance = motor.resistance / n
634 self.Kv = motor.Kv
635 self.Kt = motor.Kt
Austin Schuh36bb8e32019-02-18 15:02:57 -0800636 self.motor_inertia = motor.motor_inertia * n
Austin Schuhb5d302f2019-01-20 20:51:19 -0800637
638
639class Vex775Pro(object):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700640
Austin Schuhb5d302f2019-01-20 20:51:19 -0800641 def __init__(self):
642 # Stall Torque in N m
643 self.stall_torque = 0.71
644 # Stall Current in Amps
645 self.stall_current = 134.0
646 # Free Speed in rad/s
647 self.free_speed = 18730.0 / 60.0 * 2.0 * numpy.pi
648 # Free Current in Amps
649 self.free_current = 0.7
650 # Resistance of the motor
651 self.resistance = 12.0 / self.stall_current
652 # Motor velocity constant
Ravago Jones5127ccc2022-07-31 16:32:45 -0700653 self.Kv = (self.free_speed /
654 (12.0 - self.resistance * self.free_current))
Austin Schuhb5d302f2019-01-20 20:51:19 -0800655 # Torque constant
656 self.Kt = self.stall_torque / self.stall_current
657 # Motor inertia in kg m^2
658 self.motor_inertia = 0.00001187
659
660
Austin Schuhf173eb82018-01-20 23:32:30 -0800661class BAG(object):
Tyler Chatow6738c362019-02-16 14:12:30 -0800662 # BAG motor specs available at http://motors.vex.com/vexpro-motors/bag-motor
663 def __init__(self):
664 # Stall Torque in (N m)
665 self.stall_torque = 0.43
666 # Stall Current in (Amps)
667 self.stall_current = 53.0
668 # Free Speed in (rad/s)
669 self.free_speed = 13180.0 / 60.0 * 2.0 * numpy.pi
670 # Free Current in (Amps)
671 self.free_current = 1.8
672 # Resistance of the motor (Ohms)
673 self.resistance = 12.0 / self.stall_current
674 # Motor velocity constant (radians / (sec * volt))
Ravago Jones5127ccc2022-07-31 16:32:45 -0700675 self.Kv = (self.free_speed /
676 (12.0 - self.resistance * self.free_current))
Tyler Chatow6738c362019-02-16 14:12:30 -0800677 # Torque constant (N * m / A)
678 self.Kt = self.stall_torque / self.stall_current
679 # Motor inertia in kg m^2
680 self.motor_inertia = 0.000006
681
Brian Silverman6260c092018-01-14 15:21:36 -0800682
683class MN3510(object):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700684
Tyler Chatow6738c362019-02-16 14:12:30 -0800685 def __init__(self):
686 # http://www.robotshop.com/en/t-motor-navigator-mn3510-360kv-brushless-motor.html#Specifications
687 # Free Current in Amps
688 self.free_current = 0.0
689 # Resistance of the motor
690 self.resistance = 0.188
691 # Stall Current in Amps
692 self.stall_current = 14.0 / self.resistance
693 # Motor velocity constant
694 self.Kv = 360.0 / 60.0 * (2.0 * numpy.pi)
695 # Torque constant Nm / A
696 self.Kt = 1.0 / self.Kv
697 # Stall Torque in N m
698 self.stall_torque = self.Kt * self.stall_current
James Kuszmaulef0c18a2020-01-12 15:44:20 -0800699
700
701class Falcon(object):
702 """Class representing the VexPro Falcon 500 motor.
703
704 All numbers based on data from
705 https://www.vexrobotics.com/vexpro/falcon-500."""
706
707 def __init__(self):
708 # Stall Torque in N m
709 self.stall_torque = 4.69
710 # Stall Current in Amps
711 self.stall_current = 257.0
712 # Free Speed in rad / sec
713 self.free_speed = 6380.0 / 60.0 * 2.0 * numpy.pi
714 # Free Current in Amps
715 self.free_current = 1.5
716 # Resistance of the motor, divided by 2 to account for the 2 motors
717 self.resistance = 12.0 / self.stall_current
718 # Motor velocity constant
Ravago Jones5127ccc2022-07-31 16:32:45 -0700719 self.Kv = (self.free_speed /
720 (12.0 - self.resistance * self.free_current))
James Kuszmaulef0c18a2020-01-12 15:44:20 -0800721 # Torque constant
722 self.Kt = self.stall_torque / self.stall_current
Austin Schuhc1c957a2020-02-20 17:47:58 -0800723 # Motor inertia in kg m^2
724 # Diameter of 1.9", weight of: 100 grams
725 # TODO(austin): Get a number from Scott Westbrook for the mass
Ravago Jones26f7ad02021-02-05 15:45:59 -0800726 self.motor_inertia = 0.1 * ((0.95 * 0.0254)**2.0)