blob: b0a0accef7be32853ebd96237a5e93e809891aee [file] [log] [blame]
James Kuszmaul998d3032018-09-08 15:41:41 -07001#include <inttypes.h>
2#include <stdio.h>
3
4#include <atomic>
5
Philipp Schrader790cb542023-07-05 21:06:52 -07006#include "motors/core/kinetis.h"
James Kuszmaul998d3032018-09-08 15:41:41 -07007#include "motors/core/time.h"
8#include "motors/fet12/current_equalization.h"
9#include "motors/fet12/motor_controls.h"
10#include "motors/motor.h"
11#include "motors/peripheral/adc.h"
James Kuszmaul7c8aad62018-09-08 18:16:18 -070012#include "motors/peripheral/adc_dma.h"
James Kuszmaul998d3032018-09-08 15:41:41 -070013#include "motors/peripheral/can.h"
Brian Silverman4787a6e2018-10-06 16:00:54 -070014#include "motors/print/print.h"
James Kuszmaul998d3032018-09-08 15:41:41 -070015#include "motors/util.h"
James Kuszmaul998d3032018-09-08 15:41:41 -070016
17namespace frc971 {
18namespace motors {
19namespace {
20
21constexpr double Kv = 22000.0 * 2.0 * M_PI / 60.0 / 30.0 * 3.6;
22constexpr double kVcc = 31.5;
23constexpr double kIcc = 125.0;
24constexpr double kR = 0.0084;
25
26struct Fet12AdcReadings {
James Kuszmaul7c8aad62018-09-08 18:16:18 -070027 // Averages of the pairs of ADC DMA channels corresponding with each channel
28 // pair. Individual values in motor_currents correspond to current sensor
29 // values, rather than the actual currents themselves (and so they still need
30 // to be decoupled).
James Kuszmaul998d3032018-09-08 15:41:41 -070031 int16_t motor_currents[3];
32 int16_t throttle, fuse_voltage;
33};
34
35void AdcInitFet12() {
36 AdcInitCommon(AdcChannels::kB, AdcChannels::kA);
37
38 // M_CH0V ADC0_SE5b
39 PORTD_PCR1 = PORT_PCR_MUX(0);
40
41 // M_CH1V ADC0_SE7b
42 PORTD_PCR6 = PORT_PCR_MUX(0);
43
44 // M_CH2V ADC0_SE14
45 PORTC_PCR0 = PORT_PCR_MUX(0);
46
47 // M_CH0F ADC1_SE5a
48 PORTE_PCR1 = PORT_PCR_MUX(0);
49
50 // M_CH1F ADC1_SE6a
51 PORTE_PCR2 = PORT_PCR_MUX(0);
52
53 // M_CH2F ADC1_SE7a
54 PORTE_PCR3 = PORT_PCR_MUX(0);
55
56 // SENSE0 ADC0_SE23
57 // dedicated
58
59 // SENSE1 ADC0_SE13
60 PORTB_PCR3 = PORT_PCR_MUX(0);
61}
62
James Kuszmaul998d3032018-09-08 15:41:41 -070063::std::atomic<Motor *> global_motor{nullptr};
James Kuszmaul7c8aad62018-09-08 18:16:18 -070064::std::atomic<teensy::AdcDmaSampler *> global_adc_dma{nullptr};
65
James Kuszmaul998d3032018-09-08 15:41:41 -070066extern "C" {
67
James Kuszmaul998d3032018-09-08 15:41:41 -070068void *__stack_chk_guard = (void *)0x67111971;
69void __stack_chk_fail(void) {
70 while (true) {
71 GPIOC_PSOR = (1 << 5);
72 printf("Stack corruption detected\n");
73 delay(1000);
74 GPIOC_PCOR = (1 << 5);
75 delay(1000);
76 }
77}
78
James Kuszmaul998d3032018-09-08 15:41:41 -070079extern char *__brkval;
80extern uint32_t __bss_ram_start__[];
81extern uint32_t __heap_start__[];
82extern uint32_t __stack_end__[];
83
84struct DebugBuffer {
85 struct Sample {
86 ::std::array<int16_t, 3> currents;
87 ::std::array<int16_t, 3> commanded_currents;
88 ::std::array<uint16_t, 3> commands;
Brian Silverman37a95d62018-11-09 16:08:32 -080089 ::std::array<int16_t, 3> readings;
James Kuszmaul998d3032018-09-08 15:41:41 -070090 uint16_t position;
91 // Driver requested current.
92 float driver_request;
93 // Requested current.
94 int16_t total_command;
95
96 float est_omega;
97 float fuse_voltage;
98 int16_t fuse_current;
99
100 float fuse_badness;
101 uint32_t cycles_since_start;
102 };
103
104 // The amount of data in the buffer. This will never decrement. This will be
105 // transferred out the serial port after it fills up.
106 ::std::atomic<size_t> size{0};
107 ::std::atomic<uint32_t> count{0};
108 // The data.
109 ::std::array<Sample, 512> samples;
110};
111
112DebugBuffer global_debug_buffer;
113
114void ftm0_isr(void) {
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700115 static uint32_t i = 0;
116 teensy::AdcDmaSampler *const adc_dma =
117 global_adc_dma.load(::std::memory_order_relaxed);
118
James Kuszmaul998d3032018-09-08 15:41:41 -0700119 Fet12AdcReadings adc_readings;
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700120 // TODO(Brian): Switch to the DMA interrupt instead of spinning.
121 while (!adc_dma->CheckDone()) {
James Kuszmaul998d3032018-09-08 15:41:41 -0700122 }
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700123
124 adc_readings.motor_currents[0] =
125 (adc_dma->adc_result(0, 0) + adc_dma->adc_result(0, 1)) / 2;
126 adc_readings.motor_currents[1] =
127 (adc_dma->adc_result(0, 2) + adc_dma->adc_result(1, 1)) / 2;
128 adc_readings.motor_currents[2] =
129 (adc_dma->adc_result(1, 0) + adc_dma->adc_result(1, 2)) / 2;
130 adc_readings.throttle = adc_dma->adc_result(0, 3);
James Kuszmaul998d3032018-09-08 15:41:41 -0700131 const ::std::array<float, 3> decoupled =
132 DecoupleCurrents(adc_readings.motor_currents);
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700133 adc_dma->Reset();
134 const uint32_t wrapped_encoder =
135 global_motor.load(::std::memory_order_relaxed)->wrapped_encoder();
Philipp Schrader790cb542023-07-05 21:06:52 -0700136 const BalancedReadings balanced = BalanceSimpleReadings(decoupled);
James Kuszmaul998d3032018-09-08 15:41:41 -0700137
James Kuszmaul521eb652018-10-17 19:09:33 -0700138#if 1
139
James Kuszmaul998d3032018-09-08 15:41:41 -0700140 static float fuse_badness = 0;
141
142 static uint32_t cycles_since_start = 0u;
143 ++cycles_since_start;
144#if 0
145 static int count = 0;
146 ++count;
147 static float currents[3] = {0.0f, 0.0f, 0.0f};
148 for (int ii = 0; ii < 3; ++ii) {
149 currents[ii] += static_cast<float>(adc_readings.motor_currents[ii]);
150 }
151
152 if (i == 0) {
153 printf(
154 "foo %d.0, %d.0, %d.0, %.3d %.3d %.3d, switching %d %d %d enc %d\n",
155 static_cast<int>(currents[0] / static_cast<float>(count)),
156 static_cast<int>(currents[1] / static_cast<float>(count)),
157 static_cast<int>(currents[2] / static_cast<float>(count)),
158 static_cast<int>(decoupled[0] * 1.0f),
159 static_cast<int>(decoupled[1] * 1.0f),
160 static_cast<int>(decoupled[2] * 1.0f),
161 global_motor.load(::std::memory_order_relaxed)->get_switching_points_cycles(0),
162 global_motor.load(::std::memory_order_relaxed)->get_switching_points_cycles(1),
163 global_motor.load(::std::memory_order_relaxed)->get_switching_points_cycles(2),
164 static_cast<int>(
165 global_motor.load(::std::memory_order_relaxed)->wrapped_encoder()));
166 count = 0;
167 currents[0] = 0.0f;
168 currents[1] = 0.0f;
169 currents[2] = 0.0f;
170 }
171#endif
172#if 1
173 constexpr float kAlpha = 0.995f;
174 constexpr float kFuseAlpha = 0.95f;
175
176 // 3400 - 760
James Kuszmaulb7707432018-10-07 14:48:11 -0700177 // Start the throttle filter at 1.0f--once it converges to near zero, we set
178 // throttle_zeroed to true and only then do we start listening to throttle
179 // commands.
180 static float filtered_throttle = 1.0f;
181 static bool throttle_zeroed = false;
James Kuszmaul998d3032018-09-08 15:41:41 -0700182 constexpr int kMaxThrottle = 3400;
183 constexpr int kMinThrottle = 760;
184 const float throttle = ::std::max(
185 0.0f,
186 ::std::min(1.0f,
187 static_cast<float>(static_cast<int>(adc_readings.throttle) -
188 kMinThrottle) /
189 static_cast<float>(kMaxThrottle - kMinThrottle)));
190
191 // y(n) = x(n) + a * (y(n-1) - x(n))
192 filtered_throttle = throttle + kAlpha * (filtered_throttle - throttle);
James Kuszmaulb7707432018-10-07 14:48:11 -0700193 if (::std::abs(filtered_throttle) < 1e-2f) {
194 // Once the filter gets near zero once, we start paying attention to it;
195 // once it gets near zero once, never ignore it again.
196 throttle_zeroed = true;
197 }
James Kuszmaul998d3032018-09-08 15:41:41 -0700198
199 const float fuse_voltage = static_cast<float>(adc_readings.fuse_voltage);
200 static float filtered_fuse_voltage = 0.0f;
201
202 filtered_fuse_voltage =
203 fuse_voltage + kFuseAlpha * (filtered_fuse_voltage - fuse_voltage);
204
205 const float velocity =
206 global_motor.load(::std::memory_order_relaxed)->estimated_velocity();
207 const float bemf = velocity / (static_cast<float>(Kv) / 1.5f);
208 const float abs_bemf = ::std::abs(bemf);
209 constexpr float kPeakCurrent = 300.0f;
Brian Silverman37a95d62018-11-09 16:08:32 -0800210 constexpr float kLimitedCurrent = 70.0f;
James Kuszmaul998d3032018-09-08 15:41:41 -0700211 const float max_bat_cur =
212 fuse_badness > (kLimitedCurrent * kLimitedCurrent * 0.95f)
213 ? kLimitedCurrent
214 : static_cast<float>(kIcc);
215 const float throttle_limit = ::std::min(
216 kPeakCurrent,
Philipp Schrader790cb542023-07-05 21:06:52 -0700217 (-abs_bemf +
218 ::std::sqrt(static_cast<float>(
219 bemf * bemf + 4.0f * static_cast<float>(kR) * 1.5f *
220 static_cast<float>(kVcc) * max_bat_cur))) /
James Kuszmaul998d3032018-09-08 15:41:41 -0700221 (2.0f * 1.5f * static_cast<float>(kR)));
222
James Kuszmaul521eb652018-10-17 19:09:33 -0700223 constexpr float kNegativeCurrent = 100.0f;
Philipp Schrader790cb542023-07-05 21:06:52 -0700224 float goal_current = -::std::min(
225 ::std::max(filtered_throttle * (kPeakCurrent + kNegativeCurrent) -
226 kNegativeCurrent,
227 -throttle_limit),
228 throttle_limit);
James Kuszmaul998d3032018-09-08 15:41:41 -0700229
James Kuszmaulb7707432018-10-07 14:48:11 -0700230 if (!throttle_zeroed) {
231 goal_current = 0.0f;
232 }
James Kuszmaula1d94c82018-10-10 20:00:09 -0700233 // Note: current reduction is 12/70 belt, 15 / 54 on chain, and 10 inch
234 // diameter wheels, so cutoff of 500 electrical rad/sec * 1 mechanical rad / 2
235 // erad * 12 / 70 * 15 / 54 * 0.127 m = 1.5m/s = 3.4 mph
James Kuszmaul998d3032018-09-08 15:41:41 -0700236 if (velocity > -500) {
237 if (goal_current > 0.0f) {
238 goal_current = 0.0f;
239 }
240 }
James Kuszmaul521eb652018-10-17 19:09:33 -0700241
Philipp Schrader790cb542023-07-05 21:06:52 -0700242 // float goal_current =
243 //-::std::min(filtered_throttle * kPeakCurrent, throttle_limit);
James Kuszmaul521eb652018-10-17 19:09:33 -0700244 const float overall_measured_current =
245 global_motor.load(::std::memory_order_relaxed)
246 ->overall_measured_current();
James Kuszmaul998d3032018-09-08 15:41:41 -0700247 const float fuse_current =
James Kuszmaul521eb652018-10-17 19:09:33 -0700248 overall_measured_current *
249 (bemf + overall_measured_current * static_cast<float>(kR) * 1.5f) /
James Kuszmaul998d3032018-09-08 15:41:41 -0700250 static_cast<float>(kVcc);
251 const int16_t fuse_current_10 = static_cast<int16_t>(10.0f * fuse_current);
252 fuse_badness += 0.00002f * (fuse_current * fuse_current - fuse_badness);
253
Philipp Schrader790cb542023-07-05 21:06:52 -0700254 global_motor.load(::std::memory_order_relaxed)->SetGoalCurrent(goal_current);
James Kuszmaul998d3032018-09-08 15:41:41 -0700255 global_motor.load(::std::memory_order_relaxed)
Austin Schuh54c8c842019-04-07 13:54:23 -0700256 ->CurrentInterrupt(balanced, wrapped_encoder);
James Kuszmaul998d3032018-09-08 15:41:41 -0700257
258 global_debug_buffer.count.fetch_add(1);
259
James Kuszmaul521eb652018-10-17 19:09:33 -0700260 const bool trigger = false && i > 10000;
James Kuszmaul998d3032018-09-08 15:41:41 -0700261 // global_debug_buffer.count.load(::std::memory_order_relaxed) >= 0;
262 size_t buffer_size =
263 global_debug_buffer.size.load(::std::memory_order_relaxed);
264 if ((buffer_size > 0 || trigger) &&
265 buffer_size != global_debug_buffer.samples.size()) {
266 global_debug_buffer.samples[buffer_size].currents[0] =
267 static_cast<int16_t>(balanced.readings[0] * 10.0f);
268 global_debug_buffer.samples[buffer_size].currents[1] =
269 static_cast<int16_t>(balanced.readings[1] * 10.0f);
270 global_debug_buffer.samples[buffer_size].currents[2] =
271 static_cast<int16_t>(balanced.readings[2] * 10.0f);
272 global_debug_buffer.samples[buffer_size].position =
273 global_motor.load(::std::memory_order_relaxed)->wrapped_encoder();
274 global_debug_buffer.samples[buffer_size].est_omega =
275 global_motor.load(::std::memory_order_relaxed)->estimated_velocity();
276 global_debug_buffer.samples[buffer_size].commands[0] =
Philipp Schrader790cb542023-07-05 21:06:52 -0700277 global_motor.load(::std::memory_order_relaxed)
278 ->get_switching_points_cycles(0);
James Kuszmaul998d3032018-09-08 15:41:41 -0700279 global_debug_buffer.samples[buffer_size].commands[1] =
Philipp Schrader790cb542023-07-05 21:06:52 -0700280 global_motor.load(::std::memory_order_relaxed)
281 ->get_switching_points_cycles(1);
James Kuszmaul998d3032018-09-08 15:41:41 -0700282 global_debug_buffer.samples[buffer_size].commands[2] =
Philipp Schrader790cb542023-07-05 21:06:52 -0700283 global_motor.load(::std::memory_order_relaxed)
284 ->get_switching_points_cycles(2);
James Kuszmaul998d3032018-09-08 15:41:41 -0700285 global_debug_buffer.samples[buffer_size].commanded_currents[0] =
286 global_motor.load(::std::memory_order_relaxed)->i_goal(0);
287 global_debug_buffer.samples[buffer_size].commanded_currents[1] =
288 global_motor.load(::std::memory_order_relaxed)->i_goal(1);
289 global_debug_buffer.samples[buffer_size].commanded_currents[2] =
290 global_motor.load(::std::memory_order_relaxed)->i_goal(2);
291 global_debug_buffer.samples[buffer_size].total_command =
292 global_motor.load(::std::memory_order_relaxed)->goal_current();
293 global_debug_buffer.samples[buffer_size].fuse_voltage =
294 filtered_fuse_voltage;
295 global_debug_buffer.samples[buffer_size].fuse_current = fuse_current_10;
296 global_debug_buffer.samples[buffer_size].driver_request =
297 ::std::max(filtered_throttle * (kPeakCurrent + kNegativeCurrent) -
298 kNegativeCurrent,
299 0.0f);
300 global_debug_buffer.samples[buffer_size].fuse_badness = fuse_badness;
Philipp Schrader790cb542023-07-05 21:06:52 -0700301 global_debug_buffer.samples[buffer_size].cycles_since_start =
302 cycles_since_start;
James Kuszmaul998d3032018-09-08 15:41:41 -0700303
304 global_debug_buffer.size.fetch_add(1);
305 }
306
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700307 ++i;
James Kuszmaul998d3032018-09-08 15:41:41 -0700308 if (buffer_size == global_debug_buffer.samples.size()) {
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700309 i = 0;
James Kuszmaul998d3032018-09-08 15:41:41 -0700310 GPIOC_PCOR = (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4);
311 GPIOD_PCOR = (1 << 4) | (1 << 5);
312
313 PERIPHERAL_BITBAND(GPIOC_PDDR, 1) = 1;
314 PERIPHERAL_BITBAND(GPIOC_PDDR, 2) = 1;
315 PERIPHERAL_BITBAND(GPIOC_PDDR, 3) = 1;
316 PERIPHERAL_BITBAND(GPIOC_PDDR, 4) = 1;
317 PERIPHERAL_BITBAND(GPIOD_PDDR, 4) = 1;
318 PERIPHERAL_BITBAND(GPIOD_PDDR, 5) = 1;
319
320 PORTC_PCR1 = PORT_PCR_DSE | PORT_PCR_MUX(1);
321 PORTC_PCR2 = PORT_PCR_DSE | PORT_PCR_MUX(1);
322 PORTC_PCR3 = PORT_PCR_DSE | PORT_PCR_MUX(1);
323 PORTC_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(1);
324 PORTD_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(1);
325 PORTD_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(1);
326 }
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700327#endif
328#else
James Kuszmaul521eb652018-10-17 19:09:33 -0700329 // Useful code when calculating resistance/inductance of motor
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700330 FTM0->SC &= ~FTM_SC_TOF;
331 FTM0->C0V = 0;
332 FTM0->C1V = 0;
333 FTM0->C2V = 0;
Brian Silverman37a95d62018-11-09 16:08:32 -0800334 FTM0->C3V = 20;
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700335 FTM0->C4V = 0;
Brian Silverman37a95d62018-11-09 16:08:32 -0800336 FTM0->C5V = 0;
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700337 FTM0->PWMLOAD = FTM_PWMLOAD_LDOK;
James Kuszmaul521eb652018-10-17 19:09:33 -0700338 (void)wrapped_encoder;
James Kuszmaul521eb652018-10-17 19:09:33 -0700339 size_t buffer_size =
340 global_debug_buffer.size.load(::std::memory_order_relaxed);
Brian Silverman37a95d62018-11-09 16:08:32 -0800341 // Setting this to true is helpful for calibrating inductance, and false is
342 // good for calibrating resistance.
343 constexpr bool start_immediately = true;
344 bool trigger = start_immediately || i > 20000;
James Kuszmaul521eb652018-10-17 19:09:33 -0700345 if ((trigger || buffer_size > 0) &&
346 buffer_size != global_debug_buffer.samples.size()) {
347 global_debug_buffer.samples[buffer_size].currents[0] =
348 static_cast<int16_t>(balanced.readings[0] * 10.0f);
349 global_debug_buffer.samples[buffer_size].currents[1] =
350 static_cast<int16_t>(balanced.readings[1] * 10.0f);
351 global_debug_buffer.samples[buffer_size].currents[2] =
352 static_cast<int16_t>(balanced.readings[2] * 10.0f);
353 global_debug_buffer.samples[buffer_size].commands[0] = FTM0->C1V;
354 global_debug_buffer.samples[buffer_size].commands[1] = FTM0->C3V;
355 global_debug_buffer.samples[buffer_size].commands[2] = FTM0->C5V;
Brian Silverman37a95d62018-11-09 16:08:32 -0800356 global_debug_buffer.samples[buffer_size].readings[0] =
357 adc_readings.motor_currents[0];
358 global_debug_buffer.samples[buffer_size].readings[1] =
359 adc_readings.motor_currents[1];
360 global_debug_buffer.samples[buffer_size].readings[2] =
361 adc_readings.motor_currents[2];
James Kuszmaul521eb652018-10-17 19:09:33 -0700362 global_debug_buffer.samples[buffer_size].position =
363 global_motor.load(::std::memory_order_relaxed)->wrapped_encoder();
364 global_debug_buffer.size.fetch_add(1);
365 }
366 if (buffer_size == global_debug_buffer.samples.size()) {
367 GPIOC_PCOR = (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4);
368 GPIOD_PCOR = (1 << 4) | (1 << 5);
369
370 PERIPHERAL_BITBAND(GPIOC_PDDR, 1) = 1;
371 PERIPHERAL_BITBAND(GPIOC_PDDR, 2) = 1;
372 PERIPHERAL_BITBAND(GPIOC_PDDR, 3) = 1;
373 PERIPHERAL_BITBAND(GPIOC_PDDR, 4) = 1;
374 PERIPHERAL_BITBAND(GPIOD_PDDR, 4) = 1;
375 PERIPHERAL_BITBAND(GPIOD_PDDR, 5) = 1;
376
377 PORTC_PCR1 = PORT_PCR_DSE | PORT_PCR_MUX(1);
378 PORTC_PCR2 = PORT_PCR_DSE | PORT_PCR_MUX(1);
379 PORTC_PCR3 = PORT_PCR_DSE | PORT_PCR_MUX(1);
380 PORTC_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(1);
381 PORTD_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(1);
382 PORTD_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(1);
383 i = 0;
384 }
385 ++i;
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700386#endif
James Kuszmaul998d3032018-09-08 15:41:41 -0700387}
388
389} // extern "C"
390
391void ConfigurePwmFtm(BigFTM *pwm_ftm) {
392 // Put them all into combine active-high mode, and all the low ones staying on
393 // all the time by default.
394 pwm_ftm->C0SC = FTM_CSC_ELSA;
395 pwm_ftm->C0V = 0;
396 pwm_ftm->C1SC = FTM_CSC_ELSA;
397 pwm_ftm->C1V = 0;
398 pwm_ftm->C2SC = FTM_CSC_ELSA;
399 pwm_ftm->C2V = 0;
400 pwm_ftm->C3SC = FTM_CSC_ELSA;
401 pwm_ftm->C3V = 0;
402 pwm_ftm->C4SC = FTM_CSC_ELSA;
403 pwm_ftm->C4V = 0;
404 pwm_ftm->C5SC = FTM_CSC_ELSA;
405 pwm_ftm->C5V = 0;
406 pwm_ftm->C6SC = FTM_CSC_ELSA;
407 pwm_ftm->C6V = 0;
408 pwm_ftm->C7SC = FTM_CSC_ELSA;
409 pwm_ftm->C7V = 0;
410
411 pwm_ftm->COMBINE = FTM_COMBINE_SYNCEN3 /* Synchronize updates usefully */ |
412 FTM_COMBINE_DTEN3 /* Enable deadtime */ |
413 FTM_COMBINE_COMP3 /* Make them complementary */ |
414 FTM_COMBINE_COMBINE3 /* Combine the channels */ |
415 FTM_COMBINE_SYNCEN2 /* Synchronize updates usefully */ |
416 FTM_COMBINE_DTEN2 /* Enable deadtime */ |
417 FTM_COMBINE_COMP2 /* Make them complementary */ |
418 FTM_COMBINE_COMBINE2 /* Combine the channels */ |
419 FTM_COMBINE_SYNCEN1 /* Synchronize updates usefully */ |
420 FTM_COMBINE_DTEN1 /* Enable deadtime */ |
421 FTM_COMBINE_COMP1 /* Make them complementary */ |
422 FTM_COMBINE_COMBINE1 /* Combine the channels */ |
423 FTM_COMBINE_SYNCEN0 /* Synchronize updates usefully */ |
424 FTM_COMBINE_DTEN0 /* Enable deadtime */ |
425 FTM_COMBINE_COMP0 /* Make them complementary */ |
426 FTM_COMBINE_COMBINE0 /* Combine the channels */;
427 // Safe state for all channels is low.
428 pwm_ftm->POL = 0;
429
430 // Set the deadtime.
431 pwm_ftm->DEADTIME =
432 FTM_DEADTIME_DTPS(0) /* Prescaler of 1 */ | FTM_DEADTIME_DTVAL(9);
433
434 pwm_ftm->CONF =
435 FTM_CONF_BDMMOD(1) /* Set everything to POLn during debug halt */;
436}
437
438// Zeros the encoder. This involves blocking for an arbitrary length of time
439// with interrupts disabled.
440void ZeroMotor() {
441#if 0
442 while (true) {
443 if (PERIPHERAL_BITBAND(GPIOB_PDIR, 11)) {
444 encoder_ftm_->CNT = 0;
445 break;
446 }
447 }
448#else
449 uint32_t scratch;
450 __disable_irq();
451 // Stuff all of this in an inline assembly statement so we can make sure the
452 // compiler doesn't decide sticking constant loads etc in the middle of
453 // the loop is a good idea, because that increases the latency of recognizing
454 // the index pulse edge which makes velocity affect the zeroing accuracy.
455 __asm__ __volatile__(
456 // A label to restart the loop.
457 "0:\n"
458 // Load the current PDIR value for the pin we care about.
459 "ldr %[scratch], [%[pdir_word]]\n"
460 // Terminate the loop if it's non-0.
461 "cbnz %[scratch], 1f\n"
462 // Go back around again.
463 "b 0b\n"
464 // A label to finish the loop.
465 "1:\n"
466 // Reset the count once we're down here. It doesn't actually matter what
467 // value we store because writing anything resets it to CNTIN (ie 0).
468 "str %[scratch], [%[cnt]]\n"
469 : [scratch] "=&l"(scratch)
Philipp Schrader790cb542023-07-05 21:06:52 -0700470 : [pdir_word] "l"(&PERIPHERAL_BITBAND(GPIOB_PDIR, 11)), [cnt] "l"(
471 &FTM1->CNT));
James Kuszmaul998d3032018-09-08 15:41:41 -0700472 __enable_irq();
473#endif
474}
475
476} // namespace
477
478extern "C" int main(void) {
479 // for background about this startup delay, please see these conversations
480 // https://forum.pjrc.com/threads/36606-startup-time-(400ms)?p=113980&viewfull=1#post113980
481 // https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273
482 delay(400);
483
484 // Set all interrupts to the second-lowest priority to start with.
485 for (int i = 0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_SANE_PRIORITY(i, 0xD);
486
487 // Now set priorities for all the ones we care about. They only have meaning
488 // relative to each other, which means centralizing them here makes it a lot
489 // more manageable.
490 NVIC_SET_SANE_PRIORITY(IRQ_FTM0, 0x3);
491 NVIC_SET_SANE_PRIORITY(IRQ_UART0_STATUS, 0xE);
492
493 // Set the LED's pin to output mode.
494 PERIPHERAL_BITBAND(GPIOC_PDDR, 5) = 1;
495 PORTC_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(1);
496
497#if 0
498 PERIPHERAL_BITBAND(GPIOA_PDDR, 15) = 1;
499 PORTA_PCR15 = PORT_PCR_DSE | PORT_PCR_MUX(1);
500#endif
501
502 // Set up the CAN pins.
503 PORTA_PCR12 = PORT_PCR_DSE | PORT_PCR_MUX(2);
504 PORTB_PCR19 = PORT_PCR_DSE | PORT_PCR_MUX(2);
505
506 DMA.CR = M_DMA_EMLM;
507
508 PORTB_PCR16 = PORT_PCR_DSE | PORT_PCR_MUX(3);
509 PORTB_PCR17 = PORT_PCR_DSE | PORT_PCR_MUX(3);
510 SIM_SCGC4 |= SIM_SCGC4_UART0;
Brian Silverman4787a6e2018-10-06 16:00:54 -0700511
512 PrintingParameters printing_parameters;
513 printing_parameters.stdout_uart_module = &UART0;
514 printing_parameters.stdout_uart_module_clock_frequency = F_CPU;
515 printing_parameters.stdout_uart_status_interrupt = IRQ_UART0_STATUS;
516 printing_parameters.dedicated_usb = true;
517 const ::std::unique_ptr<PrintingImplementation> printing =
518 CreatePrinting(printing_parameters);
519 printing->Initialize();
James Kuszmaul998d3032018-09-08 15:41:41 -0700520
521 AdcInitFet12();
522 MathInit();
523 delay(100);
524 can_init(0, 1);
525
526 MotorControlsImplementation controls;
527
528 delay(100);
529
530 // Index pin
531 PORTB_PCR11 = PORT_PCR_MUX(1);
532 // FTM1_QD_PH{A,B}
533 PORTB_PCR0 = PORT_PCR_MUX(6);
534 PORTB_PCR1 = PORT_PCR_MUX(6);
535
536 // FTM0_CH[0-5]
537 PORTC_PCR1 = PORT_PCR_DSE | PORT_PCR_MUX(4);
538 PORTC_PCR2 = PORT_PCR_DSE | PORT_PCR_MUX(4);
539 PORTC_PCR3 = PORT_PCR_DSE | PORT_PCR_MUX(4);
540 PORTC_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(4);
541 PORTD_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(4);
542 PORTD_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(4);
543
544 Motor motor(FTM0, FTM1, &controls, {&FTM0->C0V, &FTM0->C2V, &FTM0->C4V});
545 motor.set_encoder_offset(810);
546 motor.set_deadtime_compensation(9);
547 ConfigurePwmFtm(FTM0);
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700548
Brian Silvermana1d84822018-09-15 17:18:49 -0700549 // TODO(Brian): Figure out how to avoid duplicating this code to slave one FTM
550 // to another.
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700551 FTM2->CONF = FTM_CONF_GTBEEN;
552 FTM2->MODE = FTM_MODE_WPDIS;
553 FTM2->MODE = FTM_MODE_WPDIS | FTM_MODE_FTMEN;
554 FTM2->SC = FTM_SC_CLKS(0) /* Disable counting for now */;
555 FTM2->CNTIN = 0;
556 FTM2->CNT = 0;
557 // TODO(Brian): Don't duplicate this.
558 FTM2->MOD = BUS_CLOCK_FREQUENCY / SWITCHING_FREQUENCY;
559 FTM2->OUTINIT = 0;
560 // All of the channels are active high.
561 FTM2->POL = 0;
562 FTM2->SYNCONF = FTM_SYNCONF_HWWRBUF | FTM_SYNCONF_SWWRBUF |
563 FTM_SYNCONF_SWRSTCNT | FTM_SYNCONF_SYNCMODE;
564 // Don't want any intermediate loading points.
565 FTM2->PWMLOAD = 0;
566
567 // Need to set them to some kind of output mode so we can actually change
568 // them.
569 FTM2->C0SC = FTM_CSC_MSA;
570 FTM2->C1SC = FTM_CSC_MSA;
571
572 // This has to happen after messing with SYNCONF, and should happen after
573 // messing with various other things so the values can get flushed out of the
574 // buffers.
Philipp Schrader790cb542023-07-05 21:06:52 -0700575 FTM2->SYNC = FTM_SYNC_SWSYNC /* Flush everything out right now */ |
576 FTM_SYNC_CNTMAX /* Load new values at the end of the cycle */;
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700577 // Wait for the software synchronization to finish.
578 while (FTM2->SYNC & FTM_SYNC_SWSYNC) {
579 }
580 FTM2->SC = FTM_SC_CLKS(1) /* Use the system clock */ |
Philipp Schrader790cb542023-07-05 21:06:52 -0700581 FTM_SC_PS(0) /* Don't prescale the clock */;
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700582 // TODO:
Philipp Schrader790cb542023-07-05 21:06:52 -0700583 // FTM2->MODE &= ~FTM_MODE_WPDIS;
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700584
585 FTM2->EXTTRIG = FTM_EXTTRIG_CH0TRIG | FTM_EXTTRIG_CH1TRIG;
586
Brian Silvermana1d84822018-09-15 17:18:49 -0700587 // TODO(Brian): Don't duplicate the timer's MOD value.
588 teensy::AdcDmaSampler adc_dma{BUS_CLOCK_FREQUENCY / SWITCHING_FREQUENCY};
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700589 // ADC0_Dx0 is 1-0
590 // ADC0_Dx2 is 1-2
591 // ADC0_Dx3 is 2-0
592 // ADC1_Dx0 is 2-0
593 // ADC1_Dx3 is 1-0
594 // Sample 0: 1-2,2-0
595 // Sample 1: 1-2,1-0
596 // Sample 2: 1-0,2-0
597 // Sample 3: 23(SENSE0),18(VIN)
598 adc_dma.set_adc0_samples({V_ADC_ADCH(2) | M_ADC_DIFF,
599 V_ADC_ADCH(2) | M_ADC_DIFF,
600 V_ADC_ADCH(0) | M_ADC_DIFF, V_ADC_ADCH(23)});
601 adc_dma.set_adc1_samples({V_ADC_ADCH(0) | M_ADC_DIFF,
602 V_ADC_ADCH(3) | M_ADC_DIFF,
603 V_ADC_ADCH(0) | M_ADC_DIFF, V_ADC_ADCH(18)});
604 adc_dma.set_ftm_delays({&FTM2->C0V, &FTM2->C1V});
605 adc_dma.set_pdb_input(PDB_IN_FTM2);
606
607 adc_dma.Initialize();
608 FTM0->CONF = FTM_CONF_GTBEEN;
James Kuszmaul998d3032018-09-08 15:41:41 -0700609 motor.Init();
610 global_motor.store(&motor, ::std::memory_order_relaxed);
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700611 global_adc_dma.store(&adc_dma, ::std::memory_order_relaxed);
612
James Kuszmaul998d3032018-09-08 15:41:41 -0700613 // Output triggers to things like the PDBs on initialization.
614 FTM0_EXTTRIG = FTM_EXTTRIG_INITTRIGEN;
615 // Don't let any memory accesses sneak past here, because we actually
616 // need everything to be starting up.
Philipp Schrader790cb542023-07-05 21:06:52 -0700617 __asm__("" ::: "memory");
James Kuszmaul998d3032018-09-08 15:41:41 -0700618
619 // Give everything a chance to get going.
620 delay(100);
621
622 printf("Ram start: %p\n", __bss_ram_start__);
623 printf("Heap start: %p\n", __heap_start__);
624 printf("Heap end: %p\n", __brkval);
625 printf("Stack start: %p\n", __stack_end__);
626
627 printf("Going silent to zero motors...\n");
628 // Give the print a chance to make it out.
629 delay(100);
630 ZeroMotor();
631
632 motor.set_encoder_multiplier(-1);
633 motor.set_encoder_calibration_offset(
James Kuszmaul521eb652018-10-17 19:09:33 -0700634 364 /*from running constant phases*/ - 26 /*average offset from lstsq*/ -
635 14 /* compensation for going backwards */);
James Kuszmaul998d3032018-09-08 15:41:41 -0700636
637 printf("Zeroed motor!\n");
638 // Give stuff a chance to recover from interrupts-disabled.
639 delay(100);
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700640 adc_dma.Reset();
James Kuszmaul998d3032018-09-08 15:41:41 -0700641 motor.Start();
James Kuszmaul7c8aad62018-09-08 18:16:18 -0700642 // Now poke the GTB to actually start both timers.
643 FTM0->CONF = FTM_CONF_GTBEEN | FTM_CONF_GTBEOUT;
644
James Kuszmaul998d3032018-09-08 15:41:41 -0700645 NVIC_ENABLE_IRQ(IRQ_FTM0);
646 GPIOC_PSOR = 1 << 5;
647
Brian Silverman37a95d62018-11-09 16:08:32 -0800648 constexpr bool dump_full_sample = false;
James Kuszmaul521eb652018-10-17 19:09:33 -0700649 constexpr bool dump_resist_calib = false;
Brian Silverman37a95d62018-11-09 16:08:32 -0800650 constexpr bool repeat_calib = true;
James Kuszmaul998d3032018-09-08 15:41:41 -0700651 while (true) {
James Kuszmaul521eb652018-10-17 19:09:33 -0700652 if (dump_resist_calib || dump_full_sample) {
James Kuszmaul998d3032018-09-08 15:41:41 -0700653 PORTC_PCR1 = PORT_PCR_DSE | PORT_PCR_MUX(4);
654 PORTC_PCR2 = PORT_PCR_DSE | PORT_PCR_MUX(4);
655 PORTC_PCR3 = PORT_PCR_DSE | PORT_PCR_MUX(4);
656 PORTC_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(4);
657 PORTD_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(4);
658 PORTD_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(4);
659 motor.Reset();
660 }
661 global_debug_buffer.size.store(0);
662 global_debug_buffer.count.store(0);
663 while (global_debug_buffer.size.load(::std::memory_order_relaxed) <
664 global_debug_buffer.samples.size()) {
665 }
James Kuszmaul521eb652018-10-17 19:09:33 -0700666 if (dump_resist_calib) {
667 // Useful prints for when calibrating resistance/inductance of motor
668 for (size_t i = 0; i < global_debug_buffer.samples.size(); ++i) {
669 const auto &sample = global_debug_buffer.samples[i];
Brian Silverman37a95d62018-11-09 16:08:32 -0800670#if 1
Philipp Schrader790cb542023-07-05 21:06:52 -0700671 printf("%u, %d, %d, %d, %u, %u, %u, %u\n", i, sample.currents[0],
672 sample.currents[1], sample.currents[2], sample.commands[0],
673 sample.commands[1], sample.commands[2], sample.position);
Brian Silverman37a95d62018-11-09 16:08:32 -0800674#else
675 printf("%" PRIu16 ",%" PRIu16 ",%" PRIu16 ",%" PRId16 ",%" PRId16
676 ",%" PRId16 "\n",
677 sample.commands[0], sample.commands[1], sample.commands[2],
678 sample.readings[0], sample.readings[1], sample.readings[2]);
679#endif
James Kuszmaul521eb652018-10-17 19:09:33 -0700680 }
681 } else if (dump_full_sample) {
James Kuszmaul998d3032018-09-08 15:41:41 -0700682 printf("Dumping data\n");
683 for (size_t i = 0; i < global_debug_buffer.samples.size(); ++i) {
684 const auto &sample = global_debug_buffer.samples[i];
685
686 printf("%u, %d, %d, %d, %u, %u, %u, %u, %d, %d, %d, %d\n", i,
687 sample.currents[0], sample.currents[1], sample.currents[2],
688 sample.commands[0], sample.commands[1], sample.commands[2],
689 sample.position, static_cast<int>(sample.est_omega),
690 sample.commanded_currents[0], sample.commanded_currents[1],
691 sample.commanded_currents[2]);
692 }
693 printf("Done dumping data\n");
694 } else {
Philipp Schrader790cb542023-07-05 21:06:52 -0700695 // const auto &sample = global_debug_buffer.samples.back();
James Kuszmaul998d3032018-09-08 15:41:41 -0700696 const DebugBuffer::Sample sample = global_debug_buffer.samples[0];
697#if 1
698 printf("%" PRIu32
699 ", %d, %d, %d, %u, %u, %u, %u, %d, %d, %d, %d, %d, %d, %d\n",
700 sample.cycles_since_start, sample.currents[0], sample.currents[1],
701 sample.currents[2], sample.commands[0], sample.commands[1],
702 sample.commands[2], sample.position,
703 static_cast<int>(sample.est_omega), sample.commanded_currents[0],
704 sample.commanded_currents[1], sample.commanded_currents[2],
705 sample.total_command, static_cast<int>(sample.driver_request),
706 static_cast<int>(sample.fuse_badness));
707#else
708 printf("%d, %d\n", static_cast<int>(sample.fuse_voltage),
709 sample.fuse_current);
710#endif
711 }
Brian Silverman37a95d62018-11-09 16:08:32 -0800712 if (!repeat_calib) {
713 while (true) {
714 }
715 }
James Kuszmaul998d3032018-09-08 15:41:41 -0700716 }
717
718 return 0;
719}
720
721} // namespace motors
722} // namespace frc971