blob: e2e32e6095eff1e52a90cfe44815635e8b590a98 [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
James Kuszmaule1755b32014-02-13 06:27:48 -080012 self.stall_torque = 2.42
Austin Schuhc8ca2442013-02-23 12:29:33 -080013 # Stall Current in Amps
James Kuszmaule1755b32014-02-13 06:27:48 -080014 self.stall_current = 133
15 # Free Speed in RPM, pulled from drivetrain
16 self.free_speed = 4650.0
Austin Schuh3c542312013-02-24 01:53:50 -080017 # Free Current in Amps
James Kuszmaule1755b32014-02-13 06:27:48 -080018 self.free_current = 2.7
Austin Schuh3bb9a442014-02-02 16:01:45 -080019 # Moment of inertia of the claw in kg m^2
James Kuszmaule1755b32014-02-13 06:27:48 -080020 # approzimately 0.76 (without ball) in CAD
21 self.J = 0.76
Austin Schuhc8ca2442013-02-23 12:29:33 -080022 # Resistance of the motor
23 self.R = 12.0 / self.stall_current + 0.024 + .003
24 # Motor velocity constant
Austin Schuh3c542312013-02-24 01:53:50 -080025 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
26 (13.5 - self.R * self.free_current))
Austin Schuhc8ca2442013-02-23 12:29:33 -080027 # Torque constant
28 self.Kt = self.stall_torque / self.stall_current
29 # Gear ratio
James Kuszmaule1755b32014-02-13 06:27:48 -080030 self.G = 14.0 / 48.0 * 18.0 / 32.0 * 18.0 / 66.0 * 12.0 / 60.0
Austin Schuhc8ca2442013-02-23 12:29:33 -080031 # Control loop time step
32 self.dt = 0.01
33
34 # State feedback matrices
35 self.A_continuous = numpy.matrix(
36 [[0, 1],
37 [0, -self.Kt / self.Kv / (self.J * self.G * self.G * self.R)]])
38 self.B_continuous = numpy.matrix(
39 [[0],
40 [self.Kt / (self.J * self.G * self.R)]])
41 self.C = numpy.matrix([[1, 0]])
42 self.D = numpy.matrix([[0]])
43
Austin Schuhc1f68892013-03-16 17:06:27 -070044 self.A, self.B = self.ContinuousToDiscrete(
45 self.A_continuous, self.B_continuous, self.dt)
Austin Schuhc8ca2442013-02-23 12:29:33 -080046
Austin Schuhc1f68892013-03-16 17:06:27 -070047 self.PlaceControllerPoles([0.85, 0.45])
Austin Schuhc8ca2442013-02-23 12:29:33 -080048
49 self.rpl = .05
50 self.ipl = 0.008
Austin Schuh3c542312013-02-24 01:53:50 -080051 self.PlaceObserverPoles([self.rpl + 1j * self.ipl,
52 self.rpl - 1j * self.ipl])
Austin Schuhc8ca2442013-02-23 12:29:33 -080053
54 self.U_max = numpy.matrix([[12.0]])
55 self.U_min = numpy.matrix([[-12.0]])
Austin Schuhc8ca2442013-02-23 12:29:33 -080056
Austin Schuhc1f68892013-03-16 17:06:27 -070057 self.InitializeState()
58
59
Austin Schuh3bb9a442014-02-02 16:01:45 -080060class ClawDeltaU(Claw):
61 def __init__(self, name="Claw"):
62 super(ClawDeltaU, self).__init__(name)
Austin Schuhc1f68892013-03-16 17:06:27 -070063 A_unaugmented = self.A
64 B_unaugmented = self.B
65
66 self.A = numpy.matrix([[0.0, 0.0, 0.0],
67 [0.0, 0.0, 0.0],
68 [0.0, 0.0, 1.0]])
69 self.A[0:2, 0:2] = A_unaugmented
70 self.A[0:2, 2] = B_unaugmented
71
72 self.B = numpy.matrix([[0.0],
73 [0.0],
74 [1.0]])
75
76 self.C = numpy.matrix([[1.0, 0.0, 0.0]])
77 self.D = numpy.matrix([[0.0]])
78
Brian Silverman7fb8e222013-03-16 20:09:58 -070079 self.PlaceControllerPoles([0.55, 0.35, 0.80])
Austin Schuhc1f68892013-03-16 17:06:27 -070080
81 print "K"
82 print self.K
83 print "Placed controller poles are"
84 print numpy.linalg.eig(self.A - self.B * self.K)[0]
85
86 self.rpl = .05
87 self.ipl = 0.008
88 self.PlaceObserverPoles([self.rpl + 1j * self.ipl,
Brian Silverman23a67ca2013-03-16 23:48:50 -070089 self.rpl - 1j * self.ipl, 0.90])
Austin Schuhc1f68892013-03-16 17:06:27 -070090 print "Placed observer poles are"
91 print numpy.linalg.eig(self.A - self.L * self.C)[0]
92
93 self.U_max = numpy.matrix([[12.0]])
94 self.U_min = numpy.matrix([[-12.0]])
95
96 self.InitializeState()
97
98
Austin Schuh3bb9a442014-02-02 16:01:45 -080099def ClipDeltaU(claw, delta_u):
100 old_u = numpy.matrix([[claw.X[2, 0]]])
101 new_u = numpy.clip(old_u + delta_u, claw.U_min, claw.U_max)
Austin Schuhc1f68892013-03-16 17:06:27 -0700102 return new_u - old_u
103
Austin Schuhc8ca2442013-02-23 12:29:33 -0800104def main(argv):
Austin Schuh3c542312013-02-24 01:53:50 -0800105 # Simulate the response of the system to a step input.
Austin Schuh3bb9a442014-02-02 16:01:45 -0800106 claw = ClawDeltaU()
Austin Schuhc8ca2442013-02-23 12:29:33 -0800107 simulated_x = []
108 for _ in xrange(100):
Austin Schuh3bb9a442014-02-02 16:01:45 -0800109 claw.Update(numpy.matrix([[12.0]]))
110 simulated_x.append(claw.X[0, 0])
Austin Schuhc8ca2442013-02-23 12:29:33 -0800111
Austin Schuh3c542312013-02-24 01:53:50 -0800112 pylab.plot(range(100), simulated_x)
113 pylab.show()
Austin Schuhc8ca2442013-02-23 12:29:33 -0800114
Austin Schuh3c542312013-02-24 01:53:50 -0800115 # Simulate the closed loop response of the system to a step input.
Austin Schuh3bb9a442014-02-02 16:01:45 -0800116 top_claw = ClawDeltaU("TopClaw")
Austin Schuhc8ca2442013-02-23 12:29:33 -0800117 close_loop_x = []
Austin Schuhc1f68892013-03-16 17:06:27 -0700118 close_loop_u = []
119 R = numpy.matrix([[1.0], [0.0], [0.0]])
Austin Schuh3bb9a442014-02-02 16:01:45 -0800120 top_claw.X[2, 0] = -5
Austin Schuhc8ca2442013-02-23 12:29:33 -0800121 for _ in xrange(100):
Austin Schuh3bb9a442014-02-02 16:01:45 -0800122 U = numpy.clip(top_claw.K * (R - top_claw.X_hat), top_claw.U_min, top_claw.U_max)
123 U = ClipDeltaU(top_claw, U)
124 top_claw.UpdateObserver(U)
125 top_claw.Update(U)
126 close_loop_x.append(top_claw.X[0, 0] * 10)
127 close_loop_u.append(top_claw.X[2, 0])
Austin Schuhc8ca2442013-02-23 12:29:33 -0800128
Austin Schuhfa033692013-02-24 01:00:55 -0800129 pylab.plot(range(100), close_loop_x)
Austin Schuhc1f68892013-03-16 17:06:27 -0700130 pylab.plot(range(100), close_loop_u)
Austin Schuhfa033692013-02-24 01:00:55 -0800131 pylab.show()
Austin Schuhc8ca2442013-02-23 12:29:33 -0800132
Austin Schuh3c542312013-02-24 01:53:50 -0800133 # Write the generated constants out to a file.
Austin Schuh3bb9a442014-02-02 16:01:45 -0800134 if len(argv) != 9:
Austin Schuhc1f68892013-03-16 17:06:27 -0700135 print "Expected .h file name and .cc file name for"
136 print "both the plant and unaugmented plant"
Austin Schuhc8ca2442013-02-23 12:29:33 -0800137 else:
Austin Schuh3bb9a442014-02-02 16:01:45 -0800138 top_unaug_claw = Claw("RawTopClaw")
139 top_unaug_loop_writer = control_loop.ControlLoopWriter("RawTopClaw",
140 [top_unaug_claw])
Austin Schuh683a0d02013-03-02 01:51:31 -0800141 if argv[1][-3:] == '.cc':
Austin Schuh3bb9a442014-02-02 16:01:45 -0800142 top_unaug_loop_writer.Write(argv[2], argv[1])
Austin Schuh683a0d02013-03-02 01:51:31 -0800143 else:
Austin Schuh3bb9a442014-02-02 16:01:45 -0800144 top_unaug_loop_writer.Write(argv[1], argv[2])
145
146 top_loop_writer = control_loop.ControlLoopWriter("TopClaw", [top_claw])
147 if argv[3][-3:] == '.cc':
148 top_loop_writer.Write(argv[4], argv[3])
149 else:
150 top_loop_writer.Write(argv[3], argv[4])
151
152 bottom_claw = ClawDeltaU("BottomClaw")
153 bottom_unaug_claw = Claw("RawBottomClaw")
154 bottom_unaug_loop_writer = control_loop.ControlLoopWriter(
155 "RawBottomClaw", [bottom_unaug_claw])
156 if argv[5][-3:] == '.cc':
157 bottom_unaug_loop_writer.Write(argv[6], argv[5])
158 else:
159 bottom_unaug_loop_writer.Write(argv[5], argv[6])
160
161 bottom_loop_writer = control_loop.ControlLoopWriter("BottomClaw",
162 [bottom_claw])
163 if argv[7][-3:] == '.cc':
164 bottom_loop_writer.Write(argv[8], argv[7])
165 else:
166 bottom_loop_writer.Write(argv[7], argv[8])
Austin Schuhc8ca2442013-02-23 12:29:33 -0800167
168if __name__ == '__main__':
169 sys.exit(main(sys.argv))