Brian Silverman | d956639 | 2018-06-10 15:02:03 -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 { |
| 13 | namespace motors { |
| 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 | estimated_velocity_ = 0; |
| 25 | filtered_current_ = 0; |
| 26 | } |
| 27 | |
| 28 | static constexpr int constant_counts_per_revolution() { return 2048; } |
Brian Silverman | d956639 | 2018-06-10 15:02:03 -0700 | [diff] [blame] | 29 | |
| 30 | int mechanical_counts_per_revolution() const override { |
| 31 | return constant_counts_per_revolution(); |
| 32 | } |
| 33 | int electrical_counts_per_revolution() const override { |
| 34 | return constant_counts_per_revolution(); |
| 35 | } |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 36 | float scale_current_reading(float reading) const override { return reading; } |
Brian Silverman | d956639 | 2018-06-10 15:02:03 -0700 | [diff] [blame] | 37 | |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 38 | ::std::array<float, 3> DoIteration(const float raw_currents[3], |
| 39 | uint32_t theta, |
| 40 | const float command_current) override; |
Brian Silverman | d956639 | 2018-06-10 15:02:03 -0700 | [diff] [blame] | 41 | |
| 42 | int16_t Debug(uint32_t theta) override; |
| 43 | |
| 44 | float estimated_velocity() const override { return estimated_velocity_; } |
| 45 | |
James Kuszmaul | 998d303 | 2018-09-08 15:41:41 -0700 | [diff] [blame] | 46 | int16_t i_goal(size_t ii) const override { |
| 47 | return static_cast<int16_t>(I_last_[ii] * 10.0f); |
| 48 | } |
| 49 | |
James Kuszmaul | 521eb65 | 2018-10-17 19:09:33 -0700 | [diff] [blame] | 50 | float overall_measured_current() const { return overall_measured_current_; } |
| 51 | |
Brian Silverman | d956639 | 2018-06-10 15:02:03 -0700 | [diff] [blame] | 52 | private: |
| 53 | const ComplexMatrix<3, 1> E1Unrotated_, E2Unrotated_; |
| 54 | |
| 55 | float estimated_velocity_ = 0; |
| 56 | float filtered_current_ = 0; |
James Kuszmaul | 521eb65 | 2018-10-17 19:09:33 -0700 | [diff] [blame] | 57 | float overall_measured_current_ = 0; |
Brian Silverman | d956639 | 2018-06-10 15:02:03 -0700 | [diff] [blame] | 58 | |
| 59 | ::Eigen::Matrix<float, 3, 1> I_last_ = ::Eigen::Matrix<float, 3, 1>::Zero(); |
James Kuszmaul | 521eb65 | 2018-10-17 19:09:33 -0700 | [diff] [blame] | 60 | ::Eigen::Matrix<float, 3, 1> I_prev_ = |
| 61 | ::Eigen::Matrix<float, 3, 1>::Zero(); |
Brian Silverman | d956639 | 2018-06-10 15:02:03 -0700 | [diff] [blame] | 62 | |
| 63 | int16_t debug_[9]; |
| 64 | }; |
| 65 | |
| 66 | } // namespace motors |
| 67 | } // namespace frc971 |
| 68 | |
| 69 | #endif // MOTORS_MOTOR_CONTROLS_H_ |