Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 1 | #ifndef FRC971_WPILIB_FALCON_H_ |
| 2 | #define FRC971_WPILIB_FALCON_H_ |
| 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 | |
| 11 | #include "aos/init.h" |
| 12 | #include "aos/logging/logging.h" |
| 13 | #include "frc971/control_loops/drivetrain/drivetrain_can_position_generated.h" |
| 14 | |
| 15 | namespace control_loops = ::frc971::control_loops; |
| 16 | |
| 17 | namespace frc971 { |
| 18 | namespace wpilib { |
| 19 | |
| 20 | static constexpr units::frequency::hertz_t kCANUpdateFreqHz = 200_Hz; |
| 21 | |
| 22 | // Gets info from and writes to falcon motors using the TalonFX controller. |
| 23 | class Falcon { |
| 24 | public: |
| 25 | Falcon(int device_id, std::string canbus, |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame^] | 26 | std::vector<ctre::phoenix6::BaseStatusSignal *> *signals, |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 27 | double stator_current_limit, double supply_current_limit); |
| 28 | |
| 29 | void PrintConfigs(); |
| 30 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame^] | 31 | void WriteConfigs(ctre::phoenix6::signals::InvertedValue invert); |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 32 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame^] | 33 | ctre::phoenix6::hardware::TalonFX *talon() { return &talon_; } |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 34 | |
| 35 | void SerializePosition(flatbuffers::FlatBufferBuilder *fbb); |
| 36 | |
| 37 | std::optional<flatbuffers::Offset<control_loops::CANFalcon>> TakeOffset(); |
| 38 | |
| 39 | int device_id() const { return device_id_; } |
| 40 | float device_temp() const { return device_temp_.GetValue().value(); } |
| 41 | float supply_voltage() const { return supply_voltage_.GetValue().value(); } |
| 42 | float supply_current() const { return supply_current_.GetValue().value(); } |
| 43 | float torque_current() const { return torque_current_.GetValue().value(); } |
| 44 | float duty_cycle() const { return duty_cycle_.GetValue().value(); } |
| 45 | float position() const { |
| 46 | return static_cast<units::angle::radian_t>(position_.GetValue()).value(); |
| 47 | } |
| 48 | |
| 49 | // returns the monotonic timestamp of the latest timesynced reading in the |
| 50 | // timebase of the the syncronized CAN bus clock. |
| 51 | int64_t GetTimestamp() { |
| 52 | std::chrono::nanoseconds latest_timestamp = |
| 53 | torque_current_.GetTimestamp().GetTime(); |
| 54 | |
| 55 | return latest_timestamp.count(); |
| 56 | } |
| 57 | |
| 58 | void RefreshNontimesyncedSignals() { device_temp_.Refresh(); }; |
| 59 | |
| 60 | void set_stator_current_limit(double stator_current_limit) { |
| 61 | stator_current_limit_ = stator_current_limit; |
| 62 | } |
| 63 | |
| 64 | void set_supply_current_limit(double supply_current_limit) { |
| 65 | supply_current_limit_ = supply_current_limit; |
| 66 | } |
| 67 | |
| 68 | private: |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame^] | 69 | ctre::phoenix6::hardware::TalonFX talon_; |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 70 | int device_id_; |
| 71 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame^] | 72 | ctre::phoenix6::signals::InvertedValue inverted_; |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 73 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame^] | 74 | ctre::phoenix6::StatusSignal<units::temperature::celsius_t> device_temp_; |
| 75 | ctre::phoenix6::StatusSignal<units::voltage::volt_t> supply_voltage_; |
| 76 | ctre::phoenix6::StatusSignal<units::current::ampere_t> supply_current_, |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 77 | torque_current_; |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame^] | 78 | ctre::phoenix6::StatusSignal<units::angle::turn_t> position_; |
| 79 | ctre::phoenix6::StatusSignal<units::dimensionless::scalar_t> duty_cycle_; |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 80 | |
| 81 | double stator_current_limit_; |
| 82 | double supply_current_limit_; |
| 83 | |
| 84 | std::optional<flatbuffers::Offset<control_loops::CANFalcon>> |
| 85 | last_position_offset_; |
| 86 | }; |
| 87 | } // namespace wpilib |
| 88 | } // namespace frc971 |
| 89 | #endif // FRC971_WPILIB_FALCON_H_ |