blob: 441999f367e9e70565182f4edefff9791e880a3a [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 Schuh3c542312013-02-24 01:53:50 -08008class Wrist(control_loop.ControlLoop):
Austin Schuhc8ca2442013-02-23 12:29:33 -08009 def __init__(self):
Austin Schuh3c542312013-02-24 01:53:50 -080010 super(Wrist, self).__init__("Wrist")
Austin Schuhc8ca2442013-02-23 12:29:33 -080011 # Stall Torque in N m
12 self.stall_torque = 1.4
13 # Stall Current in Amps
14 self.stall_current = 86
15 # Free Speed in RPM
16 self.free_speed = 6200.0
Austin Schuh3c542312013-02-24 01:53:50 -080017 # Free Current in Amps
18 self.free_current = 1.5
Austin Schuhc8ca2442013-02-23 12:29:33 -080019 # Moment of inertia of the wrist in kg m^2
20 self.J = 0.51
21 # Resistance of the motor
22 self.R = 12.0 / self.stall_current + 0.024 + .003
23 # Motor velocity constant
Austin Schuh3c542312013-02-24 01:53:50 -080024 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
25 (13.5 - self.R * self.free_current))
Austin Schuhc8ca2442013-02-23 12:29:33 -080026 # Torque constant
27 self.Kt = self.stall_torque / self.stall_current
28 # Gear ratio
29 self.G = 1.0 / ((84.0 / 20.0) * (50.0 / 14.0) * (40.0 / 14.0) * (40.0 / 12.0))
30 # Control loop time step
31 self.dt = 0.01
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
Austin Schuh3c542312013-02-24 01:53:50 -080043 self.ContinuousToDiscrete(self.A_continuous, self.B_continuous,
44 self.dt, self.C)
Austin Schuhc8ca2442013-02-23 12:29:33 -080045
Austin Schuh3c542312013-02-24 01:53:50 -080046 self.PlaceControllerPoles([.89, .85])
Austin Schuhc8ca2442013-02-23 12:29:33 -080047
48 self.rpl = .05
49 self.ipl = 0.008
Austin Schuh3c542312013-02-24 01:53:50 -080050 self.PlaceObserverPoles([self.rpl + 1j * self.ipl,
51 self.rpl - 1j * self.ipl])
Austin Schuhc8ca2442013-02-23 12:29:33 -080052
53 self.U_max = numpy.matrix([[12.0]])
54 self.U_min = numpy.matrix([[-12.0]])
Austin Schuhc8ca2442013-02-23 12:29:33 -080055
56def main(argv):
Austin Schuh3c542312013-02-24 01:53:50 -080057 # Simulate the response of the system to a step input.
Austin Schuhc8ca2442013-02-23 12:29:33 -080058 wrist = Wrist()
59 simulated_x = []
60 for _ in xrange(100):
61 wrist.Update(numpy.matrix([[12.0]]))
62 simulated_x.append(wrist.X[0, 0])
63
Austin Schuh3c542312013-02-24 01:53:50 -080064 pylab.plot(range(100), simulated_x)
65 pylab.show()
Austin Schuhc8ca2442013-02-23 12:29:33 -080066
Austin Schuh3c542312013-02-24 01:53:50 -080067 # Simulate the closed loop response of the system to a step input.
Austin Schuhc8ca2442013-02-23 12:29:33 -080068 wrist = Wrist()
69 close_loop_x = []
Austin Schuhc8ca2442013-02-23 12:29:33 -080070 R = numpy.matrix([[1.0], [0.0]])
71 for _ in xrange(100):
Austin Schuh3c542312013-02-24 01:53:50 -080072 U = numpy.clip(wrist.K * (R - wrist.X_hat), wrist.U_min, wrist.U_max)
73 wrist.UpdateObserver(U)
Austin Schuhc8ca2442013-02-23 12:29:33 -080074 wrist.Update(U)
75 close_loop_x.append(wrist.X[0, 0])
76
Austin Schuhfa033692013-02-24 01:00:55 -080077 pylab.plot(range(100), close_loop_x)
78 pylab.show()
Austin Schuhc8ca2442013-02-23 12:29:33 -080079
Austin Schuh3c542312013-02-24 01:53:50 -080080 # Write the generated constants out to a file.
Austin Schuhc8ca2442013-02-23 12:29:33 -080081 if len(argv) != 3:
82 print "Expected .cc file name and .h file name"
83 else:
Austin Schuh3c542312013-02-24 01:53:50 -080084 wrist.DumpHeaderFile(argv[1])
85 wrist.DumpCppFile(argv[2], argv[1])
Austin Schuhc8ca2442013-02-23 12:29:33 -080086
87if __name__ == '__main__':
88 sys.exit(main(sys.argv))