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