blob: 27bebbcf14f4b6f20e1831612e719cd603a6b187 [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
Austin Schuh8afe35a2013-10-27 10:59:15 -07008
9class CIM(control_loop.ControlLoop):
10 def __init__(self):
11 super(CIM, self).__init__("CIM")
12 # Stall Torque in N m
13 self.stall_torque = 2.42
14 # Stall Current in Amps
15 self.stall_current = 133
16 # Free Speed in RPM
17 self.free_speed = 4650.0
18 # Free Current in Amps
19 self.free_current = 2.7
20 # Moment of inertia of the CIM in kg m^2
21 self.J = 0.0001
22 # Resistance of the motor, divided by 2 to account for the 2 motors
23 self.R = 12.0 / self.stall_current
24 # Motor velocity constant
25 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
26 (12.0 - self.R * self.free_current))
27 # Torque constant
28 self.Kt = self.stall_torque / self.stall_current
29 # Control loop time step
30 self.dt = 0.01
31
32 # State feedback matrices
33 self.A_continuous = numpy.matrix(
34 [[-self.Kt / self.Kv / (self.J * self.R)]])
35 self.B_continuous = numpy.matrix(
36 [[self.Kt / (self.J * self.R)]])
37 self.C = numpy.matrix([[1]])
38 self.D = numpy.matrix([[0]])
39
40 self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
41 self.B_continuous, self.dt)
42
43 self.PlaceControllerPoles([0.01])
Austin Schuh427b3702013-11-02 13:44:09 -070044 self.PlaceObserverPoles([0.01])
Austin Schuh8afe35a2013-10-27 10:59:15 -070045
46 self.U_max = numpy.matrix([[12.0]])
47 self.U_min = numpy.matrix([[-12.0]])
48
49 self.InitializeState()
50
51
James Kuszmaulf254c1a2013-03-10 16:31:26 -070052class Drivetrain(control_loop.ControlLoop):
Austin Schuhde4d7fe2013-10-08 22:22:45 -070053 def __init__(self, left_low=True, right_low=True):
James Kuszmaulf254c1a2013-03-10 16:31:26 -070054 super(Drivetrain, self).__init__("Drivetrain")
55 # Stall Torque in N m
56 self.stall_torque = 2.42
57 # Stall Current in Amps
58 self.stall_current = 133
59 # Free Speed in RPM. Used number from last year.
60 self.free_speed = 4650.0
61 # Free Current in Amps
62 self.free_current = 2.7
63 # Moment of inertia of the drivetrain in kg m^2
64 # Just borrowed from last year.
Austin Schuh4352ac62013-03-19 06:23:16 +000065 self.J = 6.4
James Kuszmaulf254c1a2013-03-10 16:31:26 -070066 # Mass of the robot, in kg.
67 self.m = 68
68 # Radius of the robot, in meters (from last year).
69 self.rb = 0.617998644 / 2.0
70 # Radius of the wheels, in meters.
71 self.r = .04445
72 # Resistance of the motor, divided by the number of motors.
Austin Schuh8afe35a2013-10-27 10:59:15 -070073 self.R = (12.0 / self.stall_current / 4 + 0.03) / (0.93 ** 2.0)
James Kuszmaulf254c1a2013-03-10 16:31:26 -070074 # Motor velocity constant
75 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
76 (12.0 - self.R * self.free_current))
77 # Torque constant
78 self.Kt = self.stall_torque / self.stall_current
79 # Gear ratios
80 self.G_low = 16.0 / 60.0 * 19.0 / 50.0
81 self.G_high = 28.0 / 48.0 * 19.0 / 50.0
Austin Schuhde4d7fe2013-10-08 22:22:45 -070082 if left_low:
83 self.Gl = self.G_low
84 else:
85 self.Gl = self.G_high
86 if right_low:
87 self.Gr = self.G_low
88 else:
89 self.Gr = self.G_high
James Kuszmaulf254c1a2013-03-10 16:31:26 -070090 # Control loop time step
91 self.dt = 0.01
92
93 # These describe the way that a given side of a robot will be influenced
94 # by the other side. Units of 1 / kg.
95 self.msp = 1.0 / self.m + self.rb * self.rb / self.J
96 self.msn = 1.0 / self.m - self.rb * self.rb / self.J
97 # The calculations which we will need for A and B.
Austin Schuhde4d7fe2013-10-08 22:22:45 -070098 self.tcl = -self.Kt / self.Kv / (self.Gl * self.Gl * self.R * self.r * self.r)
99 self.tcr = -self.Kt / self.Kv / (self.Gr * self.Gr * self.R * self.r * self.r)
100 self.mpl = self.Kt / (self.Gl * self.R * self.r)
101 self.mpr = self.Kt / (self.Gr * self.R * self.r)
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700102
103 # State feedback matrices
104 # X will be of the format
Austin Schuhde4d7fe2013-10-08 22:22:45 -0700105 # [[positionl], [velocityl], [positionr], velocityr]]
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700106 self.A_continuous = numpy.matrix(
107 [[0, 1, 0, 0],
Austin Schuhde4d7fe2013-10-08 22:22:45 -0700108 [0, self.msp * self.tcl, 0, self.msn * self.tcr],
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700109 [0, 0, 0, 1],
Austin Schuhde4d7fe2013-10-08 22:22:45 -0700110 [0, self.msn * self.tcl, 0, self.msp * self.tcr]])
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700111 self.B_continuous = numpy.matrix(
112 [[0, 0],
Austin Schuhde4d7fe2013-10-08 22:22:45 -0700113 [self.msp * self.mpl, self.msn * self.mpr],
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700114 [0, 0],
Austin Schuhde4d7fe2013-10-08 22:22:45 -0700115 [self.msn * self.mpl, self.msp * self.mpr]])
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700116 self.C = numpy.matrix([[1, 0, 0, 0],
117 [0, 0, 1, 0]])
118 self.D = numpy.matrix([[0, 0],
119 [0, 0]])
120
Austin Schuh4352ac62013-03-19 06:23:16 +0000121 self.A, self.B = self.ContinuousToDiscrete(
122 self.A_continuous, self.B_continuous, self.dt)
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700123
124 # Poles from last year.
Austin Schuh4352ac62013-03-19 06:23:16 +0000125 self.hp = 0.65
126 self.lp = 0.83
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700127 self.PlaceControllerPoles([self.hp, self.hp, self.lp, self.lp])
128
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700129 self.hlp = 0.07
130 self.llp = 0.09
131 self.PlaceObserverPoles([self.hlp, self.hlp, self.llp, self.llp])
132
133 self.U_max = numpy.matrix([[12.0], [12.0]])
134 self.U_min = numpy.matrix([[-12.0], [-12.0]])
Austin Schuh4352ac62013-03-19 06:23:16 +0000135 self.InitializeState()
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700136
137def main(argv):
138 # Simulate the response of the system to a step input.
139 drivetrain = Drivetrain()
140 simulated_left = []
141 simulated_right = []
142 for _ in xrange(100):
143 drivetrain.Update(numpy.matrix([[12.0], [12.0]]))
144 simulated_left.append(drivetrain.X[0, 0])
145 simulated_right.append(drivetrain.X[2, 0])
146
Austin Schuh4352ac62013-03-19 06:23:16 +0000147 #pylab.plot(range(100), simulated_left)
148 #pylab.plot(range(100), simulated_right)
149 #pylab.show()
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700150
151 # Simulate forwards motion.
152 drivetrain = Drivetrain()
153 close_loop_left = []
154 close_loop_right = []
155 R = numpy.matrix([[1.0], [0.0], [1.0], [0.0]])
156 for _ in xrange(100):
157 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
158 drivetrain.U_min, drivetrain.U_max)
159 drivetrain.UpdateObserver(U)
160 drivetrain.Update(U)
161 close_loop_left.append(drivetrain.X[0, 0])
162 close_loop_right.append(drivetrain.X[2, 0])
163
Austin Schuh4352ac62013-03-19 06:23:16 +0000164 #pylab.plot(range(100), close_loop_left)
165 #pylab.plot(range(100), close_loop_right)
166 #pylab.show()
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700167
168 # Try turning in place
169 drivetrain = Drivetrain()
170 close_loop_left = []
171 close_loop_right = []
172 R = numpy.matrix([[-1.0], [0.0], [1.0], [0.0]])
173 for _ in xrange(100):
174 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
175 drivetrain.U_min, drivetrain.U_max)
176 drivetrain.UpdateObserver(U)
177 drivetrain.Update(U)
178 close_loop_left.append(drivetrain.X[0, 0])
179 close_loop_right.append(drivetrain.X[2, 0])
180
Austin Schuh4352ac62013-03-19 06:23:16 +0000181 #pylab.plot(range(100), close_loop_left)
182 #pylab.plot(range(100), close_loop_right)
183 #pylab.show()
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700184
185 # Try turning just one side.
186 drivetrain = Drivetrain()
187 close_loop_left = []
188 close_loop_right = []
189 R = numpy.matrix([[0.0], [0.0], [1.0], [0.0]])
190 for _ in xrange(100):
191 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
192 drivetrain.U_min, drivetrain.U_max)
193 drivetrain.UpdateObserver(U)
194 drivetrain.Update(U)
195 close_loop_left.append(drivetrain.X[0, 0])
196 close_loop_right.append(drivetrain.X[2, 0])
197
Austin Schuh4352ac62013-03-19 06:23:16 +0000198 #pylab.plot(range(100), close_loop_left)
199 #pylab.plot(range(100), close_loop_right)
200 #pylab.show()
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700201
202 # Write the generated constants out to a file.
203 if len(argv) != 3:
204 print "Expected .h file name and .cc file name"
205 else:
Austin Schuh4352ac62013-03-19 06:23:16 +0000206 loop_writer = control_loop.ControlLoopWriter("Drivetrain", [drivetrain])
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700207 if argv[1][-3:] == '.cc':
Austin Schuh4352ac62013-03-19 06:23:16 +0000208 loop_writer.Write(argv[2], argv[1])
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700209 else:
Austin Schuh4352ac62013-03-19 06:23:16 +0000210 loop_writer.Write(argv[1], argv[2])
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700211
212if __name__ == '__main__':
213 sys.exit(main(sys.argv))