Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 1 | import controls |
| 2 | import numpy |
Austin Schuh | 572ff40 | 2015-11-08 12:17:50 -0800 | [diff] [blame] | 3 | import os |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 4 | |
Ben Fredrickson | 1b45f78 | 2014-02-23 07:44:36 +0000 | [diff] [blame] | 5 | class Constant(object): |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 6 | def __init__ (self, name, formatt, value): |
| 7 | self.name = name |
| 8 | self.formatt = formatt |
| 9 | self.value = value |
| 10 | self.formatToType = {} |
Brian Silverman | 4e55e58 | 2015-11-10 14:16:37 -0500 | [diff] [blame] | 11 | self.formatToType['%f'] = "double" |
| 12 | self.formatToType['%d'] = "int" |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 13 | def __str__ (self): |
| 14 | return str("\nstatic constexpr %s %s = "+ self.formatt +";\n") % \ |
| 15 | (self.formatToType[self.formatt], self.name, self.value) |
Ben Fredrickson | 1b45f78 | 2014-02-23 07:44:36 +0000 | [diff] [blame] | 16 | |
| 17 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 18 | class ControlLoopWriter(object): |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 19 | def __init__(self, gain_schedule_name, loops, namespaces=None, |
| 20 | write_constants=False, plant_type='StateFeedbackPlant', |
| 21 | observer_type='StateFeedbackObserver'): |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 22 | """Constructs a control loop writer. |
| 23 | |
| 24 | Args: |
| 25 | gain_schedule_name: string, Name of the overall controller. |
| 26 | loops: array[ControlLoop], a list of control loops to gain schedule |
| 27 | in order. |
| 28 | namespaces: array[string], a list of names of namespaces to nest in |
| 29 | order. If None, the default will be used. |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 30 | plant_type: string, The C++ type of the plant. |
| 31 | observer_type: string, The C++ type of the observer. |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 32 | """ |
| 33 | self._gain_schedule_name = gain_schedule_name |
| 34 | self._loops = loops |
| 35 | if namespaces: |
| 36 | self._namespaces = namespaces |
| 37 | else: |
| 38 | self._namespaces = ['frc971', 'control_loops'] |
| 39 | |
| 40 | self._namespace_start = '\n'.join( |
| 41 | ['namespace %s {' % name for name in self._namespaces]) |
| 42 | |
| 43 | self._namespace_end = '\n'.join( |
| 44 | ['} // namespace %s' % name for name in reversed(self._namespaces)]) |
Austin Schuh | 86093ad | 2016-02-06 14:29:34 -0800 | [diff] [blame] | 45 | |
Ben Fredrickson | 1b45f78 | 2014-02-23 07:44:36 +0000 | [diff] [blame] | 46 | self._constant_list = [] |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 47 | self._plant_type = plant_type |
| 48 | self._observer_type = observer_type |
Austin Schuh | 2593385 | 2014-02-23 02:04:13 -0800 | [diff] [blame] | 49 | |
| 50 | def AddConstant(self, constant): |
| 51 | """Adds a constant to write. |
| 52 | |
| 53 | Args: |
| 54 | constant: Constant, the constant to add to the header. |
| 55 | """ |
| 56 | self._constant_list.append(constant) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 57 | |
Brian Silverman | e51ad63 | 2014-01-08 15:12:29 -0800 | [diff] [blame] | 58 | def _TopDirectory(self): |
| 59 | return self._namespaces[0] |
| 60 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 61 | def _HeaderGuard(self, header_file): |
Austin Schuh | 16cf47a | 2015-11-28 13:20:33 -0800 | [diff] [blame] | 62 | return ('_'.join([namespace.upper() for namespace in self._namespaces]) + '_' + |
Austin Schuh | 572ff40 | 2015-11-08 12:17:50 -0800 | [diff] [blame] | 63 | os.path.basename(header_file).upper() |
| 64 | .replace('.', '_').replace('/', '_') + '_') |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 65 | |
| 66 | def Write(self, header_file, cc_file): |
| 67 | """Writes the loops to the specified files.""" |
| 68 | self.WriteHeader(header_file) |
Austin Schuh | 572ff40 | 2015-11-08 12:17:50 -0800 | [diff] [blame] | 69 | self.WriteCC(os.path.basename(header_file), cc_file) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 70 | |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 71 | def _GenericType(self, typename, extra_args=None): |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 72 | """Returns a loop template using typename for the type.""" |
| 73 | num_states = self._loops[0].A.shape[0] |
| 74 | num_inputs = self._loops[0].B.shape[1] |
| 75 | num_outputs = self._loops[0].C.shape[0] |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 76 | if extra_args is not None: |
| 77 | extra_args = ', ' + extra_args |
| 78 | else: |
| 79 | extra_args = '' |
| 80 | return '%s<%d, %d, %d%s>' % ( |
| 81 | typename, num_states, num_inputs, num_outputs, extra_args) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 82 | |
| 83 | def _ControllerType(self): |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 84 | """Returns a template name for StateFeedbackController.""" |
| 85 | return self._GenericType('StateFeedbackController') |
| 86 | |
| 87 | def _ObserverType(self): |
| 88 | """Returns a template name for StateFeedbackObserver.""" |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 89 | return self._GenericType(self._observer_type) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 90 | |
| 91 | def _LoopType(self): |
| 92 | """Returns a template name for StateFeedbackLoop.""" |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 93 | extra_args = '%s, %s' % (self._PlantType(), self._ObserverType()) |
| 94 | return self._GenericType('StateFeedbackLoop', extra_args) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 95 | |
| 96 | def _PlantType(self): |
| 97 | """Returns a template name for StateFeedbackPlant.""" |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 98 | return self._GenericType(self._plant_type) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 99 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 100 | def _PlantCoeffType(self): |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 101 | """Returns a template name for StateFeedbackPlantCoefficients.""" |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 102 | return self._GenericType(self._plant_type + 'Coefficients') |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 103 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 104 | def _ControllerCoeffType(self): |
| 105 | """Returns a template name for StateFeedbackControllerCoefficients.""" |
| 106 | return self._GenericType('StateFeedbackControllerCoefficients') |
| 107 | |
| 108 | def _ObserverCoeffType(self): |
| 109 | """Returns a template name for StateFeedbackObserverCoefficients.""" |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 110 | return self._GenericType(self._observer_type + 'Coefficients') |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 111 | |
James Kuszmaul | 0e86651 | 2014-02-21 13:12:52 -0800 | [diff] [blame] | 112 | def WriteHeader(self, header_file, double_appendage=False, MoI_ratio=0.0): |
| 113 | """Writes the header file to the file named header_file. |
| 114 | Set double_appendage to true in order to include a ratio of |
| 115 | moments of inertia constant. Currently, only used for 2014 claw.""" |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 116 | with open(header_file, 'w') as fd: |
| 117 | header_guard = self._HeaderGuard(header_file) |
| 118 | fd.write('#ifndef %s\n' |
| 119 | '#define %s\n\n' % (header_guard, header_guard)) |
| 120 | fd.write('#include \"frc971/control_loops/state_feedback_loop.h\"\n') |
Austin Schuh | 4cc4fe2 | 2017-11-23 19:13:09 -0800 | [diff] [blame^] | 121 | if (self._plant_type == 'StateFeedbackHybridPlant' or |
| 122 | self._observer_type == 'HybridKalman'): |
| 123 | fd.write('#include \"frc971/control_loops/hybrid_state_feedback_loop.h\"\n') |
| 124 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 125 | fd.write('\n') |
| 126 | |
| 127 | fd.write(self._namespace_start) |
Ben Fredrickson | 1b45f78 | 2014-02-23 07:44:36 +0000 | [diff] [blame] | 128 | |
| 129 | for const in self._constant_list: |
| 130 | fd.write(str(const)) |
| 131 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 132 | fd.write('\n\n') |
| 133 | for loop in self._loops: |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 134 | fd.write(loop.DumpPlantHeader(self._PlantCoeffType())) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 135 | fd.write('\n') |
| 136 | fd.write(loop.DumpControllerHeader()) |
| 137 | fd.write('\n') |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 138 | fd.write(loop.DumpObserverHeader(self._ObserverCoeffType())) |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 139 | fd.write('\n') |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 140 | |
| 141 | fd.write('%s Make%sPlant();\n\n' % |
| 142 | (self._PlantType(), self._gain_schedule_name)) |
| 143 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 144 | fd.write('%s Make%sController();\n\n' % |
| 145 | (self._ControllerType(), self._gain_schedule_name)) |
| 146 | |
| 147 | fd.write('%s Make%sObserver();\n\n' % |
| 148 | (self._ObserverType(), self._gain_schedule_name)) |
| 149 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 150 | fd.write('%s Make%sLoop();\n\n' % |
| 151 | (self._LoopType(), self._gain_schedule_name)) |
| 152 | |
| 153 | fd.write(self._namespace_end) |
| 154 | fd.write('\n\n') |
| 155 | fd.write("#endif // %s\n" % header_guard) |
| 156 | |
| 157 | def WriteCC(self, header_file_name, cc_file): |
| 158 | """Writes the cc file to the file named cc_file.""" |
| 159 | with open(cc_file, 'w') as fd: |
Austin Schuh | 572ff40 | 2015-11-08 12:17:50 -0800 | [diff] [blame] | 160 | fd.write('#include \"%s/%s\"\n' % |
| 161 | (os.path.join(*self._namespaces), header_file_name)) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 162 | fd.write('\n') |
| 163 | fd.write('#include <vector>\n') |
| 164 | fd.write('\n') |
| 165 | fd.write('#include \"frc971/control_loops/state_feedback_loop.h\"\n') |
| 166 | fd.write('\n') |
| 167 | fd.write(self._namespace_start) |
| 168 | fd.write('\n\n') |
| 169 | for loop in self._loops: |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 170 | fd.write(loop.DumpPlant(self._PlantCoeffType())) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 171 | fd.write('\n') |
| 172 | |
| 173 | for loop in self._loops: |
| 174 | fd.write(loop.DumpController()) |
| 175 | fd.write('\n') |
| 176 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 177 | for loop in self._loops: |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 178 | fd.write(loop.DumpObserver(self._ObserverCoeffType())) |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 179 | fd.write('\n') |
| 180 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 181 | fd.write('%s Make%sPlant() {\n' % |
| 182 | (self._PlantType(), self._gain_schedule_name)) |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 183 | fd.write(' ::std::vector< ::std::unique_ptr<%s>> plants(%d);\n' % ( |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 184 | self._PlantCoeffType(), len(self._loops))) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 185 | for index, loop in enumerate(self._loops): |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 186 | fd.write(' plants[%d] = ::std::unique_ptr<%s>(new %s(%s));\n' % |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 187 | (index, self._PlantCoeffType(), self._PlantCoeffType(), |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 188 | loop.PlantFunction())) |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 189 | fd.write(' return %s(&plants);\n' % self._PlantType()) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 190 | fd.write('}\n\n') |
| 191 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 192 | fd.write('%s Make%sController() {\n' % |
| 193 | (self._ControllerType(), self._gain_schedule_name)) |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 194 | fd.write(' ::std::vector< ::std::unique_ptr<%s>> controllers(%d);\n' % ( |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 195 | self._ControllerCoeffType(), len(self._loops))) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 196 | for index, loop in enumerate(self._loops): |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 197 | fd.write(' controllers[%d] = ::std::unique_ptr<%s>(new %s(%s));\n' % |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 198 | (index, self._ControllerCoeffType(), self._ControllerCoeffType(), |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 199 | loop.ControllerFunction())) |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 200 | fd.write(' return %s(&controllers);\n' % self._ControllerType()) |
| 201 | fd.write('}\n\n') |
| 202 | |
| 203 | fd.write('%s Make%sObserver() {\n' % |
| 204 | (self._ObserverType(), self._gain_schedule_name)) |
| 205 | fd.write(' ::std::vector< ::std::unique_ptr<%s>> observers(%d);\n' % ( |
| 206 | self._ObserverCoeffType(), len(self._loops))) |
| 207 | for index, loop in enumerate(self._loops): |
| 208 | fd.write(' observers[%d] = ::std::unique_ptr<%s>(new %s(%s));\n' % |
| 209 | (index, self._ObserverCoeffType(), self._ObserverCoeffType(), |
| 210 | loop.ObserverFunction())) |
| 211 | fd.write(' return %s(&observers);\n' % self._ObserverType()) |
| 212 | fd.write('}\n\n') |
| 213 | |
| 214 | fd.write('%s Make%sLoop() {\n' % |
| 215 | (self._LoopType(), self._gain_schedule_name)) |
| 216 | fd.write(' return %s(Make%sPlant(), Make%sController(), Make%sObserver());\n' % |
| 217 | (self._LoopType(), self._gain_schedule_name, |
| 218 | self._gain_schedule_name, self._gain_schedule_name)) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 219 | fd.write('}\n\n') |
| 220 | |
| 221 | fd.write(self._namespace_end) |
| 222 | fd.write('\n') |
| 223 | |
| 224 | |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 225 | class ControlLoop(object): |
| 226 | def __init__(self, name): |
| 227 | """Constructs a control loop object. |
| 228 | |
| 229 | Args: |
| 230 | name: string, The name of the loop to use when writing the C++ files. |
| 231 | """ |
| 232 | self._name = name |
| 233 | |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 234 | def ContinuousToDiscrete(self, A_continuous, B_continuous, dt): |
| 235 | """Calculates the discrete time values for A and B. |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 236 | |
| 237 | Args: |
| 238 | A_continuous: numpy.matrix, The continuous time A matrix |
| 239 | B_continuous: numpy.matrix, The continuous time B matrix |
| 240 | dt: float, The time step of the control loop |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 241 | |
| 242 | Returns: |
| 243 | (A, B), numpy.matrix, the control matricies. |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 244 | """ |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 245 | return controls.c2d(A_continuous, B_continuous, dt) |
| 246 | |
| 247 | def InitializeState(self): |
| 248 | """Sets X, Y, and X_hat to zero defaults.""" |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 249 | self.X = numpy.zeros((self.A.shape[0], 1)) |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 250 | self.Y = self.C * self.X |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 251 | self.X_hat = numpy.zeros((self.A.shape[0], 1)) |
| 252 | |
| 253 | def PlaceControllerPoles(self, poles): |
| 254 | """Places the controller poles. |
| 255 | |
| 256 | Args: |
| 257 | poles: array, An array of poles. Must be complex conjegates if they have |
| 258 | any imaginary portions. |
| 259 | """ |
| 260 | self.K = controls.dplace(self.A, self.B, poles) |
| 261 | |
| 262 | def PlaceObserverPoles(self, poles): |
| 263 | """Places the observer poles. |
| 264 | |
| 265 | Args: |
| 266 | poles: array, An array of poles. Must be complex conjegates if they have |
| 267 | any imaginary portions. |
| 268 | """ |
| 269 | self.L = controls.dplace(self.A.T, self.C.T, poles).T |
| 270 | |
| 271 | def Update(self, U): |
| 272 | """Simulates one time step with the provided U.""" |
Austin Schuh | 1d00573 | 2015-03-01 00:10:20 -0800 | [diff] [blame] | 273 | #U = numpy.clip(U, self.U_min, self.U_max) |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 274 | self.X = self.A * self.X + self.B * U |
| 275 | self.Y = self.C * self.X + self.D * U |
| 276 | |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 277 | def PredictObserver(self, U): |
| 278 | """Runs the predict step of the observer update.""" |
| 279 | self.X_hat = (self.A * self.X_hat + self.B * U) |
| 280 | |
| 281 | def CorrectObserver(self, U): |
| 282 | """Runs the correct step of the observer update.""" |
| 283 | self.X_hat += numpy.linalg.inv(self.A) * self.L * ( |
| 284 | self.Y - self.C * self.X_hat - self.D * U) |
| 285 | |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 286 | def UpdateObserver(self, U): |
| 287 | """Updates the observer given the provided U.""" |
| 288 | self.X_hat = (self.A * self.X_hat + self.B * U + |
| 289 | self.L * (self.Y - self.C * self.X_hat - self.D * U)) |
| 290 | |
| 291 | def _DumpMatrix(self, matrix_name, matrix): |
| 292 | """Dumps the provided matrix into a variable called matrix_name. |
| 293 | |
| 294 | Args: |
| 295 | matrix_name: string, The variable name to save the matrix to. |
| 296 | matrix: The matrix to dump. |
| 297 | |
| 298 | Returns: |
| 299 | string, The C++ commands required to populate a variable named matrix_name |
| 300 | with the contents of matrix. |
| 301 | """ |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 302 | ans = [' Eigen::Matrix<double, %d, %d> %s;\n' % ( |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 303 | matrix.shape[0], matrix.shape[1], matrix_name)] |
Brian Silverman | 0f63738 | 2013-03-03 17:44:46 -0800 | [diff] [blame] | 304 | for x in xrange(matrix.shape[0]): |
| 305 | for y in xrange(matrix.shape[1]): |
Austin Schuh | df79d11 | 2016-10-15 21:25:32 -0700 | [diff] [blame] | 306 | ans.append(' %s(%d, %d) = %s;\n' % (matrix_name, x, y, repr(matrix[x, y]))) |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 307 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 308 | return ''.join(ans) |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 309 | |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 310 | def DumpPlantHeader(self, plant_coefficient_type): |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 311 | """Writes out a c++ header declaration which will create a Plant object. |
| 312 | |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 313 | Returns: |
| 314 | string, The header declaration for the function. |
| 315 | """ |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 316 | return '%s Make%sPlantCoefficients();\n' % ( |
| 317 | plant_coefficient_type, self._name) |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 318 | |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 319 | def DumpPlant(self, plant_coefficient_type): |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 320 | """Writes out a c++ function which will create a PlantCoefficients object. |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 321 | |
| 322 | Returns: |
| 323 | string, The function which will create the object. |
| 324 | """ |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 325 | ans = ['%s Make%sPlantCoefficients() {\n' % ( |
| 326 | plant_coefficient_type, self._name)] |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 327 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 328 | ans.append(self._DumpMatrix('C', self.C)) |
| 329 | ans.append(self._DumpMatrix('D', self.D)) |
| 330 | ans.append(self._DumpMatrix('U_max', self.U_max)) |
| 331 | ans.append(self._DumpMatrix('U_min', self.U_min)) |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 332 | |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 333 | if plant_coefficient_type.startswith('StateFeedbackPlant'): |
| 334 | ans.append(self._DumpMatrix('A', self.A)) |
| 335 | ans.append(self._DumpMatrix('A_inv', numpy.linalg.inv(self.A))) |
| 336 | ans.append(self._DumpMatrix('B', self.B)) |
| 337 | ans.append(' return %s' |
| 338 | '(A, A_inv, B, C, D, U_max, U_min);\n' % ( |
| 339 | plant_coefficient_type)) |
| 340 | elif plant_coefficient_type.startswith('StateFeedbackHybridPlant'): |
| 341 | ans.append(self._DumpMatrix('A_continuous', self.A_continuous)) |
| 342 | ans.append(self._DumpMatrix('B_continuous', self.B_continuous)) |
| 343 | ans.append(' return %s' |
| 344 | '(A_continuous, B_continuous, C, D, U_max, U_min);\n' % ( |
| 345 | plant_coefficient_type)) |
| 346 | else: |
| 347 | glog.fatal('Unsupported plant type %s', plant_coefficient_type) |
| 348 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 349 | ans.append('}\n') |
| 350 | return ''.join(ans) |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 351 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 352 | def PlantFunction(self): |
| 353 | """Returns the name of the plant coefficient function.""" |
| 354 | return 'Make%sPlantCoefficients()' % self._name |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 355 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 356 | def ControllerFunction(self): |
| 357 | """Returns the name of the controller function.""" |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 358 | return 'Make%sControllerCoefficients()' % self._name |
| 359 | |
| 360 | def ObserverFunction(self): |
| 361 | """Returns the name of the controller function.""" |
| 362 | return 'Make%sObserverCoefficients()' % self._name |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 363 | |
| 364 | def DumpControllerHeader(self): |
| 365 | """Writes out a c++ header declaration which will create a Controller object. |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 366 | |
| 367 | Returns: |
| 368 | string, The header declaration for the function. |
| 369 | """ |
| 370 | num_states = self.A.shape[0] |
| 371 | num_inputs = self.B.shape[1] |
| 372 | num_outputs = self.C.shape[0] |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 373 | return 'StateFeedbackControllerCoefficients<%d, %d, %d> %s;\n' % ( |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 374 | num_states, num_inputs, num_outputs, self.ControllerFunction()) |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 375 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 376 | def DumpController(self): |
| 377 | """Returns a c++ function which will create a Controller object. |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 378 | |
| 379 | Returns: |
| 380 | string, The function which will create the object. |
| 381 | """ |
| 382 | num_states = self.A.shape[0] |
| 383 | num_inputs = self.B.shape[1] |
| 384 | num_outputs = self.C.shape[0] |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 385 | ans = ['StateFeedbackControllerCoefficients<%d, %d, %d> %s {\n' % ( |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 386 | num_states, num_inputs, num_outputs, self.ControllerFunction())] |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 387 | |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 388 | ans.append(self._DumpMatrix('K', self.K)) |
Austin Schuh | 86093ad | 2016-02-06 14:29:34 -0800 | [diff] [blame] | 389 | if not hasattr(self, 'Kff'): |
| 390 | self.Kff = numpy.matrix(numpy.zeros(self.K.shape)) |
| 391 | |
| 392 | ans.append(self._DumpMatrix('Kff', self.Kff)) |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 393 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 394 | ans.append(' return StateFeedbackControllerCoefficients<%d, %d, %d>' |
| 395 | '(K, Kff);\n' % ( |
Austin Schuh | c5fceb8 | 2017-02-25 16:24:12 -0800 | [diff] [blame] | 396 | num_states, num_inputs, num_outputs)) |
Austin Schuh | e349062 | 2013-03-13 01:24:30 -0700 | [diff] [blame] | 397 | ans.append('}\n') |
| 398 | return ''.join(ans) |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 399 | |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 400 | def DumpObserverHeader(self, observer_coefficient_type): |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 401 | """Writes out a c++ header declaration which will create a Observer object. |
| 402 | |
| 403 | Returns: |
| 404 | string, The header declaration for the function. |
| 405 | """ |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 406 | return '%s %s;\n' % ( |
| 407 | observer_coefficient_type, self.ObserverFunction()) |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 408 | |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 409 | def DumpObserver(self, observer_coefficient_type): |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 410 | """Returns a c++ function which will create a Observer object. |
| 411 | |
| 412 | Returns: |
| 413 | string, The function which will create the object. |
| 414 | """ |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 415 | ans = ['%s %s {\n' % ( |
| 416 | observer_coefficient_type, self.ObserverFunction())] |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 417 | |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 418 | if observer_coefficient_type.startswith('StateFeedbackObserver'): |
| 419 | ans.append(self._DumpMatrix('L', self.L)) |
| 420 | ans.append(' return %s(L);\n' % (observer_coefficient_type,)) |
| 421 | elif observer_coefficient_type.startswith('HybridKalman'): |
| 422 | ans.append(self._DumpMatrix('Q_continuous', self.Q_continuous)) |
| 423 | ans.append(self._DumpMatrix('R_continuous', self.R_continuous)) |
| 424 | ans.append(self._DumpMatrix('P_steady_state', self.P_steady_state)) |
| 425 | ans.append(' return %s(Q_continuous, R_continuous, P_steady_state);\n' % ( |
| 426 | observer_coefficient_type,)) |
Brian Silverman | a3a20cc | 2017-03-05 18:35:20 -0800 | [diff] [blame] | 427 | else: |
| 428 | glog.fatal('Unsupported observer type %s', observer_coefficient_type) |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 429 | |
Austin Schuh | 3250183 | 2017-02-25 18:32:56 -0800 | [diff] [blame] | 430 | ans.append('}\n') |
| 431 | return ''.join(ans) |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 432 | |
| 433 | class HybridControlLoop(ControlLoop): |
| 434 | def __init__(self, name): |
| 435 | super(HybridControlLoop, self).__init__(name=name) |
| 436 | |
Brian Silverman | 59c829a | 2017-03-05 18:36:54 -0800 | [diff] [blame] | 437 | def Discretize(self, dt): |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 438 | [self.A, self.B, self.Q, self.R] = \ |
| 439 | controls.kalmd(self.A_continuous, self.B_continuous, |
| 440 | self.Q_continuous, self.R_continuous, dt) |
| 441 | |
| 442 | def PredictHybridObserver(self, U, dt): |
Brian Silverman | 59c829a | 2017-03-05 18:36:54 -0800 | [diff] [blame] | 443 | self.Discretize(dt) |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 444 | self.X_hat = self.A * self.X_hat + self.B * U |
| 445 | self.P = (self.A * self.P * self.A.T + self.Q) |
| 446 | |
| 447 | def CorrectHybridObserver(self, U): |
| 448 | Y_bar = self.Y - self.C * self.X_hat |
| 449 | C_t = self.C.T |
| 450 | S = self.C * self.P * C_t + self.R |
| 451 | self.KalmanGain = self.P * C_t * numpy.linalg.inv(S) |
| 452 | self.X_hat = self.X_hat + self.KalmanGain * Y_bar |
| 453 | self.P = (numpy.eye(len(self.A)) - self.KalmanGain * self.C) * self.P |
| 454 | |
| 455 | def InitializeState(self): |
| 456 | super(HybridControlLoop, self).InitializeState() |
| 457 | if hasattr(self, 'Q_steady_state'): |
| 458 | self.P = self.Q_steady_state |
| 459 | else: |
| 460 | self.P = numpy.matrix(numpy.zeros((self.A.shape[0], self.A.shape[0]))) |