Now you can download a copy of these docs so you can use them offline! Download now
RobotBase.cpp
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2008. 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 $(WIND_BASE)/WPILib. */
5 /*----------------------------------------------------------------------------*/
6 
7 #include "RobotBase.h"
8 
9 #include "DriverStation.h"
10 #include "NetworkCommunication/FRCComm.h"
11 #include "NetworkCommunication/symModuleLink.h"
12 #include "NetworkCommunication/UsageReporting.h"
13 #include "Utility.h"
14 #include <moduleLib.h>
15 #include <taskLib.h>
16 #include <unldLib.h>
17 #include <cstring>
18 
19 RobotBase* RobotBase::m_instance = NULL;
20 
21 const char *FILE_NAME = "/c/FRC_Lib_Version.ini";
22 const char *VERSION_STRING = "C++ 2014 Update 0";
23 
24 
25 void RobotBase::setInstance(RobotBase* robot)
26 {
27  wpi_assert(m_instance == NULL);
28  m_instance = robot;
29 }
30 
31 RobotBase &RobotBase::getInstance()
32 {
33  return *m_instance;
34 }
35 
45  : m_task (NULL)
46  , m_ds (NULL)
47 {
49 }
50 
57 {
59  delete m_task;
60  m_task = NULL;
61  m_instance = NULL;
62 }
63 
70 {
71  return m_watchdog.IsSystemActive();
72 }
73 
80 {
81  return m_watchdog;
82 }
83 
89 {
90  return m_ds->IsEnabled();
91 }
92 
98 {
99  return m_ds->IsDisabled();
100 }
101 
107 {
108  return m_ds->IsAutonomous();
109 }
110 
116 {
117  return m_ds->IsOperatorControl();
118 }
119 
125 {
126  return m_ds->IsTest();
127 }
128 
134 {
135  return m_ds->IsNewControlData();
136 }
137 
141 void RobotBase::robotTask(FUNCPTR factory, Task *task)
142 {
143  RobotBase::setInstance((RobotBase*)factory());
144  RobotBase::getInstance().m_task = task;
145  RobotBase::getInstance().StartCompetition();
146 }
147 
148 void RobotBase::WriteVersionString() {
149  FILE *file = fopen(FILE_NAME, "w");
150  if (file != NULL) {
151  fputs(VERSION_STRING, file);
152  fclose(file);
153  }
154 }
155 
164 void RobotBase::startRobotTask(FUNCPTR factory)
165 {
166 #ifdef SVN_REV
167  if (strlen(SVN_REV))
168  {
169  printf("WPILib was compiled from SVN revision %s\n", SVN_REV);
170  }
171  else
172  {
173  printf("WPILib was compiled from a location that is not source controlled.\n");
174  }
175 #else
176  printf("WPILib was compiled without -D'SVN_REV=nnnn'\n");
177 #endif
178 
179  // Check for startup code already running
180  int32_t oldId = taskNameToId(const_cast<char*>("FRC_RobotTask"));
181  if (oldId != ERROR)
182  {
183  // Find the startup code module.
184  char moduleName[256];
185  moduleNameFindBySymbolName("FRC_UserProgram_StartupLibraryInit", moduleName);
186  MODULE_ID startupModId = moduleFindByName(moduleName);
187  if (startupModId != NULL)
188  {
189  // Remove the startup code.
190  unldByModuleId(startupModId, 0);
191  printf("!!! Error: Default code was still running... It was unloaded for you... Please try again.\n");
192  return;
193  }
194  // This case should no longer get hit.
195  printf("!!! Error: Other robot code is still running... Unload it and then try again.\n");
196  return;
197  }
198 
199  // Let the framework know that we are starting a new user program so the Driver Station can disable.
200  FRC_NetworkCommunication_observeUserProgramStarting();
201 
202  // Let the Usage Reporting framework know that there is a C++ program running
203  nUsageReporting::report(nUsageReporting::kResourceType_Language, nUsageReporting::kLanguage_CPlusPlus);
204 
205  RobotBase::WriteVersionString();
206 
207  // Start robot task
208  // This is done to ensure that the C++ robot task is spawned with the floating point
209  // context save parameter.
210  Task *task = new Task("RobotTask", (FUNCPTR)RobotBase::robotTask, Task::kDefaultPriority, 64000);
211  task->Start((int32_t)factory, (int32_t)task);
212 }
213 
221 {
222 public:
223  RobotDeleter() {}
224  ~RobotDeleter()
225  {
226  delete &RobotBase::getInstance();
227  }
228 };
229 static RobotDeleter g_robotDeleter;
bool Start(uint32_t arg0=0, uint32_t arg1=0, uint32_t arg2=0, uint32_t arg3=0, uint32_t arg4=0, uint32_t arg5=0, uint32_t arg6=0, uint32_t arg7=0, uint32_t arg8=0, uint32_t arg9=0)
Definition: Task.cpp:52
bool IsOperatorControl()
Definition: RobotBase.cpp:115
bool IsNewControlData()
bool IsAutonomous()
Definition: RobotBase.cpp:106
static void DeleteSingletons()
Definition: SensorBase.cpp:58
bool IsEnabled()
Definition: RobotBase.cpp:88
bool IsTest()
Definition: RobotBase.cpp:124
bool IsSystemActive()
Definition: Watchdog.cpp:163
Watchdog & GetWatchdog()
Definition: RobotBase.cpp:79
static void robotTask(FUNCPTR factory, Task *task)
Definition: RobotBase.cpp:141
bool IsDisabled()
Definition: RobotBase.cpp:97
static DriverStation * GetInstance()
Definition: Task.h:17
static void startRobotTask(FUNCPTR factory)
Definition: RobotBase.cpp:164
bool IsSystemActive()
Definition: RobotBase.cpp:69
virtual ~RobotBase()
Definition: RobotBase.cpp:56
bool IsNewDataAvailable()
Definition: RobotBase.cpp:133

Generated on Sat Apr 26 2014 12:26:45 for WPILibC++ by doxygen 1.8.6