blob: f6858fd27bbf29ca89f00c5d32970825bdb78a8d [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
11#include "aos/init.h"
12#include "aos/logging/logging.h"
13#include "frc971/control_loops/drivetrain/drivetrain_can_position_generated.h"
14
15namespace control_loops = ::frc971::control_loops;
16
17namespace frc971 {
18namespace wpilib {
19
20static constexpr units::frequency::hertz_t kCANUpdateFreqHz = 200_Hz;
21
22// Gets info from and writes to falcon motors using the TalonFX controller.
23class Falcon {
24 public:
25 Falcon(int device_id, std::string canbus,
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070026 std::vector<ctre::phoenix6::BaseStatusSignal *> *signals,
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070027 double stator_current_limit, double supply_current_limit);
28
29 void PrintConfigs();
30
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070031 void WriteConfigs(ctre::phoenix6::signals::InvertedValue invert);
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070032
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070033 ctre::phoenix6::hardware::TalonFX *talon() { return &talon_; }
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070034
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 Hendersonfcc0d122023-08-05 17:03:34 -070069 ctre::phoenix6::hardware::TalonFX talon_;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070070 int device_id_;
71
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070072 ctre::phoenix6::signals::InvertedValue inverted_;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070073
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070074 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 Hendersonf29e3182023-05-25 06:51:24 -070077 torque_current_;
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070078 ctre::phoenix6::StatusSignal<units::angle::turn_t> position_;
79 ctre::phoenix6::StatusSignal<units::dimensionless::scalar_t> duty_cycle_;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070080
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_