blob: a2e46e4f20ed44e09684f8d54b04255ada01cc3a [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
45 self.K = controls.dplace(self.A, self.B, [.95, .92])
46
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
119
120def main(argv):
121 wrist = Wrist()
122 simulated_x = []
123 for _ in xrange(100):
124 wrist.Update(numpy.matrix([[12.0]]))
125 simulated_x.append(wrist.X[0, 0])
126
127 pylab.plot(range(100), simulated_x)
128 pylab.show()
129
130 wrist = Wrist()
131 close_loop_x = []
132 X_hat = numpy.matrix([[0.0], [0.0]])
133 R = numpy.matrix([[1.0], [0.0]])
134 for _ in xrange(100):
135 U = numpy.clip(wrist.K * (R - X_hat), wrist.U_min, wrist.U_max)
136 X_hat = wrist.A * X_hat + wrist.B * U + wrist.L * (wrist.Y - wrist.C * X_hat - wrist.D * U)
137 wrist.Update(U)
138 close_loop_x.append(wrist.X[0, 0])
139
140 pylab.plot(range(100), close_loop_x)
141 pylab.show()
142
143 if len(argv) != 3:
144 print "Expected .cc file name and .h file name"
145 else:
146 namespace_start = ("namespace frc971 {\n"
147 "namespace control_loops {\n\n");
148
149 namespace_end = ("} // namespace frc971\n"
150 "} // namespace control_loops\n");
151
152 header_start = ("#ifndef FRC971_CONTROL_LOOPS_WRIST_MOTOR_PLANT_H_\n"
153 "#define // FRC971_CONTROL_LOOPS_WRIST_MOTOR_PLANT_H_\n\n")
154 header_end = "#endif // FRC971_CONTROL_LOOPS_WRIST_MOTOR_PLANT_H_\n";
155
156 with open(argv[1], "w") as fd:
157 fd.write(namespace_start)
158 fd.write(wrist.DumpPlant("Wrist"))
159 fd.write('\n')
160 fd.write(namespace_end)
161
162 with open(argv[2], "w") as fd:
163 fd.write(header_start)
164 fd.write(namespace_start)
165 fd.write("#include \"frc971/control_loops/state_feedback_loop.h\"\n")
166 fd.write('\n')
167 fd.write(wrist.DumpPlantHeader("Wrist"))
168 fd.write('\n')
169 fd.write(namespace_end)
170 fd.write(header_end)
171
172
173if __name__ == '__main__':
174 sys.exit(main(sys.argv))