blob: 83beb90530bedb3512f7eef9284fe0ccdda886b0 [file] [log] [blame]
James Kuszmaulcdd033e2013-03-02 15:10:43 -08001#!/usr/bin/python
2
3import numpy
4import sys
5from matplotlib import pylab
6import control_loop
7
8class Shooter(control_loop.ControlLoop):
9 def __init__(self):
10 super(Shooter, self).__init__("Shooter")
11 # Stall Torque in N m
Austin Schuh0e38a5d2013-03-03 03:53:35 -080012 self.stall_torque = 0.49819248
James Kuszmaulcdd033e2013-03-02 15:10:43 -080013 # Stall Current in Amps
Austin Schuh0e38a5d2013-03-03 03:53:35 -080014 self.stall_current = 85
James Kuszmaulcdd033e2013-03-02 15:10:43 -080015 # Free Speed in RPM
Austin Schuhe052d8a2013-03-10 18:58:18 -070016 self.free_speed = 19300.0 - 1500.0
James Kuszmaulcdd033e2013-03-02 15:10:43 -080017 # Free Current in Amps
18 self.free_current = 1.4
19 # Moment of inertia of the shooter wheel in kg m^2
Austin Schuhe3490622013-03-13 01:24:30 -070020 self.J = 0.0032
James Kuszmaulcdd033e2013-03-02 15:10:43 -080021 # Resistance of the motor, divided by 2 to account for the 2 motors
22 self.R = 12.0 / self.stall_current / 2
23 # Motor velocity constant
Austin Schuh0e38a5d2013-03-03 03:53:35 -080024 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
James Kuszmaulcdd033e2013-03-02 15:10:43 -080025 (12.0 - self.R * self.free_current))
26 # Torque constant
27 self.Kt = self.stall_torque / self.stall_current
28 # Gear ratio
29 self.G = 11.0 / 34.0
30 # Control loop time step
31 self.dt = 0.01
32
33 # State feedback matrices
Austin Schuh0e38a5d2013-03-03 03:53:35 -080034 self.A_continuous = numpy.matrix(
James Kuszmaulcdd033e2013-03-02 15:10:43 -080035 [[0, 1],
36 [0, -self.Kt / self.Kv / (self.J * self.G * self.G * self.R)]])
Austin Schuh0e38a5d2013-03-03 03:53:35 -080037 self.B_continuous = numpy.matrix(
James Kuszmaulcdd033e2013-03-02 15:10:43 -080038 [[0],
39 [self.Kt / (self.J * self.G * self.R)]])
Austin Schuh0e38a5d2013-03-03 03:53:35 -080040 self.C = numpy.matrix([[1, 0]])
41 self.D = numpy.matrix([[0]])
James Kuszmaulcdd033e2013-03-02 15:10:43 -080042
Austin Schuh0e38a5d2013-03-03 03:53:35 -080043 self.ContinuousToDiscrete(self.A_continuous, self.B_continuous,
James Kuszmaulcdd033e2013-03-02 15:10:43 -080044 self.dt, self.C)
45
Austin Schuh0e38a5d2013-03-03 03:53:35 -080046 self.PlaceControllerPoles([.6, .981])
James Kuszmaulcdd033e2013-03-02 15:10:43 -080047
48 self.rpl = .45
49 self.ipl = 0.07
50 self.PlaceObserverPoles([self.rpl + 1j * self.ipl,
51 self.rpl - 1j * self.ipl])
52
53 self.U_max = numpy.matrix([[12.0]])
54 self.U_min = numpy.matrix([[-12.0]])
55
56
57def main(argv):
58 # Simulate the response of the system to a step input.
Austin Schuh383878f2013-03-10 01:34:34 -080059 shooter_data = numpy.genfromtxt('shooter/shooter_data.csv', delimiter=',')
James Kuszmaulcdd033e2013-03-02 15:10:43 -080060 shooter = Shooter()
61 simulated_x = []
Austin Schuh383878f2013-03-10 01:34:34 -080062 real_x = []
63 x_vel = []
64 initial_x = shooter_data[0, 2]
65 last_x = initial_x
66 for i in xrange(shooter_data.shape[0]):
67 shooter.Update(numpy.matrix([[shooter_data[i, 1]]]))
James Kuszmaulcdd033e2013-03-02 15:10:43 -080068 simulated_x.append(shooter.X[0, 0])
Austin Schuh383878f2013-03-10 01:34:34 -080069 x_offset = shooter_data[i, 2] - initial_x
Austin Schuhe052d8a2013-03-10 18:58:18 -070070 real_x.append(x_offset)
71 x_vel.append((shooter_data[i, 2] - last_x) * 100.0)
Austin Schuh383878f2013-03-10 01:34:34 -080072 last_x = shooter_data[i, 2]
James Kuszmaulcdd033e2013-03-02 15:10:43 -080073
Austin Schuh383878f2013-03-10 01:34:34 -080074 sim_delay = 1
75 pylab.plot(range(sim_delay, shooter_data.shape[0] + sim_delay),
76 simulated_x, label='Simulation')
77 pylab.plot(range(shooter_data.shape[0]), real_x, label='Reality')
78 pylab.plot(range(shooter_data.shape[0]), x_vel, label='Velocity')
79 pylab.legend()
80 pylab.show()
James Kuszmaulcdd033e2013-03-02 15:10:43 -080081
82 # Simulate the closed loop response of the system to a step input.
83 shooter = Shooter()
84 close_loop_x = []
85 close_loop_U = []
Austin Schuh0e38a5d2013-03-03 03:53:35 -080086 velocity_goal = 300
James Kuszmaulcdd033e2013-03-02 15:10:43 -080087 R = numpy.matrix([[0.0], [velocity_goal]])
88 for _ in pylab.linspace(0,1.99,200):
89 # Iterate the position up.
90 R = numpy.matrix([[R[0, 0] + 10.5], [velocity_goal]])
91 # Prevents the position goal from going beyond what is necessary.
92 velocity_weight_scalar = 0.35
Austin Schuh0e38a5d2013-03-03 03:53:35 -080093 max_reference = (
94 (shooter.U_max[0, 0] - velocity_weight_scalar *
95 (velocity_goal - shooter.X_hat[1, 0]) * shooter.K[0, 1]) /
96 shooter.K[0, 0] +
97 shooter.X_hat[0, 0])
98 min_reference = (
99 (shooter.U_min[0, 0] - velocity_weight_scalar *
100 (velocity_goal - shooter.X_hat[1, 0]) * shooter.K[0, 1]) /
101 shooter.K[0, 0] +
102 shooter.X_hat[0, 0])
103 R[0, 0] = numpy.clip(R[0, 0], min_reference, max_reference)
104 U = numpy.clip(shooter.K * (R - shooter.X_hat),
105 shooter.U_min, shooter.U_max)
James Kuszmaulcdd033e2013-03-02 15:10:43 -0800106 shooter.UpdateObserver(U)
107 shooter.Update(U)
108 close_loop_x.append(shooter.X[1, 0])
109 close_loop_U.append(U[0, 0])
110
Austin Schuh0e38a5d2013-03-03 03:53:35 -0800111 #pylab.plotfile("shooter.csv", (0,1))
112 #pylab.plot(pylab.linspace(0,1.99,200), close_loop_U, 'ro')
113 #pylab.plotfile("shooter.csv", (0,2))
James Kuszmaulcdd033e2013-03-02 15:10:43 -0800114 pylab.plot(pylab.linspace(0,1.99,200), close_loop_x, 'ro')
115 pylab.show()
116
117 # Simulate spin down.
118 spin_down_x = [];
Austin Schuh0e38a5d2013-03-03 03:53:35 -0800119 R = numpy.matrix([[50.0], [0.0]])
James Kuszmaulcdd033e2013-03-02 15:10:43 -0800120 for _ in xrange(150):
121 U = 0
122 shooter.UpdateObserver(U)
123 shooter.Update(U)
124 spin_down_x.append(shooter.X[1, 0])
125
Austin Schuh0e38a5d2013-03-03 03:53:35 -0800126 #pylab.plot(range(150), spin_down_x)
127 #pylab.show()
James Kuszmaulcdd033e2013-03-02 15:10:43 -0800128
Austin Schuh0e38a5d2013-03-03 03:53:35 -0800129 if len(argv) != 3:
130 print "Expected .h file name and .cc file name"
James Kuszmaulcdd033e2013-03-02 15:10:43 -0800131 else:
Austin Schuhe3490622013-03-13 01:24:30 -0700132 loop_writer = control_loop.ControlLoopWriter("Shooter", [shooter])
Austin Schuh0e38a5d2013-03-03 03:53:35 -0800133 if argv[1][-3:] == '.cc':
Austin Schuhe3490622013-03-13 01:24:30 -0700134 loop_writer.Write(argv[2], argv[1])
Austin Schuh0e38a5d2013-03-03 03:53:35 -0800135 else:
Austin Schuhe3490622013-03-13 01:24:30 -0700136 loop_writer.Write(argv[1], argv[2])
James Kuszmaulcdd033e2013-03-02 15:10:43 -0800137
138
139if __name__ == '__main__':
140 sys.exit(main(sys.argv))