blob: 014556d95893caf0be199b77a152485887b87c5d [file] [log] [blame]
Maxwell Hendersonf29e3182023-05-25 06:51:24 -07001#include "frc971/wpilib/falcon.h"
2
3using frc971::wpilib::Falcon;
Maxwell Hendersonf8c96892023-06-28 19:55:59 -07004using frc971::wpilib::kMaxBringupPower;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -07005
6Falcon::Falcon(int device_id, std::string canbus,
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -07007 std::vector<ctre::phoenix6::BaseStatusSignal *> *signals,
Maxwell Hendersonf29e3182023-05-25 06:51:24 -07008 double stator_current_limit, double supply_current_limit)
9 : talon_(device_id, canbus),
10 device_id_(device_id),
11 device_temp_(talon_.GetDeviceTemp()),
12 supply_voltage_(talon_.GetSupplyVoltage()),
13 supply_current_(talon_.GetSupplyCurrent()),
14 torque_current_(talon_.GetTorqueCurrent()),
15 position_(talon_.GetPosition()),
16 duty_cycle_(talon_.GetDutyCycle()),
17 stator_current_limit_(stator_current_limit),
18 supply_current_limit_(supply_current_limit) {
19 // device temp is not timesynced so don't add it to the list of signals
20 device_temp_.SetUpdateFrequency(kCANUpdateFreqHz);
21
22 CHECK_NOTNULL(signals);
23
24 supply_voltage_.SetUpdateFrequency(kCANUpdateFreqHz);
25 signals->push_back(&supply_voltage_);
26
27 supply_current_.SetUpdateFrequency(kCANUpdateFreqHz);
28 signals->push_back(&supply_current_);
29
30 torque_current_.SetUpdateFrequency(kCANUpdateFreqHz);
31 signals->push_back(&torque_current_);
32
33 position_.SetUpdateFrequency(kCANUpdateFreqHz);
34 signals->push_back(&position_);
35
36 duty_cycle_.SetUpdateFrequency(kCANUpdateFreqHz);
37 signals->push_back(&duty_cycle_);
38}
39
40void Falcon::PrintConfigs() {
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070041 ctre::phoenix6::configs::TalonFXConfiguration configuration;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070042 ctre::phoenix::StatusCode status =
43 talon_.GetConfigurator().Refresh(configuration);
44 if (!status.IsOK()) {
45 AOS_LOG(ERROR, "Failed to get falcon configuration: %s: %s",
46 status.GetName(), status.GetDescription());
47 }
48 AOS_LOG(INFO, "configuration: %s", configuration.ToString().c_str());
49}
50
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070051void Falcon::WriteConfigs(ctre::phoenix6::signals::InvertedValue invert) {
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070052 inverted_ = invert;
53
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070054 ctre::phoenix6::configs::CurrentLimitsConfigs current_limits;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070055 current_limits.StatorCurrentLimit = stator_current_limit_;
56 current_limits.StatorCurrentLimitEnable = true;
57 current_limits.SupplyCurrentLimit = supply_current_limit_;
58 current_limits.SupplyCurrentLimitEnable = true;
59
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070060 ctre::phoenix6::configs::MotorOutputConfigs output_configs;
61 output_configs.NeutralMode = ctre::phoenix6::signals::NeutralModeValue::Brake;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070062 output_configs.DutyCycleNeutralDeadband = 0;
63
64 output_configs.Inverted = inverted_;
65
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070066 ctre::phoenix6::configs::TalonFXConfiguration configuration;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070067 configuration.CurrentLimits = current_limits;
68 configuration.MotorOutput = output_configs;
69
70 ctre::phoenix::StatusCode status =
71 talon_.GetConfigurator().Apply(configuration);
72 if (!status.IsOK()) {
73 AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s",
74 status.GetName(), status.GetDescription());
75 }
76
77 PrintConfigs();
78}
79
Maxwell Hendersonf8c96892023-06-28 19:55:59 -070080ctre::phoenix::StatusCode Falcon::WriteCurrent(double current,
81 double max_voltage) {
82 ctre::phoenix6::controls::TorqueCurrentFOC control(
83 static_cast<units::current::ampere_t>(current));
84 // Using 0_Hz here makes it a one-shot update.
85 control.UpdateFreqHz = 0_Hz;
86 control.MaxAbsDutyCycle =
87 ::aos::Clip(max_voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0;
88 ctre::phoenix::StatusCode status = talon()->SetControl(control);
89 if (!status.IsOK()) {
90 AOS_LOG(ERROR, "Failed to write control to falcon %d: %s: %s", device_id(),
91 status.GetName(), status.GetDescription());
92 }
93
94 return status;
95}
96
Maxwell Henderson40f774d2023-08-23 10:55:07 -070097void Falcon::SerializePosition(flatbuffers::FlatBufferBuilder *fbb,
98 double gear_ratio) {
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070099 control_loops::CANFalcon::Builder builder(*fbb);
100 builder.add_id(device_id_);
101 builder.add_device_temp(device_temp());
102 builder.add_supply_voltage(supply_voltage());
103 builder.add_supply_current(supply_current());
104 builder.add_torque_current(torque_current());
105 builder.add_duty_cycle(duty_cycle());
Maxwell Henderson40f774d2023-08-23 10:55:07 -0700106 builder.add_position(position() * gear_ratio);
Maxwell Hendersonf29e3182023-05-25 06:51:24 -0700107
108 last_position_offset_ = builder.Finish();
109}
110
111std::optional<flatbuffers::Offset<control_loops::CANFalcon>>
112Falcon::TakeOffset() {
113 auto option_offset = last_position_offset_;
114
115 last_position_offset_.reset();
116
117 return option_offset;
118}