Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 1 | import controls |
| 2 | import numpy |
Austin Schuh | 572ff40 | 2015-11-08 12:17:50 -0800 | [diff] [blame] | 3 | import os |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 4 | |
Ben Fredrickson | 1b45f78 | 2014-02-23 07:44:36 +0000 | [diff] [blame] | 5 | class Constant(object): |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 6 | def __init__ (self, name, formatt, value): |
| 7 | self.name = name |
| 8 | self.formatt = formatt |
| 9 | self.value = value |
| 10 | self.formatToType = {} |
Brian Silverman | 4e55e58 | 2015-11-10 14:16:37 -0500 | [diff] [blame] | 11 | self.formatToType['%f'] = "double" |
| 12 | self.formatToType['%d'] = "int" |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 13 | |
| 14 | def Render(self, loop_type): |
| 15 | typestring = self.formatToType[self.formatt] |
| 16 | if loop_type == 'float' and typestring == 'double': |
| 17 | typestring = loop_type |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 18 | return str("\nstatic constexpr %s %s = "+ self.formatt +";\n") % \ |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 19 | (typestring, self.name, self.value) |
Ben Fredrickson | 1b45f78 | 2014-02-23 07:44:36 +0000 | [diff] [blame] | 20 | |
| 21 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 22 | class ControlLoopWriter(object): |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 23 | def __init__(self, gain_schedule_name, loops, namespaces=None, |
| 24 | write_constants=False, plant_type='StateFeedbackPlant', |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 25 | observer_type='StateFeedbackObserver', |
| 26 | scalar_type='double'): |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 27 | """Constructs a control loop writer. |
| 28 | |
| 29 | Args: |
| 30 | gain_schedule_name: string, Name of the overall controller. |
| 31 | loops: array[ControlLoop], a list of control loops to gain schedule |
| 32 | in order. |
| 33 | namespaces: array[string], a list of names of namespaces to nest in |
| 34 | order. If None, the default will be used. |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 35 | plant_type: string, The C++ type of the plant. |
| 36 | observer_type: string, The C++ type of the observer. |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 37 | scalar_type: string, The C++ type of the base scalar. |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 38 | """ |
| 39 | self._gain_schedule_name = gain_schedule_name |
| 40 | self._loops = loops |
| 41 | if namespaces: |
| 42 | self._namespaces = namespaces |
| 43 | else: |
| 44 | self._namespaces = ['frc971', 'control_loops'] |
| 45 | |
| 46 | self._namespace_start = '\n'.join( |
| 47 | ['namespace %s {' % name for name in self._namespaces]) |
| 48 | |
| 49 | self._namespace_end = '\n'.join( |
| 50 | ['} // namespace %s' % name for name in reversed(self._namespaces)]) |
Austin Schuh | 86093ad | 2016-02-06 14:29:34 -0800 | [diff] [blame] | 51 | |
Ben Fredrickson | 1b45f78 | 2014-02-23 07:44:36 +0000 | [diff] [blame] | 52 | self._constant_list = [] |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 53 | self._plant_type = plant_type |
| 54 | self._observer_type = observer_type |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 55 | self._scalar_type = scalar_type |
Austin Schuh | 2593385 | 2014-02-23 02:04:13 -0800 | [diff] [blame] | 56 | |
| 57 | def AddConstant(self, constant): |
| 58 | """Adds a constant to write. |
| 59 | |
| 60 | Args: |
| 61 | constant: Constant, the constant to add to the header. |
| 62 | """ |
| 63 | self._constant_list.append(constant) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 64 | |
Brian Silverman | e51ad63 | 2014-01-08 15:12:29 -0800 | [diff] [blame] | 65 | def _TopDirectory(self): |
| 66 | return self._namespaces[0] |
| 67 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 68 | def _HeaderGuard(self, header_file): |
Austin Schuh | 16cf47a | 2015-11-28 13:20:33 -0800 | [diff] [blame] | 69 | return ('_'.join([namespace.upper() for namespace in self._namespaces]) + '_' + |
Austin Schuh | 572ff40 | 2015-11-08 12:17:50 -0800 | [diff] [blame] | 70 | os.path.basename(header_file).upper() |
| 71 | .replace('.', '_').replace('/', '_') + '_') |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 72 | |
| 73 | def Write(self, header_file, cc_file): |
| 74 | """Writes the loops to the specified files.""" |
| 75 | self.WriteHeader(header_file) |
Austin Schuh | 572ff40 | 2015-11-08 12:17:50 -0800 | [diff] [blame] | 76 | self.WriteCC(os.path.basename(header_file), cc_file) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 77 | |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 78 | def _GenericType(self, typename, extra_args=None): |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 79 | """Returns a loop template using typename for the type.""" |
| 80 | num_states = self._loops[0].A.shape[0] |
| 81 | num_inputs = self._loops[0].B.shape[1] |
| 82 | num_outputs = self._loops[0].C.shape[0] |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 83 | if extra_args is not None: |
| 84 | extra_args = ', ' + extra_args |
| 85 | else: |
| 86 | extra_args = '' |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 87 | if self._scalar_type != 'double': |
| 88 | extra_args += ', ' + self._scalar_type |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 89 | return '%s<%d, %d, %d%s>' % ( |
| 90 | typename, num_states, num_inputs, num_outputs, extra_args) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 91 | |
| 92 | def _ControllerType(self): |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 93 | """Returns a template name for StateFeedbackController.""" |
| 94 | return self._GenericType('StateFeedbackController') |
| 95 | |
| 96 | def _ObserverType(self): |
| 97 | """Returns a template name for StateFeedbackObserver.""" |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 98 | return self._GenericType(self._observer_type) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 99 | |
| 100 | def _LoopType(self): |
| 101 | """Returns a template name for StateFeedbackLoop.""" |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 102 | num_states = self._loops[0].A.shape[0] |
| 103 | num_inputs = self._loops[0].B.shape[1] |
| 104 | num_outputs = self._loops[0].C.shape[0] |
| 105 | |
| 106 | return 'StateFeedbackLoop<%d, %d, %d, %s, %s, %s>' % ( |
| 107 | num_states, |
| 108 | num_inputs, |
| 109 | num_outputs, self._scalar_type, |
| 110 | self._PlantType(), self._ObserverType()) |
| 111 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 112 | |
| 113 | def _PlantType(self): |
| 114 | """Returns a template name for StateFeedbackPlant.""" |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 115 | return self._GenericType(self._plant_type) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 116 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 117 | def _PlantCoeffType(self): |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 118 | """Returns a template name for StateFeedbackPlantCoefficients.""" |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 119 | return self._GenericType(self._plant_type + 'Coefficients') |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 120 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 121 | def _ControllerCoeffType(self): |
| 122 | """Returns a template name for StateFeedbackControllerCoefficients.""" |
| 123 | return self._GenericType('StateFeedbackControllerCoefficients') |
| 124 | |
| 125 | def _ObserverCoeffType(self): |
| 126 | """Returns a template name for StateFeedbackObserverCoefficients.""" |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 127 | return self._GenericType(self._observer_type + 'Coefficients') |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 128 | |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 129 | def WriteHeader(self, header_file): |
| 130 | """Writes the header file to the file named header_file.""" |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 131 | with open(header_file, 'w') as fd: |
| 132 | header_guard = self._HeaderGuard(header_file) |
| 133 | fd.write('#ifndef %s\n' |
| 134 | '#define %s\n\n' % (header_guard, header_guard)) |
| 135 | fd.write('#include \"frc971/control_loops/state_feedback_loop.h\"\n') |
Austin Schuh | 4cc4fe2 | 2017-11-23 19:13:09 -0800 | [diff] [blame] | 136 | if (self._plant_type == 'StateFeedbackHybridPlant' or |
| 137 | self._observer_type == 'HybridKalman'): |
| 138 | fd.write('#include \"frc971/control_loops/hybrid_state_feedback_loop.h\"\n') |
| 139 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 140 | fd.write('\n') |
| 141 | |
| 142 | fd.write(self._namespace_start) |
Ben Fredrickson | 1b45f78 | 2014-02-23 07:44:36 +0000 | [diff] [blame] | 143 | |
| 144 | for const in self._constant_list: |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 145 | fd.write(const.Render(self._scalar_type)) |
Ben Fredrickson | 1b45f78 | 2014-02-23 07:44:36 +0000 | [diff] [blame] | 146 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 147 | fd.write('\n\n') |
| 148 | for loop in self._loops: |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 149 | fd.write(loop.DumpPlantHeader(self._PlantCoeffType())) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 150 | fd.write('\n') |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 151 | fd.write(loop.DumpControllerHeader(self._scalar_type)) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 152 | fd.write('\n') |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 153 | fd.write(loop.DumpObserverHeader(self._ObserverCoeffType())) |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 154 | fd.write('\n') |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 155 | |
| 156 | fd.write('%s Make%sPlant();\n\n' % |
| 157 | (self._PlantType(), self._gain_schedule_name)) |
| 158 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 159 | fd.write('%s Make%sController();\n\n' % |
| 160 | (self._ControllerType(), self._gain_schedule_name)) |
| 161 | |
| 162 | fd.write('%s Make%sObserver();\n\n' % |
| 163 | (self._ObserverType(), self._gain_schedule_name)) |
| 164 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 165 | fd.write('%s Make%sLoop();\n\n' % |
| 166 | (self._LoopType(), self._gain_schedule_name)) |
| 167 | |
| 168 | fd.write(self._namespace_end) |
| 169 | fd.write('\n\n') |
| 170 | fd.write("#endif // %s\n" % header_guard) |
| 171 | |
| 172 | def WriteCC(self, header_file_name, cc_file): |
| 173 | """Writes the cc file to the file named cc_file.""" |
| 174 | with open(cc_file, 'w') as fd: |
Austin Schuh | 572ff40 | 2015-11-08 12:17:50 -0800 | [diff] [blame] | 175 | fd.write('#include \"%s/%s\"\n' % |
| 176 | (os.path.join(*self._namespaces), header_file_name)) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 177 | fd.write('\n') |
| 178 | fd.write('#include <vector>\n') |
| 179 | fd.write('\n') |
| 180 | fd.write('#include \"frc971/control_loops/state_feedback_loop.h\"\n') |
| 181 | fd.write('\n') |
| 182 | fd.write(self._namespace_start) |
| 183 | fd.write('\n\n') |
| 184 | for loop in self._loops: |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 185 | fd.write(loop.DumpPlant(self._PlantCoeffType(), self._scalar_type)) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 186 | fd.write('\n') |
| 187 | |
| 188 | for loop in self._loops: |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 189 | fd.write(loop.DumpController(self._scalar_type)) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 190 | fd.write('\n') |
| 191 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 192 | for loop in self._loops: |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 193 | fd.write(loop.DumpObserver(self._ObserverCoeffType(), self._scalar_type)) |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 194 | fd.write('\n') |
| 195 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 196 | fd.write('%s Make%sPlant() {\n' % |
| 197 | (self._PlantType(), self._gain_schedule_name)) |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 198 | fd.write(' ::std::vector< ::std::unique_ptr<%s>> plants(%d);\n' % ( |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 199 | self._PlantCoeffType(), len(self._loops))) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 200 | for index, loop in enumerate(self._loops): |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 201 | fd.write(' plants[%d] = ::std::unique_ptr<%s>(new %s(%s));\n' % |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 202 | (index, self._PlantCoeffType(), self._PlantCoeffType(), |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 203 | loop.PlantFunction())) |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 204 | fd.write(' return %s(&plants);\n' % self._PlantType()) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 205 | fd.write('}\n\n') |
| 206 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 207 | fd.write('%s Make%sController() {\n' % |
| 208 | (self._ControllerType(), self._gain_schedule_name)) |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 209 | fd.write(' ::std::vector< ::std::unique_ptr<%s>> controllers(%d);\n' % ( |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 210 | self._ControllerCoeffType(), len(self._loops))) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 211 | for index, loop in enumerate(self._loops): |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 212 | fd.write(' controllers[%d] = ::std::unique_ptr<%s>(new %s(%s));\n' % |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 213 | (index, self._ControllerCoeffType(), self._ControllerCoeffType(), |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 214 | loop.ControllerFunction())) |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 215 | fd.write(' return %s(&controllers);\n' % self._ControllerType()) |
| 216 | fd.write('}\n\n') |
| 217 | |
| 218 | fd.write('%s Make%sObserver() {\n' % |
| 219 | (self._ObserverType(), self._gain_schedule_name)) |
| 220 | fd.write(' ::std::vector< ::std::unique_ptr<%s>> observers(%d);\n' % ( |
| 221 | self._ObserverCoeffType(), len(self._loops))) |
| 222 | for index, loop in enumerate(self._loops): |
| 223 | fd.write(' observers[%d] = ::std::unique_ptr<%s>(new %s(%s));\n' % |
| 224 | (index, self._ObserverCoeffType(), self._ObserverCoeffType(), |
| 225 | loop.ObserverFunction())) |
| 226 | fd.write(' return %s(&observers);\n' % self._ObserverType()) |
| 227 | fd.write('}\n\n') |
| 228 | |
| 229 | fd.write('%s Make%sLoop() {\n' % |
| 230 | (self._LoopType(), self._gain_schedule_name)) |
| 231 | fd.write(' return %s(Make%sPlant(), Make%sController(), Make%sObserver());\n' % |
| 232 | (self._LoopType(), self._gain_schedule_name, |
| 233 | self._gain_schedule_name, self._gain_schedule_name)) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 234 | fd.write('}\n\n') |
| 235 | |
| 236 | fd.write(self._namespace_end) |
| 237 | fd.write('\n') |
| 238 | |
| 239 | |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 240 | class ControlLoop(object): |
| 241 | def __init__(self, name): |
| 242 | """Constructs a control loop object. |
| 243 | |
| 244 | Args: |
| 245 | name: string, The name of the loop to use when writing the C++ files. |
| 246 | """ |
| 247 | self._name = name |
| 248 | |
Austin Schuh | b5d302f | 2019-01-20 20:51:19 -0800 | [diff] [blame^] | 249 | @property |
| 250 | def name(self): |
| 251 | """Returns the name""" |
| 252 | return self._name |
| 253 | |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 254 | def ContinuousToDiscrete(self, A_continuous, B_continuous, dt): |
| 255 | """Calculates the discrete time values for A and B. |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 256 | |
| 257 | Args: |
| 258 | A_continuous: numpy.matrix, The continuous time A matrix |
| 259 | B_continuous: numpy.matrix, The continuous time B matrix |
| 260 | dt: float, The time step of the control loop |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 261 | |
| 262 | Returns: |
| 263 | (A, B), numpy.matrix, the control matricies. |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 264 | """ |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 265 | return controls.c2d(A_continuous, B_continuous, dt) |
| 266 | |
| 267 | def InitializeState(self): |
| 268 | """Sets X, Y, and X_hat to zero defaults.""" |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 269 | self.X = numpy.zeros((self.A.shape[0], 1)) |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 270 | self.Y = self.C * self.X |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 271 | self.X_hat = numpy.zeros((self.A.shape[0], 1)) |
| 272 | |
| 273 | def PlaceControllerPoles(self, poles): |
| 274 | """Places the controller poles. |
| 275 | |
| 276 | Args: |
| 277 | poles: array, An array of poles. Must be complex conjegates if they have |
| 278 | any imaginary portions. |
| 279 | """ |
| 280 | self.K = controls.dplace(self.A, self.B, poles) |
| 281 | |
| 282 | def PlaceObserverPoles(self, poles): |
| 283 | """Places the observer poles. |
| 284 | |
| 285 | Args: |
| 286 | poles: array, An array of poles. Must be complex conjegates if they have |
| 287 | any imaginary portions. |
| 288 | """ |
| 289 | self.L = controls.dplace(self.A.T, self.C.T, poles).T |
| 290 | |
Sabina Davis | 3922dfa | 2018-02-10 23:10:05 -0800 | [diff] [blame] | 291 | |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 292 | def Update(self, U): |
| 293 | """Simulates one time step with the provided U.""" |
Austin Schuh | 1d00573 | 2015-03-01 00:10:20 -0800 | [diff] [blame] | 294 | #U = numpy.clip(U, self.U_min, self.U_max) |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 295 | self.X = self.A * self.X + self.B * U |
| 296 | self.Y = self.C * self.X + self.D * U |
| 297 | |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 298 | def PredictObserver(self, U): |
| 299 | """Runs the predict step of the observer update.""" |
| 300 | self.X_hat = (self.A * self.X_hat + self.B * U) |
| 301 | |
| 302 | def CorrectObserver(self, U): |
| 303 | """Runs the correct step of the observer update.""" |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 304 | if hasattr(self, 'KalmanGain'): |
| 305 | KalmanGain = self.KalmanGain |
| 306 | else: |
| 307 | KalmanGain = numpy.linalg.inv(self.A) * self.L |
| 308 | self.X_hat += KalmanGain * (self.Y - self.C * self.X_hat - self.D * U) |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 309 | |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 310 | def UpdateObserver(self, U): |
| 311 | """Updates the observer given the provided U.""" |
Sabina Davis | 3922dfa | 2018-02-10 23:10:05 -0800 | [diff] [blame] | 312 | if hasattr(self, 'KalmanGain'): |
| 313 | KalmanGain = self.KalmanGain |
| 314 | else: |
| 315 | KalmanGain = numpy.linalg.inv(self.A) * self.L |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 316 | self.X_hat = (self.A * self.X_hat + self.B * U + |
Sabina Davis | 3922dfa | 2018-02-10 23:10:05 -0800 | [diff] [blame] | 317 | self.A * KalmanGain * (self.Y - self.C * self.X_hat - self.D * U)) |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 318 | |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 319 | def _DumpMatrix(self, matrix_name, matrix, scalar_type): |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 320 | """Dumps the provided matrix into a variable called matrix_name. |
| 321 | |
| 322 | Args: |
| 323 | matrix_name: string, The variable name to save the matrix to. |
| 324 | matrix: The matrix to dump. |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 325 | scalar_type: The C++ type to use for the scalar in the matrix. |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 326 | |
| 327 | Returns: |
| 328 | string, The C++ commands required to populate a variable named matrix_name |
| 329 | with the contents of matrix. |
| 330 | """ |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 331 | ans = [' Eigen::Matrix<%s, %d, %d> %s;\n' % ( |
| 332 | scalar_type, matrix.shape[0], matrix.shape[1], matrix_name)] |
Brian Silverman | 0f63738 | 2013-03-03 17:44:46 -0800 | [diff] [blame] | 333 | for x in xrange(matrix.shape[0]): |
| 334 | for y in xrange(matrix.shape[1]): |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 335 | write_type = repr(matrix[x, y]) |
| 336 | if scalar_type == 'float': |
Austin Schuh | bcce26a | 2018-03-26 23:41:24 -0700 | [diff] [blame] | 337 | if '.' not in write_type: |
| 338 | write_type += '.0' |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 339 | write_type += 'f' |
| 340 | ans.append(' %s(%d, %d) = %s;\n' % (matrix_name, x, y, write_type)) |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 341 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 342 | return ''.join(ans) |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 343 | |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 344 | def DumpPlantHeader(self, plant_coefficient_type): |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 345 | """Writes out a c++ header declaration which will create a Plant object. |
| 346 | |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 347 | Returns: |
| 348 | string, The header declaration for the function. |
| 349 | """ |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 350 | return '%s Make%sPlantCoefficients();\n' % ( |
| 351 | plant_coefficient_type, self._name) |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 352 | |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 353 | def DumpPlant(self, plant_coefficient_type, scalar_type): |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 354 | """Writes out a c++ function which will create a PlantCoefficients object. |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 355 | |
| 356 | Returns: |
| 357 | string, The function which will create the object. |
| 358 | """ |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 359 | ans = ['%s Make%sPlantCoefficients() {\n' % ( |
| 360 | plant_coefficient_type, self._name)] |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 361 | |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 362 | ans.append(self._DumpMatrix('C', self.C, scalar_type)) |
| 363 | ans.append(self._DumpMatrix('D', self.D, scalar_type)) |
| 364 | ans.append(self._DumpMatrix('U_max', self.U_max, scalar_type)) |
| 365 | ans.append(self._DumpMatrix('U_min', self.U_min, scalar_type)) |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 366 | |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 367 | if plant_coefficient_type.startswith('StateFeedbackPlant'): |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 368 | ans.append(self._DumpMatrix('A', self.A, scalar_type)) |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 369 | ans.append(self._DumpMatrix('B', self.B, scalar_type)) |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 370 | ans.append(' return %s' |
Sabina Davis | 3922dfa | 2018-02-10 23:10:05 -0800 | [diff] [blame] | 371 | '(A, B, C, D, U_max, U_min);\n' % ( |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 372 | plant_coefficient_type)) |
| 373 | elif plant_coefficient_type.startswith('StateFeedbackHybridPlant'): |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 374 | ans.append(self._DumpMatrix('A_continuous', self.A_continuous, scalar_type)) |
| 375 | ans.append(self._DumpMatrix('B_continuous', self.B_continuous, scalar_type)) |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 376 | ans.append(' return %s' |
| 377 | '(A_continuous, B_continuous, C, D, U_max, U_min);\n' % ( |
| 378 | plant_coefficient_type)) |
| 379 | else: |
| 380 | glog.fatal('Unsupported plant type %s', plant_coefficient_type) |
| 381 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 382 | ans.append('}\n') |
| 383 | return ''.join(ans) |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 384 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 385 | def PlantFunction(self): |
| 386 | """Returns the name of the plant coefficient function.""" |
| 387 | return 'Make%sPlantCoefficients()' % self._name |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 388 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 389 | def ControllerFunction(self): |
| 390 | """Returns the name of the controller function.""" |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 391 | return 'Make%sControllerCoefficients()' % self._name |
| 392 | |
| 393 | def ObserverFunction(self): |
| 394 | """Returns the name of the controller function.""" |
| 395 | return 'Make%sObserverCoefficients()' % self._name |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 396 | |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 397 | def DumpControllerHeader(self, scalar_type): |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 398 | """Writes out a c++ header declaration which will create a Controller object. |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 399 | |
| 400 | Returns: |
| 401 | string, The header declaration for the function. |
| 402 | """ |
| 403 | num_states = self.A.shape[0] |
| 404 | num_inputs = self.B.shape[1] |
| 405 | num_outputs = self.C.shape[0] |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 406 | return 'StateFeedbackControllerCoefficients<%d, %d, %d, %s> %s;\n' % ( |
| 407 | num_states, num_inputs, num_outputs, scalar_type, |
| 408 | self.ControllerFunction()) |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 409 | |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 410 | def DumpController(self, scalar_type): |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 411 | """Returns a c++ function which will create a Controller object. |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 412 | |
| 413 | Returns: |
| 414 | string, The function which will create the object. |
| 415 | """ |
| 416 | num_states = self.A.shape[0] |
| 417 | num_inputs = self.B.shape[1] |
| 418 | num_outputs = self.C.shape[0] |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 419 | ans = ['StateFeedbackControllerCoefficients<%d, %d, %d, %s> %s {\n' % ( |
| 420 | num_states, num_inputs, num_outputs, scalar_type, |
| 421 | self.ControllerFunction())] |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 422 | |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 423 | ans.append(self._DumpMatrix('K', self.K, scalar_type)) |
Austin Schuh | 86093ad | 2016-02-06 14:29:34 -0800 | [diff] [blame] | 424 | if not hasattr(self, 'Kff'): |
| 425 | self.Kff = numpy.matrix(numpy.zeros(self.K.shape)) |
| 426 | |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 427 | ans.append(self._DumpMatrix('Kff', self.Kff, scalar_type)) |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 428 | |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 429 | ans.append(' return StateFeedbackControllerCoefficients<%d, %d, %d, %s>' |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 430 | '(K, Kff);\n' % ( |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 431 | num_states, num_inputs, num_outputs, scalar_type)) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 432 | ans.append('}\n') |
| 433 | return ''.join(ans) |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 434 | |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 435 | def DumpObserverHeader(self, observer_coefficient_type): |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 436 | """Writes out a c++ header declaration which will create a Observer object. |
| 437 | |
| 438 | Returns: |
| 439 | string, The header declaration for the function. |
| 440 | """ |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 441 | return '%s %s;\n' % ( |
| 442 | observer_coefficient_type, self.ObserverFunction()) |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 443 | |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 444 | def DumpObserver(self, observer_coefficient_type, scalar_type): |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 445 | """Returns a c++ function which will create a Observer object. |
| 446 | |
| 447 | Returns: |
| 448 | string, The function which will create the object. |
| 449 | """ |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 450 | ans = ['%s %s {\n' % ( |
| 451 | observer_coefficient_type, self.ObserverFunction())] |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 452 | |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 453 | if observer_coefficient_type.startswith('StateFeedbackObserver'): |
Sabina Davis | 3922dfa | 2018-02-10 23:10:05 -0800 | [diff] [blame] | 454 | if hasattr(self, 'KalmanGain'): |
| 455 | KalmanGain = self.KalmanGain |
| 456 | else: |
| 457 | KalmanGain = numpy.linalg.inv(self.A) * self.L |
| 458 | ans.append(self._DumpMatrix('KalmanGain', KalmanGain, scalar_type)) |
| 459 | ans.append(' return %s(KalmanGain);\n' % (observer_coefficient_type,)) |
| 460 | |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 461 | elif observer_coefficient_type.startswith('HybridKalman'): |
Austin Schuh | 20388b6 | 2017-11-23 22:40:46 -0800 | [diff] [blame] | 462 | ans.append(self._DumpMatrix('Q_continuous', self.Q_continuous, scalar_type)) |
| 463 | ans.append(self._DumpMatrix('R_continuous', self.R_continuous, scalar_type)) |
| 464 | ans.append(self._DumpMatrix('P_steady_state', self.P_steady_state, scalar_type)) |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 465 | ans.append(' return %s(Q_continuous, R_continuous, P_steady_state);\n' % ( |
| 466 | observer_coefficient_type,)) |
Brian Silverman | a3a20cc | 2017-03-05 18:35:20 -0800 | [diff] [blame] | 467 | else: |
| 468 | glog.fatal('Unsupported observer type %s', observer_coefficient_type) |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 469 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 470 | ans.append('}\n') |
| 471 | return ''.join(ans) |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 472 | |
| 473 | class HybridControlLoop(ControlLoop): |
| 474 | def __init__(self, name): |
| 475 | super(HybridControlLoop, self).__init__(name=name) |
| 476 | |
Brian Silverman | 59c829a | 2017-03-05 18:36:54 -0800 | [diff] [blame] | 477 | def Discretize(self, dt): |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 478 | [self.A, self.B, self.Q, self.R] = \ |
| 479 | controls.kalmd(self.A_continuous, self.B_continuous, |
| 480 | self.Q_continuous, self.R_continuous, dt) |
| 481 | |
| 482 | def PredictHybridObserver(self, U, dt): |
Brian Silverman | 59c829a | 2017-03-05 18:36:54 -0800 | [diff] [blame] | 483 | self.Discretize(dt) |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 484 | self.X_hat = self.A * self.X_hat + self.B * U |
| 485 | self.P = (self.A * self.P * self.A.T + self.Q) |
| 486 | |
| 487 | def CorrectHybridObserver(self, U): |
| 488 | Y_bar = self.Y - self.C * self.X_hat |
| 489 | C_t = self.C.T |
| 490 | S = self.C * self.P * C_t + self.R |
| 491 | self.KalmanGain = self.P * C_t * numpy.linalg.inv(S) |
| 492 | self.X_hat = self.X_hat + self.KalmanGain * Y_bar |
| 493 | self.P = (numpy.eye(len(self.A)) - self.KalmanGain * self.C) * self.P |
| 494 | |
| 495 | def InitializeState(self): |
| 496 | super(HybridControlLoop, self).InitializeState() |
| 497 | if hasattr(self, 'Q_steady_state'): |
| 498 | self.P = self.Q_steady_state |
| 499 | else: |
| 500 | self.P = numpy.matrix(numpy.zeros((self.A.shape[0], self.A.shape[0]))) |
Campbell Crowley | 33e0e3d | 2017-12-27 17:55:40 -0800 | [diff] [blame] | 501 | |
| 502 | |
| 503 | class CIM(object): |
| 504 | def __init__(self): |
| 505 | # Stall Torque in N m |
| 506 | self.stall_torque = 2.42 |
| 507 | # Stall Current in Amps |
| 508 | self.stall_current = 133.0 |
| 509 | # Free Speed in rad/s |
| 510 | self.free_speed = 5500.0 / 60.0 * 2.0 * numpy.pi |
| 511 | # Free Current in Amps |
| 512 | self.free_current = 4.7 |
| 513 | # Resistance of the motor |
| 514 | self.resistance = 12.0 / self.stall_current |
| 515 | # Motor velocity constant |
| 516 | self.Kv = (self.free_speed / (12.0 - self.resistance * self.free_current)) |
| 517 | # Torque constant |
| 518 | self.Kt = self.stall_torque / self.stall_current |
Lee Mracek | 97fc8af | 2018-01-13 04:38:52 -0500 | [diff] [blame] | 519 | |
| 520 | |
| 521 | class MiniCIM(object): |
| 522 | def __init__(self): |
| 523 | # Stall Torque in N m |
| 524 | self.stall_torque = 1.41 |
| 525 | # Stall Current in Amps |
| 526 | self.stall_current = 89.0 |
| 527 | # Free Speed in rad/s |
| 528 | self.free_speed = 5840.0 / 60.0 * 2.0 * numpy.pi |
| 529 | # Free Current in Amps |
| 530 | self.free_current = 3.0 |
| 531 | # Resistance of the motor |
| 532 | self.resistance = 12.0 / self.stall_current |
| 533 | # Motor velocity constant |
| 534 | self.Kv = (self.free_speed / (12.0 - self.resistance * self.free_current)) |
| 535 | # Torque constant |
| 536 | self.Kt = self.stall_torque / self.stall_current |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 537 | |
| 538 | |
Austin Schuh | b5d302f | 2019-01-20 20:51:19 -0800 | [diff] [blame^] | 539 | class NMotor(object): |
| 540 | def __init__(self, motor, n): |
| 541 | """Gangs together n motors.""" |
| 542 | self.motor = motor |
| 543 | self.stall_torque = motor.stall_torque * n |
| 544 | self.stall_current = motor.stall_current * n |
| 545 | self.free_speed = motor.free_speed |
| 546 | |
| 547 | self.free_current = motor.free_current * n |
| 548 | self.resistance = motor.resistance / n |
| 549 | self.Kv = motor.Kv |
| 550 | self.Kt = motor.Kt |
| 551 | |
| 552 | |
| 553 | class Vex775Pro(object): |
| 554 | def __init__(self): |
| 555 | # Stall Torque in N m |
| 556 | self.stall_torque = 0.71 |
| 557 | # Stall Current in Amps |
| 558 | self.stall_current = 134.0 |
| 559 | # Free Speed in rad/s |
| 560 | self.free_speed = 18730.0 / 60.0 * 2.0 * numpy.pi |
| 561 | # Free Current in Amps |
| 562 | self.free_current = 0.7 |
| 563 | # Resistance of the motor |
| 564 | self.resistance = 12.0 / self.stall_current |
| 565 | # Motor velocity constant |
| 566 | self.Kv = (self.free_speed / (12.0 - self.resistance * self.free_current)) |
| 567 | # Torque constant |
| 568 | self.Kt = self.stall_torque / self.stall_current |
| 569 | # Motor inertia in kg m^2 |
| 570 | self.motor_inertia = 0.00001187 |
| 571 | |
| 572 | |
Austin Schuh | f173eb8 | 2018-01-20 23:32:30 -0800 | [diff] [blame] | 573 | class BAG(object): |
| 574 | # BAG motor specs available at http://motors.vex.com/vexpro-motors/bag-motor |
| 575 | def __init__(self): |
| 576 | # Stall Torque in (N m) |
| 577 | self.stall_torque = 0.43 |
| 578 | # Stall Current in (Amps) |
| 579 | self.stall_current = 53.0 |
| 580 | # Free Speed in (rad/s) |
| 581 | self.free_speed = 13180.0 / 60.0 * 2.0 * numpy.pi |
| 582 | # Free Current in (Amps) |
| 583 | self.free_current = 1.8 |
| 584 | # Resistance of the motor (Ohms) |
| 585 | self.resistance = 12.0 / self.stall_current |
| 586 | # Motor velocity constant (radians / (sec * volt)) |
| 587 | self.Kv = (self.free_speed / (12.0 - self.resistance * self.free_current)) |
| 588 | # Torque constant (N * m / A) |
| 589 | self.Kt = self.stall_torque / self.stall_current |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 590 | |
| 591 | class MN3510(object): |
| 592 | def __init__(self): |
| 593 | # http://www.robotshop.com/en/t-motor-navigator-mn3510-360kv-brushless-motor.html#Specifications |
| 594 | # Free Current in Amps |
| 595 | self.free_current = 0.0 |
| 596 | # Resistance of the motor |
| 597 | self.resistance = 0.188 |
| 598 | # Stall Current in Amps |
| 599 | self.stall_current = 14.0 / self.resistance |
| 600 | # Motor velocity constant |
| 601 | self.Kv = 360.0 / 60.0 * (2.0 * numpy.pi) |
| 602 | # Torque constant Nm / A |
| 603 | self.Kt = 1.0 / self.Kv |
| 604 | # Stall Torque in N m |
| 605 | self.stall_torque = self.Kt * self.stall_current |