Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 1 | #include "frc971/wpilib/talonfx.h" |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 2 | |
Maxwell Henderson | f8c9689 | 2023-06-28 19:55:59 -0700 | [diff] [blame] | 3 | using frc971::wpilib::kMaxBringupPower; |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 4 | using frc971::wpilib::TalonFX; |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 5 | |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 6 | TalonFX::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 Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 9 | : talon_(device_id, canbus), |
| 10 | device_id_(device_id), |
Maxwell Henderson | a8724bf | 2024-02-23 17:34:31 -0800 | [diff] [blame] | 11 | neutral_mode_(ctre::phoenix6::signals::NeutralModeValue::Brake), |
Maxwell Henderson | acf6368 | 2023-08-19 22:18:09 -0700 | [diff] [blame] | 12 | inverted_(inverted), |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 13 | 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 Schuh | 6bdcc37 | 2024-06-27 14:49:11 -0700 | [diff] [blame] | 24 | CHECK(signals != nullptr); |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 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 Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 42 | TalonFX::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 Henderson | acf6368 | 2023-08-19 22:18:09 -0700 | [diff] [blame] | 47 | |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 48 | void TalonFX::PrintConfigs() { |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 49 | ctre::phoenix6::configs::TalonFXConfiguration configuration; |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 50 | ctre::phoenix::StatusCode status = |
| 51 | talon_.GetConfigurator().Refresh(configuration); |
| 52 | if (!status.IsOK()) { |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 53 | AOS_LOG(ERROR, "Failed to get talonfx motor configuration: %s: %s", |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 54 | status.GetName(), status.GetDescription()); |
| 55 | } |
| 56 | AOS_LOG(INFO, "configuration: %s", configuration.ToString().c_str()); |
| 57 | } |
| 58 | |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 59 | void TalonFX::WriteConfigs() { |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 60 | ctre::phoenix6::configs::CurrentLimitsConfigs current_limits; |
James Kuszmaul | 2a951e2 | 2024-10-29 22:15:43 -0700 | [diff] [blame^] | 61 | current_limits.StatorCurrentLimit = |
| 62 | units::current::ampere_t{stator_current_limit_}; |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 63 | current_limits.StatorCurrentLimitEnable = true; |
James Kuszmaul | 2a951e2 | 2024-10-29 22:15:43 -0700 | [diff] [blame^] | 64 | current_limits.SupplyCurrentLimit = |
| 65 | units::current::ampere_t{supply_current_limit_}; |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 66 | current_limits.SupplyCurrentLimitEnable = true; |
| 67 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 68 | ctre::phoenix6::configs::MotorOutputConfigs output_configs; |
Maxwell Henderson | a8724bf | 2024-02-23 17:34:31 -0800 | [diff] [blame] | 69 | output_configs.NeutralMode = neutral_mode_; |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 70 | output_configs.DutyCycleNeutralDeadband = 0; |
| 71 | |
| 72 | output_configs.Inverted = inverted_; |
| 73 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 74 | ctre::phoenix6::configs::TalonFXConfiguration configuration; |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 75 | 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 Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 81 | AOS_LOG(ERROR, "Failed to set talonfx motor configuration: %s: %s", |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 82 | status.GetName(), status.GetDescription()); |
| 83 | } |
| 84 | |
| 85 | PrintConfigs(); |
| 86 | } |
| 87 | |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 88 | ctre::phoenix::StatusCode TalonFX::WriteCurrent(double current, |
| 89 | double max_voltage) { |
Maxwell Henderson | f8c9689 | 2023-06-28 19:55:59 -0700 | [diff] [blame] | 90 | 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 Henderson | 1c19df9 | 2023-12-22 16:50:17 -0800 | [diff] [blame] | 94 | control.MaxAbsDutyCycle = SafeSpeed(max_voltage); |
| 95 | ctre::phoenix::StatusCode status = talon()->SetControl(control); |
| 96 | if (!status.IsOK()) { |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 97 | AOS_LOG(ERROR, "Failed to write control to talonfx motor %d: %s: %s", |
| 98 | device_id(), status.GetName(), status.GetDescription()); |
Maxwell Henderson | 1c19df9 | 2023-12-22 16:50:17 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | return status; |
| 102 | } |
| 103 | |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 104 | ctre::phoenix::StatusCode TalonFX::WriteVoltage(double voltage) { |
Maxwell Henderson | 1c19df9 | 2023-12-22 16:50:17 -0800 | [diff] [blame] | 105 | 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 Henderson | f8c9689 | 2023-06-28 19:55:59 -0700 | [diff] [blame] | 111 | ctre::phoenix::StatusCode status = talon()->SetControl(control); |
| 112 | if (!status.IsOK()) { |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 113 | AOS_LOG(ERROR, "Failed to write control to talonfx motor %d: %s: %s", |
| 114 | device_id(), status.GetName(), status.GetDescription()); |
Maxwell Henderson | f8c9689 | 2023-06-28 19:55:59 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | return status; |
| 118 | } |
Maxwell Henderson | dfa609a | 2024-01-12 20:48:36 -0800 | [diff] [blame] | 119 | void TalonFX::SerializePosition(control_loops::CANTalonFXStatic *can_talonfx, |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 120 | double gear_ratio) { |
Maxwell Henderson | dfa609a | 2024-01-12 20:48:36 -0800 | [diff] [blame] | 121 | 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 Henderson | 4d17e4a | 2024-03-15 15:12:54 -0700 | [diff] [blame] | 128 | can_talonfx->set_timestamp(GetTimestamp()); |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 129 | } |