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