blob: 498a4858cd318a6b0ba5b364e7ca8ea105b5feaa [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
Austin Schuh683a0d02013-03-02 01:51:31 -080020 # TODO(aschuh): Measure this in reality. It doesn't seem high enough.
21 # James measured 0.51, but that can't be right given what I am seeing.
22 self.J = 1.51
Austin Schuhc8ca2442013-02-23 12:29:33 -080023 # Resistance of the motor
24 self.R = 12.0 / self.stall_current + 0.024 + .003
25 # Motor velocity constant
Austin Schuh3c542312013-02-24 01:53:50 -080026 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
27 (13.5 - self.R * self.free_current))
Austin Schuhc8ca2442013-02-23 12:29:33 -080028 # Torque constant
29 self.Kt = self.stall_torque / self.stall_current
30 # Gear ratio
31 self.G = 1.0 / ((84.0 / 20.0) * (50.0 / 14.0) * (40.0 / 14.0) * (40.0 / 12.0))
32 # Control loop time step
33 self.dt = 0.01
34
35 # State feedback matrices
36 self.A_continuous = numpy.matrix(
37 [[0, 1],
38 [0, -self.Kt / self.Kv / (self.J * self.G * self.G * self.R)]])
39 self.B_continuous = numpy.matrix(
40 [[0],
41 [self.Kt / (self.J * self.G * self.R)]])
42 self.C = numpy.matrix([[1, 0]])
43 self.D = numpy.matrix([[0]])
44
Austin Schuh3c542312013-02-24 01:53:50 -080045 self.ContinuousToDiscrete(self.A_continuous, self.B_continuous,
46 self.dt, self.C)
Austin Schuhc8ca2442013-02-23 12:29:33 -080047
Austin Schuh683a0d02013-03-02 01:51:31 -080048 self.PlaceControllerPoles([.84, .84])
49
50 print self.K
Austin Schuhc8ca2442013-02-23 12:29:33 -080051
52 self.rpl = .05
53 self.ipl = 0.008
Austin Schuh3c542312013-02-24 01:53:50 -080054 self.PlaceObserverPoles([self.rpl + 1j * self.ipl,
55 self.rpl - 1j * self.ipl])
Austin Schuhc8ca2442013-02-23 12:29:33 -080056
57 self.U_max = numpy.matrix([[12.0]])
58 self.U_min = numpy.matrix([[-12.0]])
Austin Schuhc8ca2442013-02-23 12:29:33 -080059
60def main(argv):
Austin Schuh3c542312013-02-24 01:53:50 -080061 # Simulate the response of the system to a step input.
Austin Schuhc8ca2442013-02-23 12:29:33 -080062 wrist = Wrist()
63 simulated_x = []
64 for _ in xrange(100):
65 wrist.Update(numpy.matrix([[12.0]]))
66 simulated_x.append(wrist.X[0, 0])
67
Austin Schuh3c542312013-02-24 01:53:50 -080068 pylab.plot(range(100), simulated_x)
69 pylab.show()
Austin Schuhc8ca2442013-02-23 12:29:33 -080070
Austin Schuh3c542312013-02-24 01:53:50 -080071 # Simulate the closed loop response of the system to a step input.
Austin Schuhc8ca2442013-02-23 12:29:33 -080072 wrist = Wrist()
73 close_loop_x = []
Austin Schuhc8ca2442013-02-23 12:29:33 -080074 R = numpy.matrix([[1.0], [0.0]])
75 for _ in xrange(100):
Austin Schuh3c542312013-02-24 01:53:50 -080076 U = numpy.clip(wrist.K * (R - wrist.X_hat), wrist.U_min, wrist.U_max)
77 wrist.UpdateObserver(U)
Austin Schuhc8ca2442013-02-23 12:29:33 -080078 wrist.Update(U)
79 close_loop_x.append(wrist.X[0, 0])
80
Austin Schuhfa033692013-02-24 01:00:55 -080081 pylab.plot(range(100), close_loop_x)
82 pylab.show()
Austin Schuhc8ca2442013-02-23 12:29:33 -080083
Austin Schuh3c542312013-02-24 01:53:50 -080084 # Write the generated constants out to a file.
Austin Schuhc8ca2442013-02-23 12:29:33 -080085 if len(argv) != 3:
Austin Schuh683a0d02013-03-02 01:51:31 -080086 print "Expected .h file name and .cc file name"
Austin Schuhc8ca2442013-02-23 12:29:33 -080087 else:
Austin Schuh683a0d02013-03-02 01:51:31 -080088 if argv[1][-3:] == '.cc':
89 print '.cc file is second'
90 else:
91 wrist.DumpHeaderFile(argv[1])
92 wrist.DumpCppFile(argv[2], argv[1])
Austin Schuhc8ca2442013-02-23 12:29:33 -080093
94if __name__ == '__main__':
95 sys.exit(main(sys.argv))