James Kuszmaul | 4a4622b | 2013-03-02 16:28:29 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import control_loop |
| 4 | import numpy |
| 5 | import sys |
| 6 | from matplotlib import pylab |
| 7 | |
| 8 | class AngleAdjust(control_loop.ControlLoop): |
| 9 | def __init__(self): |
| 10 | super(AngleAdjust, self).__init__("AngleAdjust") |
| 11 | # Stall Torque in N m |
| 12 | self.stall_torque = .428 |
| 13 | # Stall Current in Amps |
| 14 | self.stall_current = 63.8 |
| 15 | # Free Speed in RPM |
Austin Schuh | 72e2677 | 2013-03-10 18:15:39 -0700 | [diff] [blame^] | 16 | self.free_speed = 14900.0 |
James Kuszmaul | 4a4622b | 2013-03-02 16:28:29 -0800 | [diff] [blame] | 17 | # Free Current in Amps |
| 18 | self.free_current = 1.2 |
| 19 | # Moment of inertia of the angle adjust about the shooter's pivot in kg m^2 |
Austin Schuh | 72e2677 | 2013-03-10 18:15:39 -0700 | [diff] [blame^] | 20 | self.J = 9.4 |
James Kuszmaul | 4a4622b | 2013-03-02 16:28:29 -0800 | [diff] [blame] | 21 | # Resistance of the motor |
| 22 | self.R = 12.0 / self.stall_current |
| 23 | # Motor velocity constant |
| 24 | self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) / |
| 25 | (12.0 - self.R * self.free_current)) |
| 26 | # Torque constant |
| 27 | self.Kt = self.stall_torque / self.stall_current |
| 28 | # Gear ratio of the gearbox multiplied by the ratio of the radii of |
| 29 | # the output and the angle adjust curve, which is essentially another gear. |
| 30 | self.G = (1.0 / 50.0) * (0.01905 / 0.41964) |
| 31 | # 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 | |
| 44 | self.ContinuousToDiscrete(self.A_continuous, self.B_continuous, |
| 45 | self.dt, self.C) |
| 46 | |
Austin Schuh | 72e2677 | 2013-03-10 18:15:39 -0700 | [diff] [blame^] | 47 | self.PlaceControllerPoles([.5, .5]) |
James Kuszmaul | 4a4622b | 2013-03-02 16:28:29 -0800 | [diff] [blame] | 48 | |
| 49 | self.rpl = .05 |
| 50 | self.ipl = 0.008 |
| 51 | self.PlaceObserverPoles([self.rpl + 1j * self.ipl, |
| 52 | self.rpl - 1j * self.ipl]) |
| 53 | |
| 54 | self.U_max = numpy.matrix([[12.0]]) |
| 55 | self.U_min = numpy.matrix([[-12.0]]) |
| 56 | |
| 57 | def main(argv): |
| 58 | # Simulate the response of the system to a step input. |
Austin Schuh | 72e2677 | 2013-03-10 18:15:39 -0700 | [diff] [blame^] | 59 | angle_adjust_data = numpy.genfromtxt( |
| 60 | 'angle_adjust/angle_adjust_data.csv', delimiter=',') |
James Kuszmaul | 4a4622b | 2013-03-02 16:28:29 -0800 | [diff] [blame] | 61 | angle_adjust = AngleAdjust() |
| 62 | simulated_x = [] |
Austin Schuh | 72e2677 | 2013-03-10 18:15:39 -0700 | [diff] [blame^] | 63 | real_x = [] |
| 64 | initial_x = angle_adjust_data[0, 2] |
| 65 | for i in xrange(angle_adjust_data.shape[0]): |
| 66 | angle_adjust.Update(numpy.matrix([[angle_adjust_data[i, 1] - 0.7]])) |
James Kuszmaul | 4a4622b | 2013-03-02 16:28:29 -0800 | [diff] [blame] | 67 | simulated_x.append(angle_adjust.X[0, 0]) |
Austin Schuh | 72e2677 | 2013-03-10 18:15:39 -0700 | [diff] [blame^] | 68 | x_offset = angle_adjust_data[i, 2] - initial_x |
| 69 | real_x.append(x_offset) |
James Kuszmaul | 4a4622b | 2013-03-02 16:28:29 -0800 | [diff] [blame] | 70 | |
Austin Schuh | 72e2677 | 2013-03-10 18:15:39 -0700 | [diff] [blame^] | 71 | sim_delay = 2 |
| 72 | pylab.plot(range(sim_delay, angle_adjust_data.shape[0] + sim_delay), |
| 73 | simulated_x, label='Simulation') |
| 74 | pylab.plot(range(angle_adjust_data.shape[0]), real_x, label='Reality') |
| 75 | pylab.legend() |
James Kuszmaul | 4a4622b | 2013-03-02 16:28:29 -0800 | [diff] [blame] | 76 | pylab.show() |
| 77 | |
| 78 | # Simulate the closed loop response of the system to a step input. |
| 79 | angle_adjust = AngleAdjust() |
| 80 | close_loop_x = [] |
| 81 | R = numpy.matrix([[1.0], [0.0]]) |
| 82 | for _ in xrange(100): |
| 83 | U = numpy.clip(angle_adjust.K * (R - angle_adjust.X_hat), angle_adjust.U_min, angle_adjust.U_max) |
| 84 | angle_adjust.UpdateObserver(U) |
| 85 | angle_adjust.Update(U) |
| 86 | close_loop_x.append(angle_adjust.X[0, 0]) |
| 87 | |
| 88 | pylab.plot(range(100), close_loop_x) |
| 89 | pylab.show() |
| 90 | |
| 91 | # Write the generated constants out to a file. |
| 92 | if len(argv) != 3: |
| 93 | print "Expected .cc file name and .h file name" |
| 94 | else: |
Austin Schuh | 72e2677 | 2013-03-10 18:15:39 -0700 | [diff] [blame^] | 95 | if argv[1][-3:] == '.cc': |
| 96 | print '.cc file is second' |
| 97 | else: |
| 98 | angle_adjust.DumpHeaderFile(argv[1]) |
| 99 | angle_adjust.DumpCppFile(argv[2], argv[1]) |
James Kuszmaul | 4a4622b | 2013-03-02 16:28:29 -0800 | [diff] [blame] | 100 | |
| 101 | if __name__ == '__main__': |
| 102 | sys.exit(main(sys.argv)) |