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