blob: ec44092bb685da40d13d5d9b5e06ac71236647a5 [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
James Kuszmauleeb98e92024-01-14 22:15:32 -08004import json
Austin Schuh3c542312013-02-24 01:53:50 -08005
Austin Schuhbcce26a2018-03-26 23:41:24 -07006
Tyler Chatow6738c362019-02-16 14:12:30 -08007class Constant(object):
Ravago Jones5127ccc2022-07-31 16:32:45 -07008
James Kuszmaul62c3bd82024-01-17 20:03:05 -08009 def __init__(self,
10 name,
11 formatt,
12 value,
13 comment=None,
14 json_name=None,
15 json_scale=1.0,
16 json_type=None):
Tyler Chatow6738c362019-02-16 14:12:30 -080017 self.name = name
18 self.formatt = formatt
19 self.value = value
20 self.formatToType = {}
21 self.formatToType['%f'] = "double"
22 self.formatToType['%d'] = "int"
Austin Schuhe8ca06a2020-03-07 22:27:39 -080023 if comment is None:
24 self.comment = ""
25 else:
26 self.comment = comment + "\n"
James Kuszmaul62c3bd82024-01-17 20:03:05 -080027 self.json_name = json_name
28 self.json_scale = json_scale
29 self.json_type = json_type
Tyler Chatow6738c362019-02-16 14:12:30 -080030
31 def Render(self, loop_type):
32 typestring = self.formatToType[self.formatt]
33 if loop_type == 'float' and typestring == 'double':
34 typestring = loop_type
Austin Schuhe8ca06a2020-03-07 22:27:39 -080035 return str("\n%sstatic constexpr %s %s = "+ self.formatt +";\n") % \
36 (self.comment, typestring, self.name, self.value)
Ben Fredrickson1b45f782014-02-23 07:44:36 +000037
James Kuszmaul62c3bd82024-01-17 20:03:05 -080038 def RenderJson(self, json_dict):
39 if self.json_name is None:
40 return
41 json_value = self.value * self.json_scale
42 json_dict[
43 self.
44 json_name] = json_value if self.json_type is None else self.json_type(
45 json_value)
46 return json_dict
47
Ben Fredrickson1b45f782014-02-23 07:44:36 +000048
James Kuszmauleeb98e92024-01-14 22:15:32 -080049def MatrixToJson(matrix):
50 """Returns JSON representation of a numpy matrix."""
51 return {
52 "rows": matrix.shape[0],
53 "cols": matrix.shape[1],
54 "storage_order": "ColMajor",
55 "data": numpy.array(matrix).flatten(order='F').tolist()
56 }
57
58
Austin Schuhe3490622013-03-13 01:24:30 -070059class ControlLoopWriter(object):
Ravago Jones5127ccc2022-07-31 16:32:45 -070060
Tyler Chatow6738c362019-02-16 14:12:30 -080061 def __init__(self,
62 gain_schedule_name,
63 loops,
64 namespaces=None,
65 write_constants=False,
66 plant_type='StateFeedbackPlant',
67 observer_type='StateFeedbackObserver',
68 scalar_type='double'):
69 """Constructs a control loop writer.
Austin Schuhe3490622013-03-13 01:24:30 -070070
Tyler Chatow6738c362019-02-16 14:12:30 -080071 Args:
72 gain_schedule_name: string, Name of the overall controller.
73 loops: array[ControlLoop], a list of control loops to gain schedule
74 in order.
75 namespaces: array[string], a list of names of namespaces to nest in
76 order. If None, the default will be used.
77 plant_type: string, The C++ type of the plant.
78 observer_type: string, The C++ type of the observer.
79 scalar_type: string, The C++ type of the base scalar.
80 """
81 self._gain_schedule_name = gain_schedule_name
82 self._loops = loops
83 if namespaces:
84 self._namespaces = namespaces
85 else:
86 self._namespaces = ['frc971', 'control_loops']
Austin Schuhe3490622013-03-13 01:24:30 -070087
Tyler Chatow6738c362019-02-16 14:12:30 -080088 self._namespace_start = '\n'.join(
89 ['namespace %s {' % name for name in self._namespaces])
Austin Schuh86093ad2016-02-06 14:29:34 -080090
Tyler Chatow6738c362019-02-16 14:12:30 -080091 self._namespace_end = '\n'.join([
92 '} // namespace %s' % name for name in reversed(self._namespaces)
93 ])
Austin Schuh25933852014-02-23 02:04:13 -080094
Tyler Chatow6738c362019-02-16 14:12:30 -080095 self._constant_list = []
96 self._plant_type = plant_type
97 self._observer_type = observer_type
98 self._scalar_type = scalar_type
Austin Schuh25933852014-02-23 02:04:13 -080099
Tyler Chatow6738c362019-02-16 14:12:30 -0800100 def AddConstant(self, constant):
101 """Adds a constant to write.
Austin Schuhe3490622013-03-13 01:24:30 -0700102
Tyler Chatow6738c362019-02-16 14:12:30 -0800103 Args:
104 constant: Constant, the constant to add to the header.
105 """
106 self._constant_list.append(constant)
Brian Silvermane51ad632014-01-08 15:12:29 -0800107
Tyler Chatow6738c362019-02-16 14:12:30 -0800108 def _TopDirectory(self):
109 return self._namespaces[0]
Austin Schuhe3490622013-03-13 01:24:30 -0700110
Tyler Chatow6738c362019-02-16 14:12:30 -0800111 def _HeaderGuard(self, header_file):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700112 return ('_'.join([namespace.upper()
113 for namespace in self._namespaces]) + '_' +
114 os.path.basename(header_file).upper().replace(
Tyler Chatow6738c362019-02-16 14:12:30 -0800115 '.', '_').replace('/', '_') + '_')
Austin Schuhe3490622013-03-13 01:24:30 -0700116
James Kuszmaul62c3bd82024-01-17 20:03:05 -0800117 def Write(self, header_file, cc_file, json_file=None, json_field=None):
Tyler Chatow6738c362019-02-16 14:12:30 -0800118 """Writes the loops to the specified files."""
119 self.WriteHeader(header_file)
120 self.WriteCC(os.path.basename(header_file), cc_file)
James Kuszmauleeb98e92024-01-14 22:15:32 -0800121 if json_file is not None:
James Kuszmaul62c3bd82024-01-17 20:03:05 -0800122 self.WriteJson(json_file, json_field)
Austin Schuhe3490622013-03-13 01:24:30 -0700123
Tyler Chatow6738c362019-02-16 14:12:30 -0800124 def _GenericType(self, typename, extra_args=None):
125 """Returns a loop template using typename for the type."""
126 num_states = self._loops[0].A.shape[0]
127 num_inputs = self._loops[0].B.shape[1]
128 num_outputs = self._loops[0].C.shape[0]
129 if extra_args is not None:
130 extra_args = ', ' + extra_args
131 else:
132 extra_args = ''
133 if self._scalar_type != 'double':
134 extra_args += ', ' + self._scalar_type
135 return '%s<%d, %d, %d%s>' % (typename, num_states, num_inputs,
136 num_outputs, extra_args)
Austin Schuh32501832017-02-25 18:32:56 -0800137
Tyler Chatow6738c362019-02-16 14:12:30 -0800138 def _ControllerType(self):
139 """Returns a template name for StateFeedbackController."""
140 return self._GenericType('StateFeedbackController')
Austin Schuhe3490622013-03-13 01:24:30 -0700141
Tyler Chatow6738c362019-02-16 14:12:30 -0800142 def _ObserverType(self):
143 """Returns a template name for StateFeedbackObserver."""
144 return self._GenericType(self._observer_type)
Austin Schuh20388b62017-11-23 22:40:46 -0800145
Tyler Chatow6738c362019-02-16 14:12:30 -0800146 def _LoopType(self):
147 """Returns a template name for StateFeedbackLoop."""
148 num_states = self._loops[0].A.shape[0]
149 num_inputs = self._loops[0].B.shape[1]
150 num_outputs = self._loops[0].C.shape[0]
Austin Schuh20388b62017-11-23 22:40:46 -0800151
Tyler Chatow6738c362019-02-16 14:12:30 -0800152 return 'StateFeedbackLoop<%d, %d, %d, %s, %s, %s>' % (
153 num_states, num_inputs, num_outputs, self._scalar_type,
154 self._PlantType(), self._ObserverType())
Austin Schuhe3490622013-03-13 01:24:30 -0700155
Tyler Chatow6738c362019-02-16 14:12:30 -0800156 def _PlantType(self):
157 """Returns a template name for StateFeedbackPlant."""
158 return self._GenericType(self._plant_type)
Austin Schuhe3490622013-03-13 01:24:30 -0700159
Tyler Chatow6738c362019-02-16 14:12:30 -0800160 def _PlantCoeffType(self):
161 """Returns a template name for StateFeedbackPlantCoefficients."""
162 return self._GenericType(self._plant_type + 'Coefficients')
Austin Schuhe3490622013-03-13 01:24:30 -0700163
Tyler Chatow6738c362019-02-16 14:12:30 -0800164 def _ControllerCoeffType(self):
165 """Returns a template name for StateFeedbackControllerCoefficients."""
166 return self._GenericType('StateFeedbackControllerCoefficients')
Austin Schuh32501832017-02-25 18:32:56 -0800167
Tyler Chatow6738c362019-02-16 14:12:30 -0800168 def _ObserverCoeffType(self):
169 """Returns a template name for StateFeedbackObserverCoefficients."""
170 return self._GenericType(self._observer_type + 'Coefficients')
Austin Schuh32501832017-02-25 18:32:56 -0800171
Tyler Chatow6738c362019-02-16 14:12:30 -0800172 def WriteHeader(self, header_file):
173 """Writes the header file to the file named header_file."""
174 with open(header_file, 'w') as fd:
175 header_guard = self._HeaderGuard(header_file)
176 fd.write('#ifndef %s\n'
177 '#define %s\n\n' % (header_guard, header_guard))
178 fd.write(
179 '#include \"frc971/control_loops/state_feedback_loop.h\"\n')
Ravago Jones26f7ad02021-02-05 15:45:59 -0800180 if (self._plant_type == 'StateFeedbackHybridPlant'
181 or self._observer_type == 'HybridKalman'):
Tyler Chatow6738c362019-02-16 14:12:30 -0800182 fd.write(
183 '#include \"frc971/control_loops/hybrid_state_feedback_loop.h\"\n'
184 )
Austin Schuh4cc4fe22017-11-23 19:13:09 -0800185
Tyler Chatow6738c362019-02-16 14:12:30 -0800186 fd.write('\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700187
Tyler Chatow6738c362019-02-16 14:12:30 -0800188 fd.write(self._namespace_start)
Ben Fredrickson1b45f782014-02-23 07:44:36 +0000189
Tyler Chatow6738c362019-02-16 14:12:30 -0800190 for const in self._constant_list:
191 fd.write(const.Render(self._scalar_type))
Ben Fredrickson1b45f782014-02-23 07:44:36 +0000192
Tyler Chatow6738c362019-02-16 14:12:30 -0800193 fd.write('\n\n')
194 for loop in self._loops:
195 fd.write(loop.DumpPlantHeader(self._PlantCoeffType()))
196 fd.write('\n')
197 fd.write(loop.DumpControllerHeader(self._scalar_type))
198 fd.write('\n')
199 fd.write(loop.DumpObserverHeader(self._ObserverCoeffType()))
200 fd.write('\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700201
Ravago Jones5127ccc2022-07-31 16:32:45 -0700202 fd.write('%s Make%sPlant();\n\n' %
203 (self._PlantType(), self._gain_schedule_name))
Austin Schuhe3490622013-03-13 01:24:30 -0700204
Ravago Jones5127ccc2022-07-31 16:32:45 -0700205 fd.write('%s Make%sController();\n\n' %
206 (self._ControllerType(), self._gain_schedule_name))
Austin Schuh32501832017-02-25 18:32:56 -0800207
Ravago Jones5127ccc2022-07-31 16:32:45 -0700208 fd.write('%s Make%sObserver();\n\n' %
209 (self._ObserverType(), self._gain_schedule_name))
Austin Schuh32501832017-02-25 18:32:56 -0800210
Ravago Jones5127ccc2022-07-31 16:32:45 -0700211 fd.write('%s Make%sLoop();\n\n' %
212 (self._LoopType(), self._gain_schedule_name))
Austin Schuhe3490622013-03-13 01:24:30 -0700213
Tyler Chatow6738c362019-02-16 14:12:30 -0800214 fd.write(self._namespace_end)
215 fd.write('\n\n')
216 fd.write("#endif // %s\n" % header_guard)
Austin Schuhe3490622013-03-13 01:24:30 -0700217
Tyler Chatow6738c362019-02-16 14:12:30 -0800218 def WriteCC(self, header_file_name, cc_file):
219 """Writes the cc file to the file named cc_file."""
220 with open(cc_file, 'w') as fd:
Ravago Jones5127ccc2022-07-31 16:32:45 -0700221 fd.write('#include \"%s/%s\"\n' %
222 (os.path.join(*self._namespaces), header_file_name))
Tyler Chatow6738c362019-02-16 14:12:30 -0800223 fd.write('\n')
James Kuszmaul03be1242020-02-21 14:52:04 -0800224 fd.write('#include <chrono>\n')
Tyler Chatow6738c362019-02-16 14:12:30 -0800225 fd.write('#include <vector>\n')
226 fd.write('\n')
227 fd.write(
228 '#include \"frc971/control_loops/state_feedback_loop.h\"\n')
229 fd.write('\n')
230 fd.write(self._namespace_start)
231 fd.write('\n\n')
232 for loop in self._loops:
233 fd.write(
234 loop.DumpPlant(self._PlantCoeffType(), self._scalar_type))
235 fd.write('\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700236
Tyler Chatow6738c362019-02-16 14:12:30 -0800237 for loop in self._loops:
238 fd.write(loop.DumpController(self._scalar_type))
239 fd.write('\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700240
Tyler Chatow6738c362019-02-16 14:12:30 -0800241 for loop in self._loops:
242 fd.write(
243 loop.DumpObserver(self._ObserverCoeffType(),
244 self._scalar_type))
245 fd.write('\n')
Austin Schuh32501832017-02-25 18:32:56 -0800246
Ravago Jones5127ccc2022-07-31 16:32:45 -0700247 fd.write('%s Make%sPlant() {\n' %
248 (self._PlantType(), self._gain_schedule_name))
Tyler Chatow6738c362019-02-16 14:12:30 -0800249 fd.write(' ::std::vector< ::std::unique_ptr<%s>> plants(%d);\n' %
250 (self._PlantCoeffType(), len(self._loops)))
251 for index, loop in enumerate(self._loops):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700252 fd.write(
253 ' plants[%d] = ::std::unique_ptr<%s>(new %s(%s));\n' %
254 (index, self._PlantCoeffType(), self._PlantCoeffType(),
255 loop.PlantFunction()))
Austin Schuhb02bf5b2021-07-31 21:28:21 -0700256 fd.write(' return %s(std::move(plants));\n' % self._PlantType())
Tyler Chatow6738c362019-02-16 14:12:30 -0800257 fd.write('}\n\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700258
Ravago Jones5127ccc2022-07-31 16:32:45 -0700259 fd.write('%s Make%sController() {\n' %
260 (self._ControllerType(), self._gain_schedule_name))
Tyler Chatow6738c362019-02-16 14:12:30 -0800261 fd.write(
262 ' ::std::vector< ::std::unique_ptr<%s>> controllers(%d);\n' %
263 (self._ControllerCoeffType(), len(self._loops)))
264 for index, loop in enumerate(self._loops):
265 fd.write(
Ravago Jones26f7ad02021-02-05 15:45:59 -0800266 ' controllers[%d] = ::std::unique_ptr<%s>(new %s(%s));\n'
267 % (index, self._ControllerCoeffType(),
268 self._ControllerCoeffType(), loop.ControllerFunction()))
Austin Schuhb02bf5b2021-07-31 21:28:21 -0700269 fd.write(' return %s(std::move(controllers));\n' %
270 self._ControllerType())
Tyler Chatow6738c362019-02-16 14:12:30 -0800271 fd.write('}\n\n')
Austin Schuh32501832017-02-25 18:32:56 -0800272
Ravago Jones5127ccc2022-07-31 16:32:45 -0700273 fd.write('%s Make%sObserver() {\n' %
274 (self._ObserverType(), self._gain_schedule_name))
275 fd.write(
276 ' ::std::vector< ::std::unique_ptr<%s>> observers(%d);\n' %
277 (self._ObserverCoeffType(), len(self._loops)))
Tyler Chatow6738c362019-02-16 14:12:30 -0800278 for index, loop in enumerate(self._loops):
279 fd.write(
Ravago Jones5127ccc2022-07-31 16:32:45 -0700280 ' observers[%d] = ::std::unique_ptr<%s>(new %s(%s));\n' %
281 (index, self._ObserverCoeffType(),
282 self._ObserverCoeffType(), loop.ObserverFunction()))
283 fd.write(' return %s(std::move(observers));\n' %
284 self._ObserverType())
Tyler Chatow6738c362019-02-16 14:12:30 -0800285 fd.write('}\n\n')
Austin Schuh32501832017-02-25 18:32:56 -0800286
Ravago Jones5127ccc2022-07-31 16:32:45 -0700287 fd.write('%s Make%sLoop() {\n' %
288 (self._LoopType(), self._gain_schedule_name))
Tyler Chatow6738c362019-02-16 14:12:30 -0800289 fd.write(
290 ' return %s(Make%sPlant(), Make%sController(), Make%sObserver());\n'
291 % (self._LoopType(), self._gain_schedule_name,
292 self._gain_schedule_name, self._gain_schedule_name))
293 fd.write('}\n\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700294
Tyler Chatow6738c362019-02-16 14:12:30 -0800295 fd.write(self._namespace_end)
296 fd.write('\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700297
James Kuszmaul62c3bd82024-01-17 20:03:05 -0800298 def WriteJson(self, json_file, json_field):
James Kuszmauleeb98e92024-01-14 22:15:32 -0800299 """Writes a JSON file of the loop constants to the specified json_file."""
300 loops = []
301 for loop in self._loops:
302 loop_json = {}
303 loop_json["plant"] = loop.DumpPlantJson(self._PlantCoeffType())
304 loop_json["controller"] = loop.DumpControllerJson()
305 loop_json["observer"] = loop.DumbObserverJson(
306 self._ObserverCoeffType())
307 loops.append(loop_json)
James Kuszmaul62c3bd82024-01-17 20:03:05 -0800308 if json_field is None:
309 with open(json_file, 'w') as f:
310 f.write(json.dumps(loops))
311 return
312 loop_config = {}
313 loop_config[json_field] = loops
314 for const in self._constant_list:
315 const.RenderJson(loop_config)
James Kuszmauleeb98e92024-01-14 22:15:32 -0800316 with open(json_file, 'w') as f:
James Kuszmaul62c3bd82024-01-17 20:03:05 -0800317 f.write(json.dumps(loop_config))
James Kuszmauleeb98e92024-01-14 22:15:32 -0800318
Austin Schuhe3490622013-03-13 01:24:30 -0700319
Austin Schuh3c542312013-02-24 01:53:50 -0800320class ControlLoop(object):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700321
Tyler Chatow6738c362019-02-16 14:12:30 -0800322 def __init__(self, name):
323 """Constructs a control loop object.
Austin Schuh3c542312013-02-24 01:53:50 -0800324
Tyler Chatow6738c362019-02-16 14:12:30 -0800325 Args:
326 name: string, The name of the loop to use when writing the C++ files.
327 """
328 self._name = name
Austin Schuhb39f4522022-03-27 13:29:42 -0700329 self.delayed_u = 0
Austin Schuhb5d302f2019-01-20 20:51:19 -0800330
Tyler Chatow6738c362019-02-16 14:12:30 -0800331 @property
332 def name(self):
333 """Returns the name"""
334 return self._name
Austin Schuh3c542312013-02-24 01:53:50 -0800335
Tyler Chatow6738c362019-02-16 14:12:30 -0800336 def ContinuousToDiscrete(self, A_continuous, B_continuous, dt):
337 """Calculates the discrete time values for A and B.
Austin Schuhc1f68892013-03-16 17:06:27 -0700338
Tyler Chatow6738c362019-02-16 14:12:30 -0800339 Args:
340 A_continuous: numpy.matrix, The continuous time A matrix
341 B_continuous: numpy.matrix, The continuous time B matrix
342 dt: float, The time step of the control loop
Austin Schuhc1f68892013-03-16 17:06:27 -0700343
Tyler Chatow6738c362019-02-16 14:12:30 -0800344 Returns:
345 (A, B), numpy.matrix, the control matricies.
346 """
347 return controls.c2d(A_continuous, B_continuous, dt)
Austin Schuh3c542312013-02-24 01:53:50 -0800348
Tyler Chatow6738c362019-02-16 14:12:30 -0800349 def InitializeState(self):
350 """Sets X, Y, and X_hat to zero defaults."""
Austin Schuh43b9ae92020-02-29 23:08:38 -0800351 self.X = numpy.matrix(numpy.zeros((self.A.shape[0], 1)))
Tyler Chatow6738c362019-02-16 14:12:30 -0800352 self.Y = self.C * self.X
Austin Schuh43b9ae92020-02-29 23:08:38 -0800353 self.X_hat = numpy.matrix(numpy.zeros((self.A.shape[0], 1)))
Ravago Jones5127ccc2022-07-31 16:32:45 -0700354 self.last_U = numpy.matrix(
355 numpy.zeros((self.B.shape[1], max(1, self.delayed_u))))
James Kuszmaul9ea3ff92024-06-14 15:02:15 -0700356 self.wrap_point = numpy.matrix(numpy.zeros(self.Y.shape))
Austin Schuh3c542312013-02-24 01:53:50 -0800357
Tyler Chatow6738c362019-02-16 14:12:30 -0800358 def PlaceControllerPoles(self, poles):
359 """Places the controller poles.
Austin Schuh3c542312013-02-24 01:53:50 -0800360
Tyler Chatow6738c362019-02-16 14:12:30 -0800361 Args:
362 poles: array, An array of poles. Must be complex conjegates if they have
363 any imaginary portions.
364 """
365 self.K = controls.dplace(self.A, self.B, poles)
Austin Schuh3c542312013-02-24 01:53:50 -0800366
Tyler Chatow6738c362019-02-16 14:12:30 -0800367 def PlaceObserverPoles(self, poles):
368 """Places the observer poles.
Austin Schuh3c542312013-02-24 01:53:50 -0800369
Tyler Chatow6738c362019-02-16 14:12:30 -0800370 Args:
371 poles: array, An array of poles. Must be complex conjegates if they have
372 any imaginary portions.
373 """
374 self.L = controls.dplace(self.A.T, self.C.T, poles).T
Sabina Davis3922dfa2018-02-10 23:10:05 -0800375
Tyler Chatow6738c362019-02-16 14:12:30 -0800376 def Update(self, U):
377 """Simulates one time step with the provided U."""
378 #U = numpy.clip(U, self.U_min, self.U_max)
Austin Schuhb39f4522022-03-27 13:29:42 -0700379 if self.delayed_u > 0:
380 self.X = self.A * self.X + self.B * self.last_U[:, -1]
381 self.Y = self.C * self.X + self.D * self.last_U[:, -1]
382 self.last_U[:, 1:] = self.last_U[:, 0:-1]
383 self.last_U[:, 0] = U.copy()
Austin Schuh64433f12022-02-21 19:40:38 -0800384 else:
385 self.X = self.A * self.X + self.B * U
386 self.Y = self.C * self.X + self.D * U
Austin Schuh3c542312013-02-24 01:53:50 -0800387
Tyler Chatow6738c362019-02-16 14:12:30 -0800388 def PredictObserver(self, U):
389 """Runs the predict step of the observer update."""
Austin Schuhb39f4522022-03-27 13:29:42 -0700390 if self.delayed_u > 0:
391 self.X_hat = (self.A * self.X_hat + self.B * self.last_U[:, -1])
392 self.last_U[:, 1:] = self.last_U[:, 0:-1]
393 self.last_U[:, 0] = U.copy()
Austin Schuh64433f12022-02-21 19:40:38 -0800394 else:
395 self.X_hat = (self.A * self.X_hat + self.B * U)
Austin Schuh1a387962015-01-31 16:36:20 -0800396
Tyler Chatow6738c362019-02-16 14:12:30 -0800397 def CorrectObserver(self, U):
398 """Runs the correct step of the observer update."""
399 if hasattr(self, 'KalmanGain'):
400 KalmanGain = self.KalmanGain
401 else:
402 KalmanGain = numpy.linalg.inv(self.A) * self.L
Austin Schuhb39f4522022-03-27 13:29:42 -0700403 if self.delayed_u > 0:
Austin Schuh64433f12022-02-21 19:40:38 -0800404 self.X_hat += KalmanGain * (self.Y - self.C * self.X_hat -
Austin Schuhb39f4522022-03-27 13:29:42 -0700405 self.D * self.last_U[:, -1])
Tyler Chatow6738c362019-02-16 14:12:30 -0800406 else:
Austin Schuh64433f12022-02-21 19:40:38 -0800407 self.X_hat += KalmanGain * (self.Y - self.C * self.X_hat -
408 self.D * U)
Austin Schuh3c542312013-02-24 01:53:50 -0800409
Tyler Chatow6738c362019-02-16 14:12:30 -0800410 def _DumpMatrix(self, matrix_name, matrix, scalar_type):
411 """Dumps the provided matrix into a variable called matrix_name.
Austin Schuh3c542312013-02-24 01:53:50 -0800412
Tyler Chatow6738c362019-02-16 14:12:30 -0800413 Args:
414 matrix_name: string, The variable name to save the matrix to.
415 matrix: The matrix to dump.
416 scalar_type: The C++ type to use for the scalar in the matrix.
Austin Schuh3c542312013-02-24 01:53:50 -0800417
Tyler Chatow6738c362019-02-16 14:12:30 -0800418 Returns:
419 string, The C++ commands required to populate a variable named matrix_name
420 with the contents of matrix.
421 """
422 ans = [
Ravago Jones26f7ad02021-02-05 15:45:59 -0800423 ' Eigen::Matrix<%s, %d, %d> %s;\n' %
424 (scalar_type, matrix.shape[0], matrix.shape[1], matrix_name)
Tyler Chatow6738c362019-02-16 14:12:30 -0800425 ]
Austin Schuh5ea48472021-02-02 20:46:41 -0800426 for x in range(matrix.shape[0]):
427 for y in range(matrix.shape[1]):
Tyler Chatow6738c362019-02-16 14:12:30 -0800428 write_type = repr(matrix[x, y])
429 if scalar_type == 'float':
Austin Schuh085eab92020-11-26 13:54:51 -0800430 if '.' not in write_type and 'e' not in write_type:
Tyler Chatow6738c362019-02-16 14:12:30 -0800431 write_type += '.0'
432 write_type += 'f'
Ravago Jones5127ccc2022-07-31 16:32:45 -0700433 ans.append(' %s(%d, %d) = %s;\n' %
434 (matrix_name, x, y, write_type))
Austin Schuh3c542312013-02-24 01:53:50 -0800435
Tyler Chatow6738c362019-02-16 14:12:30 -0800436 return ''.join(ans)
Austin Schuh3c542312013-02-24 01:53:50 -0800437
Tyler Chatow6738c362019-02-16 14:12:30 -0800438 def DumpPlantHeader(self, plant_coefficient_type):
439 """Writes out a c++ header declaration which will create a Plant object.
Austin Schuh3c542312013-02-24 01:53:50 -0800440
Tyler Chatow6738c362019-02-16 14:12:30 -0800441 Returns:
442 string, The header declaration for the function.
443 """
444 return '%s Make%sPlantCoefficients();\n' % (plant_coefficient_type,
445 self._name)
Austin Schuh3c542312013-02-24 01:53:50 -0800446
James Kuszmauleeb98e92024-01-14 22:15:32 -0800447 def DumpPlantJson(self, plant_coefficient_type):
448 result = {
449 "c": MatrixToJson(self.C),
450 "d": MatrixToJson(self.D),
451 "u_min": MatrixToJson(self.U_min),
452 "u_max": MatrixToJson(self.U_max),
453 "u_limit_coefficient": MatrixToJson(self.U_limit_coefficient),
454 "u_limit_constant": MatrixToJson(self.U_limit_constant),
James Kuszmaul9ea3ff92024-06-14 15:02:15 -0700455 "delayed_u": self.delayed_u,
456 "wrap_point": MatrixToJson(self.wrap_point)
James Kuszmauleeb98e92024-01-14 22:15:32 -0800457 }
458 if plant_coefficient_type.startswith('StateFeedbackPlant'):
459 result["a"] = MatrixToJson(self.A)
460 result["b"] = MatrixToJson(self.B)
461 result["dt"] = int(self.dt * 1e9)
462 elif plant_coefficient_type.startswith('StateFeedbackHybridPlant'):
463 result["a_continuous"] = MatrixToJson(self.A_continuous)
464 result["b_continuous"] = MatrixToJson(self.B_continuous)
465 else:
466 glog.fatal('Unsupported plant type %s', plant_coefficient_type)
467 return result
468
Tyler Chatow6738c362019-02-16 14:12:30 -0800469 def DumpPlant(self, plant_coefficient_type, scalar_type):
470 """Writes out a c++ function which will create a PlantCoefficients object.
Austin Schuh3c542312013-02-24 01:53:50 -0800471
Tyler Chatow6738c362019-02-16 14:12:30 -0800472 Returns:
473 string, The function which will create the object.
474 """
475 ans = [
Ravago Jones5127ccc2022-07-31 16:32:45 -0700476 '%s Make%sPlantCoefficients() {\n' %
477 (plant_coefficient_type, self._name)
Tyler Chatow6738c362019-02-16 14:12:30 -0800478 ]
Austin Schuh3c542312013-02-24 01:53:50 -0800479
Ravago Jonesc471ebe2023-07-05 20:37:00 -0700480 num_states = self.A.shape[0]
481 num_inputs = self.B.shape[1]
482 num_outputs = self.C.shape[0]
483
Tyler Chatow6738c362019-02-16 14:12:30 -0800484 ans.append(self._DumpMatrix('C', self.C, scalar_type))
485 ans.append(self._DumpMatrix('D', self.D, scalar_type))
486 ans.append(self._DumpMatrix('U_max', self.U_max, scalar_type))
487 ans.append(self._DumpMatrix('U_min', self.U_min, scalar_type))
Austin Schuh3c542312013-02-24 01:53:50 -0800488
Ravago Jonesc471ebe2023-07-05 20:37:00 -0700489 if not hasattr(self, 'U_limit_coefficient'):
490 self.U_limit_coefficient = numpy.matrix(
491 numpy.zeros((num_inputs, num_states)))
492
493 if not hasattr(self, 'U_limit_constant'):
494 self.U_limit_constant = self.U_max
495
496 ans.append(
497 self._DumpMatrix('U_limit_coefficient', self.U_limit_coefficient,
498 scalar_type))
499 ans.append(
500 self._DumpMatrix('U_limit_constant', self.U_limit_constant,
501 scalar_type))
502
Austin Schuhb39f4522022-03-27 13:29:42 -0700503 delayed_u_string = str(self.delayed_u)
Tyler Chatow6738c362019-02-16 14:12:30 -0800504 if plant_coefficient_type.startswith('StateFeedbackPlant'):
505 ans.append(self._DumpMatrix('A', self.A, scalar_type))
506 ans.append(self._DumpMatrix('B', self.B, scalar_type))
James Kuszmaul9ea3ff92024-06-14 15:02:15 -0700507 ans.append(
508 self._DumpMatrix('wrap_point', self.wrap_point, scalar_type))
Ravago Jones5127ccc2022-07-31 16:32:45 -0700509 ans.append(' const std::chrono::nanoseconds dt(%d);\n' %
510 (self.dt * 1e9))
Ravago Jonesc471ebe2023-07-05 20:37:00 -0700511 ans.append(
512 ' return %s'
James Kuszmaul9ea3ff92024-06-14 15:02:15 -0700513 '(A, B, C, D, U_max, U_min, U_limit_coefficient, U_limit_constant, dt, %s, wrap_point);\n'
Ravago Jonesc471ebe2023-07-05 20:37:00 -0700514 % (plant_coefficient_type, delayed_u_string))
Tyler Chatow6738c362019-02-16 14:12:30 -0800515 elif plant_coefficient_type.startswith('StateFeedbackHybridPlant'):
516 ans.append(
517 self._DumpMatrix('A_continuous', self.A_continuous,
518 scalar_type))
519 ans.append(
520 self._DumpMatrix('B_continuous', self.B_continuous,
521 scalar_type))
Ravago Jones5127ccc2022-07-31 16:32:45 -0700522 ans.append(
523 ' return %s'
Ravago Jonesc471ebe2023-07-05 20:37:00 -0700524 '(A_continuous, B_continuous, C, D, U_max, U_min, U_limit_coefficient, U_limit_constant, %s);\n'
525 % (plant_coefficient_type, delayed_u_string))
Tyler Chatow6738c362019-02-16 14:12:30 -0800526 else:
527 glog.fatal('Unsupported plant type %s', plant_coefficient_type)
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800528
Tyler Chatow6738c362019-02-16 14:12:30 -0800529 ans.append('}\n')
530 return ''.join(ans)
Austin Schuh3c542312013-02-24 01:53:50 -0800531
Tyler Chatow6738c362019-02-16 14:12:30 -0800532 def PlantFunction(self):
533 """Returns the name of the plant coefficient function."""
534 return 'Make%sPlantCoefficients()' % self._name
Austin Schuh3c542312013-02-24 01:53:50 -0800535
Tyler Chatow6738c362019-02-16 14:12:30 -0800536 def ControllerFunction(self):
537 """Returns the name of the controller function."""
538 return 'Make%sControllerCoefficients()' % self._name
Austin Schuh32501832017-02-25 18:32:56 -0800539
Tyler Chatow6738c362019-02-16 14:12:30 -0800540 def ObserverFunction(self):
541 """Returns the name of the controller function."""
542 return 'Make%sObserverCoefficients()' % self._name
Austin Schuhe3490622013-03-13 01:24:30 -0700543
Tyler Chatow6738c362019-02-16 14:12:30 -0800544 def DumpControllerHeader(self, scalar_type):
545 """Writes out a c++ header declaration which will create a Controller object.
Austin Schuh3c542312013-02-24 01:53:50 -0800546
Tyler Chatow6738c362019-02-16 14:12:30 -0800547 Returns:
548 string, The header declaration for the function.
549 """
550 num_states = self.A.shape[0]
551 num_inputs = self.B.shape[1]
552 num_outputs = self.C.shape[0]
553 return 'StateFeedbackControllerCoefficients<%d, %d, %d, %s> %s;\n' % (
554 num_states, num_inputs, num_outputs, scalar_type,
555 self.ControllerFunction())
Austin Schuh3c542312013-02-24 01:53:50 -0800556
James Kuszmauleeb98e92024-01-14 22:15:32 -0800557 def DumpControllerJson(self):
558 result = {"k": MatrixToJson(self.K), "kff": MatrixToJson(self.Kff)}
559 return result
560
Tyler Chatow6738c362019-02-16 14:12:30 -0800561 def DumpController(self, scalar_type):
562 """Returns a c++ function which will create a Controller object.
Austin Schuh3c542312013-02-24 01:53:50 -0800563
Tyler Chatow6738c362019-02-16 14:12:30 -0800564 Returns:
565 string, The function which will create the object.
566 """
567 num_states = self.A.shape[0]
568 num_inputs = self.B.shape[1]
569 num_outputs = self.C.shape[0]
570 ans = [
571 'StateFeedbackControllerCoefficients<%d, %d, %d, %s> %s {\n' %
572 (num_states, num_inputs, num_outputs, scalar_type,
573 self.ControllerFunction())
574 ]
Austin Schuh3c542312013-02-24 01:53:50 -0800575
Tyler Chatow6738c362019-02-16 14:12:30 -0800576 ans.append(self._DumpMatrix('K', self.K, scalar_type))
577 if not hasattr(self, 'Kff'):
578 self.Kff = numpy.matrix(numpy.zeros(self.K.shape))
Austin Schuh86093ad2016-02-06 14:29:34 -0800579
Tyler Chatow6738c362019-02-16 14:12:30 -0800580 ans.append(self._DumpMatrix('Kff', self.Kff, scalar_type))
Austin Schuh3c542312013-02-24 01:53:50 -0800581
Tyler Chatow6738c362019-02-16 14:12:30 -0800582 ans.append(
583 ' return StateFeedbackControllerCoefficients<%d, %d, %d, %s>'
584 '(K, Kff);\n' % (num_states, num_inputs, num_outputs, scalar_type))
585 ans.append('}\n')
586 return ''.join(ans)
Austin Schuh32501832017-02-25 18:32:56 -0800587
Tyler Chatow6738c362019-02-16 14:12:30 -0800588 def DumpObserverHeader(self, observer_coefficient_type):
589 """Writes out a c++ header declaration which will create a Observer object.
Austin Schuh32501832017-02-25 18:32:56 -0800590
Tyler Chatow6738c362019-02-16 14:12:30 -0800591 Returns:
592 string, The header declaration for the function.
593 """
Ravago Jones26f7ad02021-02-05 15:45:59 -0800594 return '%s %s;\n' % (observer_coefficient_type,
595 self.ObserverFunction())
Austin Schuh32501832017-02-25 18:32:56 -0800596
James Kuszmauleeb98e92024-01-14 22:15:32 -0800597 def GetObserverCoefficients(self):
598 if hasattr(self, 'KalmanGain'):
599 KalmanGain = self.KalmanGain
600 Q = self.Q
601 R = self.R
602 else:
603 KalmanGain = numpy.linalg.inv(self.A) * self.L
604 Q = numpy.zeros(self.A.shape)
605 R = numpy.zeros((self.C.shape[0], self.C.shape[0]))
606 return (KalmanGain, Q, R)
607
608 def DumbObserverJson(self, observer_coefficient_type):
609 result = {"delayed_u": self.delayed_u}
610 if observer_coefficient_type.startswith('StateFeedbackObserver'):
611 KalmanGain, Q, R = self.GetObserverCoefficients()
612 result["kalman_gain"] = MatrixToJson(KalmanGain)
613 result["q"] = MatrixToJson(Q)
614 result["r"] = MatrixToJson(R)
615 elif observer_coefficient_type.startswith('HybridKalman'):
616 result["q_continuous"] = MatrixToJson(self.Q_continuous)
617 result["r_continuous"] = MatrixToJson(self.R_continuous)
618 result["p_steady_state"] = MatrixToJson(self.P_steady_state)
619 else:
620 glog.fatal('Unsupported plant type %s', observer_coefficient_type)
621 return result
622
Tyler Chatow6738c362019-02-16 14:12:30 -0800623 def DumpObserver(self, observer_coefficient_type, scalar_type):
624 """Returns a c++ function which will create a Observer object.
Austin Schuh32501832017-02-25 18:32:56 -0800625
Tyler Chatow6738c362019-02-16 14:12:30 -0800626 Returns:
627 string, The function which will create the object.
628 """
629 ans = [
630 '%s %s {\n' % (observer_coefficient_type, self.ObserverFunction())
631 ]
Austin Schuh32501832017-02-25 18:32:56 -0800632
Austin Schuhb39f4522022-03-27 13:29:42 -0700633 delayed_u_string = str(self.delayed_u)
Tyler Chatow6738c362019-02-16 14:12:30 -0800634 if observer_coefficient_type.startswith('StateFeedbackObserver'):
James Kuszmauleeb98e92024-01-14 22:15:32 -0800635 KalmanGain, Q, R = self.GetObserverCoefficients()
Tyler Chatow6738c362019-02-16 14:12:30 -0800636 ans.append(self._DumpMatrix('KalmanGain', KalmanGain, scalar_type))
637 ans.append(self._DumpMatrix('Q', Q, scalar_type))
638 ans.append(self._DumpMatrix('R', R, scalar_type))
Austin Schuh64433f12022-02-21 19:40:38 -0800639 ans.append(' return %s(KalmanGain, Q, R, %s);\n' %
640 (observer_coefficient_type, delayed_u_string))
Sabina Davis3922dfa2018-02-10 23:10:05 -0800641
Tyler Chatow6738c362019-02-16 14:12:30 -0800642 elif observer_coefficient_type.startswith('HybridKalman'):
643 ans.append(
644 self._DumpMatrix('Q_continuous', self.Q_continuous,
645 scalar_type))
646 ans.append(
647 self._DumpMatrix('R_continuous', self.R_continuous,
648 scalar_type))
649 ans.append(
650 self._DumpMatrix('P_steady_state', self.P_steady_state,
651 scalar_type))
652 ans.append(
Ravago Jones5127ccc2022-07-31 16:32:45 -0700653 ' return %s(Q_continuous, R_continuous, P_steady_state, %s);\n'
654 % (observer_coefficient_type, delayed_u_string))
Tyler Chatow6738c362019-02-16 14:12:30 -0800655 else:
656 glog.fatal('Unsupported observer type %s',
657 observer_coefficient_type)
Austin Schuh32501832017-02-25 18:32:56 -0800658
Tyler Chatow6738c362019-02-16 14:12:30 -0800659 ans.append('}\n')
660 return ''.join(ans)
661
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800662
663class HybridControlLoop(ControlLoop):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700664
Tyler Chatow6738c362019-02-16 14:12:30 -0800665 def __init__(self, name):
666 super(HybridControlLoop, self).__init__(name=name)
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800667
Tyler Chatow6738c362019-02-16 14:12:30 -0800668 def Discretize(self, dt):
669 [self.A, self.B, self.Q, self.R] = \
670 controls.kalmd(self.A_continuous, self.B_continuous,
671 self.Q_continuous, self.R_continuous, dt)
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800672
Tyler Chatow6738c362019-02-16 14:12:30 -0800673 def PredictHybridObserver(self, U, dt):
674 self.Discretize(dt)
Austin Schuhb39f4522022-03-27 13:29:42 -0700675 if self.delayed_u > 0:
676 self.X_hat = self.A * self.X_hat + self.B * self.last_U[:, -1]
677 self.last_U[:, 1:] = self.last_U[:, 0:-1]
678 self.last_U[:, 0] = U.copy()
Austin Schuh64433f12022-02-21 19:40:38 -0800679 else:
680 self.X_hat = self.A * self.X_hat + self.B * U
681
Tyler Chatow6738c362019-02-16 14:12:30 -0800682 self.P = (self.A * self.P * self.A.T + self.Q)
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800683
Tyler Chatow6738c362019-02-16 14:12:30 -0800684 def CorrectHybridObserver(self, U):
685 Y_bar = self.Y - self.C * self.X_hat
686 C_t = self.C.T
687 S = self.C * self.P * C_t + self.R
688 self.KalmanGain = self.P * C_t * numpy.linalg.inv(S)
689 self.X_hat = self.X_hat + self.KalmanGain * Y_bar
690 self.P = (numpy.eye(len(self.A)) - self.KalmanGain * self.C) * self.P
691
692 def InitializeState(self):
693 super(HybridControlLoop, self).InitializeState()
694 if hasattr(self, 'Q_steady_state'):
695 self.P = self.Q_steady_state
696 else:
697 self.P = numpy.matrix(
698 numpy.zeros((self.A.shape[0], self.A.shape[0])))
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800699
700
701class CIM(object):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700702
Tyler Chatow6738c362019-02-16 14:12:30 -0800703 def __init__(self):
704 # Stall Torque in N m
705 self.stall_torque = 2.42
706 # Stall Current in Amps
707 self.stall_current = 133.0
708 # Free Speed in rad/s
709 self.free_speed = 5500.0 / 60.0 * 2.0 * numpy.pi
710 # Free Current in Amps
711 self.free_current = 4.7
712 # Resistance of the motor
713 self.resistance = 12.0 / self.stall_current
714 # Motor velocity constant
Ravago Jones5127ccc2022-07-31 16:32:45 -0700715 self.Kv = (self.free_speed /
716 (12.0 - self.resistance * self.free_current))
Tyler Chatow6738c362019-02-16 14:12:30 -0800717 # Torque constant
718 self.Kt = self.stall_torque / self.stall_current
Lee Mracek97fc8af2018-01-13 04:38:52 -0500719
720
721class MiniCIM(object):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700722
Tyler Chatow6738c362019-02-16 14:12:30 -0800723 def __init__(self):
724 # Stall Torque in N m
725 self.stall_torque = 1.41
726 # Stall Current in Amps
727 self.stall_current = 89.0
728 # Free Speed in rad/s
729 self.free_speed = 5840.0 / 60.0 * 2.0 * numpy.pi
730 # Free Current in Amps
731 self.free_current = 3.0
732 # Resistance of the motor
733 self.resistance = 12.0 / self.stall_current
734 # Motor velocity constant
Ravago Jones5127ccc2022-07-31 16:32:45 -0700735 self.Kv = (self.free_speed /
736 (12.0 - self.resistance * self.free_current))
Tyler Chatow6738c362019-02-16 14:12:30 -0800737 # Torque constant
738 self.Kt = self.stall_torque / self.stall_current
Austin Schuhf173eb82018-01-20 23:32:30 -0800739
milind-uf70e8e12021-10-02 12:36:00 -0700740 # Motor inertia in kg m^2
741 self.motor_inertia = 0.0001634
742
Austin Schuhf173eb82018-01-20 23:32:30 -0800743
Austin Schuhb5d302f2019-01-20 20:51:19 -0800744class NMotor(object):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700745
Austin Schuhb5d302f2019-01-20 20:51:19 -0800746 def __init__(self, motor, n):
747 """Gangs together n motors."""
748 self.motor = motor
749 self.stall_torque = motor.stall_torque * n
750 self.stall_current = motor.stall_current * n
751 self.free_speed = motor.free_speed
752
753 self.free_current = motor.free_current * n
754 self.resistance = motor.resistance / n
755 self.Kv = motor.Kv
756 self.Kt = motor.Kt
Austin Schuh36bb8e32019-02-18 15:02:57 -0800757 self.motor_inertia = motor.motor_inertia * n
Austin Schuhb5d302f2019-01-20 20:51:19 -0800758
759
760class Vex775Pro(object):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700761
Austin Schuhb5d302f2019-01-20 20:51:19 -0800762 def __init__(self):
763 # Stall Torque in N m
764 self.stall_torque = 0.71
765 # Stall Current in Amps
766 self.stall_current = 134.0
767 # Free Speed in rad/s
768 self.free_speed = 18730.0 / 60.0 * 2.0 * numpy.pi
769 # Free Current in Amps
770 self.free_current = 0.7
771 # Resistance of the motor
772 self.resistance = 12.0 / self.stall_current
773 # Motor velocity constant
Ravago Jones5127ccc2022-07-31 16:32:45 -0700774 self.Kv = (self.free_speed /
775 (12.0 - self.resistance * self.free_current))
Austin Schuhb5d302f2019-01-20 20:51:19 -0800776 # Torque constant
777 self.Kt = self.stall_torque / self.stall_current
778 # Motor inertia in kg m^2
779 self.motor_inertia = 0.00001187
780
781
Austin Schuhf173eb82018-01-20 23:32:30 -0800782class BAG(object):
Tyler Chatow6738c362019-02-16 14:12:30 -0800783 # BAG motor specs available at http://motors.vex.com/vexpro-motors/bag-motor
784 def __init__(self):
785 # Stall Torque in (N m)
786 self.stall_torque = 0.43
787 # Stall Current in (Amps)
788 self.stall_current = 53.0
789 # Free Speed in (rad/s)
790 self.free_speed = 13180.0 / 60.0 * 2.0 * numpy.pi
791 # Free Current in (Amps)
792 self.free_current = 1.8
793 # Resistance of the motor (Ohms)
794 self.resistance = 12.0 / self.stall_current
795 # Motor velocity constant (radians / (sec * volt))
Ravago Jones5127ccc2022-07-31 16:32:45 -0700796 self.Kv = (self.free_speed /
797 (12.0 - self.resistance * self.free_current))
Tyler Chatow6738c362019-02-16 14:12:30 -0800798 # Torque constant (N * m / A)
799 self.Kt = self.stall_torque / self.stall_current
800 # Motor inertia in kg m^2
801 self.motor_inertia = 0.000006
802
Brian Silverman6260c092018-01-14 15:21:36 -0800803
804class MN3510(object):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700805
Tyler Chatow6738c362019-02-16 14:12:30 -0800806 def __init__(self):
807 # http://www.robotshop.com/en/t-motor-navigator-mn3510-360kv-brushless-motor.html#Specifications
808 # Free Current in Amps
809 self.free_current = 0.0
810 # Resistance of the motor
811 self.resistance = 0.188
812 # Stall Current in Amps
813 self.stall_current = 14.0 / self.resistance
814 # Motor velocity constant
815 self.Kv = 360.0 / 60.0 * (2.0 * numpy.pi)
816 # Torque constant Nm / A
817 self.Kt = 1.0 / self.Kv
818 # Stall Torque in N m
819 self.stall_torque = self.Kt * self.stall_current
James Kuszmaulef0c18a2020-01-12 15:44:20 -0800820
821
822class Falcon(object):
823 """Class representing the VexPro Falcon 500 motor.
824
825 All numbers based on data from
826 https://www.vexrobotics.com/vexpro/falcon-500."""
827
828 def __init__(self):
829 # Stall Torque in N m
830 self.stall_torque = 4.69
831 # Stall Current in Amps
832 self.stall_current = 257.0
833 # Free Speed in rad / sec
834 self.free_speed = 6380.0 / 60.0 * 2.0 * numpy.pi
835 # Free Current in Amps
836 self.free_current = 1.5
837 # Resistance of the motor, divided by 2 to account for the 2 motors
838 self.resistance = 12.0 / self.stall_current
839 # Motor velocity constant
Ravago Jones5127ccc2022-07-31 16:32:45 -0700840 self.Kv = (self.free_speed /
841 (12.0 - self.resistance * self.free_current))
James Kuszmaulef0c18a2020-01-12 15:44:20 -0800842 # Torque constant
843 self.Kt = self.stall_torque / self.stall_current
Austin Schuhc1c957a2020-02-20 17:47:58 -0800844 # Motor inertia in kg m^2
845 # Diameter of 1.9", weight of: 100 grams
846 # TODO(austin): Get a number from Scott Westbrook for the mass
Ravago Jones26f7ad02021-02-05 15:45:59 -0800847 self.motor_inertia = 0.1 * ((0.95 * 0.0254)**2.0)
Filip Kujawa7e835182024-01-13 16:22:09 -0800848
849
850class KrakenFOC(object):
851 """Class representing the WCP Kraken X60 motor using
852 Field Oriented Controls (FOC) communication.
853
854 All numbers based on data from
855 https://wcproducts.com/products/kraken.
856 """
857
858 def __init__(self):
859 # Stall Torque in N m
860 self.stall_torque = 9.37
861 # Stall Current in Amps
862 self.stall_current = 483.0
863 # Free Speed in rad / sec
864 self.free_speed = 5800.0 / 60.0 * 2.0 * numpy.pi
865 # Free Current in Amps
866 self.free_current = 2.0
867 # Resistance of the motor, divided by 2 to account for the 2 motors
868 self.resistance = 12.0 / self.stall_current
869 # Motor velocity constant
870 self.Kv = (self.free_speed /
871 (12.0 - self.resistance * self.free_current))
872 # Torque constant
873 self.Kt = self.stall_torque / self.stall_current
874 # Motor inertia in kg m^2
875 # Diameter of 1.9", weight of: 100 grams
876 # TODO(Filip): Update motor inertia for Kraken, currently using Falcon motor inertia
877 self.motor_inertia = 0.1 * ((0.95 * 0.0254)**2.0)