-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRobotBase.cpp
More file actions
73 lines (62 loc) · 2.24 KB
/
RobotBase.cpp
File metadata and controls
73 lines (62 loc) · 2.24 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "RobotBase.hpp"
#include <ctre/phoenix6/unmanaged/Unmanaged.hpp>
int RobotBase::Run()
{
printf("Robot program startup complete\n");
while (IsRunning()) {
auto const start = std::chrono::steady_clock::now();
/* run the robot periodic function */
RobotPeriodic();
/* check if we're enabled */
if (IsEnabled()) {
/* enabled */
if (!_lastEnabled.value_or(false)) {
/* just switched, run enabled init */
printf("Robot ENABLED\n");
EnabledInit();
_lastEnabled = true;
}
/* enable for 100 ms */
ctre::phoenix::unmanaged::FeedEnable(100);
/* run enabled periodic */
EnabledPeriodic();
} else {
/* disabled */
if (_lastEnabled.value_or(true)) {
/* just switched, run disabled init */
printf("Robot DISABLED\n");
DisabledInit();
if (_lastEnabled.has_value()) {
/* we were enabled, disable now */
ctre::phoenix::unmanaged::FeedEnable(0);
}
_lastEnabled = false;
}
/* run disabled periodic */
DisabledPeriodic();
}
auto const end = std::chrono::steady_clock::now();
/* yield for the remainder of the loop time */
units::millisecond_t const dtMs{std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() / 1000.0};
if (dtMs < _loopTime) {
SleepFor(_loopTime - dtMs);
} else {
/* loop overrun */
ReportLoopOverrun(dtMs);
/* yield control of this thread */
SleepFor(0_ms);
}
}
return 0;
}
void RobotBase::ReportLoopOverrun(units::millisecond_t measured)
{
auto const now = std::chrono::steady_clock::now();
auto const dtMs = std::chrono::duration_cast<std::chrono::milliseconds>(now - _lastErrorTime).count();
if (dtMs > kErrorTimeMs) {
fprintf(stderr, "Warning: %.1fms loop time overrun\n"
" Robot loop took %.3fms\n",
_loopTime.value(), measured.value());
_lastErrorTime = now;
}
}