Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 3 | import control_loop |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 4 | import controls |
Austin Schuh | fe6d37e | 2014-03-24 09:45:01 -0700 | [diff] [blame] | 5 | import polytope |
| 6 | import polydrivetrain |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 7 | import numpy |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 8 | import sys |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 9 | import matplotlib |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 10 | from matplotlib import pylab |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 11 | |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 12 | class Claw(control_loop.ControlLoop): |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 13 | def __init__(self, name="Claw", mass=None): |
Austin Schuh | 3bb9a44 | 2014-02-02 16:01:45 -0800 | [diff] [blame] | 14 | super(Claw, self).__init__(name) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 15 | # Stall Torque in N m |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 16 | self.stall_torque = 0.476 |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 17 | # Stall Current in Amps |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 18 | self.stall_current = 80.730 |
James Kuszmaul | 9279740 | 2014-02-17 14:08:49 -0800 | [diff] [blame] | 19 | # Free Speed in RPM |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 20 | self.free_speed = 13906.0 |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 21 | # Free Current in Amps |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 22 | self.free_current = 5.820 |
| 23 | # Mass of the claw |
| 24 | if mass is None: |
| 25 | self.mass = 5.0 |
| 26 | else: |
| 27 | self.mass = mass |
James Kuszmaul | 3ac190a | 2014-03-26 20:11:04 -0700 | [diff] [blame] | 28 | |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 29 | # Resistance of the motor |
James Kuszmaul | 9279740 | 2014-02-17 14:08:49 -0800 | [diff] [blame] | 30 | self.R = 12.0 / self.stall_current |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 31 | # Motor velocity constant |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 32 | self.Kv = ((self.free_speed / 60.0 * 2.0 * numpy.pi) / |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 33 | (12.0 - self.R * self.free_current)) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 34 | # Torque constant |
| 35 | self.Kt = self.stall_torque / self.stall_current |
| 36 | # Gear ratio |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 37 | self.G = (56.0 / 12.0) * (54.0 / 14.0) * (64.0 / 14.0) * (72.0 / 18.0) |
| 38 | # Claw length |
| 39 | self.r = 18 * 0.0254 |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 40 | |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 41 | self.J = self.r * self.mass |
| 42 | |
| 43 | # Control loop time step |
| 44 | self.dt = 0.005 |
| 45 | |
| 46 | # State is [position, velocity] |
| 47 | # Input is [Voltage] |
| 48 | |
| 49 | C1 = self.G * self.G * self.Kt / (self.R * self.J * self.Kv) |
| 50 | C2 = self.Kt * self.G / (self.J * self.R) |
James Kuszmaul | e2afbe4 | 2014-02-17 22:29:59 -0800 | [diff] [blame] | 51 | |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 52 | self.A_continuous = numpy.matrix( |
James Kuszmaul | e2afbe4 | 2014-02-17 22:29:59 -0800 | [diff] [blame] | 53 | [[0, 1], |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 54 | [0, -C1]]) |
James Kuszmaul | e2afbe4 | 2014-02-17 22:29:59 -0800 | [diff] [blame] | 55 | |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 56 | # Start with the unmodified input |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 57 | self.B_continuous = numpy.matrix( |
James Kuszmaul | e2afbe4 | 2014-02-17 22:29:59 -0800 | [diff] [blame] | 58 | [[0], |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 59 | [C2]]) |
James Kuszmaul | e2afbe4 | 2014-02-17 22:29:59 -0800 | [diff] [blame] | 60 | |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 61 | self.C = numpy.matrix([[1, 0]]) |
| 62 | self.D = numpy.matrix([[0]]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 63 | |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 64 | self.A, self.B = self.ContinuousToDiscrete( |
| 65 | self.A_continuous, self.B_continuous, self.dt) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 66 | |
Austin Schuh | 0d9467a | 2014-02-15 22:36:45 -0800 | [diff] [blame] | 67 | controlability = controls.ctrb(self.A, self.B); |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 68 | |
Austin Schuh | bd94702 | 2015-03-01 00:10:01 -0800 | [diff] [blame^] | 69 | print "Free speed is", self.free_speed * numpy.pi * 2.0 / 60.0 / self.G |
| 70 | |
| 71 | q_pos = 0.15 |
| 72 | q_vel = 2.5 |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 73 | self.Q = numpy.matrix([[(1.0 / (q_pos ** 2.0)), 0.0], |
| 74 | [0.0, (1.0 / (q_vel ** 2.0))]]) |
| 75 | |
| 76 | self.R = numpy.matrix([[(1.0 / (12.0 ** 2.0))]]) |
| 77 | self.K = controls.dlqr(self.A, self.B, self.Q, self.R) |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 78 | |
Austin Schuh | 8a436e8 | 2015-02-16 23:31:28 -0800 | [diff] [blame] | 79 | print 'K', self.K |
| 80 | print 'Poles are', numpy.linalg.eig(self.A - self.B * self.K)[0] |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 81 | |
Austin Schuh | 8a436e8 | 2015-02-16 23:31:28 -0800 | [diff] [blame] | 82 | self.rpl = 0.30 |
| 83 | self.ipl = 0.10 |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 84 | self.PlaceObserverPoles([self.rpl + 1j * self.ipl, |
| 85 | self.rpl - 1j * self.ipl]) |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 86 | |
Austin Schuh | bd94702 | 2015-03-01 00:10:01 -0800 | [diff] [blame^] | 87 | print 'L is', self.L |
| 88 | |
| 89 | q_pos = 0.018 |
| 90 | q_vel = 0.05 |
| 91 | self.Q = numpy.matrix([[(q_pos ** 2.0), 0.0], |
| 92 | [0.0, (q_vel ** 2.0)]]) |
| 93 | |
| 94 | r_volts = 0.1 |
| 95 | self.R = numpy.matrix([[(r_volts ** 2.0)]]) |
| 96 | |
| 97 | self.KalmanGain, self.Q_steady = controls.kalman( |
| 98 | A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R) |
| 99 | |
| 100 | print 'Kal', self.KalmanGain |
| 101 | self.L = self.A * self.KalmanGain |
| 102 | print 'KalL is', self.L |
| 103 | |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 104 | # The box formed by U_min and U_max must encompass all possible values, |
| 105 | # or else Austin's code gets angry. |
| 106 | self.U_max = numpy.matrix([[12.0]]) |
| 107 | self.U_min = numpy.matrix([[-12.0]]) |
Austin Schuh | c1f6889 | 2013-03-16 17:06:27 -0700 | [diff] [blame] | 108 | |
| 109 | self.InitializeState() |
| 110 | |
Austin Schuh | fe6d37e | 2014-03-24 09:45:01 -0700 | [diff] [blame] | 111 | |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 112 | def run_test(claw, initial_X, goal, max_separation_error=0.01, |
| 113 | show_graph=True, iterations=200, controller_claw=None, |
| 114 | observer_claw=None): |
| 115 | """Runs the claw plant with an initial condition and goal. |
Brian Silverman | 6dd2c53 | 2014-03-29 23:34:39 -0700 | [diff] [blame] | 116 | |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 117 | The tests themselves are not terribly sophisticated; I just test for |
James Kuszmaul | f63b0ae | 2014-03-25 16:52:11 -0700 | [diff] [blame] | 118 | whether the goal has been reached and whether the separation goes |
Brian Silverman | 6dd2c53 | 2014-03-29 23:34:39 -0700 | [diff] [blame] | 119 | outside of the initial and goal values by more than max_separation_error. |
James Kuszmaul | f63b0ae | 2014-03-25 16:52:11 -0700 | [diff] [blame] | 120 | Prints out something for a failure of either condition and returns |
| 121 | False if tests fail. |
| 122 | Args: |
| 123 | claw: claw object to use. |
| 124 | initial_X: starting state. |
| 125 | goal: goal state. |
| 126 | show_graph: Whether or not to display a graph showing the changing |
| 127 | states and voltages. |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 128 | iterations: Number of timesteps to run the model for. |
| 129 | controller_claw: claw object to get K from, or None if we should |
| 130 | use claw. |
| 131 | observer_claw: claw object to use for the observer, or None if we should |
| 132 | use the actual state. |
| 133 | """ |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 134 | |
James Kuszmaul | f63b0ae | 2014-03-25 16:52:11 -0700 | [diff] [blame] | 135 | claw.X = initial_X |
| 136 | |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 137 | if controller_claw is None: |
| 138 | controller_claw = claw |
| 139 | |
| 140 | if observer_claw is not None: |
| 141 | observer_claw.X_hat = initial_X + 0.01 |
| 142 | observer_claw.X_hat = initial_X |
| 143 | |
James Kuszmaul | f63b0ae | 2014-03-25 16:52:11 -0700 | [diff] [blame] | 144 | # Various lists for graphing things. |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 145 | t = [] |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 146 | x = [] |
| 147 | v = [] |
| 148 | x_hat = [] |
| 149 | u = [] |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 150 | |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 151 | sep_plot_gain = 100.0 |
James Kuszmaul | f63b0ae | 2014-03-25 16:52:11 -0700 | [diff] [blame] | 152 | |
| 153 | for i in xrange(iterations): |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 154 | X_hat = claw.X |
| 155 | if observer_claw is not None: |
| 156 | X_hat = observer_claw.X_hat |
| 157 | x_hat.append(observer_claw.X_hat[0, 0]) |
| 158 | U = controller_claw.K * (goal - X_hat) |
| 159 | U[0, 0] = numpy.clip(U[0, 0], -12, 12) |
| 160 | x.append(claw.X[0, 0]) |
| 161 | v.append(claw.X[1, 0]) |
| 162 | if observer_claw is not None: |
| 163 | observer_claw.PredictObserver(U) |
James Kuszmaul | f63b0ae | 2014-03-25 16:52:11 -0700 | [diff] [blame] | 164 | claw.Update(U) |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 165 | if observer_claw is not None: |
| 166 | observer_claw.Y = claw.Y |
| 167 | observer_claw.CorrectObserver(U) |
Brian Silverman | b087c0a | 2014-03-30 12:59:52 -0700 | [diff] [blame] | 168 | |
James Kuszmaul | f63b0ae | 2014-03-25 16:52:11 -0700 | [diff] [blame] | 169 | t.append(i * claw.dt) |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 170 | u.append(U[0, 0]) |
James Kuszmaul | f63b0ae | 2014-03-25 16:52:11 -0700 | [diff] [blame] | 171 | |
| 172 | if show_graph: |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 173 | pylab.subplot(2, 1, 1) |
| 174 | pylab.plot(t, x, label='x') |
| 175 | if observer_claw is not None: |
| 176 | pylab.plot(t, x_hat, label='x_hat') |
| 177 | pylab.legend() |
| 178 | |
| 179 | pylab.subplot(2, 1, 2) |
| 180 | pylab.plot(t, u, label='u') |
James Kuszmaul | f63b0ae | 2014-03-25 16:52:11 -0700 | [diff] [blame] | 181 | pylab.legend() |
| 182 | pylab.show() |
| 183 | |
James Kuszmaul | f63b0ae | 2014-03-25 16:52:11 -0700 | [diff] [blame] | 184 | |
| 185 | def main(argv): |
Austin Schuh | bd94702 | 2015-03-01 00:10:01 -0800 | [diff] [blame^] | 186 | loaded_mass = 0 |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 187 | #loaded_mass = 0 |
Austin Schuh | bd94702 | 2015-03-01 00:10:01 -0800 | [diff] [blame^] | 188 | claw = Claw(mass=4 + loaded_mass) |
| 189 | claw_controller = Claw(mass=5 + 0) |
| 190 | observer_claw = Claw(mass=5 + 0) |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 191 | #observer_claw = None |
James Kuszmaul | f63b0ae | 2014-03-25 16:52:11 -0700 | [diff] [blame] | 192 | |
| 193 | # Test moving the claw with constant separation. |
Austin Schuh | 1a38796 | 2015-01-31 16:36:20 -0800 | [diff] [blame] | 194 | initial_X = numpy.matrix([[0.0], [0.0]]) |
| 195 | R = numpy.matrix([[1.0], [0.0]]) |
| 196 | run_test(claw, initial_X, R, controller_claw=claw_controller, |
| 197 | observer_claw=observer_claw) |
Brian Silverman | f05948b | 2014-03-30 00:24:36 -0700 | [diff] [blame] | 198 | |
Austin Schuh | 3c54231 | 2013-02-24 01:53:50 -0800 | [diff] [blame] | 199 | # Write the generated constants out to a file. |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 200 | if len(argv) != 3: |
| 201 | print "Expected .h file name and .cc file name for the claw." |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 202 | else: |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 203 | claw = Claw("Claw") |
| 204 | loop_writer = control_loop.ControlLoopWriter("Claw", [claw]) |
Austin Schuh | 683a0d0 | 2013-03-02 01:51:31 -0800 | [diff] [blame] | 205 | if argv[1][-3:] == '.cc': |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 206 | loop_writer.Write(argv[2], argv[1]) |
Austin Schuh | 683a0d0 | 2013-03-02 01:51:31 -0800 | [diff] [blame] | 207 | else: |
Austin Schuh | cda86af | 2014-02-16 16:16:39 -0800 | [diff] [blame] | 208 | loop_writer.Write(argv[1], argv[2]) |
Austin Schuh | c8ca244 | 2013-02-23 12:29:33 -0800 | [diff] [blame] | 209 | |
| 210 | if __name__ == '__main__': |
| 211 | sys.exit(main(sys.argv)) |