blob: c3ff26de6aa1badcbb5a7efd30e4affcbbc9f330 [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
Maxwell Hendersonacf63682023-08-19 22:18:09 -07006Falcon::Falcon(int device_id, bool inverted, 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),
Maxwell Hendersonacf63682023-08-19 22:18:09 -070011 inverted_(inverted),
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070012 device_temp_(talon_.GetDeviceTemp()),
13 supply_voltage_(talon_.GetSupplyVoltage()),
14 supply_current_(talon_.GetSupplyCurrent()),
15 torque_current_(talon_.GetTorqueCurrent()),
16 position_(talon_.GetPosition()),
17 duty_cycle_(talon_.GetDutyCycle()),
18 stator_current_limit_(stator_current_limit),
19 supply_current_limit_(supply_current_limit) {
20 // device temp is not timesynced so don't add it to the list of signals
21 device_temp_.SetUpdateFrequency(kCANUpdateFreqHz);
22
23 CHECK_NOTNULL(signals);
24
25 supply_voltage_.SetUpdateFrequency(kCANUpdateFreqHz);
26 signals->push_back(&supply_voltage_);
27
28 supply_current_.SetUpdateFrequency(kCANUpdateFreqHz);
29 signals->push_back(&supply_current_);
30
31 torque_current_.SetUpdateFrequency(kCANUpdateFreqHz);
32 signals->push_back(&torque_current_);
33
34 position_.SetUpdateFrequency(kCANUpdateFreqHz);
35 signals->push_back(&position_);
36
37 duty_cycle_.SetUpdateFrequency(kCANUpdateFreqHz);
38 signals->push_back(&duty_cycle_);
39}
40
Maxwell Hendersonacf63682023-08-19 22:18:09 -070041Falcon::Falcon(FalconParams params, std::string canbus,
42 std::vector<ctre::phoenix6::BaseStatusSignal *> *signals,
43 double stator_current_limit, double supply_current_limit)
44 : Falcon(params.device_id, params.inverted, canbus, signals,
45 stator_current_limit, supply_current_limit) {}
46
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070047void Falcon::PrintConfigs() {
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070048 ctre::phoenix6::configs::TalonFXConfiguration configuration;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070049 ctre::phoenix::StatusCode status =
50 talon_.GetConfigurator().Refresh(configuration);
51 if (!status.IsOK()) {
52 AOS_LOG(ERROR, "Failed to get falcon configuration: %s: %s",
53 status.GetName(), status.GetDescription());
54 }
55 AOS_LOG(INFO, "configuration: %s", configuration.ToString().c_str());
56}
57
Maxwell Hendersonacf63682023-08-19 22:18:09 -070058void Falcon::WriteConfigs() {
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070059 ctre::phoenix6::configs::CurrentLimitsConfigs current_limits;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070060 current_limits.StatorCurrentLimit = stator_current_limit_;
61 current_limits.StatorCurrentLimitEnable = true;
62 current_limits.SupplyCurrentLimit = supply_current_limit_;
63 current_limits.SupplyCurrentLimitEnable = true;
64
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070065 ctre::phoenix6::configs::MotorOutputConfigs output_configs;
66 output_configs.NeutralMode = ctre::phoenix6::signals::NeutralModeValue::Brake;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070067 output_configs.DutyCycleNeutralDeadband = 0;
68
69 output_configs.Inverted = inverted_;
70
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070071 ctre::phoenix6::configs::TalonFXConfiguration configuration;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070072 configuration.CurrentLimits = current_limits;
73 configuration.MotorOutput = output_configs;
74
75 ctre::phoenix::StatusCode status =
76 talon_.GetConfigurator().Apply(configuration);
77 if (!status.IsOK()) {
78 AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s",
79 status.GetName(), status.GetDescription());
80 }
81
82 PrintConfigs();
83}
84
Maxwell Hendersonf8c96892023-06-28 19:55:59 -070085ctre::phoenix::StatusCode Falcon::WriteCurrent(double current,
86 double max_voltage) {
87 ctre::phoenix6::controls::TorqueCurrentFOC control(
88 static_cast<units::current::ampere_t>(current));
89 // Using 0_Hz here makes it a one-shot update.
90 control.UpdateFreqHz = 0_Hz;
91 control.MaxAbsDutyCycle =
92 ::aos::Clip(max_voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0;
93 ctre::phoenix::StatusCode status = talon()->SetControl(control);
94 if (!status.IsOK()) {
95 AOS_LOG(ERROR, "Failed to write control to falcon %d: %s: %s", device_id(),
96 status.GetName(), status.GetDescription());
97 }
98
99 return status;
100}
101
Maxwell Henderson40f774d2023-08-23 10:55:07 -0700102void Falcon::SerializePosition(flatbuffers::FlatBufferBuilder *fbb,
103 double gear_ratio) {
Maxwell Hendersonf29e3182023-05-25 06:51:24 -0700104 control_loops::CANFalcon::Builder builder(*fbb);
105 builder.add_id(device_id_);
106 builder.add_device_temp(device_temp());
107 builder.add_supply_voltage(supply_voltage());
108 builder.add_supply_current(supply_current());
109 builder.add_torque_current(torque_current());
110 builder.add_duty_cycle(duty_cycle());
Maxwell Henderson40f774d2023-08-23 10:55:07 -0700111 builder.add_position(position() * gear_ratio);
Maxwell Hendersonf29e3182023-05-25 06:51:24 -0700112
113 last_position_offset_ = builder.Finish();
114}
115
116std::optional<flatbuffers::Offset<control_loops::CANFalcon>>
117Falcon::TakeOffset() {
118 auto option_offset = last_position_offset_;
119
120 last_position_offset_.reset();
121
122 return option_offset;
123}