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