2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2015-06-25 15:07:55 -04:00
|
|
|
/* Copyright (c) FIRST 2008. All Rights Reserved.
|
|
|
|
|
*/
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "DriverStation.h"
|
2014-06-12 18:07:45 -04:00
|
|
|
#include "AnalogInput.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "Timer.h"
|
2014-08-08 17:05:49 -04:00
|
|
|
#include "NetworkCommunication/FRCComm.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "MotorSafetyHelper.h"
|
|
|
|
|
#include "Utility.h"
|
|
|
|
|
#include "WPIErrors.h"
|
|
|
|
|
#include <string.h>
|
2014-05-02 17:54:01 -04:00
|
|
|
#include "Log.hpp"
|
2014-01-06 10:12:21 -05:00
|
|
|
|
|
|
|
|
// set the logging level
|
|
|
|
|
TLogLevel dsLogLevel = logDEBUG;
|
2014-12-03 11:02:12 -05:00
|
|
|
const double JOYSTICK_UNPLUGGED_MESSAGE_INTERVAL = 1.0;
|
2014-01-06 10:12:21 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
#define DS_LOG(level) \
|
|
|
|
|
if (level > dsLogLevel) \
|
|
|
|
|
; \
|
|
|
|
|
else \
|
|
|
|
|
Log().Get(level)
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
const uint32_t DriverStation::kJoystickPorts;
|
|
|
|
|
|
|
|
|
|
/**
|
2014-12-11 16:44:55 -05:00
|
|
|
* DriverStation constructor.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* This is only called once the first time GetInstance() is called
|
|
|
|
|
*/
|
2015-06-24 01:06:29 -07:00
|
|
|
DriverStation::DriverStation() {
|
2015-06-25 15:07:55 -04:00
|
|
|
// All joysticks should default to having zero axes, povs and buttons, so
|
|
|
|
|
// uninitialized memory doesn't get sent to speed controllers.
|
|
|
|
|
for (unsigned int i = 0; i < kJoystickPorts; i++) {
|
|
|
|
|
m_joystickAxes[i].count = 0;
|
|
|
|
|
m_joystickPOVs[i].count = 0;
|
|
|
|
|
m_joystickButtons[i].count = 0;
|
|
|
|
|
m_joystickDescriptor[i].isXbox = 0;
|
|
|
|
|
m_joystickDescriptor[i].type = -1;
|
|
|
|
|
m_joystickDescriptor[i].name[0] = '\0';
|
|
|
|
|
}
|
|
|
|
|
// Register that semaphore with the network communications task.
|
|
|
|
|
// It will signal when new packet data is available.
|
2015-11-15 21:24:03 -08:00
|
|
|
HALSetNewDataSem(&m_packetDataAvailableCond);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
AddToSingletonList();
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2014-08-14 00:07:02 -07:00
|
|
|
m_task = Task("DriverStation", &DriverStation::Run, this);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
DriverStation::~DriverStation() {
|
2014-08-14 00:07:02 -07:00
|
|
|
m_isRunning = false;
|
|
|
|
|
m_task.join();
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
// Unregister our semaphore.
|
2015-06-23 04:49:51 -07:00
|
|
|
HALSetNewDataSem(nullptr);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
void DriverStation::Run() {
|
2014-08-14 00:07:02 -07:00
|
|
|
m_isRunning = true;
|
2015-06-25 15:07:55 -04:00
|
|
|
int period = 0;
|
2014-08-14 00:07:02 -07:00
|
|
|
while (m_isRunning) {
|
2015-06-25 01:54:20 -07:00
|
|
|
{
|
|
|
|
|
std::unique_lock<priority_mutex> lock(m_packetDataAvailableMutex);
|
|
|
|
|
m_packetDataAvailableCond.wait(lock);
|
|
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
GetData();
|
2015-06-25 01:54:20 -07:00
|
|
|
m_waitForDataCond.notify_all();
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
if (++period >= 4) {
|
|
|
|
|
MotorSafetyHelper::CheckMotors();
|
|
|
|
|
period = 0;
|
|
|
|
|
}
|
|
|
|
|
if (m_userInDisabled) HALNetworkCommunicationObserveUserProgramDisabled();
|
|
|
|
|
if (m_userInAutonomous)
|
|
|
|
|
HALNetworkCommunicationObserveUserProgramAutonomous();
|
|
|
|
|
if (m_userInTeleop) HALNetworkCommunicationObserveUserProgramTeleop();
|
|
|
|
|
if (m_userInTest) HALNetworkCommunicationObserveUserProgramTest();
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a pointer to the singleton DriverStation.
|
2014-12-29 14:09:37 -05:00
|
|
|
* @return Pointer to the DS instance
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
DriverStation &DriverStation::GetInstance() {
|
2015-12-03 00:44:17 -08:00
|
|
|
static DriverStation *instance = new DriverStation();
|
|
|
|
|
return *instance;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-12-11 16:44:55 -05:00
|
|
|
/**
|
|
|
|
|
* Copy data from the DS task for the user.
|
|
|
|
|
* If no new data exists, it will just be returned, otherwise
|
|
|
|
|
* the data will be copied from the DS polling loop.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void DriverStation::GetData() {
|
|
|
|
|
// Get the status of all of the joysticks
|
|
|
|
|
for (uint8_t stick = 0; stick < kJoystickPorts; stick++) {
|
|
|
|
|
HALGetJoystickAxes(stick, &m_joystickAxes[stick]);
|
|
|
|
|
HALGetJoystickPOVs(stick, &m_joystickPOVs[stick]);
|
|
|
|
|
HALGetJoystickButtons(stick, &m_joystickButtons[stick]);
|
|
|
|
|
HALGetJoystickDescriptor(stick, &m_joystickDescriptor[stick]);
|
|
|
|
|
}
|
2015-06-25 01:54:20 -07:00
|
|
|
m_newControlData.give();
|
2014-12-11 16:44:55 -05:00
|
|
|
}
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
2014-08-06 11:29:15 -04:00
|
|
|
* Read the battery voltage.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2014-12-29 14:09:37 -05:00
|
|
|
* @return The battery voltage in Volts.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
float DriverStation::GetBatteryVoltage() const {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
float voltage = getVinVoltage(&status);
|
|
|
|
|
wpi_setErrorWithContext(status, "getVinVoltage");
|
2014-08-08 14:56:22 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
return voltage;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-12-29 14:09:37 -05:00
|
|
|
/**
|
|
|
|
|
* Reports errors related to unplugged joysticks
|
|
|
|
|
* Throttles the errors so that they don't overwhelm the DS
|
|
|
|
|
*/
|
2014-12-03 07:42:48 -05:00
|
|
|
void DriverStation::ReportJoystickUnpluggedError(std::string message) {
|
2015-06-25 15:07:55 -04:00
|
|
|
double currentTime = Timer::GetFPGATimestamp();
|
|
|
|
|
if (currentTime > m_nextMessageTime) {
|
|
|
|
|
ReportError(message);
|
|
|
|
|
m_nextMessageTime = currentTime + JOYSTICK_UNPLUGGED_MESSAGE_INTERVAL;
|
|
|
|
|
}
|
2014-12-03 07:42:48 -05:00
|
|
|
}
|
|
|
|
|
|
2015-01-24 18:02:06 -05:00
|
|
|
/**
|
2014-12-29 14:09:37 -05:00
|
|
|
* Returns the number of axes on a given joystick port
|
2014-12-05 20:13:23 -05:00
|
|
|
*
|
|
|
|
|
* @param stick The joystick port number
|
2014-12-29 14:09:37 -05:00
|
|
|
* @return The number of axes on the indicated joystick
|
2014-12-05 20:13:23 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
int DriverStation::GetStickAxisCount(uint32_t stick) const {
|
|
|
|
|
if (stick >= kJoystickPorts) {
|
|
|
|
|
wpi_setWPIError(BadJoystickIndex);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
HALJoystickAxes joystickAxes;
|
|
|
|
|
HALGetJoystickAxes(stick, &joystickAxes);
|
|
|
|
|
return joystickAxes.count;
|
2014-12-05 20:13:23 -05:00
|
|
|
}
|
|
|
|
|
|
2015-01-24 18:02:06 -05:00
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* Returns the name of the joystick at the given port
|
2015-06-15 12:34:57 -04:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param stick The joystick port number
|
|
|
|
|
* @return The name of the joystick at the given port
|
2015-06-15 12:34:57 -04:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
std::string DriverStation::GetJoystickName(uint32_t stick) const {
|
|
|
|
|
if (stick >= kJoystickPorts) {
|
|
|
|
|
wpi_setWPIError(BadJoystickIndex);
|
|
|
|
|
}
|
|
|
|
|
std::string retVal(m_joystickDescriptor[0].name);
|
|
|
|
|
return retVal;
|
2015-06-15 12:34:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* Returns the type of joystick at a given port
|
2015-06-15 12:34:57 -04:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param stick The joystick port number
|
|
|
|
|
* @return The HID type of joystick at the given port
|
2015-06-15 12:34:57 -04:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
int DriverStation::GetJoystickType(uint32_t stick) const {
|
|
|
|
|
if (stick >= kJoystickPorts) {
|
|
|
|
|
wpi_setWPIError(BadJoystickIndex);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return (int)m_joystickDescriptor[stick].type;
|
2015-06-15 12:34:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* Returns a boolean indicating if the controller is an xbox controller.
|
2015-06-15 12:34:57 -04:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param stick The joystick port number
|
|
|
|
|
* @return A boolean that is true if the controller is an xbox controller.
|
2015-06-15 12:34:57 -04:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool DriverStation::GetJoystickIsXbox(uint32_t stick) const {
|
|
|
|
|
if (stick >= kJoystickPorts) {
|
|
|
|
|
wpi_setWPIError(BadJoystickIndex);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return (bool)m_joystickDescriptor[stick].isXbox;
|
2015-06-15 12:34:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* Returns the types of Axes on a given joystick port
|
2015-06-15 12:34:57 -04:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* @param stick The joystick port number and the target axis
|
|
|
|
|
* @return What type of axis the axis is reporting to be
|
2015-06-15 12:34:57 -04:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
int DriverStation::GetJoystickAxisType(uint32_t stick, uint8_t axis) const {
|
|
|
|
|
if (stick >= kJoystickPorts) {
|
|
|
|
|
wpi_setWPIError(BadJoystickIndex);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return m_joystickDescriptor[stick].axisTypes[axis];
|
2015-06-15 12:34:57 -04:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
/**
|
2014-12-29 14:09:37 -05:00
|
|
|
* Returns the number of POVs on a given joystick port
|
2014-12-05 20:13:23 -05:00
|
|
|
*
|
|
|
|
|
* @param stick The joystick port number
|
2014-12-29 14:09:37 -05:00
|
|
|
* @return The number of POVs on the indicated joystick
|
2014-12-05 20:13:23 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
int DriverStation::GetStickPOVCount(uint32_t stick) const {
|
|
|
|
|
if (stick >= kJoystickPorts) {
|
|
|
|
|
wpi_setWPIError(BadJoystickIndex);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
HALJoystickPOVs joystickPOVs;
|
|
|
|
|
HALGetJoystickPOVs(stick, &joystickPOVs);
|
|
|
|
|
return joystickPOVs.count;
|
2014-12-05 20:13:23 -05:00
|
|
|
}
|
|
|
|
|
|
2015-01-24 18:02:06 -05:00
|
|
|
/**
|
2014-12-29 14:09:37 -05:00
|
|
|
* Returns the number of buttons on a given joystick port
|
2014-12-05 20:13:23 -05:00
|
|
|
*
|
|
|
|
|
* @param stick The joystick port number
|
|
|
|
|
* @return The number of buttons on the indicated joystick
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
int DriverStation::GetStickButtonCount(uint32_t stick) const {
|
|
|
|
|
if (stick >= kJoystickPorts) {
|
|
|
|
|
wpi_setWPIError(BadJoystickIndex);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
HALJoystickButtons joystickButtons;
|
|
|
|
|
HALGetJoystickButtons(stick, &joystickButtons);
|
|
|
|
|
return joystickButtons.count;
|
2014-12-05 20:13:23 -05:00
|
|
|
}
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
|
|
|
|
* Get the value of the axis on a joystick.
|
|
|
|
|
* This depends on the mapping of the joystick connected to the specified port.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param stick The joystick to read.
|
|
|
|
|
* @param axis The analog axis value to read from the joystick.
|
|
|
|
|
* @return The value of the axis on the joystick.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
float DriverStation::GetStickAxis(uint32_t stick, uint32_t axis) {
|
|
|
|
|
if (stick >= kJoystickPorts) {
|
|
|
|
|
wpi_setWPIError(BadJoystickIndex);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (axis >= m_joystickAxes[stick].count) {
|
|
|
|
|
if (axis >= kMaxJoystickAxes)
|
|
|
|
|
wpi_setWPIError(BadJoystickAxis);
|
|
|
|
|
else
|
|
|
|
|
ReportJoystickUnpluggedError(
|
|
|
|
|
"WARNING: Joystick Axis missing, check if all controllers are "
|
|
|
|
|
"plugged in\n");
|
|
|
|
|
return 0.0f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int8_t value = m_joystickAxes[stick].axes[axis];
|
|
|
|
|
|
|
|
|
|
if (value < 0) {
|
|
|
|
|
return value / 128.0f;
|
|
|
|
|
} else {
|
|
|
|
|
return value / 127.0f;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-10-17 14:46:25 -04:00
|
|
|
/**
|
|
|
|
|
* Get the state of a POV on the joystick.
|
|
|
|
|
*
|
|
|
|
|
* @return the angle of the POV in degrees, or -1 if the POV is not pressed.
|
|
|
|
|
*/
|
|
|
|
|
int DriverStation::GetStickPOV(uint32_t stick, uint32_t pov) {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (stick >= kJoystickPorts) {
|
|
|
|
|
wpi_setWPIError(BadJoystickIndex);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2014-12-11 16:44:55 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
if (pov >= m_joystickPOVs[stick].count) {
|
|
|
|
|
if (pov >= kMaxJoystickPOVs)
|
|
|
|
|
wpi_setWPIError(BadJoystickAxis);
|
|
|
|
|
else
|
|
|
|
|
ReportJoystickUnpluggedError(
|
|
|
|
|
"WARNING: Joystick POV missing, check if all controllers are plugged "
|
|
|
|
|
"in\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2014-10-17 14:46:25 -04:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
return m_joystickPOVs[stick].povs[pov];
|
2014-10-17 14:46:25 -04:00
|
|
|
}
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
|
|
|
|
* The state of the buttons on the joystick.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* @param stick The joystick to read.
|
|
|
|
|
* @return The state of the buttons on the joystick.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
uint32_t DriverStation::GetStickButtons(uint32_t stick) const {
|
|
|
|
|
if (stick >= kJoystickPorts) {
|
|
|
|
|
wpi_setWPIError(BadJoystickIndex);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2014-12-18 10:57:11 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
return m_joystickButtons[stick].buttons;
|
2014-12-18 10:57:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The state of one joystick button. Button indexes begin at 1.
|
|
|
|
|
*
|
|
|
|
|
* @param stick The joystick to read.
|
|
|
|
|
* @param button The button index, beginning at 1.
|
|
|
|
|
* @return The state of the joystick button.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool DriverStation::GetStickButton(uint32_t stick, uint8_t button) {
|
|
|
|
|
if (stick >= kJoystickPorts) {
|
|
|
|
|
wpi_setWPIError(BadJoystickIndex);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (button > m_joystickButtons[stick].count) {
|
|
|
|
|
ReportJoystickUnpluggedError(
|
|
|
|
|
"WARNING: Joystick Button missing, check if all controllers are "
|
|
|
|
|
"plugged in\n");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (button == 0) {
|
|
|
|
|
ReportJoystickUnpluggedError(
|
|
|
|
|
"ERROR: Button indexes begin at 1 in WPILib for C++ and Java");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return ((0x1 << (button - 1)) & m_joystickButtons[stick].buttons) != 0;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-12-29 14:09:37 -05:00
|
|
|
/**
|
|
|
|
|
* Check if the DS has enabled the robot
|
|
|
|
|
* @return True if the robot is enabled and the DS is connected
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool DriverStation::IsEnabled() const {
|
|
|
|
|
HALControlWord controlWord;
|
|
|
|
|
memset(&controlWord, 0, sizeof(controlWord));
|
|
|
|
|
HALGetControlWord(&controlWord);
|
|
|
|
|
return controlWord.enabled && controlWord.dsAttached;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-12-29 14:09:37 -05:00
|
|
|
/**
|
|
|
|
|
* Check if the robot is disabled
|
|
|
|
|
* @return True if the robot is explicitly disabled or the DS is not connected
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool DriverStation::IsDisabled() const {
|
|
|
|
|
HALControlWord controlWord;
|
|
|
|
|
memset(&controlWord, 0, sizeof(controlWord));
|
|
|
|
|
HALGetControlWord(&controlWord);
|
|
|
|
|
return !(controlWord.enabled && controlWord.dsAttached);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-12-29 14:09:37 -05:00
|
|
|
/**
|
|
|
|
|
* Check if the DS is commanding autonomous mode
|
|
|
|
|
* @return True if the robot is being commanded to be in autonomous mode
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool DriverStation::IsAutonomous() const {
|
|
|
|
|
HALControlWord controlWord;
|
|
|
|
|
memset(&controlWord, 0, sizeof(controlWord));
|
|
|
|
|
HALGetControlWord(&controlWord);
|
|
|
|
|
return controlWord.autonomous;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-12-29 14:09:37 -05:00
|
|
|
/**
|
|
|
|
|
* Check if the DS is commanding teleop mode
|
|
|
|
|
* @return True if the robot is being commanded to be in teleop mode
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool DriverStation::IsOperatorControl() const {
|
|
|
|
|
HALControlWord controlWord;
|
|
|
|
|
memset(&controlWord, 0, sizeof(controlWord));
|
|
|
|
|
HALGetControlWord(&controlWord);
|
|
|
|
|
return !(controlWord.autonomous || controlWord.test);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-12-29 14:09:37 -05:00
|
|
|
/**
|
|
|
|
|
* Check if the DS is commanding test mode
|
|
|
|
|
* @return True if the robot is being commanded to be in test mode
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool DriverStation::IsTest() const {
|
|
|
|
|
HALControlWord controlWord;
|
|
|
|
|
HALGetControlWord(&controlWord);
|
|
|
|
|
return controlWord.test;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-12-29 14:09:37 -05:00
|
|
|
/**
|
|
|
|
|
* Check if the DS is attached
|
|
|
|
|
* @return True if the DS is connected to the robot
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool DriverStation::IsDSAttached() const {
|
|
|
|
|
HALControlWord controlWord;
|
|
|
|
|
memset(&controlWord, 0, sizeof(controlWord));
|
|
|
|
|
HALGetControlWord(&controlWord);
|
|
|
|
|
return controlWord.dsAttached;
|
2014-11-18 10:56:25 -05:00
|
|
|
}
|
|
|
|
|
|
2014-12-29 14:09:37 -05:00
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* Check if the FPGA outputs are enabled. The outputs may be disabled if the
|
|
|
|
|
* robot is disabled
|
2014-12-29 14:09:37 -05:00
|
|
|
* or e-stopped, the watchdog has expired, or if the roboRIO browns out.
|
|
|
|
|
* @return True if the FPGA outputs are enabled.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool DriverStation::IsSysActive() const {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
bool retVal = HALGetSystemActive(&status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
return retVal;
|
2014-11-18 10:56:25 -05:00
|
|
|
}
|
|
|
|
|
|
2014-12-29 14:09:37 -05:00
|
|
|
/**
|
|
|
|
|
* Check if the system is browned out.
|
|
|
|
|
* @return True if the system is browned out
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool DriverStation::IsSysBrownedOut() const {
|
|
|
|
|
int32_t status = 0;
|
|
|
|
|
bool retVal = HALGetBrownedOut(&status);
|
|
|
|
|
wpi_setErrorWithContext(status, getHALErrorMessage(status));
|
|
|
|
|
return retVal;
|
2014-11-18 10:56:25 -05:00
|
|
|
}
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* Has a new control packet from the driver station arrived since the last time
|
|
|
|
|
* this function was called?
|
2013-12-15 18:30:16 -05:00
|
|
|
* Warning: If you call this function from more than one place at the same time,
|
2014-12-29 14:09:37 -05:00
|
|
|
* you will not get the get the intended behaviour.
|
2013-12-15 18:30:16 -05:00
|
|
|
* @return True if the control data has been updated since the last call.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool DriverStation::IsNewControlData() const {
|
2015-06-25 01:54:20 -07:00
|
|
|
return m_newControlData.tryTake() == false;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Is the driver station attached to a Field Management System?
|
2015-06-25 15:07:55 -04:00
|
|
|
* @return True if the robot is competing on a field being controlled by a Field
|
|
|
|
|
* Management System
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool DriverStation::IsFMSAttached() const {
|
|
|
|
|
HALControlWord controlWord;
|
|
|
|
|
HALGetControlWord(&controlWord);
|
|
|
|
|
return controlWord.fmsAttached;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the alliance that the driver station says it is on.
|
|
|
|
|
* This could return kRed or kBlue
|
2014-12-29 14:09:37 -05:00
|
|
|
* @return The Alliance enum (kRed, kBlue or kInvalid)
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
DriverStation::Alliance DriverStation::GetAlliance() const {
|
|
|
|
|
HALAllianceStationID allianceStationID;
|
|
|
|
|
HALGetAllianceStation(&allianceStationID);
|
|
|
|
|
switch (allianceStationID) {
|
|
|
|
|
case kHALAllianceStationID_red1:
|
|
|
|
|
case kHALAllianceStationID_red2:
|
|
|
|
|
case kHALAllianceStationID_red3:
|
|
|
|
|
return kRed;
|
|
|
|
|
case kHALAllianceStationID_blue1:
|
|
|
|
|
case kHALAllianceStationID_blue2:
|
|
|
|
|
case kHALAllianceStationID_blue3:
|
|
|
|
|
return kBlue;
|
|
|
|
|
default:
|
|
|
|
|
return kInvalid;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the driver station location on the field
|
|
|
|
|
* This could return 1, 2, or 3
|
2014-12-29 14:09:37 -05:00
|
|
|
* @return The location of the driver station (1-3, 0 for invalid)
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
uint32_t DriverStation::GetLocation() const {
|
|
|
|
|
HALAllianceStationID allianceStationID;
|
|
|
|
|
HALGetAllianceStation(&allianceStationID);
|
|
|
|
|
switch (allianceStationID) {
|
|
|
|
|
case kHALAllianceStationID_red1:
|
|
|
|
|
case kHALAllianceStationID_blue1:
|
|
|
|
|
return 1;
|
|
|
|
|
case kHALAllianceStationID_red2:
|
|
|
|
|
case kHALAllianceStationID_blue2:
|
|
|
|
|
return 2;
|
|
|
|
|
case kHALAllianceStationID_red3:
|
|
|
|
|
case kHALAllianceStationID_blue3:
|
|
|
|
|
return 3;
|
|
|
|
|
default:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Wait until a new packet comes from the driver station
|
|
|
|
|
* This blocks on a semaphore, so the waiting is efficient.
|
2015-06-25 15:07:55 -04:00
|
|
|
* This is a good way to delay processing until there is new driver station data
|
|
|
|
|
* to act on
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void DriverStation::WaitForData() {
|
2015-06-25 01:54:20 -07:00
|
|
|
std::unique_lock<priority_mutex> lock(m_waitForDataMutex);
|
|
|
|
|
m_waitForDataCond.wait(lock);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the approximate match time
|
2015-06-25 15:07:55 -04:00
|
|
|
* The FMS does not send an official match time to the robots, but does send an
|
|
|
|
|
* approximate match time.
|
|
|
|
|
* The value will count down the time remaining in the current period (auto or
|
|
|
|
|
* teleop).
|
|
|
|
|
* Warning: This is not an official time (so it cannot be used to dispute ref
|
|
|
|
|
* calls or guarantee that a function
|
2014-12-29 14:09:37 -05:00
|
|
|
* will trigger before the match ends)
|
2015-06-25 15:07:55 -04:00
|
|
|
* The Practice Match function of the DS approximates the behaviour seen on the
|
|
|
|
|
* field.
|
2014-12-29 14:09:37 -05:00
|
|
|
* @return Time remaining in current match period (auto or teleop)
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
double DriverStation::GetMatchTime() const {
|
|
|
|
|
float matchTime;
|
|
|
|
|
HALGetMatchTime(&matchTime);
|
|
|
|
|
return (double)matchTime;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
2014-10-20 17:19:28 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Report an error to the DriverStation messages window.
|
|
|
|
|
* The error is also printed to the program console.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void DriverStation::ReportError(std::string error) {
|
|
|
|
|
std::cout << error << std::endl;
|
|
|
|
|
|
|
|
|
|
HALControlWord controlWord;
|
|
|
|
|
HALGetControlWord(&controlWord);
|
|
|
|
|
if (controlWord.dsAttached) {
|
|
|
|
|
HALSetErrorData(error.c_str(), error.size(), 0);
|
|
|
|
|
}
|
2014-10-20 17:19:28 -04:00
|
|
|
}
|