blob: 27c1bcda325f794827e2825018c1b7e538029a81 [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
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -08009namespace frc971::wpilib {
Campbell Crowleyc0cfb132015-12-30 20:58:02 -080010
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) {
Austin Schuhff70b982023-02-24 21:06:44 -080022 threads.emplace_back([this, i]() {
23 LOG(INFO) << "Starting " << loops_[i]->name() << " with priority "
24 << loops_[i]->runtime_realtime_priority();
25 loops_[i]->Run();
26 });
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070027 }
Austin Schuhff70b982023-02-24 21:06:44 -080028 LOG(INFO) << "Starting " << loops_[0]->name() << " with priority "
29 << loops_[0]->runtime_realtime_priority();
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070030 // Save some memory and run the last one in the main thread.
31 loops_[0]->Run();
32
33 for (::std::thread &thread : threads) {
34 thread.join();
35 }
36
Austin Schuhae87e312020-08-01 16:15:01 -070037 LOG(ERROR) << "Exiting WPILibRobot";
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070038 }
39
40 protected:
41 // Adds a loop to the list of loops to run.
42 void AddLoop(::aos::ShmEventLoop *loop) { loops_.push_back(loop); }
43
44 private:
45 // List of the event loops to run in RunLoops.
46 ::std::vector<::aos::ShmEventLoop *> loops_;
Campbell Crowleyc0cfb132015-12-30 20:58:02 -080047};
48
49#define AOS_ROBOT_CLASS(_ClassName_) \
50 START_ROBOT_CLASS(::frc971::wpilib::WPILibAdapterRobot<_ClassName_>)
51
52template <typename T>
Parker Schuhd3b7a8872018-02-19 16:42:27 -080053class WPILibAdapterRobot : public frc::RobotBase {
Campbell Crowleyc0cfb132015-12-30 20:58:02 -080054 public:
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070055 void StartCompetition() override {
Austin Schuh2d7fc662021-01-23 15:37:51 -080056 PCHECK(setuid(0) == 0) << ": Failed to change user to root";
Austin Schuh094d09b2020-11-20 23:26:52 -080057 // Just allow overcommit memory like usual. Various processes map memory
58 // they will never use, and the roboRIO doesn't have enough RAM to handle
59 // it. This is in here instead of starter.sh because starter.sh doesn't run
60 // with permissions on a roboRIO.
Austin Schuh2d7fc662021-01-23 15:37:51 -080061 PCHECK(system("echo 0 > /proc/sys/vm/overcommit_memory") == 0);
Austin Schuh719d6802021-11-05 23:46:20 -070062 PCHECK(system("busybox ps -ef | grep '\\[ktimersoftd/0\\]' | awk '{print "
63 "$1}' | xargs chrt -f -p 70") == 0);
64 PCHECK(system("busybox ps -ef | grep '\\[ktimersoftd/1\\]' | awk '{print "
65 "$1}' | xargs chrt -f -p 70") == 0);
66 PCHECK(system("busybox ps -ef | grep '\\[irq/54-eth0\\]' | awk '{print "
67 "$1}' | xargs chrt -f -p 17") == 0);
Austin Schuh094d09b2020-11-20 23:26:52 -080068
69 // Configure throttling so we reserve 5% of the CPU for non-rt work.
70 // This makes things significantly more stable when work explodes.
71 // This is in here instead of starter.sh for the same reasons, starter is
72 // suid and runs as admin, so this actually works.
Austin Schuh2d7fc662021-01-23 15:37:51 -080073 PCHECK(system("/sbin/sysctl -w kernel.sched_rt_period_us=1000000") == 0);
74 PCHECK(system("/sbin/sysctl -w kernel.sched_rt_runtime_us=950000") == 0);
Austin Schuhbd1fe9c2019-06-29 16:35:48 -070075
76 robot_.Run();
77 }
Campbell Crowleyc0cfb132015-12-30 20:58:02 -080078
79 private:
80 T robot_;
81};
82
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080083} // namespace frc971::wpilib
Campbell Crowleyc0cfb132015-12-30 20:58:02 -080084
Brian Silverman4be7ffe2016-01-02 14:16:06 -080085#endif // FRC971_WPILIB_NEWROBOTBASE_H_