Austin Schuh | ac61c88 | 2022-02-23 17:22:22 -0800 | [diff] [blame] | 1 | from frc971.control_loops.python import angular_system |
Austin Schuh | 8216245 | 2022-02-07 22:01:45 -0800 | [diff] [blame] | 2 | from frc971.control_loops.python import control_loop |
| 3 | from frc971.control_loops.python import controls |
Austin Schuh | ac61c88 | 2022-02-23 17:22:22 -0800 | [diff] [blame] | 4 | from aos.util.trapezoid_profile import TrapezoidProfile |
Austin Schuh | 8216245 | 2022-02-07 22:01:45 -0800 | [diff] [blame] | 5 | import numpy |
| 6 | from matplotlib import pylab |
| 7 | |
| 8 | import gflags |
| 9 | import glog |
| 10 | |
Austin Schuh | ac61c88 | 2022-02-23 17:22:22 -0800 | [diff] [blame] | 11 | CatapultParams = angular_system.AngularSystemParams |
Austin Schuh | 8216245 | 2022-02-07 22:01:45 -0800 | [diff] [blame] | 12 | |
| 13 | |
Austin Schuh | ac61c88 | 2022-02-23 17:22:22 -0800 | [diff] [blame] | 14 | # TODO(austin): This is mostly the same as angular_system. Can we either wrap an angular_system or assign it? |
| 15 | class Catapult(angular_system.AngularSystem): |
Austin Schuh | 8216245 | 2022-02-07 22:01:45 -0800 | [diff] [blame] | 16 | def __init__(self, params, name="Catapult"): |
Austin Schuh | ac61c88 | 2022-02-23 17:22:22 -0800 | [diff] [blame] | 17 | super(Catapult, self).__init__(params, name) |
Austin Schuh | b39f452 | 2022-03-27 13:29:42 -0700 | [diff] [blame^] | 18 | # Signal that we have a 2 cycle output delay to compensate for in |
Austin Schuh | ac61c88 | 2022-02-23 17:22:22 -0800 | [diff] [blame] | 19 | # our observer. |
Austin Schuh | b39f452 | 2022-03-27 13:29:42 -0700 | [diff] [blame^] | 20 | self.delayed_u = 2 |
Austin Schuh | 8216245 | 2022-02-07 22:01:45 -0800 | [diff] [blame] | 21 | |
| 22 | self.InitializeState() |
| 23 | |
| 24 | |
Austin Schuh | ac61c88 | 2022-02-23 17:22:22 -0800 | [diff] [blame] | 25 | class IntegralCatapult(angular_system.IntegralAngularSystem): |
Austin Schuh | 8216245 | 2022-02-07 22:01:45 -0800 | [diff] [blame] | 26 | def __init__(self, params, name="IntegralCatapult"): |
| 27 | super(IntegralCatapult, self).__init__(params, name=name) |
Austin Schuh | b39f452 | 2022-03-27 13:29:42 -0700 | [diff] [blame^] | 28 | # Signal that we have a 2 cycle output delay to compensate for in |
Austin Schuh | ac61c88 | 2022-02-23 17:22:22 -0800 | [diff] [blame] | 29 | # our observer. |
Austin Schuh | b39f452 | 2022-03-27 13:29:42 -0700 | [diff] [blame^] | 30 | self.delayed_u = 2 |
Austin Schuh | 8216245 | 2022-02-07 22:01:45 -0800 | [diff] [blame] | 31 | |
| 32 | self.InitializeState() |
| 33 | |
| 34 | |
| 35 | def MaxSpeed(params, U, final_position): |
| 36 | """Runs the catapult plant with an initial condition and goal. |
| 37 | |
| 38 | Args: |
| 39 | catapult: Catapult object to use. |
| 40 | goal: goal state. |
| 41 | iterations: Number of timesteps to run the model for. |
| 42 | controller_catapult: Catapult object to get K from, or None if we should |
| 43 | use catapult. |
| 44 | observer_catapult: Catapult object to use for the observer, or None if we |
| 45 | should use the actual state. |
| 46 | """ |
| 47 | |
| 48 | # Various lists for graphing things. |
| 49 | catapult = Catapult(params, params.name) |
| 50 | controller_catapult = IntegralCatapult(params, params.name) |
| 51 | observer_catapult = IntegralCatapult(params, params.name) |
| 52 | vbat = 12.0 |
| 53 | |
| 54 | while True: |
| 55 | X_hat = catapult.X |
| 56 | if catapult.X[0, 0] > final_position: |
Austin Schuh | ac61c88 | 2022-02-23 17:22:22 -0800 | [diff] [blame] | 57 | return catapult.X[1, 0] * params.radius |
Austin Schuh | 8216245 | 2022-02-07 22:01:45 -0800 | [diff] [blame] | 58 | |
| 59 | if observer_catapult is not None: |
| 60 | X_hat = observer_catapult.X_hat |
| 61 | |
| 62 | U[0, 0] = numpy.clip(U[0, 0], -vbat, vbat) |
| 63 | |
| 64 | if observer_catapult is not None: |
| 65 | observer_catapult.Y = catapult.Y |
Austin Schuh | ac61c88 | 2022-02-23 17:22:22 -0800 | [diff] [blame] | 66 | observer_catapult.CorrectObserver(U) |
Austin Schuh | 8216245 | 2022-02-07 22:01:45 -0800 | [diff] [blame] | 67 | |
| 68 | applied_U = U.copy() |
| 69 | catapult.Update(applied_U) |
| 70 | |
| 71 | if observer_catapult is not None: |
Austin Schuh | ac61c88 | 2022-02-23 17:22:22 -0800 | [diff] [blame] | 72 | observer_catapult.PredictObserver(U) |
Austin Schuh | 8216245 | 2022-02-07 22:01:45 -0800 | [diff] [blame] | 73 | |
| 74 | |
| 75 | def PlotShot(params, U, final_position): |
| 76 | """Runs the catapult plant with an initial condition and goal. |
| 77 | |
| 78 | Args: |
| 79 | catapult: Catapult object to use. |
| 80 | goal: goal state. |
| 81 | iterations: Number of timesteps to run the model for. |
| 82 | controller_catapult: Catapult object to get K from, or None if we should |
| 83 | use catapult. |
| 84 | observer_catapult: Catapult object to use for the observer, or None if we |
| 85 | should use the actual state. |
| 86 | """ |
| 87 | |
| 88 | # Various lists for graphing things. |
| 89 | t = [] |
| 90 | x = [] |
| 91 | x_hat = [] |
| 92 | v = [] |
| 93 | w_hat = [] |
| 94 | v_hat = [] |
| 95 | a = [] |
| 96 | u = [] |
| 97 | offset = [] |
| 98 | |
| 99 | catapult = Catapult(params, params.name) |
| 100 | controller_catapult = IntegralCatapult(params, params.name) |
| 101 | observer_catapult = IntegralCatapult(params, params.name) |
| 102 | vbat = 12.0 |
| 103 | |
| 104 | if t: |
| 105 | initial_t = t[-1] + catapult.dt |
| 106 | else: |
| 107 | initial_t = 0 |
| 108 | |
| 109 | for i in range(10000): |
| 110 | X_hat = catapult.X |
| 111 | if catapult.X[0, 0] > final_position: |
| 112 | break |
| 113 | |
| 114 | if observer_catapult is not None: |
| 115 | X_hat = observer_catapult.X_hat |
| 116 | x_hat.append(observer_catapult.X_hat[0, 0]) |
| 117 | w_hat.append(observer_catapult.X_hat[1, 0]) |
Austin Schuh | ac61c88 | 2022-02-23 17:22:22 -0800 | [diff] [blame] | 118 | v_hat.append(observer_catapult.X_hat[1, 0] * params.radius) |
Austin Schuh | 8216245 | 2022-02-07 22:01:45 -0800 | [diff] [blame] | 119 | |
| 120 | U[0, 0] = numpy.clip(U[0, 0], -vbat, vbat) |
| 121 | x.append(catapult.X[0, 0]) |
| 122 | |
| 123 | if v: |
| 124 | last_v = v[-1] |
| 125 | else: |
| 126 | last_v = 0 |
| 127 | |
| 128 | v.append(catapult.X[1, 0]) |
| 129 | a.append((v[-1] - last_v) / catapult.dt) |
| 130 | |
| 131 | if observer_catapult is not None: |
| 132 | observer_catapult.Y = catapult.Y |
Austin Schuh | ac61c88 | 2022-02-23 17:22:22 -0800 | [diff] [blame] | 133 | observer_catapult.CorrectObserver(U) |
Austin Schuh | 8216245 | 2022-02-07 22:01:45 -0800 | [diff] [blame] | 134 | offset.append(observer_catapult.X_hat[2, 0]) |
| 135 | |
| 136 | catapult.Update(U) |
| 137 | |
| 138 | if observer_catapult is not None: |
Austin Schuh | ac61c88 | 2022-02-23 17:22:22 -0800 | [diff] [blame] | 139 | observer_catapult.PredictObserver(U) |
Austin Schuh | 8216245 | 2022-02-07 22:01:45 -0800 | [diff] [blame] | 140 | |
| 141 | t.append(initial_t + i * catapult.dt) |
| 142 | u.append(U[0, 0]) |
| 143 | |
| 144 | pylab.subplot(3, 1, 1) |
| 145 | pylab.plot(t, v, label='v') |
| 146 | pylab.plot(t, x_hat, label='x_hat') |
| 147 | pylab.plot(t, v, label='v') |
| 148 | pylab.plot(t, v_hat, label='v_hat') |
| 149 | pylab.plot(t, w_hat, label='w_hat') |
| 150 | pylab.legend() |
| 151 | |
| 152 | pylab.subplot(3, 1, 2) |
| 153 | pylab.plot(t, u, label='u') |
| 154 | pylab.plot(t, offset, label='voltage_offset') |
| 155 | pylab.legend() |
| 156 | |
| 157 | pylab.subplot(3, 1, 3) |
| 158 | pylab.plot(t, a, label='a') |
| 159 | pylab.legend() |
| 160 | |
| 161 | pylab.show() |
Austin Schuh | 2e28d87 | 2022-02-19 18:25:57 -0800 | [diff] [blame] | 162 | |
Austin Schuh | ac61c88 | 2022-02-23 17:22:22 -0800 | [diff] [blame] | 163 | |
| 164 | RunTest = angular_system.RunTest |
| 165 | |
| 166 | |
| 167 | def PlotStep(params, R, plant_params=None): |
| 168 | """Plots a step move to the goal. |
| 169 | |
| 170 | Args: |
| 171 | params: CatapultParams for the controller and observer |
| 172 | plant_params: CatapultParams for the plant. Defaults to params if |
| 173 | plant_params is None. |
| 174 | R: numpy.matrix(2, 1), the goal""" |
| 175 | plant = Catapult(plant_params or params, params.name) |
| 176 | controller = IntegralCatapult(params, params.name) |
| 177 | observer = IntegralCatapult(params, params.name) |
| 178 | |
| 179 | # Test moving the system. |
| 180 | initial_X = numpy.matrix([[0.0], [0.0]]) |
| 181 | augmented_R = numpy.matrix(numpy.zeros((3, 1))) |
| 182 | augmented_R[0:2, :] = R |
| 183 | RunTest(plant, |
| 184 | end_goal=augmented_R, |
| 185 | controller=controller, |
| 186 | observer=observer, |
| 187 | duration=2.0, |
| 188 | use_profile=False, |
| 189 | kick_time=1.0, |
| 190 | kick_magnitude=0.0) |
| 191 | |
| 192 | |
| 193 | def PlotKick(params, R, plant_params=None): |
| 194 | """Plots a step motion with a kick at 1.0 seconds. |
| 195 | |
| 196 | Args: |
| 197 | params: CatapultParams for the controller and observer |
| 198 | plant_params: CatapultParams for the plant. Defaults to params if |
| 199 | plant_params is None. |
| 200 | R: numpy.matrix(2, 1), the goal""" |
| 201 | plant = Catapult(plant_params or params, params.name) |
| 202 | controller = IntegralCatapult(params, params.name) |
| 203 | observer = IntegralCatapult(params, params.name) |
| 204 | |
| 205 | # Test moving the system. |
| 206 | initial_X = numpy.matrix([[0.0], [0.0]]) |
| 207 | augmented_R = numpy.matrix(numpy.zeros((3, 1))) |
| 208 | augmented_R[0:2, :] = R |
| 209 | RunTest(plant, |
| 210 | end_goal=augmented_R, |
| 211 | controller=controller, |
| 212 | observer=observer, |
| 213 | duration=2.0, |
| 214 | use_profile=False, |
| 215 | kick_time=1.0, |
| 216 | kick_magnitude=2.0) |
| 217 | |
| 218 | |
| 219 | def PlotMotion(params, |
| 220 | R, |
| 221 | max_velocity=10.0, |
| 222 | max_acceleration=70.0, |
| 223 | plant_params=None): |
| 224 | """Plots a trapezoidal motion. |
| 225 | |
| 226 | Args: |
| 227 | params: CatapultParams for the controller and observer |
| 228 | plant_params: CatapultParams for the plant. Defaults to params if |
| 229 | plant_params is None. |
| 230 | R: numpy.matrix(2, 1), the goal, |
| 231 | max_velocity: float, The max velocity of the profile. |
| 232 | max_acceleration: float, The max acceleration of the profile. |
| 233 | """ |
| 234 | plant = Catapult(plant_params or params, params.name) |
| 235 | controller = IntegralCatapult(params, params.name) |
| 236 | observer = IntegralCatapult(params, params.name) |
| 237 | |
| 238 | # Test moving the system. |
| 239 | initial_X = numpy.matrix([[0.0], [0.0]]) |
| 240 | augmented_R = numpy.matrix(numpy.zeros((3, 1))) |
| 241 | augmented_R[0:2, :] = R |
| 242 | RunTest(plant, |
| 243 | end_goal=augmented_R, |
| 244 | controller=controller, |
| 245 | observer=observer, |
| 246 | duration=2.0, |
| 247 | use_profile=True, |
| 248 | max_velocity=max_velocity, |
| 249 | max_acceleration=max_acceleration) |
| 250 | |
| 251 | |
Austin Schuh | 2e28d87 | 2022-02-19 18:25:57 -0800 | [diff] [blame] | 252 | def WriteCatapult(params, plant_files, controller_files, year_namespaces): |
| 253 | """Writes out the constants for a catapult to a file. |
| 254 | |
| 255 | Args: |
| 256 | params: list of CatapultParams or CatapultParams, the |
| 257 | parameters defining the system. |
| 258 | plant_files: list of strings, the cc and h files for the plant. |
| 259 | controller_files: list of strings, the cc and h files for the integral |
| 260 | controller. |
| 261 | year_namespaces: list of strings, the namespace list to use. |
| 262 | """ |
| 263 | # Write the generated constants out to a file. |
| 264 | catapults = [] |
| 265 | integral_catapults = [] |
| 266 | |
| 267 | if type(params) is list: |
| 268 | name = params[0].name |
| 269 | for index, param in enumerate(params): |
Austin Schuh | ac61c88 | 2022-02-23 17:22:22 -0800 | [diff] [blame] | 270 | catapults.append(Catapult(param, param.name + str(index))) |
Austin Schuh | 2e28d87 | 2022-02-19 18:25:57 -0800 | [diff] [blame] | 271 | integral_catapults.append( |
Austin Schuh | ac61c88 | 2022-02-23 17:22:22 -0800 | [diff] [blame] | 272 | IntegralCatapult(param, 'Integral' + param.name + str(index))) |
Austin Schuh | 2e28d87 | 2022-02-19 18:25:57 -0800 | [diff] [blame] | 273 | else: |
| 274 | name = params.name |
| 275 | catapults.append(Catapult(params, params.name)) |
| 276 | integral_catapults.append( |
| 277 | IntegralCatapult(params, 'Integral' + params.name)) |
| 278 | |
Austin Schuh | ac61c88 | 2022-02-23 17:22:22 -0800 | [diff] [blame] | 279 | loop_writer = control_loop.ControlLoopWriter(name, |
| 280 | catapults, |
| 281 | namespaces=year_namespaces) |
Austin Schuh | 2e28d87 | 2022-02-19 18:25:57 -0800 | [diff] [blame] | 282 | loop_writer.AddConstant( |
| 283 | control_loop.Constant('kOutputRatio', '%f', catapults[0].G)) |
| 284 | loop_writer.AddConstant( |
| 285 | control_loop.Constant('kFreeSpeed', '%f', |
| 286 | catapults[0].motor.free_speed)) |
| 287 | loop_writer.Write(plant_files[0], plant_files[1]) |
| 288 | |
| 289 | integral_loop_writer = control_loop.ControlLoopWriter( |
| 290 | 'Integral' + name, integral_catapults, namespaces=year_namespaces) |
| 291 | integral_loop_writer.Write(controller_files[0], controller_files[1]) |