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