blob: 603f4a22304cb8bb0cfda0b00ac935de42f4855c [file] [log] [blame]
Maxwell Henderson10ed5c32024-01-09 12:40:54 -08001#ifndef FRC971_WPILIB_TALONFX_MOTOR_H_
2#define FRC971_WPILIB_TALONFX_MOTOR_H_
Maxwell Hendersonf29e3182023-05-25 06:51:24 -07003
4#include <chrono>
5#include <cinttypes>
6#include <vector>
7
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -07008#include "ctre/phoenix6/TalonFX.hpp"
Maxwell Hendersonf29e3182023-05-25 06:51:24 -07009#include "glog/logging.h"
10
Maxwell Hendersonf8c96892023-06-28 19:55:59 -070011#include "aos/commonmath.h"
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070012#include "aos/init.h"
13#include "aos/logging/logging.h"
14#include "frc971/control_loops/drivetrain/drivetrain_can_position_generated.h"
15
16namespace control_loops = ::frc971::control_loops;
17
18namespace frc971 {
19namespace wpilib {
20
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080021struct TalonFXParams {
Maxwell Hendersonacf63682023-08-19 22:18:09 -070022 int device_id;
23 bool inverted;
24};
25
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070026static constexpr units::frequency::hertz_t kCANUpdateFreqHz = 200_Hz;
Maxwell Hendersonf8c96892023-06-28 19:55:59 -070027static constexpr double kMaxBringupPower = 12.0;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070028
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080029// Class which represents a motor controlled by a TalonFX motor controller over
30// CAN.
31class TalonFX {
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070032 public:
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080033 TalonFX(int device_id, bool inverted, std::string canbus,
34 std::vector<ctre::phoenix6::BaseStatusSignal *> *signals,
35 double stator_current_limit, double supply_current_limit);
Maxwell Hendersonacf63682023-08-19 22:18:09 -070036
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080037 TalonFX(TalonFXParams params, std::string canbus,
38 std::vector<ctre::phoenix6::BaseStatusSignal *> *signals,
39 double stator_current_limit, double supply_current_limit);
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070040
41 void PrintConfigs();
42
Maxwell Hendersonacf63682023-08-19 22:18:09 -070043 void WriteConfigs();
Maxwell Hendersonf8c96892023-06-28 19:55:59 -070044 ctre::phoenix::StatusCode WriteCurrent(double current, double max_voltage);
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070045
Maxwell Henderson1c19df92023-12-22 16:50:17 -080046 ctre::phoenix::StatusCode WriteVoltage(double voltage);
47
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070048 ctre::phoenix6::hardware::TalonFX *talon() { return &talon_; }
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070049
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080050 // The position of the TalonFX output shaft is multiplied by gear_ratio
Maxwell Henderson40f774d2023-08-23 10:55:07 -070051 void SerializePosition(flatbuffers::FlatBufferBuilder *fbb,
52 double gear_ratio);
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070053
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080054 std::optional<flatbuffers::Offset<control_loops::CANTalonFX>> TakeOffset();
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070055
56 int device_id() const { return device_id_; }
57 float device_temp() const { return device_temp_.GetValue().value(); }
58 float supply_voltage() const { return supply_voltage_.GetValue().value(); }
59 float supply_current() const { return supply_current_.GetValue().value(); }
60 float torque_current() const { return torque_current_.GetValue().value(); }
61 float duty_cycle() const { return duty_cycle_.GetValue().value(); }
62 float position() const {
63 return static_cast<units::angle::radian_t>(position_.GetValue()).value();
64 }
65
66 // returns the monotonic timestamp of the latest timesynced reading in the
67 // timebase of the the syncronized CAN bus clock.
68 int64_t GetTimestamp() {
69 std::chrono::nanoseconds latest_timestamp =
70 torque_current_.GetTimestamp().GetTime();
71
72 return latest_timestamp.count();
73 }
74
75 void RefreshNontimesyncedSignals() { device_temp_.Refresh(); };
76
77 void set_stator_current_limit(double stator_current_limit) {
78 stator_current_limit_ = stator_current_limit;
79 }
80
81 void set_supply_current_limit(double supply_current_limit) {
82 supply_current_limit_ = supply_current_limit;
83 }
84
Maxwell Henderson1c19df92023-12-22 16:50:17 -080085 static double SafeSpeed(double voltage) {
86 return (::aos::Clip(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
87 }
88
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070089 private:
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070090 ctre::phoenix6::hardware::TalonFX talon_;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070091 int device_id_;
92
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070093 ctre::phoenix6::signals::InvertedValue inverted_;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070094
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070095 ctre::phoenix6::StatusSignal<units::temperature::celsius_t> device_temp_;
96 ctre::phoenix6::StatusSignal<units::voltage::volt_t> supply_voltage_;
97 ctre::phoenix6::StatusSignal<units::current::ampere_t> supply_current_,
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070098 torque_current_;
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070099 ctre::phoenix6::StatusSignal<units::angle::turn_t> position_;
100 ctre::phoenix6::StatusSignal<units::dimensionless::scalar_t> duty_cycle_;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -0700101
102 double stator_current_limit_;
103 double supply_current_limit_;
104
Maxwell Henderson10ed5c32024-01-09 12:40:54 -0800105 std::optional<flatbuffers::Offset<control_loops::CANTalonFX>>
Maxwell Hendersonf29e3182023-05-25 06:51:24 -0700106 last_position_offset_;
107};
108} // namespace wpilib
109} // namespace frc971
Maxwell Henderson10ed5c32024-01-09 12:40:54 -0800110#endif // FRC971_WPILIB_TALONFX_MOTOR_H_