blob: 804c7a06f1b21ef994d72eca4b4735f75651e6a9 [file] [log] [blame]
Maxwell Hendersonf29e3182023-05-25 06:51:24 -07001#ifndef FRC971_WPILIB_FALCON_H_
2#define FRC971_WPILIB_FALCON_H_
3
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 Hendersonacf63682023-08-19 22:18:09 -070021struct FalconParams {
22 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
29// Gets info from and writes to falcon motors using the TalonFX controller.
30class Falcon {
31 public:
Maxwell Hendersonacf63682023-08-19 22:18:09 -070032 Falcon(int device_id, bool inverted, std::string canbus,
33 std::vector<ctre::phoenix6::BaseStatusSignal *> *signals,
34 double stator_current_limit, double supply_current_limit);
35
36 Falcon(FalconParams params, std::string canbus,
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070037 std::vector<ctre::phoenix6::BaseStatusSignal *> *signals,
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070038 double stator_current_limit, double supply_current_limit);
39
40 void PrintConfigs();
41
Maxwell Hendersonacf63682023-08-19 22:18:09 -070042 void WriteConfigs();
Maxwell Hendersonf8c96892023-06-28 19:55:59 -070043 ctre::phoenix::StatusCode WriteCurrent(double current, double max_voltage);
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070044
Maxwell Henderson1c19df92023-12-22 16:50:17 -080045 ctre::phoenix::StatusCode WriteVoltage(double voltage);
46
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070047 ctre::phoenix6::hardware::TalonFX *talon() { return &talon_; }
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070048
Maxwell Henderson40f774d2023-08-23 10:55:07 -070049 // The position of the Falcon output shaft is multiplied by gear_ratio
50 void SerializePosition(flatbuffers::FlatBufferBuilder *fbb,
51 double gear_ratio);
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070052
53 std::optional<flatbuffers::Offset<control_loops::CANFalcon>> TakeOffset();
54
55 int device_id() const { return device_id_; }
56 float device_temp() const { return device_temp_.GetValue().value(); }
57 float supply_voltage() const { return supply_voltage_.GetValue().value(); }
58 float supply_current() const { return supply_current_.GetValue().value(); }
59 float torque_current() const { return torque_current_.GetValue().value(); }
60 float duty_cycle() const { return duty_cycle_.GetValue().value(); }
61 float position() const {
62 return static_cast<units::angle::radian_t>(position_.GetValue()).value();
63 }
64
65 // returns the monotonic timestamp of the latest timesynced reading in the
66 // timebase of the the syncronized CAN bus clock.
67 int64_t GetTimestamp() {
68 std::chrono::nanoseconds latest_timestamp =
69 torque_current_.GetTimestamp().GetTime();
70
71 return latest_timestamp.count();
72 }
73
74 void RefreshNontimesyncedSignals() { device_temp_.Refresh(); };
75
76 void set_stator_current_limit(double stator_current_limit) {
77 stator_current_limit_ = stator_current_limit;
78 }
79
80 void set_supply_current_limit(double supply_current_limit) {
81 supply_current_limit_ = supply_current_limit;
82 }
83
Maxwell Henderson1c19df92023-12-22 16:50:17 -080084 static double SafeSpeed(double voltage) {
85 return (::aos::Clip(voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0);
86 }
87
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070088 private:
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070089 ctre::phoenix6::hardware::TalonFX talon_;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070090 int device_id_;
91
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070092 ctre::phoenix6::signals::InvertedValue inverted_;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070093
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070094 ctre::phoenix6::StatusSignal<units::temperature::celsius_t> device_temp_;
95 ctre::phoenix6::StatusSignal<units::voltage::volt_t> supply_voltage_;
96 ctre::phoenix6::StatusSignal<units::current::ampere_t> supply_current_,
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070097 torque_current_;
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070098 ctre::phoenix6::StatusSignal<units::angle::turn_t> position_;
99 ctre::phoenix6::StatusSignal<units::dimensionless::scalar_t> duty_cycle_;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -0700100
101 double stator_current_limit_;
102 double supply_current_limit_;
103
104 std::optional<flatbuffers::Offset<control_loops::CANFalcon>>
105 last_position_offset_;
106};
107} // namespace wpilib
108} // namespace frc971
109#endif // FRC971_WPILIB_FALCON_H_