WPILibC++  unspecified
RobotBase.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <thread>
11 
12 #include <HAL/HAL.h>
13 #include <wpi/raw_ostream.h>
14 
15 #include "Base.h"
16 
17 namespace frc {
18 
19 class DriverStation;
20 
21 template <class Robot>
22 int StartRobot() {
23  if (!HAL_Initialize(500, 0)) {
24  wpi::errs() << "FATAL ERROR: HAL could not be initialized\n";
25  return -1;
26  }
27  HAL_Report(HALUsageReporting::kResourceType_Language,
28  HALUsageReporting::kLanguage_CPlusPlus);
29  wpi::outs() << "\n********** Robot program starting **********\n";
30  static Robot robot;
31  robot.StartCompetition();
32 
33  return 0;
34 }
35 
36 #define START_ROBOT_CLASS(_ClassName_) \
37  WPI_DEPRECATED("Call frc::StartRobot<" #_ClassName_ \
38  ">() in your own main() instead of using the " \
39  "START_ROBOT_CLASS(" #_ClassName_ ") macro.") \
40  int StartRobotClassImpl() { return frc::StartRobot<_ClassName_>(); } \
41  int main() { return StartRobotClassImpl(); }
42 
53 class RobotBase {
54  public:
60  bool IsEnabled() const;
61 
67  bool IsDisabled() const;
68 
75  bool IsAutonomous() const;
76 
83  bool IsOperatorControl() const;
84 
91  bool IsTest() const;
92 
99  bool IsNewDataAvailable() const;
100 
104  static std::thread::id GetThreadId();
105 
106  virtual void StartCompetition() = 0;
107 
108  static constexpr bool IsReal() {
109 #ifdef __FRC_ROBORIO__
110  return true;
111 #else
112  return false;
113 #endif
114  }
115 
116  static constexpr bool IsSimulation() { return !IsReal(); }
117 
118  protected:
130  RobotBase();
131 
132  virtual ~RobotBase() = default;
133 
134  RobotBase(const RobotBase&) = delete;
135  RobotBase& operator=(const RobotBase&) = delete;
136 
137  DriverStation& m_ds;
138 
139  static std::thread::id m_threadId;
140 };
141 
142 } // namespace frc
bool IsEnabled() const
Determine if the Robot is currently enabled.
Definition: RobotBase.cpp:61
Definition: Utility.cpp:119
bool IsAutonomous() const
Determine if the robot is currently in Autonomous mode.
Definition: RobotBase.cpp:65
bool IsDisabled() const
Determine if the Robot is currently disabled.
Definition: RobotBase.cpp:63
static std::thread::id GetThreadId()
Gets the ID of the main robot thread.
Definition: RobotBase.cpp:73
Provide access to the network communication data to / from the Driver Station.
Definition: DriverStation.h:34
bool IsNewDataAvailable() const
Indicates if new data is available from the driver station.
Definition: RobotBase.cpp:71
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:685
bool IsOperatorControl() const
Determine if the robot is currently in Operator Control mode.
Definition: RobotBase.cpp:67
raw_ostream & outs()
This returns a reference to a raw_ostream for standard output.
Definition: raw_ostream.cpp:675
Implement a Robot Program framework.
Definition: RobotBase.h:53
bool IsTest() const
Determine if the robot is currently in Test mode.
Definition: RobotBase.cpp:69
RobotBase()
Constructor for a generic robot program.
Definition: RobotBase.cpp:75