Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 1 | #include "motors/core/kinetis.h" |
| 2 | |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 3 | #include <inttypes.h> |
Brian Silverman | dabdf90 | 2017-10-21 15:34:40 -0400 | [diff] [blame] | 4 | #include <stdio.h> |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 5 | |
| 6 | #include <atomic> |
| 7 | #include <cmath> |
| 8 | |
Brian Silverman | dabdf90 | 2017-10-21 15:34:40 -0400 | [diff] [blame] | 9 | #include "frc971/control_loops/drivetrain/integral_haptic_trigger.h" |
| 10 | #include "frc971/control_loops/drivetrain/integral_haptic_wheel.h" |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 11 | #include "motors/core/time.h" |
| 12 | #include "motors/motor.h" |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 13 | #include "motors/peripheral/can.h" |
Austin Schuh | b402fd4 | 2019-04-13 00:02:53 -0700 | [diff] [blame^] | 14 | #include "motors/pistol_grip/controller_adc.h" |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 15 | #include "motors/pistol_grip/motor_controls.h" |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame] | 16 | #include "motors/print/print.h" |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 17 | #include "motors/util.h" |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 18 | |
| 19 | #define MOTOR0_PWM_FTM FTM3 |
| 20 | #define MOTOR0_ENCODER_FTM FTM2 |
| 21 | #define MOTOR1_PWM_FTM FTM0 |
| 22 | #define MOTOR1_ENCODER_FTM FTM1 |
| 23 | |
Austin Schuh | 745e4ed | 2019-04-07 15:50:16 -0700 | [diff] [blame] | 24 | extern const float kWheelCoggingTorque0[4096]; |
| 25 | extern const float kWheelCoggingTorque1[4096]; |
| 26 | extern const float kTriggerCoggingTorque0[4096]; |
| 27 | extern const float kTriggerCoggingTorque1[4096]; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 28 | |
| 29 | namespace frc971 { |
Brian Silverman | a96c1a4 | 2018-05-12 12:11:31 -0700 | [diff] [blame] | 30 | namespace motors { |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 31 | namespace { |
| 32 | |
Austin Schuh | 745e4ed | 2019-04-07 15:50:16 -0700 | [diff] [blame] | 33 | ::std::atomic<const float *> trigger_cogging_torque{nullptr}; |
| 34 | ::std::atomic<const float *> wheel_cogging_torque{nullptr}; |
| 35 | |
| 36 | float TriggerCoggingTorque(uint32_t index) { |
| 37 | return trigger_cogging_torque.load(::std::memory_order_relaxed)[index]; |
| 38 | } |
| 39 | |
| 40 | float WheelCoggingTorque(uint32_t index) { |
| 41 | return wheel_cogging_torque.load(::std::memory_order_relaxed)[index]; |
| 42 | } |
| 43 | |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 44 | using ::frc971::control_loops::drivetrain::MakeIntegralHapticTriggerPlant; |
| 45 | using ::frc971::control_loops::drivetrain::MakeIntegralHapticTriggerObserver; |
| 46 | using ::frc971::control_loops::drivetrain::MakeIntegralHapticWheelPlant; |
| 47 | using ::frc971::control_loops::drivetrain::MakeIntegralHapticWheelObserver; |
| 48 | |
Austin Schuh | 80b9993 | 2019-04-07 14:04:41 -0700 | [diff] [blame] | 49 | // Returns an identifier for the processor we're running on. |
| 50 | // This isn't guaranteed to be unique, but it should be close enough. |
| 51 | uint8_t ProcessorIdentifier() { |
| 52 | // This XORs together all the bytes of the unique identifier provided by the |
| 53 | // hardware. |
| 54 | uint8_t r = 0; |
| 55 | for (uint8_t uid : {SIM_UIDH, SIM_UIDMH, SIM_UIDML, SIM_UIDL}) { |
| 56 | r = r ^ ((uid >> 0) & 0xFF); |
| 57 | r = r ^ ((uid >> 8) & 0xFF); |
| 58 | r = r ^ ((uid >> 16) & 0xFF); |
| 59 | r = r ^ ((uid >> 24) & 0xFF); |
| 60 | } |
| 61 | return r; |
| 62 | } |
| 63 | |
| 64 | uint8_t ProcessorIndex() { |
| 65 | switch (ProcessorIdentifier()) { |
| 66 | case static_cast<uint8_t>(0xaa): |
| 67 | return 1; |
| 68 | default: |
| 69 | return 0; |
| 70 | } |
| 71 | } |
| 72 | |
Brian Silverman | 9ed2cf1 | 2018-05-12 13:06:38 -0700 | [diff] [blame] | 73 | |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 74 | constexpr float kHapticWheelCurrentLimit = static_cast<float>( |
| 75 | ::frc971::control_loops::drivetrain::kHapticWheelCurrentLimit); |
| 76 | constexpr float kHapticTriggerCurrentLimit = static_cast<float>( |
| 77 | ::frc971::control_loops::drivetrain::kHapticTriggerCurrentLimit); |
| 78 | |
| 79 | ::std::atomic<Motor *> global_motor0{nullptr}, global_motor1{nullptr}; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 80 | |
| 81 | // Angle last time the current loop ran. |
| 82 | ::std::atomic<float> global_wheel_angle{0.0f}; |
| 83 | ::std::atomic<float> global_trigger_angle{0.0f}; |
| 84 | |
| 85 | // Wheel observer/plant. |
| 86 | ::std::atomic<StateFeedbackObserver<3, 1, 1, float> *> global_wheel_observer{ |
| 87 | nullptr}; |
| 88 | ::std::atomic<StateFeedbackPlant<3, 1, 1, float> *> global_wheel_plant{nullptr}; |
| 89 | // Throttle observer/plant. |
| 90 | ::std::atomic<StateFeedbackObserver<3, 1, 1, float> *> global_trigger_observer{ |
| 91 | nullptr}; |
| 92 | ::std::atomic<StateFeedbackPlant<3, 1, 1, float> *> global_trigger_plant{ |
| 93 | nullptr}; |
| 94 | |
| 95 | // Torques for the current loop to apply. |
| 96 | ::std::atomic<float> global_wheel_current{0.0f}; |
| 97 | ::std::atomic<float> global_trigger_torque{0.0f}; |
| 98 | |
| 99 | constexpr int kSwitchingDivisor = 2; |
| 100 | |
| 101 | float analog_ratio(uint16_t reading) { |
| 102 | static constexpr uint16_t kMin = 260, kMax = 3812; |
| 103 | return static_cast<float>(::std::max(::std::min(reading, kMax), kMin) - |
| 104 | kMin) / |
| 105 | static_cast<float>(kMax - kMin); |
| 106 | } |
| 107 | |
| 108 | constexpr float InterpolateFloat(float x1, float x0, float y1, float y0, float x) { |
| 109 | return (x - x0) * (y1 - y0) / (x1 - x0) + y0; |
| 110 | } |
| 111 | |
| 112 | float absolute_wheel(float wheel_position) { |
Austin Schuh | 4a8d492 | 2019-04-07 15:31:30 -0700 | [diff] [blame] | 113 | const float kCenterOffset = (ProcessorIndex() == 1) ? -0.683f : -0.935f; |
| 114 | |
| 115 | wheel_position += kCenterOffset; |
| 116 | |
| 117 | if (wheel_position > 0.5f) { |
| 118 | wheel_position -= 1.0f; |
| 119 | } else if (wheel_position < -0.5f) { |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 120 | wheel_position += 1.0f; |
| 121 | } |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 122 | return wheel_position; |
| 123 | } |
| 124 | |
| 125 | extern "C" { |
| 126 | |
| 127 | void *__stack_chk_guard = (void *)0x67111971; |
| 128 | void __stack_chk_fail() { |
| 129 | while (true) { |
| 130 | GPIOC_PSOR = (1 << 5); |
| 131 | printf("Stack corruption detected\n"); |
| 132 | delay(1000); |
| 133 | GPIOC_PCOR = (1 << 5); |
| 134 | delay(1000); |
| 135 | } |
| 136 | } |
| 137 | |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 138 | extern uint32_t __bss_ram_start__[], __bss_ram_end__[]; |
| 139 | extern uint32_t __data_ram_start__[], __data_ram_end__[]; |
| 140 | extern uint32_t __heap_start__[], __heap_end__[]; |
| 141 | extern uint32_t __stack_end__[]; |
| 142 | |
| 143 | } // extern "C" |
| 144 | |
| 145 | constexpr float kWheelMaxExtension = 1.0f; |
| 146 | constexpr float kWheelFrictionMax = 0.2f; |
| 147 | float WheelCenteringCurrent(float scalar, float angle, float velocity) { |
| 148 | float friction_goal_current = -angle * 10.0f; |
| 149 | if (friction_goal_current > kWheelFrictionMax) { |
| 150 | friction_goal_current = kWheelFrictionMax; |
| 151 | } else if (friction_goal_current < -kWheelFrictionMax) { |
| 152 | friction_goal_current = -kWheelFrictionMax; |
| 153 | } |
| 154 | |
| 155 | constexpr float kWheelSpringNonlinearity = 0.45f; |
| 156 | |
| 157 | float goal_current = -((1.0f - kWheelSpringNonlinearity) * angle + |
| 158 | kWheelSpringNonlinearity * angle * angle * angle) * |
| 159 | 6.0f - |
| 160 | velocity * 0.04f; |
| 161 | if (goal_current > 5.0f - scalar) { |
| 162 | goal_current = 5.0f - scalar; |
| 163 | } else if (goal_current < -5.0f + scalar) { |
| 164 | goal_current = -5.0f + scalar; |
| 165 | } |
| 166 | |
| 167 | return goal_current * scalar + friction_goal_current; |
| 168 | } |
| 169 | |
Austin Schuh | fbbf0f0 | 2019-04-07 14:27:16 -0700 | [diff] [blame] | 170 | float CoggingCurrent1(uint32_t encoder, int32_t absolute_encoder) { |
| 171 | constexpr float kP = 0.05f; |
| 172 | constexpr float kI = 0.00001f; |
| 173 | static int goal = -6700; |
| 174 | |
| 175 | const int error = goal - static_cast<int>(absolute_encoder); |
| 176 | static float error_sum = 0.0f; |
| 177 | float goal_current = static_cast<float>(error) * kP + error_sum * kI; |
| 178 | |
| 179 | goal_current = ::std::min(1.0f, ::std::max(-1.0f, goal_current)); |
| 180 | |
| 181 | static int i = 0; |
| 182 | if (error == 0) { |
| 183 | ++i; |
| 184 | } else { |
| 185 | i = 0; |
| 186 | } |
| 187 | if (i >= 100) { |
| 188 | printf("reading1: %d %d a:%d e:%d\n", goal, |
| 189 | static_cast<int>(goal_current * 10000.0f), |
| 190 | static_cast<int>(encoder), |
| 191 | static_cast<int>(error)); |
| 192 | static int counting_up = 0; |
| 193 | if (absolute_encoder <= -6900) { |
| 194 | counting_up = 1; |
| 195 | } else if (absolute_encoder >= 6900) { |
| 196 | counting_up = 0; |
| 197 | } |
| 198 | if (counting_up) { |
| 199 | ++goal; |
| 200 | } else { |
| 201 | --goal; |
| 202 | } |
| 203 | i = 0; |
| 204 | } |
| 205 | |
| 206 | error_sum += static_cast<float>(error); |
| 207 | if (error_sum > 1.0f / kI) { |
| 208 | error_sum = 1.0f / kI; |
| 209 | } else if (error_sum < -1.0f / kI) { |
| 210 | error_sum = -1.0f / kI; |
| 211 | } |
| 212 | return goal_current; |
| 213 | } |
| 214 | |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 215 | extern "C" void ftm0_isr() { |
| 216 | SmallAdcReadings readings; |
| 217 | { |
| 218 | DisableInterrupts disable_interrupts; |
| 219 | readings = AdcReadSmall1(disable_interrupts); |
| 220 | } |
| 221 | uint32_t encoder = |
| 222 | global_motor1.load(::std::memory_order_relaxed)->wrapped_encoder(); |
| 223 | int32_t absolute_encoder = global_motor1.load(::std::memory_order_relaxed) |
| 224 | ->absolute_encoder(encoder); |
| 225 | |
| 226 | const float angle = absolute_encoder / static_cast<float>((15320 - 1488) / 2); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 227 | |
Austin Schuh | fbbf0f0 | 2019-04-07 14:27:16 -0700 | [diff] [blame] | 228 | (void)CoggingCurrent1; |
Austin Schuh | 54c8c84 | 2019-04-07 13:54:23 -0700 | [diff] [blame] | 229 | float goal_current = global_wheel_current.load(::std::memory_order_relaxed) + |
Austin Schuh | 745e4ed | 2019-04-07 15:50:16 -0700 | [diff] [blame] | 230 | WheelCoggingTorque(encoder); |
Austin Schuh | fbbf0f0 | 2019-04-07 14:27:16 -0700 | [diff] [blame] | 231 | //float goal_current = CoggingCurrent1(encoder, absolute_encoder); |
Austin Schuh | 54c8c84 | 2019-04-07 13:54:23 -0700 | [diff] [blame] | 232 | //float goal_current = kWheelCoggingTorque[encoder]; |
| 233 | //float goal_current = 0.0f; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 234 | |
| 235 | global_motor1.load(::std::memory_order_relaxed)->SetGoalCurrent(goal_current); |
| 236 | global_motor1.load(::std::memory_order_relaxed) |
Austin Schuh | 54c8c84 | 2019-04-07 13:54:23 -0700 | [diff] [blame] | 237 | ->CurrentInterrupt(BalanceSimpleReadings(readings.currents), encoder); |
Austin Schuh | fbbf0f0 | 2019-04-07 14:27:16 -0700 | [diff] [blame] | 238 | //global_motor1.load(::std::memory_order_relaxed)->CycleFixedPhaseInterupt(); |
Austin Schuh | 54c8c84 | 2019-04-07 13:54:23 -0700 | [diff] [blame] | 239 | |
| 240 | global_wheel_angle.store(angle); |
Austin Schuh | 5b0e6b6 | 2019-04-07 14:23:37 -0700 | [diff] [blame] | 241 | |
| 242 | /* |
| 243 | SmallInitReadings position_readings; |
| 244 | { |
| 245 | DisableInterrupts disable_interrupts; |
| 246 | position_readings = AdcReadSmallInit(disable_interrupts); |
| 247 | } |
| 248 | |
| 249 | static int i = 0; |
| 250 | if (i == 1000) { |
| 251 | i = 0; |
| 252 | float wheel_position = |
| 253 | absolute_wheel(analog_ratio(position_readings.wheel_abs)); |
| 254 | printf( |
| 255 | "ecnt %" PRIu32 " arev:%d erev:%d abs:%d awp:%d uncalwheel:%d\n", |
| 256 | encoder, |
| 257 | static_cast<int>((1.0f - analog_ratio(position_readings.motor1_abs)) * |
| 258 | 7000.0f), |
| 259 | static_cast<int>(encoder * 7.0f / 4096.0f * 1000.0f), |
| 260 | static_cast<int>(absolute_encoder), |
| 261 | static_cast<int>(wheel_position * 1000.0f), |
| 262 | static_cast<int>(analog_ratio(position_readings.wheel_abs) * 1000.0f)); |
| 263 | } else if (i == 200) { |
| 264 | printf("out %" PRIu32 " %" PRIu32 " %" PRIu32 "\n", |
| 265 | global_motor1.load(::std::memory_order_relaxed) |
| 266 | ->output_registers()[0][2], |
| 267 | global_motor1.load(::std::memory_order_relaxed) |
| 268 | ->output_registers()[1][2], |
| 269 | global_motor1.load(::std::memory_order_relaxed) |
| 270 | ->output_registers()[2][2]); |
| 271 | } |
| 272 | ++i; |
| 273 | */ |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Austin Schuh | 876b4f0 | 2018-03-10 19:16:59 -0800 | [diff] [blame] | 276 | constexpr float kTriggerMaxExtension = -0.70f; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 277 | constexpr float kTriggerCenter = 0.0f; |
Austin Schuh | 876b4f0 | 2018-03-10 19:16:59 -0800 | [diff] [blame] | 278 | constexpr float kCenteringStiffness = 0.15f; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 279 | float TriggerCenteringCurrent(float trigger_angle) { |
| 280 | float goal_current = (kTriggerCenter - trigger_angle) * 3.0f; |
Austin Schuh | 876b4f0 | 2018-03-10 19:16:59 -0800 | [diff] [blame] | 281 | float knotch_goal_current = (kTriggerCenter - trigger_angle) * 8.0f; |
| 282 | if (knotch_goal_current < -kCenteringStiffness) { |
| 283 | knotch_goal_current = -kCenteringStiffness; |
| 284 | } else if (knotch_goal_current > kCenteringStiffness) { |
| 285 | knotch_goal_current = kCenteringStiffness; |
| 286 | } |
| 287 | |
| 288 | goal_current += knotch_goal_current; |
| 289 | |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 290 | if (goal_current < -1.0f) { |
| 291 | goal_current = -1.0f; |
| 292 | } else if (goal_current > 1.0f) { |
| 293 | goal_current = 1.0f; |
| 294 | if (trigger_angle < kTriggerMaxExtension) { |
| 295 | goal_current -= (30.0f * (trigger_angle - kTriggerMaxExtension)); |
Austin Schuh | 876b4f0 | 2018-03-10 19:16:59 -0800 | [diff] [blame] | 296 | if (goal_current > 4.0f) { |
| 297 | goal_current = 4.0f; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | } |
| 301 | return goal_current; |
| 302 | } |
| 303 | |
Austin Schuh | fbbf0f0 | 2019-04-07 14:27:16 -0700 | [diff] [blame] | 304 | float CoggingCurrent0(uint32_t encoder, int32_t absolute_encoder) { |
| 305 | constexpr float kP = 0.05f; |
| 306 | constexpr float kI = 0.00001f; |
| 307 | static int goal = 0; |
| 308 | |
| 309 | const int error = goal - static_cast<int>(absolute_encoder); |
| 310 | static float error_sum = 0.0f; |
| 311 | float goal_current = static_cast<float>(error) * kP + error_sum * kI; |
| 312 | |
| 313 | goal_current = ::std::min(1.0f, ::std::max(-1.0f, goal_current)); |
| 314 | |
| 315 | static int i = 0; |
| 316 | if (error == 0) { |
| 317 | ++i; |
| 318 | } else { |
| 319 | i = 0; |
| 320 | } |
| 321 | |
| 322 | if (i >= 100) { |
| 323 | printf("reading0: %d %d a:%d e:%d\n", goal, |
| 324 | static_cast<int>(goal_current * 10000.0f), |
| 325 | static_cast<int>(encoder), |
| 326 | static_cast<int>(error)); |
| 327 | static int counting_up = 0; |
| 328 | if (absolute_encoder <= -1390) { |
| 329 | counting_up = 1; |
| 330 | } else if (absolute_encoder >= 1390) { |
| 331 | counting_up = 0; |
| 332 | } |
| 333 | if (counting_up) { |
| 334 | ++goal; |
| 335 | } else { |
| 336 | --goal; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | error_sum += static_cast<float>(error); |
| 341 | if (error_sum > 1.0f / kI) { |
| 342 | error_sum = 1.0f / kI; |
| 343 | } else if (error_sum < -1.0f / kI) { |
| 344 | error_sum = -1.0f / kI; |
| 345 | } |
| 346 | return goal_current; |
| 347 | } |
| 348 | |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 349 | extern "C" void ftm3_isr() { |
| 350 | SmallAdcReadings readings; |
| 351 | { |
| 352 | DisableInterrupts disable_interrupts; |
| 353 | readings = AdcReadSmall0(disable_interrupts); |
| 354 | } |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 355 | |
Austin Schuh | 54c8c84 | 2019-04-07 13:54:23 -0700 | [diff] [blame] | 356 | const uint32_t encoder = |
| 357 | global_motor0.load(::std::memory_order_relaxed)->wrapped_encoder(); |
| 358 | const int32_t absolute_encoder = |
| 359 | global_motor0.load(::std::memory_order_relaxed) |
| 360 | ->absolute_encoder(encoder); |
| 361 | |
| 362 | const float trigger_angle = absolute_encoder / 1370.f; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 363 | |
Austin Schuh | fbbf0f0 | 2019-04-07 14:27:16 -0700 | [diff] [blame] | 364 | (void)CoggingCurrent0; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 365 | const float goal_current = |
Austin Schuh | 54c8c84 | 2019-04-07 13:54:23 -0700 | [diff] [blame] | 366 | global_trigger_torque.load(::std::memory_order_relaxed) + |
Austin Schuh | 745e4ed | 2019-04-07 15:50:16 -0700 | [diff] [blame] | 367 | TriggerCoggingTorque(encoder); |
Austin Schuh | 54c8c84 | 2019-04-07 13:54:23 -0700 | [diff] [blame] | 368 | //const float goal_current = kTriggerCoggingTorque[encoder]; |
| 369 | //const float goal_current = 0.0f; |
Austin Schuh | fbbf0f0 | 2019-04-07 14:27:16 -0700 | [diff] [blame] | 370 | //const float goal_current = CoggingCurrent0(encoder, absolute_encoder); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 371 | |
| 372 | global_motor0.load(::std::memory_order_relaxed)->SetGoalCurrent(goal_current); |
| 373 | global_motor0.load(::std::memory_order_relaxed) |
Austin Schuh | 54c8c84 | 2019-04-07 13:54:23 -0700 | [diff] [blame] | 374 | ->CurrentInterrupt(BalanceSimpleReadings(readings.currents), encoder); |
Austin Schuh | fbbf0f0 | 2019-04-07 14:27:16 -0700 | [diff] [blame] | 375 | //global_motor0.load(::std::memory_order_relaxed)->CycleFixedPhaseInterupt(); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 376 | |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 377 | global_trigger_angle.store(trigger_angle); |
Austin Schuh | 5b0e6b6 | 2019-04-07 14:23:37 -0700 | [diff] [blame] | 378 | |
| 379 | /* |
| 380 | SmallInitReadings position_readings; |
| 381 | { |
| 382 | DisableInterrupts disable_interrupts; |
| 383 | position_readings = AdcReadSmallInit(disable_interrupts); |
| 384 | } |
| 385 | |
| 386 | static int i = 0; |
| 387 | if (i == 1000) { |
| 388 | i = 0; |
| 389 | printf("ecnt %" PRIu32 " arev:%d erev:%d abs:%d\n", encoder, |
| 390 | static_cast<int>((analog_ratio(position_readings.motor0_abs)) * |
| 391 | 7000.0f), |
| 392 | static_cast<int>(encoder * 7.0f / 4096.0f * 1000.0f), |
| 393 | static_cast<int>(absolute_encoder)); |
| 394 | } else if (i == 200) { |
| 395 | printf("out %" PRIu32 " %" PRIu32 " %" PRIu32 "\n", |
| 396 | global_motor0.load(::std::memory_order_relaxed) |
| 397 | ->output_registers()[0][2], |
| 398 | global_motor0.load(::std::memory_order_relaxed) |
| 399 | ->output_registers()[1][2], |
| 400 | global_motor0.load(::std::memory_order_relaxed) |
| 401 | ->output_registers()[2][2]); |
| 402 | } |
| 403 | ++i; |
| 404 | */ |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 405 | } |
| 406 | |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 407 | int ConvertFloat16(float val) { |
| 408 | int result = static_cast<int>(val * 32768.0f) + 32768; |
| 409 | if (result > 0xffff) { |
| 410 | result = 0xffff; |
| 411 | } else if (result < 0) { |
| 412 | result = 0; |
| 413 | } |
| 414 | return result; |
| 415 | } |
| 416 | int ConvertFloat14(float val) { |
| 417 | int result = static_cast<int>(val * 8192.0f) + 8192; |
| 418 | if (result > 0x3fff) { |
| 419 | result = 0x3fff; |
| 420 | } else if (result < 0) { |
| 421 | result = 0; |
| 422 | } |
| 423 | return result; |
| 424 | } |
| 425 | |
| 426 | extern "C" void pit3_isr() { |
| 427 | PIT_TFLG3 = 1; |
| 428 | const float absolute_trigger_angle = |
| 429 | global_trigger_angle.load(::std::memory_order_relaxed); |
| 430 | const float absolute_wheel_angle = |
| 431 | global_wheel_angle.load(::std::memory_order_relaxed); |
| 432 | |
| 433 | // Force a barrier here so we sample everything guaranteed at the beginning. |
| 434 | __asm__("" ::: "memory"); |
| 435 | const float absolute_wheel_angle_radians = |
| 436 | absolute_wheel_angle * static_cast<float>(M_PI) * (338.16f / 360.0f); |
| 437 | const float absolute_trigger_angle_radians = |
| 438 | absolute_trigger_angle * static_cast<float>(M_PI) * (45.0f / 360.0f); |
| 439 | |
| 440 | static uint32_t last_command_time = 0; |
| 441 | static float trigger_goal_position = 0.0f; |
| 442 | static float trigger_goal_velocity = 0.0f; |
| 443 | static float trigger_haptic_current = 0.0f; |
| 444 | static bool trigger_centering = true; |
| 445 | static bool trigger_haptics = false; |
| 446 | { |
| 447 | uint8_t data[8]; |
| 448 | int length; |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 449 | can_receive(data, &length, 0); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 450 | if (length > 0) { |
| 451 | last_command_time = micros(); |
| 452 | trigger_goal_position = |
| 453 | static_cast<float>( |
| 454 | static_cast<int32_t>(static_cast<uint32_t>(data[0]) | |
| 455 | (static_cast<uint32_t>(data[1]) << 8)) - |
| 456 | 32768) / |
Brian Silverman | 6c8b88b | 2018-09-03 18:17:02 -0700 | [diff] [blame] | 457 | static_cast<float>(32768.0 * M_PI / 8.0); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 458 | trigger_goal_velocity = |
| 459 | static_cast<float>( |
| 460 | static_cast<int32_t>(static_cast<uint32_t>(data[2]) | |
| 461 | (static_cast<uint32_t>(data[3]) << 8)) - |
| 462 | 32768) / |
Brian Silverman | 6c8b88b | 2018-09-03 18:17:02 -0700 | [diff] [blame] | 463 | static_cast<float>(32768.0 * 4.0); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 464 | |
| 465 | trigger_haptic_current = |
| 466 | static_cast<float>( |
| 467 | static_cast<int32_t>(static_cast<uint32_t>(data[4]) | |
| 468 | (static_cast<uint32_t>(data[5]) << 8)) - |
| 469 | 32768) / |
Brian Silverman | 6c8b88b | 2018-09-03 18:17:02 -0700 | [diff] [blame] | 470 | static_cast<float>(32768.0 * 2.0); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 471 | if (trigger_haptic_current > kHapticTriggerCurrentLimit) { |
| 472 | trigger_haptic_current = kHapticTriggerCurrentLimit; |
| 473 | } else if (trigger_haptic_current < -kHapticTriggerCurrentLimit) { |
| 474 | trigger_haptic_current = -kHapticTriggerCurrentLimit; |
| 475 | } |
| 476 | trigger_centering = !!(data[7] & 0x01); |
| 477 | trigger_haptics = !!(data[7] & 0x02); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | static float wheel_goal_position = 0.0f; |
| 482 | static float wheel_goal_velocity = 0.0f; |
| 483 | static float wheel_haptic_current = 0.0f; |
| 484 | static float wheel_kp = 0.0f; |
| 485 | static bool wheel_centering = true; |
| 486 | static float wheel_centering_scalar = 0.25f; |
| 487 | { |
| 488 | uint8_t data[8]; |
| 489 | int length; |
Brian Silverman | 54dd2fe | 2018-03-16 23:44:31 -0700 | [diff] [blame] | 490 | can_receive(data, &length, 1); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 491 | if (length == 8) { |
| 492 | last_command_time = micros(); |
| 493 | wheel_goal_position = |
| 494 | static_cast<float>( |
| 495 | static_cast<int32_t>(static_cast<uint32_t>(data[0]) | |
| 496 | (static_cast<uint32_t>(data[1]) << 8)) - |
| 497 | 32768) / |
Brian Silverman | 6c8b88b | 2018-09-03 18:17:02 -0700 | [diff] [blame] | 498 | static_cast<float>(32768.0 * M_PI); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 499 | wheel_goal_velocity = |
| 500 | static_cast<float>( |
| 501 | static_cast<int32_t>(static_cast<uint32_t>(data[2]) | |
| 502 | (static_cast<uint32_t>(data[3]) << 8)) - |
| 503 | 32768) / |
Brian Silverman | 6c8b88b | 2018-09-03 18:17:02 -0700 | [diff] [blame] | 504 | static_cast<float>(32768.0 * 10.0); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 505 | |
| 506 | wheel_haptic_current = |
| 507 | static_cast<float>( |
| 508 | static_cast<int32_t>(static_cast<uint32_t>(data[4]) | |
| 509 | (static_cast<uint32_t>(data[5]) << 8)) - |
| 510 | 32768) / |
Brian Silverman | 6c8b88b | 2018-09-03 18:17:02 -0700 | [diff] [blame] | 511 | static_cast<float>(32768.0 * 2.0); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 512 | if (wheel_haptic_current > kHapticWheelCurrentLimit) { |
| 513 | wheel_haptic_current = kHapticWheelCurrentLimit; |
| 514 | } else if (wheel_haptic_current < -kHapticWheelCurrentLimit) { |
| 515 | wheel_haptic_current = -kHapticWheelCurrentLimit; |
| 516 | } |
| 517 | wheel_kp = static_cast<float>(data[6]) * 30.0f / 255.0f; |
| 518 | wheel_centering = !!(data[7] & 0x01); |
| 519 | wheel_centering_scalar = ((data[7] >> 1) & 0x7f) / 127.0f; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | static constexpr uint32_t kTimeout = 100000; |
| 524 | if (!time_after(time_add(last_command_time, kTimeout), micros())) { |
| 525 | last_command_time = time_subtract(micros(), kTimeout); |
| 526 | trigger_goal_position = 0.0f; |
| 527 | trigger_goal_velocity = 0.0f; |
| 528 | trigger_haptic_current = 0.0f; |
| 529 | trigger_centering = true; |
| 530 | trigger_haptics = false; |
| 531 | |
| 532 | wheel_goal_position = 0.0f; |
| 533 | wheel_goal_velocity = 0.0f; |
| 534 | wheel_haptic_current = 0.0f; |
| 535 | wheel_centering = true; |
| 536 | wheel_centering_scalar = 0.25f; |
Brian Silverman | 17ffa8c | 2018-03-09 18:27:29 -0800 | [diff] [blame] | 537 | // Avoid wrapping back into the valid range. |
| 538 | last_command_time = time_subtract(micros(), kTimeout); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | StateFeedbackPlant<3, 1, 1, float> *const trigger_plant = |
| 542 | global_trigger_plant.load(::std::memory_order_relaxed); |
| 543 | StateFeedbackObserver<3, 1, 1, float> *const trigger_observer = |
| 544 | global_trigger_observer.load(::std::memory_order_relaxed); |
| 545 | ::Eigen::Matrix<float, 1, 1> trigger_Y; |
| 546 | trigger_Y << absolute_trigger_angle_radians; |
| 547 | trigger_observer->Correct(*trigger_plant, |
| 548 | ::Eigen::Matrix<float, 1, 1>::Zero(), trigger_Y); |
| 549 | |
| 550 | StateFeedbackPlant<3, 1, 1, float> *const wheel_plant = |
| 551 | global_wheel_plant.load(::std::memory_order_relaxed); |
| 552 | StateFeedbackObserver<3, 1, 1, float> *const wheel_observer = |
| 553 | global_wheel_observer.load(::std::memory_order_relaxed); |
| 554 | ::Eigen::Matrix<float, 1, 1> wheel_Y; |
| 555 | wheel_Y << absolute_wheel_angle_radians; |
| 556 | wheel_observer->Correct(*wheel_plant, ::Eigen::Matrix<float, 1, 1>::Zero(), |
| 557 | wheel_Y); |
| 558 | |
| 559 | float kWheelD = (wheel_kp - 10.0f) * (0.25f - 0.20f) / 5.0f + 0.20f; |
| 560 | if (wheel_kp < 0.5f) { |
| 561 | kWheelD = wheel_kp * 0.05f / 0.5f; |
| 562 | } else if (wheel_kp < 1.0f) { |
| 563 | kWheelD = InterpolateFloat(1.0f, 0.5f, 0.06f, 0.05f, wheel_kp); |
| 564 | } else if (wheel_kp < 2.0f) { |
| 565 | kWheelD = InterpolateFloat(2.0f, 1.0f, 0.08f, 0.06f, wheel_kp); |
| 566 | } else if (wheel_kp < 3.0f) { |
| 567 | kWheelD = InterpolateFloat(3.0f, 2.0f, 0.10f, 0.08f, wheel_kp); |
| 568 | } else if (wheel_kp < 5.0f) { |
| 569 | kWheelD = InterpolateFloat(5.0f, 3.0f, 0.13f, 0.10f, wheel_kp); |
| 570 | } else if (wheel_kp < 10.0f) { |
| 571 | kWheelD = InterpolateFloat(10.0f, 5.0f, 0.20f, 0.13f, wheel_kp); |
| 572 | } |
| 573 | |
| 574 | float wheel_goal_current = wheel_haptic_current; |
| 575 | |
| 576 | wheel_goal_current += |
| 577 | (wheel_goal_position - absolute_wheel_angle_radians) * wheel_kp + |
| 578 | (wheel_goal_velocity - wheel_observer->X_hat()(1, 0)) * kWheelD; |
| 579 | |
| 580 | // Compute the torques to apply to each motor. |
| 581 | if (wheel_centering) { |
| 582 | wheel_goal_current += |
| 583 | WheelCenteringCurrent(wheel_centering_scalar, absolute_wheel_angle, |
| 584 | wheel_observer->X_hat()(1, 0)); |
| 585 | } |
| 586 | |
| 587 | if (wheel_goal_current > kHapticWheelCurrentLimit) { |
| 588 | wheel_goal_current = kHapticWheelCurrentLimit; |
| 589 | } else if (wheel_goal_current < -kHapticWheelCurrentLimit) { |
| 590 | wheel_goal_current = -kHapticWheelCurrentLimit; |
| 591 | } |
| 592 | global_wheel_current.store(wheel_goal_current, ::std::memory_order_relaxed); |
| 593 | |
| 594 | constexpr float kTriggerP = |
| 595 | static_cast<float>(::frc971::control_loops::drivetrain::kHapticTriggerP); |
| 596 | constexpr float kTriggerD = |
| 597 | static_cast<float>(::frc971::control_loops::drivetrain::kHapticTriggerD); |
| 598 | float trigger_goal_current = trigger_haptic_current; |
| 599 | if (trigger_haptics) { |
| 600 | trigger_goal_current += |
| 601 | (trigger_goal_position - absolute_trigger_angle_radians) * kTriggerP + |
| 602 | (trigger_goal_velocity - trigger_observer->X_hat()(1, 0)) * kTriggerD; |
| 603 | } |
| 604 | |
| 605 | if (trigger_centering) { |
| 606 | trigger_goal_current += TriggerCenteringCurrent(absolute_trigger_angle); |
| 607 | } |
| 608 | |
| 609 | if (trigger_goal_current > kHapticTriggerCurrentLimit) { |
| 610 | trigger_goal_current = kHapticTriggerCurrentLimit; |
| 611 | } else if (trigger_goal_current < -kHapticTriggerCurrentLimit) { |
| 612 | trigger_goal_current = -kHapticTriggerCurrentLimit; |
| 613 | } |
| 614 | global_trigger_torque.store(trigger_goal_current, |
| 615 | ::std::memory_order_relaxed); |
| 616 | |
| 617 | uint8_t buttons = 0; |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 618 | if (!PERIPHERAL_BITBAND(GPIOA_PDIR, 14)) { |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 619 | buttons |= 0x1; |
| 620 | } |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 621 | if (!PERIPHERAL_BITBAND(GPIOE_PDIR, 26)) { |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 622 | buttons |= 0x2; |
| 623 | } |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 624 | if (!PERIPHERAL_BITBAND(GPIOC_PDIR, 7)) { |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 625 | buttons |= 0x4; |
| 626 | } |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 627 | if (!PERIPHERAL_BITBAND(GPIOD_PDIR, 0)) { |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 628 | buttons |= 0x8; |
| 629 | } |
| 630 | |
| 631 | float trigger_angle = absolute_trigger_angle; |
| 632 | |
| 633 | // Adjust the trigger range for reporting back. |
| 634 | // TODO(austin): We'll likely need to make this symmetric for the controls to |
| 635 | // work out well. |
| 636 | if (trigger_angle > kTriggerCenter) { |
| 637 | trigger_angle = (trigger_angle - kTriggerCenter) / (1.0f - kTriggerCenter); |
| 638 | } else { |
| 639 | trigger_angle = (trigger_angle - kTriggerCenter) / |
| 640 | (kTriggerCenter - kTriggerMaxExtension); |
| 641 | } |
| 642 | |
| 643 | // TODO(austin): Class + fns. This is a mess. |
| 644 | // TODO(austin): Move this to a separate file. It's too big. |
| 645 | int can_trigger = ConvertFloat16(absolute_trigger_angle); |
| 646 | int can_trigger_velocity = |
| 647 | ConvertFloat16(trigger_observer->X_hat()(1, 0) / 50.0f); |
| 648 | int can_trigger_torque = |
| 649 | ConvertFloat16(trigger_observer->X_hat()(2, 0) * 2.0f); |
| 650 | int can_trigger_current = ConvertFloat14(trigger_goal_current / 10.0f); |
| 651 | |
| 652 | int can_wheel = ConvertFloat16(absolute_wheel_angle); |
| 653 | int can_wheel_velocity = |
| 654 | ConvertFloat16(wheel_observer->X_hat()(1, 0) / 50.0f); |
| 655 | int can_wheel_torque = ConvertFloat16(wheel_observer->X_hat()(2, 0) * 2.0f); |
| 656 | int can_wheel_current = ConvertFloat14(wheel_goal_current / 10.0f); |
| 657 | |
| 658 | { |
| 659 | const uint8_t trigger_joystick_values[8] = { |
| 660 | static_cast<uint8_t>(can_trigger & 0xff), |
| 661 | static_cast<uint8_t>((can_trigger >> 8) & 0xff), |
| 662 | static_cast<uint8_t>(can_trigger_velocity & 0xff), |
| 663 | static_cast<uint8_t>((can_trigger_velocity >> 8) & 0xff), |
| 664 | static_cast<uint8_t>(can_trigger_torque & 0xff), |
| 665 | static_cast<uint8_t>((can_trigger_torque >> 8) & 0xff), |
| 666 | static_cast<uint8_t>(can_trigger_current & 0xff), |
| 667 | static_cast<uint8_t>(((buttons & 0x3) << 6) | |
| 668 | (can_trigger_current >> 8))}; |
| 669 | const uint8_t wheel_joystick_values[8] = { |
| 670 | static_cast<uint8_t>(can_wheel & 0xff), |
| 671 | static_cast<uint8_t>((can_wheel >> 8) & 0xff), |
| 672 | static_cast<uint8_t>(can_wheel_velocity & 0xff), |
| 673 | static_cast<uint8_t>((can_wheel_velocity >> 8) & 0xff), |
| 674 | static_cast<uint8_t>(can_wheel_torque & 0xff), |
| 675 | static_cast<uint8_t>((can_wheel_torque >> 8) & 0xff), |
| 676 | static_cast<uint8_t>(can_wheel_current & 0xff), |
| 677 | static_cast<uint8_t>(((buttons & 0xc) << 4) | |
| 678 | (can_wheel_current >> 8))}; |
| 679 | |
| 680 | can_send(0, trigger_joystick_values, 8, 2); |
| 681 | can_send(1, wheel_joystick_values, 8, 3); |
| 682 | } |
| 683 | |
| 684 | ::Eigen::Matrix<float, 1, 1> trigger_U; |
| 685 | trigger_U << trigger_goal_current; |
| 686 | ::Eigen::Matrix<float, 1, 1> wheel_U; |
| 687 | wheel_U << wheel_goal_current; |
| 688 | trigger_observer->Predict(trigger_plant, trigger_U, |
| 689 | ::std::chrono::milliseconds(1)); |
| 690 | wheel_observer->Predict(wheel_plant, wheel_U, ::std::chrono::milliseconds(1)); |
| 691 | } |
| 692 | |
| 693 | void ConfigurePwmFtm(BigFTM *pwm_ftm) { |
| 694 | // Put them all into combine active-high mode, and all the low ones staying |
| 695 | // off all the time by default. We'll then use only the low ones. |
| 696 | pwm_ftm->C0SC = FTM_CSC_ELSB; |
| 697 | pwm_ftm->C0V = 0; |
| 698 | pwm_ftm->C1SC = FTM_CSC_ELSB; |
| 699 | pwm_ftm->C1V = 0; |
| 700 | pwm_ftm->C2SC = FTM_CSC_ELSB; |
| 701 | pwm_ftm->C2V = 0; |
| 702 | pwm_ftm->C3SC = FTM_CSC_ELSB; |
| 703 | pwm_ftm->C3V = 0; |
| 704 | pwm_ftm->C4SC = FTM_CSC_ELSB; |
| 705 | pwm_ftm->C4V = 0; |
| 706 | pwm_ftm->C5SC = FTM_CSC_ELSB; |
| 707 | pwm_ftm->C5V = 0; |
| 708 | pwm_ftm->C6SC = FTM_CSC_ELSB; |
| 709 | pwm_ftm->C6V = 0; |
| 710 | pwm_ftm->C7SC = FTM_CSC_ELSB; |
| 711 | pwm_ftm->C7V = 0; |
| 712 | |
| 713 | pwm_ftm->COMBINE = FTM_COMBINE_SYNCEN3 /* Synchronize updates usefully */ | |
| 714 | FTM_COMBINE_COMP3 /* Make them complementary */ | |
| 715 | FTM_COMBINE_COMBINE3 /* Combine the channels */ | |
| 716 | FTM_COMBINE_SYNCEN2 /* Synchronize updates usefully */ | |
| 717 | FTM_COMBINE_COMP2 /* Make them complementary */ | |
| 718 | FTM_COMBINE_COMBINE2 /* Combine the channels */ | |
| 719 | FTM_COMBINE_SYNCEN1 /* Synchronize updates usefully */ | |
| 720 | FTM_COMBINE_COMP1 /* Make them complementary */ | |
| 721 | FTM_COMBINE_COMBINE1 /* Combine the channels */ | |
| 722 | FTM_COMBINE_SYNCEN0 /* Synchronize updates usefully */ | |
| 723 | FTM_COMBINE_COMP0 /* Make them complementary */ | |
| 724 | FTM_COMBINE_COMBINE0 /* Combine the channels */; |
| 725 | } |
| 726 | |
| 727 | bool CountValid(uint32_t count) { |
| 728 | static constexpr int kMaxMovement = 1; |
| 729 | return count <= kMaxMovement || count >= (4096 - kMaxMovement); |
| 730 | } |
| 731 | |
| 732 | bool ZeroMotors(uint16_t *motor0_offset, uint16_t *motor1_offset, |
| 733 | uint16_t *wheel_offset) { |
| 734 | static constexpr int kNumberSamples = 1024; |
| 735 | static_assert(UINT16_MAX * kNumberSamples <= UINT32_MAX, "Too many samples"); |
| 736 | uint32_t motor0_sum = 0, motor1_sum = 0, wheel_sum = 0; |
| 737 | |
| 738 | // First clear both encoders. |
| 739 | MOTOR0_ENCODER_FTM->CNT = MOTOR1_ENCODER_FTM->CNT = 0; |
| 740 | for (int i = 0; i < kNumberSamples; ++i) { |
| 741 | delay(1); |
| 742 | |
| 743 | if (!CountValid(MOTOR0_ENCODER_FTM->CNT)) { |
| 744 | printf("Motor 0 moved too much\n"); |
| 745 | return false; |
| 746 | } |
| 747 | if (!CountValid(MOTOR1_ENCODER_FTM->CNT)) { |
| 748 | printf("Motor 1 moved too much\n"); |
| 749 | return false; |
| 750 | } |
| 751 | |
| 752 | DisableInterrupts disable_interrupts; |
| 753 | const SmallInitReadings readings = AdcReadSmallInit(disable_interrupts); |
| 754 | motor0_sum += readings.motor0_abs; |
| 755 | motor1_sum += readings.motor1_abs; |
| 756 | wheel_sum += readings.wheel_abs; |
| 757 | } |
| 758 | |
| 759 | *motor0_offset = (motor0_sum + kNumberSamples / 2) / kNumberSamples; |
| 760 | *motor1_offset = (motor1_sum + kNumberSamples / 2) / kNumberSamples; |
| 761 | *wheel_offset = (wheel_sum + kNumberSamples / 2) / kNumberSamples; |
| 762 | |
| 763 | return true; |
| 764 | } |
| 765 | |
| 766 | } // namespace |
| 767 | |
| 768 | extern "C" int main() { |
| 769 | // for background about this startup delay, please see these conversations |
| 770 | // https://forum.pjrc.com/threads/36606-startup-time-(400ms)?p=113980&viewfull=1#post113980 |
| 771 | // https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273 |
| 772 | delay(400); |
| 773 | |
| 774 | // Set all interrupts to the second-lowest priority to start with. |
| 775 | for (int i = 0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_SANE_PRIORITY(i, 0xD); |
| 776 | |
| 777 | // Now set priorities for all the ones we care about. They only have meaning |
| 778 | // relative to each other, which means centralizing them here makes it a lot |
| 779 | // more manageable. |
| 780 | NVIC_SET_SANE_PRIORITY(IRQ_USBOTG, 0x7); |
| 781 | NVIC_SET_SANE_PRIORITY(IRQ_FTM0, 0x3); |
| 782 | NVIC_SET_SANE_PRIORITY(IRQ_FTM3, 0x3); |
| 783 | NVIC_SET_SANE_PRIORITY(IRQ_PIT_CH3, 0x5); |
| 784 | |
| 785 | // Set the LED's pin to output mode. |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 786 | PERIPHERAL_BITBAND(GPIOC_PDDR, 5) = 1; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 787 | PORTC_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 788 | |
| 789 | // Set up the CAN pins. |
| 790 | PORTA_PCR12 = PORT_PCR_DSE | PORT_PCR_MUX(2); |
| 791 | PORTA_PCR13 = PORT_PCR_DSE | PORT_PCR_MUX(2); |
| 792 | |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 793 | // .1ms filter time. |
| 794 | PORTA_DFWR = PORTC_DFWR = PORTD_DFWR = PORTE_DFWR = 6000; |
| 795 | |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 796 | // BTN0 |
| 797 | PORTC_PCR7 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 798 | PORTC_DFER |= 1 << 7; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 799 | // BTN1 |
| 800 | PORTE_PCR26 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 801 | PORTE_DFER |= 1 << 26; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 802 | // BTN2 |
| 803 | PORTA_PCR14 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 804 | PORTA_DFER |= 1 << 14; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 805 | // BTN3 |
| 806 | PORTD_PCR0 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); |
Brian Silverman | ff7b387 | 2018-03-10 18:08:30 -0800 | [diff] [blame] | 807 | PORTD_DFER |= 1 << 0; |
| 808 | // BTN4 |
| 809 | PORTD_PCR7 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); |
| 810 | PORTD_DFER |= 1 << 7; |
| 811 | // BTN5 (only new revision) |
| 812 | PORTA_PCR15 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); |
| 813 | PORTA_DFER |= 1 << 15; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 814 | |
| 815 | PORTA_PCR5 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); |
| 816 | |
Brian Silverman | 45564a8 | 2018-09-02 16:35:22 -0700 | [diff] [blame] | 817 | DMA.CR = M_DMA_EMLM; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 818 | |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame] | 819 | PrintingParameters printing_parameters; |
| 820 | printing_parameters.dedicated_usb = true; |
| 821 | const ::std::unique_ptr<PrintingImplementation> printing = |
| 822 | CreatePrinting(printing_parameters); |
| 823 | printing->Initialize(); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 824 | |
| 825 | AdcInitSmall(); |
| 826 | MathInit(); |
| 827 | delay(100); |
| 828 | can_init(2, 3); |
| 829 | |
| 830 | GPIOD_PCOR = 1 << 3; |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 831 | PERIPHERAL_BITBAND(GPIOD_PDDR, 3) = 1; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 832 | PORTD_PCR3 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 833 | GPIOD_PSOR = 1 << 3; |
| 834 | |
| 835 | GPIOC_PCOR = 1 << 4; |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 836 | PERIPHERAL_BITBAND(GPIOC_PDDR, 4) = 1; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 837 | PORTC_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 838 | GPIOC_PSOR = 1 << 4; |
| 839 | |
| 840 | LittleMotorControlsImplementation controls0, controls1; |
| 841 | |
| 842 | delay(100); |
| 843 | |
| 844 | // M0_EA = FTM1_QD_PHB |
| 845 | PORTB_PCR19 = PORT_PCR_MUX(6); |
| 846 | // M0_EB = FTM1_QD_PHA |
| 847 | PORTB_PCR18 = PORT_PCR_MUX(6); |
| 848 | |
| 849 | // M1_EA = FTM1_QD_PHA |
| 850 | PORTB_PCR0 = PORT_PCR_MUX(6); |
| 851 | // M1_EB = FTM1_QD_PHB |
| 852 | PORTB_PCR1 = PORT_PCR_MUX(6); |
| 853 | |
| 854 | // M0_CH0 = FTM3_CH4 |
| 855 | PORTC_PCR8 = PORT_PCR_DSE | PORT_PCR_MUX(3); |
| 856 | // M0_CH1 = FTM3_CH2 |
| 857 | PORTD_PCR2 = PORT_PCR_DSE | PORT_PCR_MUX(4); |
| 858 | // M0_CH2 = FTM3_CH6 |
| 859 | PORTC_PCR10 = PORT_PCR_DSE | PORT_PCR_MUX(3); |
| 860 | |
| 861 | // M1_CH0 = FTM0_CH0 |
| 862 | PORTC_PCR1 = PORT_PCR_DSE | PORT_PCR_MUX(4); |
| 863 | // M1_CH1 = FTM0_CH2 |
| 864 | PORTC_PCR3 = PORT_PCR_DSE | PORT_PCR_MUX(4); |
| 865 | // M1_CH2 = FTM0_CH4 |
| 866 | PORTD_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(4); |
| 867 | |
| 868 | Motor motor0( |
| 869 | MOTOR0_PWM_FTM, MOTOR0_ENCODER_FTM, &controls0, |
| 870 | {&MOTOR0_PWM_FTM->C4V, &MOTOR0_PWM_FTM->C2V, &MOTOR0_PWM_FTM->C6V}); |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame] | 871 | motor0.set_printing_implementation(printing.get()); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 872 | motor0.set_switching_divisor(kSwitchingDivisor); |
| 873 | Motor motor1( |
| 874 | MOTOR1_PWM_FTM, MOTOR1_ENCODER_FTM, &controls1, |
| 875 | {&MOTOR1_PWM_FTM->C0V, &MOTOR1_PWM_FTM->C2V, &MOTOR1_PWM_FTM->C4V}); |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame] | 876 | motor1.set_printing_implementation(printing.get()); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 877 | motor1.set_switching_divisor(kSwitchingDivisor); |
| 878 | ConfigurePwmFtm(MOTOR0_PWM_FTM); |
| 879 | ConfigurePwmFtm(MOTOR1_PWM_FTM); |
| 880 | motor0.Init(); |
| 881 | motor1.Init(); |
| 882 | global_motor0.store(&motor0, ::std::memory_order_relaxed); |
| 883 | global_motor1.store(&motor1, ::std::memory_order_relaxed); |
| 884 | |
| 885 | SIM_SCGC6 |= SIM_SCGC6_PIT; |
Brian Silverman | b0de240 | 2018-03-24 03:48:28 -0400 | [diff] [blame] | 886 | // Workaround for errata e7914. |
| 887 | (void)PIT_MCR; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 888 | PIT_MCR = 0; |
Brian Silverman | b0de240 | 2018-03-24 03:48:28 -0400 | [diff] [blame] | 889 | PIT_LDVAL3 = (BUS_CLOCK_FREQUENCY / 1000) - 1; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 890 | PIT_TCTRL3 = PIT_TCTRL_TIE | PIT_TCTRL_TEN; |
| 891 | |
| 892 | // Have them both wait for the GTB signal. |
| 893 | FTM0->CONF = FTM3->CONF = |
| 894 | FTM_CONF_GTBEEN | FTM_CONF_NUMTOF(kSwitchingDivisor - 1); |
| 895 | // Make FTM3's period half of what it should be so we can get it a half-cycle |
| 896 | // out of phase. |
| 897 | const uint32_t original_mod = FTM3->MOD; |
| 898 | FTM3->MOD = ((original_mod + 1) / 2) - 1; |
| 899 | FTM3->SYNC |= FTM_SYNC_SWSYNC; |
| 900 | |
| 901 | // Output triggers to things like the PDBs on initialization. |
| 902 | FTM0_EXTTRIG = FTM_EXTTRIG_INITTRIGEN; |
| 903 | FTM3_EXTTRIG = FTM_EXTTRIG_INITTRIGEN; |
| 904 | // Don't let any memory accesses sneak past here, because we actually |
| 905 | // need everything to be starting up. |
| 906 | __asm__("" ::: "memory"); |
| 907 | |
| 908 | // Give everything a chance to get going. |
| 909 | delay(100); |
| 910 | |
| 911 | printf("BSS: %p-%p\n", __bss_ram_start__, __bss_ram_end__); |
| 912 | printf("data: %p-%p\n", __data_ram_start__, __data_ram_end__); |
| 913 | printf("heap start: %p\n", __heap_start__); |
| 914 | printf("stack start: %p\n", __stack_end__); |
| 915 | |
Austin Schuh | 745e4ed | 2019-04-07 15:50:16 -0700 | [diff] [blame] | 916 | trigger_cogging_torque.store(ProcessorIndex() == 0 ? kTriggerCoggingTorque0 |
| 917 | : kTriggerCoggingTorque1); |
| 918 | wheel_cogging_torque.store(ProcessorIndex() == 0 ? kWheelCoggingTorque0 |
| 919 | : kWheelCoggingTorque1); |
| 920 | |
Austin Schuh | 80b9993 | 2019-04-07 14:04:41 -0700 | [diff] [blame] | 921 | printf("Zeroing motors for %d:%x\n", static_cast<int>(ProcessorIndex()), |
| 922 | (unsigned int)ProcessorIdentifier()); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 923 | uint16_t motor0_offset, motor1_offset, wheel_offset; |
| 924 | while (!ZeroMotors(&motor0_offset, &motor1_offset, &wheel_offset)) { |
| 925 | } |
| 926 | printf("Done zeroing\n"); |
| 927 | |
| 928 | const float motor0_offset_scaled = -analog_ratio(motor0_offset); |
| 929 | const float motor1_offset_scaled = analog_ratio(motor1_offset); |
| 930 | // Good for the initial trigger. |
| 931 | { |
Austin Schuh | 4a8d492 | 2019-04-07 15:31:30 -0700 | [diff] [blame] | 932 | // Calibrate winding phase error here. |
| 933 | const float kZeroOffset0 = ProcessorIndex() == 1 ? 0.275f : 0.27f; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 934 | const int motor0_starting_point = static_cast<int>( |
| 935 | (motor0_offset_scaled + (kZeroOffset0 / 7.0f)) * 4096.0f); |
| 936 | printf("Motor 0 starting at %d\n", motor0_starting_point); |
| 937 | motor0.set_encoder_calibration_offset(motor0_starting_point); |
| 938 | motor0.set_encoder_multiplier(-1); |
| 939 | |
Austin Schuh | 4a8d492 | 2019-04-07 15:31:30 -0700 | [diff] [blame] | 940 | // Calibrate output coordinate neutral here. |
| 941 | motor0.set_encoder_offset( |
| 942 | motor0.encoder_offset() + |
| 943 | (ProcessorIndex() == 1 ? (-3096 - 430 - 30 - 6) : (-2065 + 20))); |
| 944 | |
| 945 | while (true) { |
| 946 | const uint32_t encoder = |
| 947 | global_motor0.load(::std::memory_order_relaxed)->wrapped_encoder(); |
| 948 | const int32_t absolute_encoder = |
| 949 | global_motor0.load(::std::memory_order_relaxed) |
| 950 | ->absolute_encoder(encoder); |
| 951 | |
| 952 | if (absolute_encoder > 2047) { |
| 953 | motor0.set_encoder_offset(motor0.encoder_offset() - 4096); |
| 954 | } else if (absolute_encoder < -2047) { |
| 955 | motor0.set_encoder_offset(motor0.encoder_offset() + 4096); |
| 956 | } else { |
| 957 | break; |
| 958 | } |
| 959 | } |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 960 | |
| 961 | uint32_t new_encoder = motor0.wrapped_encoder(); |
| 962 | int32_t absolute_encoder = motor0.absolute_encoder(new_encoder); |
| 963 | printf("Motor 0 encoder %d absolute %d\n", static_cast<int>(new_encoder), |
| 964 | static_cast<int>(absolute_encoder)); |
| 965 | } |
| 966 | |
| 967 | { |
Austin Schuh | 4a8d492 | 2019-04-07 15:31:30 -0700 | [diff] [blame] | 968 | const float kZeroOffset1 = ProcessorIndex() == 1 ? 0.420f : 0.26f; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 969 | const int motor1_starting_point = static_cast<int>( |
| 970 | (motor1_offset_scaled + (kZeroOffset1 / 7.0f)) * 4096.0f); |
| 971 | printf("Motor 1 starting at %d\n", motor1_starting_point); |
| 972 | motor1.set_encoder_calibration_offset(motor1_starting_point); |
| 973 | motor1.set_encoder_multiplier(-1); |
| 974 | |
| 975 | float wheel_position = absolute_wheel(analog_ratio(wheel_offset)); |
| 976 | |
| 977 | uint32_t encoder = motor1.wrapped_encoder(); |
| 978 | |
| 979 | printf("Wheel starting at %d, encoder %" PRId32 "\n", |
| 980 | static_cast<int>(wheel_position * 1000.0f), encoder); |
| 981 | |
| 982 | constexpr float kWheelGearRatio = (1.25f + 0.02f) / 0.35f; |
Austin Schuh | 4a8d492 | 2019-04-07 15:31:30 -0700 | [diff] [blame] | 983 | const float kWrappedWheelAtZero = |
| 984 | ProcessorIndex() == 1 ? (0.934630859375f) : 0.6586310546875f; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 985 | |
| 986 | const int encoder_wraps = |
| 987 | static_cast<int>(lround(wheel_position * kWheelGearRatio - |
| 988 | (encoder / 4096.f) + kWrappedWheelAtZero)); |
| 989 | |
| 990 | printf("Wraps: %d\n", encoder_wraps); |
| 991 | motor1.set_encoder_offset(4096 * encoder_wraps + motor1.encoder_offset() - |
| 992 | static_cast<int>(kWrappedWheelAtZero * 4096)); |
| 993 | printf("Wheel encoder now at %d\n", |
| 994 | static_cast<int>(1000.f / 4096.f * |
| 995 | motor1.absolute_encoder(motor1.wrapped_encoder()))); |
| 996 | } |
| 997 | |
| 998 | // Turn an LED on for Austin. |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 999 | PERIPHERAL_BITBAND(GPIOC_PDDR, 6) = 1; |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 1000 | GPIOC_PCOR = 1 << 6; |
| 1001 | PORTC_PCR6 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 1002 | |
| 1003 | // M0_THW |
| 1004 | PORTC_PCR11 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); |
| 1005 | // M0_FAULT |
| 1006 | PORTD_PCR6 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); |
| 1007 | // M1_THW |
| 1008 | PORTC_PCR2 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); |
| 1009 | // M1_FAULT |
| 1010 | PORTD_PCR5 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1); |
| 1011 | |
| 1012 | motor0.Start(); |
| 1013 | motor1.Start(); |
| 1014 | { |
| 1015 | // We rely on various things happening faster than the timer period, so make |
| 1016 | // sure slow USB or whatever interrupts don't prevent that. |
| 1017 | DisableInterrupts disable_interrupts; |
| 1018 | |
| 1019 | // First clear the overflow flag. |
| 1020 | FTM3->SC &= ~FTM_SC_TOF; |
| 1021 | |
| 1022 | // Now poke the GTB to actually start both timers. |
| 1023 | FTM0->CONF = FTM_CONF_GTBEEN | FTM_CONF_GTBEOUT | |
| 1024 | FTM_CONF_NUMTOF(kSwitchingDivisor - 1); |
| 1025 | |
| 1026 | // Wait for it to overflow twice. For some reason, just once doesn't work. |
| 1027 | while (!(FTM3->SC & FTM_SC_TOF)) { |
| 1028 | } |
| 1029 | FTM3->SC &= ~FTM_SC_TOF; |
| 1030 | while (!(FTM3->SC & FTM_SC_TOF)) { |
| 1031 | } |
| 1032 | |
| 1033 | // Now put the MOD value back to what it was. |
| 1034 | FTM3->MOD = original_mod; |
| 1035 | FTM3->PWMLOAD = FTM_PWMLOAD_LDOK; |
| 1036 | |
| 1037 | // And then clear the overflow flags before enabling interrupts so we |
| 1038 | // actually wait until the next overflow to start doing interrupts. |
| 1039 | FTM0->SC &= ~FTM_SC_TOF; |
| 1040 | FTM3->SC &= ~FTM_SC_TOF; |
| 1041 | NVIC_ENABLE_IRQ(IRQ_FTM0); |
| 1042 | NVIC_ENABLE_IRQ(IRQ_FTM3); |
| 1043 | } |
| 1044 | global_trigger_plant.store( |
| 1045 | new StateFeedbackPlant<3, 1, 1, float>(MakeIntegralHapticTriggerPlant())); |
| 1046 | global_trigger_observer.store(new StateFeedbackObserver<3, 1, 1, float>( |
| 1047 | MakeIntegralHapticTriggerObserver())); |
| 1048 | global_trigger_observer.load(::std::memory_order_relaxed) |
| 1049 | ->Reset(global_trigger_plant.load(::std::memory_order_relaxed)); |
| 1050 | |
| 1051 | global_wheel_plant.store( |
| 1052 | new StateFeedbackPlant<3, 1, 1, float>(MakeIntegralHapticWheelPlant())); |
| 1053 | global_wheel_observer.store(new StateFeedbackObserver<3, 1, 1, float>( |
| 1054 | MakeIntegralHapticWheelObserver())); |
| 1055 | global_wheel_observer.load(::std::memory_order_relaxed) |
| 1056 | ->Reset(global_wheel_plant.load(::std::memory_order_relaxed)); |
| 1057 | |
| 1058 | delay(1000); |
| 1059 | |
| 1060 | NVIC_ENABLE_IRQ(IRQ_PIT_CH3); |
| 1061 | |
| 1062 | // TODO(Brian): Use SLEEPONEXIT to reduce interrupt latency? |
| 1063 | while (true) { |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 1064 | if (!PERIPHERAL_BITBAND(GPIOC_PDIR, 11)) { |
| 1065 | if (!PERIPHERAL_BITBAND(GPIOC_PDOR, 5)) { |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 1066 | printf("M0_THW\n"); |
| 1067 | } |
| 1068 | GPIOC_PSOR = 1 << 5; |
| 1069 | } |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 1070 | if (!PERIPHERAL_BITBAND(GPIOD_PDIR, 6)) { |
| 1071 | if (!PERIPHERAL_BITBAND(GPIOC_PDOR, 5)) { |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 1072 | printf("M0_FAULT\n"); |
| 1073 | } |
| 1074 | GPIOC_PSOR = 1 << 5; |
| 1075 | } |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 1076 | if (!PERIPHERAL_BITBAND(GPIOC_PDIR, 2)) { |
| 1077 | if (!PERIPHERAL_BITBAND(GPIOC_PDOR, 5)) { |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 1078 | printf("M1_THW\n"); |
| 1079 | } |
| 1080 | GPIOC_PSOR = 1 << 5; |
| 1081 | } |
Brian Silverman | 33eb5fa | 2018-02-11 18:36:19 -0500 | [diff] [blame] | 1082 | if (!PERIPHERAL_BITBAND(GPIOD_PDIR, 5)) { |
| 1083 | if (!PERIPHERAL_BITBAND(GPIOC_PDOR, 5)) { |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 1084 | printf("M1_FAULT\n"); |
| 1085 | } |
| 1086 | GPIOC_PSOR = 1 << 5; |
| 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | return 0; |
| 1091 | } |
| 1092 | |
Brian Silverman | a96c1a4 | 2018-05-12 12:11:31 -0700 | [diff] [blame] | 1093 | } // namespace motors |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 1094 | } // namespace frc971 |