Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 1 | #ifndef MOTORS_MOTOR_H_ |
| 2 | #define MOTORS_MOTOR_H_ |
| 3 | |
| 4 | #include <limits.h> |
| 5 | |
| 6 | #include <array> |
| 7 | |
| 8 | #include "motors/algorithms.h" |
| 9 | #include "motors/core/kinetis.h" |
| 10 | #include "motors/peripheral/adc.h" |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame^] | 11 | #include "motors/peripheral/configuration.h" |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 12 | #include "motors/util.h" |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame^] | 13 | #include "motors/usb/cdc.h" |
| 14 | #include "motors/core/time.h" |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 15 | |
| 16 | namespace frc971 { |
| 17 | namespace salsa { |
| 18 | |
| 19 | class MotorControls { |
| 20 | public: |
| 21 | MotorControls() = default; |
| 22 | virtual ~MotorControls() = default; |
| 23 | |
| 24 | MotorControls(const MotorControls &) = delete; |
| 25 | void operator=(const MotorControls &) = delete; |
| 26 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame^] | 27 | // Scales a current reading from ADC units to amps. |
| 28 | // |
| 29 | // Note that this doesn't apply any offset. The common offset will be |
| 30 | // automatically removed as part of the balancing process. |
| 31 | virtual float scale_current_reading(float reading) const = 0; |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 32 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame^] | 33 | virtual int mechanical_counts_per_revolution() const = 0; |
| 34 | virtual int electrical_counts_per_revolution() const = 0; |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 35 | |
| 36 | // raw_currents are in amps for each phase. |
| 37 | // theta is in electrical counts, which will be less than |
| 38 | // counts_per_revolution(). |
| 39 | virtual ::std::array<uint32_t, 3> DoIteration( |
| 40 | const float raw_currents[3], uint32_t theta, |
| 41 | const float command_current) = 0; |
| 42 | |
| 43 | virtual int16_t Debug(uint32_t theta) = 0; |
| 44 | |
| 45 | virtual float estimated_velocity() const = 0; |
| 46 | }; |
| 47 | |
| 48 | // Controls a single motor. |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame^] | 49 | class Motor final { |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 50 | public: |
| 51 | // pwm_ftm is used to drive the PWM outputs. |
| 52 | // encoder_ftm is used for reading the encoder. |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame^] | 53 | Motor(BigFTM *pwm_ftm, LittleFTM *encoder_ftm, MotorControls *controls, |
| 54 | const ::std::array<volatile uint32_t *, 3> &output_registers); |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 55 | |
| 56 | Motor(const Motor &) = delete; |
| 57 | void operator=(const Motor &) = delete; |
| 58 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame^] | 59 | void set_debug_tty(teensy::AcmTty *debug_tty) { debug_tty_ = debug_tty; } |
| 60 | void set_deadtime_compensation(int deadtime_compensation) { |
| 61 | deadtime_compensation_ = deadtime_compensation; |
| 62 | } |
| 63 | void set_switching_divisor(int switching_divisor) { |
| 64 | switching_divisor_ = switching_divisor; |
| 65 | } |
| 66 | void set_encoder_offset(int encoder_offset) { |
| 67 | encoder_offset_ = encoder_offset; |
| 68 | // Add mechanical_counts_per_revolution to the offset so that when we mod |
| 69 | // below, we are guaranteed to be > 0 regardless of the encoder multiplier. |
| 70 | // % isn't well-defined with negative numbers. |
| 71 | while (encoder_offset_ < controls_->mechanical_counts_per_revolution()) { |
| 72 | encoder_offset_ += controls_->mechanical_counts_per_revolution(); |
| 73 | } |
| 74 | } |
| 75 | void set_encoder_multiplier(int encoder_multiplier) { |
| 76 | encoder_multiplier_ = encoder_multiplier; |
| 77 | } |
| 78 | |
| 79 | int encoder() { |
| 80 | return encoder_multiplier_ * encoder_ftm_->CNT; |
| 81 | } |
| 82 | uint32_t wrapped_encoder() { |
| 83 | return (encoder() + encoder_offset_) % |
| 84 | controls_->mechanical_counts_per_revolution(); |
| 85 | } |
| 86 | |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 87 | // Sets up everything but doesn't actually start the timers. |
| 88 | // |
| 89 | // This assumes the global time base configuration happens outside so the |
| 90 | // timers for both motors (if applicable) are synced up. |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 91 | void Init(); |
| 92 | |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 93 | // Starts the timers. |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame^] | 94 | // |
| 95 | // If the global time base is in use, it must be activated after this. |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 96 | void Start(); |
| 97 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame^] | 98 | void HandleInterrupt(const BalancedReadings &readings, |
| 99 | uint32_t captured_wrapped_encoder); |
| 100 | |
| 101 | void SetGoalCurrent(float goal_current) { |
| 102 | DisableInterrupts disable_interrupts; |
| 103 | goal_current_ = goal_current; |
| 104 | last_current_set_time_ = micros(); |
| 105 | } |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 106 | |
| 107 | private: |
| 108 | // Represents the ADC reading which is closest to an edge. |
| 109 | struct CloseAdcReading { |
| 110 | // Adds a new reading to the readings to balance or pushes the previous |
| 111 | // closest one there and saves off this one. |
| 112 | // |
| 113 | // Returns true if it saves off the new reading. |
| 114 | bool MaybeUse(int new_distance, const MediumAdcReadings &adc_readings, |
| 115 | int phase, int sample, ReadingsToBalance *to_balance) { |
| 116 | if (new_distance < distance) { |
| 117 | if (distance != INT_MAX) { |
| 118 | to_balance->Add(index, value); |
| 119 | } |
| 120 | distance = new_distance; |
| 121 | value = adc_readings.motor_currents[phase][sample]; |
| 122 | index = phase; |
| 123 | return true; |
| 124 | } |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | int distance = INT_MAX; |
| 129 | int32_t value = 0; |
| 130 | int index = 0; |
| 131 | }; |
| 132 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame^] | 133 | inline int counts_per_cycle() const { |
| 134 | return BUS_CLOCK_FREQUENCY / SWITCHING_FREQUENCY / switching_divisor_; |
| 135 | } |
| 136 | |
| 137 | uint32_t CalculateOnTime(uint32_t width) const; |
| 138 | uint32_t CalculateOffTime(uint32_t width) const; |
| 139 | |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 140 | bool flip_time_offset_ = false; |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame^] | 141 | int deadtime_compensation_ = 0; |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 142 | |
| 143 | BigFTM *const pwm_ftm_; |
| 144 | LittleFTM *const encoder_ftm_; |
| 145 | MotorControls *const controls_; |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame^] | 146 | const ::std::array<volatile uint32_t *, 3> output_registers_; |
| 147 | |
| 148 | float goal_current_ = 0; |
| 149 | uint32_t last_current_set_time_ = 0; |
| 150 | int switching_divisor_ = 1; |
| 151 | int encoder_offset_ = 0; |
| 152 | int encoder_multiplier_ = 1; |
| 153 | |
| 154 | teensy::AcmTty *debug_tty_ = nullptr; |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | } // namespace salsa |
| 158 | } // namespace frc971 |
| 159 | |
| 160 | #endif // MOTORS_MOTOR_H_ |