blob: 864a4e4c5b52c1ad2734ed54cdfc08d0eb70a5f4 [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
Austin Schuhe8ca06a2020-03-07 22:27:39 -08009 def __init__(self, name, formatt, value, comment=None):
Tyler Chatow6738c362019-02-16 14:12:30 -080010 self.name = name
11 self.formatt = formatt
12 self.value = value
13 self.formatToType = {}
14 self.formatToType['%f'] = "double"
15 self.formatToType['%d'] = "int"
Austin Schuhe8ca06a2020-03-07 22:27:39 -080016 if comment is None:
17 self.comment = ""
18 else:
19 self.comment = comment + "\n"
Tyler Chatow6738c362019-02-16 14:12:30 -080020
21 def Render(self, loop_type):
22 typestring = self.formatToType[self.formatt]
23 if loop_type == 'float' and typestring == 'double':
24 typestring = loop_type
Austin Schuhe8ca06a2020-03-07 22:27:39 -080025 return str("\n%sstatic constexpr %s %s = "+ self.formatt +";\n") % \
26 (self.comment, typestring, self.name, self.value)
Ben Fredrickson1b45f782014-02-23 07:44:36 +000027
28
James Kuszmauleeb98e92024-01-14 22:15:32 -080029def MatrixToJson(matrix):
30 """Returns JSON representation of a numpy matrix."""
31 return {
32 "rows": matrix.shape[0],
33 "cols": matrix.shape[1],
34 "storage_order": "ColMajor",
35 "data": numpy.array(matrix).flatten(order='F').tolist()
36 }
37
38
Austin Schuhe3490622013-03-13 01:24:30 -070039class ControlLoopWriter(object):
Ravago Jones5127ccc2022-07-31 16:32:45 -070040
Tyler Chatow6738c362019-02-16 14:12:30 -080041 def __init__(self,
42 gain_schedule_name,
43 loops,
44 namespaces=None,
45 write_constants=False,
46 plant_type='StateFeedbackPlant',
47 observer_type='StateFeedbackObserver',
48 scalar_type='double'):
49 """Constructs a control loop writer.
Austin Schuhe3490622013-03-13 01:24:30 -070050
Tyler Chatow6738c362019-02-16 14:12:30 -080051 Args:
52 gain_schedule_name: string, Name of the overall controller.
53 loops: array[ControlLoop], a list of control loops to gain schedule
54 in order.
55 namespaces: array[string], a list of names of namespaces to nest in
56 order. If None, the default will be used.
57 plant_type: string, The C++ type of the plant.
58 observer_type: string, The C++ type of the observer.
59 scalar_type: string, The C++ type of the base scalar.
60 """
61 self._gain_schedule_name = gain_schedule_name
62 self._loops = loops
63 if namespaces:
64 self._namespaces = namespaces
65 else:
66 self._namespaces = ['frc971', 'control_loops']
Austin Schuhe3490622013-03-13 01:24:30 -070067
Tyler Chatow6738c362019-02-16 14:12:30 -080068 self._namespace_start = '\n'.join(
69 ['namespace %s {' % name for name in self._namespaces])
Austin Schuh86093ad2016-02-06 14:29:34 -080070
Tyler Chatow6738c362019-02-16 14:12:30 -080071 self._namespace_end = '\n'.join([
72 '} // namespace %s' % name for name in reversed(self._namespaces)
73 ])
Austin Schuh25933852014-02-23 02:04:13 -080074
Tyler Chatow6738c362019-02-16 14:12:30 -080075 self._constant_list = []
76 self._plant_type = plant_type
77 self._observer_type = observer_type
78 self._scalar_type = scalar_type
Austin Schuh25933852014-02-23 02:04:13 -080079
Tyler Chatow6738c362019-02-16 14:12:30 -080080 def AddConstant(self, constant):
81 """Adds a constant to write.
Austin Schuhe3490622013-03-13 01:24:30 -070082
Tyler Chatow6738c362019-02-16 14:12:30 -080083 Args:
84 constant: Constant, the constant to add to the header.
85 """
86 self._constant_list.append(constant)
Brian Silvermane51ad632014-01-08 15:12:29 -080087
Tyler Chatow6738c362019-02-16 14:12:30 -080088 def _TopDirectory(self):
89 return self._namespaces[0]
Austin Schuhe3490622013-03-13 01:24:30 -070090
Tyler Chatow6738c362019-02-16 14:12:30 -080091 def _HeaderGuard(self, header_file):
Ravago Jones5127ccc2022-07-31 16:32:45 -070092 return ('_'.join([namespace.upper()
93 for namespace in self._namespaces]) + '_' +
94 os.path.basename(header_file).upper().replace(
Tyler Chatow6738c362019-02-16 14:12:30 -080095 '.', '_').replace('/', '_') + '_')
Austin Schuhe3490622013-03-13 01:24:30 -070096
James Kuszmauleeb98e92024-01-14 22:15:32 -080097 def Write(self, header_file, cc_file, json_file=None):
Tyler Chatow6738c362019-02-16 14:12:30 -080098 """Writes the loops to the specified files."""
99 self.WriteHeader(header_file)
100 self.WriteCC(os.path.basename(header_file), cc_file)
James Kuszmauleeb98e92024-01-14 22:15:32 -0800101 if json_file is not None:
102 self.WriteJson(json_file)
Austin Schuhe3490622013-03-13 01:24:30 -0700103
Tyler Chatow6738c362019-02-16 14:12:30 -0800104 def _GenericType(self, typename, extra_args=None):
105 """Returns a loop template using typename for the type."""
106 num_states = self._loops[0].A.shape[0]
107 num_inputs = self._loops[0].B.shape[1]
108 num_outputs = self._loops[0].C.shape[0]
109 if extra_args is not None:
110 extra_args = ', ' + extra_args
111 else:
112 extra_args = ''
113 if self._scalar_type != 'double':
114 extra_args += ', ' + self._scalar_type
115 return '%s<%d, %d, %d%s>' % (typename, num_states, num_inputs,
116 num_outputs, extra_args)
Austin Schuh32501832017-02-25 18:32:56 -0800117
Tyler Chatow6738c362019-02-16 14:12:30 -0800118 def _ControllerType(self):
119 """Returns a template name for StateFeedbackController."""
120 return self._GenericType('StateFeedbackController')
Austin Schuhe3490622013-03-13 01:24:30 -0700121
Tyler Chatow6738c362019-02-16 14:12:30 -0800122 def _ObserverType(self):
123 """Returns a template name for StateFeedbackObserver."""
124 return self._GenericType(self._observer_type)
Austin Schuh20388b62017-11-23 22:40:46 -0800125
Tyler Chatow6738c362019-02-16 14:12:30 -0800126 def _LoopType(self):
127 """Returns a template name for StateFeedbackLoop."""
128 num_states = self._loops[0].A.shape[0]
129 num_inputs = self._loops[0].B.shape[1]
130 num_outputs = self._loops[0].C.shape[0]
Austin Schuh20388b62017-11-23 22:40:46 -0800131
Tyler Chatow6738c362019-02-16 14:12:30 -0800132 return 'StateFeedbackLoop<%d, %d, %d, %s, %s, %s>' % (
133 num_states, num_inputs, num_outputs, self._scalar_type,
134 self._PlantType(), self._ObserverType())
Austin Schuhe3490622013-03-13 01:24:30 -0700135
Tyler Chatow6738c362019-02-16 14:12:30 -0800136 def _PlantType(self):
137 """Returns a template name for StateFeedbackPlant."""
138 return self._GenericType(self._plant_type)
Austin Schuhe3490622013-03-13 01:24:30 -0700139
Tyler Chatow6738c362019-02-16 14:12:30 -0800140 def _PlantCoeffType(self):
141 """Returns a template name for StateFeedbackPlantCoefficients."""
142 return self._GenericType(self._plant_type + 'Coefficients')
Austin Schuhe3490622013-03-13 01:24:30 -0700143
Tyler Chatow6738c362019-02-16 14:12:30 -0800144 def _ControllerCoeffType(self):
145 """Returns a template name for StateFeedbackControllerCoefficients."""
146 return self._GenericType('StateFeedbackControllerCoefficients')
Austin Schuh32501832017-02-25 18:32:56 -0800147
Tyler Chatow6738c362019-02-16 14:12:30 -0800148 def _ObserverCoeffType(self):
149 """Returns a template name for StateFeedbackObserverCoefficients."""
150 return self._GenericType(self._observer_type + 'Coefficients')
Austin Schuh32501832017-02-25 18:32:56 -0800151
Tyler Chatow6738c362019-02-16 14:12:30 -0800152 def WriteHeader(self, header_file):
153 """Writes the header file to the file named header_file."""
154 with open(header_file, 'w') as fd:
155 header_guard = self._HeaderGuard(header_file)
156 fd.write('#ifndef %s\n'
157 '#define %s\n\n' % (header_guard, header_guard))
158 fd.write(
159 '#include \"frc971/control_loops/state_feedback_loop.h\"\n')
Ravago Jones26f7ad02021-02-05 15:45:59 -0800160 if (self._plant_type == 'StateFeedbackHybridPlant'
161 or self._observer_type == 'HybridKalman'):
Tyler Chatow6738c362019-02-16 14:12:30 -0800162 fd.write(
163 '#include \"frc971/control_loops/hybrid_state_feedback_loop.h\"\n'
164 )
Austin Schuh4cc4fe22017-11-23 19:13:09 -0800165
Tyler Chatow6738c362019-02-16 14:12:30 -0800166 fd.write('\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700167
Tyler Chatow6738c362019-02-16 14:12:30 -0800168 fd.write(self._namespace_start)
Ben Fredrickson1b45f782014-02-23 07:44:36 +0000169
Tyler Chatow6738c362019-02-16 14:12:30 -0800170 for const in self._constant_list:
171 fd.write(const.Render(self._scalar_type))
Ben Fredrickson1b45f782014-02-23 07:44:36 +0000172
Tyler Chatow6738c362019-02-16 14:12:30 -0800173 fd.write('\n\n')
174 for loop in self._loops:
175 fd.write(loop.DumpPlantHeader(self._PlantCoeffType()))
176 fd.write('\n')
177 fd.write(loop.DumpControllerHeader(self._scalar_type))
178 fd.write('\n')
179 fd.write(loop.DumpObserverHeader(self._ObserverCoeffType()))
180 fd.write('\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700181
Ravago Jones5127ccc2022-07-31 16:32:45 -0700182 fd.write('%s Make%sPlant();\n\n' %
183 (self._PlantType(), self._gain_schedule_name))
Austin Schuhe3490622013-03-13 01:24:30 -0700184
Ravago Jones5127ccc2022-07-31 16:32:45 -0700185 fd.write('%s Make%sController();\n\n' %
186 (self._ControllerType(), self._gain_schedule_name))
Austin Schuh32501832017-02-25 18:32:56 -0800187
Ravago Jones5127ccc2022-07-31 16:32:45 -0700188 fd.write('%s Make%sObserver();\n\n' %
189 (self._ObserverType(), self._gain_schedule_name))
Austin Schuh32501832017-02-25 18:32:56 -0800190
Ravago Jones5127ccc2022-07-31 16:32:45 -0700191 fd.write('%s Make%sLoop();\n\n' %
192 (self._LoopType(), self._gain_schedule_name))
Austin Schuhe3490622013-03-13 01:24:30 -0700193
Tyler Chatow6738c362019-02-16 14:12:30 -0800194 fd.write(self._namespace_end)
195 fd.write('\n\n')
196 fd.write("#endif // %s\n" % header_guard)
Austin Schuhe3490622013-03-13 01:24:30 -0700197
Tyler Chatow6738c362019-02-16 14:12:30 -0800198 def WriteCC(self, header_file_name, cc_file):
199 """Writes the cc file to the file named cc_file."""
200 with open(cc_file, 'w') as fd:
Ravago Jones5127ccc2022-07-31 16:32:45 -0700201 fd.write('#include \"%s/%s\"\n' %
202 (os.path.join(*self._namespaces), header_file_name))
Tyler Chatow6738c362019-02-16 14:12:30 -0800203 fd.write('\n')
James Kuszmaul03be1242020-02-21 14:52:04 -0800204 fd.write('#include <chrono>\n')
Tyler Chatow6738c362019-02-16 14:12:30 -0800205 fd.write('#include <vector>\n')
206 fd.write('\n')
207 fd.write(
208 '#include \"frc971/control_loops/state_feedback_loop.h\"\n')
209 fd.write('\n')
210 fd.write(self._namespace_start)
211 fd.write('\n\n')
212 for loop in self._loops:
213 fd.write(
214 loop.DumpPlant(self._PlantCoeffType(), self._scalar_type))
215 fd.write('\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700216
Tyler Chatow6738c362019-02-16 14:12:30 -0800217 for loop in self._loops:
218 fd.write(loop.DumpController(self._scalar_type))
219 fd.write('\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700220
Tyler Chatow6738c362019-02-16 14:12:30 -0800221 for loop in self._loops:
222 fd.write(
223 loop.DumpObserver(self._ObserverCoeffType(),
224 self._scalar_type))
225 fd.write('\n')
Austin Schuh32501832017-02-25 18:32:56 -0800226
Ravago Jones5127ccc2022-07-31 16:32:45 -0700227 fd.write('%s Make%sPlant() {\n' %
228 (self._PlantType(), self._gain_schedule_name))
Tyler Chatow6738c362019-02-16 14:12:30 -0800229 fd.write(' ::std::vector< ::std::unique_ptr<%s>> plants(%d);\n' %
230 (self._PlantCoeffType(), len(self._loops)))
231 for index, loop in enumerate(self._loops):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700232 fd.write(
233 ' plants[%d] = ::std::unique_ptr<%s>(new %s(%s));\n' %
234 (index, self._PlantCoeffType(), self._PlantCoeffType(),
235 loop.PlantFunction()))
Austin Schuhb02bf5b2021-07-31 21:28:21 -0700236 fd.write(' return %s(std::move(plants));\n' % self._PlantType())
Tyler Chatow6738c362019-02-16 14:12:30 -0800237 fd.write('}\n\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700238
Ravago Jones5127ccc2022-07-31 16:32:45 -0700239 fd.write('%s Make%sController() {\n' %
240 (self._ControllerType(), self._gain_schedule_name))
Tyler Chatow6738c362019-02-16 14:12:30 -0800241 fd.write(
242 ' ::std::vector< ::std::unique_ptr<%s>> controllers(%d);\n' %
243 (self._ControllerCoeffType(), len(self._loops)))
244 for index, loop in enumerate(self._loops):
245 fd.write(
Ravago Jones26f7ad02021-02-05 15:45:59 -0800246 ' controllers[%d] = ::std::unique_ptr<%s>(new %s(%s));\n'
247 % (index, self._ControllerCoeffType(),
248 self._ControllerCoeffType(), loop.ControllerFunction()))
Austin Schuhb02bf5b2021-07-31 21:28:21 -0700249 fd.write(' return %s(std::move(controllers));\n' %
250 self._ControllerType())
Tyler Chatow6738c362019-02-16 14:12:30 -0800251 fd.write('}\n\n')
Austin Schuh32501832017-02-25 18:32:56 -0800252
Ravago Jones5127ccc2022-07-31 16:32:45 -0700253 fd.write('%s Make%sObserver() {\n' %
254 (self._ObserverType(), self._gain_schedule_name))
255 fd.write(
256 ' ::std::vector< ::std::unique_ptr<%s>> observers(%d);\n' %
257 (self._ObserverCoeffType(), len(self._loops)))
Tyler Chatow6738c362019-02-16 14:12:30 -0800258 for index, loop in enumerate(self._loops):
259 fd.write(
Ravago Jones5127ccc2022-07-31 16:32:45 -0700260 ' observers[%d] = ::std::unique_ptr<%s>(new %s(%s));\n' %
261 (index, self._ObserverCoeffType(),
262 self._ObserverCoeffType(), loop.ObserverFunction()))
263 fd.write(' return %s(std::move(observers));\n' %
264 self._ObserverType())
Tyler Chatow6738c362019-02-16 14:12:30 -0800265 fd.write('}\n\n')
Austin Schuh32501832017-02-25 18:32:56 -0800266
Ravago Jones5127ccc2022-07-31 16:32:45 -0700267 fd.write('%s Make%sLoop() {\n' %
268 (self._LoopType(), self._gain_schedule_name))
Tyler Chatow6738c362019-02-16 14:12:30 -0800269 fd.write(
270 ' return %s(Make%sPlant(), Make%sController(), Make%sObserver());\n'
271 % (self._LoopType(), self._gain_schedule_name,
272 self._gain_schedule_name, self._gain_schedule_name))
273 fd.write('}\n\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700274
Tyler Chatow6738c362019-02-16 14:12:30 -0800275 fd.write(self._namespace_end)
276 fd.write('\n')
Austin Schuhe3490622013-03-13 01:24:30 -0700277
James Kuszmauleeb98e92024-01-14 22:15:32 -0800278 def WriteJson(self, json_file):
279 """Writes a JSON file of the loop constants to the specified json_file."""
280 loops = []
281 for loop in self._loops:
282 loop_json = {}
283 loop_json["plant"] = loop.DumpPlantJson(self._PlantCoeffType())
284 loop_json["controller"] = loop.DumpControllerJson()
285 loop_json["observer"] = loop.DumbObserverJson(
286 self._ObserverCoeffType())
287 loops.append(loop_json)
288 with open(json_file, 'w') as f:
289 f.write(json.dumps(loops))
290
Austin Schuhe3490622013-03-13 01:24:30 -0700291
Austin Schuh3c542312013-02-24 01:53:50 -0800292class ControlLoop(object):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700293
Tyler Chatow6738c362019-02-16 14:12:30 -0800294 def __init__(self, name):
295 """Constructs a control loop object.
Austin Schuh3c542312013-02-24 01:53:50 -0800296
Tyler Chatow6738c362019-02-16 14:12:30 -0800297 Args:
298 name: string, The name of the loop to use when writing the C++ files.
299 """
300 self._name = name
Austin Schuhb39f4522022-03-27 13:29:42 -0700301 self.delayed_u = 0
Austin Schuhb5d302f2019-01-20 20:51:19 -0800302
Tyler Chatow6738c362019-02-16 14:12:30 -0800303 @property
304 def name(self):
305 """Returns the name"""
306 return self._name
Austin Schuh3c542312013-02-24 01:53:50 -0800307
Tyler Chatow6738c362019-02-16 14:12:30 -0800308 def ContinuousToDiscrete(self, A_continuous, B_continuous, dt):
309 """Calculates the discrete time values for A and B.
Austin Schuhc1f68892013-03-16 17:06:27 -0700310
Tyler Chatow6738c362019-02-16 14:12:30 -0800311 Args:
312 A_continuous: numpy.matrix, The continuous time A matrix
313 B_continuous: numpy.matrix, The continuous time B matrix
314 dt: float, The time step of the control loop
Austin Schuhc1f68892013-03-16 17:06:27 -0700315
Tyler Chatow6738c362019-02-16 14:12:30 -0800316 Returns:
317 (A, B), numpy.matrix, the control matricies.
318 """
319 return controls.c2d(A_continuous, B_continuous, dt)
Austin Schuh3c542312013-02-24 01:53:50 -0800320
Tyler Chatow6738c362019-02-16 14:12:30 -0800321 def InitializeState(self):
322 """Sets X, Y, and X_hat to zero defaults."""
Austin Schuh43b9ae92020-02-29 23:08:38 -0800323 self.X = numpy.matrix(numpy.zeros((self.A.shape[0], 1)))
Tyler Chatow6738c362019-02-16 14:12:30 -0800324 self.Y = self.C * self.X
Austin Schuh43b9ae92020-02-29 23:08:38 -0800325 self.X_hat = numpy.matrix(numpy.zeros((self.A.shape[0], 1)))
Ravago Jones5127ccc2022-07-31 16:32:45 -0700326 self.last_U = numpy.matrix(
327 numpy.zeros((self.B.shape[1], max(1, self.delayed_u))))
Austin Schuh3c542312013-02-24 01:53:50 -0800328
Tyler Chatow6738c362019-02-16 14:12:30 -0800329 def PlaceControllerPoles(self, poles):
330 """Places the controller poles.
Austin Schuh3c542312013-02-24 01:53:50 -0800331
Tyler Chatow6738c362019-02-16 14:12:30 -0800332 Args:
333 poles: array, An array of poles. Must be complex conjegates if they have
334 any imaginary portions.
335 """
336 self.K = controls.dplace(self.A, self.B, poles)
Austin Schuh3c542312013-02-24 01:53:50 -0800337
Tyler Chatow6738c362019-02-16 14:12:30 -0800338 def PlaceObserverPoles(self, poles):
339 """Places the observer poles.
Austin Schuh3c542312013-02-24 01:53:50 -0800340
Tyler Chatow6738c362019-02-16 14:12:30 -0800341 Args:
342 poles: array, An array of poles. Must be complex conjegates if they have
343 any imaginary portions.
344 """
345 self.L = controls.dplace(self.A.T, self.C.T, poles).T
Sabina Davis3922dfa2018-02-10 23:10:05 -0800346
Tyler Chatow6738c362019-02-16 14:12:30 -0800347 def Update(self, U):
348 """Simulates one time step with the provided U."""
349 #U = numpy.clip(U, self.U_min, self.U_max)
Austin Schuhb39f4522022-03-27 13:29:42 -0700350 if self.delayed_u > 0:
351 self.X = self.A * self.X + self.B * self.last_U[:, -1]
352 self.Y = self.C * self.X + self.D * self.last_U[:, -1]
353 self.last_U[:, 1:] = self.last_U[:, 0:-1]
354 self.last_U[:, 0] = U.copy()
Austin Schuh64433f12022-02-21 19:40:38 -0800355 else:
356 self.X = self.A * self.X + self.B * U
357 self.Y = self.C * self.X + self.D * U
Austin Schuh3c542312013-02-24 01:53:50 -0800358
Tyler Chatow6738c362019-02-16 14:12:30 -0800359 def PredictObserver(self, U):
360 """Runs the predict step of the observer update."""
Austin Schuhb39f4522022-03-27 13:29:42 -0700361 if self.delayed_u > 0:
362 self.X_hat = (self.A * self.X_hat + self.B * self.last_U[:, -1])
363 self.last_U[:, 1:] = self.last_U[:, 0:-1]
364 self.last_U[:, 0] = U.copy()
Austin Schuh64433f12022-02-21 19:40:38 -0800365 else:
366 self.X_hat = (self.A * self.X_hat + self.B * U)
Austin Schuh1a387962015-01-31 16:36:20 -0800367
Tyler Chatow6738c362019-02-16 14:12:30 -0800368 def CorrectObserver(self, U):
369 """Runs the correct step of the observer update."""
370 if hasattr(self, 'KalmanGain'):
371 KalmanGain = self.KalmanGain
372 else:
373 KalmanGain = numpy.linalg.inv(self.A) * self.L
Austin Schuhb39f4522022-03-27 13:29:42 -0700374 if self.delayed_u > 0:
Austin Schuh64433f12022-02-21 19:40:38 -0800375 self.X_hat += KalmanGain * (self.Y - self.C * self.X_hat -
Austin Schuhb39f4522022-03-27 13:29:42 -0700376 self.D * self.last_U[:, -1])
Tyler Chatow6738c362019-02-16 14:12:30 -0800377 else:
Austin Schuh64433f12022-02-21 19:40:38 -0800378 self.X_hat += KalmanGain * (self.Y - self.C * self.X_hat -
379 self.D * U)
Austin Schuh3c542312013-02-24 01:53:50 -0800380
Tyler Chatow6738c362019-02-16 14:12:30 -0800381 def _DumpMatrix(self, matrix_name, matrix, scalar_type):
382 """Dumps the provided matrix into a variable called matrix_name.
Austin Schuh3c542312013-02-24 01:53:50 -0800383
Tyler Chatow6738c362019-02-16 14:12:30 -0800384 Args:
385 matrix_name: string, The variable name to save the matrix to.
386 matrix: The matrix to dump.
387 scalar_type: The C++ type to use for the scalar in the matrix.
Austin Schuh3c542312013-02-24 01:53:50 -0800388
Tyler Chatow6738c362019-02-16 14:12:30 -0800389 Returns:
390 string, The C++ commands required to populate a variable named matrix_name
391 with the contents of matrix.
392 """
393 ans = [
Ravago Jones26f7ad02021-02-05 15:45:59 -0800394 ' Eigen::Matrix<%s, %d, %d> %s;\n' %
395 (scalar_type, matrix.shape[0], matrix.shape[1], matrix_name)
Tyler Chatow6738c362019-02-16 14:12:30 -0800396 ]
Austin Schuh5ea48472021-02-02 20:46:41 -0800397 for x in range(matrix.shape[0]):
398 for y in range(matrix.shape[1]):
Tyler Chatow6738c362019-02-16 14:12:30 -0800399 write_type = repr(matrix[x, y])
400 if scalar_type == 'float':
Austin Schuh085eab92020-11-26 13:54:51 -0800401 if '.' not in write_type and 'e' not in write_type:
Tyler Chatow6738c362019-02-16 14:12:30 -0800402 write_type += '.0'
403 write_type += 'f'
Ravago Jones5127ccc2022-07-31 16:32:45 -0700404 ans.append(' %s(%d, %d) = %s;\n' %
405 (matrix_name, x, y, write_type))
Austin Schuh3c542312013-02-24 01:53:50 -0800406
Tyler Chatow6738c362019-02-16 14:12:30 -0800407 return ''.join(ans)
Austin Schuh3c542312013-02-24 01:53:50 -0800408
Tyler Chatow6738c362019-02-16 14:12:30 -0800409 def DumpPlantHeader(self, plant_coefficient_type):
410 """Writes out a c++ header declaration which will create a Plant object.
Austin Schuh3c542312013-02-24 01:53:50 -0800411
Tyler Chatow6738c362019-02-16 14:12:30 -0800412 Returns:
413 string, The header declaration for the function.
414 """
415 return '%s Make%sPlantCoefficients();\n' % (plant_coefficient_type,
416 self._name)
Austin Schuh3c542312013-02-24 01:53:50 -0800417
James Kuszmauleeb98e92024-01-14 22:15:32 -0800418 def DumpPlantJson(self, plant_coefficient_type):
419 result = {
420 "c": MatrixToJson(self.C),
421 "d": MatrixToJson(self.D),
422 "u_min": MatrixToJson(self.U_min),
423 "u_max": MatrixToJson(self.U_max),
424 "u_limit_coefficient": MatrixToJson(self.U_limit_coefficient),
425 "u_limit_constant": MatrixToJson(self.U_limit_constant),
426 "delayed_u": self.delayed_u
427 }
428 if plant_coefficient_type.startswith('StateFeedbackPlant'):
429 result["a"] = MatrixToJson(self.A)
430 result["b"] = MatrixToJson(self.B)
431 result["dt"] = int(self.dt * 1e9)
432 elif plant_coefficient_type.startswith('StateFeedbackHybridPlant'):
433 result["a_continuous"] = MatrixToJson(self.A_continuous)
434 result["b_continuous"] = MatrixToJson(self.B_continuous)
435 else:
436 glog.fatal('Unsupported plant type %s', plant_coefficient_type)
437 return result
438
Tyler Chatow6738c362019-02-16 14:12:30 -0800439 def DumpPlant(self, plant_coefficient_type, scalar_type):
440 """Writes out a c++ function which will create a PlantCoefficients object.
Austin Schuh3c542312013-02-24 01:53:50 -0800441
Tyler Chatow6738c362019-02-16 14:12:30 -0800442 Returns:
443 string, The function which will create the object.
444 """
445 ans = [
Ravago Jones5127ccc2022-07-31 16:32:45 -0700446 '%s Make%sPlantCoefficients() {\n' %
447 (plant_coefficient_type, self._name)
Tyler Chatow6738c362019-02-16 14:12:30 -0800448 ]
Austin Schuh3c542312013-02-24 01:53:50 -0800449
Ravago Jonesc471ebe2023-07-05 20:37:00 -0700450 num_states = self.A.shape[0]
451 num_inputs = self.B.shape[1]
452 num_outputs = self.C.shape[0]
453
Tyler Chatow6738c362019-02-16 14:12:30 -0800454 ans.append(self._DumpMatrix('C', self.C, scalar_type))
455 ans.append(self._DumpMatrix('D', self.D, scalar_type))
456 ans.append(self._DumpMatrix('U_max', self.U_max, scalar_type))
457 ans.append(self._DumpMatrix('U_min', self.U_min, scalar_type))
Austin Schuh3c542312013-02-24 01:53:50 -0800458
Ravago Jonesc471ebe2023-07-05 20:37:00 -0700459 if not hasattr(self, 'U_limit_coefficient'):
460 self.U_limit_coefficient = numpy.matrix(
461 numpy.zeros((num_inputs, num_states)))
462
463 if not hasattr(self, 'U_limit_constant'):
464 self.U_limit_constant = self.U_max
465
466 ans.append(
467 self._DumpMatrix('U_limit_coefficient', self.U_limit_coefficient,
468 scalar_type))
469 ans.append(
470 self._DumpMatrix('U_limit_constant', self.U_limit_constant,
471 scalar_type))
472
Austin Schuhb39f4522022-03-27 13:29:42 -0700473 delayed_u_string = str(self.delayed_u)
Tyler Chatow6738c362019-02-16 14:12:30 -0800474 if plant_coefficient_type.startswith('StateFeedbackPlant'):
475 ans.append(self._DumpMatrix('A', self.A, scalar_type))
476 ans.append(self._DumpMatrix('B', self.B, scalar_type))
Ravago Jones5127ccc2022-07-31 16:32:45 -0700477 ans.append(' const std::chrono::nanoseconds dt(%d);\n' %
478 (self.dt * 1e9))
Ravago Jonesc471ebe2023-07-05 20:37:00 -0700479 ans.append(
480 ' return %s'
481 '(A, B, C, D, U_max, U_min, U_limit_coefficient, U_limit_constant, dt, %s);\n'
482 % (plant_coefficient_type, delayed_u_string))
Tyler Chatow6738c362019-02-16 14:12:30 -0800483 elif plant_coefficient_type.startswith('StateFeedbackHybridPlant'):
484 ans.append(
485 self._DumpMatrix('A_continuous', self.A_continuous,
486 scalar_type))
487 ans.append(
488 self._DumpMatrix('B_continuous', self.B_continuous,
489 scalar_type))
Ravago Jones5127ccc2022-07-31 16:32:45 -0700490 ans.append(
491 ' return %s'
Ravago Jonesc471ebe2023-07-05 20:37:00 -0700492 '(A_continuous, B_continuous, C, D, U_max, U_min, U_limit_coefficient, U_limit_constant, %s);\n'
493 % (plant_coefficient_type, delayed_u_string))
Tyler Chatow6738c362019-02-16 14:12:30 -0800494 else:
495 glog.fatal('Unsupported plant type %s', plant_coefficient_type)
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800496
Tyler Chatow6738c362019-02-16 14:12:30 -0800497 ans.append('}\n')
498 return ''.join(ans)
Austin Schuh3c542312013-02-24 01:53:50 -0800499
Tyler Chatow6738c362019-02-16 14:12:30 -0800500 def PlantFunction(self):
501 """Returns the name of the plant coefficient function."""
502 return 'Make%sPlantCoefficients()' % self._name
Austin Schuh3c542312013-02-24 01:53:50 -0800503
Tyler Chatow6738c362019-02-16 14:12:30 -0800504 def ControllerFunction(self):
505 """Returns the name of the controller function."""
506 return 'Make%sControllerCoefficients()' % self._name
Austin Schuh32501832017-02-25 18:32:56 -0800507
Tyler Chatow6738c362019-02-16 14:12:30 -0800508 def ObserverFunction(self):
509 """Returns the name of the controller function."""
510 return 'Make%sObserverCoefficients()' % self._name
Austin Schuhe3490622013-03-13 01:24:30 -0700511
Tyler Chatow6738c362019-02-16 14:12:30 -0800512 def DumpControllerHeader(self, scalar_type):
513 """Writes out a c++ header declaration which will create a Controller object.
Austin Schuh3c542312013-02-24 01:53:50 -0800514
Tyler Chatow6738c362019-02-16 14:12:30 -0800515 Returns:
516 string, The header declaration for the function.
517 """
518 num_states = self.A.shape[0]
519 num_inputs = self.B.shape[1]
520 num_outputs = self.C.shape[0]
521 return 'StateFeedbackControllerCoefficients<%d, %d, %d, %s> %s;\n' % (
522 num_states, num_inputs, num_outputs, scalar_type,
523 self.ControllerFunction())
Austin Schuh3c542312013-02-24 01:53:50 -0800524
James Kuszmauleeb98e92024-01-14 22:15:32 -0800525 def DumpControllerJson(self):
526 result = {"k": MatrixToJson(self.K), "kff": MatrixToJson(self.Kff)}
527 return result
528
Tyler Chatow6738c362019-02-16 14:12:30 -0800529 def DumpController(self, scalar_type):
530 """Returns a c++ function which will create a Controller object.
Austin Schuh3c542312013-02-24 01:53:50 -0800531
Tyler Chatow6738c362019-02-16 14:12:30 -0800532 Returns:
533 string, The function which will create the object.
534 """
535 num_states = self.A.shape[0]
536 num_inputs = self.B.shape[1]
537 num_outputs = self.C.shape[0]
538 ans = [
539 'StateFeedbackControllerCoefficients<%d, %d, %d, %s> %s {\n' %
540 (num_states, num_inputs, num_outputs, scalar_type,
541 self.ControllerFunction())
542 ]
Austin Schuh3c542312013-02-24 01:53:50 -0800543
Tyler Chatow6738c362019-02-16 14:12:30 -0800544 ans.append(self._DumpMatrix('K', self.K, scalar_type))
545 if not hasattr(self, 'Kff'):
546 self.Kff = numpy.matrix(numpy.zeros(self.K.shape))
Austin Schuh86093ad2016-02-06 14:29:34 -0800547
Tyler Chatow6738c362019-02-16 14:12:30 -0800548 ans.append(self._DumpMatrix('Kff', self.Kff, scalar_type))
Austin Schuh3c542312013-02-24 01:53:50 -0800549
Tyler Chatow6738c362019-02-16 14:12:30 -0800550 ans.append(
551 ' return StateFeedbackControllerCoefficients<%d, %d, %d, %s>'
552 '(K, Kff);\n' % (num_states, num_inputs, num_outputs, scalar_type))
553 ans.append('}\n')
554 return ''.join(ans)
Austin Schuh32501832017-02-25 18:32:56 -0800555
Tyler Chatow6738c362019-02-16 14:12:30 -0800556 def DumpObserverHeader(self, observer_coefficient_type):
557 """Writes out a c++ header declaration which will create a Observer object.
Austin Schuh32501832017-02-25 18:32:56 -0800558
Tyler Chatow6738c362019-02-16 14:12:30 -0800559 Returns:
560 string, The header declaration for the function.
561 """
Ravago Jones26f7ad02021-02-05 15:45:59 -0800562 return '%s %s;\n' % (observer_coefficient_type,
563 self.ObserverFunction())
Austin Schuh32501832017-02-25 18:32:56 -0800564
James Kuszmauleeb98e92024-01-14 22:15:32 -0800565 def GetObserverCoefficients(self):
566 if hasattr(self, 'KalmanGain'):
567 KalmanGain = self.KalmanGain
568 Q = self.Q
569 R = self.R
570 else:
571 KalmanGain = numpy.linalg.inv(self.A) * self.L
572 Q = numpy.zeros(self.A.shape)
573 R = numpy.zeros((self.C.shape[0], self.C.shape[0]))
574 return (KalmanGain, Q, R)
575
576 def DumbObserverJson(self, observer_coefficient_type):
577 result = {"delayed_u": self.delayed_u}
578 if observer_coefficient_type.startswith('StateFeedbackObserver'):
579 KalmanGain, Q, R = self.GetObserverCoefficients()
580 result["kalman_gain"] = MatrixToJson(KalmanGain)
581 result["q"] = MatrixToJson(Q)
582 result["r"] = MatrixToJson(R)
583 elif observer_coefficient_type.startswith('HybridKalman'):
584 result["q_continuous"] = MatrixToJson(self.Q_continuous)
585 result["r_continuous"] = MatrixToJson(self.R_continuous)
586 result["p_steady_state"] = MatrixToJson(self.P_steady_state)
587 else:
588 glog.fatal('Unsupported plant type %s', observer_coefficient_type)
589 return result
590
Tyler Chatow6738c362019-02-16 14:12:30 -0800591 def DumpObserver(self, observer_coefficient_type, scalar_type):
592 """Returns a c++ function which will create a Observer object.
Austin Schuh32501832017-02-25 18:32:56 -0800593
Tyler Chatow6738c362019-02-16 14:12:30 -0800594 Returns:
595 string, The function which will create the object.
596 """
597 ans = [
598 '%s %s {\n' % (observer_coefficient_type, self.ObserverFunction())
599 ]
Austin Schuh32501832017-02-25 18:32:56 -0800600
Austin Schuhb39f4522022-03-27 13:29:42 -0700601 delayed_u_string = str(self.delayed_u)
Tyler Chatow6738c362019-02-16 14:12:30 -0800602 if observer_coefficient_type.startswith('StateFeedbackObserver'):
James Kuszmauleeb98e92024-01-14 22:15:32 -0800603 KalmanGain, Q, R = self.GetObserverCoefficients()
Tyler Chatow6738c362019-02-16 14:12:30 -0800604 ans.append(self._DumpMatrix('KalmanGain', KalmanGain, scalar_type))
605 ans.append(self._DumpMatrix('Q', Q, scalar_type))
606 ans.append(self._DumpMatrix('R', R, scalar_type))
Austin Schuh64433f12022-02-21 19:40:38 -0800607 ans.append(' return %s(KalmanGain, Q, R, %s);\n' %
608 (observer_coefficient_type, delayed_u_string))
Sabina Davis3922dfa2018-02-10 23:10:05 -0800609
Tyler Chatow6738c362019-02-16 14:12:30 -0800610 elif observer_coefficient_type.startswith('HybridKalman'):
611 ans.append(
612 self._DumpMatrix('Q_continuous', self.Q_continuous,
613 scalar_type))
614 ans.append(
615 self._DumpMatrix('R_continuous', self.R_continuous,
616 scalar_type))
617 ans.append(
618 self._DumpMatrix('P_steady_state', self.P_steady_state,
619 scalar_type))
620 ans.append(
Ravago Jones5127ccc2022-07-31 16:32:45 -0700621 ' return %s(Q_continuous, R_continuous, P_steady_state, %s);\n'
622 % (observer_coefficient_type, delayed_u_string))
Tyler Chatow6738c362019-02-16 14:12:30 -0800623 else:
624 glog.fatal('Unsupported observer type %s',
625 observer_coefficient_type)
Austin Schuh32501832017-02-25 18:32:56 -0800626
Tyler Chatow6738c362019-02-16 14:12:30 -0800627 ans.append('}\n')
628 return ''.join(ans)
629
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800630
631class HybridControlLoop(ControlLoop):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700632
Tyler Chatow6738c362019-02-16 14:12:30 -0800633 def __init__(self, name):
634 super(HybridControlLoop, self).__init__(name=name)
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800635
Tyler Chatow6738c362019-02-16 14:12:30 -0800636 def Discretize(self, dt):
637 [self.A, self.B, self.Q, self.R] = \
638 controls.kalmd(self.A_continuous, self.B_continuous,
639 self.Q_continuous, self.R_continuous, dt)
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800640
Tyler Chatow6738c362019-02-16 14:12:30 -0800641 def PredictHybridObserver(self, U, dt):
642 self.Discretize(dt)
Austin Schuhb39f4522022-03-27 13:29:42 -0700643 if self.delayed_u > 0:
644 self.X_hat = self.A * self.X_hat + self.B * self.last_U[:, -1]
645 self.last_U[:, 1:] = self.last_U[:, 0:-1]
646 self.last_U[:, 0] = U.copy()
Austin Schuh64433f12022-02-21 19:40:38 -0800647 else:
648 self.X_hat = self.A * self.X_hat + self.B * U
649
Tyler Chatow6738c362019-02-16 14:12:30 -0800650 self.P = (self.A * self.P * self.A.T + self.Q)
Austin Schuh3ad5ed82017-02-25 21:36:19 -0800651
Tyler Chatow6738c362019-02-16 14:12:30 -0800652 def CorrectHybridObserver(self, U):
653 Y_bar = self.Y - self.C * self.X_hat
654 C_t = self.C.T
655 S = self.C * self.P * C_t + self.R
656 self.KalmanGain = self.P * C_t * numpy.linalg.inv(S)
657 self.X_hat = self.X_hat + self.KalmanGain * Y_bar
658 self.P = (numpy.eye(len(self.A)) - self.KalmanGain * self.C) * self.P
659
660 def InitializeState(self):
661 super(HybridControlLoop, self).InitializeState()
662 if hasattr(self, 'Q_steady_state'):
663 self.P = self.Q_steady_state
664 else:
665 self.P = numpy.matrix(
666 numpy.zeros((self.A.shape[0], self.A.shape[0])))
Campbell Crowley33e0e3d2017-12-27 17:55:40 -0800667
668
669class CIM(object):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700670
Tyler Chatow6738c362019-02-16 14:12:30 -0800671 def __init__(self):
672 # Stall Torque in N m
673 self.stall_torque = 2.42
674 # Stall Current in Amps
675 self.stall_current = 133.0
676 # Free Speed in rad/s
677 self.free_speed = 5500.0 / 60.0 * 2.0 * numpy.pi
678 # Free Current in Amps
679 self.free_current = 4.7
680 # Resistance of the motor
681 self.resistance = 12.0 / self.stall_current
682 # Motor velocity constant
Ravago Jones5127ccc2022-07-31 16:32:45 -0700683 self.Kv = (self.free_speed /
684 (12.0 - self.resistance * self.free_current))
Tyler Chatow6738c362019-02-16 14:12:30 -0800685 # Torque constant
686 self.Kt = self.stall_torque / self.stall_current
Lee Mracek97fc8af2018-01-13 04:38:52 -0500687
688
689class MiniCIM(object):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700690
Tyler Chatow6738c362019-02-16 14:12:30 -0800691 def __init__(self):
692 # Stall Torque in N m
693 self.stall_torque = 1.41
694 # Stall Current in Amps
695 self.stall_current = 89.0
696 # Free Speed in rad/s
697 self.free_speed = 5840.0 / 60.0 * 2.0 * numpy.pi
698 # Free Current in Amps
699 self.free_current = 3.0
700 # Resistance of the motor
701 self.resistance = 12.0 / self.stall_current
702 # Motor velocity constant
Ravago Jones5127ccc2022-07-31 16:32:45 -0700703 self.Kv = (self.free_speed /
704 (12.0 - self.resistance * self.free_current))
Tyler Chatow6738c362019-02-16 14:12:30 -0800705 # Torque constant
706 self.Kt = self.stall_torque / self.stall_current
Austin Schuhf173eb82018-01-20 23:32:30 -0800707
milind-uf70e8e12021-10-02 12:36:00 -0700708 # Motor inertia in kg m^2
709 self.motor_inertia = 0.0001634
710
Austin Schuhf173eb82018-01-20 23:32:30 -0800711
Austin Schuhb5d302f2019-01-20 20:51:19 -0800712class NMotor(object):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700713
Austin Schuhb5d302f2019-01-20 20:51:19 -0800714 def __init__(self, motor, n):
715 """Gangs together n motors."""
716 self.motor = motor
717 self.stall_torque = motor.stall_torque * n
718 self.stall_current = motor.stall_current * n
719 self.free_speed = motor.free_speed
720
721 self.free_current = motor.free_current * n
722 self.resistance = motor.resistance / n
723 self.Kv = motor.Kv
724 self.Kt = motor.Kt
Austin Schuh36bb8e32019-02-18 15:02:57 -0800725 self.motor_inertia = motor.motor_inertia * n
Austin Schuhb5d302f2019-01-20 20:51:19 -0800726
727
728class Vex775Pro(object):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700729
Austin Schuhb5d302f2019-01-20 20:51:19 -0800730 def __init__(self):
731 # Stall Torque in N m
732 self.stall_torque = 0.71
733 # Stall Current in Amps
734 self.stall_current = 134.0
735 # Free Speed in rad/s
736 self.free_speed = 18730.0 / 60.0 * 2.0 * numpy.pi
737 # Free Current in Amps
738 self.free_current = 0.7
739 # Resistance of the motor
740 self.resistance = 12.0 / self.stall_current
741 # Motor velocity constant
Ravago Jones5127ccc2022-07-31 16:32:45 -0700742 self.Kv = (self.free_speed /
743 (12.0 - self.resistance * self.free_current))
Austin Schuhb5d302f2019-01-20 20:51:19 -0800744 # Torque constant
745 self.Kt = self.stall_torque / self.stall_current
746 # Motor inertia in kg m^2
747 self.motor_inertia = 0.00001187
748
749
Austin Schuhf173eb82018-01-20 23:32:30 -0800750class BAG(object):
Tyler Chatow6738c362019-02-16 14:12:30 -0800751 # BAG motor specs available at http://motors.vex.com/vexpro-motors/bag-motor
752 def __init__(self):
753 # Stall Torque in (N m)
754 self.stall_torque = 0.43
755 # Stall Current in (Amps)
756 self.stall_current = 53.0
757 # Free Speed in (rad/s)
758 self.free_speed = 13180.0 / 60.0 * 2.0 * numpy.pi
759 # Free Current in (Amps)
760 self.free_current = 1.8
761 # Resistance of the motor (Ohms)
762 self.resistance = 12.0 / self.stall_current
763 # Motor velocity constant (radians / (sec * volt))
Ravago Jones5127ccc2022-07-31 16:32:45 -0700764 self.Kv = (self.free_speed /
765 (12.0 - self.resistance * self.free_current))
Tyler Chatow6738c362019-02-16 14:12:30 -0800766 # Torque constant (N * m / A)
767 self.Kt = self.stall_torque / self.stall_current
768 # Motor inertia in kg m^2
769 self.motor_inertia = 0.000006
770
Brian Silverman6260c092018-01-14 15:21:36 -0800771
772class MN3510(object):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700773
Tyler Chatow6738c362019-02-16 14:12:30 -0800774 def __init__(self):
775 # http://www.robotshop.com/en/t-motor-navigator-mn3510-360kv-brushless-motor.html#Specifications
776 # Free Current in Amps
777 self.free_current = 0.0
778 # Resistance of the motor
779 self.resistance = 0.188
780 # Stall Current in Amps
781 self.stall_current = 14.0 / self.resistance
782 # Motor velocity constant
783 self.Kv = 360.0 / 60.0 * (2.0 * numpy.pi)
784 # Torque constant Nm / A
785 self.Kt = 1.0 / self.Kv
786 # Stall Torque in N m
787 self.stall_torque = self.Kt * self.stall_current
James Kuszmaulef0c18a2020-01-12 15:44:20 -0800788
789
790class Falcon(object):
791 """Class representing the VexPro Falcon 500 motor.
792
793 All numbers based on data from
794 https://www.vexrobotics.com/vexpro/falcon-500."""
795
796 def __init__(self):
797 # Stall Torque in N m
798 self.stall_torque = 4.69
799 # Stall Current in Amps
800 self.stall_current = 257.0
801 # Free Speed in rad / sec
802 self.free_speed = 6380.0 / 60.0 * 2.0 * numpy.pi
803 # Free Current in Amps
804 self.free_current = 1.5
805 # Resistance of the motor, divided by 2 to account for the 2 motors
806 self.resistance = 12.0 / self.stall_current
807 # Motor velocity constant
Ravago Jones5127ccc2022-07-31 16:32:45 -0700808 self.Kv = (self.free_speed /
809 (12.0 - self.resistance * self.free_current))
James Kuszmaulef0c18a2020-01-12 15:44:20 -0800810 # Torque constant
811 self.Kt = self.stall_torque / self.stall_current
Austin Schuhc1c957a2020-02-20 17:47:58 -0800812 # Motor inertia in kg m^2
813 # Diameter of 1.9", weight of: 100 grams
814 # TODO(austin): Get a number from Scott Westbrook for the mass
Ravago Jones26f7ad02021-02-05 15:45:59 -0800815 self.motor_inertia = 0.1 * ((0.95 * 0.0254)**2.0)
Filip Kujawa7e835182024-01-13 16:22:09 -0800816
817
818class KrakenFOC(object):
819 """Class representing the WCP Kraken X60 motor using
820 Field Oriented Controls (FOC) communication.
821
822 All numbers based on data from
823 https://wcproducts.com/products/kraken.
824 """
825
826 def __init__(self):
827 # Stall Torque in N m
828 self.stall_torque = 9.37
829 # Stall Current in Amps
830 self.stall_current = 483.0
831 # Free Speed in rad / sec
832 self.free_speed = 5800.0 / 60.0 * 2.0 * numpy.pi
833 # Free Current in Amps
834 self.free_current = 2.0
835 # Resistance of the motor, divided by 2 to account for the 2 motors
836 self.resistance = 12.0 / self.stall_current
837 # Motor velocity constant
838 self.Kv = (self.free_speed /
839 (12.0 - self.resistance * self.free_current))
840 # Torque constant
841 self.Kt = self.stall_torque / self.stall_current
842 # Motor inertia in kg m^2
843 # Diameter of 1.9", weight of: 100 grams
844 # TODO(Filip): Update motor inertia for Kraken, currently using Falcon motor inertia
845 self.motor_inertia = 0.1 * ((0.95 * 0.0254)**2.0)