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