blob: ae82812de8150d0eb7fffceaf64ec36e2db36e29 [file] [log] [blame]
Maxwell Henderson74059a42023-12-22 17:40:15 -08001#include "frc971/wpilib/can_drivetrain_writer.h"
2
3using frc971::wpilib::CANDrivetrainWriter;
4
5CANDrivetrainWriter::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 Henderson10ed5c32024-01-09 12:40:54 -080014void 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 Henderson74059a42023-12-22 17:40:15 -080019}
20
21void CANDrivetrainWriter::HandleCANConfiguration(
22 const CANConfiguration &configuration) {
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080023 for (auto talonfx : right_talonfxs_) {
24 talonfx->PrintConfigs();
Maxwell Henderson74059a42023-12-22 17:40:15 -080025 }
26
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080027 for (auto talonfx : left_talonfxs_) {
28 talonfx->PrintConfigs();
Maxwell Henderson74059a42023-12-22 17:40:15 -080029 }
30
31 if (configuration.reapply()) {
32 WriteConfigs();
33 }
34}
35
36void CANDrivetrainWriter::WriteConfigs() {
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080037 for (auto talonfx : right_talonfxs_) {
38 talonfx->WriteConfigs();
Maxwell Henderson74059a42023-12-22 17:40:15 -080039 }
40
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080041 for (auto talonfx : left_talonfxs_) {
42 talonfx->WriteConfigs();
Maxwell Henderson74059a42023-12-22 17:40:15 -080043 }
44}
45
46void CANDrivetrainWriter::Write(
47 const ::frc971::control_loops::drivetrain::Output &output) {
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080048 for (auto talonfx : right_talonfxs_) {
49 talonfx->WriteVoltage(output.right_voltage());
Maxwell Henderson74059a42023-12-22 17:40:15 -080050 }
51
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080052 for (auto talonfx : left_talonfxs_) {
53 talonfx->WriteVoltage(output.left_voltage());
Maxwell Henderson74059a42023-12-22 17:40:15 -080054 }
55}
56
57void CANDrivetrainWriter::Stop() {
58 AOS_LOG(WARNING, "Drivetrain CAN output too old.\n");
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080059 for (auto talonfx : right_talonfxs_) {
60 talonfx->WriteVoltage(0);
Maxwell Henderson74059a42023-12-22 17:40:15 -080061 }
62
Maxwell Henderson10ed5c32024-01-09 12:40:54 -080063 for (auto talonfx : left_talonfxs_) {
64 talonfx->WriteVoltage(0);
Maxwell Henderson74059a42023-12-22 17:40:15 -080065 }
66}