diff --git a/wpilibcExamples/src/main/cpp/examples/MecanumControllerCommand/cpp/RobotContainer.cpp b/wpilibcExamples/src/main/cpp/examples/MecanumControllerCommand/cpp/RobotContainer.cpp index 856ec99911..0d3db36cb0 100644 --- a/wpilibcExamples/src/main/cpp/examples/MecanumControllerCommand/cpp/RobotContainer.cpp +++ b/wpilibcExamples/src/main/cpp/examples/MecanumControllerCommand/cpp/RobotContainer.cpp @@ -111,8 +111,8 @@ frc2::CommandPtr RobotContainer::GetAutonomousCommand() { // command, then stop at the end. return frc2::cmd::Sequence( frc2::InstantCommand( - [this, &exampleTrajectory]() { - m_drive.ResetOdometry(exampleTrajectory.InitialPose()); + [this, initialPose = exampleTrajectory.InitialPose()]() { + m_drive.ResetOdometry(initialPose); }, {}) .ToPtr(), diff --git a/wpilibcExamples/src/main/cpp/examples/RamseteCommand/cpp/RobotContainer.cpp b/wpilibcExamples/src/main/cpp/examples/RamseteCommand/cpp/RobotContainer.cpp index 5fa8c62011..feacbcc2a7 100644 --- a/wpilibcExamples/src/main/cpp/examples/RamseteCommand/cpp/RobotContainer.cpp +++ b/wpilibcExamples/src/main/cpp/examples/RamseteCommand/cpp/RobotContainer.cpp @@ -84,8 +84,8 @@ frc2::CommandPtr RobotContainer::GetAutonomousCommand() { // Reset odometry to the initial pose of the trajectory, run path following // command, then stop at the end. return frc2::cmd::RunOnce( - [this, &exampleTrajectory] { - m_drive.ResetOdometry(exampleTrajectory.InitialPose()); + [this, initialPose = exampleTrajectory.InitialPose()] { + m_drive.ResetOdometry(initialPose); }, {}) .AndThen(std::move(ramseteCommand)) diff --git a/wpilibcExamples/src/main/cpp/examples/SwerveControllerCommand/cpp/RobotContainer.cpp b/wpilibcExamples/src/main/cpp/examples/SwerveControllerCommand/cpp/RobotContainer.cpp index 2ca4b71893..6c8de05753 100644 --- a/wpilibcExamples/src/main/cpp/examples/SwerveControllerCommand/cpp/RobotContainer.cpp +++ b/wpilibcExamples/src/main/cpp/examples/SwerveControllerCommand/cpp/RobotContainer.cpp @@ -93,8 +93,8 @@ frc2::CommandPtr RobotContainer::GetAutonomousCommand() { // command, then stop at the end. return frc2::cmd::Sequence( frc2::InstantCommand( - [this, &exampleTrajectory]() { - m_drive.ResetOdometry(exampleTrajectory.InitialPose()); + [this, initialPose = exampleTrajectory.InitialPose()]() { + m_drive.ResetOdometry(initialPose); }, {}) .ToPtr(), diff --git a/wpilibcExamples/src/test/cpp/examples/MecanumControllerCommand/cpp/MecanumControllerCommandTest.cpp b/wpilibcExamples/src/test/cpp/examples/MecanumControllerCommand/cpp/MecanumControllerCommandTest.cpp new file mode 100644 index 0000000000..74d84f167f --- /dev/null +++ b/wpilibcExamples/src/test/cpp/examples/MecanumControllerCommand/cpp/MecanumControllerCommandTest.cpp @@ -0,0 +1,64 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +#include + +#include +#include +#include +#include + +#include "Robot.h" + +class MecanumControllerCommandTest : public testing::Test { + Robot m_robot; + std::optional m_thread; + bool joystickWarning; + + public: + void SetUp() override { + frc::sim::PauseTiming(); + joystickWarning = frc::DriverStation::IsJoystickConnectionWarningSilenced(); + frc::DriverStation::SilenceJoystickConnectionWarning(true); + + m_thread = std::thread([&] { m_robot.StartCompetition(); }); + frc::sim::StepTiming(0.0_ms); // Wait for Notifiers + } + + void TearDown() override { + m_robot.EndCompetition(); + m_thread->join(); + + frc::sim::DriverStationSim::ResetData(); + frc::DriverStation::SilenceJoystickConnectionWarning(joystickWarning); + } +}; + +TEST_F(MecanumControllerCommandTest, Match) { + // auto + frc::sim::DriverStationSim::SetAutonomous(true); + frc::sim::DriverStationSim::SetEnabled(true); + frc::sim::DriverStationSim::NotifyNewData(); + + frc::sim::StepTiming(15_s); + + // brief disabled period- exact duration shouldn't matter + frc::sim::DriverStationSim::SetAutonomous(false); + frc::sim::DriverStationSim::SetEnabled(false); + frc::sim::DriverStationSim::NotifyNewData(); + + frc::sim::StepTiming(3_s); + + // teleop + frc::sim::DriverStationSim::SetAutonomous(false); + frc::sim::DriverStationSim::SetEnabled(true); + frc::sim::DriverStationSim::NotifyNewData(); + + frc::sim::StepTiming(135_s); + + // end of match + frc::sim::DriverStationSim::SetAutonomous(false); + frc::sim::DriverStationSim::SetEnabled(false); + frc::sim::DriverStationSim::NotifyNewData(); +} diff --git a/wpilibcExamples/src/test/cpp/examples/MecanumControllerCommand/cpp/main.cpp b/wpilibcExamples/src/test/cpp/examples/MecanumControllerCommand/cpp/main.cpp new file mode 100644 index 0000000000..38d5cfa2fe --- /dev/null +++ b/wpilibcExamples/src/test/cpp/examples/MecanumControllerCommand/cpp/main.cpp @@ -0,0 +1,16 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +#include +#include + +/** + * Runs all unit tests. + */ +int main(int argc, char** argv) { + HAL_Initialize(500, 0); + ::testing::InitGoogleTest(&argc, argv); + int ret = RUN_ALL_TESTS(); + return ret; +} diff --git a/wpilibcExamples/src/test/cpp/examples/RamseteCommand/cpp/RamseteCommandTest.cpp b/wpilibcExamples/src/test/cpp/examples/RamseteCommand/cpp/RamseteCommandTest.cpp new file mode 100644 index 0000000000..74d84f167f --- /dev/null +++ b/wpilibcExamples/src/test/cpp/examples/RamseteCommand/cpp/RamseteCommandTest.cpp @@ -0,0 +1,64 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +#include + +#include +#include +#include +#include + +#include "Robot.h" + +class MecanumControllerCommandTest : public testing::Test { + Robot m_robot; + std::optional m_thread; + bool joystickWarning; + + public: + void SetUp() override { + frc::sim::PauseTiming(); + joystickWarning = frc::DriverStation::IsJoystickConnectionWarningSilenced(); + frc::DriverStation::SilenceJoystickConnectionWarning(true); + + m_thread = std::thread([&] { m_robot.StartCompetition(); }); + frc::sim::StepTiming(0.0_ms); // Wait for Notifiers + } + + void TearDown() override { + m_robot.EndCompetition(); + m_thread->join(); + + frc::sim::DriverStationSim::ResetData(); + frc::DriverStation::SilenceJoystickConnectionWarning(joystickWarning); + } +}; + +TEST_F(MecanumControllerCommandTest, Match) { + // auto + frc::sim::DriverStationSim::SetAutonomous(true); + frc::sim::DriverStationSim::SetEnabled(true); + frc::sim::DriverStationSim::NotifyNewData(); + + frc::sim::StepTiming(15_s); + + // brief disabled period- exact duration shouldn't matter + frc::sim::DriverStationSim::SetAutonomous(false); + frc::sim::DriverStationSim::SetEnabled(false); + frc::sim::DriverStationSim::NotifyNewData(); + + frc::sim::StepTiming(3_s); + + // teleop + frc::sim::DriverStationSim::SetAutonomous(false); + frc::sim::DriverStationSim::SetEnabled(true); + frc::sim::DriverStationSim::NotifyNewData(); + + frc::sim::StepTiming(135_s); + + // end of match + frc::sim::DriverStationSim::SetAutonomous(false); + frc::sim::DriverStationSim::SetEnabled(false); + frc::sim::DriverStationSim::NotifyNewData(); +} diff --git a/wpilibcExamples/src/test/cpp/examples/RamseteCommand/cpp/main.cpp b/wpilibcExamples/src/test/cpp/examples/RamseteCommand/cpp/main.cpp new file mode 100644 index 0000000000..38d5cfa2fe --- /dev/null +++ b/wpilibcExamples/src/test/cpp/examples/RamseteCommand/cpp/main.cpp @@ -0,0 +1,16 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +#include +#include + +/** + * Runs all unit tests. + */ +int main(int argc, char** argv) { + HAL_Initialize(500, 0); + ::testing::InitGoogleTest(&argc, argv); + int ret = RUN_ALL_TESTS(); + return ret; +} diff --git a/wpilibcExamples/src/test/cpp/examples/SwerveControllerCommand/cpp/SwerveControllerCommandTest.cpp b/wpilibcExamples/src/test/cpp/examples/SwerveControllerCommand/cpp/SwerveControllerCommandTest.cpp new file mode 100644 index 0000000000..eb20804de0 --- /dev/null +++ b/wpilibcExamples/src/test/cpp/examples/SwerveControllerCommand/cpp/SwerveControllerCommandTest.cpp @@ -0,0 +1,69 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +#include +#include + +#include +#include +#include +#include + +#include "Robot.h" + +class SwerveControllerCommandTest : public testing::Test { + Robot m_robot; + std::optional m_thread; + bool joystickWarning; + + public: + void SetUp() override { + frc::sim::PauseTiming(); + joystickWarning = frc::DriverStation::IsJoystickConnectionWarningSilenced(); + frc::DriverStation::SilenceJoystickConnectionWarning(true); + + m_thread = std::thread([&] { m_robot.StartCompetition(); }); + frc::sim::StepTiming(0.0_ms); // Wait for Notifiers + } + + void TearDown() override { + m_robot.EndCompetition(); + m_thread->join(); + + frc::sim::DriverStationSim::ResetData(); + frc::DriverStation::SilenceJoystickConnectionWarning(joystickWarning); + } +}; + +TEST_F(SwerveControllerCommandTest, Match) { + std::cerr << "autonomous" << std::endl; + // auto + frc::sim::DriverStationSim::SetAutonomous(true); + frc::sim::DriverStationSim::SetEnabled(true); + frc::sim::DriverStationSim::NotifyNewData(); + + frc::sim::StepTiming(15_s); + + // brief disabled period- exact duration shouldn't matter + std::cerr << "mid disabled" << std::endl; + frc::sim::DriverStationSim::SetAutonomous(false); + frc::sim::DriverStationSim::SetEnabled(false); + frc::sim::DriverStationSim::NotifyNewData(); + + frc::sim::StepTiming(3_s); + + // teleop + std::cerr << "teleop" << std::endl; + frc::sim::DriverStationSim::SetAutonomous(false); + frc::sim::DriverStationSim::SetEnabled(true); + frc::sim::DriverStationSim::NotifyNewData(); + + frc::sim::StepTiming(135_s); + + // end of match + std::cerr << "end of match" << std::endl; + frc::sim::DriverStationSim::SetAutonomous(false); + frc::sim::DriverStationSim::SetEnabled(false); + frc::sim::DriverStationSim::NotifyNewData(); +} diff --git a/wpilibcExamples/src/test/cpp/examples/SwerveControllerCommand/cpp/main.cpp b/wpilibcExamples/src/test/cpp/examples/SwerveControllerCommand/cpp/main.cpp new file mode 100644 index 0000000000..38d5cfa2fe --- /dev/null +++ b/wpilibcExamples/src/test/cpp/examples/SwerveControllerCommand/cpp/main.cpp @@ -0,0 +1,16 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +#include +#include + +/** + * Runs all unit tests. + */ +int main(int argc, char** argv) { + HAL_Initialize(500, 0); + ::testing::InitGoogleTest(&argc, argv); + int ret = RUN_ALL_TESTS(); + return ret; +}