blob: 93f2a4dea9fb3c99c886b8cf17c74f95794ef21b [file] [log] [blame]
Brian Silverman8d3816a2017-07-03 18:52:15 -07001#include "motors/motor.h"
2
3#include <limits.h>
Brian Silverman19ea60f2018-01-03 21:43:15 -08004#include <stdio.h>
5#include <inttypes.h>
Brian Silverman8d3816a2017-07-03 18:52:15 -07006
7#include <array>
8
9#include "motors/peripheral/configuration.h"
Brian Silverman8d3816a2017-07-03 18:52:15 -070010#include "motors/peripheral/can.h"
11
Brian Silverman19ea60f2018-01-03 21:43:15 -080012extern "C" float analog_ratio(uint16_t reading);
Brian Silverman6260c092018-01-14 15:21:36 -080013extern "C" float absolute_wheel(uint16_t reading);
Brian Silverman19ea60f2018-01-03 21:43:15 -080014
Brian Silverman8d3816a2017-07-03 18:52:15 -070015namespace frc971 {
Brian Silvermana96c1a42018-05-12 12:11:31 -070016namespace motors {
Brian Silverman8d3816a2017-07-03 18:52:15 -070017
Brian Silverman19ea60f2018-01-03 21:43:15 -080018Motor::Motor(BigFTM *pwm_ftm, LittleFTM *encoder_ftm, MotorControls *controls,
19 const ::std::array<volatile uint32_t *, 3> &output_registers)
20 : pwm_ftm_(pwm_ftm),
21 encoder_ftm_(encoder_ftm),
22 controls_(controls),
23 output_registers_(output_registers) {
Brian Silverman8d3816a2017-07-03 18:52:15 -070024 // PWMSYNC doesn't matter because we set SYNCMODE down below.
25 pwm_ftm_->MODE = FTM_MODE_WPDIS;
26 pwm_ftm_->MODE = FTM_MODE_WPDIS | FTM_MODE_FTMEN;
27 encoder_ftm_->MODE = FTM_MODE_WPDIS;
28 encoder_ftm_->MODE = FTM_MODE_WPDIS | FTM_MODE_FTMEN;
29
30 pwm_ftm_->SC = FTM_SC_CLKS(0) /* Disable counting for now */;
31 encoder_ftm_->SC =
32 FTM_SC_CLKS(1) /* Use the system clock (not sure it matters) */ |
33 FTM_SC_PS(0) /* Don't prescale the clock (not sure it matters) */;
Brian Silverman19ea60f2018-01-03 21:43:15 -080034}
Brian Silverman8d3816a2017-07-03 18:52:15 -070035
Brian Silverman19ea60f2018-01-03 21:43:15 -080036static_assert((BUS_CLOCK_FREQUENCY % SWITCHING_FREQUENCY) == 0,
37 "Switching frequency needs to divide the bus clock frequency");
38
39static_assert(BUS_CLOCK_FREQUENCY / SWITCHING_FREQUENCY < UINT16_MAX,
40 "Need to prescale");
41
42void Motor::Init() {
Brian Silverman8d3816a2017-07-03 18:52:15 -070043 pwm_ftm_->CNTIN = encoder_ftm_->CNTIN = 0;
44 pwm_ftm_->CNT = encoder_ftm_->CNT = 0;
45
Brian Silverman19ea60f2018-01-03 21:43:15 -080046 pwm_ftm_->MOD = counts_per_cycle() - 1;
47 encoder_ftm_->MOD = controls_->mechanical_counts_per_revolution() - 1;
Brian Silverman8d3816a2017-07-03 18:52:15 -070048
49 // I think you have to set this to something other than 0 for the quadrature
50 // encoder mode to actually work? This is "input capture on rising edge only",
51 // which should be fine.
52 encoder_ftm_->C0SC = FTM_CSC_ELSA;
53 encoder_ftm_->C1SC = FTM_CSC_ELSA;
54
55 // Initialize all the channels to 0.
Brian Silverman8d3816a2017-07-03 18:52:15 -070056 pwm_ftm_->OUTINIT = 0;
57
Brian Silverman19ea60f2018-01-03 21:43:15 -080058 // All of the channels are active high (low-side ones with separate high/low
59 // drives are defaulted on elsewhere).
Brian Silverman8d3816a2017-07-03 18:52:15 -070060 pwm_ftm_->POL = 0;
61
62 encoder_ftm_->FILTER = FTM_FILTER_CH0FVAL(0) /* No filter */ |
63 FTM_FILTER_CH1FVAL(0) /* No filter */;
64
65 // Could set PHAFLTREN and PHBFLTREN here to enable the filters.
66 encoder_ftm_->QDCTRL = FTM_QDCTRL_QUADEN;
67
68 pwm_ftm_->SYNCONF =
69 FTM_SYNCONF_HWWRBUF /* Hardware trigger flushes switching points */ |
70 FTM_SYNCONF_SWWRBUF /* Software trigger flushes switching points */ |
71 FTM_SYNCONF_SWRSTCNT /* Software trigger resets the count */ |
72 FTM_SYNCONF_SYNCMODE /* Use the new synchronization mode */;
73 encoder_ftm_->SYNCONF =
74 FTM_SYNCONF_SWWRBUF /* Software trigger flushes MOD */ |
75 FTM_SYNCONF_SWRSTCNT /* Software trigger resets the count */ |
76 FTM_SYNCONF_SYNCMODE /* Use the new synchronization mode */;
77
78 // Don't want any intermediate loading points.
79 pwm_ftm_->PWMLOAD = 0;
80
81 // This has to happen after messing with SYNCONF, and should happen after
82 // messing with various other things so the values can get flushed out of the
83 // buffers.
84 pwm_ftm_->SYNC =
85 FTM_SYNC_SWSYNC /* Flush everything out right now */ |
86 FTM_SYNC_CNTMAX /* Load new values at the end of the cycle */;
87 encoder_ftm_->SYNC = FTM_SYNC_SWSYNC /* Flush everything out right now */;
88
89 // Wait for the software synchronization to finish.
90 while (pwm_ftm_->SYNC & FTM_SYNC_SWSYNC) {
91 }
92 while (encoder_ftm_->SYNC & FTM_SYNC_SWSYNC) {
93 }
94}
95
Brian Silverman8d3816a2017-07-03 18:52:15 -070096void Motor::Start() {
97 pwm_ftm_->SC = FTM_SC_TOIE /* Interrupt on overflow */ |
98 FTM_SC_CLKS(1) /* Use the system clock */ |
99 FTM_SC_PS(0) /* Don't prescale the clock */;
100 pwm_ftm_->MODE &= ~FTM_MODE_WPDIS;
101 encoder_ftm_->MODE &= ~FTM_MODE_WPDIS;
Brian Silverman8d3816a2017-07-03 18:52:15 -0700102}
103
Brian Silverman19ea60f2018-01-03 21:43:15 -0800104#define USE_ABSOLUTE_CUTOFF 0
105#define DO_CONTROLS 1
106#define DO_FIXED_PULSE 0
Brian Silverman8d3816a2017-07-03 18:52:15 -0700107
Brian Silverman19ea60f2018-01-03 21:43:15 -0800108#define USE_CUTOFF 1
109#define PRINT_READINGS 0
110#define PRINT_ALL_READINGS 0
111#define TAKE_SAMPLE 0
112#define SAMPLE_UNTIL_DONE 0
113#define DO_STEP_RESPONSE 0
114#define DO_PULSE_SWEEP 0
115#define PRINT_TIMING 0
Brian Silverman8d3816a2017-07-03 18:52:15 -0700116
Brian Silverman19ea60f2018-01-03 21:43:15 -0800117void Motor::HandleInterrupt(const BalancedReadings &balanced,
118 uint32_t captured_wrapped_encoder) {
119 pwm_ftm_->SC &= ~FTM_SC_TOF;
Brian Silverman8d3816a2017-07-03 18:52:15 -0700120
Brian Silverman19ea60f2018-01-03 21:43:15 -0800121#if PRINT_TIMING
122 const uint32_t start_nanos = nanos();
Brian Silverman8d3816a2017-07-03 18:52:15 -0700123#endif
124
Brian Silverman19ea60f2018-01-03 21:43:15 -0800125 if (!time_after(time_add(last_current_set_time_, 100000), micros())) {
126 goal_current_ = 0;
127 }
128
Brian Silverman8d3816a2017-07-03 18:52:15 -0700129#if DO_CONTROLS
Brian Silverman8d3816a2017-07-03 18:52:15 -0700130 const ::std::array<uint32_t, 3> switching_points =
Brian Silverman19ea60f2018-01-03 21:43:15 -0800131 controls_->DoIteration(balanced.readings, captured_wrapped_encoder, goal_current_);
132#if USE_CUTOFF
133 //constexpr uint32_t kMax = 2945;
134 constexpr uint32_t kMax = 1445;
Brian Silverman8d3816a2017-07-03 18:52:15 -0700135 static bool done = false;
136 bool done_now = false;
137 if (switching_points[0] > kMax || switching_points[1] > kMax ||
138 switching_points[2] > kMax) {
139 done_now = true;
140 }
Brian Silverman8d3816a2017-07-03 18:52:15 -0700141#if USE_ABSOLUTE_CUTOFF
142 static unsigned int current_done_count = 0;
143 bool current_done = false;
144 for (int phase = 0; phase < 3; ++phase) {
145 const float scaled_reading =
Brian Silverman19ea60f2018-01-03 21:43:15 -0800146 controls_->scale_current_reading(balanced.readings[phase]);
Brian Silverman8d3816a2017-07-03 18:52:15 -0700147 static constexpr float kMaxBalancedCurrent = 190.0f;
148 if (scaled_reading > kMaxBalancedCurrent ||
149 scaled_reading < -kMaxBalancedCurrent) {
150 current_done = true;
151 }
152 }
153 if (current_done) {
154 if (current_done_count > 5) {
155 done_now = true;
156 }
157 ++current_done_count;
158 } else {
159 current_done_count = 0;
160 }
161#endif
162 if (done_now && !done) {
163 printf("done now\n");
164 printf("switching_points %" PRIu32 " %" PRIu32 " %" PRIu32 "\n",
165 switching_points[0], switching_points[1], switching_points[2]);
Brian Silverman8d3816a2017-07-03 18:52:15 -0700166 printf("balanced %" PRIu16 " %" PRIu16 " %" PRIu16 "\n",
167 static_cast<uint16_t>(balanced.readings[0]),
168 static_cast<uint16_t>(balanced.readings[1]),
169 static_cast<uint16_t>(balanced.readings[2]));
170 done = true;
171 }
172 if (!done) {
Brian Silverman19ea60f2018-01-03 21:43:15 -0800173#else
174 if (true) {
175#endif
176 output_registers_[0][0] = CalculateOnTime(switching_points[0]);
177 output_registers_[0][2] = CalculateOffTime(switching_points[0]);
178 output_registers_[1][0] = CalculateOnTime(switching_points[1]);
179 output_registers_[1][2] = CalculateOffTime(switching_points[1]);
180 output_registers_[2][0] = CalculateOnTime(switching_points[2]);
181 output_registers_[2][2] = CalculateOffTime(switching_points[2]);
Brian Silverman8d3816a2017-07-03 18:52:15 -0700182 flip_time_offset_ = !flip_time_offset_;
183 } else {
Brian Silverman19ea60f2018-01-03 21:43:15 -0800184 output_registers_[0][0] = 0;
185 output_registers_[0][2] = 0;
186 output_registers_[1][0] = 0;
187 output_registers_[1][2] = 0;
188 output_registers_[2][0] = 0;
189 output_registers_[2][2] = 0;
Brian Silverman8d3816a2017-07-03 18:52:15 -0700190 }
Brian Silverman8d3816a2017-07-03 18:52:15 -0700191#elif DO_FIXED_PULSE
Brian Silverman19ea60f2018-01-03 21:43:15 -0800192 // Step through all the phases one by one in a loop. This should slowly move
193 // the trigger.
194 // If we fire phase 1, we should go to 0 radians.
195 // If we fire phase 2, we should go to -2.0 * PI / 3.0 radians.
196 // If we fire phase 3, we should go to 2.0 * PI / 3.0 radians.
197 // These numbers were confirmed by the python motor simulation.
Brian Silverman6260c092018-01-14 15:21:36 -0800198 static int phase_to_fire_count = -300000;
Brian Silverman19ea60f2018-01-03 21:43:15 -0800199 static int phase_to_fire = 0;
200 ++phase_to_fire_count;
201 if (phase_to_fire_count > 200000) {
202 phase_to_fire_count = 0;
203 ++phase_to_fire;
204 if (phase_to_fire > 2) {
205 phase_to_fire = 0;
206 }
Brian Silverman8d3816a2017-07-03 18:52:15 -0700207 }
Brian Silverman19ea60f2018-01-03 21:43:15 -0800208
209 // An on-width of 60 with 30V in means about 50A through the motor and about
210 // 30W total power dumped by the motor for the big one.
211 // For the small one, an on-width of 120/3000 with 14V in means about 2A
212 // through the motor.
213 constexpr int kPhaseFireWidth = 60;
214 output_registers_[0][0] = 0;
215 output_registers_[0][2] = phase_to_fire == 0 ? kPhaseFireWidth : 0;
216 output_registers_[1][0] = 0;
217 output_registers_[1][2] = phase_to_fire == 1 ? kPhaseFireWidth : 0;
218 output_registers_[2][0] = 0;
219 output_registers_[2][2] = phase_to_fire == 2 ? kPhaseFireWidth : 0;
Brian Silverman8d3816a2017-07-03 18:52:15 -0700220#endif
Brian Silverman6260c092018-01-14 15:21:36 -0800221 (void)balanced;
222 (void)captured_wrapped_encoder;
Brian Silverman8d3816a2017-07-03 18:52:15 -0700223#if PRINT_READINGS
224 static int i = 0;
Brian Silverman19ea60f2018-01-03 21:43:15 -0800225 if (i == 1000) {
Brian Silverman8d3816a2017-07-03 18:52:15 -0700226 i = 0;
Brian Silverman19ea60f2018-01-03 21:43:15 -0800227 SmallInitReadings readings;
228 {
229 DisableInterrupts disable_interrupts;
230 readings = AdcReadSmallInit(disable_interrupts);
231 }
Brian Silverman6260c092018-01-14 15:21:36 -0800232 //printf(
233 //"enc %" PRIu32 " %d %d\n", captured_wrapped_encoder,
234 //static_cast<int>((1.0f - analog_ratio(readings.motor1_abs)) * 7000.0f),
235 //static_cast<int>(captured_wrapped_encoder * 7.0f / 4096.0f * 1000.0f));
236 float wheel_position = absolute_wheel(analog_ratio(readings.wheel_abs));
237
Brian Silverman19ea60f2018-01-03 21:43:15 -0800238 printf(
Brian Silverman6260c092018-01-14 15:21:36 -0800239 "ecnt %" PRIu32 " arev:%d erev:%d %d\n", captured_wrapped_encoder,
240 static_cast<int>((analog_ratio(readings.motor1_abs)) * 7000.0f),
241 static_cast<int>(captured_wrapped_encoder * 7.0f / 4096.0f * 1000.0f),
242 static_cast<int>(wheel_position * 1000.0f));
Brian Silverman19ea60f2018-01-03 21:43:15 -0800243 } else if (i == 200) {
Brian Silverman6260c092018-01-14 15:21:36 -0800244#if DO_CONTROLS
Brian Silverman19ea60f2018-01-03 21:43:15 -0800245 printf("out %" PRIu32 " %" PRIu32 " %" PRIu32 "\n", switching_points[0],
246 switching_points[1], switching_points[2]);
247#else
248 printf("out %" PRIu32 " %" PRIu32 " %" PRIu32 "\n", output_registers_[0][2],
249 output_registers_[1][2], output_registers_[2][2]);
250#endif
251 //printf("0=%f\n", (double)balanced.readings[0]);
252 } else if (i == 400) {
253 //printf("1=%f\n", (double)balanced.readings[1]);
254 } else if (i == 600) {
255 //printf("2=%f\n", (double)balanced.readings[2]);
Brian Silverman8d3816a2017-07-03 18:52:15 -0700256 } else {
257 //printf("%" PRIu32 " to %" PRIu32 "\n", start_count, end_count);
258 }
259 ++i;
Brian Silverman8d3816a2017-07-03 18:52:15 -0700260#elif PRINT_ALL_READINGS
261 printf("ref=%" PRIu16 " 0.0=%" PRIu16 " 1.0=%" PRIu16 " 2.0=%" PRIu16
262 " in=%" PRIu16 " 0.1=%" PRIu16 " 1.1=%" PRIu16 " 2.1=%" PRIu16 "\n",
263 adc_readings.motor_current_ref, adc_readings.motor_currents[0][0],
264 adc_readings.motor_currents[1][0], adc_readings.motor_currents[2][0],
265 adc_readings.input_voltage, adc_readings.motor_currents[0][1],
266 adc_readings.motor_currents[1][1], adc_readings.motor_currents[2][1]);
Brian Silverman8d3816a2017-07-03 18:52:15 -0700267#elif TAKE_SAMPLE
268#if 0
269 constexpr int kStartupWait = 50000;
270#elif 0
271 constexpr int kStartupWait = 0;
272#elif 0
273 constexpr int kStartupWait = 30000;
274#elif 1
275 constexpr int kStartupWait = 2 * 20000;
276#endif
277 constexpr int kSubsampling = 1;
Brian Silverman19ea60f2018-01-03 21:43:15 -0800278 //constexpr int kPoints = 5000;
279 constexpr int kPoints = 1000;
Brian Silverman8d3816a2017-07-03 18:52:15 -0700280 constexpr int kSamplingEnd = kStartupWait + kPoints * kSubsampling;
281 (void)kSamplingEnd;
282 static int j = 0;
283 static int16_t data[kPoints][11];
284 static int written = 0;
Brian Silverman19ea60f2018-01-03 21:43:15 -0800285 static bool done_writing = false;
Brian Silverman8d3816a2017-07-03 18:52:15 -0700286 static_assert((kStartupWait % kSubsampling) == 0, "foo");
287 static_assert((kPoints % kSubsampling) == 0, "foo");
288 if (j < kStartupWait) {
289 // Wait to be started up.
290 ++j;
Brian Silverman19ea60f2018-01-03 21:43:15 -0800291#if SAMPLE_UNTIL_DONE
Brian Silverman8d3816a2017-07-03 18:52:15 -0700292 } else if (!done) {
Brian Silverman19ea60f2018-01-03 21:43:15 -0800293#else
294 } else if (j < kSamplingEnd && (j % kSubsampling) == 0) {
Brian Silverman8d3816a2017-07-03 18:52:15 -0700295#endif
296 {
297 const int index = ((j - kStartupWait) / kSubsampling) % kPoints;
Brian Silverman19ea60f2018-01-03 21:43:15 -0800298 auto &point = data[index];
Brian Silverman8d3816a2017-07-03 18:52:15 -0700299#if 0
300 point[0] = adc_readings.motor_currents[0][0];
301 point[1] = adc_readings.motor_currents[1][0];
302 point[2] = adc_readings.motor_currents[2][0];
303 point[3] = adc_readings.motor_currents[0][1];
304 point[4] = adc_readings.motor_currents[1][1];
305 point[5] = adc_readings.motor_currents[2][1];
306#else
Brian Silverman19ea60f2018-01-03 21:43:15 -0800307 point[0] = balanced.readings[0];
308 point[1] = balanced.readings[1];
309 point[2] = balanced.readings[2];
Brian Silverman8d3816a2017-07-03 18:52:15 -0700310#if 1
311 point[3] = controls_->Debug(0);
312 point[4] = controls_->Debug(1);
313 point[5] = controls_->Debug(2);
314 point[6] = controls_->Debug(3);
315 point[7] = controls_->Debug(4);
316 point[8] = controls_->Debug(5);
317 point[9] = controls_->Debug(6);
318 point[10] = controls_->Debug(7);
319#else
Brian Silverman19ea60f2018-01-03 21:43:15 -0800320#if 0
Brian Silverman8d3816a2017-07-03 18:52:15 -0700321 point[3] = adc_readings.motor_currents[0][0];
322 point[4] = adc_readings.motor_currents[1][0];
323 point[5] = adc_readings.motor_currents[2][0];
324 point[6] = adc_readings.motor_currents[0][1];
325 point[7] = adc_readings.motor_currents[1][1];
326 point[8] = adc_readings.motor_currents[2][1];
Brian Silverman19ea60f2018-01-03 21:43:15 -0800327#else
328 point[3] = 0;
329 point[4] = 0;
330 point[5] = 0;
331 point[6] = 0;
332 point[7] = 0;
333 point[8] = 0;
Brian Silverman8d3816a2017-07-03 18:52:15 -0700334#endif
Brian Silverman19ea60f2018-01-03 21:43:15 -0800335 point[9] = pwm_ftm_->C2V;
336 point[10] = pwm_ftm_->C3V;
337#endif
Brian Silverman8d3816a2017-07-03 18:52:15 -0700338#if 0
339 point[3] = pwm_ftm_->C1V - pwm_ftm_->C0V;
340 point[4] = pwm_ftm_->C3V - pwm_ftm_->C2V;
341 point[5] = pwm_ftm_->C5V - pwm_ftm_->C4V;
342#endif
343#endif
Brian Silverman19ea60f2018-01-03 21:43:15 -0800344 point[9] = captured_wrapped_encoder;
345 SmallInitReadings readings;
346 {
347 DisableInterrupts disable_interrupts;
348 readings = AdcReadSmallInit(disable_interrupts);
349 }
350 point[10] = readings.motor0_abs;
Brian Silverman8d3816a2017-07-03 18:52:15 -0700351 }
352
Brian Silverman8d3816a2017-07-03 18:52:15 -0700353#if DO_STEP_RESPONSE
354 // Step response
Brian Silverman19ea60f2018-01-03 21:43:15 -0800355 if (j > kStartupWait + 200 / kSubsampling) {
356 pwm_ftm_->C3V = 240;
Brian Silverman8d3816a2017-07-03 18:52:15 -0700357 }
Brian Silverman8d3816a2017-07-03 18:52:15 -0700358#elif DO_PULSE_SWEEP
359 // Sweep the pulse through the ADC sampling points.
360 static constexpr int kMax = 2500;
361 static constexpr int kExtraWait = 1500;
362 if (j > kStartupWait && j < kStartupWait + kExtraWait) {
Brian Silverman19ea60f2018-01-03 21:43:15 -0800363 pwm_ftm_->C2V = 0;
364 pwm_ftm_->C3V = 240;
Brian Silverman8d3816a2017-07-03 18:52:15 -0700365 } else if (j < kStartupWait + kMax + kExtraWait) {
366 uint32_t start = j - kStartupWait - kExtraWait;
Brian Silverman19ea60f2018-01-03 21:43:15 -0800367 pwm_ftm_->C2V = start;
368 pwm_ftm_->C3V = start + 240;
Brian Silverman8d3816a2017-07-03 18:52:15 -0700369 } else {
Brian Silverman19ea60f2018-01-03 21:43:15 -0800370 pwm_ftm_->C2V = 0;
371 pwm_ftm_->C3V = 0;
Brian Silverman8d3816a2017-07-03 18:52:15 -0700372 }
373#endif
374
375 ++j;
Brian Silverman19ea60f2018-01-03 21:43:15 -0800376#if SAMPLE_UNTIL_DONE
377 } else if (false) {
378#else
Brian Silverman8d3816a2017-07-03 18:52:15 -0700379 } else if (j < kSamplingEnd) {
380 ++j;
381 } else if (j == kSamplingEnd) {
Brian Silverman8d3816a2017-07-03 18:52:15 -0700382#endif
383 printf("finished\n");
384 ++j;
Brian Silverman19ea60f2018-01-03 21:43:15 -0800385#if SAMPLE_UNTIL_DONE
Brian Silverman8d3816a2017-07-03 18:52:15 -0700386 } else if (done) {
Brian Silverman19ea60f2018-01-03 21:43:15 -0800387#else
388 } else {
Brian Silverman8d3816a2017-07-03 18:52:15 -0700389#endif
390 // Time to write the data out.
Brian Silverman19ea60f2018-01-03 21:43:15 -0800391 if (written < (int)sizeof(data) && debug_tty_ != nullptr) {
Brian Silverman8d3816a2017-07-03 18:52:15 -0700392 int to_write = sizeof(data) - written;
Brian Silverman19ea60f2018-01-03 21:43:15 -0800393 if (to_write > 64) {
394 to_write = 64;
Brian Silverman8d3816a2017-07-03 18:52:15 -0700395 }
Brian Silverman19ea60f2018-01-03 21:43:15 -0800396 int result = debug_tty_->Write(((const char *)data) + written, to_write);
Brian Silverman8d3816a2017-07-03 18:52:15 -0700397 if (result >= 0) {
Brian Silverman19ea60f2018-01-03 21:43:15 -0800398 written += result;
Brian Silverman8d3816a2017-07-03 18:52:15 -0700399 } else {
Brian Silverman19ea60f2018-01-03 21:43:15 -0800400 printf("error\n");
Brian Silverman8d3816a2017-07-03 18:52:15 -0700401 }
Brian Silverman19ea60f2018-01-03 21:43:15 -0800402 }
403 if (!done_writing && written >= (int)sizeof(data) &&
404 debug_tty_->write_queue_empty()) {
405 printf("done writing %d\n", written);
406 done_writing = true;
Brian Silverman8d3816a2017-07-03 18:52:15 -0700407 }
408 }
409#endif
Brian Silverman8d3816a2017-07-03 18:52:15 -0700410 (void)balanced;
411
412 // Tell the hardware to use the new switching points.
Brian Silverman19ea60f2018-01-03 21:43:15 -0800413 // TODO(Brian): Somehow verify that we consistently hit the first or second
414 // timer-cycle with the new values (if there's two).
Brian Silverman8d3816a2017-07-03 18:52:15 -0700415 pwm_ftm_->PWMLOAD = FTM_PWMLOAD_LDOK;
416
Brian Silverman19ea60f2018-01-03 21:43:15 -0800417#if PRINT_TIMING
418 static int print_timing_count = 0;
419 static uint32_t print_timing_total = 0;
420 print_timing_total += time_subtract(nanos(), start_nanos);
421 if (++print_timing_count == 1000) {
422 printf("took %" PRIu32 "/%d\n", print_timing_total, print_timing_count);
423 print_timing_count = 0;
424 print_timing_total = 0;
425 }
426#endif
427
Brian Silverman8d3816a2017-07-03 18:52:15 -0700428 // If another cycle has already started, turn the light on right now.
429 if (pwm_ftm_->SC & FTM_SC_TOF) {
430 GPIOC_PSOR = 1 << 5;
431 }
432}
433
Brian Silverman19ea60f2018-01-03 21:43:15 -0800434uint32_t Motor::CalculateOnTime(uint32_t width) const {
435 if (width > 0) {
436 width += deadtime_compensation_;
437 if (flip_time_offset_) {
438 width += 1;
439 }
440 }
441 return (counts_per_cycle() - width) / 2;
442}
443
444uint32_t Motor::CalculateOffTime(uint32_t width) const {
445 if (width > 0) {
446 width += deadtime_compensation_;
447 if (!flip_time_offset_) {
448 width += 1;
449 }
450 }
451 return (counts_per_cycle() + width) / 2;
452}
453
Brian Silvermana96c1a42018-05-12 12:11:31 -0700454} // namespace motors
Brian Silverman8d3816a2017-07-03 18:52:15 -0700455} // namespace frc971