blob: 832d4ccdccf5cc8c12a8e6afc3decca287f9a77c [file] [log] [blame]
James Kuszmauladab8f92013-11-06 08:42:34 -08001import sys
2sys.path.append('../../frc971/control_loops/python')
3import controls
4import numpy
5
6class ControlLoopWriter(object):
7 def __init__(self, gain_schedule_name, loops, namespaces=None):
8 """Constructs a control loop writer.
9
10 Args:
11 gain_schedule_name: string, Name of the overall controller.
12 loops: array[ControlLoop], a list of control loops to gain schedule
13 in order.
14 namespaces: array[string], a list of names of namespaces to nest in
15 order. If None, the default will be used.
16 """
17 self._gain_schedule_name = gain_schedule_name
18 self._loops = loops
19 if namespaces:
20 self._namespaces = namespaces
21 else:
22 self._namespaces = ['bot3', 'control_loops']
23
24 self._namespace_start = '\n'.join(
25 ['namespace %s {' % name for name in self._namespaces])
26
27 self._namespace_end = '\n'.join(
28 ['} // namespace %s' % name for name in reversed(self._namespaces)])
29
30 def _HeaderGuard(self, header_file):
31 return ('BOT3_CONTROL_LOOPS_' +
32 header_file.upper().replace('.', '_').replace('/', '_') +
33 '_')
34
35 def Write(self, header_file, cc_file):
36 """Writes the loops to the specified files."""
37 self.WriteHeader(header_file)
38 self.WriteCC(header_file, cc_file)
39
40 def _GenericType(self, typename):
41 """Returns a loop template using typename for the type."""
42 num_states = self._loops[0].A.shape[0]
43 num_inputs = self._loops[0].B.shape[1]
44 num_outputs = self._loops[0].C.shape[0]
45 return '%s<%d, %d, %d>' % (
46 typename, num_states, num_inputs, num_outputs)
47
48 def _ControllerType(self):
49 """Returns a template name for StateFeedbackController."""
50 return self._GenericType('StateFeedbackController')
51
52 def _LoopType(self):
53 """Returns a template name for StateFeedbackLoop."""
54 return self._GenericType('StateFeedbackLoop')
55
56 def _PlantType(self):
57 """Returns a template name for StateFeedbackPlant."""
58 return self._GenericType('StateFeedbackPlant')
59
60 def _CoeffType(self):
61 """Returns a template name for StateFeedbackPlantCoefficients."""
62 return self._GenericType('StateFeedbackPlantCoefficients')
63
64 def WriteHeader(self, header_file):
65 """Writes the header file to the file named header_file."""
66 with open(header_file, 'w') as fd:
67 header_guard = self._HeaderGuard(header_file)
68 fd.write('#ifndef %s\n'
69 '#define %s\n\n' % (header_guard, header_guard))
70 fd.write('#include \"frc971/control_loops/state_feedback_loop.h\"\n')
71 fd.write('\n')
72
73 fd.write(self._namespace_start)
74 fd.write('\n\n')
75 for loop in self._loops:
76 fd.write(loop.DumpPlantHeader())
77 fd.write('\n')
78 fd.write(loop.DumpControllerHeader())
79 fd.write('\n')
80
81 fd.write('%s Make%sPlant();\n\n' %
82 (self._PlantType(), self._gain_schedule_name))
83
84 fd.write('%s Make%sLoop();\n\n' %
85 (self._LoopType(), self._gain_schedule_name))
86
87 fd.write(self._namespace_end)
88 fd.write('\n\n')
89 fd.write("#endif // %s\n" % header_guard)
90
91 def WriteCC(self, header_file_name, cc_file):
92 """Writes the cc file to the file named cc_file."""
93 with open(cc_file, 'w') as fd:
94 fd.write('#include \"bot3/control_loops/%s\"\n' % header_file_name)
95 fd.write('\n')
96 fd.write('#include <vector>\n')
97 fd.write('\n')
98 fd.write('#include \"frc971/control_loops/state_feedback_loop.h\"\n')
99 fd.write('\n')
100 fd.write(self._namespace_start)
101 fd.write('\n\n')
102 for loop in self._loops:
103 fd.write(loop.DumpPlant())
104 fd.write('\n')
105
106 for loop in self._loops:
107 fd.write(loop.DumpController())
108 fd.write('\n')
109
110 fd.write('%s Make%sPlant() {\n' %
111 (self._PlantType(), self._gain_schedule_name))
112 fd.write(' ::std::vector<%s *> plants(%d);\n' % (
113 self._CoeffType(), len(self._loops)))
114 for index, loop in enumerate(self._loops):
115 fd.write(' plants[%d] = new %s(%s);\n' %
116 (index, self._CoeffType(),
117 loop.PlantFunction()))
118 fd.write(' return %s(plants);\n' % self._PlantType())
119 fd.write('}\n\n')
120
121 fd.write('%s Make%sLoop() {\n' %
122 (self._LoopType(), self._gain_schedule_name))
123 fd.write(' ::std::vector<%s *> controllers(%d);\n' % (
124 self._ControllerType(), len(self._loops)))
125 for index, loop in enumerate(self._loops):
126 fd.write(' controllers[%d] = new %s(%s);\n' %
127 (index, self._ControllerType(),
128 loop.ControllerFunction()))
129 fd.write(' return %s(controllers);\n' % self._LoopType())
130 fd.write('}\n\n')
131
132 fd.write(self._namespace_end)
133 fd.write('\n')
134
135
136class ControlLoop(object):
137 def __init__(self, name):
138 """Constructs a control loop object.
139
140 Args:
141 name: string, The name of the loop to use when writing the C++ files.
142 """
143 self._name = name
144
145 def ContinuousToDiscrete(self, A_continuous, B_continuous, dt):
146 """Calculates the discrete time values for A and B.
147
148 Args:
149 A_continuous: numpy.matrix, The continuous time A matrix
150 B_continuous: numpy.matrix, The continuous time B matrix
151 dt: float, The time step of the control loop
152
153 Returns:
154 (A, B), numpy.matrix, the control matricies.
155 """
156 return controls.c2d(A_continuous, B_continuous, dt)
157
158 def InitializeState(self):
159 """Sets X, Y, and X_hat to zero defaults."""
160 self.X = numpy.zeros((self.A.shape[0], 1))
161 self.Y = self.C * self.X
162 self.X_hat = numpy.zeros((self.A.shape[0], 1))
163
164 def PlaceControllerPoles(self, poles):
165 """Places the controller poles.
166
167 Args:
168 poles: array, An array of poles. Must be complex conjegates if they have
169 any imaginary portions.
170 """
171 self.K = controls.dplace(self.A, self.B, poles)
172
173 def PlaceObserverPoles(self, poles):
174 """Places the observer poles.
175
176 Args:
177 poles: array, An array of poles. Must be complex conjegates if they have
178 any imaginary portions.
179 """
180 self.L = controls.dplace(self.A.T, self.C.T, poles).T
181
182 def Update(self, U):
183 """Simulates one time step with the provided U."""
184 U = numpy.clip(U, self.U_min, self.U_max)
185 self.X = self.A * self.X + self.B * U
186 self.Y = self.C * self.X + self.D * U
187
188 def UpdateObserver(self, U):
189 """Updates the observer given the provided U."""
190 self.X_hat = (self.A * self.X_hat + self.B * U +
191 self.L * (self.Y - self.C * self.X_hat - self.D * U))
192
193 def _DumpMatrix(self, matrix_name, matrix):
194 """Dumps the provided matrix into a variable called matrix_name.
195
196 Args:
197 matrix_name: string, The variable name to save the matrix to.
198 matrix: The matrix to dump.
199
200 Returns:
201 string, The C++ commands required to populate a variable named matrix_name
202 with the contents of matrix.
203 """
204 ans = [' Eigen::Matrix<double, %d, %d> %s;\n' % (
205 matrix.shape[0], matrix.shape[1], matrix_name)]
206 first = True
207 for x in xrange(matrix.shape[0]):
208 for y in xrange(matrix.shape[1]):
209 element = matrix[x, y]
210 if first:
211 ans.append(' %s << ' % matrix_name)
212 first = False
213 else:
214 ans.append(', ')
215 ans.append(str(element))
216
217 ans.append(';\n')
218 return ''.join(ans)
219
220 def DumpPlantHeader(self):
221 """Writes out a c++ header declaration which will create a Plant object.
222
223 Returns:
224 string, The header declaration for the function.
225 """
226 num_states = self.A.shape[0]
227 num_inputs = self.B.shape[1]
228 num_outputs = self.C.shape[0]
229 return 'StateFeedbackPlantCoefficients<%d, %d, %d> Make%sPlantCoefficients();\n' % (
230 num_states, num_inputs, num_outputs, self._name)
231
232 def DumpPlant(self):
233 """Writes out a c++ function which will create a PlantCoefficients object.
234
235 Returns:
236 string, The function which will create the object.
237 """
238 num_states = self.A.shape[0]
239 num_inputs = self.B.shape[1]
240 num_outputs = self.C.shape[0]
241 ans = ['StateFeedbackPlantCoefficients<%d, %d, %d>'
242 ' Make%sPlantCoefficients() {\n' % (
243 num_states, num_inputs, num_outputs, self._name)]
244
245 ans.append(self._DumpMatrix('A', self.A))
246 ans.append(self._DumpMatrix('B', self.B))
247 ans.append(self._DumpMatrix('C', self.C))
248 ans.append(self._DumpMatrix('D', self.D))
249 ans.append(self._DumpMatrix('U_max', self.U_max))
250 ans.append(self._DumpMatrix('U_min', self.U_min))
251
252 ans.append(' return StateFeedbackPlantCoefficients<%d, %d, %d>'
253 '(A, B, C, D, U_max, U_min);\n' % (num_states, num_inputs,
254 num_outputs))
255 ans.append('}\n')
256 return ''.join(ans)
257
258 def PlantFunction(self):
259 """Returns the name of the plant coefficient function."""
260 return 'Make%sPlantCoefficients()' % self._name
261
262 def ControllerFunction(self):
263 """Returns the name of the controller function."""
264 return 'Make%sController()' % self._name
265
266 def DumpControllerHeader(self):
267 """Writes out a c++ header declaration which will create a Controller object.
268
269 Returns:
270 string, The header declaration for the function.
271 """
272 num_states = self.A.shape[0]
273 num_inputs = self.B.shape[1]
274 num_outputs = self.C.shape[0]
275 return 'StateFeedbackController<%d, %d, %d> %s;\n' % (
276 num_states, num_inputs, num_outputs, self.ControllerFunction())
277
278 def DumpController(self):
279 """Returns a c++ function which will create a Controller object.
280
281 Returns:
282 string, The function which will create the object.
283 """
284 num_states = self.A.shape[0]
285 num_inputs = self.B.shape[1]
286 num_outputs = self.C.shape[0]
287 ans = ['StateFeedbackController<%d, %d, %d> %s {\n' % (
288 num_states, num_inputs, num_outputs, self.ControllerFunction())]
289
290 ans.append(self._DumpMatrix('L', self.L))
291 ans.append(self._DumpMatrix('K', self.K))
292
293 ans.append(' return StateFeedbackController<%d, %d, %d>'
294 '(L, K, Make%sPlantCoefficients());\n' % (num_states, num_inputs,
295 num_outputs, self._name))
296 ans.append('}\n')
297 return ''.join(ans)