James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 1 | #include "motors/core/kinetis.h" |
| 2 | |
| 3 | #include <inttypes.h> |
| 4 | #include <stdio.h> |
| 5 | |
| 6 | #include <atomic> |
| 7 | |
| 8 | #include "motors/core/time.h" |
| 9 | #include "motors/fet12/current_equalization.h" |
| 10 | #include "motors/fet12/motor_controls.h" |
| 11 | #include "motors/motor.h" |
| 12 | #include "motors/peripheral/adc.h" |
James Kuszmaul | 7c8aad6 | 2018-09-08 18:16:18 -0700 | [diff] [blame] | 13 | #include "motors/peripheral/adc_dma.h" |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 14 | #include "motors/peripheral/can.h" |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame] | 15 | #include "motors/print/print.h" |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 16 | #include "motors/util.h" |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 17 | |
| 18 | namespace frc971 { |
| 19 | namespace motors { |
| 20 | namespace { |
| 21 | |
| 22 | constexpr double Kv = 22000.0 * 2.0 * M_PI / 60.0 / 30.0 * 3.6; |
| 23 | constexpr double kVcc = 31.5; |
| 24 | constexpr double kIcc = 125.0; |
| 25 | constexpr double kR = 0.0084; |
| 26 | |
| 27 | struct Fet12AdcReadings { |
James Kuszmaul | 7c8aad6 | 2018-09-08 18:16:18 -0700 | [diff] [blame] | 28 | // Averages of the pairs of ADC DMA channels corresponding with each channel |
| 29 | // pair. Individual values in motor_currents correspond to current sensor |
| 30 | // values, rather than the actual currents themselves (and so they still need |
| 31 | // to be decoupled). |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 32 | int16_t motor_currents[3]; |
| 33 | int16_t throttle, fuse_voltage; |
| 34 | }; |
| 35 | |
| 36 | void AdcInitFet12() { |
| 37 | AdcInitCommon(AdcChannels::kB, AdcChannels::kA); |
| 38 | |
| 39 | // M_CH0V ADC0_SE5b |
| 40 | PORTD_PCR1 = PORT_PCR_MUX(0); |
| 41 | |
| 42 | // M_CH1V ADC0_SE7b |
| 43 | PORTD_PCR6 = PORT_PCR_MUX(0); |
| 44 | |
| 45 | // M_CH2V ADC0_SE14 |
| 46 | PORTC_PCR0 = PORT_PCR_MUX(0); |
| 47 | |
| 48 | // M_CH0F ADC1_SE5a |
| 49 | PORTE_PCR1 = PORT_PCR_MUX(0); |
| 50 | |
| 51 | // M_CH1F ADC1_SE6a |
| 52 | PORTE_PCR2 = PORT_PCR_MUX(0); |
| 53 | |
| 54 | // M_CH2F ADC1_SE7a |
| 55 | PORTE_PCR3 = PORT_PCR_MUX(0); |
| 56 | |
| 57 | // SENSE0 ADC0_SE23 |
| 58 | // dedicated |
| 59 | |
| 60 | // SENSE1 ADC0_SE13 |
| 61 | PORTB_PCR3 = PORT_PCR_MUX(0); |
| 62 | } |
| 63 | |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 64 | ::std::atomic<Motor *> global_motor{nullptr}; |
James Kuszmaul | 7c8aad6 | 2018-09-08 18:16:18 -0700 | [diff] [blame] | 65 | ::std::atomic<teensy::AdcDmaSampler *> global_adc_dma{nullptr}; |
| 66 | |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 67 | extern "C" { |
| 68 | |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 69 | void *__stack_chk_guard = (void *)0x67111971; |
| 70 | void __stack_chk_fail(void) { |
| 71 | while (true) { |
| 72 | GPIOC_PSOR = (1 << 5); |
| 73 | printf("Stack corruption detected\n"); |
| 74 | delay(1000); |
| 75 | GPIOC_PCOR = (1 << 5); |
| 76 | delay(1000); |
| 77 | } |
| 78 | } |
| 79 | |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 80 | extern char *__brkval; |
| 81 | extern uint32_t __bss_ram_start__[]; |
| 82 | extern uint32_t __heap_start__[]; |
| 83 | extern uint32_t __stack_end__[]; |
| 84 | |
| 85 | struct DebugBuffer { |
| 86 | struct Sample { |
| 87 | ::std::array<int16_t, 3> currents; |
| 88 | ::std::array<int16_t, 3> commanded_currents; |
| 89 | ::std::array<uint16_t, 3> commands; |
| 90 | 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 | |
| 112 | DebugBuffer global_debug_buffer; |
| 113 | |
| 114 | void ftm0_isr(void) { |
James Kuszmaul | 7c8aad6 | 2018-09-08 18:16:18 -0700 | [diff] [blame] | 115 | static uint32_t i = 0; |
| 116 | teensy::AdcDmaSampler *const adc_dma = |
| 117 | global_adc_dma.load(::std::memory_order_relaxed); |
| 118 | |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 119 | Fet12AdcReadings adc_readings; |
James Kuszmaul | 7c8aad6 | 2018-09-08 18:16:18 -0700 | [diff] [blame] | 120 | // TODO(Brian): Switch to the DMA interrupt instead of spinning. |
| 121 | while (!adc_dma->CheckDone()) { |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 122 | } |
James Kuszmaul | 7c8aad6 | 2018-09-08 18:16:18 -0700 | [diff] [blame] | 123 | |
| 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 Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 131 | const ::std::array<float, 3> decoupled = |
| 132 | DecoupleCurrents(adc_readings.motor_currents); |
James Kuszmaul | 7c8aad6 | 2018-09-08 18:16:18 -0700 | [diff] [blame] | 133 | adc_dma->Reset(); |
| 134 | const uint32_t wrapped_encoder = |
| 135 | global_motor.load(::std::memory_order_relaxed)->wrapped_encoder(); |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 136 | const BalancedReadings balanced = |
| 137 | BalanceSimpleReadings(decoupled); |
| 138 | |
James Kuszmaul | 521eb65 | 2018-10-17 19:09:33 -0700 | [diff] [blame] | 139 | #if 1 |
| 140 | |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 141 | static float fuse_badness = 0; |
| 142 | |
| 143 | static uint32_t cycles_since_start = 0u; |
| 144 | ++cycles_since_start; |
| 145 | #if 0 |
| 146 | static int count = 0; |
| 147 | ++count; |
| 148 | static float currents[3] = {0.0f, 0.0f, 0.0f}; |
| 149 | for (int ii = 0; ii < 3; ++ii) { |
| 150 | currents[ii] += static_cast<float>(adc_readings.motor_currents[ii]); |
| 151 | } |
| 152 | |
| 153 | if (i == 0) { |
| 154 | printf( |
| 155 | "foo %d.0, %d.0, %d.0, %.3d %.3d %.3d, switching %d %d %d enc %d\n", |
| 156 | static_cast<int>(currents[0] / static_cast<float>(count)), |
| 157 | static_cast<int>(currents[1] / static_cast<float>(count)), |
| 158 | static_cast<int>(currents[2] / static_cast<float>(count)), |
| 159 | static_cast<int>(decoupled[0] * 1.0f), |
| 160 | static_cast<int>(decoupled[1] * 1.0f), |
| 161 | static_cast<int>(decoupled[2] * 1.0f), |
| 162 | global_motor.load(::std::memory_order_relaxed)->get_switching_points_cycles(0), |
| 163 | global_motor.load(::std::memory_order_relaxed)->get_switching_points_cycles(1), |
| 164 | global_motor.load(::std::memory_order_relaxed)->get_switching_points_cycles(2), |
| 165 | static_cast<int>( |
| 166 | global_motor.load(::std::memory_order_relaxed)->wrapped_encoder())); |
| 167 | count = 0; |
| 168 | currents[0] = 0.0f; |
| 169 | currents[1] = 0.0f; |
| 170 | currents[2] = 0.0f; |
| 171 | } |
| 172 | #endif |
| 173 | #if 1 |
| 174 | constexpr float kAlpha = 0.995f; |
| 175 | constexpr float kFuseAlpha = 0.95f; |
| 176 | |
| 177 | // 3400 - 760 |
James Kuszmaul | b770743 | 2018-10-07 14:48:11 -0700 | [diff] [blame] | 178 | // Start the throttle filter at 1.0f--once it converges to near zero, we set |
| 179 | // throttle_zeroed to true and only then do we start listening to throttle |
| 180 | // commands. |
| 181 | static float filtered_throttle = 1.0f; |
| 182 | static bool throttle_zeroed = false; |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 183 | constexpr int kMaxThrottle = 3400; |
| 184 | constexpr int kMinThrottle = 760; |
| 185 | const float throttle = ::std::max( |
| 186 | 0.0f, |
| 187 | ::std::min(1.0f, |
| 188 | static_cast<float>(static_cast<int>(adc_readings.throttle) - |
| 189 | kMinThrottle) / |
| 190 | static_cast<float>(kMaxThrottle - kMinThrottle))); |
| 191 | |
| 192 | // y(n) = x(n) + a * (y(n-1) - x(n)) |
| 193 | filtered_throttle = throttle + kAlpha * (filtered_throttle - throttle); |
James Kuszmaul | b770743 | 2018-10-07 14:48:11 -0700 | [diff] [blame] | 194 | if (::std::abs(filtered_throttle) < 1e-2f) { |
| 195 | // Once the filter gets near zero once, we start paying attention to it; |
| 196 | // once it gets near zero once, never ignore it again. |
| 197 | throttle_zeroed = true; |
| 198 | } |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 199 | |
| 200 | const float fuse_voltage = static_cast<float>(adc_readings.fuse_voltage); |
| 201 | static float filtered_fuse_voltage = 0.0f; |
| 202 | |
| 203 | filtered_fuse_voltage = |
| 204 | fuse_voltage + kFuseAlpha * (filtered_fuse_voltage - fuse_voltage); |
| 205 | |
| 206 | const float velocity = |
| 207 | global_motor.load(::std::memory_order_relaxed)->estimated_velocity(); |
| 208 | const float bemf = velocity / (static_cast<float>(Kv) / 1.5f); |
| 209 | const float abs_bemf = ::std::abs(bemf); |
| 210 | constexpr float kPeakCurrent = 300.0f; |
| 211 | constexpr float kLimitedCurrent = 75.0f; |
| 212 | const float max_bat_cur = |
| 213 | fuse_badness > (kLimitedCurrent * kLimitedCurrent * 0.95f) |
| 214 | ? kLimitedCurrent |
| 215 | : static_cast<float>(kIcc); |
| 216 | const float throttle_limit = ::std::min( |
| 217 | kPeakCurrent, |
| 218 | (-abs_bemf + ::std::sqrt(static_cast<float>( |
| 219 | bemf * bemf + |
| 220 | 4.0f * static_cast<float>(kR) * 1.5f * |
| 221 | static_cast<float>(kVcc) * max_bat_cur))) / |
| 222 | (2.0f * 1.5f * static_cast<float>(kR))); |
| 223 | |
James Kuszmaul | 521eb65 | 2018-10-17 19:09:33 -0700 | [diff] [blame] | 224 | constexpr float kNegativeCurrent = 100.0f; |
James Kuszmaul | a1d94c8 | 2018-10-10 20:00:09 -0700 | [diff] [blame] | 225 | float goal_current = |
| 226 | -::std::min( |
| 227 | ::std::max(filtered_throttle * (kPeakCurrent + kNegativeCurrent) - |
| 228 | kNegativeCurrent, |
| 229 | -throttle_limit), |
| 230 | throttle_limit); |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 231 | |
James Kuszmaul | b770743 | 2018-10-07 14:48:11 -0700 | [diff] [blame] | 232 | if (!throttle_zeroed) { |
| 233 | goal_current = 0.0f; |
| 234 | } |
James Kuszmaul | a1d94c8 | 2018-10-10 20:00:09 -0700 | [diff] [blame] | 235 | // Note: current reduction is 12/70 belt, 15 / 54 on chain, and 10 inch |
| 236 | // diameter wheels, so cutoff of 500 electrical rad/sec * 1 mechanical rad / 2 |
| 237 | // erad * 12 / 70 * 15 / 54 * 0.127 m = 1.5m/s = 3.4 mph |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 238 | if (velocity > -500) { |
| 239 | if (goal_current > 0.0f) { |
| 240 | goal_current = 0.0f; |
| 241 | } |
| 242 | } |
James Kuszmaul | 521eb65 | 2018-10-17 19:09:33 -0700 | [diff] [blame] | 243 | |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 244 | //float goal_current = |
| 245 | //-::std::min(filtered_throttle * kPeakCurrent, throttle_limit); |
James Kuszmaul | 521eb65 | 2018-10-17 19:09:33 -0700 | [diff] [blame] | 246 | const float overall_measured_current = |
| 247 | global_motor.load(::std::memory_order_relaxed) |
| 248 | ->overall_measured_current(); |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 249 | const float fuse_current = |
James Kuszmaul | 521eb65 | 2018-10-17 19:09:33 -0700 | [diff] [blame] | 250 | overall_measured_current * |
| 251 | (bemf + overall_measured_current * static_cast<float>(kR) * 1.5f) / |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 252 | static_cast<float>(kVcc); |
| 253 | const int16_t fuse_current_10 = static_cast<int16_t>(10.0f * fuse_current); |
| 254 | fuse_badness += 0.00002f * (fuse_current * fuse_current - fuse_badness); |
| 255 | |
| 256 | global_motor.load(::std::memory_order_relaxed) |
| 257 | ->SetGoalCurrent(goal_current); |
| 258 | global_motor.load(::std::memory_order_relaxed) |
Austin Schuh | 54c8c84 | 2019-04-07 13:54:23 -0700 | [diff] [blame] | 259 | ->CurrentInterrupt(balanced, wrapped_encoder); |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 260 | |
| 261 | global_debug_buffer.count.fetch_add(1); |
| 262 | |
James Kuszmaul | 521eb65 | 2018-10-17 19:09:33 -0700 | [diff] [blame] | 263 | const bool trigger = false && i > 10000; |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 264 | // global_debug_buffer.count.load(::std::memory_order_relaxed) >= 0; |
| 265 | size_t buffer_size = |
| 266 | global_debug_buffer.size.load(::std::memory_order_relaxed); |
| 267 | if ((buffer_size > 0 || trigger) && |
| 268 | buffer_size != global_debug_buffer.samples.size()) { |
| 269 | global_debug_buffer.samples[buffer_size].currents[0] = |
| 270 | static_cast<int16_t>(balanced.readings[0] * 10.0f); |
| 271 | global_debug_buffer.samples[buffer_size].currents[1] = |
| 272 | static_cast<int16_t>(balanced.readings[1] * 10.0f); |
| 273 | global_debug_buffer.samples[buffer_size].currents[2] = |
| 274 | static_cast<int16_t>(balanced.readings[2] * 10.0f); |
| 275 | global_debug_buffer.samples[buffer_size].position = |
| 276 | global_motor.load(::std::memory_order_relaxed)->wrapped_encoder(); |
| 277 | global_debug_buffer.samples[buffer_size].est_omega = |
| 278 | global_motor.load(::std::memory_order_relaxed)->estimated_velocity(); |
| 279 | global_debug_buffer.samples[buffer_size].commands[0] = |
| 280 | global_motor.load(::std::memory_order_relaxed)->get_switching_points_cycles(0); |
| 281 | global_debug_buffer.samples[buffer_size].commands[1] = |
| 282 | global_motor.load(::std::memory_order_relaxed)->get_switching_points_cycles(1); |
| 283 | global_debug_buffer.samples[buffer_size].commands[2] = |
| 284 | global_motor.load(::std::memory_order_relaxed)->get_switching_points_cycles(2); |
| 285 | 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; |
| 301 | global_debug_buffer.samples[buffer_size].cycles_since_start = cycles_since_start; |
| 302 | |
| 303 | global_debug_buffer.size.fetch_add(1); |
| 304 | } |
| 305 | |
James Kuszmaul | 7c8aad6 | 2018-09-08 18:16:18 -0700 | [diff] [blame] | 306 | ++i; |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 307 | if (buffer_size == global_debug_buffer.samples.size()) { |
James Kuszmaul | 7c8aad6 | 2018-09-08 18:16:18 -0700 | [diff] [blame] | 308 | i = 0; |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 309 | GPIOC_PCOR = (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4); |
| 310 | GPIOD_PCOR = (1 << 4) | (1 << 5); |
| 311 | |
| 312 | PERIPHERAL_BITBAND(GPIOC_PDDR, 1) = 1; |
| 313 | PERIPHERAL_BITBAND(GPIOC_PDDR, 2) = 1; |
| 314 | PERIPHERAL_BITBAND(GPIOC_PDDR, 3) = 1; |
| 315 | PERIPHERAL_BITBAND(GPIOC_PDDR, 4) = 1; |
| 316 | PERIPHERAL_BITBAND(GPIOD_PDDR, 4) = 1; |
| 317 | PERIPHERAL_BITBAND(GPIOD_PDDR, 5) = 1; |
| 318 | |
| 319 | PORTC_PCR1 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 320 | PORTC_PCR2 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 321 | PORTC_PCR3 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 322 | PORTC_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 323 | PORTD_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 324 | PORTD_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 325 | } |
James Kuszmaul | 7c8aad6 | 2018-09-08 18:16:18 -0700 | [diff] [blame] | 326 | #else |
| 327 | #endif |
| 328 | #else |
James Kuszmaul | 521eb65 | 2018-10-17 19:09:33 -0700 | [diff] [blame] | 329 | // Useful code when calculating resistance/inductance of motor |
James Kuszmaul | 7c8aad6 | 2018-09-08 18:16:18 -0700 | [diff] [blame] | 330 | FTM0->SC &= ~FTM_SC_TOF; |
| 331 | FTM0->C0V = 0; |
| 332 | FTM0->C1V = 0; |
| 333 | FTM0->C2V = 0; |
| 334 | FTM0->C3V = 0; |
| 335 | FTM0->C4V = 0; |
James Kuszmaul | 521eb65 | 2018-10-17 19:09:33 -0700 | [diff] [blame] | 336 | FTM0->C5V = 10; |
James Kuszmaul | 7c8aad6 | 2018-09-08 18:16:18 -0700 | [diff] [blame] | 337 | FTM0->PWMLOAD = FTM_PWMLOAD_LDOK; |
James Kuszmaul | 521eb65 | 2018-10-17 19:09:33 -0700 | [diff] [blame] | 338 | (void)wrapped_encoder; |
| 339 | (void)real_throttle; |
| 340 | size_t buffer_size = |
| 341 | global_debug_buffer.size.load(::std::memory_order_relaxed); |
| 342 | bool trigger = true || i > 20000; |
| 343 | if ((trigger || buffer_size > 0) && |
| 344 | buffer_size != global_debug_buffer.samples.size()) { |
| 345 | global_debug_buffer.samples[buffer_size].currents[0] = |
| 346 | static_cast<int16_t>(balanced.readings[0] * 10.0f); |
| 347 | global_debug_buffer.samples[buffer_size].currents[1] = |
| 348 | static_cast<int16_t>(balanced.readings[1] * 10.0f); |
| 349 | global_debug_buffer.samples[buffer_size].currents[2] = |
| 350 | static_cast<int16_t>(balanced.readings[2] * 10.0f); |
| 351 | global_debug_buffer.samples[buffer_size].commands[0] = FTM0->C1V; |
| 352 | global_debug_buffer.samples[buffer_size].commands[1] = FTM0->C3V; |
| 353 | global_debug_buffer.samples[buffer_size].commands[2] = FTM0->C5V; |
| 354 | global_debug_buffer.samples[buffer_size].position = |
| 355 | global_motor.load(::std::memory_order_relaxed)->wrapped_encoder(); |
| 356 | global_debug_buffer.size.fetch_add(1); |
| 357 | } |
| 358 | if (buffer_size == global_debug_buffer.samples.size()) { |
| 359 | GPIOC_PCOR = (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4); |
| 360 | GPIOD_PCOR = (1 << 4) | (1 << 5); |
| 361 | |
| 362 | PERIPHERAL_BITBAND(GPIOC_PDDR, 1) = 1; |
| 363 | PERIPHERAL_BITBAND(GPIOC_PDDR, 2) = 1; |
| 364 | PERIPHERAL_BITBAND(GPIOC_PDDR, 3) = 1; |
| 365 | PERIPHERAL_BITBAND(GPIOC_PDDR, 4) = 1; |
| 366 | PERIPHERAL_BITBAND(GPIOD_PDDR, 4) = 1; |
| 367 | PERIPHERAL_BITBAND(GPIOD_PDDR, 5) = 1; |
| 368 | |
| 369 | PORTC_PCR1 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 370 | PORTC_PCR2 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 371 | PORTC_PCR3 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 372 | PORTC_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 373 | PORTD_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 374 | PORTD_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 375 | i = 0; |
| 376 | } |
| 377 | ++i; |
James Kuszmaul | 7c8aad6 | 2018-09-08 18:16:18 -0700 | [diff] [blame] | 378 | #endif |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 379 | |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | } // extern "C" |
| 383 | |
| 384 | void ConfigurePwmFtm(BigFTM *pwm_ftm) { |
| 385 | // Put them all into combine active-high mode, and all the low ones staying on |
| 386 | // all the time by default. |
| 387 | pwm_ftm->C0SC = FTM_CSC_ELSA; |
| 388 | pwm_ftm->C0V = 0; |
| 389 | pwm_ftm->C1SC = FTM_CSC_ELSA; |
| 390 | pwm_ftm->C1V = 0; |
| 391 | pwm_ftm->C2SC = FTM_CSC_ELSA; |
| 392 | pwm_ftm->C2V = 0; |
| 393 | pwm_ftm->C3SC = FTM_CSC_ELSA; |
| 394 | pwm_ftm->C3V = 0; |
| 395 | pwm_ftm->C4SC = FTM_CSC_ELSA; |
| 396 | pwm_ftm->C4V = 0; |
| 397 | pwm_ftm->C5SC = FTM_CSC_ELSA; |
| 398 | pwm_ftm->C5V = 0; |
| 399 | pwm_ftm->C6SC = FTM_CSC_ELSA; |
| 400 | pwm_ftm->C6V = 0; |
| 401 | pwm_ftm->C7SC = FTM_CSC_ELSA; |
| 402 | pwm_ftm->C7V = 0; |
| 403 | |
| 404 | pwm_ftm->COMBINE = FTM_COMBINE_SYNCEN3 /* Synchronize updates usefully */ | |
| 405 | FTM_COMBINE_DTEN3 /* Enable deadtime */ | |
| 406 | FTM_COMBINE_COMP3 /* Make them complementary */ | |
| 407 | FTM_COMBINE_COMBINE3 /* Combine the channels */ | |
| 408 | FTM_COMBINE_SYNCEN2 /* Synchronize updates usefully */ | |
| 409 | FTM_COMBINE_DTEN2 /* Enable deadtime */ | |
| 410 | FTM_COMBINE_COMP2 /* Make them complementary */ | |
| 411 | FTM_COMBINE_COMBINE2 /* Combine the channels */ | |
| 412 | FTM_COMBINE_SYNCEN1 /* Synchronize updates usefully */ | |
| 413 | FTM_COMBINE_DTEN1 /* Enable deadtime */ | |
| 414 | FTM_COMBINE_COMP1 /* Make them complementary */ | |
| 415 | FTM_COMBINE_COMBINE1 /* Combine the channels */ | |
| 416 | FTM_COMBINE_SYNCEN0 /* Synchronize updates usefully */ | |
| 417 | FTM_COMBINE_DTEN0 /* Enable deadtime */ | |
| 418 | FTM_COMBINE_COMP0 /* Make them complementary */ | |
| 419 | FTM_COMBINE_COMBINE0 /* Combine the channels */; |
| 420 | // Safe state for all channels is low. |
| 421 | pwm_ftm->POL = 0; |
| 422 | |
| 423 | // Set the deadtime. |
| 424 | pwm_ftm->DEADTIME = |
| 425 | FTM_DEADTIME_DTPS(0) /* Prescaler of 1 */ | FTM_DEADTIME_DTVAL(9); |
| 426 | |
| 427 | pwm_ftm->CONF = |
| 428 | FTM_CONF_BDMMOD(1) /* Set everything to POLn during debug halt */; |
| 429 | } |
| 430 | |
| 431 | // Zeros the encoder. This involves blocking for an arbitrary length of time |
| 432 | // with interrupts disabled. |
| 433 | void ZeroMotor() { |
| 434 | #if 0 |
| 435 | while (true) { |
| 436 | if (PERIPHERAL_BITBAND(GPIOB_PDIR, 11)) { |
| 437 | encoder_ftm_->CNT = 0; |
| 438 | break; |
| 439 | } |
| 440 | } |
| 441 | #else |
| 442 | uint32_t scratch; |
| 443 | __disable_irq(); |
| 444 | // Stuff all of this in an inline assembly statement so we can make sure the |
| 445 | // compiler doesn't decide sticking constant loads etc in the middle of |
| 446 | // the loop is a good idea, because that increases the latency of recognizing |
| 447 | // the index pulse edge which makes velocity affect the zeroing accuracy. |
| 448 | __asm__ __volatile__( |
| 449 | // A label to restart the loop. |
| 450 | "0:\n" |
| 451 | // Load the current PDIR value for the pin we care about. |
| 452 | "ldr %[scratch], [%[pdir_word]]\n" |
| 453 | // Terminate the loop if it's non-0. |
| 454 | "cbnz %[scratch], 1f\n" |
| 455 | // Go back around again. |
| 456 | "b 0b\n" |
| 457 | // A label to finish the loop. |
| 458 | "1:\n" |
| 459 | // Reset the count once we're down here. It doesn't actually matter what |
| 460 | // value we store because writing anything resets it to CNTIN (ie 0). |
| 461 | "str %[scratch], [%[cnt]]\n" |
| 462 | : [scratch] "=&l"(scratch) |
| 463 | : [pdir_word] "l"(&PERIPHERAL_BITBAND(GPIOB_PDIR, 11)), |
| 464 | [cnt] "l"(&FTM1->CNT)); |
| 465 | __enable_irq(); |
| 466 | #endif |
| 467 | } |
| 468 | |
| 469 | } // namespace |
| 470 | |
| 471 | extern "C" int main(void) { |
| 472 | // for background about this startup delay, please see these conversations |
| 473 | // https://forum.pjrc.com/threads/36606-startup-time-(400ms)?p=113980&viewfull=1#post113980 |
| 474 | // https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273 |
| 475 | delay(400); |
| 476 | |
| 477 | // Set all interrupts to the second-lowest priority to start with. |
| 478 | for (int i = 0; i < NVIC_NUM_INTERRUPTS; i++) NVIC_SET_SANE_PRIORITY(i, 0xD); |
| 479 | |
| 480 | // Now set priorities for all the ones we care about. They only have meaning |
| 481 | // relative to each other, which means centralizing them here makes it a lot |
| 482 | // more manageable. |
| 483 | NVIC_SET_SANE_PRIORITY(IRQ_FTM0, 0x3); |
| 484 | NVIC_SET_SANE_PRIORITY(IRQ_UART0_STATUS, 0xE); |
| 485 | |
| 486 | // Set the LED's pin to output mode. |
| 487 | PERIPHERAL_BITBAND(GPIOC_PDDR, 5) = 1; |
| 488 | PORTC_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 489 | |
| 490 | #if 0 |
| 491 | PERIPHERAL_BITBAND(GPIOA_PDDR, 15) = 1; |
| 492 | PORTA_PCR15 = PORT_PCR_DSE | PORT_PCR_MUX(1); |
| 493 | #endif |
| 494 | |
| 495 | // Set up the CAN pins. |
| 496 | PORTA_PCR12 = PORT_PCR_DSE | PORT_PCR_MUX(2); |
| 497 | PORTB_PCR19 = PORT_PCR_DSE | PORT_PCR_MUX(2); |
| 498 | |
| 499 | DMA.CR = M_DMA_EMLM; |
| 500 | |
| 501 | PORTB_PCR16 = PORT_PCR_DSE | PORT_PCR_MUX(3); |
| 502 | PORTB_PCR17 = PORT_PCR_DSE | PORT_PCR_MUX(3); |
| 503 | SIM_SCGC4 |= SIM_SCGC4_UART0; |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame] | 504 | |
| 505 | PrintingParameters printing_parameters; |
| 506 | printing_parameters.stdout_uart_module = &UART0; |
| 507 | printing_parameters.stdout_uart_module_clock_frequency = F_CPU; |
| 508 | printing_parameters.stdout_uart_status_interrupt = IRQ_UART0_STATUS; |
| 509 | printing_parameters.dedicated_usb = true; |
| 510 | const ::std::unique_ptr<PrintingImplementation> printing = |
| 511 | CreatePrinting(printing_parameters); |
| 512 | printing->Initialize(); |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 513 | |
| 514 | AdcInitFet12(); |
| 515 | MathInit(); |
| 516 | delay(100); |
| 517 | can_init(0, 1); |
| 518 | |
| 519 | MotorControlsImplementation controls; |
| 520 | |
| 521 | delay(100); |
| 522 | |
| 523 | // Index pin |
| 524 | PORTB_PCR11 = PORT_PCR_MUX(1); |
| 525 | // FTM1_QD_PH{A,B} |
| 526 | PORTB_PCR0 = PORT_PCR_MUX(6); |
| 527 | PORTB_PCR1 = PORT_PCR_MUX(6); |
| 528 | |
| 529 | // FTM0_CH[0-5] |
| 530 | PORTC_PCR1 = PORT_PCR_DSE | PORT_PCR_MUX(4); |
| 531 | PORTC_PCR2 = PORT_PCR_DSE | PORT_PCR_MUX(4); |
| 532 | PORTC_PCR3 = PORT_PCR_DSE | PORT_PCR_MUX(4); |
| 533 | PORTC_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(4); |
| 534 | PORTD_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(4); |
| 535 | PORTD_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(4); |
| 536 | |
| 537 | Motor motor(FTM0, FTM1, &controls, {&FTM0->C0V, &FTM0->C2V, &FTM0->C4V}); |
| 538 | motor.set_encoder_offset(810); |
| 539 | motor.set_deadtime_compensation(9); |
| 540 | ConfigurePwmFtm(FTM0); |
James Kuszmaul | 7c8aad6 | 2018-09-08 18:16:18 -0700 | [diff] [blame] | 541 | |
Brian Silverman | a1d8482 | 2018-09-15 17:18:49 -0700 | [diff] [blame] | 542 | // TODO(Brian): Figure out how to avoid duplicating this code to slave one FTM |
| 543 | // to another. |
James Kuszmaul | 7c8aad6 | 2018-09-08 18:16:18 -0700 | [diff] [blame] | 544 | FTM2->CONF = FTM_CONF_GTBEEN; |
| 545 | FTM2->MODE = FTM_MODE_WPDIS; |
| 546 | FTM2->MODE = FTM_MODE_WPDIS | FTM_MODE_FTMEN; |
| 547 | FTM2->SC = FTM_SC_CLKS(0) /* Disable counting for now */; |
| 548 | FTM2->CNTIN = 0; |
| 549 | FTM2->CNT = 0; |
| 550 | // TODO(Brian): Don't duplicate this. |
| 551 | FTM2->MOD = BUS_CLOCK_FREQUENCY / SWITCHING_FREQUENCY; |
| 552 | FTM2->OUTINIT = 0; |
| 553 | // All of the channels are active high. |
| 554 | FTM2->POL = 0; |
| 555 | FTM2->SYNCONF = FTM_SYNCONF_HWWRBUF | FTM_SYNCONF_SWWRBUF | |
| 556 | FTM_SYNCONF_SWRSTCNT | FTM_SYNCONF_SYNCMODE; |
| 557 | // Don't want any intermediate loading points. |
| 558 | FTM2->PWMLOAD = 0; |
| 559 | |
| 560 | // Need to set them to some kind of output mode so we can actually change |
| 561 | // them. |
| 562 | FTM2->C0SC = FTM_CSC_MSA; |
| 563 | FTM2->C1SC = FTM_CSC_MSA; |
| 564 | |
| 565 | // This has to happen after messing with SYNCONF, and should happen after |
| 566 | // messing with various other things so the values can get flushed out of the |
| 567 | // buffers. |
| 568 | FTM2->SYNC = |
| 569 | FTM_SYNC_SWSYNC /* Flush everything out right now */ | |
| 570 | FTM_SYNC_CNTMAX /* Load new values at the end of the cycle */; |
| 571 | // Wait for the software synchronization to finish. |
| 572 | while (FTM2->SYNC & FTM_SYNC_SWSYNC) { |
| 573 | } |
| 574 | FTM2->SC = FTM_SC_CLKS(1) /* Use the system clock */ | |
| 575 | FTM_SC_PS(0) /* Don't prescale the clock */; |
| 576 | // TODO: |
| 577 | //FTM2->MODE &= ~FTM_MODE_WPDIS; |
| 578 | |
| 579 | FTM2->EXTTRIG = FTM_EXTTRIG_CH0TRIG | FTM_EXTTRIG_CH1TRIG; |
| 580 | |
Brian Silverman | a1d8482 | 2018-09-15 17:18:49 -0700 | [diff] [blame] | 581 | // TODO(Brian): Don't duplicate the timer's MOD value. |
| 582 | teensy::AdcDmaSampler adc_dma{BUS_CLOCK_FREQUENCY / SWITCHING_FREQUENCY}; |
James Kuszmaul | 7c8aad6 | 2018-09-08 18:16:18 -0700 | [diff] [blame] | 583 | // ADC0_Dx0 is 1-0 |
| 584 | // ADC0_Dx2 is 1-2 |
| 585 | // ADC0_Dx3 is 2-0 |
| 586 | // ADC1_Dx0 is 2-0 |
| 587 | // ADC1_Dx3 is 1-0 |
| 588 | // Sample 0: 1-2,2-0 |
| 589 | // Sample 1: 1-2,1-0 |
| 590 | // Sample 2: 1-0,2-0 |
| 591 | // Sample 3: 23(SENSE0),18(VIN) |
| 592 | adc_dma.set_adc0_samples({V_ADC_ADCH(2) | M_ADC_DIFF, |
| 593 | V_ADC_ADCH(2) | M_ADC_DIFF, |
| 594 | V_ADC_ADCH(0) | M_ADC_DIFF, V_ADC_ADCH(23)}); |
| 595 | adc_dma.set_adc1_samples({V_ADC_ADCH(0) | M_ADC_DIFF, |
| 596 | V_ADC_ADCH(3) | M_ADC_DIFF, |
| 597 | V_ADC_ADCH(0) | M_ADC_DIFF, V_ADC_ADCH(18)}); |
| 598 | adc_dma.set_ftm_delays({&FTM2->C0V, &FTM2->C1V}); |
| 599 | adc_dma.set_pdb_input(PDB_IN_FTM2); |
| 600 | |
| 601 | adc_dma.Initialize(); |
| 602 | FTM0->CONF = FTM_CONF_GTBEEN; |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 603 | motor.Init(); |
| 604 | global_motor.store(&motor, ::std::memory_order_relaxed); |
James Kuszmaul | 7c8aad6 | 2018-09-08 18:16:18 -0700 | [diff] [blame] | 605 | global_adc_dma.store(&adc_dma, ::std::memory_order_relaxed); |
| 606 | |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 607 | // Output triggers to things like the PDBs on initialization. |
| 608 | FTM0_EXTTRIG = FTM_EXTTRIG_INITTRIGEN; |
| 609 | // Don't let any memory accesses sneak past here, because we actually |
| 610 | // need everything to be starting up. |
| 611 | __asm__("" :: : "memory"); |
| 612 | |
| 613 | // Give everything a chance to get going. |
| 614 | delay(100); |
| 615 | |
| 616 | printf("Ram start: %p\n", __bss_ram_start__); |
| 617 | printf("Heap start: %p\n", __heap_start__); |
| 618 | printf("Heap end: %p\n", __brkval); |
| 619 | printf("Stack start: %p\n", __stack_end__); |
| 620 | |
| 621 | printf("Going silent to zero motors...\n"); |
| 622 | // Give the print a chance to make it out. |
| 623 | delay(100); |
| 624 | ZeroMotor(); |
| 625 | |
| 626 | motor.set_encoder_multiplier(-1); |
| 627 | motor.set_encoder_calibration_offset( |
James Kuszmaul | 521eb65 | 2018-10-17 19:09:33 -0700 | [diff] [blame] | 628 | 364 /*from running constant phases*/ - 26 /*average offset from lstsq*/ - |
| 629 | 14 /* compensation for going backwards */); |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 630 | |
| 631 | printf("Zeroed motor!\n"); |
| 632 | // Give stuff a chance to recover from interrupts-disabled. |
| 633 | delay(100); |
James Kuszmaul | 7c8aad6 | 2018-09-08 18:16:18 -0700 | [diff] [blame] | 634 | adc_dma.Reset(); |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 635 | motor.Start(); |
James Kuszmaul | 7c8aad6 | 2018-09-08 18:16:18 -0700 | [diff] [blame] | 636 | // Now poke the GTB to actually start both timers. |
| 637 | FTM0->CONF = FTM_CONF_GTBEEN | FTM_CONF_GTBEOUT; |
| 638 | |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 639 | NVIC_ENABLE_IRQ(IRQ_FTM0); |
| 640 | GPIOC_PSOR = 1 << 5; |
| 641 | |
James Kuszmaul | 521eb65 | 2018-10-17 19:09:33 -0700 | [diff] [blame] | 642 | constexpr bool dump_full_sample = true; |
| 643 | constexpr bool dump_resist_calib = false; |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 644 | while (true) { |
James Kuszmaul | 521eb65 | 2018-10-17 19:09:33 -0700 | [diff] [blame] | 645 | if (dump_resist_calib || dump_full_sample) { |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 646 | PORTC_PCR1 = PORT_PCR_DSE | PORT_PCR_MUX(4); |
| 647 | PORTC_PCR2 = PORT_PCR_DSE | PORT_PCR_MUX(4); |
| 648 | PORTC_PCR3 = PORT_PCR_DSE | PORT_PCR_MUX(4); |
| 649 | PORTC_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(4); |
| 650 | PORTD_PCR4 = PORT_PCR_DSE | PORT_PCR_MUX(4); |
| 651 | PORTD_PCR5 = PORT_PCR_DSE | PORT_PCR_MUX(4); |
| 652 | motor.Reset(); |
| 653 | } |
| 654 | global_debug_buffer.size.store(0); |
| 655 | global_debug_buffer.count.store(0); |
| 656 | while (global_debug_buffer.size.load(::std::memory_order_relaxed) < |
| 657 | global_debug_buffer.samples.size()) { |
| 658 | } |
James Kuszmaul | 521eb65 | 2018-10-17 19:09:33 -0700 | [diff] [blame] | 659 | if (dump_resist_calib) { |
| 660 | // Useful prints for when calibrating resistance/inductance of motor |
| 661 | for (size_t i = 0; i < global_debug_buffer.samples.size(); ++i) { |
| 662 | const auto &sample = global_debug_buffer.samples[i]; |
| 663 | printf("%u, %d, %d, %d, %u, %u, %u, %u\n", i, |
| 664 | sample.currents[0], sample.currents[1], sample.currents[2], |
| 665 | sample.commands[0], sample.commands[1], sample.commands[2], |
| 666 | sample.position); |
| 667 | } |
| 668 | } else if (dump_full_sample) { |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 669 | printf("Dumping data\n"); |
| 670 | for (size_t i = 0; i < global_debug_buffer.samples.size(); ++i) { |
| 671 | const auto &sample = global_debug_buffer.samples[i]; |
| 672 | |
| 673 | printf("%u, %d, %d, %d, %u, %u, %u, %u, %d, %d, %d, %d\n", i, |
| 674 | sample.currents[0], sample.currents[1], sample.currents[2], |
| 675 | sample.commands[0], sample.commands[1], sample.commands[2], |
| 676 | sample.position, static_cast<int>(sample.est_omega), |
| 677 | sample.commanded_currents[0], sample.commanded_currents[1], |
| 678 | sample.commanded_currents[2]); |
| 679 | } |
| 680 | printf("Done dumping data\n"); |
| 681 | } else { |
| 682 | //const auto &sample = global_debug_buffer.samples.back(); |
| 683 | const DebugBuffer::Sample sample = global_debug_buffer.samples[0]; |
| 684 | #if 1 |
| 685 | printf("%" PRIu32 |
| 686 | ", %d, %d, %d, %u, %u, %u, %u, %d, %d, %d, %d, %d, %d, %d\n", |
| 687 | sample.cycles_since_start, sample.currents[0], sample.currents[1], |
| 688 | sample.currents[2], sample.commands[0], sample.commands[1], |
| 689 | sample.commands[2], sample.position, |
| 690 | static_cast<int>(sample.est_omega), sample.commanded_currents[0], |
| 691 | sample.commanded_currents[1], sample.commanded_currents[2], |
| 692 | sample.total_command, static_cast<int>(sample.driver_request), |
| 693 | static_cast<int>(sample.fuse_badness)); |
| 694 | #else |
| 695 | printf("%d, %d\n", static_cast<int>(sample.fuse_voltage), |
| 696 | sample.fuse_current); |
| 697 | #endif |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | return 0; |
| 702 | } |
| 703 | |
| 704 | } // namespace motors |
| 705 | } // namespace frc971 |