blob: 3f551ebe118e6dfb42cd4d330f1393bbfa2baa1f [file] [log] [blame]
Maxwell Hendersonf29e3182023-05-25 06:51:24 -07001#include "frc971/wpilib/falcon.h"
2
3using frc971::wpilib::Falcon;
4
5Falcon::Falcon(int device_id, std::string canbus,
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -07006 std::vector<ctre::phoenix6::BaseStatusSignal *> *signals,
Maxwell Hendersonf29e3182023-05-25 06:51:24 -07007 double stator_current_limit, double supply_current_limit)
8 : talon_(device_id, canbus),
9 device_id_(device_id),
10 device_temp_(talon_.GetDeviceTemp()),
11 supply_voltage_(talon_.GetSupplyVoltage()),
12 supply_current_(talon_.GetSupplyCurrent()),
13 torque_current_(talon_.GetTorqueCurrent()),
14 position_(talon_.GetPosition()),
15 duty_cycle_(talon_.GetDutyCycle()),
16 stator_current_limit_(stator_current_limit),
17 supply_current_limit_(supply_current_limit) {
18 // device temp is not timesynced so don't add it to the list of signals
19 device_temp_.SetUpdateFrequency(kCANUpdateFreqHz);
20
21 CHECK_NOTNULL(signals);
22
23 supply_voltage_.SetUpdateFrequency(kCANUpdateFreqHz);
24 signals->push_back(&supply_voltage_);
25
26 supply_current_.SetUpdateFrequency(kCANUpdateFreqHz);
27 signals->push_back(&supply_current_);
28
29 torque_current_.SetUpdateFrequency(kCANUpdateFreqHz);
30 signals->push_back(&torque_current_);
31
32 position_.SetUpdateFrequency(kCANUpdateFreqHz);
33 signals->push_back(&position_);
34
35 duty_cycle_.SetUpdateFrequency(kCANUpdateFreqHz);
36 signals->push_back(&duty_cycle_);
37}
38
39void Falcon::PrintConfigs() {
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070040 ctre::phoenix6::configs::TalonFXConfiguration configuration;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070041 ctre::phoenix::StatusCode status =
42 talon_.GetConfigurator().Refresh(configuration);
43 if (!status.IsOK()) {
44 AOS_LOG(ERROR, "Failed to get falcon configuration: %s: %s",
45 status.GetName(), status.GetDescription());
46 }
47 AOS_LOG(INFO, "configuration: %s", configuration.ToString().c_str());
48}
49
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070050void Falcon::WriteConfigs(ctre::phoenix6::signals::InvertedValue invert) {
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070051 inverted_ = invert;
52
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070053 ctre::phoenix6::configs::CurrentLimitsConfigs current_limits;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070054 current_limits.StatorCurrentLimit = stator_current_limit_;
55 current_limits.StatorCurrentLimitEnable = true;
56 current_limits.SupplyCurrentLimit = supply_current_limit_;
57 current_limits.SupplyCurrentLimitEnable = true;
58
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070059 ctre::phoenix6::configs::MotorOutputConfigs output_configs;
60 output_configs.NeutralMode = ctre::phoenix6::signals::NeutralModeValue::Brake;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070061 output_configs.DutyCycleNeutralDeadband = 0;
62
63 output_configs.Inverted = inverted_;
64
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070065 ctre::phoenix6::configs::TalonFXConfiguration configuration;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070066 configuration.CurrentLimits = current_limits;
67 configuration.MotorOutput = output_configs;
68
69 ctre::phoenix::StatusCode status =
70 talon_.GetConfigurator().Apply(configuration);
71 if (!status.IsOK()) {
72 AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s",
73 status.GetName(), status.GetDescription());
74 }
75
76 PrintConfigs();
77}
78
79void Falcon::SerializePosition(flatbuffers::FlatBufferBuilder *fbb) {
80 control_loops::CANFalcon::Builder builder(*fbb);
81 builder.add_id(device_id_);
82 builder.add_device_temp(device_temp());
83 builder.add_supply_voltage(supply_voltage());
84 builder.add_supply_current(supply_current());
85 builder.add_torque_current(torque_current());
86 builder.add_duty_cycle(duty_cycle());
87 builder.add_position(position());
88
89 last_position_offset_ = builder.Finish();
90}
91
92std::optional<flatbuffers::Offset<control_loops::CANFalcon>>
93Falcon::TakeOffset() {
94 auto option_offset = last_position_offset_;
95
96 last_position_offset_.reset();
97
98 return option_offset;
99}