ArPiRobot-CoreLib C++
C++ library for ArPiRobot robots
BaseRobot.hpp
1 /*
2  * Copyright 2021 Marcus Behel
3  *
4  * This file is part of ArPiRobot-CoreLib.
5  *
6  * ArPiRobot-CoreLib is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * ArPiRobot-CoreLib is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with ArPiRobot-CoreLib. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #pragma once
21 
22 #include <arpirobot/core/device/BaseDevice.hpp>
23 #include <arpirobot/core/robot/RobotProfile.hpp>
24 #include <arpirobot/core/scheduler.hpp>
25 #include <chrono>
26 #include <thread>
27 #include <mutex>
28 #include <vector>
29 #include <memory>
30 
31 namespace arpirobot{
32 
40  class BaseRobot{
41  public:
42  BaseRobot() = default;
43  BaseRobot(const BaseRobot &other) = delete;
44 
45  virtual ~BaseRobot() = default;
46 
47  BaseRobot &operator=(const BaseRobot &other) = delete;
48 
52  void feedWatchdog();
53 
57  void start();
58 
65  static std::shared_ptr<Task> scheduleRepeatedFunction(const std::function<void()> &&func, sched_clk::duration rate);
66 
73  static void runOnceSoon(const std::function<void()> &&func);
74 
79  static void removeTaskFromScheduler(std::shared_ptr<Task> task);
80 
88  static void beginWhenReady(BaseDevice *device);
89 
93  static void deviceDestroyed(BaseDevice *device);
94 
98  virtual void robotStarted() = 0;
99 
103  virtual void robotEnabled() = 0;
104 
108  virtual void robotDisabled() = 0;
109 
113  virtual void enabledPeriodic() = 0;
114 
118  virtual void disabledPeriodic() = 0;
119 
123  virtual void periodic() = 0;
124 
125  // True after start, false before stop
126  // Used to determine if a BaseRobot instance is running or not
127  static bool exists;
128 
129  private:
130  static void stopSignalHandler(int signal);
131 
132  static void ignoreSignalHandler(int signal);
133 
134  void modeBasedPeriodic();
135 
136  void doPeriodic();
137 
138  void runWatchdog();
139 
140  void onDisable();
141 
142  void onEnable();
143 
147 
148  // Status
149  bool isEnabled = false;
150 
151  // Watchdog
152  std::mutex watchdogMutex;
153  std::chrono::steady_clock::time_point lastWatchdogFeed;
154  bool watchdogDidDisable = false;
155 
156 
157  // Static variables
158 
159  // Devices used on current robot
160  // Not using shared_ptr because devices add themselves to this list
161  // and remove themselves when destroyed
162  static std::vector<BaseDevice*> devices;
163  static std::mutex devicesLock;
164  static bool devicesBeginNow;
165 
166  // Stop flag from Ctrl+C
167  static bool stop;
168 
169  // Scheduler
170  static Scheduler *scheduler;
171 
172  // Lock to ensure access to "exists" var is exclusive
173  static std::mutex existsLock;
174  };
175 }
Definition: BaseDevice.hpp:35
Definition: BaseRobot.hpp:40
virtual void robotDisabled()=0
static void runOnceSoon(const std::function< void()> &&func)
virtual void periodic()=0
virtual void robotEnabled()=0
virtual void robotStarted()=0
virtual void enabledPeriodic()=0
static void removeTaskFromScheduler(std::shared_ptr< Task > task)
static void beginWhenReady(BaseDevice *device)
virtual void disabledPeriodic()=0
static std::shared_ptr< Task > scheduleRepeatedFunction(const std::function< void()> &&func, sched_clk::duration rate)
static void deviceDestroyed(BaseDevice *device)
Definition: scheduler.hpp:93
Definition: ArduinoDevice.hpp:27