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