Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 1 | #ifndef MOTORS_MOTOR_CONTROLS_H_ |
| 2 | #define MOTORS_MOTOR_CONTROLS_H_ |
| 3 | |
| 4 | #include <array> |
| 5 | #include <complex> |
| 6 | |
| 7 | #include "motors/math.h" |
| 8 | #include "motors/motor.h" |
| 9 | |
| 10 | #include "Eigen/Dense" |
| 11 | |
| 12 | namespace frc971 { |
Brian Silverman | a96c1a4 | 2018-05-12 12:11:31 -0700 | [diff] [blame] | 13 | namespace motors { |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 14 | |
| 15 | class MotorControlsImplementation : public MotorControls { |
| 16 | public: |
| 17 | template <int kRows, int kCols> |
| 18 | using ComplexMatrix = ::Eigen::Matrix<::std::complex<float>, kRows, kCols>; |
| 19 | |
| 20 | MotorControlsImplementation(); |
| 21 | ~MotorControlsImplementation() override = default; |
| 22 | |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 23 | void Reset() override {} |
| 24 | |
Brian Silverman | 19ea60f | 2018-01-03 21:43:15 -0800 | [diff] [blame] | 25 | static constexpr int constant_counts_per_revolution() { return 1024; } |
| 26 | |
| 27 | int mechanical_counts_per_revolution() const override { |
| 28 | return constant_counts_per_revolution(); |
| 29 | } |
| 30 | int electrical_counts_per_revolution() const override { |
| 31 | return constant_counts_per_revolution(); |
| 32 | } |
| 33 | float scale_current_reading(float reading) const override { |
| 34 | return reading * |
| 35 | static_cast<float>(1.0 / 4096.0 /* Full-scale ADC reading */ * |
| 36 | 3.3 /* ADC reference voltage */ / |
| 37 | (1.47 / (0.768 + 1.47)) /* 5V -> 3.3V divider */ / |
| 38 | 50.0 /* Current sense amplification */ / |
| 39 | 0.0003 /* Sense resistor */); |
| 40 | } |
| 41 | |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 42 | ::std::array<float, 3> DoIteration(const float raw_currents[3], |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 43 | uint32_t theta, |
| 44 | const float command_current) override; |
| 45 | |
| 46 | int16_t Debug(uint32_t theta) override; |
| 47 | |
| 48 | float estimated_velocity() const override { return estimated_velocity_; } |
| 49 | |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 50 | int16_t i_goal(size_t ii) const override { |
| 51 | return static_cast<int16_t>(I_last_[ii] * 10.0f); |
| 52 | } |
| 53 | |
James Kuszmaul | 521eb65 | 2018-10-17 19:09:33 -0700 | [diff] [blame] | 54 | float overall_measured_current() const override { |
| 55 | return overall_measured_current_; |
| 56 | } |
| 57 | |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 58 | private: |
| 59 | const ComplexMatrix<3, 1> E1Unrotated_, E2Unrotated_; |
| 60 | |
| 61 | float estimated_velocity_ = 0; |
| 62 | float filtered_current_ = 0; |
James Kuszmaul | 521eb65 | 2018-10-17 19:09:33 -0700 | [diff] [blame] | 63 | float overall_measured_current_ = 0; |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 64 | |
| 65 | ::Eigen::Matrix<float, 3, 1> I_last_ = ::Eigen::Matrix<float, 3, 1>::Zero(); |
| 66 | |
| 67 | int16_t debug_[9]; |
| 68 | }; |
| 69 | |
Brian Silverman | a96c1a4 | 2018-05-12 12:11:31 -0700 | [diff] [blame] | 70 | } // namespace motors |
Brian Silverman | 8d3816a | 2017-07-03 18:52:15 -0700 | [diff] [blame] | 71 | } // namespace frc971 |
| 72 | |
| 73 | #endif // MOTORS_MOTOR_CONTROLS_H_ |