blob: 978c48ed2aee00af69662b3d354a95514c1780c3 [file] [log] [blame]
Campbell Crowleyc0cfb132015-12-30 20:58:02 -08001#ifndef FRC971_WPILIB_NEWROBOTBASE_H_
2#define FRC971_WPILIB_NEWROBOTBASE_H_
3
Alex Perrycb7da4b2019-08-28 19:35:56 -07004#include "aos/events/shm_event_loop.h"
Austin Schuhbd1fe9c2019-06-29 16:35:48 -07005#include "aos/init.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -07006#include "aos/logging/logging.h"
Parker Schuhd3b7a8872018-02-19 16:42:27 -08007#include "frc971/wpilib/ahal/RobotBase.h"
Campbell Crowleyc0cfb132015-12-30 20:58:02 -08008
9namespace frc971 {
10namespace wpilib {
11
12class WPILibRobotBase {
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070013 public:
Campbell Crowleyc0cfb132015-12-30 20:58:02 -080014 virtual void Run() = 0;
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070015
16 // Runs all the loops.
17 void RunLoops() {
18 // TODO(austin): SIGINT handler calling Exit on all the loops.
19 // TODO(austin): RegisterSignalHandler in ShmEventLoop for others.
20
21 ::std::vector<::std::thread> threads;
22 for (size_t i = 1; i < loops_.size(); ++i) {
23 threads.emplace_back([this, i]() { loops_[i]->Run(); });
24 }
25 // Save some memory and run the last one in the main thread.
26 loops_[0]->Run();
27
28 for (::std::thread &thread : threads) {
29 thread.join();
30 }
31
Austin Schuhf257f3c2019-10-27 21:00:43 -070032 AOS_LOG(ERROR, "Exiting WPILibRobot\n");
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070033
34 ::aos::Cleanup();
35 }
36
37 protected:
38 // Adds a loop to the list of loops to run.
39 void AddLoop(::aos::ShmEventLoop *loop) { loops_.push_back(loop); }
40
41 private:
42 // List of the event loops to run in RunLoops.
43 ::std::vector<::aos::ShmEventLoop *> loops_;
Campbell Crowleyc0cfb132015-12-30 20:58:02 -080044};
45
46#define AOS_ROBOT_CLASS(_ClassName_) \
47 START_ROBOT_CLASS(::frc971::wpilib::WPILibAdapterRobot<_ClassName_>)
48
49template <typename T>
Parker Schuhd3b7a8872018-02-19 16:42:27 -080050class WPILibAdapterRobot : public frc::RobotBase {
Campbell Crowleyc0cfb132015-12-30 20:58:02 -080051 public:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070052 void StartCompetition() override {
Austin Schuh9fe68f72019-08-10 19:32:03 -070053 ::aos::InitNRT(true);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070054
55 robot_.Run();
56 }
Campbell Crowleyc0cfb132015-12-30 20:58:02 -080057
58 private:
59 T robot_;
60};
61
Brian Silverman4be7ffe2016-01-02 14:16:06 -080062} // namespace wpilib
63} // namespace frc971
Campbell Crowleyc0cfb132015-12-30 20:58:02 -080064
Brian Silverman4be7ffe2016-01-02 14:16:06 -080065#endif // FRC971_WPILIB_NEWROBOTBASE_H_