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; |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 61 | current_limits.StatorCurrentLimit = stator_current_limit_; |
| 62 | current_limits.StatorCurrentLimitEnable = true; |
| 63 | current_limits.SupplyCurrentLimit = supply_current_limit_; |
| 64 | current_limits.SupplyCurrentLimitEnable = true; |
James Kuszmaul | 37f97c9 | 2024-03-15 21:27:23 -0700 | [diff] [blame] | 65 | current_limits.SupplyTimeThreshold = 0.0; |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 66 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 67 | ctre::phoenix6::configs::MotorOutputConfigs output_configs; |
Maxwell Henderson | a8724bf | 2024-02-23 17:34:31 -0800 | [diff] [blame] | 68 | output_configs.NeutralMode = neutral_mode_; |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 69 | output_configs.DutyCycleNeutralDeadband = 0; |
| 70 | |
| 71 | output_configs.Inverted = inverted_; |
| 72 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 73 | ctre::phoenix6::configs::TalonFXConfiguration configuration; |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 74 | 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 Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 80 | AOS_LOG(ERROR, "Failed to set talonfx motor configuration: %s: %s", |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 81 | status.GetName(), status.GetDescription()); |
| 82 | } |
| 83 | |
| 84 | PrintConfigs(); |
| 85 | } |
| 86 | |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 87 | ctre::phoenix::StatusCode TalonFX::WriteCurrent(double current, |
| 88 | double max_voltage) { |
Maxwell Henderson | f8c9689 | 2023-06-28 19:55:59 -0700 | [diff] [blame] | 89 | 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 Henderson | 1c19df9 | 2023-12-22 16:50:17 -0800 | [diff] [blame] | 93 | control.MaxAbsDutyCycle = SafeSpeed(max_voltage); |
| 94 | ctre::phoenix::StatusCode status = talon()->SetControl(control); |
| 95 | if (!status.IsOK()) { |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 96 | AOS_LOG(ERROR, "Failed to write control to talonfx motor %d: %s: %s", |
| 97 | device_id(), status.GetName(), status.GetDescription()); |
Maxwell Henderson | 1c19df9 | 2023-12-22 16:50:17 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | return status; |
| 101 | } |
| 102 | |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 103 | ctre::phoenix::StatusCode TalonFX::WriteVoltage(double voltage) { |
Maxwell Henderson | 1c19df9 | 2023-12-22 16:50:17 -0800 | [diff] [blame] | 104 | 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 Henderson | f8c9689 | 2023-06-28 19:55:59 -0700 | [diff] [blame] | 110 | ctre::phoenix::StatusCode status = talon()->SetControl(control); |
| 111 | if (!status.IsOK()) { |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 112 | AOS_LOG(ERROR, "Failed to write control to talonfx motor %d: %s: %s", |
| 113 | device_id(), status.GetName(), status.GetDescription()); |
Maxwell Henderson | f8c9689 | 2023-06-28 19:55:59 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | return status; |
| 117 | } |
Maxwell Henderson | dfa609a | 2024-01-12 20:48:36 -0800 | [diff] [blame] | 118 | void TalonFX::SerializePosition(control_loops::CANTalonFXStatic *can_talonfx, |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 119 | double gear_ratio) { |
Maxwell Henderson | dfa609a | 2024-01-12 20:48:36 -0800 | [diff] [blame] | 120 | 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 Henderson | 4d17e4a | 2024-03-15 15:12:54 -0700 | [diff] [blame] | 127 | can_talonfx->set_timestamp(GetTimestamp()); |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 128 | } |