-
Notifications
You must be signed in to change notification settings - Fork 2
/
RobotBase.hpp
57 lines (46 loc) · 1.27 KB
/
RobotBase.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#pragma once
#include "units/time.h"
#include <chrono>
#include <thread>
#include <stdint.h>
/**
* Manages a robot program.
*/
class RobotBase {
public:
virtual void RobotInit() = 0;
virtual void RobotPeriodic() = 0;
virtual bool IsEnabled() = 0;
virtual void EnabledInit() = 0;
virtual void EnabledPeriodic() = 0;
virtual void DisabledInit() = 0;
virtual void DisabledPeriodic() = 0;
virtual bool IsRunning() { return true; }
private:
units::millisecond_t _loopTime = 20_ms;
int _lastEnabled = -1;
public:
/**
* Sleeps for the specified amount of time.
*/
static inline void SleepFor(units::microsecond_t us)
{
std::this_thread::sleep_for(std::chrono::microseconds{(uint64_t)us.value()});
}
/**
* Sets the loop time for the robot program periodic calls.
*/
void SetLoopTime(units::millisecond_t loopTime = 20_ms)
{
_loopTime = loopTime;
}
/**
* Runs the robot program.
*/
int Run();
private:
static constexpr auto kErrorTimeMs = 500;
std::chrono::time_point<std::chrono::steady_clock> _lastErrorTime = std::chrono::steady_clock::now();
/** Reports a loop overrun with debouncing. */
void ReportLoopOverrun(units::millisecond_t measured);
};