Austin Schuh | 085eab9 | 2020-11-26 13:54:51 -0800 | [diff] [blame] | 1 | #!/usr/bin/python3 |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 2 | |
| 3 | from frc971.control_loops.python import control_loop |
| 4 | from frc971.control_loops.python import controls |
| 5 | import numpy |
Austin Schuh | 6c20f20 | 2017-02-18 22:31:44 -0800 | [diff] [blame] | 6 | import scipy |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 7 | import sys |
| 8 | from matplotlib import pylab |
| 9 | |
| 10 | import gflags |
| 11 | import glog |
| 12 | |
| 13 | FLAGS = gflags.FLAGS |
| 14 | |
| 15 | gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.') |
| 16 | |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 17 | |
| 18 | def PlotDiff(list1, list2, time): |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 19 | pylab.subplot(1, 1, 1) |
| 20 | pylab.plot(time, numpy.subtract(list1, list2), label='diff') |
| 21 | pylab.legend() |
| 22 | |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 23 | |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 24 | class VelocityShooter(control_loop.HybridControlLoop): |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 25 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 26 | def __init__(self, name='VelocityShooter'): |
| 27 | super(VelocityShooter, self).__init__(name) |
| 28 | # Number of motors |
| 29 | self.num_motors = 2.0 |
| 30 | # Stall Torque in N m |
| 31 | self.stall_torque = 0.71 * self.num_motors |
| 32 | # Stall Current in Amps |
| 33 | self.stall_current = 134.0 * self.num_motors |
| 34 | # Free Speed in RPM |
| 35 | self.free_speed_rpm = 18730.0 |
| 36 | # Free Speed in rotations/second. |
| 37 | self.free_speed = self.free_speed_rpm / 60.0 |
| 38 | # Free Current in Amps |
| 39 | self.free_current = 0.7 * self.num_motors |
| 40 | # Moment of inertia of the shooter wheel in kg m^2 |
| 41 | # 1400.6 grams/cm^2 |
| 42 | # 1.407 *1e-4 kg m^2 |
| 43 | self.J = 0.00120 |
| 44 | # Resistance of the motor, divided by 2 to account for the 2 motors |
| 45 | self.R = 12.0 / self.stall_current |
| 46 | # Motor velocity constant |
| 47 | self.Kv = ((self.free_speed * 2.0 * numpy.pi) / |
| 48 | (12.0 - self.R * self.free_current)) |
| 49 | # Torque constant |
| 50 | self.Kt = self.stall_torque / self.stall_current |
| 51 | # Gear ratio |
| 52 | self.G = 12.0 / 36.0 |
| 53 | # Control loop time step |
| 54 | self.dt = 0.00505 |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 55 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 56 | # State feedback matrices |
| 57 | # [angular velocity] |
| 58 | self.A_continuous = numpy.matrix( |
| 59 | [[-self.Kt / (self.Kv * self.J * self.G * self.G * self.R)]]) |
| 60 | self.B_continuous = numpy.matrix( |
| 61 | [[self.Kt / (self.J * self.G * self.R)]]) |
| 62 | self.C = numpy.matrix([[1]]) |
| 63 | self.D = numpy.matrix([[0]]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 64 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 65 | # The states are [unfiltered_velocity] |
| 66 | self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, |
| 67 | self.B_continuous, self.dt) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 68 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 69 | self.PlaceControllerPoles([.75]) |
Austin Schuh | cd3237a | 2017-02-18 14:19:26 -0800 | [diff] [blame] | 70 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 71 | glog.debug('K %s', repr(self.K)) |
| 72 | glog.debug('System poles are %s', |
| 73 | repr(numpy.linalg.eig(self.A_continuous)[0])) |
| 74 | glog.debug('Poles are %s', |
| 75 | repr(numpy.linalg.eig(self.A - self.B * self.K)[0])) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 76 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 77 | self.PlaceObserverPoles([0.3]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 78 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 79 | self.U_max = numpy.matrix([[12.0]]) |
| 80 | self.U_min = numpy.matrix([[-12.0]]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 81 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 82 | qff_vel = 8.0 |
| 83 | self.Qff = numpy.matrix([[1.0 / (qff_vel**2.0)]]) |
| 84 | |
| 85 | self.Kff = controls.TwoStateFeedForwards(self.B, self.Qff) |
| 86 | self.InitializeState() |
| 87 | |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 88 | |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 89 | class SecondOrderVelocityShooter(VelocityShooter): |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 90 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 91 | def __init__(self, name='SecondOrderVelocityShooter'): |
| 92 | super(SecondOrderVelocityShooter, self).__init__(name) |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 93 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 94 | self.A_continuous_unaugmented = self.A_continuous |
| 95 | self.B_continuous_unaugmented = self.B_continuous |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 96 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 97 | self.A_continuous = numpy.matrix(numpy.zeros((2, 2))) |
| 98 | self.A_continuous[0:1, 0:1] = self.A_continuous_unaugmented |
| 99 | self.A_continuous[1, 0] = 175.0 |
| 100 | self.A_continuous[1, 1] = -self.A_continuous[1, 0] |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 101 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 102 | self.B_continuous = numpy.matrix(numpy.zeros((2, 1))) |
| 103 | self.B_continuous[0:1, 0] = self.B_continuous_unaugmented |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 104 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 105 | self.C = numpy.matrix([[0, 1]]) |
| 106 | self.D = numpy.matrix([[0]]) |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 107 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 108 | # The states are [unfiltered_velocity, velocity] |
| 109 | self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, |
| 110 | self.B_continuous, self.dt) |
Austin Schuh | eb5c22e | 2017-04-09 18:30:28 -0700 | [diff] [blame] | 111 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 112 | self.PlaceControllerPoles([.70, 0.60]) |
Austin Schuh | eb5c22e | 2017-04-09 18:30:28 -0700 | [diff] [blame] | 113 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 114 | q_vel = 40.0 |
| 115 | q_filteredvel = 30.0 |
| 116 | self.Q = numpy.matrix([[(1.0 / (q_vel**2.0)), 0.0], |
| 117 | [0.0, (1.0 / (q_filteredvel**2.0))]]) |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 118 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 119 | self.R = numpy.matrix([[(1.0 / (3.0**2.0))]]) |
| 120 | self.K = controls.dlqr(self.A, self.B, self.Q, self.R) |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 121 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 122 | glog.debug('K %s', repr(self.K)) |
| 123 | glog.debug('System poles are %s', |
| 124 | repr(numpy.linalg.eig(self.A_continuous)[0])) |
| 125 | glog.debug('Poles are %s', |
| 126 | repr(numpy.linalg.eig(self.A - self.B * self.K)[0])) |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 127 | |
Austin Schuh | 085eab9 | 2020-11-26 13:54:51 -0800 | [diff] [blame] | 128 | self.PlaceObserverPoles([0.299, 0.301]) |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 129 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 130 | self.U_max = numpy.matrix([[12.0]]) |
| 131 | self.U_min = numpy.matrix([[-12.0]]) |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 132 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 133 | qff_vel = 8.0 |
| 134 | self.Qff = numpy.matrix([[1.0 / (qff_vel**2.0), 0.0], |
| 135 | [0.0, 1.0 / (qff_vel**2.0)]]) |
| 136 | |
| 137 | self.Kff = controls.TwoStateFeedForwards(self.B, self.Qff) |
| 138 | self.InitializeState() |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 139 | |
| 140 | |
| 141 | class Shooter(SecondOrderVelocityShooter): |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 142 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 143 | def __init__(self, name='Shooter'): |
| 144 | super(Shooter, self).__init__(name) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 145 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 146 | self.A_continuous_unaugmented = self.A_continuous |
| 147 | self.B_continuous_unaugmented = self.B_continuous |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 148 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 149 | self.A_continuous = numpy.matrix(numpy.zeros((3, 3))) |
| 150 | self.A_continuous[1:3, 1:3] = self.A_continuous_unaugmented |
| 151 | self.A_continuous[0, 2] = 1 |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 152 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 153 | self.B_continuous = numpy.matrix(numpy.zeros((3, 1))) |
| 154 | self.B_continuous[1:3, 0] = self.B_continuous_unaugmented |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 155 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 156 | # State feedback matrices |
| 157 | # [position, unfiltered_velocity, angular velocity] |
| 158 | self.C = numpy.matrix([[1, 0, 0]]) |
| 159 | self.D = numpy.matrix([[0]]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 160 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 161 | self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, |
| 162 | self.B_continuous, self.dt) |
| 163 | glog.debug(repr(self.A_continuous)) |
| 164 | glog.debug(repr(self.B_continuous)) |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 165 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 166 | observeability = controls.ctrb(self.A.T, self.C.T) |
| 167 | glog.debug('Rank of augmented observability matrix. %d', |
| 168 | numpy.linalg.matrix_rank(observeability)) |
Austin Schuh | c66b6fc | 2017-03-25 19:56:59 -0700 | [diff] [blame] | 169 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 170 | self.PlaceObserverPoles([0.9, 0.8, 0.7]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 171 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 172 | self.K_unaugmented = self.K |
| 173 | self.K = numpy.matrix(numpy.zeros((1, 3))) |
| 174 | self.K[0, 1:3] = self.K_unaugmented |
| 175 | self.Kff_unaugmented = self.Kff |
| 176 | self.Kff = numpy.matrix(numpy.zeros((1, 3))) |
| 177 | self.Kff[0, 1:3] = self.Kff_unaugmented |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 178 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 179 | self.InitializeState() |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 180 | |
| 181 | |
| 182 | class IntegralShooter(Shooter): |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 183 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 184 | def __init__(self, name='IntegralShooter'): |
| 185 | super(IntegralShooter, self).__init__(name=name) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 186 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 187 | self.A_continuous_unaugmented = self.A_continuous |
| 188 | self.B_continuous_unaugmented = self.B_continuous |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 189 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 190 | self.A_continuous = numpy.matrix(numpy.zeros((4, 4))) |
| 191 | self.A_continuous[0:3, 0:3] = self.A_continuous_unaugmented |
| 192 | self.A_continuous[0:3, 3] = self.B_continuous_unaugmented |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 193 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 194 | self.B_continuous = numpy.matrix(numpy.zeros((4, 1))) |
| 195 | self.B_continuous[0:3, 0] = self.B_continuous_unaugmented |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 196 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 197 | self.C_unaugmented = self.C |
| 198 | self.C = numpy.matrix(numpy.zeros((1, 4))) |
| 199 | self.C[0:1, 0:3] = self.C_unaugmented |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 200 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 201 | # The states are [position, unfiltered_velocity, velocity, torque_error] |
| 202 | self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, |
| 203 | self.B_continuous, self.dt) |
Austin Schuh | 6c20f20 | 2017-02-18 22:31:44 -0800 | [diff] [blame] | 204 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 205 | glog.debug('A: \n%s', repr(self.A_continuous)) |
| 206 | glog.debug('eig(A): \n%s', repr(scipy.linalg.eig(self.A_continuous))) |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 207 | glog.debug('schur(A): \n%s', |
| 208 | repr(scipy.linalg.schur(self.A_continuous))) |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 209 | glog.debug('A_dt(A): \n%s', repr(self.A)) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 210 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 211 | q_pos = 0.01 |
| 212 | q_vel = 5.0 |
| 213 | q_velfilt = 1.5 |
| 214 | q_voltage = 2.0 |
| 215 | self.Q_continuous = numpy.matrix([[(q_pos**2.0), 0.0, 0.0, 0.0], |
| 216 | [0.0, (q_vel**2.0), 0.0, 0.0], |
| 217 | [0.0, 0.0, (q_velfilt**2.0), 0.0], |
| 218 | [0.0, 0.0, 0.0, (q_voltage**2.0)]]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 219 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 220 | r_pos = 0.0003 |
| 221 | self.R_continuous = numpy.matrix([[(r_pos**2.0)]]) |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 222 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 223 | _, _, self.Q, self.R = controls.kalmd(A_continuous=self.A_continuous, |
| 224 | B_continuous=self.B_continuous, |
| 225 | Q_continuous=self.Q_continuous, |
| 226 | R_continuous=self.R_continuous, |
| 227 | dt=self.dt) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 228 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 229 | self.KalmanGain, self.P_steady_state = controls.kalman(A=self.A, |
| 230 | B=self.B, |
| 231 | C=self.C, |
| 232 | Q=self.Q, |
| 233 | R=self.R) |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 234 | self.L = self.A * self.KalmanGain |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 235 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 236 | self.K_unaugmented = self.K |
| 237 | self.K = numpy.matrix(numpy.zeros((1, 4))) |
| 238 | self.K[0, 0:3] = self.K_unaugmented |
| 239 | self.K[0, 3] = 1 |
| 240 | self.Kff_unaugmented = self.Kff |
| 241 | self.Kff = numpy.matrix(numpy.zeros((1, 4))) |
| 242 | self.Kff[0, 0:3] = self.Kff_unaugmented |
| 243 | |
| 244 | self.InitializeState() |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 245 | |
| 246 | |
| 247 | class ScenarioPlotter(object): |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 248 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 249 | def __init__(self): |
| 250 | # Various lists for graphing things. |
| 251 | self.t = [] |
| 252 | self.x = [] |
| 253 | self.v = [] |
| 254 | self.a = [] |
| 255 | self.x_hat = [] |
| 256 | self.u = [] |
| 257 | self.offset = [] |
| 258 | self.diff = [] |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 259 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 260 | def run_test(self, |
| 261 | shooter, |
| 262 | goal, |
| 263 | iterations=200, |
| 264 | controller_shooter=None, |
| 265 | observer_shooter=None, |
| 266 | hybrid_obs=False): |
| 267 | """Runs the shooter plant with an initial condition and goal. |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 268 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 269 | Args: |
| 270 | shooter: Shooter object to use. |
| 271 | goal: goal state. |
| 272 | iterations: Number of timesteps to run the model for. |
| 273 | controller_shooter: Shooter object to get K from, or None if we should |
| 274 | use shooter. |
| 275 | observer_shooter: Shooter object to use for the observer, or None if we |
| 276 | should use the actual state. |
| 277 | """ |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 278 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 279 | if controller_shooter is None: |
| 280 | controller_shooter = shooter |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 281 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 282 | vbat = 12.0 |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 283 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 284 | if self.t: |
| 285 | initial_t = self.t[-1] + shooter.dt |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 286 | else: |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 287 | initial_t = 0 |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 288 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 289 | last_U = numpy.matrix([[0.0]]) |
Austin Schuh | 5ea4847 | 2021-02-02 20:46:41 -0800 | [diff] [blame] | 290 | for i in range(iterations): |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 291 | X_hat = shooter.X |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 292 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 293 | if observer_shooter is not None: |
| 294 | X_hat = observer_shooter.X_hat |
| 295 | self.x_hat.append(observer_shooter.X_hat[2, 0]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 296 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 297 | ff_U = controller_shooter.Kff * (goal - observer_shooter.A * goal) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 298 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 299 | U = controller_shooter.K * (goal - X_hat) + ff_U |
| 300 | U[0, 0] = numpy.clip(U[0, 0], -vbat, vbat) |
| 301 | self.x.append(shooter.X[0, 0]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 302 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 303 | self.diff.append(shooter.X[2, 0] - observer_shooter.X_hat[2, 0]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 304 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 305 | if self.v: |
| 306 | last_v = self.v[-1] |
| 307 | else: |
| 308 | last_v = 0 |
| 309 | |
| 310 | self.v.append(shooter.X[2, 0]) |
| 311 | self.a.append((self.v[-1] - last_v) / shooter.dt) |
| 312 | |
| 313 | if observer_shooter is not None: |
| 314 | if i != 0: |
| 315 | observer_shooter.Y = shooter.Y |
| 316 | observer_shooter.CorrectObserver(U) |
| 317 | self.offset.append(observer_shooter.X_hat[3, 0]) |
| 318 | |
| 319 | applied_U = last_U.copy() |
| 320 | if i > 60: |
| 321 | applied_U += 2 |
| 322 | shooter.Update(applied_U) |
| 323 | |
| 324 | if observer_shooter is not None: |
| 325 | if hybrid_obs: |
| 326 | observer_shooter.PredictHybridObserver(last_U, shooter.dt) |
| 327 | else: |
| 328 | observer_shooter.PredictObserver(last_U) |
| 329 | last_U = U.copy() |
| 330 | |
| 331 | self.t.append(initial_t + i * shooter.dt) |
| 332 | self.u.append(U[0, 0]) |
| 333 | |
| 334 | def Plot(self): |
| 335 | pylab.figure() |
| 336 | pylab.subplot(3, 1, 1) |
| 337 | pylab.plot(self.t, self.v, label='x') |
| 338 | pylab.plot(self.t, self.x_hat, label='x_hat') |
| 339 | pylab.legend() |
| 340 | |
| 341 | pylab.subplot(3, 1, 2) |
| 342 | pylab.plot(self.t, self.u, label='u') |
| 343 | pylab.plot(self.t, self.offset, label='voltage_offset') |
| 344 | pylab.legend() |
| 345 | |
| 346 | pylab.subplot(3, 1, 3) |
| 347 | pylab.plot(self.t, self.a, label='a') |
| 348 | pylab.legend() |
| 349 | |
| 350 | pylab.draw() |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 351 | |
| 352 | |
| 353 | def main(argv): |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 354 | scenario_plotter = ScenarioPlotter() |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 355 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 356 | if FLAGS.plot: |
| 357 | iterations = 200 |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 358 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 359 | initial_X = numpy.matrix([[0.0], [0.0], [0.0]]) |
| 360 | R = numpy.matrix([[0.0], [100.0], [100.0], [0.0]]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 361 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 362 | scenario_plotter_int = ScenarioPlotter() |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 363 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 364 | shooter = Shooter() |
| 365 | shooter_controller = IntegralShooter() |
| 366 | observer_shooter_hybrid = IntegralShooter() |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 367 | |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 368 | scenario_plotter_int.run_test(shooter, |
| 369 | goal=R, |
| 370 | controller_shooter=shooter_controller, |
| 371 | observer_shooter=observer_shooter_hybrid, |
| 372 | iterations=iterations, |
| 373 | hybrid_obs=True) |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 374 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 375 | scenario_plotter_int.Plot() |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 376 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 377 | pylab.show() |
Austin Schuh | 3ad5ed8 | 2017-02-25 21:36:19 -0800 | [diff] [blame] | 378 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 379 | if len(argv) != 5: |
| 380 | glog.fatal('Expected .h file name and .cc file name') |
| 381 | else: |
| 382 | namespaces = ['y2017', 'control_loops', 'superstructure', 'shooter'] |
| 383 | shooter = Shooter('Shooter') |
Ravago Jones | 5127ccc | 2022-07-31 16:32:45 -0700 | [diff] [blame] | 384 | loop_writer = control_loop.ControlLoopWriter('Shooter', [shooter], |
| 385 | namespaces=namespaces) |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 386 | loop_writer.AddConstant( |
| 387 | control_loop.Constant('kFreeSpeed', '%f', shooter.free_speed)) |
| 388 | loop_writer.AddConstant( |
| 389 | control_loop.Constant('kOutputRatio', '%f', shooter.G)) |
| 390 | loop_writer.Write(argv[1], argv[2]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 391 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 392 | integral_shooter = IntegralShooter('IntegralShooter') |
| 393 | integral_loop_writer = control_loop.ControlLoopWriter( |
| 394 | 'IntegralShooter', [integral_shooter], |
| 395 | namespaces=namespaces, |
| 396 | plant_type='StateFeedbackHybridPlant', |
| 397 | observer_type='HybridKalman') |
| 398 | integral_loop_writer.Write(argv[3], argv[4]) |
Austin Schuh | 48d60c1 | 2017-02-04 21:58:58 -0800 | [diff] [blame] | 399 | |
| 400 | |
| 401 | if __name__ == '__main__': |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 402 | argv = FLAGS(sys.argv) |
| 403 | glog.init() |
| 404 | sys.exit(main(argv)) |