Maxwell Henderson | 74059a4 | 2023-12-22 17:40:15 -0800 | [diff] [blame] | 1 | #include "frc971/wpilib/can_drivetrain_writer.h" |
| 2 | |
| 3 | using frc971::wpilib::CANDrivetrainWriter; |
| 4 | |
| 5 | CANDrivetrainWriter::CANDrivetrainWriter(::aos::EventLoop *event_loop) |
| 6 | : ::frc971::wpilib::LoopOutputHandler< |
| 7 | ::frc971::control_loops::drivetrain::Output>(event_loop, |
| 8 | "/drivetrain") { |
| 9 | event_loop->SetRuntimeRealtimePriority(kDrivetrainWriterPriority); |
| 10 | |
| 11 | event_loop->OnRun([this]() { WriteConfigs(); }); |
| 12 | } |
| 13 | |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 14 | void CANDrivetrainWriter::set_talonfxs( |
| 15 | std::vector<std::shared_ptr<TalonFX>> right_talonfxs, |
| 16 | std::vector<std::shared_ptr<TalonFX>> left_talonfxs) { |
| 17 | right_talonfxs_ = std::move(right_talonfxs); |
| 18 | left_talonfxs_ = std::move(left_talonfxs); |
Maxwell Henderson | 74059a4 | 2023-12-22 17:40:15 -0800 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | void CANDrivetrainWriter::HandleCANConfiguration( |
| 22 | const CANConfiguration &configuration) { |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 23 | for (auto talonfx : right_talonfxs_) { |
| 24 | talonfx->PrintConfigs(); |
Maxwell Henderson | 74059a4 | 2023-12-22 17:40:15 -0800 | [diff] [blame] | 25 | } |
| 26 | |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 27 | for (auto talonfx : left_talonfxs_) { |
| 28 | talonfx->PrintConfigs(); |
Maxwell Henderson | 74059a4 | 2023-12-22 17:40:15 -0800 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | if (configuration.reapply()) { |
| 32 | WriteConfigs(); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | void CANDrivetrainWriter::WriteConfigs() { |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 37 | for (auto talonfx : right_talonfxs_) { |
| 38 | talonfx->WriteConfigs(); |
Maxwell Henderson | 74059a4 | 2023-12-22 17:40:15 -0800 | [diff] [blame] | 39 | } |
| 40 | |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 41 | for (auto talonfx : left_talonfxs_) { |
| 42 | talonfx->WriteConfigs(); |
Maxwell Henderson | 74059a4 | 2023-12-22 17:40:15 -0800 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
| 46 | void CANDrivetrainWriter::Write( |
| 47 | const ::frc971::control_loops::drivetrain::Output &output) { |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 48 | for (auto talonfx : right_talonfxs_) { |
| 49 | talonfx->WriteVoltage(output.right_voltage()); |
Maxwell Henderson | 74059a4 | 2023-12-22 17:40:15 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 52 | for (auto talonfx : left_talonfxs_) { |
| 53 | talonfx->WriteVoltage(output.left_voltage()); |
Maxwell Henderson | 74059a4 | 2023-12-22 17:40:15 -0800 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
| 57 | void CANDrivetrainWriter::Stop() { |
| 58 | AOS_LOG(WARNING, "Drivetrain CAN output too old.\n"); |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 59 | for (auto talonfx : right_talonfxs_) { |
| 60 | talonfx->WriteVoltage(0); |
Maxwell Henderson | 74059a4 | 2023-12-22 17:40:15 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Maxwell Henderson | 10ed5c3 | 2024-01-09 12:40:54 -0800 | [diff] [blame] | 63 | for (auto talonfx : left_talonfxs_) { |
| 64 | talonfx->WriteVoltage(0); |
Maxwell Henderson | 74059a4 | 2023-12-22 17:40:15 -0800 | [diff] [blame] | 65 | } |
| 66 | } |