mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-29 02:21:44 +00:00
Add ability to run robot main loop in a separate thread (#1895)
Default behavior is still to run the robot main loop in the main thread. The ability to run the robot main loop in a separate thread and add a hook for running a different function in the main thread is needed for simulation GUI support on some platforms.
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
|
||||
#include <thread>
|
||||
|
||||
#include <hal/Main.h>
|
||||
#include <wpi/raw_ostream.h>
|
||||
|
||||
#include "frc/Base.h"
|
||||
@@ -19,14 +20,37 @@ class DriverStation;
|
||||
|
||||
int RunHALInitialization();
|
||||
|
||||
namespace impl {
|
||||
|
||||
template <class Robot>
|
||||
void RunRobot() {
|
||||
static Robot robot;
|
||||
robot.StartCompetition();
|
||||
}
|
||||
|
||||
} // namespace impl
|
||||
|
||||
template <class Robot>
|
||||
int StartRobot() {
|
||||
int halInit = RunHALInitialization();
|
||||
if (halInit != 0) {
|
||||
return halInit;
|
||||
}
|
||||
static Robot robot;
|
||||
robot.StartCompetition();
|
||||
if (HAL_HasMain()) {
|
||||
std::thread([] {
|
||||
try {
|
||||
impl::RunRobot<Robot>();
|
||||
} catch (...) {
|
||||
HAL_ExitMain();
|
||||
throw;
|
||||
}
|
||||
HAL_ExitMain();
|
||||
})
|
||||
.detach();
|
||||
HAL_RunMain();
|
||||
} else {
|
||||
impl::RunRobot<Robot>();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user