blob: 5e77618bfd00d004568b8a9d0422ad33ee0f727f [file] [log] [blame]
Maxwell Henderson10ed5c32024-01-09 12:40:54 -08001#include "frc971/wpilib/talonfx.h"
Maxwell Hendersonf29e3182023-05-25 06:51:24 -07002
Maxwell Hendersonf8c96892023-06-28 19:55:59 -07003using frc971::wpilib::kMaxBringupPower;
Maxwell Henderson10ed5c32024-01-09 12:40:54 -08004using frc971::wpilib::TalonFX;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -07005
Maxwell Henderson10ed5c32024-01-09 12:40:54 -08006TalonFX::TalonFX(int device_id, bool inverted, std::string canbus,
7 std::vector<ctre::phoenix6::BaseStatusSignal *> *signals,
8 double stator_current_limit, double supply_current_limit)
Maxwell Hendersonf29e3182023-05-25 06:51:24 -07009 : 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 Henderson10ed5c32024-01-09 12:40:54 -080041TalonFX::TalonFX(TalonFXParams params, std::string canbus,
42 std::vector<ctre::phoenix6::BaseStatusSignal *> *signals,
43 double stator_current_limit, double supply_current_limit)
44 : TalonFX(params.device_id, params.inverted, canbus, signals,
45 stator_current_limit, supply_current_limit) {}
Maxwell Hendersonacf63682023-08-19 22:18:09 -070046
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080047void TalonFX::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()) {
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080052 AOS_LOG(ERROR, "Failed to get talonfx motor configuration: %s: %s",
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070053 status.GetName(), status.GetDescription());
54 }
55 AOS_LOG(INFO, "configuration: %s", configuration.ToString().c_str());
56}
57
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080058void TalonFX::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()) {
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080078 AOS_LOG(ERROR, "Failed to set talonfx motor configuration: %s: %s",
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070079 status.GetName(), status.GetDescription());
80 }
81
82 PrintConfigs();
83}
84
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080085ctre::phoenix::StatusCode TalonFX::WriteCurrent(double current,
86 double max_voltage) {
Maxwell Hendersonf8c96892023-06-28 19:55:59 -070087 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;
Maxwell Henderson1c19df92023-12-22 16:50:17 -080091 control.MaxAbsDutyCycle = SafeSpeed(max_voltage);
92 ctre::phoenix::StatusCode status = talon()->SetControl(control);
93 if (!status.IsOK()) {
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080094 AOS_LOG(ERROR, "Failed to write control to talonfx motor %d: %s: %s",
95 device_id(), status.GetName(), status.GetDescription());
Maxwell Henderson1c19df92023-12-22 16:50:17 -080096 }
97
98 return status;
99}
100
Maxwell Henderson10ed5c32024-01-09 12:40:54 -0800101ctre::phoenix::StatusCode TalonFX::WriteVoltage(double voltage) {
Maxwell Henderson1c19df92023-12-22 16:50:17 -0800102 ctre::phoenix6::controls::DutyCycleOut control(SafeSpeed(voltage));
103
104 // Using 0_Hz here makes it a one-shot update.
105 control.UpdateFreqHz = 0_Hz;
106 control.EnableFOC = true;
107
Maxwell Hendersonf8c96892023-06-28 19:55:59 -0700108 ctre::phoenix::StatusCode status = talon()->SetControl(control);
109 if (!status.IsOK()) {
Maxwell Henderson10ed5c32024-01-09 12:40:54 -0800110 AOS_LOG(ERROR, "Failed to write control to talonfx motor %d: %s: %s",
111 device_id(), status.GetName(), status.GetDescription());
Maxwell Hendersonf8c96892023-06-28 19:55:59 -0700112 }
113
114 return status;
115}
Maxwell Hendersondfa609a2024-01-12 20:48:36 -0800116void TalonFX::SerializePosition(control_loops::CANTalonFXStatic *can_talonfx,
Maxwell Henderson10ed5c32024-01-09 12:40:54 -0800117 double gear_ratio) {
Maxwell Hendersondfa609a2024-01-12 20:48:36 -0800118 can_talonfx->set_id(device_id_);
119 can_talonfx->set_device_temp(device_temp());
120 can_talonfx->set_supply_voltage(supply_voltage());
121 can_talonfx->set_supply_current(supply_current());
122 can_talonfx->set_torque_current(torque_current());
123 can_talonfx->set_duty_cycle(duty_cycle());
124 can_talonfx->set_position(position() * gear_ratio);
Maxwell Hendersonf29e3182023-05-25 06:51:24 -0700125}