blob: 0e791cba3f11482fb9b8704fdf07ad5792fc596c [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):
9 def __init__(self):
10 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
38 self.G = self.G_low
39 # Control loop time step
40 self.dt = 0.01
41
42 # These describe the way that a given side of a robot will be influenced
43 # by the other side. Units of 1 / kg.
44 self.msp = 1.0 / self.m + self.rb * self.rb / self.J
45 self.msn = 1.0 / self.m - self.rb * self.rb / self.J
46 # The calculations which we will need for A and B.
47 self.tc = -self.Kt / self.Kv / (self.G * self.G * self.R * self.r * self.r)
48 self.mp = self.Kt / (self.G * self.R * self.r)
49
50 # State feedback matrices
51 # X will be of the format
52 # [[position1], [velocity1], [position2], velocity2]]
53 self.A_continuous = numpy.matrix(
54 [[0, 1, 0, 0],
55 [0, self.msp * self.tc, 0, self.msn * self.tc],
56 [0, 0, 0, 1],
57 [0, self.msn * self.tc, 0, self.msp * self.tc]])
58 self.B_continuous = numpy.matrix(
59 [[0, 0],
60 [self.msp * self.mp, self.msn * self.mp],
61 [0, 0],
62 [self.msn * self.mp, self.msp * self.mp]])
63 self.C = numpy.matrix([[1, 0, 0, 0],
64 [0, 0, 1, 0]])
65 self.D = numpy.matrix([[0, 0],
66 [0, 0]])
67
Austin Schuh4352ac62013-03-19 06:23:16 +000068 self.A, self.B = self.ContinuousToDiscrete(
69 self.A_continuous, self.B_continuous, self.dt)
James Kuszmaulf254c1a2013-03-10 16:31:26 -070070
71 # Poles from last year.
Austin Schuh4352ac62013-03-19 06:23:16 +000072 self.hp = 0.65
73 self.lp = 0.83
James Kuszmaulf254c1a2013-03-10 16:31:26 -070074 self.PlaceControllerPoles([self.hp, self.hp, self.lp, self.lp])
75
76 print self.K
77
78 self.hlp = 0.07
79 self.llp = 0.09
80 self.PlaceObserverPoles([self.hlp, self.hlp, self.llp, self.llp])
81
82 self.U_max = numpy.matrix([[12.0], [12.0]])
83 self.U_min = numpy.matrix([[-12.0], [-12.0]])
Austin Schuh4352ac62013-03-19 06:23:16 +000084 self.InitializeState()
James Kuszmaulf254c1a2013-03-10 16:31:26 -070085
86def main(argv):
87 # Simulate the response of the system to a step input.
88 drivetrain = Drivetrain()
89 simulated_left = []
90 simulated_right = []
91 for _ in xrange(100):
92 drivetrain.Update(numpy.matrix([[12.0], [12.0]]))
93 simulated_left.append(drivetrain.X[0, 0])
94 simulated_right.append(drivetrain.X[2, 0])
95
Austin Schuh4352ac62013-03-19 06:23:16 +000096 #pylab.plot(range(100), simulated_left)
97 #pylab.plot(range(100), simulated_right)
98 #pylab.show()
James Kuszmaulf254c1a2013-03-10 16:31:26 -070099
100 # Simulate forwards motion.
101 drivetrain = Drivetrain()
102 close_loop_left = []
103 close_loop_right = []
104 R = numpy.matrix([[1.0], [0.0], [1.0], [0.0]])
105 for _ in xrange(100):
106 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
107 drivetrain.U_min, drivetrain.U_max)
108 drivetrain.UpdateObserver(U)
109 drivetrain.Update(U)
110 close_loop_left.append(drivetrain.X[0, 0])
111 close_loop_right.append(drivetrain.X[2, 0])
112
Austin Schuh4352ac62013-03-19 06:23:16 +0000113 #pylab.plot(range(100), close_loop_left)
114 #pylab.plot(range(100), close_loop_right)
115 #pylab.show()
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700116
117 # Try turning in place
118 drivetrain = Drivetrain()
119 close_loop_left = []
120 close_loop_right = []
121 R = numpy.matrix([[-1.0], [0.0], [1.0], [0.0]])
122 for _ in xrange(100):
123 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
124 drivetrain.U_min, drivetrain.U_max)
125 drivetrain.UpdateObserver(U)
126 drivetrain.Update(U)
127 close_loop_left.append(drivetrain.X[0, 0])
128 close_loop_right.append(drivetrain.X[2, 0])
129
Austin Schuh4352ac62013-03-19 06:23:16 +0000130 #pylab.plot(range(100), close_loop_left)
131 #pylab.plot(range(100), close_loop_right)
132 #pylab.show()
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700133
134 # Try turning just one side.
135 drivetrain = Drivetrain()
136 close_loop_left = []
137 close_loop_right = []
138 R = numpy.matrix([[0.0], [0.0], [1.0], [0.0]])
139 for _ in xrange(100):
140 U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat),
141 drivetrain.U_min, drivetrain.U_max)
142 drivetrain.UpdateObserver(U)
143 drivetrain.Update(U)
144 close_loop_left.append(drivetrain.X[0, 0])
145 close_loop_right.append(drivetrain.X[2, 0])
146
Austin Schuh4352ac62013-03-19 06:23:16 +0000147 #pylab.plot(range(100), close_loop_left)
148 #pylab.plot(range(100), close_loop_right)
149 #pylab.show()
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700150
151 # Write the generated constants out to a file.
152 if len(argv) != 3:
153 print "Expected .h file name and .cc file name"
154 else:
Austin Schuh4352ac62013-03-19 06:23:16 +0000155 loop_writer = control_loop.ControlLoopWriter("Drivetrain", [drivetrain])
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700156 if argv[1][-3:] == '.cc':
Austin Schuh4352ac62013-03-19 06:23:16 +0000157 loop_writer.Write(argv[2], argv[1])
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700158 else:
Austin Schuh4352ac62013-03-19 06:23:16 +0000159 loop_writer.Write(argv[1], argv[2])
James Kuszmaulf254c1a2013-03-10 16:31:26 -0700160
161if __name__ == '__main__':
162 sys.exit(main(sys.argv))