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