blob: ed180d8a3546fcd3f98af6780ca9c15350a4951e [file] [log] [blame]
James Kuszmaulf254c1a2013-03-10 16:31:26 -07001#!/usr/bin/python
2
3import control_loop
4import numpy
5import sys
6from matplotlib import pylab
7
8class Drivetrain(control_loop.ControlLoop):
Austin Schuhde4d7fe2013-10-08 22:22:45 -07009 def __init__(self, left_low=True, right_low=True):
James Kuszmaulf254c1a2013-03-10 16:31:26 -070010 super(Drivetrain, self).__init__("Drivetrain")
11 # Stall Torque in N m
12 self.stall_torque = 2.42
13 # Stall Current in Amps
14 self.stall_current = 133
15 # Free Speed in RPM. Used number from last year.
16 self.free_speed = 4650.0
17 # Free Current in Amps
18 self.free_current = 2.7
19 # Moment of inertia of the drivetrain in kg m^2
20 # Just borrowed from last year.
Austin Schuh4352ac62013-03-19 06:23:16 +000021 self.J = 6.4
James Kuszmaulf254c1a2013-03-10 16:31:26 -070022 # Mass of the robot, in kg.
23 self.m = 68
24 # Radius of the robot, in meters (from last year).
25 self.rb = 0.617998644 / 2.0
26 # Radius of the wheels, in meters.
27 self.r = .04445
28 # Resistance of the motor, divided by the number of motors.
Austin Schuh4352ac62013-03-19 06:23:16 +000029 self.R = 12.0 / self.stall_current / 6 + 0.03
James Kuszmaulf254c1a2013-03-10 16:31:26 -070030 # Motor velocity constant
31 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
32 (12.0 - self.R * self.free_current))
33 # Torque constant
34 self.Kt = self.stall_torque / self.stall_current
35 # Gear ratios
36 self.G_low = 16.0 / 60.0 * 19.0 / 50.0
37 self.G_high = 28.0 / 48.0 * 19.0 / 50.0
Austin Schuhde4d7fe2013-10-08 22:22:45 -070038 if left_low:
39 self.Gl = self.G_low
40 else:
41 self.Gl = self.G_high
42 if right_low:
43 self.Gr = self.G_low
44 else:
45 self.Gr = self.G_high
James Kuszmaulf254c1a2013-03-10 16:31:26 -070046 # Control loop time step
47 self.dt = 0.01
48
49 # These describe the way that a given side of a robot will be influenced
50 # by the other side. Units of 1 / kg.
51 self.msp = 1.0 / self.m + self.rb * self.rb / self.J
52 self.msn = 1.0 / self.m - self.rb * self.rb / self.J
53 # The calculations which we will need for A and B.
Austin Schuhde4d7fe2013-10-08 22:22:45 -070054 self.tcl = -self.Kt / self.Kv / (self.Gl * self.Gl * self.R * self.r * self.r)
55 self.tcr = -self.Kt / self.Kv / (self.Gr * self.Gr * self.R * self.r * self.r)
56 self.mpl = self.Kt / (self.Gl * self.R * self.r)
57 self.mpr = self.Kt / (self.Gr * self.R * self.r)
James Kuszmaulf254c1a2013-03-10 16:31:26 -070058
59 # State feedback matrices
60 # X will be of the format
Austin Schuhde4d7fe2013-10-08 22:22:45 -070061 # [[positionl], [velocityl], [positionr], velocityr]]
James Kuszmaulf254c1a2013-03-10 16:31:26 -070062 self.A_continuous = numpy.matrix(
63 [[0, 1, 0, 0],
Austin Schuhde4d7fe2013-10-08 22:22:45 -070064 [0, self.msp * self.tcl, 0, self.msn * self.tcr],
James Kuszmaulf254c1a2013-03-10 16:31:26 -070065 [0, 0, 0, 1],
Austin Schuhde4d7fe2013-10-08 22:22:45 -070066 [0, self.msn * self.tcl, 0, self.msp * self.tcr]])
James Kuszmaulf254c1a2013-03-10 16:31:26 -070067 self.B_continuous = numpy.matrix(
68 [[0, 0],
Austin Schuhde4d7fe2013-10-08 22:22:45 -070069 [self.msp * self.mpl, self.msn * self.mpr],
James Kuszmaulf254c1a2013-03-10 16:31:26 -070070 [0, 0],
Austin Schuhde4d7fe2013-10-08 22:22:45 -070071 [self.msn * self.mpl, self.msp * self.mpr]])
James Kuszmaulf254c1a2013-03-10 16:31:26 -070072 self.C = numpy.matrix([[1, 0, 0, 0],
73 [0, 0, 1, 0]])
74 self.D = numpy.matrix([[0, 0],
75 [0, 0]])
76
Austin Schuh4352ac62013-03-19 06:23:16 +000077 self.A, self.B = self.ContinuousToDiscrete(
78 self.A_continuous, self.B_continuous, self.dt)
James Kuszmaulf254c1a2013-03-10 16:31:26 -070079
80 # Poles from last year.
Austin Schuh4352ac62013-03-19 06:23:16 +000081 self.hp = 0.65
82 self.lp = 0.83
James Kuszmaulf254c1a2013-03-10 16:31:26 -070083 self.PlaceControllerPoles([self.hp, self.hp, self.lp, self.lp])
84
James Kuszmaulf254c1a2013-03-10 16:31:26 -070085 self.hlp = 0.07
86 self.llp = 0.09
87 self.PlaceObserverPoles([self.hlp, self.hlp, self.llp, self.llp])
88
89 self.U_max = numpy.matrix([[12.0], [12.0]])
90 self.U_min = numpy.matrix([[-12.0], [-12.0]])
Austin Schuh4352ac62013-03-19 06:23:16 +000091 self.InitializeState()
James Kuszmaulf254c1a2013-03-10 16:31:26 -070092
93def main(argv):
94 # Simulate the response of the system to a step input.
95 drivetrain = Drivetrain()
96 simulated_left = []
97 simulated_right = []
98 for _ in xrange(100):
99 drivetrain.Update(numpy.matrix([[12.0], [12.0]]))
100 simulated_left.append(drivetrain.X[0, 0])
101 simulated_right.append(drivetrain.X[2, 0])
102
Austin Schuh4352ac62013-03-19 06:23:16 +0000103 #pylab.plot(range(100), simulated_left)
104 #pylab.plot(range(100), simulated_right)
105 #pylab.show()
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700106
107 # Simulate forwards motion.
108 drivetrain = Drivetrain()
109 close_loop_left = []
110 close_loop_right = []
111 R = numpy.matrix([[1.0], [0.0], [1.0], [0.0]])
112 for _ in xrange(100):
113 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
114 drivetrain.U_min, drivetrain.U_max)
115 drivetrain.UpdateObserver(U)
116 drivetrain.Update(U)
117 close_loop_left.append(drivetrain.X[0, 0])
118 close_loop_right.append(drivetrain.X[2, 0])
119
Austin Schuh4352ac62013-03-19 06:23:16 +0000120 #pylab.plot(range(100), close_loop_left)
121 #pylab.plot(range(100), close_loop_right)
122 #pylab.show()
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700123
124 # Try turning in place
125 drivetrain = Drivetrain()
126 close_loop_left = []
127 close_loop_right = []
128 R = numpy.matrix([[-1.0], [0.0], [1.0], [0.0]])
129 for _ in xrange(100):
130 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
131 drivetrain.U_min, drivetrain.U_max)
132 drivetrain.UpdateObserver(U)
133 drivetrain.Update(U)
134 close_loop_left.append(drivetrain.X[0, 0])
135 close_loop_right.append(drivetrain.X[2, 0])
136
Austin Schuh4352ac62013-03-19 06:23:16 +0000137 #pylab.plot(range(100), close_loop_left)
138 #pylab.plot(range(100), close_loop_right)
139 #pylab.show()
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700140
141 # Try turning just one side.
142 drivetrain = Drivetrain()
143 close_loop_left = []
144 close_loop_right = []
145 R = numpy.matrix([[0.0], [0.0], [1.0], [0.0]])
146 for _ in xrange(100):
147 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
148 drivetrain.U_min, drivetrain.U_max)
149 drivetrain.UpdateObserver(U)
150 drivetrain.Update(U)
151 close_loop_left.append(drivetrain.X[0, 0])
152 close_loop_right.append(drivetrain.X[2, 0])
153
Austin Schuh4352ac62013-03-19 06:23:16 +0000154 #pylab.plot(range(100), close_loop_left)
155 #pylab.plot(range(100), close_loop_right)
156 #pylab.show()
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700157
158 # Write the generated constants out to a file.
159 if len(argv) != 3:
160 print "Expected .h file name and .cc file name"
161 else:
Austin Schuh4352ac62013-03-19 06:23:16 +0000162 loop_writer = control_loop.ControlLoopWriter("Drivetrain", [drivetrain])
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700163 if argv[1][-3:] == '.cc':
Austin Schuh4352ac62013-03-19 06:23:16 +0000164 loop_writer.Write(argv[2], argv[1])
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700165 else:
Austin Schuh4352ac62013-03-19 06:23:16 +0000166 loop_writer.Write(argv[1], argv[2])
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700167
168if __name__ == '__main__':
169 sys.exit(main(sys.argv))