-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRobotBase.cpp
75 lines (62 loc) · 2.19 KB
/
RobotBase.cpp
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
74
75
#include "RobotBase.hpp"
#include "ctre/phoenix6/unmanaged/Unmanaged.hpp" // for FeedEnable
int RobotBase::Run()
{
printf("Starting robot program...\n");
/* this is robot startup, run robot init */
RobotInit();
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 != 1) {
/* just switched, run enabled init */
printf("Robot ENABLED\n");
EnabledInit();
_lastEnabled = 1;
}
/* enable for 100 ms */
ctre::phoenix::unmanaged::FeedEnable(100);
/* run enabled periodic */
EnabledPeriodic();
} else {
/* disabled */
if (_lastEnabled != 0) {
/* just switched, run disabled init */
printf("Robot DISABLED\n");
DisabledInit();
_lastEnabled = 0;
}
/* 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);
}
}
/* program shutting down */
printf("Stopping robot program...\n");
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;
}
}