Brian Silverman | d44258e | 2018-05-14 10:01:18 -0700 | [diff] [blame] | 1 | #include "motors/core/kinetis.h" |
| 2 | |
| 3 | #include <inttypes.h> |
| 4 | #include <math.h> |
| 5 | #include <stdio.h> |
| 6 | |
| 7 | #include <atomic> |
| 8 | |
| 9 | #include "motors/core/time.h" |
| 10 | #include "motors/peripheral/adc.h" |
| 11 | #include "motors/usb/cdc.h" |
| 12 | #include "motors/usb/usb.h" |
| 13 | #include "motors/util.h" |
| 14 | |
| 15 | namespace frc971 { |
| 16 | namespace motors { |
| 17 | namespace { |
| 18 | |
| 19 | struct Fet12AdcReadings { |
| 20 | // 1100 off, 3160 floored |
| 21 | uint16_t throttle; |
| 22 | }; |
| 23 | |
| 24 | void AdcInitFet12() { |
| 25 | AdcInitCommon(); |
| 26 | |
| 27 | // EI2C_SCL (end pin) ADC0_SE13 |
| 28 | PORTB_PCR3 = PORT_PCR_MUX(0); |
| 29 | } |
| 30 | |
| 31 | Fet12AdcReadings AdcReadFet12(const DisableInterrupts &) { |
| 32 | Fet12AdcReadings r; |
| 33 | |
| 34 | ADC0_SC1A = 13; |
| 35 | while (!(ADC0_SC1A & ADC_SC1_COCO)) { |
| 36 | } |
| 37 | r.throttle = ADC0_RA; |
| 38 | |
| 39 | return r; |
| 40 | } |
| 41 | |
| 42 | bool ReadButton() { return PERIPHERAL_BITBAND(GPIOB_PDIR, 2); } |
| 43 | |
| 44 | ::std::atomic<teensy::AcmTty *> global_stdout{nullptr}; |
| 45 | |
| 46 | extern "C" { |
| 47 | |
| 48 | void *__stack_chk_guard = (void *)0x67111971; |
| 49 | void __stack_chk_fail(void) { |
| 50 | while (true) { |
| 51 | GPIOC_PSOR = (1 << 5); |
| 52 | printf("Stack corruption detected\n"); |
| 53 | delay(1000); |
| 54 | GPIOC_PCOR = (1 << 5); |
| 55 | delay(1000); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | int _write(int /*file*/, char *ptr, int len) { |
| 60 | teensy::AcmTty *const tty = global_stdout.load(::std::memory_order_acquire); |
| 61 | if (tty != nullptr) { |
| 62 | return tty->Write(ptr, len); |
| 63 | } |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | void __stack_chk_fail(void); |
| 68 | |
| 69 | extern char *__brkval; |
| 70 | extern uint32_t __bss_ram_start__[]; |
| 71 | extern uint32_t __heap_start__[]; |
| 72 | extern uint32_t __stack_end__[]; |
| 73 | |
| 74 | } // extern "C" |
| 75 | |
| 76 | constexpr int kOutputCounts = 37500; |
| 77 | constexpr int kOutputPrescalerShift = 4; |
| 78 | |
| 79 | void SetOutputWidth(float ms) { |
| 80 | static constexpr float kScale = static_cast<float>( |
| 81 | static_cast<double>(kOutputCounts) / 10.0 /* milliseconds per period */); |
| 82 | const int width = static_cast<int>(ms * kScale + 0.5f); |
| 83 | FTM3->C6V = width - 1; |
| 84 | FTM3->PWMLOAD = FTM_PWMLOAD_LDOK; |
| 85 | } |
| 86 | |
| 87 | } // namespace |
| 88 | |
| 89 | extern "C" int main(void) { |
| 90 | // for background about this startup delay, please see these conversations |
| 91 | // https://forum.pjrc.com/threads/36606-startup-time-(400ms)?p=113980&viewfull=1#post113980 |
| 92 | // https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273 |
| 93 | delay(400); |
| 94 | |
| 95 | // Set all interrupts to the second-lowest priority to start with. |
| 96 | for (int i = 0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_SANE_PRIORITY(i, 0xD); |
| 97 | |
| 98 | // Now set priorities for all the ones we care about. They only have meaning |
| 99 | // relative to each other, which means centralizing them here makes it a lot |
| 100 | // more manageable. |
| 101 | NVIC_SET_SANE_PRIORITY(IRQ_USBOTG, 0x7); |
| 102 | NVIC_SET_SANE_PRIORITY(IRQ_FTM0, 0x3); |
| 103 | |
| 104 | // Set the LED's pin to output mode. |
| 105 | PERIPHERAL_BITBAND(GPIOC_PDDR, 5) = 1; |
| 106 | PORTC_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 107 | |
| 108 | // EI2C_SCL (not end) PTB3 |
| 109 | PORTB_PCR2 = PORT_PCR_MUX(1); |
| 110 | |
| 111 | #if 0 |
| 112 | PERIPHERAL_BITBAND(GPIOA_PDDR, 15) = 1; |
| 113 | PORTA_PCR15 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 114 | #endif |
| 115 | |
Brian Silverman | 45564a8 | 2018-09-02 16:35:22 -0700 | [diff] [blame] | 116 | DMA.CR = M_DMA_EMLM; |
Brian Silverman | d44258e | 2018-05-14 10:01:18 -0700 | [diff] [blame] | 117 | |
| 118 | teensy::UsbDevice usb_device(0, 0x16c0, 0x0490); |
| 119 | usb_device.SetManufacturer("FRC 971 Spartan Robotics"); |
| 120 | usb_device.SetProduct("Pistol Grip Controller debug"); |
| 121 | teensy::AcmTty tty1(&usb_device); |
| 122 | teensy::AcmTty tty2(&usb_device); |
| 123 | global_stdout.store(&tty1, ::std::memory_order_release); |
| 124 | usb_device.Initialize(); |
| 125 | |
| 126 | AdcInitFet12(); |
| 127 | delay(1000); |
| 128 | |
| 129 | #if 0 |
| 130 | GPIOD_PCOR = 1 << 3; |
| 131 | PERIPHERAL_BITBAND(GPIOD_PDDR, 3) = 1; |
| 132 | PORTD_PCR3 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 133 | delay(1000); |
| 134 | GPIOD_PSOR = 1 << 3; |
| 135 | delay(1000); |
| 136 | GPIOD_PCOR = 1 << 3; |
| 137 | delay(1000); |
| 138 | #endif |
| 139 | |
| 140 | delay(1000); |
| 141 | |
| 142 | // Index pin |
| 143 | PORTA_PCR7 = PORT_PCR_MUX(1); |
| 144 | // FTM1_QD_PH{A,B} |
| 145 | PORTB_PCR0 = PORT_PCR_MUX(6); |
| 146 | PORTB_PCR1 = PORT_PCR_MUX(6); |
| 147 | |
| 148 | // FTM3_CH6 for PWM_IN (used as output) |
| 149 | PORTE_PCR11 = PORT_PCR_MUX(6); |
| 150 | |
| 151 | auto *const encoder_ftm = FTM1; |
| 152 | // PWMSYNC doesn't matter because we set SYNCMODE down below. |
| 153 | encoder_ftm->MODE = FTM_MODE_WPDIS; |
| 154 | encoder_ftm->MODE = FTM_MODE_WPDIS | FTM_MODE_FTMEN; |
| 155 | encoder_ftm->SC = |
| 156 | FTM_SC_CLKS(1) /* Use the system clock (not sure it matters) */ | |
| 157 | FTM_SC_PS(0) /* Don't prescale the clock (not sure it matters) */; |
| 158 | |
| 159 | encoder_ftm->MOD = 1023; |
| 160 | |
| 161 | // I think you have to set this to something other than 0 for the quadrature |
| 162 | // encoder mode to actually work? This is "input capture on rising edge only", |
| 163 | // which should be fine. |
| 164 | encoder_ftm->C0SC = FTM_CSC_ELSA; |
| 165 | encoder_ftm->C1SC = FTM_CSC_ELSA; |
| 166 | |
| 167 | encoder_ftm->FILTER = FTM_FILTER_CH0FVAL(0) /* No filter */ | |
| 168 | FTM_FILTER_CH1FVAL(0) /* No filter */; |
| 169 | |
| 170 | // Could set PHAFLTREN and PHBFLTREN here to enable the filters. |
| 171 | encoder_ftm->QDCTRL = FTM_QDCTRL_QUADEN; |
| 172 | |
| 173 | encoder_ftm->SYNCONF = |
| 174 | FTM_SYNCONF_SWWRBUF /* Software trigger flushes MOD */ | |
| 175 | FTM_SYNCONF_SWRSTCNT /* Software trigger resets the count */ | |
| 176 | FTM_SYNCONF_SYNCMODE /* Use the new synchronization mode */; |
| 177 | |
| 178 | encoder_ftm->SYNC = FTM_SYNC_SWSYNC /* Flush everything out right now */; |
| 179 | // Wait for the software synchronization to finish. |
| 180 | while (encoder_ftm->SYNC & FTM_SYNC_SWSYNC) { |
| 181 | } |
| 182 | |
| 183 | auto *const pwm_ftm = FTM3; |
| 184 | // PWMSYNC doesn't matter because we set SYNCMODE down below. |
| 185 | pwm_ftm->MODE = FTM_MODE_WPDIS; |
| 186 | pwm_ftm->MODE = FTM_MODE_WPDIS | FTM_MODE_FTMEN; |
| 187 | pwm_ftm->SC = FTM_SC_CLKS(0) /* Disable counting for now */ | |
| 188 | FTM_SC_PS(kOutputPrescalerShift); |
| 189 | |
| 190 | pwm_ftm->CNTIN = 0; |
| 191 | pwm_ftm->CNT = 0; |
| 192 | pwm_ftm->MOD = kOutputCounts - 1; |
| 193 | |
| 194 | // High-true edge-aligned mode (turns on at start, off at match). |
| 195 | pwm_ftm->C0SC = FTM_CSC_MSB | FTM_CSC_ELSB; |
| 196 | pwm_ftm->C1SC = FTM_CSC_MSB | FTM_CSC_ELSB; |
| 197 | pwm_ftm->C2SC = FTM_CSC_MSB | FTM_CSC_ELSB; |
| 198 | pwm_ftm->C3SC = FTM_CSC_MSB | FTM_CSC_ELSB; |
| 199 | pwm_ftm->C4SC = FTM_CSC_MSB | FTM_CSC_ELSB; |
| 200 | pwm_ftm->C5SC = FTM_CSC_MSB | FTM_CSC_ELSB; |
| 201 | pwm_ftm->C6SC = FTM_CSC_MSB | FTM_CSC_ELSB; |
| 202 | pwm_ftm->C7SC = FTM_CSC_MSB | FTM_CSC_ELSB; |
| 203 | |
| 204 | pwm_ftm->COMBINE = FTM_COMBINE_SYNCEN3 /* Synchronize updates usefully */ | |
| 205 | FTM_COMBINE_SYNCEN2 /* Synchronize updates usefully */ | |
| 206 | FTM_COMBINE_SYNCEN1 /* Synchronize updates usefully */ | |
| 207 | FTM_COMBINE_SYNCEN0 /* Synchronize updates usefully */; |
| 208 | |
| 209 | // Initialize all the channels to 0. |
| 210 | pwm_ftm->OUTINIT = 0; |
| 211 | |
| 212 | // All of the channels are active high. |
| 213 | pwm_ftm->POL = 0; |
| 214 | |
| 215 | pwm_ftm->SYNCONF = |
| 216 | FTM_SYNCONF_HWWRBUF /* Hardware trigger flushes switching points */ | |
| 217 | FTM_SYNCONF_SWWRBUF /* Software trigger flushes switching points */ | |
| 218 | FTM_SYNCONF_SWRSTCNT /* Software trigger resets the count */ | |
| 219 | FTM_SYNCONF_SYNCMODE /* Use the new synchronization mode */; |
| 220 | |
| 221 | // Don't want any intermediate loading points. |
| 222 | pwm_ftm->PWMLOAD = 0; |
| 223 | |
| 224 | // This has to happen after messing with SYNCONF, and should happen after |
| 225 | // messing with various other things so the values can get flushed out of the |
| 226 | // buffers. |
| 227 | pwm_ftm->SYNC = FTM_SYNC_SWSYNC /* Flush everything out right now */ | |
| 228 | FTM_SYNC_CNTMAX /* Load new values at the end of the cycle */; |
| 229 | // Wait for the software synchronization to finish. |
| 230 | while (pwm_ftm->SYNC & FTM_SYNC_SWSYNC) { |
| 231 | } |
| 232 | |
| 233 | // Don't let any memory accesses sneak past here, because we actually |
| 234 | // need everything to be starting up. |
| 235 | __asm__("" :: : "memory"); |
| 236 | |
| 237 | // Give everything a chance to get going. |
| 238 | delay(100); |
| 239 | |
| 240 | printf("Ram start: %p\n", __bss_ram_start__); |
| 241 | printf("Heap start: %p\n", __heap_start__); |
| 242 | printf("Heap end: %p\n", __brkval); |
| 243 | printf("Stack start: %p\n", __stack_end__); |
| 244 | |
| 245 | encoder_ftm->MODE &= ~FTM_MODE_WPDIS; |
| 246 | pwm_ftm->SC = FTM_SC_TOIE /* Interrupt on overflow */ | |
| 247 | FTM_SC_CLKS(1) /* Use the system clock */ | |
| 248 | FTM_SC_PS(kOutputPrescalerShift); |
| 249 | pwm_ftm->MODE &= ~FTM_MODE_WPDIS; |
| 250 | |
| 251 | GPIOC_PSOR = 1 << 5; |
| 252 | |
| 253 | uint16_t old_encoder = FTM1->CNT; |
| 254 | uint32_t start_time = micros(); |
| 255 | while (true) { |
| 256 | const uint32_t end_time = start_time + UINT32_C(500); |
| 257 | while (micros() < end_time) { |
| 258 | } |
| 259 | start_time = end_time; |
| 260 | |
| 261 | Fet12AdcReadings adc_readings; |
| 262 | { |
| 263 | DisableInterrupts disable_interrupts; |
| 264 | adc_readings = AdcReadFet12(disable_interrupts); |
| 265 | } |
| 266 | const float pedal_position = ::std::min( |
| 267 | 1.0f, |
| 268 | ::std::max(0.0f, static_cast<float>(adc_readings.throttle - 1200) / |
| 269 | static_cast<float>(3120 - 1200))); |
| 270 | |
| 271 | const uint16_t new_encoder = FTM1->CNT; |
| 272 | // Full speed is ~418. |
| 273 | // Low gear is positive. |
| 274 | int16_t encoder_delta = |
| 275 | static_cast<int16_t>(new_encoder) - static_cast<int16_t>(old_encoder); |
| 276 | if (encoder_delta < -512) { |
| 277 | encoder_delta += 1024; |
| 278 | } |
| 279 | if (encoder_delta > 512) { |
| 280 | encoder_delta -= 1024; |
| 281 | } |
| 282 | old_encoder = new_encoder; |
| 283 | |
| 284 | // Positive -> low gear |
| 285 | float speed = ::std::min( |
| 286 | 1.0f, ::std::max(-1.0f, static_cast<float>(encoder_delta) / 418.0f)); |
| 287 | |
| 288 | float out_command; |
| 289 | if (ReadButton()) { |
| 290 | out_command = pedal_position; |
| 291 | } else { |
| 292 | out_command = -pedal_position; |
| 293 | } |
| 294 | |
| 295 | static constexpr float kMaxCurrentFull = 0.155f; |
| 296 | static constexpr float kMaxCurrentStopped = 0.29f; |
| 297 | float abs_speed; |
| 298 | if (speed > 0.0f) { |
| 299 | abs_speed = speed; |
| 300 | } else { |
| 301 | abs_speed = -speed; |
| 302 | } |
| 303 | float max_current = |
| 304 | abs_speed * (kMaxCurrentFull - kMaxCurrentStopped) + kMaxCurrentStopped; |
| 305 | if (abs_speed < 0.06f) { |
| 306 | max_current = 0.27f; |
| 307 | } |
| 308 | if (speed > 0.0f) { |
| 309 | out_command = |
| 310 | ::std::min(speed + max_current, |
| 311 | ::std::max(speed - 2.0f * max_current, out_command)); |
| 312 | } else { |
| 313 | out_command = ::std::min(speed + 2.0f * max_current, |
| 314 | ::std::max(speed - max_current, out_command)); |
| 315 | } |
| 316 | |
| 317 | static float slew_limited_command = 0.0f; |
| 318 | constexpr float kMaxChangePerCycle = 1.0f / 150.0f; |
| 319 | |
| 320 | if (out_command < slew_limited_command - kMaxChangePerCycle) { |
| 321 | out_command = slew_limited_command - kMaxChangePerCycle; |
| 322 | } else if (out_command > slew_limited_command + kMaxChangePerCycle) { |
| 323 | out_command = slew_limited_command + kMaxChangePerCycle; |
| 324 | } |
| 325 | |
| 326 | slew_limited_command = out_command; |
| 327 | |
| 328 | const float pwm_out = 1.5f + -slew_limited_command / 2.0f; |
| 329 | SetOutputWidth(pwm_out); |
| 330 | |
| 331 | static int i = 0; |
| 332 | if (i == 100) { |
| 333 | i = 0; |
| 334 | printf("enc %" PRIu32 " throttle %" PRIu16 " %d out %d %d %d\n", |
| 335 | FTM1->CNT, adc_readings.throttle, ReadButton(), |
| 336 | (int)(pwm_out * 1000), (int)encoder_delta, |
| 337 | (int)(abs_speed * 1000)); |
| 338 | } |
| 339 | ++i; |
| 340 | } |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | } // namespace motors |
| 346 | } // namespace frc971 |