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