Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 1 | #include "frc971/wpilib/falcon.h" |
| 2 | |
| 3 | using frc971::wpilib::Falcon; |
Maxwell Henderson | f8c9689 | 2023-06-28 19:55:59 -0700 | [diff] [blame] | 4 | using frc971::wpilib::kMaxBringupPower; |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 5 | |
| 6 | Falcon::Falcon(int device_id, std::string canbus, |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 7 | std::vector<ctre::phoenix6::BaseStatusSignal *> *signals, |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 8 | double stator_current_limit, double supply_current_limit) |
| 9 | : talon_(device_id, canbus), |
| 10 | device_id_(device_id), |
| 11 | device_temp_(talon_.GetDeviceTemp()), |
| 12 | supply_voltage_(talon_.GetSupplyVoltage()), |
| 13 | supply_current_(talon_.GetSupplyCurrent()), |
| 14 | torque_current_(talon_.GetTorqueCurrent()), |
| 15 | position_(talon_.GetPosition()), |
| 16 | duty_cycle_(talon_.GetDutyCycle()), |
| 17 | stator_current_limit_(stator_current_limit), |
| 18 | supply_current_limit_(supply_current_limit) { |
| 19 | // device temp is not timesynced so don't add it to the list of signals |
| 20 | device_temp_.SetUpdateFrequency(kCANUpdateFreqHz); |
| 21 | |
| 22 | CHECK_NOTNULL(signals); |
| 23 | |
| 24 | supply_voltage_.SetUpdateFrequency(kCANUpdateFreqHz); |
| 25 | signals->push_back(&supply_voltage_); |
| 26 | |
| 27 | supply_current_.SetUpdateFrequency(kCANUpdateFreqHz); |
| 28 | signals->push_back(&supply_current_); |
| 29 | |
| 30 | torque_current_.SetUpdateFrequency(kCANUpdateFreqHz); |
| 31 | signals->push_back(&torque_current_); |
| 32 | |
| 33 | position_.SetUpdateFrequency(kCANUpdateFreqHz); |
| 34 | signals->push_back(&position_); |
| 35 | |
| 36 | duty_cycle_.SetUpdateFrequency(kCANUpdateFreqHz); |
| 37 | signals->push_back(&duty_cycle_); |
| 38 | } |
| 39 | |
| 40 | void Falcon::PrintConfigs() { |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 41 | ctre::phoenix6::configs::TalonFXConfiguration configuration; |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 42 | ctre::phoenix::StatusCode status = |
| 43 | talon_.GetConfigurator().Refresh(configuration); |
| 44 | if (!status.IsOK()) { |
| 45 | AOS_LOG(ERROR, "Failed to get falcon configuration: %s: %s", |
| 46 | status.GetName(), status.GetDescription()); |
| 47 | } |
| 48 | AOS_LOG(INFO, "configuration: %s", configuration.ToString().c_str()); |
| 49 | } |
| 50 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 51 | void Falcon::WriteConfigs(ctre::phoenix6::signals::InvertedValue invert) { |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 52 | inverted_ = invert; |
| 53 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 54 | ctre::phoenix6::configs::CurrentLimitsConfigs current_limits; |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 55 | current_limits.StatorCurrentLimit = stator_current_limit_; |
| 56 | current_limits.StatorCurrentLimitEnable = true; |
| 57 | current_limits.SupplyCurrentLimit = supply_current_limit_; |
| 58 | current_limits.SupplyCurrentLimitEnable = true; |
| 59 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 60 | ctre::phoenix6::configs::MotorOutputConfigs output_configs; |
| 61 | output_configs.NeutralMode = ctre::phoenix6::signals::NeutralModeValue::Brake; |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 62 | output_configs.DutyCycleNeutralDeadband = 0; |
| 63 | |
| 64 | output_configs.Inverted = inverted_; |
| 65 | |
Maxwell Henderson | fcc0d12 | 2023-08-05 17:03:34 -0700 | [diff] [blame] | 66 | ctre::phoenix6::configs::TalonFXConfiguration configuration; |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 67 | configuration.CurrentLimits = current_limits; |
| 68 | configuration.MotorOutput = output_configs; |
| 69 | |
| 70 | ctre::phoenix::StatusCode status = |
| 71 | talon_.GetConfigurator().Apply(configuration); |
| 72 | if (!status.IsOK()) { |
| 73 | AOS_LOG(ERROR, "Failed to set falcon configuration: %s: %s", |
| 74 | status.GetName(), status.GetDescription()); |
| 75 | } |
| 76 | |
| 77 | PrintConfigs(); |
| 78 | } |
| 79 | |
Maxwell Henderson | f8c9689 | 2023-06-28 19:55:59 -0700 | [diff] [blame] | 80 | ctre::phoenix::StatusCode Falcon::WriteCurrent(double current, |
| 81 | double max_voltage) { |
| 82 | ctre::phoenix6::controls::TorqueCurrentFOC control( |
| 83 | static_cast<units::current::ampere_t>(current)); |
| 84 | // Using 0_Hz here makes it a one-shot update. |
| 85 | control.UpdateFreqHz = 0_Hz; |
| 86 | control.MaxAbsDutyCycle = |
| 87 | ::aos::Clip(max_voltage, -kMaxBringupPower, kMaxBringupPower) / 12.0; |
| 88 | ctre::phoenix::StatusCode status = talon()->SetControl(control); |
| 89 | if (!status.IsOK()) { |
| 90 | AOS_LOG(ERROR, "Failed to write control to falcon %d: %s: %s", device_id(), |
| 91 | status.GetName(), status.GetDescription()); |
| 92 | } |
| 93 | |
| 94 | return status; |
| 95 | } |
| 96 | |
Maxwell Henderson | 40f774d | 2023-08-23 10:55:07 -0700 | [diff] [blame^] | 97 | void Falcon::SerializePosition(flatbuffers::FlatBufferBuilder *fbb, |
| 98 | double gear_ratio) { |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 99 | control_loops::CANFalcon::Builder builder(*fbb); |
| 100 | builder.add_id(device_id_); |
| 101 | builder.add_device_temp(device_temp()); |
| 102 | builder.add_supply_voltage(supply_voltage()); |
| 103 | builder.add_supply_current(supply_current()); |
| 104 | builder.add_torque_current(torque_current()); |
| 105 | builder.add_duty_cycle(duty_cycle()); |
Maxwell Henderson | 40f774d | 2023-08-23 10:55:07 -0700 | [diff] [blame^] | 106 | builder.add_position(position() * gear_ratio); |
Maxwell Henderson | f29e318 | 2023-05-25 06:51:24 -0700 | [diff] [blame] | 107 | |
| 108 | last_position_offset_ = builder.Finish(); |
| 109 | } |
| 110 | |
| 111 | std::optional<flatbuffers::Offset<control_loops::CANFalcon>> |
| 112 | Falcon::TakeOffset() { |
| 113 | auto option_offset = last_position_offset_; |
| 114 | |
| 115 | last_position_offset_.reset(); |
| 116 | |
| 117 | return option_offset; |
| 118 | } |