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:
Peter Johnson
2019-09-28 15:43:24 -07:00
committed by GitHub
parent 457f94ba26
commit b23baf611a
12 changed files with 239 additions and 18 deletions

View File

@@ -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;
}