blob: ba88548413787437e32dad1b4167fb5be38ceaee [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;
James Kuszmaul37f97c92024-03-15 21:27:23 -070065 current_limits.SupplyTimeThreshold = 0.0;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070066
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070067 ctre::phoenix6::configs::MotorOutputConfigs output_configs;
Maxwell Hendersona8724bf2024-02-23 17:34:31 -080068 output_configs.NeutralMode = neutral_mode_;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070069 output_configs.DutyCycleNeutralDeadband = 0;
70
71 output_configs.Inverted = inverted_;
72
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070073 ctre::phoenix6::configs::TalonFXConfiguration configuration;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070074 configuration.CurrentLimits = current_limits;
75 configuration.MotorOutput = output_configs;
76
77 ctre::phoenix::StatusCode status =
78 talon_.GetConfigurator().Apply(configuration);
79 if (!status.IsOK()) {
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080080 AOS_LOG(ERROR, "Failed to set talonfx motor configuration: %s: %s",
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070081 status.GetName(), status.GetDescription());
82 }
83
84 PrintConfigs();
85}
86
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080087ctre::phoenix::StatusCode TalonFX::WriteCurrent(double current,
88 double max_voltage) {
Maxwell Hendersonf8c96892023-06-28 19:55:59 -070089 ctre::phoenix6::controls::TorqueCurrentFOC control(
90 static_cast<units::current::ampere_t>(current));
91 // Using 0_Hz here makes it a one-shot update.
92 control.UpdateFreqHz = 0_Hz;
Maxwell Henderson1c19df92023-12-22 16:50:17 -080093 control.MaxAbsDutyCycle = SafeSpeed(max_voltage);
94 ctre::phoenix::StatusCode status = talon()->SetControl(control);
95 if (!status.IsOK()) {
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080096 AOS_LOG(ERROR, "Failed to write control to talonfx motor %d: %s: %s",
97 device_id(), status.GetName(), status.GetDescription());
Maxwell Henderson1c19df92023-12-22 16:50:17 -080098 }
99
100 return status;
101}
102
Maxwell Henderson10ed5c32024-01-09 12:40:54 -0800103ctre::phoenix::StatusCode TalonFX::WriteVoltage(double voltage) {
Maxwell Henderson1c19df92023-12-22 16:50:17 -0800104 ctre::phoenix6::controls::DutyCycleOut control(SafeSpeed(voltage));
105
106 // Using 0_Hz here makes it a one-shot update.
107 control.UpdateFreqHz = 0_Hz;
108 control.EnableFOC = true;
109
Maxwell Hendersonf8c96892023-06-28 19:55:59 -0700110 ctre::phoenix::StatusCode status = talon()->SetControl(control);
111 if (!status.IsOK()) {
Maxwell Henderson10ed5c32024-01-09 12:40:54 -0800112 AOS_LOG(ERROR, "Failed to write control to talonfx motor %d: %s: %s",
113 device_id(), status.GetName(), status.GetDescription());
Maxwell Hendersonf8c96892023-06-28 19:55:59 -0700114 }
115
116 return status;
117}
Maxwell Hendersondfa609a2024-01-12 20:48:36 -0800118void TalonFX::SerializePosition(control_loops::CANTalonFXStatic *can_talonfx,
Maxwell Henderson10ed5c32024-01-09 12:40:54 -0800119 double gear_ratio) {
Maxwell Hendersondfa609a2024-01-12 20:48:36 -0800120 can_talonfx->set_id(device_id_);
121 can_talonfx->set_device_temp(device_temp());
122 can_talonfx->set_supply_voltage(supply_voltage());
123 can_talonfx->set_supply_current(supply_current());
124 can_talonfx->set_torque_current(torque_current());
125 can_talonfx->set_duty_cycle(duty_cycle());
126 can_talonfx->set_position(position() * gear_ratio);
Maxwell Hendersonf29e3182023-05-25 06:51:24 -0700127}