Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 1 | #include "motors/motor.h" |
| 2 | |
| 3 | #include <limits.h> |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 4 | #include <stdio.h> |
| 5 | #include <inttypes.h> |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 6 | |
| 7 | #include <array> |
| 8 | |
| 9 | #include "motors/peripheral/configuration.h" |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 10 | #include "motors/peripheral/can.h" |
| 11 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 12 | extern "C" float analog_ratio(uint16_t reading); |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 13 | extern "C" float absolute_wheel(uint16_t reading); |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 14 | |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 15 | namespace frc971 { |
Brian Silverman | a96c1a4 | 2018-05-12 12:11:31 -0700 | [diff] [blame] | 16 | namespace motors { |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 17 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 18 | Motor::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 Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 24 | // 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 Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 34 | } |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 35 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 36 | static_assert((BUS_CLOCK_FREQUENCY % SWITCHING_FREQUENCY) == 0, |
| 37 | "Switching frequency needs to divide the bus clock frequency"); |
| 38 | |
| 39 | static_assert(BUS_CLOCK_FREQUENCY / SWITCHING_FREQUENCY < UINT16_MAX, |
| 40 | "Need to prescale"); |
| 41 | |
| 42 | void Motor::Init() { |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 43 | pwm_ftm_->CNTIN = encoder_ftm_->CNTIN = 0; |
| 44 | pwm_ftm_->CNT = encoder_ftm_->CNT = 0; |
| 45 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 46 | pwm_ftm_->MOD = counts_per_cycle() - 1; |
| 47 | encoder_ftm_->MOD = controls_->mechanical_counts_per_revolution() - 1; |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 48 | |
| 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 Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 56 | pwm_ftm_->OUTINIT = 0; |
| 57 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 58 | // All of the channels are active high (low-side ones with separate high/low |
| 59 | // drives are defaulted on elsewhere). |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 60 | 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 Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 96 | void 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 Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 104 | #define USE_ABSOLUTE_CUTOFF 0 |
| 105 | #define DO_CONTROLS 1 |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 106 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 107 | #define USE_CUTOFF 1 |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 108 | #define PRINT_ALL_READINGS 0 |
| 109 | #define TAKE_SAMPLE 0 |
| 110 | #define SAMPLE_UNTIL_DONE 0 |
| 111 | #define DO_STEP_RESPONSE 0 |
| 112 | #define DO_PULSE_SWEEP 0 |
| 113 | #define PRINT_TIMING 0 |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 114 | |
Austin Schuh | efe7293 | 2019-04-13 00:03:19 -0700 | [diff] [blame] | 115 | // An on-width of 60 with 30V in means about 50A through the motor and about |
| 116 | // 30W total power dumped by the motor for the big one. |
| 117 | // For the small one, an on-width of 120/3000 with 14V in means about 2A |
| 118 | // through the motor. |
| 119 | void Motor::CycleFixedPhaseInterupt(int period) { |
Austin Schuh | 6bf8275 | 2019-04-07 13:55:01 -0700 | [diff] [blame] | 120 | pwm_ftm_->SC &= ~FTM_SC_TOF; |
| 121 | // Step through all the phases one by one in a loop. This should slowly move |
| 122 | // the trigger. |
| 123 | // If we fire phase 1, we should go to PI radians. |
| 124 | // If we fire phase 2, we should go to 1.0 * PI / 3.0 radians. |
| 125 | // If we fire phase 3, we should go to -1.0 * PI / 3.0 radians. |
| 126 | // These numbers were confirmed by the python motor simulation. |
| 127 | static int phase_to_fire_count = -300000; |
| 128 | static int phase_to_fire = 0; |
| 129 | ++phase_to_fire_count; |
| 130 | if (phase_to_fire_count > 500000) { |
| 131 | phase_to_fire_count = 0; |
| 132 | ++phase_to_fire; |
| 133 | if (phase_to_fire > 2) { |
| 134 | phase_to_fire = 0; |
| 135 | } |
| 136 | } |
| 137 | phase_to_fire = 0; |
| 138 | |
Austin Schuh | 6bf8275 | 2019-04-07 13:55:01 -0700 | [diff] [blame] | 139 | output_registers_[0][0] = 0; |
Austin Schuh | efe7293 | 2019-04-13 00:03:19 -0700 | [diff] [blame] | 140 | output_registers_[0][2] = phase_to_fire == 0 ? period : 0; |
Austin Schuh | 6bf8275 | 2019-04-07 13:55:01 -0700 | [diff] [blame] | 141 | |
| 142 | const float switching_points_max = static_cast<float>(counts_per_cycle()); |
| 143 | switching_points_ratio_[0] = |
| 144 | static_cast<float>(output_registers_[0][2]) / switching_points_max; |
| 145 | output_registers_[1][0] = 0; |
Austin Schuh | efe7293 | 2019-04-13 00:03:19 -0700 | [diff] [blame] | 146 | output_registers_[1][2] = phase_to_fire == 1 ? period : 0; |
Austin Schuh | 6bf8275 | 2019-04-07 13:55:01 -0700 | [diff] [blame] | 147 | switching_points_ratio_[1] = |
| 148 | static_cast<float>(output_registers_[1][2]) / switching_points_max; |
| 149 | output_registers_[2][0] = 0; |
Austin Schuh | efe7293 | 2019-04-13 00:03:19 -0700 | [diff] [blame] | 150 | output_registers_[2][2] = phase_to_fire == 2 ? period : 0; |
Austin Schuh | 6bf8275 | 2019-04-07 13:55:01 -0700 | [diff] [blame] | 151 | switching_points_ratio_[2] = |
| 152 | static_cast<float>(output_registers_[2][2]) / switching_points_max; |
| 153 | |
| 154 | // Tell the hardware to use the new switching points. |
| 155 | // TODO(Brian): Somehow verify that we consistently hit the first or second |
| 156 | // timer-cycle with the new values (if there's two). |
| 157 | pwm_ftm_->PWMLOAD = FTM_PWMLOAD_LDOK; |
| 158 | |
| 159 | // If another cycle has already started, turn the light on right now. |
| 160 | if (pwm_ftm_->SC & FTM_SC_TOF) { |
| 161 | GPIOC_PSOR = 1 << 5; |
| 162 | } |
| 163 | } |
| 164 | |
Austin Schuh | 54c8c84 | 2019-04-07 13:54:23 -0700 | [diff] [blame] | 165 | void Motor::CurrentInterrupt(const BalancedReadings &balanced, |
| 166 | uint32_t captured_wrapped_encoder) { |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 167 | pwm_ftm_->SC &= ~FTM_SC_TOF; |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 168 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 169 | #if PRINT_TIMING |
| 170 | const uint32_t start_nanos = nanos(); |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 171 | #endif // PRINT_TIMING |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 172 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 173 | if (!time_after(time_add(last_current_set_time_, 100000), micros())) { |
| 174 | goal_current_ = 0; |
| 175 | } |
| 176 | |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 177 | #if DO_CONTROLS |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 178 | switching_points_ratio_ = controls_->DoIteration( |
| 179 | balanced.readings, captured_wrapped_encoder, goal_current_); |
| 180 | const float switching_points_max = static_cast<float>(counts_per_cycle()); |
| 181 | const ::std::array<uint32_t, 3> switching_points = { |
| 182 | static_cast<uint32_t>(switching_points_ratio_[0] * switching_points_max), |
| 183 | static_cast<uint32_t>(switching_points_ratio_[1] * switching_points_max), |
| 184 | static_cast<uint32_t>(switching_points_ratio_[2] * switching_points_max)}; |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 185 | #if USE_CUTOFF |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 186 | constexpr uint32_t kMax = 2995; |
| 187 | //constexpr uint32_t kMax = 1445; |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 188 | static bool done = false; |
| 189 | bool done_now = false; |
| 190 | if (switching_points[0] > kMax || switching_points[1] > kMax || |
| 191 | switching_points[2] > kMax) { |
| 192 | done_now = true; |
| 193 | } |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 194 | #if USE_ABSOLUTE_CUTOFF |
| 195 | static unsigned int current_done_count = 0; |
| 196 | bool current_done = false; |
| 197 | for (int phase = 0; phase < 3; ++phase) { |
| 198 | const float scaled_reading = |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 199 | controls_->scale_current_reading(balanced.readings[phase]); |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 200 | static constexpr float kMaxBalancedCurrent = 50.0f; |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 201 | if (scaled_reading > kMaxBalancedCurrent || |
| 202 | scaled_reading < -kMaxBalancedCurrent) { |
| 203 | current_done = true; |
| 204 | } |
| 205 | } |
| 206 | if (current_done) { |
| 207 | if (current_done_count > 5) { |
| 208 | done_now = true; |
| 209 | } |
| 210 | ++current_done_count; |
| 211 | } else { |
| 212 | current_done_count = 0; |
| 213 | } |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 214 | #endif // USE_ABSOLUTE_CUTOFF |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 215 | if (done_now && !done) { |
| 216 | printf("done now\n"); |
| 217 | printf("switching_points %" PRIu32 " %" PRIu32 " %" PRIu32 "\n", |
| 218 | switching_points[0], switching_points[1], switching_points[2]); |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 219 | printf("balanced %" PRIu16 " %" PRIu16 " %" PRIu16 "\n", |
| 220 | static_cast<uint16_t>(balanced.readings[0]), |
| 221 | static_cast<uint16_t>(balanced.readings[1]), |
| 222 | static_cast<uint16_t>(balanced.readings[2])); |
| 223 | done = true; |
| 224 | } |
| 225 | if (!done) { |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 226 | #else // USE_CUTOFF |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 227 | if (true) { |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 228 | #endif // USE_CUTOFF |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 229 | output_registers_[0][0] = CalculateOnTime(switching_points[0]); |
| 230 | output_registers_[0][2] = CalculateOffTime(switching_points[0]); |
| 231 | output_registers_[1][0] = CalculateOnTime(switching_points[1]); |
| 232 | output_registers_[1][2] = CalculateOffTime(switching_points[1]); |
| 233 | output_registers_[2][0] = CalculateOnTime(switching_points[2]); |
| 234 | output_registers_[2][2] = CalculateOffTime(switching_points[2]); |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 235 | flip_time_offset_ = !flip_time_offset_; |
| 236 | } else { |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 237 | output_registers_[0][0] = 0; |
| 238 | output_registers_[0][2] = 0; |
| 239 | output_registers_[1][0] = 0; |
| 240 | output_registers_[1][2] = 0; |
| 241 | output_registers_[2][0] = 0; |
| 242 | output_registers_[2][2] = 0; |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 243 | } |
Austin Schuh | 6bf8275 | 2019-04-07 13:55:01 -0700 | [diff] [blame] | 244 | #endif // DO_CONTROLS |
Brian Silverman | 6260c09 | 2018-01-14 15:21:36 -0800 | [diff] [blame] | 245 | (void)balanced; |
| 246 | (void)captured_wrapped_encoder; |
Austin Schuh | 5b0e6b6 | 2019-04-07 14:23:37 -0700 | [diff] [blame] | 247 | #if PRINT_ALL_READINGS |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 248 | printf("ref=%" PRIu16 " 0.0=%" PRIu16 " 1.0=%" PRIu16 " 2.0=%" PRIu16 |
| 249 | " in=%" PRIu16 " 0.1=%" PRIu16 " 1.1=%" PRIu16 " 2.1=%" PRIu16 "\n", |
| 250 | adc_readings.motor_current_ref, adc_readings.motor_currents[0][0], |
| 251 | adc_readings.motor_currents[1][0], adc_readings.motor_currents[2][0], |
| 252 | adc_readings.input_voltage, adc_readings.motor_currents[0][1], |
| 253 | adc_readings.motor_currents[1][1], adc_readings.motor_currents[2][1]); |
Austin Schuh | 5b0e6b6 | 2019-04-07 14:23:37 -0700 | [diff] [blame] | 254 | #elif TAKE_SAMPLE // PRINT_ALL_READINGS |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 255 | #if 0 |
| 256 | constexpr int kStartupWait = 50000; |
| 257 | #elif 0 |
| 258 | constexpr int kStartupWait = 0; |
| 259 | #elif 0 |
| 260 | constexpr int kStartupWait = 30000; |
| 261 | #elif 1 |
| 262 | constexpr int kStartupWait = 2 * 20000; |
| 263 | #endif |
| 264 | constexpr int kSubsampling = 1; |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 265 | //constexpr int kPoints = 5000; |
| 266 | constexpr int kPoints = 1000; |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 267 | constexpr int kSamplingEnd = kStartupWait + kPoints * kSubsampling; |
| 268 | (void)kSamplingEnd; |
| 269 | static int j = 0; |
| 270 | static int16_t data[kPoints][11]; |
| 271 | static int written = 0; |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 272 | static bool done_writing = false; |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 273 | static_assert((kStartupWait % kSubsampling) == 0, "foo"); |
| 274 | static_assert((kPoints % kSubsampling) == 0, "foo"); |
| 275 | if (j < kStartupWait) { |
| 276 | // Wait to be started up. |
| 277 | ++j; |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 278 | #if SAMPLE_UNTIL_DONE |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 279 | } else if (!done) { |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 280 | #else // SAMPLE_UNTIL_DONE |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 281 | } else if (j < kSamplingEnd && (j % kSubsampling) == 0) { |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 282 | #endif // SAMPLE_UNTIL_DONE |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 283 | { |
| 284 | const int index = ((j - kStartupWait) / kSubsampling) % kPoints; |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 285 | auto &point = data[index]; |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 286 | // Start obnoxious #if 0/#if 1 |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 287 | #if 0 |
| 288 | point[0] = adc_readings.motor_currents[0][0]; |
| 289 | point[1] = adc_readings.motor_currents[1][0]; |
| 290 | point[2] = adc_readings.motor_currents[2][0]; |
| 291 | point[3] = adc_readings.motor_currents[0][1]; |
| 292 | point[4] = adc_readings.motor_currents[1][1]; |
| 293 | point[5] = adc_readings.motor_currents[2][1]; |
| 294 | #else |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 295 | point[0] = balanced.readings[0]; |
| 296 | point[1] = balanced.readings[1]; |
| 297 | point[2] = balanced.readings[2]; |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 298 | #if 1 |
| 299 | point[3] = controls_->Debug(0); |
| 300 | point[4] = controls_->Debug(1); |
| 301 | point[5] = controls_->Debug(2); |
| 302 | point[6] = controls_->Debug(3); |
| 303 | point[7] = controls_->Debug(4); |
| 304 | point[8] = controls_->Debug(5); |
| 305 | point[9] = controls_->Debug(6); |
| 306 | point[10] = controls_->Debug(7); |
| 307 | #else |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 308 | #if 0 |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 309 | point[3] = adc_readings.motor_currents[0][0]; |
| 310 | point[4] = adc_readings.motor_currents[1][0]; |
| 311 | point[5] = adc_readings.motor_currents[2][0]; |
| 312 | point[6] = adc_readings.motor_currents[0][1]; |
| 313 | point[7] = adc_readings.motor_currents[1][1]; |
| 314 | point[8] = adc_readings.motor_currents[2][1]; |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 315 | #else |
| 316 | point[3] = 0; |
| 317 | point[4] = 0; |
| 318 | point[5] = 0; |
| 319 | point[6] = 0; |
| 320 | point[7] = 0; |
| 321 | point[8] = 0; |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 322 | #endif |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 323 | point[9] = pwm_ftm_->C2V; |
| 324 | point[10] = pwm_ftm_->C3V; |
| 325 | #endif |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 326 | #if 0 |
| 327 | point[3] = pwm_ftm_->C1V - pwm_ftm_->C0V; |
| 328 | point[4] = pwm_ftm_->C3V - pwm_ftm_->C2V; |
| 329 | point[5] = pwm_ftm_->C5V - pwm_ftm_->C4V; |
| 330 | #endif |
| 331 | #endif |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 332 | // End obnoxious #if 0/#if 1 |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 333 | point[9] = captured_wrapped_encoder; |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 334 | //SmallInitReadings readings; |
| 335 | //{ |
| 336 | //DisableInterrupts disable_interrupts; |
| 337 | //readings = AdcReadSmallInit(disable_interrupts); |
| 338 | //} |
| 339 | //point[10] = readings.motor0_abs; |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 340 | } |
| 341 | |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 342 | #if DO_STEP_RESPONSE |
| 343 | // Step response |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 344 | if (j > kStartupWait + 200 / kSubsampling) { |
| 345 | pwm_ftm_->C3V = 240; |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 346 | } |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 347 | #elif DO_PULSE_SWEEP // DO_STEP_RESPONSE |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 348 | // Sweep the pulse through the ADC sampling points. |
| 349 | static constexpr int kMax = 2500; |
| 350 | static constexpr int kExtraWait = 1500; |
| 351 | if (j > kStartupWait && j < kStartupWait + kExtraWait) { |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 352 | pwm_ftm_->C2V = 0; |
| 353 | pwm_ftm_->C3V = 240; |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 354 | } else if (j < kStartupWait + kMax + kExtraWait) { |
| 355 | uint32_t start = j - kStartupWait - kExtraWait; |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 356 | pwm_ftm_->C2V = start; |
| 357 | pwm_ftm_->C3V = start + 240; |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 358 | } else { |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 359 | pwm_ftm_->C2V = 0; |
| 360 | pwm_ftm_->C3V = 0; |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 361 | } |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 362 | #endif // DO_STEP_RESPONSE/DO_PULSE_SWEEP |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 363 | |
| 364 | ++j; |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 365 | #if SAMPLE_UNTIL_DONE |
| 366 | } else if (false) { |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 367 | #else // SAMPLE_UNTIL_DONE |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 368 | } else if (j < kSamplingEnd) { |
| 369 | ++j; |
| 370 | } else if (j == kSamplingEnd) { |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 371 | #endif // SAMPLE_UNTIL_DONE |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 372 | printf("finished\n"); |
| 373 | ++j; |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 374 | #if SAMPLE_UNTIL_DONE |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 375 | } else if (done) { |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 376 | #else // SAMPLE_UNTIL_DONE |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 377 | } else { |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 378 | #endif // SAMPLE_UNTIL_DONE |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 379 | // Time to write the data out. |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame] | 380 | if (written < (int)sizeof(data) && printing_implementation_ != nullptr) { |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 381 | int to_write = sizeof(data) - written; |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 382 | if (to_write > 64) { |
| 383 | to_write = 64; |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 384 | } |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame] | 385 | int result = printing_implementation_->Write(((const char *)data) + written, to_write); |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 386 | if (result >= 0) { |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 387 | written += result; |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 388 | } else { |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 389 | printf("error\n"); |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 390 | } |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 391 | } |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame] | 392 | #if 0 |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 393 | if (!done_writing && written >= (int)sizeof(data) && |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame] | 394 | printing_implementation_->write_queue_empty()) { |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 395 | printf("done writing %d\n", written); |
| 396 | done_writing = true; |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 397 | } |
Brian Silverman | 4787a6e | 2018-10-06 16:00:54 -0700 | [diff] [blame] | 398 | #endif |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 399 | } |
Austin Schuh | 5b0e6b6 | 2019-04-07 14:23:37 -0700 | [diff] [blame] | 400 | #endif // PRINT_ALL_READINGS/TAKE_SAMPLE |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 401 | (void)balanced; |
| 402 | |
| 403 | // Tell the hardware to use the new switching points. |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 404 | // TODO(Brian): Somehow verify that we consistently hit the first or second |
| 405 | // timer-cycle with the new values (if there's two). |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 406 | pwm_ftm_->PWMLOAD = FTM_PWMLOAD_LDOK; |
| 407 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 408 | #if PRINT_TIMING |
| 409 | static int print_timing_count = 0; |
| 410 | static uint32_t print_timing_total = 0; |
| 411 | print_timing_total += time_subtract(nanos(), start_nanos); |
| 412 | if (++print_timing_count == 1000) { |
| 413 | printf("took %" PRIu32 "/%d\n", print_timing_total, print_timing_count); |
| 414 | print_timing_count = 0; |
| 415 | print_timing_total = 0; |
| 416 | } |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 417 | #endif // PRINT_TIMING |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 418 | |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 419 | // If another cycle has already started, turn the light on right now. |
| 420 | if (pwm_ftm_->SC & FTM_SC_TOF) { |
| 421 | GPIOC_PSOR = 1 << 5; |
| 422 | } |
| 423 | } |
| 424 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 425 | uint32_t Motor::CalculateOnTime(uint32_t width) const { |
| 426 | if (width > 0) { |
| 427 | width += deadtime_compensation_; |
| 428 | if (flip_time_offset_) { |
| 429 | width += 1; |
| 430 | } |
| 431 | } |
| 432 | return (counts_per_cycle() - width) / 2; |
| 433 | } |
| 434 | |
| 435 | uint32_t Motor::CalculateOffTime(uint32_t width) const { |
| 436 | if (width > 0) { |
| 437 | width += deadtime_compensation_; |
| 438 | if (!flip_time_offset_) { |
| 439 | width += 1; |
| 440 | } |
| 441 | } |
| 442 | return (counts_per_cycle() + width) / 2; |
| 443 | } |
| 444 | |
Brian Silverman | a96c1a4 | 2018-05-12 12:11:31 -0700 | [diff] [blame] | 445 | } // namespace motors |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 446 | } // namespace frc971 |