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