blob: 86672c48ab278410d5a3916259ea837fd47e6755 [file] [log] [blame]
Campbell Crowleyc0cfb132015-12-30 20:58:02 -08001#ifndef FRC971_WPILIB_NEWROBOTBASE_H_
2#define FRC971_WPILIB_NEWROBOTBASE_H_
3
Austin Schuhbd1fe9c2019-06-29 16:35:48 -07004#include "aos/events/shm-event-loop.h"
5#include "aos/init.h"
Parker Schuhd3b7a8872018-02-19 16:42:27 -08006#include "frc971/wpilib/ahal/RobotBase.h"
Campbell Crowleyc0cfb132015-12-30 20:58:02 -08007
8namespace frc971 {
9namespace wpilib {
10
11class WPILibRobotBase {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070012 public:
Campbell Crowleyc0cfb132015-12-30 20:58:02 -080013 virtual void Run() = 0;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070014
15 // Runs all the loops.
16 void RunLoops() {
17 // TODO(austin): SIGINT handler calling Exit on all the loops.
18 // TODO(austin): RegisterSignalHandler in ShmEventLoop for others.
19
20 ::std::vector<::std::thread> threads;
21 for (size_t i = 1; i < loops_.size(); ++i) {
22 threads.emplace_back([this, i]() { loops_[i]->Run(); });
23 }
24 // Save some memory and run the last one in the main thread.
25 loops_[0]->Run();
26
27 for (::std::thread &thread : threads) {
28 thread.join();
29 }
30
Austin Schuhf257f3c2019-10-27 21:00:43 -070031 AOS_LOG(ERROR, "Exiting WPILibRobot\n");
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070032
33 ::aos::Cleanup();
34 }
35
36 protected:
37 // Adds a loop to the list of loops to run.
38 void AddLoop(::aos::ShmEventLoop *loop) { loops_.push_back(loop); }
39
40 private:
41 // List of the event loops to run in RunLoops.
42 ::std::vector<::aos::ShmEventLoop *> loops_;
Campbell Crowleyc0cfb132015-12-30 20:58:02 -080043};
44
45#define AOS_ROBOT_CLASS(_ClassName_) \
46 START_ROBOT_CLASS(::frc971::wpilib::WPILibAdapterRobot<_ClassName_>)
47
48template <typename T>
Parker Schuhd3b7a8872018-02-19 16:42:27 -080049class WPILibAdapterRobot : public frc::RobotBase {
Campbell Crowleyc0cfb132015-12-30 20:58:02 -080050 public:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070051 void StartCompetition() override {
Austin Schuh9fe68f72019-08-10 19:32:03 -070052 ::aos::InitNRT(true);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070053
54 robot_.Run();
55 }
Campbell Crowleyc0cfb132015-12-30 20:58:02 -080056
57 private:
58 T robot_;
59};
60
Brian Silverman4be7ffe2016-01-02 14:16:06 -080061} // namespace wpilib
62} // namespace frc971
Campbell Crowleyc0cfb132015-12-30 20:58:02 -080063
Brian Silverman4be7ffe2016-01-02 14:16:06 -080064#endif // FRC971_WPILIB_NEWROBOTBASE_H_