blob: 8b1d9ad6ae2bb2d061be782e8c69a2d37b6b2953 [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):
James Kuszmaule1755b32014-02-13 06:27:48 -080053 def __init__(self, left_low=True, right_low=True):
54 super(Drivetrain, self).__init__("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.
James Kuszmaule1755b32014-02-13 06:27:48 -080073 self.R = 12.0 / self.stall_current / 4
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
James Kuszmaule1755b32014-02-13 06:27:48 -080080 self.G_low = 18.0 / 60.0 * 18.0 / 50.0
81 self.G_high = 28.0 / 50.0 * 18.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
Ben Fredrickson890c3fe2014-03-02 00:15:16 +000092
93 print "THE NUMBER I WANT" + str((self.free_speed / 60.0 * 2.0 * numpy.pi) * self.G_high * self.r)
James Kuszmaulf254c1a2013-03-10 16:31:26 -070094
95 # These describe the way that a given side of a robot will be influenced
96 # by the other side. Units of 1 / kg.
97 self.msp = 1.0 / self.m + self.rb * self.rb / self.J
98 self.msn = 1.0 / self.m - self.rb * self.rb / self.J
99 # The calculations which we will need for A and B.
Austin Schuhde4d7fe2013-10-08 22:22:45 -0700100 self.tcl = -self.Kt / self.Kv / (self.Gl * self.Gl * self.R * self.r * self.r)
101 self.tcr = -self.Kt / self.Kv / (self.Gr * self.Gr * self.R * self.r * self.r)
102 self.mpl = self.Kt / (self.Gl * self.R * self.r)
103 self.mpr = self.Kt / (self.Gr * self.R * self.r)
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700104
105 # State feedback matrices
106 # X will be of the format
Austin Schuhde4d7fe2013-10-08 22:22:45 -0700107 # [[positionl], [velocityl], [positionr], velocityr]]
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700108 self.A_continuous = numpy.matrix(
109 [[0, 1, 0, 0],
Austin Schuhde4d7fe2013-10-08 22:22:45 -0700110 [0, self.msp * self.tcl, 0, self.msn * self.tcr],
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700111 [0, 0, 0, 1],
Austin Schuhde4d7fe2013-10-08 22:22:45 -0700112 [0, self.msn * self.tcl, 0, self.msp * self.tcr]])
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700113 self.B_continuous = numpy.matrix(
114 [[0, 0],
Austin Schuhde4d7fe2013-10-08 22:22:45 -0700115 [self.msp * self.mpl, self.msn * self.mpr],
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700116 [0, 0],
Austin Schuhde4d7fe2013-10-08 22:22:45 -0700117 [self.msn * self.mpl, self.msp * self.mpr]])
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700118 self.C = numpy.matrix([[1, 0, 0, 0],
119 [0, 0, 1, 0]])
120 self.D = numpy.matrix([[0, 0],
121 [0, 0]])
122
Ben Fredrickson890c3fe2014-03-02 00:15:16 +0000123 #print "THE NUMBER I WANT" + str(numpy.linalg.inv(self.A_continuous) * -self.B_continuous * numpy.matrix([[12.0], [12.0]]))
Austin Schuh4352ac62013-03-19 06:23:16 +0000124 self.A, self.B = self.ContinuousToDiscrete(
125 self.A_continuous, self.B_continuous, self.dt)
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700126
127 # Poles from last year.
Austin Schuh4352ac62013-03-19 06:23:16 +0000128 self.hp = 0.65
129 self.lp = 0.83
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700130 self.PlaceControllerPoles([self.hp, self.hp, self.lp, self.lp])
131
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700132 self.hlp = 0.07
133 self.llp = 0.09
134 self.PlaceObserverPoles([self.hlp, self.hlp, self.llp, self.llp])
135
136 self.U_max = numpy.matrix([[12.0], [12.0]])
137 self.U_min = numpy.matrix([[-12.0], [-12.0]])
Austin Schuh4352ac62013-03-19 06:23:16 +0000138 self.InitializeState()
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700139
140def main(argv):
141 # Simulate the response of the system to a step input.
142 drivetrain = Drivetrain()
143 simulated_left = []
144 simulated_right = []
145 for _ in xrange(100):
146 drivetrain.Update(numpy.matrix([[12.0], [12.0]]))
147 simulated_left.append(drivetrain.X[0, 0])
148 simulated_right.append(drivetrain.X[2, 0])
149
Austin Schuh4352ac62013-03-19 06:23:16 +0000150 #pylab.plot(range(100), simulated_left)
151 #pylab.plot(range(100), simulated_right)
152 #pylab.show()
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700153
154 # Simulate forwards motion.
155 drivetrain = Drivetrain()
156 close_loop_left = []
157 close_loop_right = []
158 R = numpy.matrix([[1.0], [0.0], [1.0], [0.0]])
159 for _ in xrange(100):
160 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
161 drivetrain.U_min, drivetrain.U_max)
162 drivetrain.UpdateObserver(U)
163 drivetrain.Update(U)
164 close_loop_left.append(drivetrain.X[0, 0])
165 close_loop_right.append(drivetrain.X[2, 0])
166
Austin Schuh4352ac62013-03-19 06:23:16 +0000167 #pylab.plot(range(100), close_loop_left)
168 #pylab.plot(range(100), close_loop_right)
169 #pylab.show()
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700170
171 # Try turning in place
172 drivetrain = Drivetrain()
173 close_loop_left = []
174 close_loop_right = []
175 R = numpy.matrix([[-1.0], [0.0], [1.0], [0.0]])
176 for _ in xrange(100):
177 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
178 drivetrain.U_min, drivetrain.U_max)
179 drivetrain.UpdateObserver(U)
180 drivetrain.Update(U)
181 close_loop_left.append(drivetrain.X[0, 0])
182 close_loop_right.append(drivetrain.X[2, 0])
183
Austin Schuh4352ac62013-03-19 06:23:16 +0000184 #pylab.plot(range(100), close_loop_left)
185 #pylab.plot(range(100), close_loop_right)
186 #pylab.show()
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700187
188 # Try turning just one side.
189 drivetrain = Drivetrain()
190 close_loop_left = []
191 close_loop_right = []
192 R = numpy.matrix([[0.0], [0.0], [1.0], [0.0]])
193 for _ in xrange(100):
194 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
195 drivetrain.U_min, drivetrain.U_max)
196 drivetrain.UpdateObserver(U)
197 drivetrain.Update(U)
198 close_loop_left.append(drivetrain.X[0, 0])
199 close_loop_right.append(drivetrain.X[2, 0])
200
Austin Schuh4352ac62013-03-19 06:23:16 +0000201 #pylab.plot(range(100), close_loop_left)
202 #pylab.plot(range(100), close_loop_right)
203 #pylab.show()
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700204
205 # Write the generated constants out to a file.
James Kuszmaule1755b32014-02-13 06:27:48 -0800206 drivetrain = Drivetrain()
Brian Silverman2c590c32013-11-04 18:08:54 -0800207
208 if len(argv) != 5:
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700209 print "Expected .h file name and .cc file name"
210 else:
James Kuszmaule1755b32014-02-13 06:27:48 -0800211 dog_loop_writer = control_loop.ControlLoopWriter("Drivetrain", [drivetrain])
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700212 if argv[1][-3:] == '.cc':
Brian Silverman2c590c32013-11-04 18:08:54 -0800213 dog_loop_writer.Write(argv[2], argv[1])
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700214 else:
Brian Silverman2c590c32013-11-04 18:08:54 -0800215 dog_loop_writer.Write(argv[1], argv[2])
216
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700217if __name__ == '__main__':
218 sys.exit(main(sys.argv))