Comran Morshed | 2a97bc8 | 2016-01-16 17:27:01 +0000 | [diff] [blame^] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | from frc971.control_loops.python import control_loop |
| 4 | import numpy |
| 5 | import sys |
| 6 | from matplotlib import pylab |
| 7 | |
| 8 | class Shooter(control_loop.ControlLoop): |
| 9 | def __init__(self): |
| 10 | super(Shooter, self).__init__("Shooter") |
| 11 | # Stall Torque in N m |
| 12 | self.stall_torque = 0.49819248 |
| 13 | # Stall Current in Amps |
| 14 | self.stall_current = 85 |
| 15 | # Free Speed in RPM |
| 16 | self.free_speed = 19300.0 - 1500.0 |
| 17 | # Free Current in Amps |
| 18 | self.free_current = 1.4 |
| 19 | # Moment of inertia of the shooter wheel in kg m^2 |
| 20 | self.J = 0.0032 |
| 21 | # 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 |
| 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 |
| 29 | self.G = 11.0 / 34.0 |
| 30 | # Control loop time step |
| 31 | self.dt = 0.005 |
| 32 | |
| 33 | # State feedback matrices |
| 34 | self.A_continuous = numpy.matrix( |
| 35 | [[0, 1], |
| 36 | [0, -self.Kt / self.Kv / (self.J * self.G * self.G * self.R)]]) |
| 37 | self.B_continuous = numpy.matrix( |
| 38 | [[0], |
| 39 | [self.Kt / (self.J * self.G * self.R)]]) |
| 40 | self.C = numpy.matrix([[1, 0]]) |
| 41 | self.D = numpy.matrix([[0]]) |
| 42 | |
| 43 | self.A, self.B = self.ContinuousToDiscrete( |
| 44 | self.A_continuous, self.B_continuous, self.dt) |
| 45 | |
| 46 | self.PlaceControllerPoles([.6, .981]) |
| 47 | |
| 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 | |
| 57 | def main(argv): |
| 58 | if len(argv) != 3: |
| 59 | print "Expected .h file name and .cc file name" |
| 60 | else: |
| 61 | namespaces = ['y2016', 'control_loops', 'shooter'] |
| 62 | shooter = Shooter() |
| 63 | loop_writer = control_loop.ControlLoopWriter("Shooter", [shooter], |
| 64 | namespaces=namespaces) |
| 65 | if argv[1][-3:] == '.cc': |
| 66 | loop_writer.Write(argv[2], argv[1]) |
| 67 | else: |
| 68 | loop_writer.Write(argv[1], argv[2]) |
| 69 | |
| 70 | |
| 71 | if __name__ == '__main__': |
| 72 | sys.exit(main(sys.argv)) |