blob: f8b38f0112d89d31509e33de39d14c639fa76054 [file] [log] [blame]
Brian Silverman54dd2fe2018-03-16 23:44:31 -07001// This file has the main for the Teensy on the simple receiver board.
2
3#include <inttypes.h>
4#include <stdio.h>
5#include <atomic>
6#include <cmath>
7
Austin Schuh4fae0fc2018-03-27 23:51:42 -07008#include "frc971/control_loops/drivetrain/polydrivetrain.h"
Brian Silverman54dd2fe2018-03-16 23:44:31 -07009#include "motors/core/kinetis.h"
Brian Silvermana3a172b2018-03-24 03:53:32 -040010#include "motors/core/time.h"
Brian Silverman54dd2fe2018-03-16 23:44:31 -070011#include "motors/peripheral/adc.h"
12#include "motors/peripheral/can.h"
Brian Silvermana3a172b2018-03-24 03:53:32 -040013#include "motors/peripheral/configuration.h"
Austin Schuh4fae0fc2018-03-27 23:51:42 -070014#include "motors/seems_reasonable/drivetrain_dog_motor_plant.h"
15#include "motors/seems_reasonable/polydrivetrain_dog_motor_plant.h"
16#include "motors/seems_reasonable/spring.h"
Brian Silverman54dd2fe2018-03-16 23:44:31 -070017#include "motors/usb/cdc.h"
Brian Silvermana3a172b2018-03-24 03:53:32 -040018#include "motors/usb/usb.h"
Brian Silverman54dd2fe2018-03-16 23:44:31 -070019#include "motors/util.h"
20
21namespace frc971 {
22namespace motors {
23namespace {
24
Austin Schuhbcce26a2018-03-26 23:41:24 -070025using ::frc971::control_loops::drivetrain::DrivetrainConfig;
26using ::frc971::control_loops::drivetrain::PolyDrivetrain;
27using ::frc971::constants::ShifterHallEffect;
28using ::frc971::control_loops::DrivetrainQueue_Goal;
29using ::frc971::control_loops::DrivetrainQueue_Output;
Austin Schuh4fae0fc2018-03-27 23:51:42 -070030using ::motors::seems_reasonable::Spring;
Austin Schuhbcce26a2018-03-26 23:41:24 -070031
Brian Silverman9ed2cf12018-05-12 13:06:38 -070032struct SimpleAdcReadings {
33 uint16_t sin, cos;
34};
35
36void AdcInitSimple() {
37 AdcInitCommon();
38
39 // ENC_SIN ADC0_SE23
40 // ENC_COS ADC1_SE23
41}
42
43SimpleAdcReadings AdcReadSimple(const DisableInterrupts &) {
44 SimpleAdcReadings r;
45
46 ADC0_SC1A = 23;
47 ADC1_SC1A = 23;
48 while (!(ADC0_SC1A & ADC_SC1_COCO)) {
49 }
50 while (!(ADC1_SC1A & ADC_SC1_COCO)) {
51 }
52 r.sin = ADC0_RA;
53 r.cos = ADC1_RA;
54
55 return r;
56}
57
Austin Schuhbcce26a2018-03-26 23:41:24 -070058const ShifterHallEffect kThreeStateDriveShifter{0.0, 0.0, 0.25, 0.75};
59
60const DrivetrainConfig<float> &GetDrivetrainConfig() {
61 static DrivetrainConfig<float> kDrivetrainConfig{
62 ::frc971::control_loops::drivetrain::ShifterType::NO_SHIFTER,
63 ::frc971::control_loops::drivetrain::LoopType::OPEN_LOOP,
64 ::frc971::control_loops::drivetrain::GyroType::SPARTAN_GYRO,
65 ::frc971::control_loops::drivetrain::IMUType::IMU_X,
66
67 ::motors::seems_reasonable::MakeDrivetrainLoop,
68 ::motors::seems_reasonable::MakeVelocityDrivetrainLoop,
69 ::std::function<StateFeedbackLoop<7, 2, 4, float>()>(),
70
71 ::motors::seems_reasonable::kDt, ::motors::seems_reasonable::kRobotRadius,
72 ::motors::seems_reasonable::kWheelRadius, ::motors::seems_reasonable::kV,
73
74 ::motors::seems_reasonable::kHighGearRatio,
75 ::motors::seems_reasonable::kLowGearRatio, kThreeStateDriveShifter,
76 kThreeStateDriveShifter, true /* default_high_gear */,
77 0 /* down_offset if using constants use
78 constants::GetValues().down_error */, 0.8 /* wheel_non_linearity */,
79 1.2 /* quickturn_wheel_multiplier */, 1.5 /* wheel_multiplier */,
80 };
81
82 return kDrivetrainConfig;
83};
84
85
Brian Silverman54dd2fe2018-03-16 23:44:31 -070086::std::atomic<teensy::AcmTty *> global_stdout{nullptr};
87
Austin Schuhbcce26a2018-03-26 23:41:24 -070088::std::atomic<PolyDrivetrain<float> *> global_polydrivetrain{nullptr};
Austin Schuh4fae0fc2018-03-27 23:51:42 -070089::std::atomic<Spring *> global_spring{nullptr};
Austin Schuhbcce26a2018-03-26 23:41:24 -070090
Brian Silvermana3a172b2018-03-24 03:53:32 -040091// Last width we received on each channel.
Brian Silverman7f5f1442018-04-06 13:00:50 -040092uint16_t pwm_input_widths[6];
Brian Silvermana3a172b2018-03-24 03:53:32 -040093// When we received a pulse on each channel in milliseconds.
Brian Silverman7f5f1442018-04-06 13:00:50 -040094uint32_t pwm_input_times[6];
Brian Silvermana3a172b2018-03-24 03:53:32 -040095
Austin Schuhbcce26a2018-03-26 23:41:24 -070096constexpr int kChannelTimeout = 100;
97
98bool lost_channel(int channel) {
99 DisableInterrupts disable_interrupts;
100 if (time_after(millis(),
101 time_add(pwm_input_times[channel], kChannelTimeout))) {
102 return true;
103 }
104 return false;
105}
106
Brian Silvermana3a172b2018-03-24 03:53:32 -0400107// Returns the most recently captured value for the specified input channel
108// scaled from -1 to 1, or 0 if it was captured over 100ms ago.
109float convert_input_width(int channel) {
110 uint16_t width;
111 {
112 DisableInterrupts disable_interrupts;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700113 if (time_after(millis(),
114 time_add(pwm_input_times[channel], kChannelTimeout))) {
Brian Silvermana3a172b2018-03-24 03:53:32 -0400115 return 0;
116 }
117
118 width = pwm_input_widths[channel];
119 }
120
121 // Values measured with a channel mapped to a button.
122 static constexpr uint16_t kMinWidth = 4133;
123 static constexpr uint16_t kMaxWidth = 7177;
124 if (width < kMinWidth) {
125 width = kMinWidth;
126 } else if (width > kMaxWidth) {
127 width = kMaxWidth;
128 }
129 return (static_cast<float>(2 * (width - kMinWidth)) /
130 static_cast<float>(kMaxWidth - kMinWidth)) -
131 1.0f;
132}
133
134// Sends a SET_RPM command to the specified VESC.
135// Note that sending 6 VESC commands every 1ms doesn't quite fit in the CAN
136// bandwidth.
137void vesc_set_rpm(int vesc_id, float rpm) {
138 const int32_t rpm_int = rpm;
139 uint32_t id = CAN_EFF_FLAG;
140 id |= vesc_id;
141 id |= (0x03 /* SET_RPM */) << 8;
142 uint8_t data[4] = {
143 static_cast<uint8_t>((rpm_int >> 24) & 0xFF),
144 static_cast<uint8_t>((rpm_int >> 16) & 0xFF),
145 static_cast<uint8_t>((rpm_int >> 8) & 0xFF),
146 static_cast<uint8_t>((rpm_int >> 0) & 0xFF),
147 };
148 can_send(id, data, sizeof(data), 2 + vesc_id);
149}
150
151// Sends a SET_CURRENT command to the specified VESC.
152// current is in amps.
153// Note that sending 6 VESC commands every 1ms doesn't quite fit in the CAN
154// bandwidth.
155void vesc_set_current(int vesc_id, float current) {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700156 constexpr float kMaxCurrent = 80.0f;
157 const int32_t current_int =
158 ::std::max(-kMaxCurrent, ::std::min(kMaxCurrent, current)) * 1000.0f;
Brian Silvermana3a172b2018-03-24 03:53:32 -0400159 uint32_t id = CAN_EFF_FLAG;
160 id |= vesc_id;
161 id |= (0x01 /* SET_CURRENT */) << 8;
162 uint8_t data[4] = {
163 static_cast<uint8_t>((current_int >> 24) & 0xFF),
164 static_cast<uint8_t>((current_int >> 16) & 0xFF),
165 static_cast<uint8_t>((current_int >> 8) & 0xFF),
166 static_cast<uint8_t>((current_int >> 0) & 0xFF),
167 };
168 can_send(id, data, sizeof(data), 2 + vesc_id);
169}
170
Brian Silverman4d1e5272018-03-26 03:18:42 -0400171// Sends a SET_DUTY command to the specified VESC.
172// duty is from -1 to 1.
173// Note that sending 6 VESC commands every 1ms doesn't quite fit in the CAN
174// bandwidth.
175void vesc_set_duty(int vesc_id, float duty) {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700176 constexpr int32_t kMaxDuty = 99999;
177 const int32_t duty_int = ::std::max(
178 -kMaxDuty, ::std::min(kMaxDuty, static_cast<int32_t>(duty * 100000.0f)));
Brian Silverman4d1e5272018-03-26 03:18:42 -0400179 uint32_t id = CAN_EFF_FLAG;
180 id |= vesc_id;
181 id |= (0x00 /* SET_DUTY */) << 8;
182 uint8_t data[4] = {
183 static_cast<uint8_t>((duty_int >> 24) & 0xFF),
184 static_cast<uint8_t>((duty_int >> 16) & 0xFF),
185 static_cast<uint8_t>((duty_int >> 8) & 0xFF),
186 static_cast<uint8_t>((duty_int >> 0) & 0xFF),
187 };
188 can_send(id, data, sizeof(data), 2 + vesc_id);
189}
190
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700191void DoVescTest() {
Brian Silverman54dd2fe2018-03-16 23:44:31 -0700192 uint32_t time = micros();
193 while (true) {
194 for (int i = 0; i < 6; ++i) {
195 const uint32_t end = time_add(time, 500000);
196 while (true) {
197 const bool done = time_after(micros(), end);
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700198 float current;
Brian Silverman54dd2fe2018-03-16 23:44:31 -0700199 if (done) {
200 current = -6;
201 } else {
202 current = 6;
203 }
Brian Silvermana3a172b2018-03-24 03:53:32 -0400204 vesc_set_current(i, current);
Brian Silverman54dd2fe2018-03-16 23:44:31 -0700205 if (done) {
206 break;
207 }
208 delay(5);
209 }
210 time = end;
211 }
212 }
213}
214
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700215void DoReceiverTest2() {
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700216 static constexpr float kMaxRpm = 10000.0f;
217 while (true) {
Brian Silvermana3a172b2018-03-24 03:53:32 -0400218 const bool flip = convert_input_width(2) > 0;
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700219
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700220 {
Brian Silvermana3a172b2018-03-24 03:53:32 -0400221 const float value = convert_input_width(0);
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700222
223 {
Brian Silvermana3a172b2018-03-24 03:53:32 -0400224 float rpm = ::std::min(0.0f, value) * kMaxRpm;
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700225 if (flip) {
226 rpm *= -1.0f;
227 }
Brian Silvermana3a172b2018-03-24 03:53:32 -0400228 vesc_set_rpm(0, rpm);
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700229 }
230
231 {
Brian Silvermana3a172b2018-03-24 03:53:32 -0400232 float rpm = ::std::max(0.0f, value) * kMaxRpm;
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700233 if (flip) {
234 rpm *= -1.0f;
235 }
Brian Silvermana3a172b2018-03-24 03:53:32 -0400236 vesc_set_rpm(1, rpm);
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700237 }
238 }
239
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700240 {
Brian Silvermana3a172b2018-03-24 03:53:32 -0400241 const float value = convert_input_width(1);
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700242
243 {
Brian Silvermana3a172b2018-03-24 03:53:32 -0400244 float rpm = ::std::min(0.0f, value) * kMaxRpm;
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700245 if (flip) {
246 rpm *= -1.0f;
247 }
Brian Silvermana3a172b2018-03-24 03:53:32 -0400248 vesc_set_rpm(2, rpm);
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700249 }
250
251 {
Brian Silvermana3a172b2018-03-24 03:53:32 -0400252 float rpm = ::std::max(0.0f, value) * kMaxRpm;
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700253 if (flip) {
254 rpm *= -1.0f;
255 }
Brian Silvermana3a172b2018-03-24 03:53:32 -0400256 vesc_set_rpm(3, rpm);
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700257 }
258 }
259
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700260 {
Brian Silvermana3a172b2018-03-24 03:53:32 -0400261 const float value = convert_input_width(4);
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700262
263 {
Brian Silvermana3a172b2018-03-24 03:53:32 -0400264 float rpm = ::std::min(0.0f, value) * kMaxRpm;
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700265 if (flip) {
266 rpm *= -1.0f;
267 }
Brian Silvermana3a172b2018-03-24 03:53:32 -0400268 vesc_set_rpm(4, rpm);
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700269 }
270
271 {
Brian Silvermana3a172b2018-03-24 03:53:32 -0400272 float rpm = ::std::max(0.0f, value) * kMaxRpm;
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700273 if (flip) {
274 rpm *= -1.0f;
275 }
Brian Silvermana3a172b2018-03-24 03:53:32 -0400276 vesc_set_rpm(5, rpm);
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700277 }
278 }
Brian Silvermana3a172b2018-03-24 03:53:32 -0400279 // Give the CAN frames a chance to go out.
280 delay(5);
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700281 }
282}
283
284void SetupPwmFtm(BigFTM *ftm) {
285 ftm->MODE = FTM_MODE_WPDIS;
286 ftm->MODE = FTM_MODE_WPDIS | FTM_MODE_FTMEN;
287 ftm->SC = FTM_SC_CLKS(0) /* Disable counting for now */;
288
289 // Can't change MOD according to the reference manual ("The Dual Edge Capture
290 // mode must be used with ... the FTM counter in Free running counter.").
291 ftm->MOD = 0xFFFF;
292
293 // Capturing rising edge.
294 ftm->C0SC = FTM_CSC_MSA | FTM_CSC_ELSA;
295 // Capturing falling edge.
Brian Silvermana3a172b2018-03-24 03:53:32 -0400296 ftm->C1SC = FTM_CSC_CHIE | FTM_CSC_MSA | FTM_CSC_ELSB;
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700297
298 // Capturing rising edge.
299 ftm->C2SC = FTM_CSC_MSA | FTM_CSC_ELSA;
300 // Capturing falling edge.
Brian Silvermana3a172b2018-03-24 03:53:32 -0400301 ftm->C3SC = FTM_CSC_CHIE | FTM_CSC_MSA | FTM_CSC_ELSB;
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700302
303 // Capturing rising edge.
304 ftm->C4SC = FTM_CSC_MSA | FTM_CSC_ELSA;
305 // Capturing falling edge.
Brian Silvermana3a172b2018-03-24 03:53:32 -0400306 ftm->C5SC = FTM_CSC_CHIE | FTM_CSC_MSA | FTM_CSC_ELSB;
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700307
308 // Capturing rising edge.
309 ftm->C6SC = FTM_CSC_MSA | FTM_CSC_ELSA;
310 // Capturing falling edge.
Brian Silvermana3a172b2018-03-24 03:53:32 -0400311 ftm->C7SC = FTM_CSC_CHIE | FTM_CSC_MSA | FTM_CSC_ELSB;
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700312
Brian Silvermana3a172b2018-03-24 03:53:32 -0400313 (void)ftm->STATUS;
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700314 ftm->STATUS = 0x00;
315
316 ftm->COMBINE = FTM_COMBINE_DECAP3 | FTM_COMBINE_DECAPEN3 |
317 FTM_COMBINE_DECAP2 | FTM_COMBINE_DECAPEN2 |
318 FTM_COMBINE_DECAP1 | FTM_COMBINE_DECAPEN1 |
319 FTM_COMBINE_DECAP0 | FTM_COMBINE_DECAPEN0;
320
321 // 34.95ms max period before it starts wrapping and being weird.
322 ftm->SC = FTM_SC_CLKS(1) /* Use the system clock */ |
323 FTM_SC_PS(4) /* Prescaler=32 */;
324
325 ftm->MODE &= ~FTM_MODE_WPDIS;
326}
327
Brian Silverman2de95d62018-03-31 12:32:24 -0700328struct AccelerometerResult {
329 uint16_t result;
330 bool success;
331};
332
333// Does a transfer on the accelerometer. Returns the resulting frame, or a
334// failure if it takes until after end_micros.
335AccelerometerResult AccelerometerTransfer(uint16_t data, uint32_t end_micros) {
336 SPI0_SR = SPI_SR_RFDF;
337 SPI0_PUSHR = SPI_PUSHR_PCS(1) | data;
338
339 while (!(SPI0_SR & SPI_SR_RFDF)) {
340 if (time_after(micros(), end_micros)) {
341 return {0, false};
342 }
343 }
344 const uint32_t popr = SPI0_POPR;
345 SPI0_SR = SPI_SR_RFDF;
346 return {static_cast<uint16_t>(popr & 0xFFFF), true};
347}
348
349constexpr uint32_t kAccelerometerTimeout = 500;
350
351bool AccelerometerWrite(uint8_t address, uint8_t data, uint32_t end_micros) {
352 const AccelerometerResult r = AccelerometerTransfer(
353 (static_cast<uint16_t>(address) << 8) | static_cast<uint16_t>(data),
354 end_micros);
355 return r.success;
356}
357
358AccelerometerResult AccelerometerRead(uint8_t address, uint32_t end_micros) {
359 AccelerometerResult r = AccelerometerTransfer(
360 (static_cast<uint16_t>(address) << 8) | UINT16_C(0x8000), end_micros);
361 r.result = r.result & UINT16_C(0xFF);
362 return r;
363}
364
365bool accelerometer_inited = false;
366
367void AccelerometerInit() {
368 accelerometer_inited = false;
369 const uint32_t end_micros = time_add(micros(), kAccelerometerTimeout);
370 {
371 const auto who_am_i = AccelerometerRead(0xF, end_micros);
372 if (!who_am_i.success) {
373 return;
374 }
375 if (who_am_i.result != 0x32) {
376 return;
377 }
378 }
379 if (!AccelerometerWrite(
380 0x20, (1 << 5) /* Normal mode */ | (1 << 3) /* 100 Hz */ |
381 (1 << 2) /* Z enabled */ | (1 << 1) /* Y enabled */ |
382 (1 << 0) /* X enabled */,
383 end_micros)) {
384 }
385 // If want to read LSB, need to enable BDU to avoid splitting reads.
386 if (!AccelerometerWrite(0x23, (0 << 6) /* Data LSB at lower address */ |
387 (3 << 4) /* 400g full scale */ |
388 (0 << 0) /* 4-wire interface */,
389 end_micros)) {
390 }
391 accelerometer_inited = true;
392}
393
394float AccelerometerConvert(uint16_t value) {
395 return static_cast<float>(400.0 / 65536.0) * static_cast<float>(value);
396}
397
398// Returns the total acceleration (in any direction) or 0 if there's an error.
399float ReadAccelerometer() {
400 if (!accelerometer_inited) {
401 AccelerometerInit();
402 return 0;
403 }
404
405 const uint32_t end_micros = time_add(micros(), kAccelerometerTimeout);
406 const auto x = AccelerometerRead(0x29, end_micros);
407 const auto y = AccelerometerRead(0x2B, end_micros);
408 const auto z = AccelerometerRead(0x2D, end_micros);
409 if (!x.success || !y.success || !z.success) {
410 accelerometer_inited = false;
411 return 0;
412 }
413
414 const float x_g = AccelerometerConvert(x.result);
415 const float y_g = AccelerometerConvert(y.result);
416 const float z_g = AccelerometerConvert(z.result);
417 return ::std::sqrt(x_g * x_g + y_g * y_g + z_g * z_g);
418}
419
Brian Silvermana3a172b2018-03-24 03:53:32 -0400420extern "C" void ftm0_isr() {
421 while (true) {
422 const uint32_t status = FTM0->STATUS;
423 if (status == 0) {
424 return;
425 }
426
427 if (status & (1 << 1)) {
428 const uint32_t start = FTM0->C0V;
429 const uint32_t end = FTM0->C1V;
430 pwm_input_widths[0] = (end - start) & 0xFFFF;
431 pwm_input_times[0] = millis();
432 }
433 if (status & (1 << 7)) {
434 const uint32_t start = FTM0->C6V;
435 const uint32_t end = FTM0->C7V;
436 pwm_input_widths[1] = (end - start) & 0xFFFF;
437 pwm_input_times[1] = millis();
438 }
439 if (status & (1 << 5)) {
440 const uint32_t start = FTM0->C4V;
441 const uint32_t end = FTM0->C5V;
442 pwm_input_widths[2] = (end - start) & 0xFFFF;
443 pwm_input_times[2] = millis();
444 }
445 if (status & (1 << 3)) {
446 const uint32_t start = FTM0->C2V;
447 const uint32_t end = FTM0->C3V;
448 pwm_input_widths[4] = (end - start) & 0xFFFF;
449 pwm_input_times[4] = millis();
450 }
451
452 FTM0->STATUS = 0;
453 }
454}
455
456extern "C" void ftm3_isr() {
457 while (true) {
458 const uint32_t status = FTM3->STATUS;
459 if (status == 0) {
460 return;
461 }
462
463 if (status & (1 << 3)) {
464 const uint32_t start = FTM3->C2V;
465 const uint32_t end = FTM3->C3V;
466 pwm_input_widths[3] = (end - start) & 0xFFFF;
467 pwm_input_times[3] = millis();
468 }
Brian Silverman7f5f1442018-04-06 13:00:50 -0400469 if (status & (1 << 7)) {
470 const uint32_t start = FTM3->C6V;
471 const uint32_t end = FTM3->C7V;
472 pwm_input_widths[5] = (end - start) & 0xFFFF;
473 pwm_input_times[5] = millis();
474 }
Brian Silvermana3a172b2018-03-24 03:53:32 -0400475
476 FTM3->STATUS = 0;
477 }
478}
479
480float ConvertEncoderChannel(uint16_t reading) {
481 // Theoretical values based on the datasheet are 931 and 2917.
482 // With these values, the magnitude ranges from 0.99-1.03, which works fine
483 // (the encoder's output appears to get less accurate in one quadrant for some
484 // reason, hence the variation).
485 static constexpr uint16_t kMin = 802, kMax = 3088;
486 if (reading < kMin) {
487 reading = kMin;
488 } else if (reading > kMax) {
489 reading = kMax;
490 }
491 return (static_cast<float>(2 * (reading - kMin)) /
492 static_cast<float>(kMax - kMin)) -
493 1.0f;
494}
495
496struct EncoderReading {
Brian Silvermana96c1a42018-05-12 12:11:31 -0700497 EncoderReading(const SimpleAdcReadings &adc_readings) {
Brian Silvermana3a172b2018-03-24 03:53:32 -0400498 const float sin = ConvertEncoderChannel(adc_readings.sin);
499 const float cos = ConvertEncoderChannel(adc_readings.cos);
500
Austin Schuhbcce26a2018-03-26 23:41:24 -0700501 const float magnitude = hypot(sin, cos);
Brian Silvermana3a172b2018-03-24 03:53:32 -0400502 const float magnitude_error = ::std::abs(magnitude - 1.0f);
Austin Schuh4fae0fc2018-03-27 23:51:42 -0700503 valid = magnitude_error < 0.30f;
Brian Silvermana3a172b2018-03-24 03:53:32 -0400504
505 angle = ::std::atan2(sin, cos);
506 }
507
508 // Angle in radians, in [-pi, pi].
509 float angle;
510
511 bool valid;
512};
513
514extern "C" void pit3_isr() {
515 PIT_TFLG3 = 1;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700516 PolyDrivetrain<float> *polydrivetrain =
517 global_polydrivetrain.load(::std::memory_order_acquire);
Austin Schuh4fae0fc2018-03-27 23:51:42 -0700518 Spring *spring = global_spring.load(::std::memory_order_acquire);
Brian Silvermana3a172b2018-03-24 03:53:32 -0400519
Brian Silvermana96c1a42018-05-12 12:11:31 -0700520 SimpleAdcReadings adc_readings;
Brian Silvermana3a172b2018-03-24 03:53:32 -0400521 {
522 DisableInterrupts disable_interrupts;
Brian Silvermana96c1a42018-05-12 12:11:31 -0700523 adc_readings = AdcReadSimple(disable_interrupts);
Brian Silvermana3a172b2018-03-24 03:53:32 -0400524 }
525
526 EncoderReading encoder(adc_readings);
Austin Schuh4fae0fc2018-03-27 23:51:42 -0700527 static float last_good_encoder = 0.0f;
528 static int invalid_encoder_count = 0;
529 if (encoder.valid) {
530 last_good_encoder = encoder.angle;
531 invalid_encoder_count = 0;
532 } else {
533 ++invalid_encoder_count;
534 }
Brian Silvermana3a172b2018-03-24 03:53:32 -0400535
Austin Schuhe666dc62018-08-08 21:09:12 -0700536 const bool lost_spring_channel = lost_channel(2) || lost_channel(3) ||
537 lost_channel(4) || lost_channel(5) ||
538 (convert_input_width(4) < 0.5f);
539
540 const bool lost_drive_channel = lost_channel(0) || lost_channel(1) ||
541 (::std::abs(convert_input_width(4)) < 0.5f);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700542
Austin Schuh4fae0fc2018-03-27 23:51:42 -0700543 if (polydrivetrain != nullptr && spring != nullptr) {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700544 DrivetrainQueue_Goal goal;
545 goal.control_loop_driving = false;
Austin Schuhe666dc62018-08-08 21:09:12 -0700546 if (lost_drive_channel) {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700547 goal.throttle = 0.0f;
548 goal.wheel = 0.0f;
549 } else {
550 goal.throttle = convert_input_width(1);
Austin Schuh4fae0fc2018-03-27 23:51:42 -0700551 goal.wheel = -convert_input_width(0);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700552 }
Austin Schuhbcce26a2018-03-26 23:41:24 -0700553 goal.quickturn = ::std::abs(polydrivetrain->velocity()) < 0.25f;
554
555 DrivetrainQueue_Output output;
556
557 polydrivetrain->SetGoal(goal);
558 polydrivetrain->Update();
559 polydrivetrain->SetOutput(&output);
560
561 vesc_set_duty(0, -output.left_voltage / 12.0f);
562 vesc_set_duty(1, -output.left_voltage / 12.0f);
563
564 vesc_set_duty(2, output.right_voltage / 12.0f);
565 vesc_set_duty(3, output.right_voltage / 12.0f);
566
Austin Schuhe666dc62018-08-08 21:09:12 -0700567 const bool prime = convert_input_width(2) > 0.1f;
568 const bool fire = convert_input_width(3) > 0.1f;
569 const bool force_move =
570 (convert_input_width(5) > 0.1f) && !lost_spring_channel;
Austin Schuh4fae0fc2018-03-27 23:51:42 -0700571
Austin Schuhe666dc62018-08-08 21:09:12 -0700572 bool unload = lost_spring_channel;
Austin Schuh4fae0fc2018-03-27 23:51:42 -0700573 static bool was_lost = true;
Austin Schuhe666dc62018-08-08 21:09:12 -0700574 bool force_reset = !lost_spring_channel && was_lost;
575 was_lost = lost_spring_channel;
Austin Schuh4fae0fc2018-03-27 23:51:42 -0700576
Austin Schuhe666dc62018-08-08 21:09:12 -0700577 spring->Iterate(unload, prime, fire, force_reset, force_move,
578 invalid_encoder_count <= 2, last_good_encoder);
Austin Schuh4fae0fc2018-03-27 23:51:42 -0700579
580 float spring_output = spring->output();
581
582 vesc_set_duty(4, -spring_output);
583 vesc_set_duty(5, spring_output);
584
Brian Silverman2de95d62018-03-31 12:32:24 -0700585 const float accelerometer = ReadAccelerometer();
586 (void)accelerometer;
587
Austin Schuh4fae0fc2018-03-27 23:51:42 -0700588 /*
589 // Massive debug. Turn on for useful bits.
Brian Silverman2de95d62018-03-31 12:32:24 -0700590 printf("acc %d/1000\n", (int)(accelerometer / 1000));
Austin Schuh4fae0fc2018-03-27 23:51:42 -0700591 if (!encoder.valid) {
592 printf("Stuck encoder: ADC %" PRIu16 " %" PRIu16
593 " enc %d/1000 %s mag %d\n",
594 adc_readings.sin, adc_readings.cos, (int)(encoder.angle * 1000),
595 encoder.valid ? "T" : "f",
596 (int)(hypot(ConvertEncoderChannel(adc_readings.sin),
597 ConvertEncoderChannel(adc_readings.cos)) *
598 1000));
599 }
Austin Schuhbcce26a2018-03-26 23:41:24 -0700600 static int i = 0;
601 ++i;
602 if (i > 20) {
603 i = 0;
Austin Schuhe666dc62018-08-08 21:09:12 -0700604 if (lost_spring_channel || lost_drive_channel) {
605 printf("200Hz loop, disabled %d %d %d %d %d %d\n",
Austin Schuhbcce26a2018-03-26 23:41:24 -0700606 (int)(convert_input_width(0) * 1000),
607 (int)(convert_input_width(1) * 1000),
608 (int)(convert_input_width(2) * 1000),
609 (int)(convert_input_width(3) * 1000),
Austin Schuhe666dc62018-08-08 21:09:12 -0700610 (int)(convert_input_width(4) * 1000),
611 (int)(convert_input_width(5) * 1000));
Austin Schuhbcce26a2018-03-26 23:41:24 -0700612 } else {
613 printf(
Austin Schuhe666dc62018-08-08 21:09:12 -0700614 "TODO(Austin): 200Hz loop %d %d %d %d %d %d, lr, %d, %d velocity %d
615 "
Austin Schuh4fae0fc2018-03-27 23:51:42 -0700616 " state: %d, near %d angle %d goal %d to: %d ADC %" PRIu16
617 " %" PRIu16 " enc %d/1000 %s from %d\n",
Austin Schuhbcce26a2018-03-26 23:41:24 -0700618 (int)(convert_input_width(0) * 1000),
619 (int)(convert_input_width(1) * 1000),
620 (int)(convert_input_width(2) * 1000),
621 (int)(convert_input_width(3) * 1000),
622 (int)(convert_input_width(4) * 1000),
Austin Schuhe666dc62018-08-08 21:09:12 -0700623 (int)(convert_input_width(5) * 1000),
Austin Schuhbcce26a2018-03-26 23:41:24 -0700624 static_cast<int>(output.left_voltage * 100),
625 static_cast<int>(output.right_voltage * 100),
626 static_cast<int>(polydrivetrain->velocity() * 100),
Austin Schuh4fae0fc2018-03-27 23:51:42 -0700627 static_cast<int>(spring->state()), static_cast<int>(spring->Near()),
628 static_cast<int>(spring->angle() * 1000),
629 static_cast<int>(spring->goal() * 1000),
630 static_cast<int>(spring->timeout()), adc_readings.sin,
631 adc_readings.cos, (int)(encoder.angle * 1000),
Austin Schuhbcce26a2018-03-26 23:41:24 -0700632 encoder.valid ? "T" : "f",
633 (int)(::std::sqrt(ConvertEncoderChannel(adc_readings.sin) *
634 ConvertEncoderChannel(adc_readings.sin) +
635 ConvertEncoderChannel(adc_readings.cos) *
636 ConvertEncoderChannel(adc_readings.cos)) *
637 1000));
638 }
639 }
Austin Schuh4fae0fc2018-03-27 23:51:42 -0700640 */
Austin Schuhbcce26a2018-03-26 23:41:24 -0700641 }
Brian Silvermana3a172b2018-03-24 03:53:32 -0400642}
643
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700644} // namespace
645
646extern "C" {
647
648void *__stack_chk_guard = (void *)0x67111971;
649
650int _write(int /*file*/, char *ptr, int len) {
651 teensy::AcmTty *const tty = global_stdout.load(::std::memory_order_acquire);
652 if (tty != nullptr) {
653 return tty->Write(ptr, len);
654 }
655 return 0;
656}
657
658void __stack_chk_fail(void);
659
Brian Silverman54dd2fe2018-03-16 23:44:31 -0700660} // extern "C"
661
662extern "C" int main(void) {
663 // for background about this startup delay, please see these conversations
664 // https://forum.pjrc.com/threads/36606-startup-time-(400ms)?p=113980&viewfull=1#post113980
665 // https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273
666 delay(400);
667
668 // Set all interrupts to the second-lowest priority to start with.
669 for (int i = 0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_SANE_PRIORITY(i, 0xD);
670
671 // Now set priorities for all the ones we care about. They only have meaning
672 // relative to each other, which means centralizing them here makes it a lot
673 // more manageable.
674 NVIC_SET_SANE_PRIORITY(IRQ_USBOTG, 0x7);
Brian Silvermana3a172b2018-03-24 03:53:32 -0400675 NVIC_SET_SANE_PRIORITY(IRQ_FTM0, 0xa);
676 NVIC_SET_SANE_PRIORITY(IRQ_FTM3, 0xa);
677 NVIC_SET_SANE_PRIORITY(IRQ_PIT_CH3, 0x5);
Brian Silverman54dd2fe2018-03-16 23:44:31 -0700678
679 // Builtin LED.
680 PERIPHERAL_BITBAND(GPIOC_PDOR, 5) = 1;
681 PORTC_PCR5 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(1);
682 PERIPHERAL_BITBAND(GPIOC_PDDR, 5) = 1;
683
684 // Set up the CAN pins.
685 PORTA_PCR12 = PORT_PCR_DSE | PORT_PCR_MUX(2);
686 PORTA_PCR13 = PORT_PCR_DSE | PORT_PCR_MUX(2);
687
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700688 // PWM_IN0
689 // FTM0_CH0
690 PORTC_PCR1 = PORT_PCR_MUX(4);
691
692 // PWM_IN1
693 // FTM0_CH6
694 PORTD_PCR6 = PORT_PCR_MUX(4);
695
696 // PWM_IN2
697 // FTM0_CH4
698 PORTD_PCR4 = PORT_PCR_MUX(4);
699
700 // PWM_IN3
701 // FTM3_CH2
702 PORTD_PCR2 = PORT_PCR_MUX(4);
703
704 // PWM_IN4
705 // FTM0_CH2
706 PORTC_PCR3 = PORT_PCR_MUX(4);
707
Brian Silverman7f5f1442018-04-06 13:00:50 -0400708 // PWM_IN5
709 // FTM3_CH6
710 PORTC_PCR10 = PORT_PCR_MUX(3);
711
Brian Silverman2de95d62018-03-31 12:32:24 -0700712 // SPI0
713 // ACC_CS PCS0
714 PORTA_PCR14 = PORT_PCR_DSE | PORT_PCR_MUX(2);
715 // SCK
716 PORTA_PCR15 = PORT_PCR_DSE | PORT_PCR_MUX(2);
717 // MOSI
718 PORTA_PCR16 = PORT_PCR_DSE | PORT_PCR_MUX(2);
719 // MISO
720 PORTA_PCR17 = PORT_PCR_DSE | PORT_PCR_MUX(2);
721
722 SIM_SCGC6 |= SIM_SCGC6_SPI0;
723 SPI0_MCR = SPI_MCR_MSTR | SPI_MCR_PCSIS(1) | SPI_MCR_CLR_TXF |
724 SPI_MCR_CLR_RXF | SPI_MCR_HALT;
725 // 60 MHz "protocol clock"
726 // 6ns CS setup
727 // 8ns CS hold
728 SPI0_CTAR0 = SPI_CTAR_FMSZ(15) | SPI_CTAR_CPOL /* Clock idles high */ |
729 SPI_CTAR_CPHA /* Data captured on trailing edge */ |
730 0 /* !LSBFE MSB first */ |
731 SPI_CTAR_PCSSCK(0) /* PCS->SCK prescaler = 1 */ |
732 SPI_CTAR_PASC(0) /* SCK->PCS prescaler = 1 */ |
733 SPI_CTAR_PDT(0) /* PCS->PCS prescaler = 1 */ |
734 SPI_CTAR_PBR(0) /* baud prescaler = 1 */ |
735 SPI_CTAR_CSSCK(0) /* PCS->SCK 2/60MHz = 33.33ns */ |
736 SPI_CTAR_ASC(0) /* SCK->PCS 2/60MHz = 33.33ns */ |
737 SPI_CTAR_DT(2) /* PCS->PSC 8/60MHz = 133.33ns */ |
738 SPI_CTAR_BR(2) /* BR 60MHz/6 = 10MHz */;
739
740 SPI0_MCR &= ~SPI_MCR_HALT;
741
Brian Silverman54dd2fe2018-03-16 23:44:31 -0700742 delay(100);
743
744 teensy::UsbDevice usb_device(0, 0x16c0, 0x0492);
745 usb_device.SetManufacturer("Seems Reasonable LLC");
746 usb_device.SetProduct("Simple Receiver Board");
747
748 teensy::AcmTty tty0(&usb_device);
749 global_stdout.store(&tty0, ::std::memory_order_release);
750 usb_device.Initialize();
751
Brian Silvermana3a172b2018-03-24 03:53:32 -0400752 SIM_SCGC6 |= SIM_SCGC6_PIT;
753 // Workaround for errata e7914.
754 (void)PIT_MCR;
755 PIT_MCR = 0;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700756 PIT_LDVAL3 = (BUS_CLOCK_FREQUENCY / 200) - 1;
Brian Silvermana3a172b2018-03-24 03:53:32 -0400757 PIT_TCTRL3 = PIT_TCTRL_TIE | PIT_TCTRL_TEN;
758
Brian Silverman54dd2fe2018-03-16 23:44:31 -0700759 can_init(0, 1);
Brian Silvermana96c1a42018-05-12 12:11:31 -0700760 AdcInitSimple();
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700761 SetupPwmFtm(FTM0);
762 SetupPwmFtm(FTM3);
Brian Silverman54dd2fe2018-03-16 23:44:31 -0700763
Austin Schuhbcce26a2018-03-26 23:41:24 -0700764 PolyDrivetrain<float> polydrivetrain(GetDrivetrainConfig(), nullptr);
765 global_polydrivetrain.store(&polydrivetrain, ::std::memory_order_release);
Austin Schuh4fae0fc2018-03-27 23:51:42 -0700766 Spring spring;
767 global_spring.store(&spring, ::std::memory_order_release);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700768
Brian Silverman54dd2fe2018-03-16 23:44:31 -0700769 // Leave the LEDs on for a bit longer.
770 delay(300);
771 printf("Done starting up\n");
772
Brian Silverman2de95d62018-03-31 12:32:24 -0700773 AccelerometerInit();
774 printf("Accelerometer init %s\n", accelerometer_inited ? "success" : "fail");
775
Brian Silverman54dd2fe2018-03-16 23:44:31 -0700776 // Done starting up, now turn the LED off.
777 PERIPHERAL_BITBAND(GPIOC_PDOR, 5) = 0;
778
Brian Silvermana3a172b2018-03-24 03:53:32 -0400779 NVIC_ENABLE_IRQ(IRQ_FTM0);
780 NVIC_ENABLE_IRQ(IRQ_FTM3);
781 NVIC_ENABLE_IRQ(IRQ_PIT_CH3);
Austin Schuhe666dc62018-08-08 21:09:12 -0700782 printf("Done starting up2\n");
Brian Silvermana3a172b2018-03-24 03:53:32 -0400783
Austin Schuhe666dc62018-08-08 21:09:12 -0700784 //DoReceiverTest2();
Austin Schuhbcce26a2018-03-26 23:41:24 -0700785 while (true) {
786 }
Brian Silverman54dd2fe2018-03-16 23:44:31 -0700787
788 return 0;
789}
790
791void __stack_chk_fail(void) {
792 while (true) {
793 GPIOC_PSOR = (1 << 5);
794 printf("Stack corruption detected\n");
795 delay(1000);
796 GPIOC_PCOR = (1 << 5);
797 delay(1000);
798 }
799}
800
801} // namespace motors
802} // namespace frc971