Convert all year's robots to proper event loops
Each robot has a couple of event loops, one per thread. Each of these
threads corresponds to the threads from before the change. y2016 has
been tested on real hardware.
Change-Id: I99f726a8bc0498204c1a3b99f15508119eed9ad3
diff --git a/frc971/wpilib/wpilib_robot_base.h b/frc971/wpilib/wpilib_robot_base.h
index 3deb0e4..2cccf65 100644
--- a/frc971/wpilib/wpilib_robot_base.h
+++ b/frc971/wpilib/wpilib_robot_base.h
@@ -1,14 +1,45 @@
#ifndef FRC971_WPILIB_NEWROBOTBASE_H_
#define FRC971_WPILIB_NEWROBOTBASE_H_
+#include "aos/events/shm-event-loop.h"
+#include "aos/init.h"
#include "frc971/wpilib/ahal/RobotBase.h"
namespace frc971 {
namespace wpilib {
class WPILibRobotBase {
-public:
+ public:
virtual void Run() = 0;
+
+ // Runs all the loops.
+ void RunLoops() {
+ // TODO(austin): SIGINT handler calling Exit on all the loops.
+ // TODO(austin): RegisterSignalHandler in ShmEventLoop for others.
+
+ ::std::vector<::std::thread> threads;
+ for (size_t i = 1; i < loops_.size(); ++i) {
+ threads.emplace_back([this, i]() { loops_[i]->Run(); });
+ }
+ // Save some memory and run the last one in the main thread.
+ loops_[0]->Run();
+
+ for (::std::thread &thread : threads) {
+ thread.join();
+ }
+
+ LOG(ERROR, "Exiting WPILibRobot\n");
+
+ ::aos::Cleanup();
+ }
+
+ protected:
+ // Adds a loop to the list of loops to run.
+ void AddLoop(::aos::ShmEventLoop *loop) { loops_.push_back(loop); }
+
+ private:
+ // List of the event loops to run in RunLoops.
+ ::std::vector<::aos::ShmEventLoop *> loops_;
};
#define AOS_ROBOT_CLASS(_ClassName_) \
@@ -17,7 +48,11 @@
template <typename T>
class WPILibAdapterRobot : public frc::RobotBase {
public:
- void StartCompetition() override { robot_.Run(); }
+ void StartCompetition() override {
+ ::aos::InitNRT();
+
+ robot_.Run();
+ }
private:
T robot_;