Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 1 | #ifndef FRC971_WPILIB_TALONFX_MOTOR_H_ |
| 2 | #define FRC971_WPILIB_TALONFX_MOTOR_H_ |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 3 | |
| 4 | #include <chrono> |
| 5 | #include <cinttypes> |
| 6 | #include <vector> |
| 7 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 8 | #include "ctre/phoenix6/TalonFX.hpp" |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 9 | #include "glog/logging.h" |
| 10 | |
Maxwell Henderson | f8c9689 | 2023-06-28 19:55:59 -0700 | [diff] [blame] | 11 | #include "aos/commonmath.h" |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 12 | #include "aos/init.h" |
| 13 | #include "aos/logging/logging.h" |
Maxwell Henderson | dfa609a | 2024-01-12 20:48:36 -0800 | [diff] [blame] | 14 | #include "frc971/control_loops/can_talonfx_static.h" |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 15 | |
| 16 | namespace control_loops = ::frc971::control_loops; |
| 17 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 18 | namespace frc971::wpilib { |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 19 | |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 20 | struct TalonFXParams { |
Maxwell Henderson | acf6368 | 2023-08-19 22:18:09 -0700 | [diff] [blame] | 21 | int device_id; |
| 22 | bool inverted; |
| 23 | }; |
| 24 | |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 25 | static constexpr units::frequency::hertz_t kCANUpdateFreqHz = 200_Hz; |
Maxwell Henderson | f8c9689 | 2023-06-28 19:55:59 -0700 | [diff] [blame] | 26 | static constexpr double kMaxBringupPower = 12.0; |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 27 | |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 28 | // Class which represents a motor controlled by a TalonFX motor controller over |
| 29 | // CAN. |
| 30 | class TalonFX { |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 31 | public: |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 32 | TalonFX(int device_id, bool inverted, std::string canbus, |
| 33 | std::vector<ctre::phoenix6::BaseStatusSignal *> *signals, |
| 34 | double stator_current_limit, double supply_current_limit); |
Maxwell Henderson | acf6368 | 2023-08-19 22:18:09 -0700 | [diff] [blame] | 35 | |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 36 | TalonFX(TalonFXParams params, std::string canbus, |
| 37 | std::vector<ctre::phoenix6::BaseStatusSignal *> *signals, |
| 38 | double stator_current_limit, double supply_current_limit); |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 39 | |
| 40 | void PrintConfigs(); |
| 41 | |
Maxwell Henderson | acf6368 | 2023-08-19 22:18:09 -0700 | [diff] [blame] | 42 | void WriteConfigs(); |
Maxwell Henderson | f8c9689 | 2023-06-28 19:55:59 -0700 | [diff] [blame] | 43 | ctre::phoenix::StatusCode WriteCurrent(double current, double max_voltage); |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 44 | |
Maxwell Henderson | 1c19df9 | 2023-12-22 16:50:17 -0800 | [diff] [blame] | 45 | ctre::phoenix::StatusCode WriteVoltage(double voltage); |
| 46 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 47 | ctre::phoenix6::hardware::TalonFX *talon() { return &talon_; } |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 48 | |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 49 | // The position of the TalonFX output shaft is multiplied by gear_ratio |
Maxwell Henderson | dfa609a | 2024-01-12 20:48:36 -0800 | [diff] [blame] | 50 | void SerializePosition(control_loops::CANTalonFXStatic *can_falcon, |
Maxwell Henderson | 40f774d | 2023-08-23 10:55:07 -0700 | [diff] [blame] | 51 | double gear_ratio); |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 52 | |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 53 | int device_id() const { return device_id_; } |
| 54 | float device_temp() const { return device_temp_.GetValue().value(); } |
| 55 | float supply_voltage() const { return supply_voltage_.GetValue().value(); } |
| 56 | float supply_current() const { return supply_current_.GetValue().value(); } |
| 57 | float torque_current() const { return torque_current_.GetValue().value(); } |
| 58 | float duty_cycle() const { return duty_cycle_.GetValue().value(); } |
| 59 | float position() const { |
| 60 | return static_cast<units::angle::radian_t>(position_.GetValue()).value(); |
| 61 | } |
| 62 | |
| 63 | // returns the monotonic timestamp of the latest timesynced reading in the |
| 64 | // timebase of the the syncronized CAN bus clock. |
| 65 | int64_t GetTimestamp() { |
| 66 | std::chrono::nanoseconds latest_timestamp = |
| 67 | torque_current_.GetTimestamp().GetTime(); |
| 68 | |
| 69 | return latest_timestamp.count(); |
| 70 | } |
| 71 | |
| 72 | void RefreshNontimesyncedSignals() { device_temp_.Refresh(); }; |
| 73 | |
| 74 | void set_stator_current_limit(double stator_current_limit) { |
| 75 | stator_current_limit_ = stator_current_limit; |
| 76 | } |
| 77 | |
| 78 | void set_supply_current_limit(double supply_current_limit) { |
| 79 | supply_current_limit_ = supply_current_limit; |
| 80 | } |
| 81 | |
Maxwell Henderson | a8724bf | 2024-02-23 17:34:31 -0800 | [diff] [blame] | 82 | void set_neutral_mode(ctre::phoenix6::signals::NeutralModeValue value) { |
| 83 | neutral_mode_ = value; |
| 84 | } |
| 85 | |
Maxwell Henderson | 1c19df9 | 2023-12-22 16:50:17 -0800 | [diff] [blame] | 86 | static double SafeSpeed(double voltage) { |
| 87 | return (::aos::Clip(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0); |
| 88 | } |
| 89 | |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 90 | private: |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 91 | ctre::phoenix6::hardware::TalonFX talon_; |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 92 | int device_id_; |
| 93 | |
Maxwell Henderson | a8724bf | 2024-02-23 17:34:31 -0800 | [diff] [blame] | 94 | ctre::phoenix6::signals::NeutralModeValue neutral_mode_; |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 95 | ctre::phoenix6::signals::InvertedValue inverted_; |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 96 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 97 | ctre::phoenix6::StatusSignal<units::temperature::celsius_t> device_temp_; |
| 98 | ctre::phoenix6::StatusSignal<units::voltage::volt_t> supply_voltage_; |
| 99 | ctre::phoenix6::StatusSignal<units::current::ampere_t> supply_current_, |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 100 | torque_current_; |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 101 | ctre::phoenix6::StatusSignal<units::angle::turn_t> position_; |
| 102 | ctre::phoenix6::StatusSignal<units::dimensionless::scalar_t> duty_cycle_; |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 103 | |
| 104 | double stator_current_limit_; |
| 105 | double supply_current_limit_; |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 106 | }; |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 107 | } // namespace frc971::wpilib |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 108 | #endif // FRC971_WPILIB_TALONFX_MOTOR_H_ |