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