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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#pragma once
#include <chrono>
#include <thread>
#include <hal/DriverStation.h>
#include <hal/HALBase.h>
#include <hal/Main.h>
#include <networktables/NetworkTable.h>
#include <wpi/RuntimeCheck.h>
#include <wpi/condition_variable.h>
#include <wpi/mutex.h>
#include <wpi/string.h>
#include "frc/Errors.h"
#include "frc/RuntimeType.h"
namespace frc {
int RunHALInitialization();
namespace impl {
#ifndef __FRC_ROBORIO__
void ResetMotorSafety();
#endif
template <class Robot>
void RunRobot(wpi::mutex& m, Robot** robot) {
try {
static Robot theRobot;
{
std::scoped_lock lock{m};
*robot = &theRobot;
}
theRobot.StartCompetition();
} catch (const frc::RuntimeError& e) {
e.Report();
FRC_ReportError(
err::Error,
"The robot program quit unexpectedly."
" This is usually due to a code error.\n"
" The above stacktrace can help determine where the error occurred.\n"
" See https://wpilib.org/stacktrace for more information.\n");
throw;
} catch (const std::exception& e) {
HAL_SendError(1, err::Error, 0, e.what(), "", "", 1);
throw;
}
}
} // namespace impl
template <class Robot>
int StartRobot() {
uint32_t foundMajor;
uint32_t foundMinor;
uint32_t expectedMajor;
uint32_t expectedMinor;
WPI_String runtimePath;
if (!WPI_IsRuntimeValid(&foundMajor, &foundMinor, &expectedMajor,
&expectedMinor, &runtimePath)) {
// We could make this error better, however unlike Java, there is only a
// single scenario that could be occuring. The entirety of VS is too out
// of date. In most cases the linker should detect this, but not always.
fmt::println(
"Your copy of Visual Studio is out of date. Please update it.\n");
return 1;
}
int halInit = RunHALInitialization();
if (halInit != 0) {
return halInit;
}
static wpi::mutex m;
static wpi::condition_variable cv;
static Robot* robot = nullptr;
static bool exited = false;
if (HAL_HasMain()) {
std::thread thr([] {
try {
impl::RunRobot<Robot>(m, &robot);
} catch (...) {
HAL_ExitMain();
{
std::scoped_lock lock{m};
robot = nullptr;
exited = true;
}
cv.notify_all();
throw;
}
HAL_ExitMain();
{
std::scoped_lock lock{m};
robot = nullptr;
exited = true;
}
cv.notify_all();
});
HAL_RunMain();
// signal loop to exit
if (robot) {
robot->EndCompetition();
}
// prefer to join, but detach to exit if it doesn't exit in a timely manner
using namespace std::chrono_literals;
std::unique_lock lock{m};
if (cv.wait_for(lock, 1s, [] { return exited; })) {
thr.join();
} else {
thr.detach();
}
} else {
impl::RunRobot<Robot>(m, &robot);
}
#ifndef __FRC_ROBORIO__
frc::impl::ResetMotorSafety();
#endif
HAL_Shutdown();
return 0;
}
/**
* Implement a Robot Program framework. The RobotBase class is intended to be
* subclassed to create a robot program. The user must implement
* StartCompetition() which will be called once and is not expected to exit. The
* user must also implement EndCompetition(), which signals to the code in
* StartCompetition() that it should exit.
*
* It is not recommended to subclass this class directly - instead subclass
* IterativeRobotBase or TimedRobot.
*/
class RobotBase {
public:
/**
* Determine if the Robot is currently enabled.
*
* @return True if the Robot is currently enabled by the Driver Station.
*/
bool IsEnabled() const;
/**
* Determine if the Robot is currently disabled.
*
* @return True if the Robot is currently disabled by the Driver Station.
*/
bool IsDisabled() const;
/**
* Determine if the robot is currently in Autonomous mode.
*
* @return True if the robot is currently operating Autonomously as determined
* by the Driver Station.
*/
bool IsAutonomous() const;
/**
* Determine if the robot is currently in Autonomous mode and enabled.
*
* @return True if the robot us currently operating Autonomously while enabled
* as determined by the Driver Station.
*/
bool IsAutonomousEnabled() const;
/**
* Determine if the robot is currently in Operator Control mode.
*
* @return True if the robot is currently operating in Tele-Op mode as
* determined by the Driver Station.
*/
bool IsTeleop() const;
/**
* Determine if the robot is current in Operator Control mode and enabled.
*
* @return True if the robot is currently operating in Tele-Op mode while
* enabled as determined by the Driver Station.
*/
bool IsTeleopEnabled() const;
/**
* Determine if the robot is currently in Test mode.
*
* @return True if the robot is currently running in Test mode as determined
* by the Driver Station.
*/
bool IsTest() const;
/**
* Determine if the robot is current in Test mode and enabled.
*
* @return True if the robot is currently operating in Test mode while
* enabled as determined by the Driver Station.
*/
bool IsTestEnabled() const;
/**
* Returns the main thread ID.
*
* @return The main thread ID.
*/
static std::thread::id GetThreadId();
/**
* Start the main robot code. This function will be called once and should not
* exit until signalled by EndCompetition()
*/
virtual void StartCompetition() = 0;
/** Ends the main loop in StartCompetition(). */
virtual void EndCompetition() = 0;
/**
* Get the current runtime type.
*
* @return Current runtime type.
*/
static RuntimeType GetRuntimeType();
/**
* Get if the robot is real.
*
* @return If the robot is running in the real world.
*/
static constexpr bool IsReal() {
#ifdef __FRC_ROBORIO__
return true;
#else
return false;
#endif
}
/**
* Get if the robot is a simulation.
*
* @return If the robot is running in simulation.
*/
static constexpr bool IsSimulation() {
#ifdef __FRC_ROBORIO__
return false;
#else
return true;
#endif
}
/**
* Constructor for a generic robot program.
*
* User code can be placed in the constructor that runs before the
* Autonomous or Operator Control period starts. The constructor will run to
* completion before Autonomous is entered.
*
* This must be used to ensure that the communications code starts. In the
* future it would be nice to put this code into it's own task that loads on
* boot so ensure that it runs.
*/
RobotBase();
virtual ~RobotBase() = default;
protected:
RobotBase(RobotBase&&) = default;
RobotBase& operator=(RobotBase&&) = default;
static std::thread::id m_threadId;
NT_Listener connListenerHandle;
bool m_dashboardDetected = false;
};
} // namespace frc