James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import control_loop |
| 4 | import numpy |
| 5 | import sys |
| 6 | from matplotlib import pylab |
| 7 | |
Austin Schuh | 8afe35a | 2013-10-27 10:59:15 -0700 | [diff] [blame^] | 8 | |
| 9 | class 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]) |
| 44 | |
| 45 | self.U_max = numpy.matrix([[12.0]]) |
| 46 | self.U_min = numpy.matrix([[-12.0]]) |
| 47 | |
| 48 | self.InitializeState() |
| 49 | |
| 50 | |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 51 | class Drivetrain(control_loop.ControlLoop): |
Austin Schuh | de4d7fe | 2013-10-08 22:22:45 -0700 | [diff] [blame] | 52 | def __init__(self, left_low=True, right_low=True): |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 53 | super(Drivetrain, self).__init__("Drivetrain") |
| 54 | # Stall Torque in N m |
| 55 | self.stall_torque = 2.42 |
| 56 | # Stall Current in Amps |
| 57 | self.stall_current = 133 |
| 58 | # Free Speed in RPM. Used number from last year. |
| 59 | self.free_speed = 4650.0 |
| 60 | # Free Current in Amps |
| 61 | self.free_current = 2.7 |
| 62 | # Moment of inertia of the drivetrain in kg m^2 |
| 63 | # Just borrowed from last year. |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 64 | self.J = 6.4 |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 65 | # Mass of the robot, in kg. |
| 66 | self.m = 68 |
| 67 | # Radius of the robot, in meters (from last year). |
| 68 | self.rb = 0.617998644 / 2.0 |
| 69 | # Radius of the wheels, in meters. |
| 70 | self.r = .04445 |
| 71 | # Resistance of the motor, divided by the number of motors. |
Austin Schuh | 8afe35a | 2013-10-27 10:59:15 -0700 | [diff] [blame^] | 72 | self.R = (12.0 / self.stall_current / 4 + 0.03) / (0.93 ** 2.0) |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 73 | # Motor velocity constant |
| 74 | self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) / |
| 75 | (12.0 - self.R * self.free_current)) |
| 76 | # Torque constant |
| 77 | self.Kt = self.stall_torque / self.stall_current |
| 78 | # Gear ratios |
| 79 | self.G_low = 16.0 / 60.0 * 19.0 / 50.0 |
| 80 | self.G_high = 28.0 / 48.0 * 19.0 / 50.0 |
Austin Schuh | de4d7fe | 2013-10-08 22:22:45 -0700 | [diff] [blame] | 81 | if left_low: |
| 82 | self.Gl = self.G_low |
| 83 | else: |
| 84 | self.Gl = self.G_high |
| 85 | if right_low: |
| 86 | self.Gr = self.G_low |
| 87 | else: |
| 88 | self.Gr = self.G_high |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 89 | # Control loop time step |
| 90 | self.dt = 0.01 |
| 91 | |
| 92 | # These describe the way that a given side of a robot will be influenced |
| 93 | # by the other side. Units of 1 / kg. |
| 94 | self.msp = 1.0 / self.m + self.rb * self.rb / self.J |
| 95 | self.msn = 1.0 / self.m - self.rb * self.rb / self.J |
| 96 | # The calculations which we will need for A and B. |
Austin Schuh | de4d7fe | 2013-10-08 22:22:45 -0700 | [diff] [blame] | 97 | self.tcl = -self.Kt / self.Kv / (self.Gl * self.Gl * self.R * self.r * self.r) |
| 98 | self.tcr = -self.Kt / self.Kv / (self.Gr * self.Gr * self.R * self.r * self.r) |
| 99 | self.mpl = self.Kt / (self.Gl * self.R * self.r) |
| 100 | self.mpr = self.Kt / (self.Gr * self.R * self.r) |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 101 | |
| 102 | # State feedback matrices |
| 103 | # X will be of the format |
Austin Schuh | de4d7fe | 2013-10-08 22:22:45 -0700 | [diff] [blame] | 104 | # [[positionl], [velocityl], [positionr], velocityr]] |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 105 | self.A_continuous = numpy.matrix( |
| 106 | [[0, 1, 0, 0], |
Austin Schuh | de4d7fe | 2013-10-08 22:22:45 -0700 | [diff] [blame] | 107 | [0, self.msp * self.tcl, 0, self.msn * self.tcr], |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 108 | [0, 0, 0, 1], |
Austin Schuh | de4d7fe | 2013-10-08 22:22:45 -0700 | [diff] [blame] | 109 | [0, self.msn * self.tcl, 0, self.msp * self.tcr]]) |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 110 | self.B_continuous = numpy.matrix( |
| 111 | [[0, 0], |
Austin Schuh | de4d7fe | 2013-10-08 22:22:45 -0700 | [diff] [blame] | 112 | [self.msp * self.mpl, self.msn * self.mpr], |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 113 | [0, 0], |
Austin Schuh | de4d7fe | 2013-10-08 22:22:45 -0700 | [diff] [blame] | 114 | [self.msn * self.mpl, self.msp * self.mpr]]) |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 115 | self.C = numpy.matrix([[1, 0, 0, 0], |
| 116 | [0, 0, 1, 0]]) |
| 117 | self.D = numpy.matrix([[0, 0], |
| 118 | [0, 0]]) |
| 119 | |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 120 | self.A, self.B = self.ContinuousToDiscrete( |
| 121 | self.A_continuous, self.B_continuous, self.dt) |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 122 | |
| 123 | # Poles from last year. |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 124 | self.hp = 0.65 |
| 125 | self.lp = 0.83 |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 126 | self.PlaceControllerPoles([self.hp, self.hp, self.lp, self.lp]) |
| 127 | |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 128 | self.hlp = 0.07 |
| 129 | self.llp = 0.09 |
| 130 | self.PlaceObserverPoles([self.hlp, self.hlp, self.llp, self.llp]) |
| 131 | |
| 132 | self.U_max = numpy.matrix([[12.0], [12.0]]) |
| 133 | self.U_min = numpy.matrix([[-12.0], [-12.0]]) |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 134 | self.InitializeState() |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 135 | |
| 136 | def main(argv): |
| 137 | # Simulate the response of the system to a step input. |
| 138 | drivetrain = Drivetrain() |
| 139 | simulated_left = [] |
| 140 | simulated_right = [] |
| 141 | for _ in xrange(100): |
| 142 | drivetrain.Update(numpy.matrix([[12.0], [12.0]])) |
| 143 | simulated_left.append(drivetrain.X[0, 0]) |
| 144 | simulated_right.append(drivetrain.X[2, 0]) |
| 145 | |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 146 | #pylab.plot(range(100), simulated_left) |
| 147 | #pylab.plot(range(100), simulated_right) |
| 148 | #pylab.show() |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 149 | |
| 150 | # Simulate forwards motion. |
| 151 | drivetrain = Drivetrain() |
| 152 | close_loop_left = [] |
| 153 | close_loop_right = [] |
| 154 | R = numpy.matrix([[1.0], [0.0], [1.0], [0.0]]) |
| 155 | for _ in xrange(100): |
| 156 | U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), |
| 157 | drivetrain.U_min, drivetrain.U_max) |
| 158 | drivetrain.UpdateObserver(U) |
| 159 | drivetrain.Update(U) |
| 160 | close_loop_left.append(drivetrain.X[0, 0]) |
| 161 | close_loop_right.append(drivetrain.X[2, 0]) |
| 162 | |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 163 | #pylab.plot(range(100), close_loop_left) |
| 164 | #pylab.plot(range(100), close_loop_right) |
| 165 | #pylab.show() |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 166 | |
| 167 | # Try turning in place |
| 168 | drivetrain = Drivetrain() |
| 169 | close_loop_left = [] |
| 170 | close_loop_right = [] |
| 171 | R = numpy.matrix([[-1.0], [0.0], [1.0], [0.0]]) |
| 172 | for _ in xrange(100): |
| 173 | U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), |
| 174 | drivetrain.U_min, drivetrain.U_max) |
| 175 | drivetrain.UpdateObserver(U) |
| 176 | drivetrain.Update(U) |
| 177 | close_loop_left.append(drivetrain.X[0, 0]) |
| 178 | close_loop_right.append(drivetrain.X[2, 0]) |
| 179 | |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 180 | #pylab.plot(range(100), close_loop_left) |
| 181 | #pylab.plot(range(100), close_loop_right) |
| 182 | #pylab.show() |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 183 | |
| 184 | # Try turning just one side. |
| 185 | drivetrain = Drivetrain() |
| 186 | close_loop_left = [] |
| 187 | close_loop_right = [] |
| 188 | R = numpy.matrix([[0.0], [0.0], [1.0], [0.0]]) |
| 189 | for _ in xrange(100): |
| 190 | U = numpy.clip(drivetrain.K * (R - drivetrain.X_hat), |
| 191 | drivetrain.U_min, drivetrain.U_max) |
| 192 | drivetrain.UpdateObserver(U) |
| 193 | drivetrain.Update(U) |
| 194 | close_loop_left.append(drivetrain.X[0, 0]) |
| 195 | close_loop_right.append(drivetrain.X[2, 0]) |
| 196 | |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 197 | #pylab.plot(range(100), close_loop_left) |
| 198 | #pylab.plot(range(100), close_loop_right) |
| 199 | #pylab.show() |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 200 | |
| 201 | # Write the generated constants out to a file. |
| 202 | if len(argv) != 3: |
| 203 | print "Expected .h file name and .cc file name" |
| 204 | else: |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 205 | loop_writer = control_loop.ControlLoopWriter("Drivetrain", [drivetrain]) |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 206 | if argv[1][-3:] == '.cc': |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 207 | loop_writer.Write(argv[2], argv[1]) |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 208 | else: |
Austin Schuh | 4352ac6 | 2013-03-19 06:23:16 +0000 | [diff] [blame] | 209 | loop_writer.Write(argv[1], argv[2]) |
James Kuszmaul | f254c1a | 2013-03-10 16:31:26 -0700 | [diff] [blame] | 210 | |
| 211 | if __name__ == '__main__': |
| 212 | sys.exit(main(sys.argv)) |