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