jerrym | f157933 | 2013-02-07 01:56:28 +0000 | [diff] [blame] | 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 |
|
| 18 | RobotBase* RobotBase::m_instance = NULL;
|
| 19 |
|
| 20 | void RobotBase::setInstance(RobotBase* robot)
|
| 21 | {
|
| 22 | wpi_assert(m_instance == NULL);
|
| 23 | m_instance = robot;
|
| 24 | }
|
| 25 |
|
| 26 | RobotBase &RobotBase::getInstance()
|
| 27 | {
|
| 28 | return *m_instance;
|
| 29 | }
|
| 30 |
|
| 31 | /**
|
| 32 | * Constructor for a generic robot program.
|
| 33 | * User code should be placed in the constuctor that runs before the Autonomous or Operator
|
| 34 | * Control period starts. The constructor will run to completion before Autonomous is entered.
|
| 35 | *
|
| 36 | * This must be used to ensure that the communications code starts. In the future it would be
|
| 37 | * nice to put this code into it's own task that loads on boot so ensure that it runs.
|
| 38 | */
|
| 39 | RobotBase::RobotBase()
|
| 40 | : m_task (NULL)
|
| 41 | , m_ds (NULL)
|
| 42 | {
|
| 43 | m_ds = DriverStation::GetInstance();
|
| 44 | }
|
| 45 |
|
| 46 | /**
|
| 47 | * Free the resources for a RobotBase class.
|
| 48 | * This includes deleting all classes that might have been allocated as Singletons to they
|
| 49 | * would never be deleted except here.
|
| 50 | */
|
| 51 | RobotBase::~RobotBase()
|
| 52 | {
|
| 53 | SensorBase::DeleteSingletons();
|
| 54 | delete m_task;
|
| 55 | m_task = NULL;
|
| 56 | m_instance = NULL;
|
| 57 | }
|
| 58 |
|
| 59 | /**
|
| 60 | * Check on the overall status of the system.
|
| 61 | *
|
| 62 | * @return Is the system active (i.e. PWM motor outputs, etc. enabled)?
|
| 63 | */
|
| 64 | bool RobotBase::IsSystemActive()
|
| 65 | {
|
| 66 | return m_watchdog.IsSystemActive();
|
| 67 | }
|
| 68 |
|
| 69 | /**
|
| 70 | * Return the instance of the Watchdog timer.
|
| 71 | * Get the watchdog timer so the user program can either disable it or feed it when
|
| 72 | * necessary.
|
| 73 | */
|
| 74 | Watchdog &RobotBase::GetWatchdog()
|
| 75 | {
|
| 76 | return m_watchdog;
|
| 77 | }
|
| 78 |
|
| 79 | /**
|
| 80 | * Determine if the Robot is currently enabled.
|
| 81 | * @return True if the Robot is currently enabled by the field controls.
|
| 82 | */
|
| 83 | bool RobotBase::IsEnabled()
|
| 84 | {
|
| 85 | return m_ds->IsEnabled();
|
| 86 | }
|
| 87 |
|
| 88 | /**
|
| 89 | * Determine if the Robot is currently disabled.
|
| 90 | * @return True if the Robot is currently disabled by the field controls.
|
| 91 | */
|
| 92 | bool RobotBase::IsDisabled()
|
| 93 | {
|
| 94 | return m_ds->IsDisabled();
|
| 95 | }
|
| 96 |
|
| 97 | /**
|
| 98 | * Determine if the robot is currently in Autnomous mode.
|
| 99 | * @return True if the robot is currently operating Autonomously as determined by the field controls.
|
| 100 | */
|
| 101 | bool RobotBase::IsAutonomous()
|
| 102 | {
|
| 103 | return m_ds->IsAutonomous();
|
| 104 | }
|
| 105 |
|
| 106 | /**
|
| 107 | * Determine if the robot is currently in Operator Control mode.
|
| 108 | * @return True if the robot is currently operating in Tele-Op mode as determined by the field controls.
|
| 109 | */
|
| 110 | bool RobotBase::IsOperatorControl()
|
| 111 | {
|
| 112 | return m_ds->IsOperatorControl();
|
| 113 | }
|
| 114 |
|
| 115 | /**
|
| 116 | * Determine if the robot is currently in Test mode.
|
| 117 | * @return True if the robot is currently running tests as determined by the field controls.
|
| 118 | */
|
| 119 | bool RobotBase::IsTest()
|
| 120 | {
|
| 121 | return m_ds->IsTest();
|
| 122 | }
|
| 123 |
|
| 124 | /**
|
| 125 | * Indicates if new data is available from the driver station.
|
| 126 | * @return Has new data arrived over the network since the last time this function was called?
|
| 127 | */
|
| 128 | bool RobotBase::IsNewDataAvailable()
|
| 129 | {
|
| 130 | return m_ds->IsNewControlData();
|
| 131 | }
|
| 132 |
|
| 133 | /**
|
| 134 | * Static interface that will start the competition in the new task.
|
| 135 | */
|
| 136 | void RobotBase::robotTask(FUNCPTR factory, Task *task)
|
| 137 | {
|
| 138 | RobotBase::setInstance((RobotBase*)factory());
|
| 139 | RobotBase::getInstance().m_task = task;
|
| 140 | RobotBase::getInstance().StartCompetition();
|
| 141 | }
|
| 142 |
|
| 143 | /**
|
| 144 | *
|
| 145 | * Start the robot code.
|
| 146 | * This function starts the robot code running by spawning a task. Currently tasks seemed to be
|
| 147 | * started by LVRT without setting the VX_FP_TASK flag so floating point context is not saved on
|
| 148 | * interrupts. Therefore the program experiences hard to debug and unpredictable results. So the
|
| 149 | * LVRT code starts this function, and it, in turn, starts the actual user program.
|
| 150 | */
|
| 151 | void RobotBase::startRobotTask(FUNCPTR factory)
|
| 152 | {
|
| 153 | #ifdef SVN_REV
|
| 154 | if (strlen(SVN_REV))
|
| 155 | {
|
| 156 | printf("WPILib was compiled from SVN revision %s\n", SVN_REV);
|
| 157 | }
|
| 158 | else
|
| 159 | {
|
| 160 | printf("WPILib was compiled from a location that is not source controlled.\n");
|
| 161 | }
|
| 162 | #else
|
| 163 | printf("WPILib was compiled without -D'SVN_REV=nnnn'\n");
|
| 164 | #endif
|
| 165 |
|
| 166 | // Check for startup code already running
|
| 167 | INT32 oldId = taskNameToId("FRC_RobotTask");
|
| 168 | if (oldId != ERROR)
|
| 169 | {
|
| 170 | // Find the startup code module.
|
| 171 | char moduleName[256];
|
| 172 | moduleNameFindBySymbolName("FRC_UserProgram_StartupLibraryInit", moduleName);
|
| 173 | MODULE_ID startupModId = moduleFindByName(moduleName);
|
| 174 | if (startupModId != NULL)
|
| 175 | {
|
| 176 | // Remove the startup code.
|
| 177 | unldByModuleId(startupModId, 0);
|
| 178 | printf("!!! Error: Default code was still running... It was unloaded for you... Please try again.\n");
|
| 179 | return;
|
| 180 | }
|
| 181 | // This case should no longer get hit.
|
| 182 | printf("!!! Error: Other robot code is still running... Unload it and then try again.\n");
|
| 183 | return;
|
| 184 | }
|
| 185 |
|
| 186 | // Let the framework know that we are starting a new user program so the Driver Station can disable.
|
| 187 | FRC_NetworkCommunication_observeUserProgramStarting();
|
| 188 |
|
| 189 | // Let the Usage Reporting framework know that there is a C++ program running
|
| 190 | nUsageReporting::report(nUsageReporting::kResourceType_Language, nUsageReporting::kLanguage_CPlusPlus);
|
| 191 |
|
| 192 | // Start robot task
|
| 193 | // This is done to ensure that the C++ robot task is spawned with the floating point
|
| 194 | // context save parameter.
|
| 195 | Task *task = new Task("RobotTask", (FUNCPTR)RobotBase::robotTask, Task::kDefaultPriority, 64000);
|
| 196 | task->Start((INT32)factory, (INT32)task);
|
| 197 | }
|
| 198 |
|
| 199 | /**
|
| 200 | * This class exists for the sole purpose of getting its destructor called when the module unloads.
|
| 201 | * Before the module is done unloading, we need to delete the RobotBase derived singleton. This should delete
|
| 202 | * the other remaining singletons that were registered. This should also stop all tasks that are using
|
| 203 | * the Task class.
|
| 204 | */
|
| 205 | class RobotDeleter
|
| 206 | {
|
| 207 | public:
|
| 208 | RobotDeleter() {}
|
| 209 | ~RobotDeleter()
|
| 210 | {
|
| 211 | delete &RobotBase::getInstance();
|
| 212 | }
|
| 213 | };
|
| 214 | static RobotDeleter g_robotDeleter;
|