blob: c0c459583cff8211a9dbb0c056818af583d027ca [file] [log] [blame]
Comran Morshede9b12922015-11-04 19:46:48 +00001#!/usr/bin/python
2
3import control_loop
4import controls
5import numpy
6import sys
7from matplotlib import pylab
8
9
10class CIM(control_loop.ControlLoop):
11 def __init__(self):
12 super(CIM, self).__init__("CIM")
13 # Stall Torque in N m
14 self.stall_torque = 2.42
15 # Stall Current in Amps
16 self.stall_current = 133
17 # Free Speed in RPM
18 self.free_speed = 4650.0
19 # Free Current in Amps
20 self.free_current = 2.7
21 # Moment of inertia of the CIM in kg m^2
22 self.J = 0.0001
23 # Resistance of the motor, divided by 2 to account for the 2 motors
24 self.R = 12.0 / self.stall_current
25 # Motor velocity constant
26 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
27 (12.0 - self.R * self.free_current))
28 # Torque constant
29 self.Kt = self.stall_torque / self.stall_current
30 # Control loop time step
31 self.dt = 0.005
32
33 # State feedback matrices
34 self.A_continuous = numpy.matrix(
35 [[-self.Kt / self.Kv / (self.J * self.R)]])
36 self.B_continuous = numpy.matrix(
37 [[self.Kt / (self.J * self.R)]])
38 self.C = numpy.matrix([[1]])
39 self.D = numpy.matrix([[0]])
40
41 self.A, self.B = self.ContinuousToDiscrete(self.A_continuous,
42 self.B_continuous, self.dt)
43
44 self.PlaceControllerPoles([0.01])
45 self.PlaceObserverPoles([0.01])
46
47 self.U_max = numpy.matrix([[12.0]])
48 self.U_min = numpy.matrix([[-12.0]])
49
50 self.InitializeState()
51
52
53class Drivetrain(control_loop.ControlLoop):
54 def __init__(self, name="Drivetrain", left_low=True, right_low=True):
55 super(Drivetrain, self).__init__(name)
56 # Stall Torque in N m
57 self.stall_torque = 2.42
58 # Stall Current in Amps
Comran Morshed41ed7c22015-11-04 21:03:37 +000059 self.stall_current = 133.0
Comran Morshede9b12922015-11-04 19:46:48 +000060 # Free Speed in RPM. Used number from last year.
61 self.free_speed = 4650.0
62 # Free Current in Amps
63 self.free_current = 2.7
64 # Moment of inertia of the drivetrain in kg m^2
65 # Just borrowed from last year.
Comran Morshed41ed7c22015-11-04 21:03:37 +000066 self.J = 10
Comran Morshede9b12922015-11-04 19:46:48 +000067 # Mass of the robot, in kg.
Comran Morshed41ed7c22015-11-04 21:03:37 +000068 self.m = 68
Comran Morshede9b12922015-11-04 19:46:48 +000069 # Radius of the robot, in meters (from last year).
Comran Morshed41ed7c22015-11-04 21:03:37 +000070 self.rb = 0.9603 / 2.0
Comran Morshede9b12922015-11-04 19:46:48 +000071 # Radius of the wheels, in meters.
Comran Morshed41ed7c22015-11-04 21:03:37 +000072 self.r = 0.0508
Comran Morshede9b12922015-11-04 19:46:48 +000073 # Resistance of the motor, divided by the number of motors.
Comran Morshed41ed7c22015-11-04 21:03:37 +000074 self.R = 12.0 / self.stall_current / 2
Comran Morshede9b12922015-11-04 19:46:48 +000075 # Motor velocity constant
76 self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) /
77 (12.0 - self.R * self.free_current))
78 # Torque constant
79 self.Kt = self.stall_torque / self.stall_current
80 # Gear ratios
Comran Morshed41ed7c22015-11-04 21:03:37 +000081 self.G_const = 18.0 / 44.0 * 18.0 / 60.0
82
83 self.G_low = self.G_const
84 self.G_high = self.G_const
85
Comran Morshede9b12922015-11-04 19:46:48 +000086 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
Comran Morshed41ed7c22015-11-04 21:03:37 +000094
Comran Morshede9b12922015-11-04 19:46:48 +000095 # Control loop time step
Comran Morshed41ed7c22015-11-04 21:03:37 +000096 self.dt = 0.005
Comran Morshede9b12922015-11-04 19:46:48 +000097
98 # These describe the way that a given side of a robot will be influenced
99 # by the other side. Units of 1 / kg.
100 self.msp = 1.0 / self.m + self.rb * self.rb / self.J
101 self.msn = 1.0 / self.m - self.rb * self.rb / self.J
102 # The calculations which we will need for A and B.
103 self.tcl = -self.Kt / self.Kv / (self.Gl * self.Gl * self.R * self.r * self.r)
104 self.tcr = -self.Kt / self.Kv / (self.Gr * self.Gr * self.R * self.r * self.r)
105 self.mpl = self.Kt / (self.Gl * self.R * self.r)
106 self.mpr = self.Kt / (self.Gr * self.R * self.r)
107
108 # State feedback matrices
109 # X will be of the format
110 # [[positionl], [velocityl], [positionr], velocityr]]
111 self.A_continuous = numpy.matrix(
112 [[0, 1, 0, 0],
113 [0, self.msp * self.tcl, 0, self.msn * self.tcr],
114 [0, 0, 0, 1],
115 [0, self.msn * self.tcl, 0, self.msp * self.tcr]])
116 self.B_continuous = numpy.matrix(
117 [[0, 0],
118 [self.msp * self.mpl, self.msn * self.mpr],
119 [0, 0],
120 [self.msn * self.mpl, self.msp * self.mpr]])
121 self.C = numpy.matrix([[1, 0, 0, 0],
122 [0, 0, 1, 0]])
123 self.D = numpy.matrix([[0, 0],
124 [0, 0]])
125
126 #print "THE NUMBER I WANT" + str(numpy.linalg.inv(self.A_continuous) * -self.B_continuous * numpy.matrix([[12.0], [12.0]]))
127 self.A, self.B = self.ContinuousToDiscrete(
128 self.A_continuous, self.B_continuous, self.dt)
129
130 # Poles from last year.
131 self.hp = 0.65
132 self.lp = 0.83
133 self.PlaceControllerPoles([self.hp, self.lp, self.hp, self.lp])
134 print self.K
135 q_pos = 0.07
136 q_vel = 1.0
137 self.Q = numpy.matrix([[(1.0 / (q_pos ** 2.0)), 0.0, 0.0, 0.0],
138 [0.0, (1.0 / (q_vel ** 2.0)), 0.0, 0.0],
139 [0.0, 0.0, (1.0 / (q_pos ** 2.0)), 0.0],
140 [0.0, 0.0, 0.0, (1.0 / (q_vel ** 2.0))]])
141
142 self.R = numpy.matrix([[(1.0 / (12.0 ** 2.0)), 0.0],
143 [0.0, (1.0 / (12.0 ** 2.0))]])
144 self.K = controls.dlqr(self.A, self.B, self.Q, self.R)
145 print self.A
146 print self.B
147 print self.K
148 print numpy.linalg.eig(self.A - self.B * self.K)[0]
149
150 self.hlp = 0.3
151 self.llp = 0.4
152 self.PlaceObserverPoles([self.hlp, self.hlp, self.llp, self.llp])
153
154 self.U_max = numpy.matrix([[12.0], [12.0]])
155 self.U_min = numpy.matrix([[-12.0], [-12.0]])
156 self.InitializeState()
157
158def main(argv):
159 # Simulate the response of the system to a step input.
160 drivetrain = Drivetrain()
161 simulated_left = []
162 simulated_right = []
163 for _ in xrange(100):
164 drivetrain.Update(numpy.matrix([[12.0], [12.0]]))
165 simulated_left.append(drivetrain.X[0, 0])
166 simulated_right.append(drivetrain.X[2, 0])
167
168 #pylab.plot(range(100), simulated_left)
169 #pylab.plot(range(100), simulated_right)
170 #pylab.show()
171
172 # Simulate forwards motion.
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
185 #pylab.plot(range(100), close_loop_left)
186 #pylab.plot(range(100), close_loop_right)
187 #pylab.show()
188
189 # Try turning in place
190 drivetrain = Drivetrain()
191 close_loop_left = []
192 close_loop_right = []
193 R = numpy.matrix([[-1.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
202 #pylab.plot(range(100), close_loop_left)
203 #pylab.plot(range(100), close_loop_right)
204 #pylab.show()
205
206 # Try turning just one side.
207 drivetrain = Drivetrain()
208 close_loop_left = []
209 close_loop_right = []
210 R = numpy.matrix([[0.0], [0.0], [1.0], [0.0]])
211 for _ in xrange(100):
212 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
213 drivetrain.U_min, drivetrain.U_max)
214 drivetrain.UpdateObserver(U)
215 drivetrain.Update(U)
216 close_loop_left.append(drivetrain.X[0, 0])
217 close_loop_right.append(drivetrain.X[2, 0])
218
219 #pylab.plot(range(100), close_loop_left)
220 #pylab.plot(range(100), close_loop_right)
221 #pylab.show()
222
223 # Write the generated constants out to a file.
224 print "Output one"
225 drivetrain_low_low = Drivetrain(name="DrivetrainLowLow", left_low=True, right_low=True)
226 drivetrain_low_high = Drivetrain(name="DrivetrainLowHigh", left_low=True, right_low=False)
227 drivetrain_high_low = Drivetrain(name="DrivetrainHighLow", left_low=False, right_low=True)
228 drivetrain_high_high = Drivetrain(name="DrivetrainHighHigh", left_low=False, right_low=False)
229
Comran Morshed41ed7c22015-11-04 21:03:37 +0000230 if len(argv) != 5:
Comran Morshede9b12922015-11-04 19:46:48 +0000231 print "Expected .h file name and .cc file name"
232 else:
233 dog_loop_writer = control_loop.ControlLoopWriter(
234 "Drivetrain", [drivetrain_low_low, drivetrain_low_high,
235 drivetrain_high_low, drivetrain_high_high],
Comran Morshed41ed7c22015-11-04 21:03:37 +0000236 namespaces=['y2014_bot3', 'control_loops'])
Comran Morshede9b12922015-11-04 19:46:48 +0000237 if argv[1][-3:] == '.cc':
238 dog_loop_writer.Write(argv[2], argv[1])
239 else:
240 dog_loop_writer.Write(argv[1], argv[2])
241
242if __name__ == '__main__':
243 sys.exit(main(sys.argv))