WPILibC++ 2025.3.1
Loading...
Searching...
No Matches
RobotBase.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <chrono>
8#include <thread>
9
10#include <hal/DriverStation.h>
11#include <hal/HALBase.h>
12#include <hal/Main.h>
14#include <wpi/RuntimeCheck.h>
16#include <wpi/mutex.h>
17#include <wpi/string.h>
18
19#include "frc/Errors.h"
20#include "frc/RuntimeType.h"
21
22namespace frc {
23
25
26namespace impl {
27#ifndef __FRC_ROBORIO__
29#endif
30
31template <class Robot>
32void RunRobot(wpi::mutex& m, Robot** robot) {
33 try {
34 static Robot theRobot;
35 {
36 std::scoped_lock lock{m};
37 *robot = &theRobot;
38 }
39 theRobot.StartCompetition();
40 } catch (const frc::RuntimeError& e) {
41 e.Report();
43 err::Error,
44 "The robot program quit unexpectedly."
45 " This is usually due to a code error.\n"
46 " The above stacktrace can help determine where the error occurred.\n"
47 " See https://wpilib.org/stacktrace for more information.\n");
48 throw;
49 } catch (const std::exception& e) {
50 HAL_SendError(1, err::Error, 0, e.what(), "", "", 1);
51 throw;
52 }
53}
54
55} // namespace impl
56
57template <class Robot>
59 uint32_t foundMajor;
60 uint32_t foundMinor;
61 uint32_t expectedMajor;
62 uint32_t expectedMinor;
63 WPI_String runtimePath;
64 if (!WPI_IsRuntimeValid(&foundMajor, &foundMinor, &expectedMajor,
65 &expectedMinor, &runtimePath)) {
66 // We could make this error better, however unlike Java, there is only a
67 // single scenario that could be occuring. The entirety of VS is too out
68 // of date. In most cases the linker should detect this, but not always.
69 fmt::println(
70 "Your copy of Visual Studio is out of date. Please update it.\n");
71 return 1;
72 }
73
74 int halInit = RunHALInitialization();
75 if (halInit != 0) {
76 return halInit;
77 }
78
79 static wpi::mutex m;
81 static Robot* robot = nullptr;
82 static bool exited = false;
83
84 if (HAL_HasMain()) {
85 std::thread thr([] {
86 try {
87 impl::RunRobot<Robot>(m, &robot);
88 } catch (...) {
90 {
91 std::scoped_lock lock{m};
92 robot = nullptr;
93 exited = true;
94 }
95 cv.notify_all();
96 throw;
97 }
98
100 {
101 std::scoped_lock lock{m};
102 robot = nullptr;
103 exited = true;
104 }
105 cv.notify_all();
106 });
107
108 HAL_RunMain();
109
110 // signal loop to exit
111 if (robot) {
112 robot->EndCompetition();
113 }
114
115 // prefer to join, but detach to exit if it doesn't exit in a timely manner
116 using namespace std::chrono_literals;
117 std::unique_lock lock{m};
118 if (cv.wait_for(lock, 1s, [] { return exited; })) {
119 thr.join();
120 } else {
121 thr.detach();
122 }
123 } else {
124 impl::RunRobot<Robot>(m, &robot);
125 }
126
127#ifndef __FRC_ROBORIO__
129#endif
130 HAL_Shutdown();
131
132 return 0;
133}
134
135/**
136 * Implement a Robot Program framework. The RobotBase class is intended to be
137 * subclassed to create a robot program. The user must implement
138 * StartCompetition() which will be called once and is not expected to exit. The
139 * user must also implement EndCompetition(), which signals to the code in
140 * StartCompetition() that it should exit.
141 *
142 * It is not recommended to subclass this class directly - instead subclass
143 * IterativeRobotBase or TimedRobot.
144 */
146 public:
147 /**
148 * Determine if the Robot is currently enabled.
149 *
150 * @return True if the Robot is currently enabled by the Driver Station.
151 */
152 bool IsEnabled() const;
153
154 /**
155 * Determine if the Robot is currently disabled.
156 *
157 * @return True if the Robot is currently disabled by the Driver Station.
158 */
159 bool IsDisabled() const;
160
161 /**
162 * Determine if the robot is currently in Autonomous mode.
163 *
164 * @return True if the robot is currently operating Autonomously as determined
165 * by the Driver Station.
166 */
167 bool IsAutonomous() const;
168
169 /**
170 * Determine if the robot is currently in Autonomous mode and enabled.
171 *
172 * @return True if the robot us currently operating Autonomously while enabled
173 * as determined by the Driver Station.
174 */
176
177 /**
178 * Determine if the robot is currently in Operator Control mode.
179 *
180 * @return True if the robot is currently operating in Tele-Op mode as
181 * determined by the Driver Station.
182 */
183 bool IsTeleop() const;
184
185 /**
186 * Determine if the robot is current in Operator Control mode and enabled.
187 *
188 * @return True if the robot is currently operating in Tele-Op mode while
189 * enabled as determined by the Driver Station.
190 */
191 bool IsTeleopEnabled() const;
192
193 /**
194 * Determine if the robot is currently in Test mode.
195 *
196 * @return True if the robot is currently running in Test mode as determined
197 * by the Driver Station.
198 */
199 bool IsTest() const;
200
201 /**
202 * Determine if the robot is current in Test mode and enabled.
203 *
204 * @return True if the robot is currently operating in Test mode while
205 * enabled as determined by the Driver Station.
206 */
207 bool IsTestEnabled() const;
208
209 /**
210 * Returns the main thread ID.
211 *
212 * @return The main thread ID.
213 */
214 static std::thread::id GetThreadId();
215
216 /**
217 * Start the main robot code. This function will be called once and should not
218 * exit until signalled by EndCompetition()
219 */
220 virtual void StartCompetition() = 0;
221
222 /** Ends the main loop in StartCompetition(). */
223 virtual void EndCompetition() = 0;
224
225 /**
226 * Get the current runtime type.
227 *
228 * @return Current runtime type.
229 */
231
232 /**
233 * Get if the robot is real.
234 *
235 * @return If the robot is running in the real world.
236 */
237 static constexpr bool IsReal() {
238#ifdef __FRC_ROBORIO__
239 return true;
240#else
241 return false;
242#endif
243 }
244
245 /**
246 * Get if the robot is a simulation.
247 *
248 * @return If the robot is running in simulation.
249 */
250 static constexpr bool IsSimulation() {
251#ifdef __FRC_ROBORIO__
252 return false;
253#else
254 return true;
255#endif
256 }
257
258 /**
259 * Constructor for a generic robot program.
260 *
261 * User code can be placed in the constructor that runs before the
262 * Autonomous or Operator Control period starts. The constructor will run to
263 * completion before Autonomous is entered.
264 *
265 * This must be used to ensure that the communications code starts. In the
266 * future it would be nice to put this code into it's own task that loads on
267 * boot so ensure that it runs.
268 */
270
271 virtual ~RobotBase() = default;
272
273 protected:
274 RobotBase(RobotBase&&) = default;
276
277 static std::thread::id m_threadId;
280};
281
282} // namespace frc
int32_t WPI_IsRuntimeValid(uint32_t *FoundMajor, uint32_t *FoundMinor, uint32_t *ExpectedMajor, uint32_t *ExpectedMinor, WPI_String *RuntimePath)
Implement a Robot Program framework.
Definition RobotBase.h:145
RobotBase()
Constructor for a generic robot program.
bool IsTestEnabled() const
Determine if the robot is current in Test mode and enabled.
static constexpr bool IsReal()
Get if the robot is real.
Definition RobotBase.h:237
static constexpr bool IsSimulation()
Get if the robot is a simulation.
Definition RobotBase.h:250
bool IsTest() const
Determine if the robot is currently in Test mode.
bool m_dashboardDetected
Definition RobotBase.h:279
static std::thread::id GetThreadId()
Returns the main thread ID.
static std::thread::id m_threadId
Definition RobotBase.h:277
RobotBase & operator=(RobotBase &&)=default
bool IsAutonomousEnabled() const
Determine if the robot is currently in Autonomous mode and enabled.
bool IsEnabled() const
Determine if the Robot is currently enabled.
virtual void StartCompetition()=0
Start the main robot code.
bool IsTeleop() const
Determine if the robot is currently in Operator Control mode.
static RuntimeType GetRuntimeType()
Get the current runtime type.
NT_Listener connListenerHandle
Definition RobotBase.h:278
bool IsDisabled() const
Determine if the Robot is currently disabled.
virtual void EndCompetition()=0
Ends the main loop in StartCompetition().
bool IsAutonomous() const
Determine if the robot is currently in Autonomous mode.
bool IsTeleopEnabled() const
Determine if the robot is current in Operator Control mode and enabled.
virtual ~RobotBase()=default
RobotBase(RobotBase &&)=default
Runtime error exception.
Definition Errors.h:20
void Report() const
Reports error to Driver Station (using HAL_SendError).
void HAL_Shutdown(void)
Call this to shut down HAL.
int32_t HAL_SendError(HAL_Bool isError, int32_t errorCode, HAL_Bool isLVCode, const char *details, const char *location, const char *callStack, HAL_Bool printMsg)
Sends an error to the driver station.
void HAL_RunMain(void)
Runs the main function provided to HAL_SetMain().
HAL_Bool HAL_HasMain(void)
Returns true if HAL_SetMain() has been called.
void HAL_ExitMain(void)
Causes HAL_RunMain() to exit.
NT_Handle NT_Listener
Definition ntcore_c.h:39
Definition VisionPipeline.h:7
void ResetMotorSafety()
void RunRobot(wpi::mutex &m, Robot **robot)
Definition RobotBase.h:32
Definition CAN.h:11
int StartRobot()
Definition RobotBase.h:58
int RunHALInitialization()
RuntimeType
Runtime type.
Definition RuntimeType.h:11
::std::condition_variable condition_variable
Definition condition_variable.h:16
::std::mutex mutex
Definition mutex.h:17
A const UTF8 string.
Definition string.h:14
#define FRC_ReportError(status, format,...)
Reports an error to the driver station (using HAL_SendError).
Definition Errors.h:137