blob: b9514fd696f4374ba3d6562a01e135e83571c44d [file] [log] [blame]
Brian Silverman8d3816a2017-07-03 18:52:15 -07001#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
12namespace frc971 {
Brian Silvermana96c1a42018-05-12 12:11:31 -070013namespace motors {
Brian Silverman8d3816a2017-07-03 18:52:15 -070014
15class 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
Brian Silverman19ea60f2018-01-03 21:43:15 -080023 static constexpr int constant_counts_per_revolution() { return 1024; }
24
25 int mechanical_counts_per_revolution() const override {
26 return constant_counts_per_revolution();
27 }
28 int electrical_counts_per_revolution() const override {
29 return constant_counts_per_revolution();
30 }
31 float scale_current_reading(float reading) const override {
32 return reading *
33 static_cast<float>(1.0 / 4096.0 /* Full-scale ADC reading */ *
34 3.3 /* ADC reference voltage */ /
35 (1.47 / (0.768 + 1.47)) /* 5V -> 3.3V divider */ /
36 50.0 /* Current sense amplification */ /
37 0.0003 /* Sense resistor */);
38 }
39
Brian Silverman8d3816a2017-07-03 18:52:15 -070040 ::std::array<uint32_t, 3> DoIteration(const float raw_currents[3],
41 uint32_t theta,
42 const float command_current) override;
43
44 int16_t Debug(uint32_t theta) override;
45
46 float estimated_velocity() const override { return estimated_velocity_; }
47
48 private:
49 const ComplexMatrix<3, 1> E1Unrotated_, E2Unrotated_;
50
51 float estimated_velocity_ = 0;
52 float filtered_current_ = 0;
53
54 ::Eigen::Matrix<float, 3, 1> I_last_ = ::Eigen::Matrix<float, 3, 1>::Zero();
55
56 int16_t debug_[9];
57};
58
Brian Silvermana96c1a42018-05-12 12:11:31 -070059} // namespace motors
Brian Silverman8d3816a2017-07-03 18:52:15 -070060} // namespace frc971
61
62#endif // MOTORS_MOTOR_CONTROLS_H_