mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[command] Speed up MecanumControllerCommand and SwerveControllerCommand tests (#2604)
Currently, these two tests take several seconds to complete and fail intermittently in Windows CI. This is caused by relying on wall clock time. Sampling the trajectory with wall clock time means the simulation must run for several seconds to reach the end of the trajectory. Also, the controller can become unstable when Windows CI experiences process scheduling delays of several hundred milliseconds. Feedback controllers don't cope well with large delays on systems with fast dynamics. This patch uses the mocking functionality of frc::Timer to advance the clock by 5ms at every timestep instead of using the wall clock time. This has two benefits: 1. The tests complete much faster because the simulation can step forward faster than real time. 2. The controller is more stable because the sample period is uniform, which should fix the occasional failures.
This commit is contained in:
@@ -9,14 +9,13 @@
|
||||
#include <frc2/command/MecanumControllerCommand.h>
|
||||
#include <frc2/command/Subsystem.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <frc/controller/PIDController.h>
|
||||
#include <frc/controller/ProfiledPIDController.h>
|
||||
#include <frc/geometry/Rotation2d.h>
|
||||
#include <frc/geometry/Translation2d.h>
|
||||
#include <frc/kinematics/MecanumDriveKinematics.h>
|
||||
#include <frc/kinematics/MecanumDriveOdometry.h>
|
||||
#include <frc/simulation/SimHooks.h>
|
||||
#include <frc/trajectory/TrajectoryGenerator.h>
|
||||
#include <wpi/math>
|
||||
|
||||
@@ -60,6 +59,10 @@ class MecanumControllerCommandTest : public ::testing::Test {
|
||||
frc::MecanumDriveOdometry m_odometry{m_kinematics, 0_rad,
|
||||
frc::Pose2d{0_m, 0_m, 0_rad}};
|
||||
|
||||
void SetUp() override { frc::sim::PauseTiming(); }
|
||||
|
||||
void TearDown() override { frc::sim::ResumeTiming(); }
|
||||
|
||||
frc::MecanumDriveWheelSpeeds getCurrentWheelSpeeds() {
|
||||
return frc::MecanumDriveWheelSpeeds{m_frontLeftSpeed, m_frontRightSpeed,
|
||||
m_rearLeftSpeed, m_rearRightSpeed};
|
||||
@@ -72,7 +75,7 @@ class MecanumControllerCommandTest : public ::testing::Test {
|
||||
};
|
||||
|
||||
TEST_F(MecanumControllerCommandTest, ReachesReference) {
|
||||
frc2::Subsystem subsystem{};
|
||||
frc2::Subsystem subsystem;
|
||||
|
||||
auto waypoints =
|
||||
std::vector{frc::Pose2d{0_m, 0_m, 0_rad}, frc::Pose2d{1_m, 5_m, 3_rad}};
|
||||
@@ -99,10 +102,12 @@ TEST_F(MecanumControllerCommandTest, ReachesReference) {
|
||||
|
||||
m_timer.Reset();
|
||||
m_timer.Start();
|
||||
|
||||
command.Initialize();
|
||||
while (!command.IsFinished()) {
|
||||
command.Execute();
|
||||
m_angle = trajectory.Sample(m_timer.Get()).pose.Rotation();
|
||||
frc::sim::StepTiming(5_ms);
|
||||
}
|
||||
m_timer.Stop();
|
||||
command.End(false);
|
||||
|
||||
Reference in New Issue
Block a user