blob: 0015e915a6049fe088dc982688b3473966cf586c [file] [log] [blame]
joe2d92e852014-01-25 14:31:24 -08001#!/usr/bin/python
2
3import control_loop
4import numpy
5import sys
6from matplotlib import pylab
7
8class Shooter(control_loop.ControlLoop):
9 def __init__(self, name="RawShooter"):
10 super(Shooter, self).__init__(name)
11 # Stall Torque in N m
James Kuszmaul49d0e6c2014-02-03 19:46:17 -080012 self.stall_torque = .4982
joe2d92e852014-01-25 14:31:24 -080013 # Stall Current in Amps
14 self.stall_current = 85
15 # Free Speed in RPM
16 self.free_speed = 19300.0
17 # Free Current in Amps
James Kuszmaul49d0e6c2014-02-03 19:46:17 -080018 self.free_current = 1.2
joe2d92e852014-01-25 14:31:24 -080019 # Moment of inertia of the shooter in kg m^2
James Kuszmaul49d0e6c2014-02-03 19:46:17 -080020 # Calculate Moment of Irtia
21 self.J = 0.3
22 # Resistance of the motor, divided by the number of motors.
23 self.R = 12.0 / self.stall_current / 2.0
joe2d92e852014-01-25 14:31:24 -080024 # Motor velocity constant
25 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
26 (13.5 - self.R * self.free_current))
27 # Torque constant
28 self.Kt = self.stall_torque / self.stall_current
James Kuszmaul49d0e6c2014-02-03 19:46:17 -080029 # Spring constant for the springs, N/m
30 self.Ks = 3600.0
joe2d92e852014-01-25 14:31:24 -080031 # Gear ratio
32 self.G = 1.0 / ((84.0 / 20.0) * (50.0 / 14.0) * (40.0 / 14.0) * (40.0 / 12.0))
33 # Control loop time step
34 self.dt = 0.01
35
James Kuszmaul49d0e6c2014-02-03 19:46:17 -080036
joe2d92e852014-01-25 14:31:24 -080037 # State feedback matrices
James Kuszmaul49d0e6c2014-02-03 19:46:17 -080038 # TODO(james): Make this work with origins other than at kx = 0.
joe2d92e852014-01-25 14:31:24 -080039 self.A_continuous = numpy.matrix(
40 [[0, 1],
James Kuszmaul49d0e6c2014-02-03 19:46:17 -080041 [-self.Ks * 0.01 / self.J,
42 -self.Kt / self.Kv / (self.J * self.G * self.G * self.R)]])
joe2d92e852014-01-25 14:31:24 -080043 self.B_continuous = numpy.matrix(
44 [[0],
45 [self.Kt / (self.J * self.G * self.R)]])
46 self.C = numpy.matrix([[1, 0]])
47 self.D = numpy.matrix([[0]])
48
49 self.A, self.B = self.ContinuousToDiscrete(
50 self.A_continuous, self.B_continuous, self.dt)
51
52 self.PlaceControllerPoles([0.85, 0.45])
53
54 self.rpl = .05
55 self.ipl = 0.008
56 self.PlaceObserverPoles([self.rpl + 1j * self.ipl,
57 self.rpl - 1j * self.ipl])
58
59 self.U_max = numpy.matrix([[12.0]])
60 self.U_min = numpy.matrix([[-12.0]])
61
62 self.InitializeState()
63
64
65class ShooterDeltaU(Shooter):
66 def __init__(self, name="Shooter"):
67 super(ShooterDeltaU, self).__init__(name)
68 A_unaugmented = self.A
69 B_unaugmented = self.B
70
71 self.A = numpy.matrix([[0.0, 0.0, 0.0],
72 [0.0, 0.0, 0.0],
73 [0.0, 0.0, 1.0]])
74 self.A[0:2, 0:2] = A_unaugmented
75 self.A[0:2, 2] = B_unaugmented
76
77 self.B = numpy.matrix([[0.0],
78 [0.0],
79 [1.0]])
80
81 self.C = numpy.matrix([[1.0, 0.0, 0.0]])
82 self.D = numpy.matrix([[0.0]])
83
James Kuszmaul49d0e6c2014-02-03 19:46:17 -080084 self.PlaceControllerPoles([0.55, 0.45, 0.80])
joe2d92e852014-01-25 14:31:24 -080085
86 print "K"
87 print self.K
88 print "Placed controller poles are"
89 print numpy.linalg.eig(self.A - self.B * self.K)[0]
90
91 self.rpl = .05
92 self.ipl = 0.008
93 self.PlaceObserverPoles([self.rpl + 1j * self.ipl,
94 self.rpl - 1j * self.ipl, 0.90])
95 print "Placed observer poles are"
96 print numpy.linalg.eig(self.A - self.L * self.C)[0]
97
98 self.U_max = numpy.matrix([[12.0]])
99 self.U_min = numpy.matrix([[-12.0]])
100
101 self.InitializeState()
102
103
104def ClipDeltaU(shooter, delta_u):
105 old_u = numpy.matrix([[shooter.X[2, 0]]])
106 new_u = numpy.clip(old_u + delta_u, shooter.U_min, shooter.U_max)
107 return new_u - old_u
108
109def main(argv):
110 # Simulate the response of the system to a step input.
James Kuszmaul49d0e6c2014-02-03 19:46:17 -0800111 shooter = Shooter()
joe2d92e852014-01-25 14:31:24 -0800112 simulated_x = []
James Kuszmaul49d0e6c2014-02-03 19:46:17 -0800113 for _ in xrange(1000):
114 shooter.Update(numpy.matrix([[2.0]]))
joe2d92e852014-01-25 14:31:24 -0800115 simulated_x.append(shooter.X[0, 0])
116
James Kuszmaul49d0e6c2014-02-03 19:46:17 -0800117 pylab.plot(range(1000), simulated_x)
joe2d92e852014-01-25 14:31:24 -0800118 pylab.show()
119
James Kuszmaul49d0e6c2014-02-03 19:46:17 -0800120 # Simulate the response of the system to a goal.
121 shooter = Shooter()
joe2d92e852014-01-25 14:31:24 -0800122 close_loop_x = []
123 close_loop_u = []
James Kuszmaul49d0e6c2014-02-03 19:46:17 -0800124 R = numpy.matrix([[1.0], [0.0]])
joe2d92e852014-01-25 14:31:24 -0800125 for _ in xrange(100):
James Kuszmaul49d0e6c2014-02-03 19:46:17 -0800126 feed_forward = (-numpy.linalg.lstsq(shooter.B_continuous, numpy.identity(
127 shooter.B_continuous.shape[0]))[0] *
128 shooter.A_continuous * R)
129 U = numpy.clip(shooter.K * (R - shooter.X_hat) + feed_forward,
130 shooter.U_min, shooter.U_max)
131#U = ClipDeltaU(shooter, U)
joe2d92e852014-01-25 14:31:24 -0800132 shooter.UpdateObserver(U)
133 shooter.Update(U)
134 close_loop_x.append(shooter.X[0, 0] * 10)
James Kuszmaul49d0e6c2014-02-03 19:46:17 -0800135 close_loop_u.append(U[0, 0])
joe2d92e852014-01-25 14:31:24 -0800136
137 pylab.plot(range(100), close_loop_x)
138 pylab.plot(range(100), close_loop_u)
139 pylab.show()
140
141 # Write the generated constants out to a file.
142 if len(argv) != 3:
143 print "Expected .h file name and .cc file name for"
144 print "both the plant and unaugmented plant"
145 else:
146 unaug_shooter = Shooter("RawShooter")
147 unaug_loop_writer = control_loop.ControlLoopWriter("RawShooter",
148 [unaug_shooter])
149 #if argv[3][-3:] == '.cc':
150 # unaug_loop_writer.Write(argv[4], argv[3])
151 #else:
152 # unaug_loop_writer.Write(argv[3], argv[4])
153
154 loop_writer = control_loop.ControlLoopWriter("Shooter", [shooter])
155 if argv[1][-3:] == '.cc':
156 loop_writer.Write(argv[2], argv[1])
157 else:
158 loop_writer.Write(argv[1], argv[2])
159
160if __name__ == '__main__':
161 sys.exit(main(sys.argv))