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