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