blob: 0da7603730183d6277851725b8060e6dc6257f18 [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
Austin Schuh6bdcc372024-06-27 14:49:11 -070024 CHECK(signals != nullptr);
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070025
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;
James Kuszmaul2a951e22024-10-29 22:15:43 -070061 current_limits.StatorCurrentLimit =
62 units::current::ampere_t{stator_current_limit_};
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070063 current_limits.StatorCurrentLimitEnable = true;
James Kuszmaul2a951e22024-10-29 22:15:43 -070064 current_limits.SupplyCurrentLimit =
65 units::current::ampere_t{supply_current_limit_};
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070066 current_limits.SupplyCurrentLimitEnable = true;
67
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070068 ctre::phoenix6::configs::MotorOutputConfigs output_configs;
Maxwell Hendersona8724bf2024-02-23 17:34:31 -080069 output_configs.NeutralMode = neutral_mode_;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070070 output_configs.DutyCycleNeutralDeadband = 0;
71
72 output_configs.Inverted = inverted_;
73
Maxwell Hendersonfcc0d122023-08-05 17:03:34 -070074 ctre::phoenix6::configs::TalonFXConfiguration configuration;
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070075 configuration.CurrentLimits = current_limits;
76 configuration.MotorOutput = output_configs;
77
78 ctre::phoenix::StatusCode status =
79 talon_.GetConfigurator().Apply(configuration);
80 if (!status.IsOK()) {
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080081 AOS_LOG(ERROR, "Failed to set talonfx motor configuration: %s: %s",
Maxwell Hendersonf29e3182023-05-25 06:51:24 -070082 status.GetName(), status.GetDescription());
83 }
84
85 PrintConfigs();
86}
87
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080088ctre::phoenix::StatusCode TalonFX::WriteCurrent(double current,
89 double max_voltage) {
Maxwell Hendersonf8c96892023-06-28 19:55:59 -070090 ctre::phoenix6::controls::TorqueCurrentFOC control(
91 static_cast<units::current::ampere_t>(current));
92 // Using 0_Hz here makes it a one-shot update.
93 control.UpdateFreqHz = 0_Hz;
Maxwell Henderson1c19df92023-12-22 16:50:17 -080094 control.MaxAbsDutyCycle = SafeSpeed(max_voltage);
95 ctre::phoenix::StatusCode status = talon()->SetControl(control);
96 if (!status.IsOK()) {
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080097 AOS_LOG(ERROR, "Failed to write control to talonfx motor %d: %s: %s",
98 device_id(), status.GetName(), status.GetDescription());
Maxwell Henderson1c19df92023-12-22 16:50:17 -080099 }
100
101 return status;
102}
103
Maxwell Henderson10ed5c32024-01-09 12:40:54 -0800104ctre::phoenix::StatusCode TalonFX::WriteVoltage(double voltage) {
Maxwell Henderson1c19df92023-12-22 16:50:17 -0800105 ctre::phoenix6::controls::DutyCycleOut control(SafeSpeed(voltage));
106
107 // Using 0_Hz here makes it a one-shot update.
108 control.UpdateFreqHz = 0_Hz;
109 control.EnableFOC = true;
110
Maxwell Hendersonf8c96892023-06-28 19:55:59 -0700111 ctre::phoenix::StatusCode status = talon()->SetControl(control);
112 if (!status.IsOK()) {
Maxwell Henderson10ed5c32024-01-09 12:40:54 -0800113 AOS_LOG(ERROR, "Failed to write control to talonfx motor %d: %s: %s",
114 device_id(), status.GetName(), status.GetDescription());
Maxwell Hendersonf8c96892023-06-28 19:55:59 -0700115 }
116
117 return status;
118}
Maxwell Hendersondfa609a2024-01-12 20:48:36 -0800119void TalonFX::SerializePosition(control_loops::CANTalonFXStatic *can_talonfx,
Maxwell Henderson10ed5c32024-01-09 12:40:54 -0800120 double gear_ratio) {
Maxwell Hendersondfa609a2024-01-12 20:48:36 -0800121 can_talonfx->set_id(device_id_);
122 can_talonfx->set_device_temp(device_temp());
123 can_talonfx->set_supply_voltage(supply_voltage());
124 can_talonfx->set_supply_current(supply_current());
125 can_talonfx->set_torque_current(torque_current());
126 can_talonfx->set_duty_cycle(duty_cycle());
127 can_talonfx->set_position(position() * gear_ratio);
Maxwell Henderson4d17e4a2024-03-15 15:12:54 -0700128 can_talonfx->set_timestamp(GetTimestamp());
Maxwell Hendersonf29e3182023-05-25 06:51:24 -0700129}