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