blob: 3d6b9fcbdce9a89f6a4931aa837e582a0ec72529 [file] [log] [blame]
Austin Schuhc8ca2442013-02-23 12:29:33 -08001#!/usr/bin/python
2
Austin Schuh3c542312013-02-24 01:53:50 -08003import control_loop
Austin Schuhc8ca2442013-02-23 12:29:33 -08004import numpy
Austin Schuhc8ca2442013-02-23 12:29:33 -08005import sys
Austin Schuhc8ca2442013-02-23 12:29:33 -08006from matplotlib import pylab
Austin Schuhc8ca2442013-02-23 12:29:33 -08007
Austin Schuh3bb9a442014-02-02 16:01:45 -08008class Claw(control_loop.ControlLoop):
9 def __init__(self, name="RawClaw"):
10 super(Claw, self).__init__(name)
Austin Schuhc8ca2442013-02-23 12:29:33 -080011 # Stall Torque in N m
12 self.stall_torque = 1.4
13 # Stall Current in Amps
14 self.stall_current = 86
15 # Free Speed in RPM
16 self.free_speed = 6200.0
Austin Schuh3c542312013-02-24 01:53:50 -080017 # Free Current in Amps
18 self.free_current = 1.5
Austin Schuh3bb9a442014-02-02 16:01:45 -080019 # Moment of inertia of the claw in kg m^2
Austin Schuh683a0d02013-03-02 01:51:31 -080020 # TODO(aschuh): Measure this in reality. It doesn't seem high enough.
21 # James measured 0.51, but that can't be right given what I am seeing.
Brian Silverman0f637382013-03-03 17:44:46 -080022 self.J = 2.0
Austin Schuhc8ca2442013-02-23 12:29:33 -080023 # Resistance of the motor
24 self.R = 12.0 / self.stall_current + 0.024 + .003
25 # Motor velocity constant
Austin Schuh3c542312013-02-24 01:53:50 -080026 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
27 (13.5 - self.R * self.free_current))
Austin Schuhc8ca2442013-02-23 12:29:33 -080028 # Torque constant
29 self.Kt = self.stall_torque / self.stall_current
30 # Gear ratio
31 self.G = 1.0 / ((84.0 / 20.0) * (50.0 / 14.0) * (40.0 / 14.0) * (40.0 / 12.0))
32 # Control loop time step
33 self.dt = 0.01
34
35 # State feedback matrices
36 self.A_continuous = numpy.matrix(
37 [[0, 1],
38 [0, -self.Kt / self.Kv / (self.J * self.G * self.G * self.R)]])
39 self.B_continuous = numpy.matrix(
40 [[0],
41 [self.Kt / (self.J * self.G * self.R)]])
42 self.C = numpy.matrix([[1, 0]])
43 self.D = numpy.matrix([[0]])
44
Austin Schuhc1f68892013-03-16 17:06:27 -070045 self.A, self.B = self.ContinuousToDiscrete(
46 self.A_continuous, self.B_continuous, self.dt)
Austin Schuhc8ca2442013-02-23 12:29:33 -080047
Austin Schuhc1f68892013-03-16 17:06:27 -070048 self.PlaceControllerPoles([0.85, 0.45])
Austin Schuhc8ca2442013-02-23 12:29:33 -080049
50 self.rpl = .05
51 self.ipl = 0.008
Austin Schuh3c542312013-02-24 01:53:50 -080052 self.PlaceObserverPoles([self.rpl + 1j * self.ipl,
53 self.rpl - 1j * self.ipl])
Austin Schuhc8ca2442013-02-23 12:29:33 -080054
55 self.U_max = numpy.matrix([[12.0]])
56 self.U_min = numpy.matrix([[-12.0]])
Austin Schuhc8ca2442013-02-23 12:29:33 -080057
Austin Schuhc1f68892013-03-16 17:06:27 -070058 self.InitializeState()
59
60
Austin Schuh3bb9a442014-02-02 16:01:45 -080061class ClawDeltaU(Claw):
62 def __init__(self, name="Claw"):
63 super(ClawDeltaU, self).__init__(name)
Austin Schuhc1f68892013-03-16 17:06:27 -070064 A_unaugmented = self.A
65 B_unaugmented = self.B
66
67 self.A = numpy.matrix([[0.0, 0.0, 0.0],
68 [0.0, 0.0, 0.0],
69 [0.0, 0.0, 1.0]])
70 self.A[0:2, 0:2] = A_unaugmented
71 self.A[0:2, 2] = B_unaugmented
72
73 self.B = numpy.matrix([[0.0],
74 [0.0],
75 [1.0]])
76
77 self.C = numpy.matrix([[1.0, 0.0, 0.0]])
78 self.D = numpy.matrix([[0.0]])
79
Brian Silverman7fb8e222013-03-16 20:09:58 -070080 self.PlaceControllerPoles([0.55, 0.35, 0.80])
Austin Schuhc1f68892013-03-16 17:06:27 -070081
82 print "K"
83 print self.K
84 print "Placed controller poles are"
85 print numpy.linalg.eig(self.A - self.B * self.K)[0]
86
87 self.rpl = .05
88 self.ipl = 0.008
89 self.PlaceObserverPoles([self.rpl + 1j * self.ipl,
Brian Silverman23a67ca2013-03-16 23:48:50 -070090 self.rpl - 1j * self.ipl, 0.90])
Austin Schuhc1f68892013-03-16 17:06:27 -070091 print "Placed observer poles are"
92 print numpy.linalg.eig(self.A - self.L * self.C)[0]
93
94 self.U_max = numpy.matrix([[12.0]])
95 self.U_min = numpy.matrix([[-12.0]])
96
97 self.InitializeState()
98
99
Austin Schuh3bb9a442014-02-02 16:01:45 -0800100def ClipDeltaU(claw, delta_u):
101 old_u = numpy.matrix([[claw.X[2, 0]]])
102 new_u = numpy.clip(old_u + delta_u, claw.U_min, claw.U_max)
Austin Schuhc1f68892013-03-16 17:06:27 -0700103 return new_u - old_u
104
Austin Schuhc8ca2442013-02-23 12:29:33 -0800105def main(argv):
Austin Schuh3c542312013-02-24 01:53:50 -0800106 # Simulate the response of the system to a step input.
Austin Schuh3bb9a442014-02-02 16:01:45 -0800107 claw = ClawDeltaU()
Austin Schuhc8ca2442013-02-23 12:29:33 -0800108 simulated_x = []
109 for _ in xrange(100):
Austin Schuh3bb9a442014-02-02 16:01:45 -0800110 claw.Update(numpy.matrix([[12.0]]))
111 simulated_x.append(claw.X[0, 0])
Austin Schuhc8ca2442013-02-23 12:29:33 -0800112
Austin Schuh3c542312013-02-24 01:53:50 -0800113 pylab.plot(range(100), simulated_x)
114 pylab.show()
Austin Schuhc8ca2442013-02-23 12:29:33 -0800115
Austin Schuh3c542312013-02-24 01:53:50 -0800116 # Simulate the closed loop response of the system to a step input.
Austin Schuh3bb9a442014-02-02 16:01:45 -0800117 top_claw = ClawDeltaU("TopClaw")
Austin Schuhc8ca2442013-02-23 12:29:33 -0800118 close_loop_x = []
Austin Schuhc1f68892013-03-16 17:06:27 -0700119 close_loop_u = []
120 R = numpy.matrix([[1.0], [0.0], [0.0]])
Austin Schuh3bb9a442014-02-02 16:01:45 -0800121 top_claw.X[2, 0] = -5
Austin Schuhc8ca2442013-02-23 12:29:33 -0800122 for _ in xrange(100):
Austin Schuh3bb9a442014-02-02 16:01:45 -0800123 U = numpy.clip(top_claw.K * (R - top_claw.X_hat), top_claw.U_min, top_claw.U_max)
124 U = ClipDeltaU(top_claw, U)
125 top_claw.UpdateObserver(U)
126 top_claw.Update(U)
127 close_loop_x.append(top_claw.X[0, 0] * 10)
128 close_loop_u.append(top_claw.X[2, 0])
Austin Schuhc8ca2442013-02-23 12:29:33 -0800129
Austin Schuhfa033692013-02-24 01:00:55 -0800130 pylab.plot(range(100), close_loop_x)
Austin Schuhc1f68892013-03-16 17:06:27 -0700131 pylab.plot(range(100), close_loop_u)
Austin Schuhfa033692013-02-24 01:00:55 -0800132 pylab.show()
Austin Schuhc8ca2442013-02-23 12:29:33 -0800133
Austin Schuh3c542312013-02-24 01:53:50 -0800134 # Write the generated constants out to a file.
Austin Schuh3bb9a442014-02-02 16:01:45 -0800135 if len(argv) != 9:
Austin Schuhc1f68892013-03-16 17:06:27 -0700136 print "Expected .h file name and .cc file name for"
137 print "both the plant and unaugmented plant"
Austin Schuhc8ca2442013-02-23 12:29:33 -0800138 else:
Austin Schuh3bb9a442014-02-02 16:01:45 -0800139 top_unaug_claw = Claw("RawTopClaw")
140 top_unaug_loop_writer = control_loop.ControlLoopWriter("RawTopClaw",
141 [top_unaug_claw])
Austin Schuh683a0d02013-03-02 01:51:31 -0800142 if argv[1][-3:] == '.cc':
Austin Schuh3bb9a442014-02-02 16:01:45 -0800143 top_unaug_loop_writer.Write(argv[2], argv[1])
Austin Schuh683a0d02013-03-02 01:51:31 -0800144 else:
Austin Schuh3bb9a442014-02-02 16:01:45 -0800145 top_unaug_loop_writer.Write(argv[1], argv[2])
146
147 top_loop_writer = control_loop.ControlLoopWriter("TopClaw", [top_claw])
148 if argv[3][-3:] == '.cc':
149 top_loop_writer.Write(argv[4], argv[3])
150 else:
151 top_loop_writer.Write(argv[3], argv[4])
152
153 bottom_claw = ClawDeltaU("BottomClaw")
154 bottom_unaug_claw = Claw("RawBottomClaw")
155 bottom_unaug_loop_writer = control_loop.ControlLoopWriter(
156 "RawBottomClaw", [bottom_unaug_claw])
157 if argv[5][-3:] == '.cc':
158 bottom_unaug_loop_writer.Write(argv[6], argv[5])
159 else:
160 bottom_unaug_loop_writer.Write(argv[5], argv[6])
161
162 bottom_loop_writer = control_loop.ControlLoopWriter("BottomClaw",
163 [bottom_claw])
164 if argv[7][-3:] == '.cc':
165 bottom_loop_writer.Write(argv[8], argv[7])
166 else:
167 bottom_loop_writer.Write(argv[7], argv[8])
Austin Schuhc8ca2442013-02-23 12:29:33 -0800168
169if __name__ == '__main__':
170 sys.exit(main(sys.argv))