blob: 678319a8fbad043db44231d6dcc911b39320f282 [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
32const ShifterHallEffect kThreeStateDriveShifter{0.0, 0.0, 0.25, 0.75};
33
34const DrivetrainConfig<float> &GetDrivetrainConfig() {
35 static DrivetrainConfig<float> kDrivetrainConfig{
36 ::frc971::control_loops::drivetrain::ShifterType::NO_SHIFTER,
37 ::frc971::control_loops::drivetrain::LoopType::OPEN_LOOP,
38 ::frc971::control_loops::drivetrain::GyroType::SPARTAN_GYRO,
39 ::frc971::control_loops::drivetrain::IMUType::IMU_X,
40
41 ::motors::seems_reasonable::MakeDrivetrainLoop,
42 ::motors::seems_reasonable::MakeVelocityDrivetrainLoop,
43 ::std::function<StateFeedbackLoop<7, 2, 4, float>()>(),
44
45 ::motors::seems_reasonable::kDt, ::motors::seems_reasonable::kRobotRadius,
46 ::motors::seems_reasonable::kWheelRadius, ::motors::seems_reasonable::kV,
47
48 ::motors::seems_reasonable::kHighGearRatio,
49 ::motors::seems_reasonable::kLowGearRatio, kThreeStateDriveShifter,
50 kThreeStateDriveShifter, true /* default_high_gear */,
51 0 /* down_offset if using constants use
52 constants::GetValues().down_error */, 0.8 /* wheel_non_linearity */,
53 1.2 /* quickturn_wheel_multiplier */, 1.5 /* wheel_multiplier */,
54 };
55
56 return kDrivetrainConfig;
57};
58
59
Brian Silverman54dd2fe2018-03-16 23:44:31 -070060::std::atomic<teensy::AcmTty *> global_stdout{nullptr};
61
Austin Schuhbcce26a2018-03-26 23:41:24 -070062::std::atomic<PolyDrivetrain<float> *> global_polydrivetrain{nullptr};
Austin Schuh4fae0fc2018-03-27 23:51:42 -070063::std::atomic<Spring *> global_spring{nullptr};
Austin Schuhbcce26a2018-03-26 23:41:24 -070064
Brian Silvermana3a172b2018-03-24 03:53:32 -040065// Last width we received on each channel.
66uint16_t pwm_input_widths[5];
67// When we received a pulse on each channel in milliseconds.
68uint32_t pwm_input_times[5];
69
Austin Schuhbcce26a2018-03-26 23:41:24 -070070constexpr int kChannelTimeout = 100;
71
72bool lost_channel(int channel) {
73 DisableInterrupts disable_interrupts;
74 if (time_after(millis(),
75 time_add(pwm_input_times[channel], kChannelTimeout))) {
76 return true;
77 }
78 return false;
79}
80
Brian Silvermana3a172b2018-03-24 03:53:32 -040081// Returns the most recently captured value for the specified input channel
82// scaled from -1 to 1, or 0 if it was captured over 100ms ago.
83float convert_input_width(int channel) {
84 uint16_t width;
85 {
86 DisableInterrupts disable_interrupts;
Austin Schuhbcce26a2018-03-26 23:41:24 -070087 if (time_after(millis(),
88 time_add(pwm_input_times[channel], kChannelTimeout))) {
Brian Silvermana3a172b2018-03-24 03:53:32 -040089 return 0;
90 }
91
92 width = pwm_input_widths[channel];
93 }
94
95 // Values measured with a channel mapped to a button.
96 static constexpr uint16_t kMinWidth = 4133;
97 static constexpr uint16_t kMaxWidth = 7177;
98 if (width < kMinWidth) {
99 width = kMinWidth;
100 } else if (width > kMaxWidth) {
101 width = kMaxWidth;
102 }
103 return (static_cast<float>(2 * (width - kMinWidth)) /
104 static_cast<float>(kMaxWidth - kMinWidth)) -
105 1.0f;
106}
107
108// Sends a SET_RPM command to the specified VESC.
109// Note that sending 6 VESC commands every 1ms doesn't quite fit in the CAN
110// bandwidth.
111void vesc_set_rpm(int vesc_id, float rpm) {
112 const int32_t rpm_int = rpm;
113 uint32_t id = CAN_EFF_FLAG;
114 id |= vesc_id;
115 id |= (0x03 /* SET_RPM */) << 8;
116 uint8_t data[4] = {
117 static_cast<uint8_t>((rpm_int >> 24) & 0xFF),
118 static_cast<uint8_t>((rpm_int >> 16) & 0xFF),
119 static_cast<uint8_t>((rpm_int >> 8) & 0xFF),
120 static_cast<uint8_t>((rpm_int >> 0) & 0xFF),
121 };
122 can_send(id, data, sizeof(data), 2 + vesc_id);
123}
124
125// Sends a SET_CURRENT command to the specified VESC.
126// current is in amps.
127// Note that sending 6 VESC commands every 1ms doesn't quite fit in the CAN
128// bandwidth.
129void vesc_set_current(int vesc_id, float current) {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700130 constexpr float kMaxCurrent = 80.0f;
131 const int32_t current_int =
132 ::std::max(-kMaxCurrent, ::std::min(kMaxCurrent, current)) * 1000.0f;
Brian Silvermana3a172b2018-03-24 03:53:32 -0400133 uint32_t id = CAN_EFF_FLAG;
134 id |= vesc_id;
135 id |= (0x01 /* SET_CURRENT */) << 8;
136 uint8_t data[4] = {
137 static_cast<uint8_t>((current_int >> 24) & 0xFF),
138 static_cast<uint8_t>((current_int >> 16) & 0xFF),
139 static_cast<uint8_t>((current_int >> 8) & 0xFF),
140 static_cast<uint8_t>((current_int >> 0) & 0xFF),
141 };
142 can_send(id, data, sizeof(data), 2 + vesc_id);
143}
144
Brian Silverman4d1e5272018-03-26 03:18:42 -0400145// Sends a SET_DUTY command to the specified VESC.
146// duty is from -1 to 1.
147// Note that sending 6 VESC commands every 1ms doesn't quite fit in the CAN
148// bandwidth.
149void vesc_set_duty(int vesc_id, float duty) {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700150 constexpr int32_t kMaxDuty = 99999;
151 const int32_t duty_int = ::std::max(
152 -kMaxDuty, ::std::min(kMaxDuty, static_cast<int32_t>(duty * 100000.0f)));
Brian Silverman4d1e5272018-03-26 03:18:42 -0400153 uint32_t id = CAN_EFF_FLAG;
154 id |= vesc_id;
155 id |= (0x00 /* SET_DUTY */) << 8;
156 uint8_t data[4] = {
157 static_cast<uint8_t>((duty_int >> 24) & 0xFF),
158 static_cast<uint8_t>((duty_int >> 16) & 0xFF),
159 static_cast<uint8_t>((duty_int >> 8) & 0xFF),
160 static_cast<uint8_t>((duty_int >> 0) & 0xFF),
161 };
162 can_send(id, data, sizeof(data), 2 + vesc_id);
163}
164
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700165void DoVescTest() {
Brian Silverman54dd2fe2018-03-16 23:44:31 -0700166 uint32_t time = micros();
167 while (true) {
168 for (int i = 0; i < 6; ++i) {
169 const uint32_t end = time_add(time, 500000);
170 while (true) {
171 const bool done = time_after(micros(), end);
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700172 float current;
Brian Silverman54dd2fe2018-03-16 23:44:31 -0700173 if (done) {
174 current = -6;
175 } else {
176 current = 6;
177 }
Brian Silvermana3a172b2018-03-24 03:53:32 -0400178 vesc_set_current(i, current);
Brian Silverman54dd2fe2018-03-16 23:44:31 -0700179 if (done) {
180 break;
181 }
182 delay(5);
183 }
184 time = end;
185 }
186 }
187}
188
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700189void DoReceiverTest2() {
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700190 static constexpr float kMaxRpm = 10000.0f;
191 while (true) {
Brian Silvermana3a172b2018-03-24 03:53:32 -0400192 const bool flip = convert_input_width(2) > 0;
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700193
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700194 {
Brian Silvermana3a172b2018-03-24 03:53:32 -0400195 const float value = convert_input_width(0);
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700196
197 {
Brian Silvermana3a172b2018-03-24 03:53:32 -0400198 float rpm = ::std::min(0.0f, value) * kMaxRpm;
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700199 if (flip) {
200 rpm *= -1.0f;
201 }
Brian Silvermana3a172b2018-03-24 03:53:32 -0400202 vesc_set_rpm(0, rpm);
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700203 }
204
205 {
Brian Silvermana3a172b2018-03-24 03:53:32 -0400206 float rpm = ::std::max(0.0f, value) * kMaxRpm;
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700207 if (flip) {
208 rpm *= -1.0f;
209 }
Brian Silvermana3a172b2018-03-24 03:53:32 -0400210 vesc_set_rpm(1, rpm);
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700211 }
212 }
213
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700214 {
Brian Silvermana3a172b2018-03-24 03:53:32 -0400215 const float value = convert_input_width(1);
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700216
217 {
Brian Silvermana3a172b2018-03-24 03:53:32 -0400218 float rpm = ::std::min(0.0f, value) * kMaxRpm;
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700219 if (flip) {
220 rpm *= -1.0f;
221 }
Brian Silvermana3a172b2018-03-24 03:53:32 -0400222 vesc_set_rpm(2, rpm);
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700223 }
224
225 {
Brian Silvermana3a172b2018-03-24 03:53:32 -0400226 float rpm = ::std::max(0.0f, value) * kMaxRpm;
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700227 if (flip) {
228 rpm *= -1.0f;
229 }
Brian Silvermana3a172b2018-03-24 03:53:32 -0400230 vesc_set_rpm(3, rpm);
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700231 }
232 }
233
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700234 {
Brian Silvermana3a172b2018-03-24 03:53:32 -0400235 const float value = convert_input_width(4);
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700236
237 {
Brian Silvermana3a172b2018-03-24 03:53:32 -0400238 float rpm = ::std::min(0.0f, value) * kMaxRpm;
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700239 if (flip) {
240 rpm *= -1.0f;
241 }
Brian Silvermana3a172b2018-03-24 03:53:32 -0400242 vesc_set_rpm(4, rpm);
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700243 }
244
245 {
Brian Silvermana3a172b2018-03-24 03:53:32 -0400246 float rpm = ::std::max(0.0f, value) * kMaxRpm;
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700247 if (flip) {
248 rpm *= -1.0f;
249 }
Brian Silvermana3a172b2018-03-24 03:53:32 -0400250 vesc_set_rpm(5, rpm);
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700251 }
252 }
Brian Silvermana3a172b2018-03-24 03:53:32 -0400253 // Give the CAN frames a chance to go out.
254 delay(5);
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700255 }
256}
257
258void SetupPwmFtm(BigFTM *ftm) {
259 ftm->MODE = FTM_MODE_WPDIS;
260 ftm->MODE = FTM_MODE_WPDIS | FTM_MODE_FTMEN;
261 ftm->SC = FTM_SC_CLKS(0) /* Disable counting for now */;
262
263 // Can't change MOD according to the reference manual ("The Dual Edge Capture
264 // mode must be used with ... the FTM counter in Free running counter.").
265 ftm->MOD = 0xFFFF;
266
267 // Capturing rising edge.
268 ftm->C0SC = FTM_CSC_MSA | FTM_CSC_ELSA;
269 // Capturing falling edge.
Brian Silvermana3a172b2018-03-24 03:53:32 -0400270 ftm->C1SC = FTM_CSC_CHIE | FTM_CSC_MSA | FTM_CSC_ELSB;
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700271
272 // Capturing rising edge.
273 ftm->C2SC = FTM_CSC_MSA | FTM_CSC_ELSA;
274 // Capturing falling edge.
Brian Silvermana3a172b2018-03-24 03:53:32 -0400275 ftm->C3SC = FTM_CSC_CHIE | FTM_CSC_MSA | FTM_CSC_ELSB;
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700276
277 // Capturing rising edge.
278 ftm->C4SC = FTM_CSC_MSA | FTM_CSC_ELSA;
279 // Capturing falling edge.
Brian Silvermana3a172b2018-03-24 03:53:32 -0400280 ftm->C5SC = FTM_CSC_CHIE | FTM_CSC_MSA | FTM_CSC_ELSB;
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700281
282 // Capturing rising edge.
283 ftm->C6SC = FTM_CSC_MSA | FTM_CSC_ELSA;
284 // Capturing falling edge.
Brian Silvermana3a172b2018-03-24 03:53:32 -0400285 ftm->C7SC = FTM_CSC_CHIE | FTM_CSC_MSA | FTM_CSC_ELSB;
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700286
Brian Silvermana3a172b2018-03-24 03:53:32 -0400287 (void)ftm->STATUS;
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700288 ftm->STATUS = 0x00;
289
290 ftm->COMBINE = FTM_COMBINE_DECAP3 | FTM_COMBINE_DECAPEN3 |
291 FTM_COMBINE_DECAP2 | FTM_COMBINE_DECAPEN2 |
292 FTM_COMBINE_DECAP1 | FTM_COMBINE_DECAPEN1 |
293 FTM_COMBINE_DECAP0 | FTM_COMBINE_DECAPEN0;
294
295 // 34.95ms max period before it starts wrapping and being weird.
296 ftm->SC = FTM_SC_CLKS(1) /* Use the system clock */ |
297 FTM_SC_PS(4) /* Prescaler=32 */;
298
299 ftm->MODE &= ~FTM_MODE_WPDIS;
300}
301
Brian Silvermana3a172b2018-03-24 03:53:32 -0400302extern "C" void ftm0_isr() {
303 while (true) {
304 const uint32_t status = FTM0->STATUS;
305 if (status == 0) {
306 return;
307 }
308
309 if (status & (1 << 1)) {
310 const uint32_t start = FTM0->C0V;
311 const uint32_t end = FTM0->C1V;
312 pwm_input_widths[0] = (end - start) & 0xFFFF;
313 pwm_input_times[0] = millis();
314 }
315 if (status & (1 << 7)) {
316 const uint32_t start = FTM0->C6V;
317 const uint32_t end = FTM0->C7V;
318 pwm_input_widths[1] = (end - start) & 0xFFFF;
319 pwm_input_times[1] = millis();
320 }
321 if (status & (1 << 5)) {
322 const uint32_t start = FTM0->C4V;
323 const uint32_t end = FTM0->C5V;
324 pwm_input_widths[2] = (end - start) & 0xFFFF;
325 pwm_input_times[2] = millis();
326 }
327 if (status & (1 << 3)) {
328 const uint32_t start = FTM0->C2V;
329 const uint32_t end = FTM0->C3V;
330 pwm_input_widths[4] = (end - start) & 0xFFFF;
331 pwm_input_times[4] = millis();
332 }
333
334 FTM0->STATUS = 0;
335 }
336}
337
338extern "C" void ftm3_isr() {
339 while (true) {
340 const uint32_t status = FTM3->STATUS;
341 if (status == 0) {
342 return;
343 }
344
345 if (status & (1 << 3)) {
346 const uint32_t start = FTM3->C2V;
347 const uint32_t end = FTM3->C3V;
348 pwm_input_widths[3] = (end - start) & 0xFFFF;
349 pwm_input_times[3] = millis();
350 }
351
352 FTM3->STATUS = 0;
353 }
354}
355
356float ConvertEncoderChannel(uint16_t reading) {
357 // Theoretical values based on the datasheet are 931 and 2917.
358 // With these values, the magnitude ranges from 0.99-1.03, which works fine
359 // (the encoder's output appears to get less accurate in one quadrant for some
360 // reason, hence the variation).
361 static constexpr uint16_t kMin = 802, kMax = 3088;
362 if (reading < kMin) {
363 reading = kMin;
364 } else if (reading > kMax) {
365 reading = kMax;
366 }
367 return (static_cast<float>(2 * (reading - kMin)) /
368 static_cast<float>(kMax - kMin)) -
369 1.0f;
370}
371
372struct EncoderReading {
373 EncoderReading(const salsa::SimpleAdcReadings &adc_readings) {
374 const float sin = ConvertEncoderChannel(adc_readings.sin);
375 const float cos = ConvertEncoderChannel(adc_readings.cos);
376
Austin Schuhbcce26a2018-03-26 23:41:24 -0700377 const float magnitude = hypot(sin, cos);
Brian Silvermana3a172b2018-03-24 03:53:32 -0400378 const float magnitude_error = ::std::abs(magnitude - 1.0f);
Austin Schuh4fae0fc2018-03-27 23:51:42 -0700379 valid = magnitude_error < 0.30f;
Brian Silvermana3a172b2018-03-24 03:53:32 -0400380
381 angle = ::std::atan2(sin, cos);
382 }
383
384 // Angle in radians, in [-pi, pi].
385 float angle;
386
387 bool valid;
388};
389
390extern "C" void pit3_isr() {
391 PIT_TFLG3 = 1;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700392 PolyDrivetrain<float> *polydrivetrain =
393 global_polydrivetrain.load(::std::memory_order_acquire);
Austin Schuh4fae0fc2018-03-27 23:51:42 -0700394 Spring *spring = global_spring.load(::std::memory_order_acquire);
Brian Silvermana3a172b2018-03-24 03:53:32 -0400395
396 salsa::SimpleAdcReadings adc_readings;
397 {
398 DisableInterrupts disable_interrupts;
399 adc_readings = salsa::AdcReadSimple(disable_interrupts);
400 }
401
402 EncoderReading encoder(adc_readings);
Austin Schuh4fae0fc2018-03-27 23:51:42 -0700403 static float last_good_encoder = 0.0f;
404 static int invalid_encoder_count = 0;
405 if (encoder.valid) {
406 last_good_encoder = encoder.angle;
407 invalid_encoder_count = 0;
408 } else {
409 ++invalid_encoder_count;
410 }
Brian Silvermana3a172b2018-03-24 03:53:32 -0400411
Austin Schuhbcce26a2018-03-26 23:41:24 -0700412 const bool lost_any_channel = lost_channel(0) || lost_channel(1) ||
413 lost_channel(2) || lost_channel(3) ||
414 lost_channel(4) ||
Austin Schuh4fae0fc2018-03-27 23:51:42 -0700415 (::std::abs(convert_input_width(4)) < 0.5f);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700416
Austin Schuh4fae0fc2018-03-27 23:51:42 -0700417 if (polydrivetrain != nullptr && spring != nullptr) {
Austin Schuhbcce26a2018-03-26 23:41:24 -0700418 DrivetrainQueue_Goal goal;
419 goal.control_loop_driving = false;
420 if (lost_any_channel) {
421 goal.throttle = 0.0f;
422 goal.wheel = 0.0f;
423 } else {
424 goal.throttle = convert_input_width(1);
Austin Schuh4fae0fc2018-03-27 23:51:42 -0700425 goal.wheel = -convert_input_width(0);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700426 }
Austin Schuhbcce26a2018-03-26 23:41:24 -0700427 goal.quickturn = ::std::abs(polydrivetrain->velocity()) < 0.25f;
428
429 DrivetrainQueue_Output output;
430
431 polydrivetrain->SetGoal(goal);
432 polydrivetrain->Update();
433 polydrivetrain->SetOutput(&output);
434
435 vesc_set_duty(0, -output.left_voltage / 12.0f);
436 vesc_set_duty(1, -output.left_voltage / 12.0f);
437
438 vesc_set_duty(2, output.right_voltage / 12.0f);
439 vesc_set_duty(3, output.right_voltage / 12.0f);
440
Austin Schuh4fae0fc2018-03-27 23:51:42 -0700441 bool prime = convert_input_width(2) > 0.1f;
442 bool fire = convert_input_width(3) > 0.1f;
443
444 bool unload = lost_any_channel;
445 static bool was_lost = true;
446 bool force_reset = !lost_any_channel && was_lost;
447 was_lost = lost_any_channel;
448
449 spring->Iterate(unload, prime, fire, force_reset, invalid_encoder_count <= 2,
450 last_good_encoder);
451
452 float spring_output = spring->output();
453
454 vesc_set_duty(4, -spring_output);
455 vesc_set_duty(5, spring_output);
456
457 /*
458 // Massive debug. Turn on for useful bits.
459 if (!encoder.valid) {
460 printf("Stuck encoder: ADC %" PRIu16 " %" PRIu16
461 " enc %d/1000 %s mag %d\n",
462 adc_readings.sin, adc_readings.cos, (int)(encoder.angle * 1000),
463 encoder.valid ? "T" : "f",
464 (int)(hypot(ConvertEncoderChannel(adc_readings.sin),
465 ConvertEncoderChannel(adc_readings.cos)) *
466 1000));
467 }
Austin Schuhbcce26a2018-03-26 23:41:24 -0700468 static int i = 0;
469 ++i;
470 if (i > 20) {
471 i = 0;
472 if (lost_any_channel) {
473 printf("200Hz loop, disabled %d %d %d %d %d\n",
474 (int)(convert_input_width(0) * 1000),
475 (int)(convert_input_width(1) * 1000),
476 (int)(convert_input_width(2) * 1000),
477 (int)(convert_input_width(3) * 1000),
478 (int)(convert_input_width(4) * 1000));
479 } else {
480 printf(
481 "TODO(Austin): 200Hz loop %d %d %d %d %d, lr, %d, %d velocity %d "
Austin Schuh4fae0fc2018-03-27 23:51:42 -0700482 " state: %d, near %d angle %d goal %d to: %d ADC %" PRIu16
483 " %" PRIu16 " enc %d/1000 %s from %d\n",
Austin Schuhbcce26a2018-03-26 23:41:24 -0700484 (int)(convert_input_width(0) * 1000),
485 (int)(convert_input_width(1) * 1000),
486 (int)(convert_input_width(2) * 1000),
487 (int)(convert_input_width(3) * 1000),
488 (int)(convert_input_width(4) * 1000),
489 static_cast<int>(output.left_voltage * 100),
490 static_cast<int>(output.right_voltage * 100),
491 static_cast<int>(polydrivetrain->velocity() * 100),
Austin Schuh4fae0fc2018-03-27 23:51:42 -0700492 static_cast<int>(spring->state()), static_cast<int>(spring->Near()),
493 static_cast<int>(spring->angle() * 1000),
494 static_cast<int>(spring->goal() * 1000),
495 static_cast<int>(spring->timeout()), adc_readings.sin,
496 adc_readings.cos, (int)(encoder.angle * 1000),
Austin Schuhbcce26a2018-03-26 23:41:24 -0700497 encoder.valid ? "T" : "f",
498 (int)(::std::sqrt(ConvertEncoderChannel(adc_readings.sin) *
499 ConvertEncoderChannel(adc_readings.sin) +
500 ConvertEncoderChannel(adc_readings.cos) *
501 ConvertEncoderChannel(adc_readings.cos)) *
502 1000));
503 }
504 }
Austin Schuh4fae0fc2018-03-27 23:51:42 -0700505 */
Austin Schuhbcce26a2018-03-26 23:41:24 -0700506 }
Brian Silvermana3a172b2018-03-24 03:53:32 -0400507}
508
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700509} // namespace
510
511extern "C" {
512
513void *__stack_chk_guard = (void *)0x67111971;
514
515int _write(int /*file*/, char *ptr, int len) {
516 teensy::AcmTty *const tty = global_stdout.load(::std::memory_order_acquire);
517 if (tty != nullptr) {
518 return tty->Write(ptr, len);
519 }
520 return 0;
521}
522
523void __stack_chk_fail(void);
524
Brian Silverman54dd2fe2018-03-16 23:44:31 -0700525} // extern "C"
526
527extern "C" int main(void) {
528 // for background about this startup delay, please see these conversations
529 // https://forum.pjrc.com/threads/36606-startup-time-(400ms)?p=113980&viewfull=1#post113980
530 // https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273
531 delay(400);
532
533 // Set all interrupts to the second-lowest priority to start with.
534 for (int i = 0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_SANE_PRIORITY(i, 0xD);
535
536 // Now set priorities for all the ones we care about. They only have meaning
537 // relative to each other, which means centralizing them here makes it a lot
538 // more manageable.
539 NVIC_SET_SANE_PRIORITY(IRQ_USBOTG, 0x7);
Brian Silvermana3a172b2018-03-24 03:53:32 -0400540 NVIC_SET_SANE_PRIORITY(IRQ_FTM0, 0xa);
541 NVIC_SET_SANE_PRIORITY(IRQ_FTM3, 0xa);
542 NVIC_SET_SANE_PRIORITY(IRQ_PIT_CH3, 0x5);
Brian Silverman54dd2fe2018-03-16 23:44:31 -0700543
544 // Builtin LED.
545 PERIPHERAL_BITBAND(GPIOC_PDOR, 5) = 1;
546 PORTC_PCR5 = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(1);
547 PERIPHERAL_BITBAND(GPIOC_PDDR, 5) = 1;
548
549 // Set up the CAN pins.
550 PORTA_PCR12 = PORT_PCR_DSE | PORT_PCR_MUX(2);
551 PORTA_PCR13 = PORT_PCR_DSE | PORT_PCR_MUX(2);
552
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700553 // PWM_IN0
554 // FTM0_CH0
555 PORTC_PCR1 = PORT_PCR_MUX(4);
556
557 // PWM_IN1
558 // FTM0_CH6
559 PORTD_PCR6 = PORT_PCR_MUX(4);
560
561 // PWM_IN2
562 // FTM0_CH4
563 PORTD_PCR4 = PORT_PCR_MUX(4);
564
565 // PWM_IN3
566 // FTM3_CH2
567 PORTD_PCR2 = PORT_PCR_MUX(4);
568
569 // PWM_IN4
570 // FTM0_CH2
571 PORTC_PCR3 = PORT_PCR_MUX(4);
572
Brian Silverman54dd2fe2018-03-16 23:44:31 -0700573 delay(100);
574
575 teensy::UsbDevice usb_device(0, 0x16c0, 0x0492);
576 usb_device.SetManufacturer("Seems Reasonable LLC");
577 usb_device.SetProduct("Simple Receiver Board");
578
579 teensy::AcmTty tty0(&usb_device);
580 global_stdout.store(&tty0, ::std::memory_order_release);
581 usb_device.Initialize();
582
Brian Silvermana3a172b2018-03-24 03:53:32 -0400583 SIM_SCGC6 |= SIM_SCGC6_PIT;
584 // Workaround for errata e7914.
585 (void)PIT_MCR;
586 PIT_MCR = 0;
Austin Schuhbcce26a2018-03-26 23:41:24 -0700587 PIT_LDVAL3 = (BUS_CLOCK_FREQUENCY / 200) - 1;
Brian Silvermana3a172b2018-03-24 03:53:32 -0400588 PIT_TCTRL3 = PIT_TCTRL_TIE | PIT_TCTRL_TEN;
589
Brian Silverman54dd2fe2018-03-16 23:44:31 -0700590 can_init(0, 1);
Brian Silvermana3a172b2018-03-24 03:53:32 -0400591 salsa::AdcInitSimple();
Brian Silverman4f8c6a72018-03-17 23:12:45 -0700592 SetupPwmFtm(FTM0);
593 SetupPwmFtm(FTM3);
Brian Silverman54dd2fe2018-03-16 23:44:31 -0700594
Austin Schuhbcce26a2018-03-26 23:41:24 -0700595 PolyDrivetrain<float> polydrivetrain(GetDrivetrainConfig(), nullptr);
596 global_polydrivetrain.store(&polydrivetrain, ::std::memory_order_release);
Austin Schuh4fae0fc2018-03-27 23:51:42 -0700597 Spring spring;
598 global_spring.store(&spring, ::std::memory_order_release);
Austin Schuhbcce26a2018-03-26 23:41:24 -0700599
Brian Silverman54dd2fe2018-03-16 23:44:31 -0700600 // Leave the LEDs on for a bit longer.
601 delay(300);
602 printf("Done starting up\n");
603
604 // Done starting up, now turn the LED off.
605 PERIPHERAL_BITBAND(GPIOC_PDOR, 5) = 0;
606
Brian Silvermana3a172b2018-03-24 03:53:32 -0400607 NVIC_ENABLE_IRQ(IRQ_FTM0);
608 NVIC_ENABLE_IRQ(IRQ_FTM3);
609 NVIC_ENABLE_IRQ(IRQ_PIT_CH3);
610
Austin Schuhbcce26a2018-03-26 23:41:24 -0700611 while (true) {
612 }
Brian Silverman54dd2fe2018-03-16 23:44:31 -0700613
614 return 0;
615}
616
617void __stack_chk_fail(void) {
618 while (true) {
619 GPIOC_PSOR = (1 << 5);
620 printf("Stack corruption detected\n");
621 delay(1000);
622 GPIOC_PCOR = (1 << 5);
623 delay(1000);
624 }
625}
626
627} // namespace motors
628} // namespace frc971