2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-01-01 01:05:57 -07:00
|
|
|
/* Copyright (c) FIRST 2008-2017. All Rights Reserved. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "DriverStation.h"
|
2016-09-14 20:52:06 -07:00
|
|
|
|
2016-10-09 11:46:01 -07:00
|
|
|
#include <chrono>
|
|
|
|
|
|
2014-06-12 18:07:45 -04:00
|
|
|
#include "AnalogInput.h"
|
2016-11-01 20:12:08 -07:00
|
|
|
#include "HAL/HAL.h"
|
2016-12-21 21:55:31 -08:00
|
|
|
#include "HAL/Power.h"
|
2016-07-14 00:17:29 -07:00
|
|
|
#include "HAL/cpp/Log.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "MotorSafetyHelper.h"
|
2016-05-20 17:30:37 -07:00
|
|
|
#include "Timer.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "Utility.h"
|
|
|
|
|
#include "WPIErrors.h"
|
2016-12-20 22:08:24 -08:00
|
|
|
#include "llvm/SmallString.h"
|
2014-01-06 10:12:21 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2014-12-03 11:02:12 -05:00
|
|
|
const double JOYSTICK_UNPLUGGED_MESSAGE_INTERVAL = 1.0;
|
2014-01-06 10:12:21 -05:00
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
const int DriverStation::kJoystickPorts;
|
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;
|
2016-11-01 20:12:08 -07:00
|
|
|
m_dsThread.join();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a pointer to the singleton DriverStation.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2014-12-29 14:09:37 -05:00
|
|
|
* @return Pointer to the DS instance
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2016-05-20 17:30:37 -07:00
|
|
|
DriverStation& DriverStation::GetInstance() {
|
2016-06-19 00:13:18 -07:00
|
|
|
static DriverStation instance;
|
|
|
|
|
return instance;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-12-11 16:44:55 -05:00
|
|
|
/**
|
2016-07-14 20:50:38 -07:00
|
|
|
* Report an error to the DriverStation messages window.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2016-07-14 20:50:38 -07:00
|
|
|
* The error is also printed to the program console.
|
2014-12-11 16:44:55 -05:00
|
|
|
*/
|
2016-12-20 22:08:24 -08:00
|
|
|
void DriverStation::ReportError(llvm::StringRef error) {
|
|
|
|
|
llvm::SmallString<128> temp;
|
|
|
|
|
HAL_SendError(1, 1, 0, error.c_str(temp), "", "", 1);
|
2014-12-11 16:44:55 -05:00
|
|
|
}
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
2016-07-14 20:50:38 -07:00
|
|
|
* Report a warning to the DriverStation messages window.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2016-07-14 20:50:38 -07:00
|
|
|
* The warning is also printed to the program console.
|
2014-12-29 14:09:37 -05:00
|
|
|
*/
|
2016-12-20 22:08:24 -08:00
|
|
|
void DriverStation::ReportWarning(llvm::StringRef error) {
|
|
|
|
|
llvm::SmallString<128> temp;
|
|
|
|
|
HAL_SendError(0, 1, 0, error.c_str(temp), "", "", 1);
|
2014-12-03 07:42:48 -05:00
|
|
|
}
|
|
|
|
|
|
2016-02-04 22:29:11 -08:00
|
|
|
/**
|
2016-07-14 20:50:38 -07:00
|
|
|
* Report an error to the DriverStation messages window.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2016-07-14 20:50:38 -07:00
|
|
|
* The error is also printed to the program console.
|
2016-02-04 22:29:11 -08:00
|
|
|
*/
|
2016-07-14 20:50:38 -07:00
|
|
|
void DriverStation::ReportError(bool is_error, int32_t code,
|
2016-12-20 22:08:24 -08:00
|
|
|
llvm::StringRef error, llvm::StringRef location,
|
|
|
|
|
llvm::StringRef stack) {
|
|
|
|
|
llvm::SmallString<128> errorTemp;
|
|
|
|
|
llvm::SmallString<128> locationTemp;
|
|
|
|
|
llvm::SmallString<128> stackTemp;
|
|
|
|
|
HAL_SendError(is_error, code, 0, error.c_str(errorTemp),
|
|
|
|
|
location.c_str(locationTemp), stack.c_str(stackTemp), 1);
|
2016-02-04 22:29:11 -08:00
|
|
|
}
|
|
|
|
|
|
2015-01-24 18:02:06 -05:00
|
|
|
/**
|
2016-07-14 20:50:38 -07:00
|
|
|
* Get the value of the axis on a joystick.
|
2014-12-05 20:13:23 -05:00
|
|
|
*
|
2016-07-14 20:50:38 -07:00
|
|
|
* This depends on the mapping of the joystick connected to the specified port.
|
|
|
|
|
*
|
|
|
|
|
* @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.
|
2014-12-05 20:13:23 -05:00
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double DriverStation::GetStickAxis(int stick, int axis) {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (stick >= kJoystickPorts) {
|
|
|
|
|
wpi_setWPIError(BadJoystickIndex);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2017-05-11 21:25:22 -07:00
|
|
|
std::unique_lock<hal::priority_mutex> lock(m_joystickDataMutex);
|
2016-07-14 20:50:38 -07:00
|
|
|
if (axis >= m_joystickAxes[stick].count) {
|
|
|
|
|
// Unlock early so error printing isn't locked.
|
|
|
|
|
m_joystickDataMutex.unlock();
|
|
|
|
|
lock.release();
|
|
|
|
|
if (axis >= HAL_kMaxJoystickAxes)
|
|
|
|
|
wpi_setWPIError(BadJoystickAxis);
|
|
|
|
|
else
|
|
|
|
|
ReportJoystickUnpluggedWarning(
|
|
|
|
|
"Joystick Axis missing, check if all controllers are plugged in");
|
2016-11-20 07:25:03 -08:00
|
|
|
return 0.0;
|
2016-07-14 20:50:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return m_joystickAxes[stick].axes[axis];
|
2014-12-05 20:13:23 -05:00
|
|
|
}
|
|
|
|
|
|
2015-01-24 18:02:06 -05:00
|
|
|
/**
|
2016-07-14 20:50:38 -07:00
|
|
|
* Get the state of a POV on the joystick.
|
2015-06-15 12:34:57 -04:00
|
|
|
*
|
2016-07-14 20:50:38 -07:00
|
|
|
* @return the angle of the POV in degrees, or -1 if the POV is not pressed.
|
2015-06-15 12:34:57 -04:00
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
int DriverStation::GetStickPOV(int stick, int pov) {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (stick >= kJoystickPorts) {
|
|
|
|
|
wpi_setWPIError(BadJoystickIndex);
|
2016-07-14 20:50:38 -07:00
|
|
|
return -1;
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2017-05-11 21:25:22 -07:00
|
|
|
std::unique_lock<hal::priority_mutex> lock(m_joystickDataMutex);
|
2016-07-14 20:50:38 -07:00
|
|
|
if (pov >= m_joystickPOVs[stick].count) {
|
|
|
|
|
// Unlock early so error printing isn't locked.
|
|
|
|
|
lock.unlock();
|
|
|
|
|
if (pov >= HAL_kMaxJoystickPOVs)
|
|
|
|
|
wpi_setWPIError(BadJoystickAxis);
|
|
|
|
|
else
|
|
|
|
|
ReportJoystickUnpluggedWarning(
|
|
|
|
|
"Joystick POV missing, check if all controllers are plugged in");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return m_joystickPOVs[stick].povs[pov];
|
2015-06-15 12:34:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-07-14 20:50:38 -07:00
|
|
|
* The state of the buttons on the joystick.
|
2015-06-15 12:34:57 -04:00
|
|
|
*
|
2016-07-14 20:50:38 -07:00
|
|
|
* @param stick The joystick to read.
|
|
|
|
|
* @return The state of the buttons on the joystick.
|
2015-06-15 12:34:57 -04:00
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
int DriverStation::GetStickButtons(int stick) const {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (stick >= kJoystickPorts) {
|
|
|
|
|
wpi_setWPIError(BadJoystickIndex);
|
2016-07-14 20:50:38 -07:00
|
|
|
return 0;
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_mutex> lock(m_joystickDataMutex);
|
2016-07-14 20:50:38 -07:00
|
|
|
return m_joystickButtons[stick].buttons;
|
2015-06-15 12:34:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-07-14 20:50:38 -07:00
|
|
|
* The state of one joystick button. Button indexes begin at 1.
|
2015-06-15 12:34:57 -04:00
|
|
|
*
|
2016-07-14 20:50:38 -07:00
|
|
|
* @param stick The joystick to read.
|
|
|
|
|
* @param button The button index, beginning at 1.
|
|
|
|
|
* @return The state of the joystick button.
|
2015-06-15 12:34:57 -04:00
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
bool DriverStation::GetStickButton(int stick, int button) {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (stick >= kJoystickPorts) {
|
|
|
|
|
wpi_setWPIError(BadJoystickIndex);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-07-14 20:50:38 -07:00
|
|
|
if (button == 0) {
|
|
|
|
|
ReportJoystickUnpluggedError(
|
|
|
|
|
"ERROR: Button indexes begin at 1 in WPILib for C++ and Java");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-05-11 21:25:22 -07:00
|
|
|
std::unique_lock<hal::priority_mutex> lock(m_joystickDataMutex);
|
2016-07-14 20:50:38 -07:00
|
|
|
if (button > m_joystickButtons[stick].count) {
|
|
|
|
|
// Unlock early so error printing isn't locked.
|
|
|
|
|
lock.unlock();
|
|
|
|
|
ReportJoystickUnpluggedWarning(
|
|
|
|
|
"Joystick Button missing, check if all controllers are "
|
|
|
|
|
"plugged in");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ((0x1 << (button - 1)) & m_joystickButtons[stick].buttons) != 0;
|
2015-06-15 12:34:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-07-14 20:50:38 -07:00
|
|
|
* Returns the number of axes on a given joystick port.
|
2015-06-15 12:34:57 -04:00
|
|
|
*
|
2016-07-14 20:50:38 -07:00
|
|
|
* @param stick The joystick port number
|
|
|
|
|
* @return The number of axes on the indicated joystick
|
2015-06-15 12:34:57 -04:00
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
int DriverStation::GetStickAxisCount(int stick) const {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (stick >= kJoystickPorts) {
|
|
|
|
|
wpi_setWPIError(BadJoystickIndex);
|
2016-07-14 20:50:38 -07:00
|
|
|
return 0;
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_mutex> lock(m_joystickDataMutex);
|
2016-07-14 20:50:38 -07:00
|
|
|
return m_joystickAxes[stick].count;
|
2015-06-15 12:34:57 -04:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
/**
|
2016-05-20 17:30:37 -07: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
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
int DriverStation::GetStickPOVCount(int stick) const {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (stick >= kJoystickPorts) {
|
|
|
|
|
wpi_setWPIError(BadJoystickIndex);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_mutex> lock(m_joystickDataMutex);
|
Implements locking in C++ DriverStation, and adds double buffering to DS Thread (#25)
Currently, about 5ms of every 20ms loop the DS thread would hold
the mutex, while grabbing data. During this time, and call to grab
joystick data would be blocked. This change grabs the joystick data
to a cache, and then grabs the mutex and moves the data references
around. This is much more efficient then the old code, and gives
teams more of their teleop loop time back for use.
Another major change this does is use preallocated arrays when entering
the JNI. Previously every JNI DS call would allocate an entire new array.
With a GC'd language where those arrays go on the heap, thats a problem,
and creates tons of garbage. That garbage is no longer created anymore,
as all arrays and byte buffers sent to JNI in the DS are preallocated.
In addition, GetJoystickName was always returning joystick 0 data, which
this fixes.
2016-05-21 00:41:15 -07:00
|
|
|
return m_joystickPOVs[stick].count;
|
2014-12-05 20:13:23 -05:00
|
|
|
}
|
|
|
|
|
|
2015-01-24 18:02:06 -05:00
|
|
|
/**
|
2016-05-20 17:30:37 -07: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
|
|
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
int DriverStation::GetStickButtonCount(int stick) const {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (stick >= kJoystickPorts) {
|
|
|
|
|
wpi_setWPIError(BadJoystickIndex);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_mutex> lock(m_joystickDataMutex);
|
Implements locking in C++ DriverStation, and adds double buffering to DS Thread (#25)
Currently, about 5ms of every 20ms loop the DS thread would hold
the mutex, while grabbing data. During this time, and call to grab
joystick data would be blocked. This change grabs the joystick data
to a cache, and then grabs the mutex and moves the data references
around. This is much more efficient then the old code, and gives
teams more of their teleop loop time back for use.
Another major change this does is use preallocated arrays when entering
the JNI. Previously every JNI DS call would allocate an entire new array.
With a GC'd language where those arrays go on the heap, thats a problem,
and creates tons of garbage. That garbage is no longer created anymore,
as all arrays and byte buffers sent to JNI in the DS are preallocated.
In addition, GetJoystickName was always returning joystick 0 data, which
this fixes.
2016-05-21 00:41:15 -07:00
|
|
|
return m_joystickButtons[stick].count;
|
2014-12-05 20:13:23 -05:00
|
|
|
}
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
2016-07-14 20:50:38 -07:00
|
|
|
* Returns a boolean indicating if the controller is an xbox controller.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2016-07-14 20:50:38 -07:00
|
|
|
* @param stick The joystick port number
|
|
|
|
|
* @return A boolean that is true if the controller is an xbox controller.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
bool DriverStation::GetJoystickIsXbox(int stick) const {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (stick >= kJoystickPorts) {
|
|
|
|
|
wpi_setWPIError(BadJoystickIndex);
|
2016-07-14 20:50:38 -07:00
|
|
|
return false;
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_mutex> lock(m_joystickDataMutex);
|
2016-07-14 20:50:38 -07:00
|
|
|
return static_cast<bool>(m_joystickDescriptor[stick].isXbox);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-10-17 14:46:25 -04:00
|
|
|
/**
|
2016-07-14 20:50:38 -07:00
|
|
|
* Returns the type of joystick at a given port.
|
2014-10-17 14:46:25 -04:00
|
|
|
*
|
2016-07-14 20:50:38 -07:00
|
|
|
* @param stick The joystick port number
|
|
|
|
|
* @return The HID type of joystick at the given port
|
2014-10-17 14:46:25 -04:00
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
int DriverStation::GetJoystickType(int stick) const {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (stick >= kJoystickPorts) {
|
|
|
|
|
wpi_setWPIError(BadJoystickIndex);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_mutex> lock(m_joystickDataMutex);
|
2016-07-14 20:50:38 -07:00
|
|
|
return static_cast<int>(m_joystickDescriptor[stick].type);
|
2014-10-17 14:46:25 -04:00
|
|
|
}
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
2016-07-14 20:50:38 -07:00
|
|
|
* Returns the name of the joystick at the given port.
|
2014-06-13 17:45:10 -04:00
|
|
|
*
|
2016-07-14 20:50:38 -07:00
|
|
|
* @param stick The joystick port number
|
|
|
|
|
* @return The name of the joystick at the given port
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
std::string DriverStation::GetJoystickName(int stick) const {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (stick >= kJoystickPorts) {
|
|
|
|
|
wpi_setWPIError(BadJoystickIndex);
|
|
|
|
|
}
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_mutex> lock(m_joystickDataMutex);
|
2016-07-14 20:50:38 -07:00
|
|
|
std::string retVal(m_joystickDescriptor[stick].name);
|
|
|
|
|
return retVal;
|
2014-12-18 10:57:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-07-14 20:50:38 -07:00
|
|
|
* Returns the types of Axes on a given joystick port.
|
2014-12-18 10:57:11 -05:00
|
|
|
*
|
2016-07-14 20:50:38 -07:00
|
|
|
* @param stick The joystick port number and the target axis
|
|
|
|
|
* @return What type of axis the axis is reporting to be
|
2014-12-18 10:57:11 -05:00
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
int DriverStation::GetJoystickAxisType(int stick, int axis) const {
|
2015-06-25 15:07:55 -04:00
|
|
|
if (stick >= kJoystickPorts) {
|
|
|
|
|
wpi_setWPIError(BadJoystickIndex);
|
2016-07-14 20:50:38 -07:00
|
|
|
return -1;
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_mutex> lock(m_joystickDataMutex);
|
2016-07-14 20:50:38 -07:00
|
|
|
return m_joystickDescriptor[stick].axisTypes[axis];
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-12-29 14:09:37 -05:00
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Check if the DS has enabled the robot.
|
|
|
|
|
*
|
2014-12-29 14:09:37 -05:00
|
|
|
* @return True if the robot is enabled and the DS is connected
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool DriverStation::IsEnabled() const {
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_ControlWord controlWord;
|
2016-07-15 13:39:26 -07:00
|
|
|
UpdateControlWord(false, controlWord);
|
2015-06-25 15:07:55 -04:00
|
|
|
return controlWord.enabled && controlWord.dsAttached;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-12-29 14:09:37 -05:00
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Check if the robot is disabled.
|
|
|
|
|
*
|
2014-12-29 14:09:37 -05:00
|
|
|
* @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 {
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_ControlWord controlWord;
|
2016-07-15 13:39:26 -07:00
|
|
|
UpdateControlWord(false, controlWord);
|
2015-06-25 15:07:55 -04:00
|
|
|
return !(controlWord.enabled && controlWord.dsAttached);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-12-29 14:09:37 -05:00
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Check if the DS is commanding autonomous mode.
|
|
|
|
|
*
|
2014-12-29 14:09:37 -05:00
|
|
|
* @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 {
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_ControlWord controlWord;
|
2016-07-15 13:39:26 -07:00
|
|
|
UpdateControlWord(false, controlWord);
|
2015-06-25 15:07:55 -04:00
|
|
|
return controlWord.autonomous;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-12-29 14:09:37 -05:00
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Check if the DS is commanding teleop mode.
|
|
|
|
|
*
|
2014-12-29 14:09:37 -05:00
|
|
|
* @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 {
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_ControlWord controlWord;
|
2016-07-15 13:39:26 -07:00
|
|
|
UpdateControlWord(false, controlWord);
|
2015-06-25 15:07:55 -04:00
|
|
|
return !(controlWord.autonomous || controlWord.test);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-12-29 14:09:37 -05:00
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Check if the DS is commanding test mode.
|
|
|
|
|
*
|
2014-12-29 14:09:37 -05:00
|
|
|
* @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 {
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_ControlWord controlWord;
|
2016-07-15 13:39:26 -07:00
|
|
|
UpdateControlWord(false, controlWord);
|
2015-06-25 15:07:55 -04:00
|
|
|
return controlWord.test;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2014-12-29 14:09:37 -05:00
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Check if the DS is attached.
|
|
|
|
|
*
|
2014-12-29 14:09:37 -05:00
|
|
|
* @return True if the DS is connected to the robot
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool DriverStation::IsDSAttached() const {
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_ControlWord controlWord;
|
2016-07-15 13:39:26 -07:00
|
|
|
UpdateControlWord(false, controlWord);
|
2015-06-25 15:07:55 -04:00
|
|
|
return controlWord.dsAttached;
|
2014-11-18 10:56:25 -05:00
|
|
|
}
|
|
|
|
|
|
2016-07-14 20:50:38 -07:00
|
|
|
/**
|
|
|
|
|
* Has a new control packet from the driver station arrived since the last time
|
|
|
|
|
* this function was called?
|
|
|
|
|
*
|
|
|
|
|
* Warning: If you call this function from more than one place at the same time,
|
|
|
|
|
* you will not get the intended behavior.
|
|
|
|
|
*
|
|
|
|
|
* @return True if the control data has been updated since the last call.
|
|
|
|
|
*/
|
2017-05-08 20:21:47 -07:00
|
|
|
bool DriverStation::IsNewControlData() const { return HAL_IsNewControlData(); }
|
2016-07-14 20:50:38 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Is the driver station attached to a Field Management System?
|
|
|
|
|
*
|
|
|
|
|
* @return True if the robot is competing on a field being controlled by a Field
|
|
|
|
|
* Management System
|
|
|
|
|
*/
|
|
|
|
|
bool DriverStation::IsFMSAttached() const {
|
|
|
|
|
HAL_ControlWord controlWord;
|
2016-07-15 13:39:26 -07:00
|
|
|
UpdateControlWord(false, controlWord);
|
2016-07-14 20:50:38 -07:00
|
|
|
return controlWord.fmsAttached;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-29 14:09:37 -05:00
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Check if the FPGA outputs are enabled.
|
|
|
|
|
*
|
|
|
|
|
* The outputs may be disabled if the robot is disabled or e-stopped, the
|
|
|
|
|
* watchdog has expired, or if the roboRIO browns out.
|
|
|
|
|
*
|
2014-12-29 14:09:37 -05:00
|
|
|
* @return True if the FPGA outputs are enabled.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool DriverStation::IsSysActive() const {
|
|
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
bool retVal = HAL_GetSystemActive(&status);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2015-06-25 15:07:55 -04:00
|
|
|
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.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2014-12-29 14:09:37 -05:00
|
|
|
* @return True if the system is browned out
|
|
|
|
|
*/
|
2016-05-25 23:09:24 -07:00
|
|
|
bool DriverStation::IsBrownedOut() const {
|
2015-06-25 15:07:55 -04:00
|
|
|
int32_t status = 0;
|
2016-07-09 00:24:26 -07:00
|
|
|
bool retVal = HAL_GetBrownedOut(&status);
|
|
|
|
|
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
|
2015-06-25 15:07:55 -04:00
|
|
|
return retVal;
|
2014-11-18 10:56:25 -05:00
|
|
|
}
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
/**
|
|
|
|
|
* Return the alliance that the driver station says it is on.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
|
|
|
|
* 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 {
|
2016-07-10 16:24:57 -07:00
|
|
|
int32_t status = 0;
|
|
|
|
|
auto allianceStationID = HAL_GetAllianceStation(&status);
|
2015-06-25 15:07:55 -04:00
|
|
|
switch (allianceStationID) {
|
2016-07-09 00:24:26 -07:00
|
|
|
case HAL_AllianceStationID_kRed1:
|
|
|
|
|
case HAL_AllianceStationID_kRed2:
|
|
|
|
|
case HAL_AllianceStationID_kRed3:
|
2015-06-25 15:07:55 -04:00
|
|
|
return kRed;
|
2016-07-09 00:24:26 -07:00
|
|
|
case HAL_AllianceStationID_kBlue1:
|
|
|
|
|
case HAL_AllianceStationID_kBlue2:
|
|
|
|
|
case HAL_AllianceStationID_kBlue3:
|
2015-06-25 15:07:55 -04:00
|
|
|
return kBlue;
|
|
|
|
|
default:
|
|
|
|
|
return kInvalid;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07: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
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
int DriverStation::GetLocation() const {
|
2016-07-10 16:24:57 -07:00
|
|
|
int32_t status = 0;
|
|
|
|
|
auto allianceStationID = HAL_GetAllianceStation(&status);
|
2015-06-25 15:07:55 -04:00
|
|
|
switch (allianceStationID) {
|
2016-07-09 00:24:26 -07:00
|
|
|
case HAL_AllianceStationID_kRed1:
|
|
|
|
|
case HAL_AllianceStationID_kBlue1:
|
2015-06-25 15:07:55 -04:00
|
|
|
return 1;
|
2016-07-09 00:24:26 -07:00
|
|
|
case HAL_AllianceStationID_kRed2:
|
|
|
|
|
case HAL_AllianceStationID_kBlue2:
|
2015-06-25 15:07:55 -04:00
|
|
|
return 2;
|
2016-07-09 00:24:26 -07:00
|
|
|
case HAL_AllianceStationID_kRed3:
|
|
|
|
|
case HAL_AllianceStationID_kBlue3:
|
2015-06-25 15:07:55 -04:00
|
|
|
return 3;
|
|
|
|
|
default:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07:00
|
|
|
* Wait until a new packet comes from the driver station.
|
|
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* This blocks on a semaphore, so the waiting is efficient.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* This is a good way to delay processing until there is new driver station data
|
2016-05-20 17:30:37 -07:00
|
|
|
* to act on.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2016-10-09 11:46:01 -07:00
|
|
|
void DriverStation::WaitForData() { WaitForData(0); }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Wait until a new packet comes from the driver station, or wait for a timeout.
|
|
|
|
|
*
|
|
|
|
|
* If the timeout is less then or equal to 0, wait indefinitely.
|
|
|
|
|
*
|
|
|
|
|
* Timeout is in milliseconds
|
|
|
|
|
*
|
|
|
|
|
* This blocks on a semaphore, so the waiting is efficient.
|
|
|
|
|
*
|
|
|
|
|
* This is a good way to delay processing until there is new driver station data
|
|
|
|
|
* to act on.
|
|
|
|
|
*
|
|
|
|
|
* @param timeout Timeout time in seconds
|
|
|
|
|
*
|
|
|
|
|
* @return true if new data, otherwise false
|
|
|
|
|
*/
|
|
|
|
|
bool DriverStation::WaitForData(double timeout) {
|
2017-05-08 20:21:47 -07:00
|
|
|
return static_cast<bool>(HAL_WaitForDSDataTimeout(timeout));
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-05-20 17:30:37 -07: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
|
2016-05-20 17:30:37 -07:00
|
|
|
* approximate match time. The value will count down the time remaining in the
|
|
|
|
|
* current period (auto or teleop).
|
|
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* Warning: This is not an official time (so it cannot be used to dispute ref
|
2016-05-20 17:30:37 -07:00
|
|
|
* calls or guarantee that a function 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.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
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 {
|
2016-07-10 16:24:57 -07:00
|
|
|
int32_t status;
|
|
|
|
|
return HAL_GetMatchTime(&status);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
2014-10-20 17:19:28 -04:00
|
|
|
|
|
|
|
|
/**
|
2016-07-14 20:50:38 -07:00
|
|
|
* Read the battery voltage.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2016-07-14 20:50:38 -07:00
|
|
|
* @return The battery voltage in Volts.
|
2014-10-20 17:19:28 -04:00
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double DriverStation::GetBatteryVoltage() const {
|
2016-07-14 20:50:38 -07:00
|
|
|
int32_t status = 0;
|
2016-11-20 07:25:03 -08:00
|
|
|
double voltage = HAL_GetVinVoltage(&status);
|
2016-07-14 20:50:38 -07:00
|
|
|
wpi_setErrorWithContext(status, "getVinVoltage");
|
|
|
|
|
|
|
|
|
|
return voltage;
|
2016-02-04 22:29:11 -08:00
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2016-02-04 22:29:11 -08:00
|
|
|
/**
|
2016-07-14 20:50:38 -07:00
|
|
|
* Copy data from the DS task for the user.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2016-07-14 20:50:38 -07:00
|
|
|
* If no new data exists, it will just be returned, otherwise
|
|
|
|
|
* the data will be copied from the DS polling loop.
|
2016-02-04 22:29:11 -08:00
|
|
|
*/
|
2016-07-14 20:50:38 -07:00
|
|
|
void DriverStation::GetData() {
|
|
|
|
|
// Get the status of all of the joysticks, and save to the cache
|
|
|
|
|
for (uint8_t stick = 0; stick < kJoystickPorts; stick++) {
|
|
|
|
|
HAL_GetJoystickAxes(stick, &m_joystickAxesCache[stick]);
|
|
|
|
|
HAL_GetJoystickPOVs(stick, &m_joystickPOVsCache[stick]);
|
|
|
|
|
HAL_GetJoystickButtons(stick, &m_joystickButtonsCache[stick]);
|
|
|
|
|
HAL_GetJoystickDescriptor(stick, &m_joystickDescriptorCache[stick]);
|
|
|
|
|
}
|
2016-07-15 13:39:26 -07:00
|
|
|
// Force a control word update, to make sure the data is the newest.
|
|
|
|
|
HAL_ControlWord controlWord;
|
|
|
|
|
UpdateControlWord(true, controlWord);
|
2016-07-14 20:50:38 -07:00
|
|
|
// Obtain a write lock on the data, swap the cached data into the
|
|
|
|
|
// main data arrays
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_mutex> lock(m_joystickDataMutex);
|
2016-07-14 20:50:38 -07:00
|
|
|
m_joystickAxes.swap(m_joystickAxesCache);
|
|
|
|
|
m_joystickPOVs.swap(m_joystickPOVsCache);
|
|
|
|
|
m_joystickButtons.swap(m_joystickButtonsCache);
|
|
|
|
|
m_joystickDescriptor.swap(m_joystickDescriptorCache);
|
2016-02-04 22:29:11 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-07-14 20:50:38 -07:00
|
|
|
* DriverStation constructor.
|
2016-05-20 17:30:37 -07:00
|
|
|
*
|
2016-07-14 20:50:38 -07:00
|
|
|
* This is only called once the first time GetInstance() is called
|
2016-02-04 22:29:11 -08:00
|
|
|
*/
|
2016-07-14 20:50:38 -07:00
|
|
|
DriverStation::DriverStation() {
|
|
|
|
|
m_joystickAxes = std::make_unique<HAL_JoystickAxes[]>(kJoystickPorts);
|
|
|
|
|
m_joystickPOVs = std::make_unique<HAL_JoystickPOVs[]>(kJoystickPorts);
|
|
|
|
|
m_joystickButtons = std::make_unique<HAL_JoystickButtons[]>(kJoystickPorts);
|
|
|
|
|
m_joystickDescriptor =
|
|
|
|
|
std::make_unique<HAL_JoystickDescriptor[]>(kJoystickPorts);
|
|
|
|
|
m_joystickAxesCache = std::make_unique<HAL_JoystickAxes[]>(kJoystickPorts);
|
|
|
|
|
m_joystickPOVsCache = std::make_unique<HAL_JoystickPOVs[]>(kJoystickPorts);
|
|
|
|
|
m_joystickButtonsCache =
|
|
|
|
|
std::make_unique<HAL_JoystickButtons[]>(kJoystickPorts);
|
|
|
|
|
m_joystickDescriptorCache =
|
|
|
|
|
std::make_unique<HAL_JoystickDescriptor[]>(kJoystickPorts);
|
|
|
|
|
|
|
|
|
|
// 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';
|
|
|
|
|
|
|
|
|
|
m_joystickAxesCache[i].count = 0;
|
|
|
|
|
m_joystickPOVsCache[i].count = 0;
|
|
|
|
|
m_joystickButtonsCache[i].count = 0;
|
|
|
|
|
m_joystickDescriptorCache[i].isXbox = 0;
|
|
|
|
|
m_joystickDescriptorCache[i].type = -1;
|
|
|
|
|
m_joystickDescriptorCache[i].name[0] = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-01 20:12:08 -07:00
|
|
|
m_dsThread = std::thread(&DriverStation::Run, this);
|
2016-07-14 20:50:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reports errors related to unplugged joysticks
|
|
|
|
|
* Throttles the errors so that they don't overwhelm the DS
|
|
|
|
|
*/
|
2016-11-26 21:22:39 -08:00
|
|
|
void DriverStation::ReportJoystickUnpluggedError(llvm::StringRef message) {
|
2016-07-14 20:50:38 -07:00
|
|
|
double currentTime = Timer::GetFPGATimestamp();
|
|
|
|
|
if (currentTime > m_nextMessageTime) {
|
|
|
|
|
ReportError(message);
|
|
|
|
|
m_nextMessageTime = currentTime + JOYSTICK_UNPLUGGED_MESSAGE_INTERVAL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reports errors related to unplugged joysticks.
|
|
|
|
|
*
|
|
|
|
|
* Throttles the errors so that they don't overwhelm the DS.
|
|
|
|
|
*/
|
2016-11-26 21:22:39 -08:00
|
|
|
void DriverStation::ReportJoystickUnpluggedWarning(llvm::StringRef message) {
|
2016-07-14 20:50:38 -07:00
|
|
|
double currentTime = Timer::GetFPGATimestamp();
|
|
|
|
|
if (currentTime > m_nextMessageTime) {
|
|
|
|
|
ReportWarning(message);
|
|
|
|
|
m_nextMessageTime = currentTime + JOYSTICK_UNPLUGGED_MESSAGE_INTERVAL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DriverStation::Run() {
|
|
|
|
|
m_isRunning = true;
|
|
|
|
|
int period = 0;
|
|
|
|
|
while (m_isRunning) {
|
|
|
|
|
HAL_WaitForDSData();
|
|
|
|
|
GetData();
|
|
|
|
|
|
|
|
|
|
if (++period >= 4) {
|
|
|
|
|
MotorSafetyHelper::CheckMotors();
|
|
|
|
|
period = 0;
|
|
|
|
|
}
|
|
|
|
|
if (m_userInDisabled) HAL_ObserveUserProgramDisabled();
|
|
|
|
|
if (m_userInAutonomous) HAL_ObserveUserProgramAutonomous();
|
|
|
|
|
if (m_userInTeleop) HAL_ObserveUserProgramTeleop();
|
|
|
|
|
if (m_userInTest) HAL_ObserveUserProgramTest();
|
|
|
|
|
}
|
2014-10-20 17:19:28 -04:00
|
|
|
}
|
2016-07-15 13:39:26 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets ControlWord data from the cache. If 50ms has passed, or the force
|
|
|
|
|
* parameter is set, the cached data is updated. Otherwise the data is just
|
|
|
|
|
* copied from the cache.
|
|
|
|
|
*
|
|
|
|
|
* @param force True to force an update to the cache, otherwise update if 50ms
|
|
|
|
|
* have passed.
|
|
|
|
|
* @param controlWord Structure to put the return control word data into.
|
|
|
|
|
*/
|
|
|
|
|
void DriverStation::UpdateControlWord(bool force,
|
|
|
|
|
HAL_ControlWord& controlWord) const {
|
|
|
|
|
auto now = std::chrono::steady_clock::now();
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_mutex> lock(m_controlWordMutex);
|
2016-07-15 13:39:26 -07:00
|
|
|
// Update every 50 ms or on force.
|
|
|
|
|
if ((now - m_lastControlWordUpdate > std::chrono::milliseconds(50)) ||
|
|
|
|
|
force) {
|
|
|
|
|
HAL_GetControlWord(&m_controlWordCache);
|
|
|
|
|
m_lastControlWordUpdate = now;
|
|
|
|
|
}
|
|
|
|
|
controlWord = m_controlWordCache;
|
|
|
|
|
}
|