blob: 7959f2f01be47e5f093385fdab36d814564b928d [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 Hendersona8724bf2024-02-23 17:34:31 -080011 neutral_mode_(ctre::phoenix6::signals::NeutralModeValue::Brake),
Maxwell Hendersonacf63682023-08-19 22:18:09 -070012 inverted_(inverted),
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070013 device_temp_(talon_.GetDeviceTemp()),
14 supply_voltage_(talon_.GetSupplyVoltage()),
15 supply_current_(talon_.GetSupplyCurrent()),
16 torque_current_(talon_.GetTorqueCurrent()),
17 position_(talon_.GetPosition()),
18 duty_cycle_(talon_.GetDutyCycle()),
19 stator_current_limit_(stator_current_limit),
20 supply_current_limit_(supply_current_limit) {
21 // device temp is not timesynced so don't add it to the list of signals
22 device_temp_.SetUpdateFrequency(kCANUpdateFreqHz);
23
24 CHECK_NOTNULL(signals);
25
26 supply_voltage_.SetUpdateFrequency(kCANUpdateFreqHz);
27 signals->push_back(&supply_voltage_);
28
29 supply_current_.SetUpdateFrequency(kCANUpdateFreqHz);
30 signals->push_back(&supply_current_);
31
32 torque_current_.SetUpdateFrequency(kCANUpdateFreqHz);
33 signals->push_back(&torque_current_);
34
35 position_.SetUpdateFrequency(kCANUpdateFreqHz);
36 signals->push_back(&position_);
37
38 duty_cycle_.SetUpdateFrequency(kCANUpdateFreqHz);
39 signals->push_back(&duty_cycle_);
40}
41
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080042TalonFX::TalonFX(TalonFXParams params, std::string canbus,
43 std::vector<ctre::phoenix6::BaseStatusSignal *> *signals,
44 double stator_current_limit, double supply_current_limit)
45 : TalonFX(params.device_id, params.inverted, canbus, signals,
46 stator_current_limit, supply_current_limit) {}
Maxwell Hendersonacf63682023-08-19 22:18:09 -070047
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080048void TalonFX::PrintConfigs() {
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070049 ctre::phoenix6::configs::TalonFXConfiguration configuration;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070050 ctre::phoenix::StatusCode status =
51 talon_.GetConfigurator().Refresh(configuration);
52 if (!status.IsOK()) {
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080053 AOS_LOG(ERROR, "Failed to get talonfx motor configuration: %s: %s",
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070054 status.GetName(), status.GetDescription());
55 }
56 AOS_LOG(INFO, "configuration: %s", configuration.ToString().c_str());
57}
58
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080059void TalonFX::WriteConfigs() {
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070060 ctre::phoenix6::configs::CurrentLimitsConfigs current_limits;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070061 current_limits.StatorCurrentLimit = stator_current_limit_;
62 current_limits.StatorCurrentLimitEnable = true;
63 current_limits.SupplyCurrentLimit = supply_current_limit_;
64 current_limits.SupplyCurrentLimitEnable = true;
65
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070066 ctre::phoenix6::configs::MotorOutputConfigs output_configs;
Maxwell Hendersona8724bf2024-02-23 17:34:31 -080067 output_configs.NeutralMode = neutral_mode_;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070068 output_configs.DutyCycleNeutralDeadband = 0;
69
70 output_configs.Inverted = inverted_;
71
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070072 ctre::phoenix6::configs::TalonFXConfiguration configuration;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070073 configuration.CurrentLimits = current_limits;
74 configuration.MotorOutput = output_configs;
75
76 ctre::phoenix::StatusCode status =
77 talon_.GetConfigurator().Apply(configuration);
78 if (!status.IsOK()) {
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080079 AOS_LOG(ERROR, "Failed to set talonfx motor configuration: %s: %s",
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070080 status.GetName(), status.GetDescription());
81 }
82
83 PrintConfigs();
84}
85
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080086ctre::phoenix::StatusCode TalonFX::WriteCurrent(double current,
87 double max_voltage) {
Maxwell Hendersonf8c96892023-06-28 19:55:59 -070088 ctre::phoenix6::controls::TorqueCurrentFOC control(
89 static_cast<units::current::ampere_t>(current));
90 // Using 0_Hz here makes it a one-shot update.
91 control.UpdateFreqHz = 0_Hz;
Maxwell Henderson1c19df92023-12-22 16:50:17 -080092 control.MaxAbsDutyCycle = SafeSpeed(max_voltage);
93 ctre::phoenix::StatusCode status = talon()->SetControl(control);
94 if (!status.IsOK()) {
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080095 AOS_LOG(ERROR, "Failed to write control to talonfx motor %d: %s: %s",
96 device_id(), status.GetName(), status.GetDescription());
Maxwell Henderson1c19df92023-12-22 16:50:17 -080097 }
98
99 return status;
100}
101
Maxwell Henderson10ed5c32024-01-09 12:40:54 -0800102ctre::phoenix::StatusCode TalonFX::WriteVoltage(double voltage) {
Maxwell Henderson1c19df92023-12-22 16:50:17 -0800103 ctre::phoenix6::controls::DutyCycleOut control(SafeSpeed(voltage));
104
105 // Using 0_Hz here makes it a one-shot update.
106 control.UpdateFreqHz = 0_Hz;
107 control.EnableFOC = true;
108
Maxwell Hendersonf8c96892023-06-28 19:55:59 -0700109 ctre::phoenix::StatusCode status = talon()->SetControl(control);
110 if (!status.IsOK()) {
Maxwell Henderson10ed5c32024-01-09 12:40:54 -0800111 AOS_LOG(ERROR, "Failed to write control to talonfx motor %d: %s: %s",
112 device_id(), status.GetName(), status.GetDescription());
Maxwell Hendersonf8c96892023-06-28 19:55:59 -0700113 }
114
115 return status;
116}
Maxwell Hendersondfa609a2024-01-12 20:48:36 -0800117void TalonFX::SerializePosition(control_loops::CANTalonFXStatic *can_talonfx,
Maxwell Henderson10ed5c32024-01-09 12:40:54 -0800118 double gear_ratio) {
Maxwell Hendersondfa609a2024-01-12 20:48:36 -0800119 can_talonfx->set_id(device_id_);
120 can_talonfx->set_device_temp(device_temp());
121 can_talonfx->set_supply_voltage(supply_voltage());
122 can_talonfx->set_supply_current(supply_current());
123 can_talonfx->set_torque_current(torque_current());
124 can_talonfx->set_duty_cycle(duty_cycle());
125 can_talonfx->set_position(position() * gear_ratio);
Maxwell Hendersonf29e3182023-05-25 06:51:24 -0700126}