blob: fcca56a1c934ba84c7854ca448bbc0acca41e35c [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):
Brian Silverman2c590c32013-11-04 18:08:54 -080053 def __init__(self, left_low=True, right_low=True, is_clutch=False):
54 super(Drivetrain, self).__init__(("Clutch" if is_clutch else "Dog" )+"Drivetrain")
James Kuszmaulf254c1a2013-03-10 16:31:26 -070055 # 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.
Brian Silverman55a930b2013-11-04 20:59:00 -080065 self.J = 4.5
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
Brian Silverman2c590c32013-11-04 18:08:54 -080080 if is_clutch:
81 self.G_low = 14.0 / 60.0 * 15.0 / 50.0
82 self.G_high = 30.0 / 44.0 * 15.0 / 50.0
83 else:
84 self.G_low = 16.0 / 60.0 * 17.0 / 50.0
85 self.G_high = 28.0 / 48.0 * 17.0 / 50.0
Austin Schuhde4d7fe2013-10-08 22:22:45 -070086 if left_low:
87 self.Gl = self.G_low
88 else:
89 self.Gl = self.G_high
90 if right_low:
91 self.Gr = self.G_low
92 else:
93 self.Gr = self.G_high
James Kuszmaulf254c1a2013-03-10 16:31:26 -070094 # Control loop time step
95 self.dt = 0.01
96
97 # These describe the way that a given side of a robot will be influenced
98 # by the other side. Units of 1 / kg.
99 self.msp = 1.0 / self.m + self.rb * self.rb / self.J
100 self.msn = 1.0 / self.m - self.rb * self.rb / self.J
101 # The calculations which we will need for A and B.
Austin Schuhde4d7fe2013-10-08 22:22:45 -0700102 self.tcl = -self.Kt / self.Kv / (self.Gl * self.Gl * self.R * self.r * self.r)
103 self.tcr = -self.Kt / self.Kv / (self.Gr * self.Gr * self.R * self.r * self.r)
104 self.mpl = self.Kt / (self.Gl * self.R * self.r)
105 self.mpr = self.Kt / (self.Gr * self.R * self.r)
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700106
107 # State feedback matrices
108 # X will be of the format
Austin Schuhde4d7fe2013-10-08 22:22:45 -0700109 # [[positionl], [velocityl], [positionr], velocityr]]
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700110 self.A_continuous = numpy.matrix(
111 [[0, 1, 0, 0],
Austin Schuhde4d7fe2013-10-08 22:22:45 -0700112 [0, self.msp * self.tcl, 0, self.msn * self.tcr],
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700113 [0, 0, 0, 1],
Austin Schuhde4d7fe2013-10-08 22:22:45 -0700114 [0, self.msn * self.tcl, 0, self.msp * self.tcr]])
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700115 self.B_continuous = numpy.matrix(
116 [[0, 0],
Austin Schuhde4d7fe2013-10-08 22:22:45 -0700117 [self.msp * self.mpl, self.msn * self.mpr],
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700118 [0, 0],
Austin Schuhde4d7fe2013-10-08 22:22:45 -0700119 [self.msn * self.mpl, self.msp * self.mpr]])
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700120 self.C = numpy.matrix([[1, 0, 0, 0],
121 [0, 0, 1, 0]])
122 self.D = numpy.matrix([[0, 0],
123 [0, 0]])
124
Austin Schuh4352ac62013-03-19 06:23:16 +0000125 self.A, self.B = self.ContinuousToDiscrete(
126 self.A_continuous, self.B_continuous, self.dt)
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700127
128 # Poles from last year.
Austin Schuh4352ac62013-03-19 06:23:16 +0000129 self.hp = 0.65
130 self.lp = 0.83
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700131 self.PlaceControllerPoles([self.hp, self.hp, self.lp, self.lp])
132
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700133 self.hlp = 0.07
134 self.llp = 0.09
135 self.PlaceObserverPoles([self.hlp, self.hlp, self.llp, self.llp])
136
137 self.U_max = numpy.matrix([[12.0], [12.0]])
138 self.U_min = numpy.matrix([[-12.0], [-12.0]])
Austin Schuh4352ac62013-03-19 06:23:16 +0000139 self.InitializeState()
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700140
141def main(argv):
142 # Simulate the response of the system to a step input.
143 drivetrain = Drivetrain()
144 simulated_left = []
145 simulated_right = []
146 for _ in xrange(100):
147 drivetrain.Update(numpy.matrix([[12.0], [12.0]]))
148 simulated_left.append(drivetrain.X[0, 0])
149 simulated_right.append(drivetrain.X[2, 0])
150
Austin Schuh4352ac62013-03-19 06:23:16 +0000151 #pylab.plot(range(100), simulated_left)
152 #pylab.plot(range(100), simulated_right)
153 #pylab.show()
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700154
155 # Simulate forwards motion.
156 drivetrain = Drivetrain()
157 close_loop_left = []
158 close_loop_right = []
159 R = numpy.matrix([[1.0], [0.0], [1.0], [0.0]])
160 for _ in xrange(100):
161 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
162 drivetrain.U_min, drivetrain.U_max)
163 drivetrain.UpdateObserver(U)
164 drivetrain.Update(U)
165 close_loop_left.append(drivetrain.X[0, 0])
166 close_loop_right.append(drivetrain.X[2, 0])
167
Austin Schuh4352ac62013-03-19 06:23:16 +0000168 #pylab.plot(range(100), close_loop_left)
169 #pylab.plot(range(100), close_loop_right)
170 #pylab.show()
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700171
172 # Try turning in place
173 drivetrain = Drivetrain()
174 close_loop_left = []
175 close_loop_right = []
176 R = numpy.matrix([[-1.0], [0.0], [1.0], [0.0]])
177 for _ in xrange(100):
178 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
179 drivetrain.U_min, drivetrain.U_max)
180 drivetrain.UpdateObserver(U)
181 drivetrain.Update(U)
182 close_loop_left.append(drivetrain.X[0, 0])
183 close_loop_right.append(drivetrain.X[2, 0])
184
Austin Schuh4352ac62013-03-19 06:23:16 +0000185 #pylab.plot(range(100), close_loop_left)
186 #pylab.plot(range(100), close_loop_right)
187 #pylab.show()
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700188
189 # Try turning just one side.
190 drivetrain = Drivetrain()
191 close_loop_left = []
192 close_loop_right = []
193 R = numpy.matrix([[0.0], [0.0], [1.0], [0.0]])
194 for _ in xrange(100):
195 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
196 drivetrain.U_min, drivetrain.U_max)
197 drivetrain.UpdateObserver(U)
198 drivetrain.Update(U)
199 close_loop_left.append(drivetrain.X[0, 0])
200 close_loop_right.append(drivetrain.X[2, 0])
201
Austin Schuh4352ac62013-03-19 06:23:16 +0000202 #pylab.plot(range(100), close_loop_left)
203 #pylab.plot(range(100), close_loop_right)
204 #pylab.show()
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700205
206 # Write the generated constants out to a file.
Brian Silverman2c590c32013-11-04 18:08:54 -0800207 dog_drivetrain = Drivetrain(is_clutch=False)
208 clutch_drivetrain = Drivetrain(is_clutch=True)
209
210 if len(argv) != 5:
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700211 print "Expected .h file name and .cc file name"
212 else:
Brian Silverman2c590c32013-11-04 18:08:54 -0800213 dog_loop_writer = control_loop.ControlLoopWriter("DogDrivetrain", [dog_drivetrain])
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700214 if argv[1][-3:] == '.cc':
Brian Silverman2c590c32013-11-04 18:08:54 -0800215 dog_loop_writer.Write(argv[2], argv[1])
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700216 else:
Brian Silverman2c590c32013-11-04 18:08:54 -0800217 dog_loop_writer.Write(argv[1], argv[2])
218
219 clutch_loop_writer = control_loop.ControlLoopWriter("ClutchDrivetrain", [clutch_drivetrain])
220 if argv[3][-3:] == '.cc':
221 clutch_loop_writer.Write(argv[4], argv[3])
222 else:
223 clutch_loop_writer.Write(argv[3], argv[4])
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700224
225if __name__ == '__main__':
226 sys.exit(main(sys.argv))