[wpilib] LinearOpMode: wait for mode change to return from OpModeRun (#8477)

We need to wait, or otherwise OpModeRobot will immediately reinstantiate
and re-run the opmode, which is generally undesirable (e.g. for
autonomous).

Fixes #8475.
This commit is contained in:
Peter Johnson
2025-12-13 21:45:22 -08:00
committed by GitHub
parent e2c9af862e
commit f6ef155166
2 changed files with 15 additions and 0 deletions

View File

@@ -4,6 +4,7 @@
#include "wpi/opmode/LinearOpMode.hpp"
#include "wpi/driverstation/DriverStation.hpp"
#include "wpi/hal/DriverStation.h"
#include "wpi/internal/DriverStationModeThread.hpp"
@@ -14,6 +15,14 @@ void LinearOpMode::OpModeRun(int64_t opModeId) {
word.SetOpModeId(opModeId);
internal::DriverStationModeThread bgThread{word};
Run();
// Wait for opmode to be stopped or disabled, otherwise OpModeRobot will
// recreate and re-run the opmode immediately.
while (IsRunning() && DriverStation::IsEnabled() &&
DriverStation::GetOpModeId() == opModeId) {
using namespace std::chrono_literals;
std::this_thread::sleep_for(20ms);
}
}
void LinearOpMode::OpModeStop() {

View File

@@ -67,6 +67,12 @@ public abstract class LinearOpMode implements OpMode {
try (DriverStationModeThread bgThread = new DriverStationModeThread(word)) {
run();
// Wait for opmode to be stopped or disabled, otherwise OpModeRobot will recreate and re-run
// the opmode immediately.
while (isRunning() && DriverStation.isEnabled() && DriverStation.getOpModeId() == opModeId) {
Thread.sleep(20);
}
}
}