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