Austin Schuh | 085eab9 | 2020-11-26 13:54:51 -0800 | [diff] [blame] | 1 | #!/usr/bin/python3 |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 2 | |
| 3 | from frc971.control_loops.python import control_loop |
| 4 | from frc971.control_loops.python import controls |
| 5 | import numpy |
| 6 | import sys |
| 7 | import copy |
| 8 | import scipy.interpolate |
| 9 | from matplotlib import pylab |
| 10 | |
| 11 | import gflags |
| 12 | import glog |
| 13 | |
| 14 | FLAGS = gflags.FLAGS |
| 15 | |
| 16 | gflags.DEFINE_bool('plot', False, 'If true, plot the loop response.') |
| 17 | gflags.DEFINE_string('data', None, 'If defined, plot the provided CAN data') |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 18 | gflags.DEFINE_bool( |
| 19 | 'rerun_kf', False, |
| 20 | 'If true, rerun the KF. The torque in the data file will be interpreted as the commanded current.' |
| 21 | ) |
| 22 | |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 23 | |
| 24 | class SystemParams(object): |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 25 | def __init__(self, J, G, kP, kD, kCompensationTimeconstant, q_pos, q_vel, |
| 26 | q_torque, current_limit): |
| 27 | self.J = J |
| 28 | self.G = G |
| 29 | self.q_pos = q_pos |
| 30 | self.q_vel = q_vel |
| 31 | self.q_torque = q_torque |
| 32 | self.kP = kP |
| 33 | self.kD = kD |
| 34 | self.kCompensationTimeconstant = kCompensationTimeconstant |
| 35 | self.r_pos = 0.001 |
| 36 | self.current_limit = current_limit |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 37 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 38 | #[15.0, 0.25], |
| 39 | #[10.0, 0.2], |
| 40 | #[5.0, 0.13], |
| 41 | #[3.0, 0.10], |
| 42 | #[2.0, 0.08], |
| 43 | #[1.0, 0.06], |
| 44 | #[0.5, 0.05], |
| 45 | #[0.25, 0.025], |
| 46 | |
| 47 | |
| 48 | kWheel = SystemParams( |
| 49 | J=0.0008, |
| 50 | G=(1.25 + 0.02) / 0.35, |
| 51 | q_pos=0.001, |
| 52 | q_vel=0.20, |
| 53 | q_torque=0.005, |
| 54 | kP=7.0, |
| 55 | kD=0.0, |
| 56 | kCompensationTimeconstant=0.95, |
| 57 | current_limit=4.5) |
| 58 | kTrigger = SystemParams( |
| 59 | J=0.00025, |
| 60 | G=(0.925 * 2.0 + 0.02) / 0.35, |
| 61 | q_pos=0.001, |
| 62 | q_vel=0.1, |
| 63 | q_torque=0.005, |
| 64 | kP=120.0, |
| 65 | kD=1.8, |
| 66 | kCompensationTimeconstant=0.95, |
| 67 | current_limit=3.0) |
| 68 | |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 69 | |
| 70 | class HapticInput(control_loop.ControlLoop): |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 71 | def __init__(self, params=None, name='HapticInput'): |
| 72 | # The defaults are for the steering wheel. |
| 73 | super(HapticInput, self).__init__(name) |
| 74 | motor = self.motor = control_loop.MN3510() |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 75 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 76 | # Moment of inertia of the wheel in kg m^2 |
| 77 | self.J = params.J |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 78 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 79 | # Control loop time step |
| 80 | self.dt = 0.001 |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 81 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 82 | # Gear ratio from the motor to the input. |
| 83 | self.G = params.G |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 84 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 85 | self.A_continuous = numpy.matrix(numpy.zeros((2, 2))) |
| 86 | self.A_continuous[1, 1] = 0 |
| 87 | self.A_continuous[0, 1] = 1 |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 88 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 89 | self.B_continuous = numpy.matrix(numpy.zeros((2, 1))) |
| 90 | self.B_continuous[1, 0] = motor.Kt * self.G / self.J |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 91 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 92 | # State feedback matrices |
| 93 | # [position, angular velocity] |
| 94 | self.C = numpy.matrix([[1.0, 0.0]]) |
| 95 | self.D = numpy.matrix([[0.0]]) |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 96 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 97 | self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, |
| 98 | self.B_continuous, self.dt) |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 99 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 100 | self.U_max = numpy.matrix([[2.5]]) |
| 101 | self.U_min = numpy.matrix([[-2.5]]) |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 102 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 103 | self.L = numpy.matrix([[0.0], [0.0]]) |
| 104 | self.K = numpy.matrix([[0.0, 0.0]]) |
| 105 | |
| 106 | self.InitializeState() |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 107 | |
| 108 | |
| 109 | class IntegralHapticInput(HapticInput): |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 110 | def __init__(self, params=None, name="IntegralHapticInput"): |
| 111 | super(IntegralHapticInput, self).__init__(name=name, params=params) |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 112 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 113 | self.A_continuous_unaugmented = self.A_continuous |
| 114 | self.B_continuous_unaugmented = self.B_continuous |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 115 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 116 | self.A_continuous = numpy.matrix(numpy.zeros((3, 3))) |
| 117 | self.A_continuous[0:2, 0:2] = self.A_continuous_unaugmented |
| 118 | self.A_continuous[1, 2] = (1 / self.J) |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 119 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 120 | self.B_continuous = numpy.matrix(numpy.zeros((3, 1))) |
| 121 | self.B_continuous[0:2, 0] = self.B_continuous_unaugmented |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 122 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 123 | self.C_unaugmented = self.C |
| 124 | self.C = numpy.matrix(numpy.zeros((1, 3))) |
| 125 | self.C[0:1, 0:2] = self.C_unaugmented |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 126 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 127 | self.A, self.B = self.ContinuousToDiscrete(self.A_continuous, |
| 128 | self.B_continuous, self.dt) |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 129 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 130 | self.Q = numpy.matrix([[(params.q_pos**2.0), 0.0, 0.0], |
| 131 | [0.0, (params.q_vel**2.0), 0.0], |
| 132 | [0.0, 0.0, (params.q_torque**2.0)]]) |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 133 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 134 | self.R = numpy.matrix([[(params.r_pos**2.0)]]) |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 135 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 136 | self.KalmanGain, self.Q_steady = controls.kalman( |
| 137 | A=self.A, B=self.B, C=self.C, Q=self.Q, R=self.R) |
| 138 | self.L = self.A * self.KalmanGain |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 139 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 140 | self.K_unaugmented = self.K |
| 141 | self.K = numpy.matrix(numpy.zeros((1, 3))) |
| 142 | self.K[0, 0:2] = self.K_unaugmented |
| 143 | self.K[0, 2] = 1.0 / (self.motor.Kt / (self.motor.resistance)) |
| 144 | |
| 145 | self.InitializeState() |
| 146 | |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 147 | |
| 148 | def ReadCan(filename): |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 149 | """Reads the candump in filename and returns the 4 fields.""" |
| 150 | trigger = [] |
| 151 | trigger_velocity = [] |
| 152 | trigger_torque = [] |
| 153 | trigger_current = [] |
| 154 | wheel = [] |
| 155 | wheel_velocity = [] |
| 156 | wheel_torque = [] |
| 157 | wheel_current = [] |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 158 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 159 | trigger_request_time = [0.0] |
| 160 | trigger_request_current = [0.0] |
| 161 | wheel_request_time = [0.0] |
| 162 | wheel_request_current = [0.0] |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 163 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 164 | with open(filename, 'r') as fd: |
| 165 | for line in fd: |
| 166 | data = line.split() |
| 167 | can_id = int(data[1], 16) |
| 168 | if can_id == 0: |
| 169 | data = [int(d, 16) for d in data[3:]] |
| 170 | trigger.append(((data[0] + (data[1] << 8)) - 32768) / 32768.0) |
| 171 | trigger_velocity.append( |
| 172 | ((data[2] + (data[3] << 8)) - 32768) / 32768.0) |
| 173 | trigger_torque.append( |
| 174 | ((data[4] + (data[5] << 8)) - 32768) / 32768.0) |
| 175 | trigger_current.append( |
| 176 | ((data[6] + ((data[7] & 0x3f) << 8)) - 8192) / 8192.0) |
| 177 | elif can_id == 1: |
| 178 | data = [int(d, 16) for d in data[3:]] |
| 179 | wheel.append(((data[0] + (data[1] << 8)) - 32768) / 32768.0) |
| 180 | wheel_velocity.append( |
| 181 | ((data[2] + (data[3] << 8)) - 32768) / 32768.0) |
| 182 | wheel_torque.append( |
| 183 | ((data[4] + (data[5] << 8)) - 32768) / 32768.0) |
| 184 | wheel_current.append( |
| 185 | ((data[6] + ((data[7] & 0x3f) << 8)) - 8192) / 8192.0) |
| 186 | elif can_id == 2: |
| 187 | data = [int(d, 16) for d in data[3:]] |
| 188 | trigger_request_current.append( |
| 189 | ((data[4] + (data[5] << 8)) - 32768) / 32768.0) |
| 190 | trigger_request_time.append(len(trigger) * 0.001) |
| 191 | elif can_id == 3: |
| 192 | data = [int(d, 16) for d in data[3:]] |
| 193 | wheel_request_current.append( |
| 194 | ((data[4] + (data[5] << 8)) - 32768) / 32768.0) |
| 195 | wheel_request_time.append(len(wheel) * 0.001) |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 196 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 197 | trigger_data_time = numpy.arange(0, len(trigger)) * 0.001 |
| 198 | wheel_data_time = numpy.arange(0, len(wheel)) * 0.001 |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 199 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 200 | # Extend out the data in the interpolation table. |
| 201 | trigger_request_time.append(trigger_data_time[-1]) |
| 202 | trigger_request_current.append(trigger_request_current[-1]) |
| 203 | wheel_request_time.append(wheel_data_time[-1]) |
| 204 | wheel_request_current.append(wheel_request_current[-1]) |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 205 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 206 | return (trigger_data_time, wheel_data_time, trigger, wheel, |
| 207 | trigger_velocity, wheel_velocity, trigger_torque, wheel_torque, |
| 208 | trigger_current, wheel_current, trigger_request_time, |
| 209 | trigger_request_current, wheel_request_time, wheel_request_current) |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 210 | |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 211 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 212 | def rerun_and_plot_kf(data_time, |
| 213 | data_radians, |
| 214 | data_current, |
| 215 | data_request_current, |
| 216 | params, |
| 217 | run_correct=True): |
| 218 | kf_velocity = [] |
| 219 | dt_velocity = [] |
| 220 | kf_position = [] |
| 221 | adjusted_position = [] |
| 222 | last_angle = None |
| 223 | haptic_observer = IntegralHapticInput(params=params) |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 224 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 225 | # Parameter sweep J. |
| 226 | num_kf = 1 |
| 227 | min_J = max_J = params.J |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 228 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 229 | # J = 0.0022 |
| 230 | #num_kf = 15 |
| 231 | #min_J = min_J / 2.0 |
| 232 | #max_J = max_J * 2.0 |
| 233 | initial_velocity = (data_radians[1] - data_radians[0]) * 1000.0 |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 234 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 235 | def DupParamsWithJ(params, J): |
| 236 | p = copy.copy(params) |
| 237 | p.J = J |
| 238 | return p |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 239 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 240 | haptic_observers = [ |
| 241 | IntegralHapticInput(params=DupParamsWithJ(params, j)) |
| 242 | for j in numpy.logspace( |
| 243 | numpy.log10(min_J), numpy.log10(max_J), num=num_kf) |
| 244 | ] |
| 245 | # Initialize all the KF's. |
| 246 | haptic_observer.X_hat[1, 0] = initial_velocity |
| 247 | haptic_observer.X_hat[0, 0] = data_radians[0] |
| 248 | for observer in haptic_observers: |
| 249 | observer.X_hat[1, 0] = initial_velocity |
| 250 | observer.X_hat[0, 0] = data_radians[0] |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 251 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 252 | last_request_current = data_request_current[0] |
Austin Schuh | 5ea4847 | 2021-02-02 20:46:41 -0800 | [diff] [blame] | 253 | kf_torques = [[] for i in range(num_kf)] |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 254 | for angle, current, request_current in zip(data_radians, data_current, |
| 255 | data_request_current): |
| 256 | # Predict and correct all the parameter swept observers. |
| 257 | for i, observer in enumerate(haptic_observers): |
| 258 | observer.Y = numpy.matrix([[angle]]) |
| 259 | if run_correct: |
| 260 | observer.CorrectObserver(numpy.matrix([[current]])) |
| 261 | kf_torques[i].append(-observer.X_hat[2, 0]) |
| 262 | observer.PredictObserver(numpy.matrix([[current]])) |
| 263 | observer.PredictObserver(numpy.matrix([[current]])) |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 264 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 265 | # Predict and correct the main observer. |
| 266 | haptic_observer.Y = numpy.matrix([[angle]]) |
| 267 | if run_correct: |
| 268 | haptic_observer.CorrectObserver(numpy.matrix([[current]])) |
| 269 | kf_position.append(haptic_observer.X_hat[0, 0]) |
| 270 | adjusted_position.append(kf_position[-1] - |
| 271 | last_request_current / params.kP) |
| 272 | last_request_current = last_request_current * params.kCompensationTimeconstant + request_current * ( |
| 273 | 1.0 - params.kCompensationTimeconstant) |
| 274 | kf_velocity.append(haptic_observer.X_hat[1, 0]) |
| 275 | if last_angle is None: |
| 276 | last_angle = angle |
| 277 | dt_velocity.append((angle - last_angle) / 0.001) |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 278 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 279 | haptic_observer.PredictObserver(numpy.matrix([[current]])) |
| 280 | last_angle = angle |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 281 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 282 | # Plot the wheel observers. |
| 283 | fig, ax1 = pylab.subplots() |
| 284 | ax1.plot(data_time, data_radians, '.', label='wheel') |
| 285 | ax1.plot(data_time, dt_velocity, '.', label='dt_velocity') |
| 286 | ax1.plot(data_time, kf_velocity, '.', label='kf_velocity') |
| 287 | ax1.plot(data_time, kf_position, '.', label='kf_position') |
| 288 | ax1.plot(data_time, adjusted_position, '.', label='adjusted_position') |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 289 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 290 | ax2 = ax1.twinx() |
| 291 | ax2.plot(data_time, data_current, label='data_current') |
| 292 | ax2.plot(data_time, data_request_current, label='request_current') |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 293 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 294 | for i, kf_torque in enumerate(kf_torques): |
| 295 | ax2.plot( |
| 296 | data_time, |
| 297 | kf_torque, |
| 298 | label='-kf_torque[%f]' % haptic_observers[i].J) |
| 299 | fig.tight_layout() |
| 300 | ax1.legend(loc=3) |
| 301 | ax2.legend(loc=4) |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 302 | |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 303 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 304 | def plot_input(data_time, |
| 305 | data_radians, |
| 306 | data_velocity, |
| 307 | data_torque, |
| 308 | data_current, |
| 309 | params, |
| 310 | run_correct=True): |
| 311 | dt_velocity = [] |
| 312 | last_angle = None |
| 313 | initial_velocity = (data_radians[1] - data_radians[0]) * 1000.0 |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 314 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 315 | for angle in data_radians: |
| 316 | if last_angle is None: |
| 317 | last_angle = angle |
| 318 | dt_velocity.append((angle - last_angle) / 0.001) |
| 319 | |
| 320 | last_angle = angle |
| 321 | |
| 322 | # Plot the wheel observers. |
| 323 | fig, ax1 = pylab.subplots() |
| 324 | ax1.plot(data_time, data_radians, '.', label='angle') |
| 325 | ax1.plot(data_time, data_velocity, '-', label='velocity') |
| 326 | ax1.plot(data_time, dt_velocity, '.', label='dt_velocity') |
| 327 | |
| 328 | ax2 = ax1.twinx() |
| 329 | ax2.plot(data_time, data_torque, label='data_torque') |
| 330 | ax2.plot(data_time, data_current, label='data_current') |
| 331 | fig.tight_layout() |
| 332 | ax1.legend(loc=3) |
| 333 | ax2.legend(loc=4) |
| 334 | |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 335 | |
| 336 | def main(argv): |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 337 | if FLAGS.plot: |
| 338 | if FLAGS.data is None: |
| 339 | haptic_wheel = HapticInput() |
| 340 | haptic_wheel_controller = IntegralHapticInput() |
| 341 | observer_haptic_wheel = IntegralHapticInput() |
| 342 | observer_haptic_wheel.X_hat[2, 0] = 0.01 |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 343 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 344 | R = numpy.matrix([[0.0], [0.0], [0.0]]) |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 345 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 346 | control_loop.TestSingleIntegralAxisSquareWave( |
| 347 | R, 1.0, haptic_wheel, haptic_wheel_controller, |
| 348 | observer_haptic_wheel) |
| 349 | else: |
| 350 | # Read the CAN trace in. |
| 351 | trigger_data_time, wheel_data_time, trigger, wheel, trigger_velocity, \ |
| 352 | wheel_velocity, trigger_torque, wheel_torque, trigger_current, \ |
| 353 | wheel_current, trigger_request_time, trigger_request_current, \ |
| 354 | wheel_request_time, wheel_request_current = ReadCan(FLAGS.data) |
| 355 | |
| 356 | wheel_radians = [w * numpy.pi * (338.16 / 360.0) for w in wheel] |
| 357 | wheel_velocity = [w * 50.0 for w in wheel_velocity] |
| 358 | wheel_torque = [w / 2.0 for w in wheel_torque] |
| 359 | wheel_current = [w * 10.0 for w in wheel_current] |
| 360 | wheel_request_current = [w * 2.0 for w in wheel_request_current] |
| 361 | resampled_wheel_request_current = scipy.interpolate.interp1d( |
| 362 | wheel_request_time, wheel_request_current, |
| 363 | kind="zero")(wheel_data_time) |
| 364 | |
| 365 | trigger_radians = [t * numpy.pi * (45.0 / 360.0) for t in trigger] |
| 366 | trigger_velocity = [t * 50.0 for t in trigger_velocity] |
| 367 | trigger_torque = [t / 2.0 for t in trigger_torque] |
| 368 | trigger_current = [t * 10.0 for t in trigger_current] |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 369 | trigger_request_current = [ |
| 370 | t * 2.0 for t in trigger_request_current |
| 371 | ] |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 372 | resampled_trigger_request_current = scipy.interpolate.interp1d( |
| 373 | trigger_request_time, trigger_request_current, |
| 374 | kind="zero")(trigger_data_time) |
| 375 | |
| 376 | if FLAGS.rerun_kf: |
| 377 | rerun_and_plot_kf( |
| 378 | trigger_data_time, |
| 379 | trigger_radians, |
| 380 | trigger_current, |
| 381 | resampled_trigger_request_current, |
| 382 | kTrigger, |
| 383 | run_correct=True) |
| 384 | rerun_and_plot_kf( |
| 385 | wheel_data_time, |
| 386 | wheel_radians, |
| 387 | wheel_current, |
| 388 | resampled_wheel_request_current, |
| 389 | kWheel, |
| 390 | run_correct=True) |
| 391 | else: |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 392 | plot_input(trigger_data_time, trigger_radians, |
| 393 | trigger_velocity, trigger_torque, trigger_current, |
| 394 | kTrigger) |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 395 | plot_input(wheel_data_time, wheel_radians, wheel_velocity, |
| 396 | wheel_torque, wheel_current, kWheel) |
| 397 | pylab.show() |
| 398 | |
| 399 | return |
| 400 | |
| 401 | if len(argv) != 9: |
| 402 | glog.fatal('Expected .h file name and .cc file name') |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 403 | else: |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 404 | namespaces = ['frc971', 'control_loops', 'drivetrain'] |
| 405 | for name, params, filenames in [('HapticWheel', kWheel, argv[1:5]), |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 406 | ('HapticTrigger', kTrigger, |
| 407 | argv[5:9])]: |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 408 | haptic_input = HapticInput(params=params, name=name) |
| 409 | loop_writer = control_loop.ControlLoopWriter( |
| 410 | name, [haptic_input], |
| 411 | namespaces=namespaces, |
| 412 | scalar_type='float') |
| 413 | loop_writer.Write(filenames[0], filenames[1]) |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 414 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 415 | integral_haptic_input = IntegralHapticInput( |
| 416 | params=params, name='Integral' + name) |
| 417 | integral_loop_writer = control_loop.ControlLoopWriter( |
| 418 | 'Integral' + name, [integral_haptic_input], |
| 419 | namespaces=namespaces, |
| 420 | scalar_type='float') |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 421 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 422 | integral_loop_writer.AddConstant( |
| 423 | control_loop.Constant("k" + name + "Dt", "%f", |
| 424 | integral_haptic_input.dt)) |
| 425 | integral_loop_writer.AddConstant( |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 426 | control_loop.Constant( |
| 427 | "k" + name + "FreeCurrent", "%f", |
| 428 | integral_haptic_input.motor.free_current)) |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 429 | integral_loop_writer.AddConstant( |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 430 | control_loop.Constant( |
| 431 | "k" + name + "StallTorque", "%f", |
| 432 | integral_haptic_input.motor.stall_torque)) |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 433 | integral_loop_writer.AddConstant( |
| 434 | control_loop.Constant("k" + name + "J", "%f", |
| 435 | integral_haptic_input.J)) |
| 436 | integral_loop_writer.AddConstant( |
| 437 | control_loop.Constant("k" + name + "R", "%f", |
| 438 | integral_haptic_input.motor.resistance)) |
| 439 | integral_loop_writer.AddConstant( |
| 440 | control_loop.Constant("k" + name + "T", "%f", |
| 441 | integral_haptic_input.motor.Kt)) |
| 442 | integral_loop_writer.AddConstant( |
| 443 | control_loop.Constant("k" + name + "V", "%f", |
| 444 | integral_haptic_input.motor.Kv)) |
| 445 | integral_loop_writer.AddConstant( |
| 446 | control_loop.Constant("k" + name + "P", "%f", params.kP)) |
| 447 | integral_loop_writer.AddConstant( |
| 448 | control_loop.Constant("k" + name + "D", "%f", params.kD)) |
| 449 | integral_loop_writer.AddConstant( |
| 450 | control_loop.Constant("k" + name + "G", "%f", params.G)) |
| 451 | integral_loop_writer.AddConstant( |
| 452 | control_loop.Constant("k" + name + "CurrentLimit", "%f", |
| 453 | params.current_limit)) |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 454 | |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 455 | integral_loop_writer.Write(filenames[2], filenames[3]) |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 456 | |
| 457 | |
| 458 | if __name__ == '__main__': |
Tyler Chatow | 6738c36 | 2019-02-16 14:12:30 -0800 | [diff] [blame] | 459 | argv = FLAGS(sys.argv) |
| 460 | sys.exit(main(argv)) |