blob: e239ac7963c0caad0a43111d5e9b2da3d832adb3 [file] [log] [blame]
Austin Schuhc8ca2442013-02-23 12:29:33 -08001#!/usr/bin/python
2
3import numpy
4import string
5import sys
6import polytope
7from matplotlib import pylab
8import controls
9
10
11class Wrist(object):
12 def __init__(self):
13 # Stall Torque in N m
14 self.stall_torque = 1.4
15 # Stall Current in Amps
16 self.stall_current = 86
17 # Free Speed in RPM
18 self.free_speed = 6200.0
19 # Moment of inertia of the wrist in kg m^2
20 self.J = 0.51
21 # Resistance of the motor
22 self.R = 12.0 / self.stall_current + 0.024 + .003
23 # Motor velocity constant
24 self.Kv = (self.free_speed / 60.0 * 2.0 * numpy.pi) / (13.5 - self.R * 1.5)
25 # Torque constant
26 self.Kt = self.stall_torque / self.stall_current
27 # Gear ratio
28 self.G = 1.0 / ((84.0 / 20.0) * (50.0 / 14.0) * (40.0 / 14.0) * (40.0 / 12.0))
29 # Control loop time step
30 self.dt = 0.01
31
32 # State feedback matrices
33 self.A_continuous = numpy.matrix(
34 [[0, 1],
35 [0, -self.Kt / self.Kv / (self.J * self.G * self.G * self.R)]])
36 self.B_continuous = numpy.matrix(
37 [[0],
38 [self.Kt / (self.J * self.G * self.R)]])
39 self.C = numpy.matrix([[1, 0]])
40 self.D = numpy.matrix([[0]])
41
42 self.A, self.B = controls.c2d(
43 self.A_continuous, self.B_continuous, self.dt)
44
Austin Schuhfa033692013-02-24 01:00:55 -080045 self.K = controls.dplace(self.A, self.B, [.89, .85])
Austin Schuhc8ca2442013-02-23 12:29:33 -080046
47 self.rpl = .05
48 self.ipl = 0.008
49 self.L = controls.dplace(self.A.T, self.C.T,
50 [self.rpl + 1j * self.ipl,
51 self.rpl - 1j * self.ipl]).T
52
53 self.X = numpy.matrix([[0],
54 [0]])
55
56 self.U_max = numpy.matrix([[12.0]])
57 self.U_min = numpy.matrix([[-12.0]])
58 self.Y = self.C * self.X
59
60 def Update(self, U):
61 U = numpy.clip(U, self.U_min, self.U_max)
62 self.X = self.A * self.X + self.B * U
63 self.Y = self.C * self.X + self.D * U
64
65 def _DumpMatrix(self, matrix_name, matrix):
66 ans = [" Eigen::Matrix<double, %d, %d> %s;\n" % (
67 matrix.shape[0], matrix.shape[1], matrix_name)]
68 first = True
69 for element in numpy.nditer(matrix, order='C'):
70 if first:
71 ans.append(" %s << " % matrix_name)
72 first = False
73 else:
74 ans.append(", ")
75 ans.append(str(element))
76
77 ans.append(";\n")
78 return "".join(ans)
79
80 def DumpPlantHeader(self, plant_name):
81 """Writes out a c++ header declaration which will create a Plant object.
82
83 Args:
84 plant_name: string, the name of the plant. Used to create the name of the
85 function. The function name will be Make<plant_name>Plant().
86 """
87 num_states = self.A.shape[0]
88 num_inputs = self.B.shape[1]
89 num_outputs = self.C.shape[0]
90 return "StateFeedbackPlant<%d, %d, %d> Make%sPlant();\n" % (
91 num_states, num_inputs, num_outputs, plant_name)
92
93 def DumpPlant(self, plant_name):
94 """Writes out a c++ function which will create a Plant object.
95
96 Args:
97 plant_name: string, the name of the plant. Used to create the name of the
98 function. The function name will be Make<plant_name>Plant().
99 """
100 num_states = self.A.shape[0]
101 num_inputs = self.B.shape[1]
102 num_outputs = self.C.shape[0]
103 ans = ["StateFeedbackPlant<%d, %d, %d> Make%sPlant() {\n" % (
104 num_states, num_inputs, num_outputs, plant_name)]
105
106 ans.append(self._DumpMatrix("A", self.A))
107 ans.append(self._DumpMatrix("B", self.B))
108 ans.append(self._DumpMatrix("C", self.C))
109 ans.append(self._DumpMatrix("D", self.D))
110 ans.append(self._DumpMatrix("U_max", self.U_max))
111 ans.append(self._DumpMatrix("U_min", self.U_min))
112
113 ans.append(" return StateFeedbackPlant<%d, %d, %d>"
114 "(A, B, C, D, U_max, U_min);\n" % (num_states, num_inputs,
115 num_outputs))
116 ans.append("}\n")
117 return "".join(ans)
118
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800119 def DumpLoopHeader(self, loop_name):
120 """Writes out a c++ header declaration which will create a Loop object.
121
122 Args:
123 loop_name: string, the name of the loop. Used to create the name of the
124 function. The function name will be Make<loop_name>Loop().
125 """
126 num_states = self.A.shape[0]
127 num_inputs = self.B.shape[1]
128 num_outputs = self.C.shape[0]
129 return "StateFeedbackLoop<%d, %d, %d> Make%sLoop();\n" % (
130 num_states, num_inputs, num_outputs, loop_name)
131
132 def DumpLoop(self, loop_name):
133 """Writes out a c++ function which will create a Loop object.
134
135 Args:
136 loop_name: string, the name of the loop. Used to create the name of the
137 function and create the plant. The function name will be
138 Make<loop_name>Loop().
139 """
140 num_states = self.A.shape[0]
141 num_inputs = self.B.shape[1]
142 num_outputs = self.C.shape[0]
143 ans = ["StateFeedbackLoop<%d, %d, %d> Make%sLoop() {\n" % (
144 num_states, num_inputs, num_outputs, loop_name)]
145
146 ans.append(self._DumpMatrix("L", self.L))
147 ans.append(self._DumpMatrix("K", self.K))
148
149 ans.append(" return StateFeedbackLoop<%d, %d, %d>"
Austin Schuhfa033692013-02-24 01:00:55 -0800150 "(L, K, Make%sPlant());\n" % (num_states, num_inputs,
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800151 num_outputs, loop_name))
152 ans.append("}\n")
153 return "".join(ans)
154
Austin Schuhc8ca2442013-02-23 12:29:33 -0800155
156def main(argv):
157 wrist = Wrist()
158 simulated_x = []
159 for _ in xrange(100):
160 wrist.Update(numpy.matrix([[12.0]]))
161 simulated_x.append(wrist.X[0, 0])
162
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800163 #pylab.plot(range(100), simulated_x)
164 #pylab.show()
Austin Schuhc8ca2442013-02-23 12:29:33 -0800165
166 wrist = Wrist()
167 close_loop_x = []
168 X_hat = numpy.matrix([[0.0], [0.0]])
169 R = numpy.matrix([[1.0], [0.0]])
170 for _ in xrange(100):
171 U = numpy.clip(wrist.K * (R - X_hat), wrist.U_min, wrist.U_max)
172 X_hat = wrist.A * X_hat + wrist.B * U + wrist.L * (wrist.Y - wrist.C * X_hat - wrist.D * U)
173 wrist.Update(U)
174 close_loop_x.append(wrist.X[0, 0])
175
Austin Schuhfa033692013-02-24 01:00:55 -0800176 pylab.plot(range(100), close_loop_x)
177 pylab.show()
Austin Schuhc8ca2442013-02-23 12:29:33 -0800178
179 if len(argv) != 3:
180 print "Expected .cc file name and .h file name"
181 else:
182 namespace_start = ("namespace frc971 {\n"
183 "namespace control_loops {\n\n");
184
185 namespace_end = ("} // namespace frc971\n"
186 "} // namespace control_loops\n");
187
188 header_start = ("#ifndef FRC971_CONTROL_LOOPS_WRIST_MOTOR_PLANT_H_\n"
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800189 "#define FRC971_CONTROL_LOOPS_WRIST_MOTOR_PLANT_H_\n\n")
Austin Schuhc8ca2442013-02-23 12:29:33 -0800190 header_end = "#endif // FRC971_CONTROL_LOOPS_WRIST_MOTOR_PLANT_H_\n";
191
192 with open(argv[1], "w") as fd:
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800193 fd.write("#include \"frc971/control_loops/wrist_motor_plant.h\"\n")
194 fd.write('\n')
195 fd.write("#include \"frc971/control_loops/state_feedback_loop.h\"\n")
196 fd.write('\n')
Austin Schuhc8ca2442013-02-23 12:29:33 -0800197 fd.write(namespace_start)
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800198 fd.write('\n')
Austin Schuhc8ca2442013-02-23 12:29:33 -0800199 fd.write(wrist.DumpPlant("Wrist"))
200 fd.write('\n')
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800201 fd.write(wrist.DumpLoop("Wrist"))
202 fd.write('\n')
Austin Schuhc8ca2442013-02-23 12:29:33 -0800203 fd.write(namespace_end)
204
205 with open(argv[2], "w") as fd:
206 fd.write(header_start)
Austin Schuhc8ca2442013-02-23 12:29:33 -0800207 fd.write("#include \"frc971/control_loops/state_feedback_loop.h\"\n")
208 fd.write('\n')
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800209 fd.write(namespace_start)
Austin Schuhc8ca2442013-02-23 12:29:33 -0800210 fd.write(wrist.DumpPlantHeader("Wrist"))
211 fd.write('\n')
Austin Schuhdc1c84a2013-02-23 16:33:10 -0800212 fd.write(wrist.DumpLoopHeader("Wrist"))
213 fd.write('\n')
Austin Schuhc8ca2442013-02-23 12:29:33 -0800214 fd.write(namespace_end)
215 fd.write(header_end)
216
217
218if __name__ == '__main__':
219 sys.exit(main(sys.argv))