blob: 7f495f1267aefc181ef2dd52d0441f949a2ad90f [file] [log] [blame]
Brian Silvermand9566392018-06-10 15:02:03 -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 Silvermand9566392018-06-10 15:02:03 -07009#include "motors/math.h"
10#include "motors/motor.h"
11
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080012namespace frc971::motors {
Brian Silvermand9566392018-06-10 15:02:03 -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 estimated_velocity_ = 0;
24 filtered_current_ = 0;
25 }
26
27 static constexpr int constant_counts_per_revolution() { return 2048; }
Brian Silvermand9566392018-06-10 15:02:03 -070028
29 int mechanical_counts_per_revolution() const override {
30 return constant_counts_per_revolution();
31 }
32 int electrical_counts_per_revolution() const override {
33 return constant_counts_per_revolution();
34 }
James Kuszmaul998d3032018-09-08 15:41:41 -070035 float scale_current_reading(float reading) const override { return reading; }
Brian Silvermand9566392018-06-10 15:02:03 -070036
James Kuszmaul998d3032018-09-08 15:41:41 -070037 ::std::array<float, 3> DoIteration(const float raw_currents[3],
38 uint32_t theta,
39 const float command_current) override;
Brian Silvermand9566392018-06-10 15:02:03 -070040
41 int16_t Debug(uint32_t theta) override;
42
43 float estimated_velocity() const override { return estimated_velocity_; }
44
James Kuszmaul998d3032018-09-08 15:41:41 -070045 int16_t i_goal(size_t ii) const override {
46 return static_cast<int16_t>(I_last_[ii] * 10.0f);
47 }
48
James Kuszmaul521eb652018-10-17 19:09:33 -070049 float overall_measured_current() const { return overall_measured_current_; }
50
Brian Silvermand9566392018-06-10 15:02:03 -070051 private:
52 const ComplexMatrix<3, 1> E1Unrotated_, E2Unrotated_;
53
54 float estimated_velocity_ = 0;
55 float filtered_current_ = 0;
James Kuszmaul521eb652018-10-17 19:09:33 -070056 float overall_measured_current_ = 0;
Brian Silvermand9566392018-06-10 15:02:03 -070057
58 ::Eigen::Matrix<float, 3, 1> I_last_ = ::Eigen::Matrix<float, 3, 1>::Zero();
Philipp Schrader790cb542023-07-05 21:06:52 -070059 ::Eigen::Matrix<float, 3, 1> I_prev_ = ::Eigen::Matrix<float, 3, 1>::Zero();
Brian Silvermand9566392018-06-10 15:02:03 -070060
61 int16_t debug_[9];
62};
63
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080064} // namespace frc971::motors
Brian Silvermand9566392018-06-10 15:02:03 -070065
66#endif // MOTORS_MOTOR_CONTROLS_H_