Campbell Crowley | c0cfb13 | 2015-12-30 20:58:02 -0800 | [diff] [blame] | 1 | #ifndef FRC971_WPILIB_NEWROBOTBASE_H_ |
| 2 | #define FRC971_WPILIB_NEWROBOTBASE_H_ |
| 3 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 4 | #include "aos/events/shm_event_loop.h" |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 5 | #include "aos/init.h" |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 6 | #include "aos/logging/logging.h" |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 7 | #include "frc971/wpilib/ahal/RobotBase.h" |
Campbell Crowley | c0cfb13 | 2015-12-30 20:58:02 -0800 | [diff] [blame] | 8 | |
| 9 | namespace frc971 { |
| 10 | namespace wpilib { |
| 11 | |
| 12 | class WPILibRobotBase { |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 13 | public: |
Campbell Crowley | c0cfb13 | 2015-12-30 20:58:02 -0800 | [diff] [blame] | 14 | virtual void Run() = 0; |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 15 | |
| 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) { |
Austin Schuh | ff70b98 | 2023-02-24 21:06:44 -0800 | [diff] [blame] | 23 | threads.emplace_back([this, i]() { |
| 24 | LOG(INFO) << "Starting " << loops_[i]->name() << " with priority " |
| 25 | << loops_[i]->runtime_realtime_priority(); |
| 26 | loops_[i]->Run(); |
| 27 | }); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 28 | } |
Austin Schuh | ff70b98 | 2023-02-24 21:06:44 -0800 | [diff] [blame] | 29 | LOG(INFO) << "Starting " << loops_[0]->name() << " with priority " |
| 30 | << loops_[0]->runtime_realtime_priority(); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 31 | // Save some memory and run the last one in the main thread. |
| 32 | loops_[0]->Run(); |
| 33 | |
| 34 | for (::std::thread &thread : threads) { |
| 35 | thread.join(); |
| 36 | } |
| 37 | |
Austin Schuh | ae87e31 | 2020-08-01 16:15:01 -0700 | [diff] [blame] | 38 | LOG(ERROR) << "Exiting WPILibRobot"; |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | protected: |
| 42 | // Adds a loop to the list of loops to run. |
| 43 | void AddLoop(::aos::ShmEventLoop *loop) { loops_.push_back(loop); } |
| 44 | |
| 45 | private: |
| 46 | // List of the event loops to run in RunLoops. |
| 47 | ::std::vector<::aos::ShmEventLoop *> loops_; |
Campbell Crowley | c0cfb13 | 2015-12-30 20:58:02 -0800 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | #define AOS_ROBOT_CLASS(_ClassName_) \ |
| 51 | START_ROBOT_CLASS(::frc971::wpilib::WPILibAdapterRobot<_ClassName_>) |
| 52 | |
| 53 | template <typename T> |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 54 | class WPILibAdapterRobot : public frc::RobotBase { |
Campbell Crowley | c0cfb13 | 2015-12-30 20:58:02 -0800 | [diff] [blame] | 55 | public: |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 56 | void StartCompetition() override { |
Austin Schuh | 2d7fc66 | 2021-01-23 15:37:51 -0800 | [diff] [blame] | 57 | PCHECK(setuid(0) == 0) << ": Failed to change user to root"; |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 58 | // Just allow overcommit memory like usual. Various processes map memory |
| 59 | // they will never use, and the roboRIO doesn't have enough RAM to handle |
| 60 | // it. This is in here instead of starter.sh because starter.sh doesn't run |
| 61 | // with permissions on a roboRIO. |
Austin Schuh | 2d7fc66 | 2021-01-23 15:37:51 -0800 | [diff] [blame] | 62 | PCHECK(system("echo 0 > /proc/sys/vm/overcommit_memory") == 0); |
Austin Schuh | 719d680 | 2021-11-05 23:46:20 -0700 | [diff] [blame] | 63 | PCHECK(system("busybox ps -ef | grep '\\[ktimersoftd/0\\]' | awk '{print " |
| 64 | "$1}' | xargs chrt -f -p 70") == 0); |
| 65 | PCHECK(system("busybox ps -ef | grep '\\[ktimersoftd/1\\]' | awk '{print " |
| 66 | "$1}' | xargs chrt -f -p 70") == 0); |
| 67 | PCHECK(system("busybox ps -ef | grep '\\[irq/54-eth0\\]' | awk '{print " |
| 68 | "$1}' | xargs chrt -f -p 17") == 0); |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 69 | |
| 70 | // Configure throttling so we reserve 5% of the CPU for non-rt work. |
| 71 | // This makes things significantly more stable when work explodes. |
| 72 | // This is in here instead of starter.sh for the same reasons, starter is |
| 73 | // suid and runs as admin, so this actually works. |
Austin Schuh | 2d7fc66 | 2021-01-23 15:37:51 -0800 | [diff] [blame] | 74 | PCHECK(system("/sbin/sysctl -w kernel.sched_rt_period_us=1000000") == 0); |
| 75 | PCHECK(system("/sbin/sysctl -w kernel.sched_rt_runtime_us=950000") == 0); |
Austin Schuh | bd1fe9c | 2019-06-29 16:35:48 -0700 | [diff] [blame] | 76 | |
| 77 | robot_.Run(); |
| 78 | } |
Campbell Crowley | c0cfb13 | 2015-12-30 20:58:02 -0800 | [diff] [blame] | 79 | |
| 80 | private: |
| 81 | T robot_; |
| 82 | }; |
| 83 | |
Brian Silverman | 4be7ffe | 2016-01-02 14:16:06 -0800 | [diff] [blame] | 84 | } // namespace wpilib |
| 85 | } // namespace frc971 |
Campbell Crowley | c0cfb13 | 2015-12-30 20:58:02 -0800 | [diff] [blame] | 86 | |
Brian Silverman | 4be7ffe | 2016-01-02 14:16:06 -0800 | [diff] [blame] | 87 | #endif // FRC971_WPILIB_NEWROBOTBASE_H_ |