blob: 7137d82ba9e310a76acddc4abbac586ad084a0a8 [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
8#include "ctre/phoenixpro/TalonFX.hpp"
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
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,
26 std::vector<ctre::phoenixpro::BaseStatusSignalValue *> *signals,
27 double stator_current_limit, double supply_current_limit);
28
29 void PrintConfigs();
30
31 void WriteConfigs(ctre::phoenixpro::signals::InvertedValue invert);
32
33 ctre::phoenixpro::hardware::TalonFX *talon() { return &talon_; }
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:
69 ctre::phoenixpro::hardware::TalonFX talon_;
70 int device_id_;
71
72 ctre::phoenixpro::signals::InvertedValue inverted_;
73
74 ctre::phoenixpro::StatusSignalValue<units::temperature::celsius_t>
75 device_temp_;
76 ctre::phoenixpro::StatusSignalValue<units::voltage::volt_t> supply_voltage_;
77 ctre::phoenixpro::StatusSignalValue<units::current::ampere_t> supply_current_,
78 torque_current_;
79 ctre::phoenixpro::StatusSignalValue<units::angle::turn_t> position_;
80 ctre::phoenixpro::StatusSignalValue<units::dimensionless::scalar_t>
81 duty_cycle_;
82
83 double stator_current_limit_;
84 double supply_current_limit_;
85
86 std::optional<flatbuffers::Offset<control_loops::CANFalcon>>
87 last_position_offset_;
88};
89} // namespace wpilib
90} // namespace frc971
91#endif // FRC971_WPILIB_FALCON_H_