blob: 6644ea45038334d9f3f1d790adfc381d7d4265d5 [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
Philipp Schrader790cb542023-07-05 21:06:52 -07007#include "Eigen/Dense"
8
Brian Silverman8d3816a2017-07-03 18:52:15 -07009#include "motors/math.h"
10#include "motors/motor.h"
11
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080012namespace frc971::motors {
Brian Silverman8d3816a2017-07-03 18:52:15 -070013
14class MotorControlsImplementation : public MotorControls {
15 public:
16 template <int kRows, int kCols>
17 using ComplexMatrix = ::Eigen::Matrix<::std::complex<float>, kRows, kCols>;
18
19 MotorControlsImplementation();
20 ~MotorControlsImplementation() override = default;
21
James Kuszmaul998d3032018-09-08 15:41:41 -070022 void Reset() override {}
23
Brian Silverman19ea60f2018-01-03 21:43:15 -080024 static constexpr int constant_counts_per_revolution() { return 1024; }
25
26 int mechanical_counts_per_revolution() const override {
27 return constant_counts_per_revolution();
28 }
29 int electrical_counts_per_revolution() const override {
30 return constant_counts_per_revolution();
31 }
32 float scale_current_reading(float reading) const override {
33 return reading *
34 static_cast<float>(1.0 / 4096.0 /* Full-scale ADC reading */ *
35 3.3 /* ADC reference voltage */ /
36 (1.47 / (0.768 + 1.47)) /* 5V -> 3.3V divider */ /
37 50.0 /* Current sense amplification */ /
38 0.0003 /* Sense resistor */);
39 }
40
James Kuszmaul998d3032018-09-08 15:41:41 -070041 ::std::array<float, 3> DoIteration(const float raw_currents[3],
Philipp Schrader790cb542023-07-05 21:06:52 -070042 uint32_t theta,
43 const float command_current) override;
Brian Silverman8d3816a2017-07-03 18:52:15 -070044
45 int16_t Debug(uint32_t theta) override;
46
47 float estimated_velocity() const override { return estimated_velocity_; }
48
James Kuszmaul998d3032018-09-08 15:41:41 -070049 int16_t i_goal(size_t ii) const override {
50 return static_cast<int16_t>(I_last_[ii] * 10.0f);
51 }
52
James Kuszmaul521eb652018-10-17 19:09:33 -070053 float overall_measured_current() const override {
54 return overall_measured_current_;
55 }
56
Brian Silverman8d3816a2017-07-03 18:52:15 -070057 private:
58 const ComplexMatrix<3, 1> E1Unrotated_, E2Unrotated_;
59
60 float estimated_velocity_ = 0;
61 float filtered_current_ = 0;
James Kuszmaul521eb652018-10-17 19:09:33 -070062 float overall_measured_current_ = 0;
Brian Silverman8d3816a2017-07-03 18:52:15 -070063
64 ::Eigen::Matrix<float, 3, 1> I_last_ = ::Eigen::Matrix<float, 3, 1>::Zero();
65
66 int16_t debug_[9];
67};
68
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080069} // namespace frc971::motors
Brian Silverman8d3816a2017-07-03 18:52:15 -070070
71#endif // MOTORS_MOTOR_CONTROLS_H_