Files
allwpilib/wpilibc/wpilibC++Devices/src/DriverStation.cpp

406 lines
10 KiB
C++
Raw Normal View History

/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. All Rights Reserved. */
/* 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"
#include "AnalogInput.h"
#include "HAL/cpp/Synchronized.hpp"
#include "Timer.h"
#include "NetworkCommunication/FRCComm.h"
#include "NetworkCommunication/UsageReporting.h"
#include "MotorSafetyHelper.h"
#include "Utility.h"
#include "WPIErrors.h"
#include <string.h>
#include "Log.hpp"
// set the logging level
TLogLevel dsLogLevel = logDEBUG;
const double JOYSTICK_UNPLUGGED_MESSAGE_INTERVAL = 1.0;
#define DS_LOG(level) \
if (level > dsLogLevel) ; \
else Log().Get(level)
const uint32_t DriverStation::kJoystickPorts;
DriverStation* DriverStation::m_instance = NULL;
/**
* DriverStation contructor.
*
* This is only called once the first time GetInstance() is called
*/
DriverStation::DriverStation()
: m_task ("DriverStation", (FUNCPTR)DriverStation::InitTask)
, m_newControlData(0)
, m_packetDataAvailableMultiWait(0)
, m_waitForDataSem(0)
, m_userInDisabled(false)
, m_userInAutonomous(false)
, m_userInTeleop(false)
, m_userInTest(false)
, m_nextMessageTime(0)
{
// Create a new semaphore
m_packetDataAvailableMultiWait = initializeMultiWait();
m_newControlData = initializeSemaphore(SEMAPHORE_EMPTY);
m_waitForDataSem = initializeMultiWait();
m_waitForDataMutex = initializeMutexNormal();
m_packetDataAvailableMultiWait = initializeMultiWait();
m_packetDataAvailableMutex = initializeMutexNormal();
// Register that semaphore with the network communications task.
// It will signal when new packet data is available.
HALSetNewDataSem(m_packetDataAvailableMultiWait);
AddToSingletonList();
if (!m_task.Start((int32_t)this))
{
wpi_setWPIError(DriverStationTaskError);
}
}
DriverStation::~DriverStation()
{
m_task.Stop();
m_instance = NULL;
deleteMultiWait(m_waitForDataSem);
// Unregister our semaphore.
HALSetNewDataSem(0);
deleteMultiWait(m_packetDataAvailableMultiWait);
deleteMutex(m_packetDataAvailableMutex);
deleteMutex(m_waitForDataMutex);
}
void DriverStation::InitTask(DriverStation *ds)
{
ds->Run();
}
void DriverStation::Run()
{
HALControlWord controlWord;
int period = 0;
while (true)
{
// need to get the controlWord to keep the motors enabled
HALGetControlWord(&controlWord);
takeMultiWait(m_packetDataAvailableMultiWait, m_packetDataAvailableMutex, 0);
giveMultiWait(m_waitForDataSem);
giveSemaphore(m_newControlData);
if (++period >= 4)
{
MotorSafetyHelper::CheckMotors();
period = 0;
}
if (m_userInDisabled)
HALNetworkCommunicationObserveUserProgramDisabled();
if (m_userInAutonomous)
HALNetworkCommunicationObserveUserProgramAutonomous();
if (m_userInTeleop)
HALNetworkCommunicationObserveUserProgramTeleop();
if (m_userInTest)
HALNetworkCommunicationObserveUserProgramTest();
}
}
/**
* Return a pointer to the singleton DriverStation.
*/
DriverStation* DriverStation::GetInstance()
{
if (m_instance == NULL)
{
m_instance = new DriverStation();
}
return m_instance;
}
/**
* Read the battery voltage.
*
* @return The battery voltage.
*/
float DriverStation::GetBatteryVoltage()
{
int32_t status = 0;
float voltage = getVinVoltage(&status);
wpi_setErrorWithContext(status, "getVinVoltage");
return voltage;
}
void DriverStation::ReportJoystickUnpluggedError(std::string message) {
double currentTime = Timer::GetFPGATimestamp();
if (currentTime > m_nextMessageTime) {
ReportError(message);
m_nextMessageTime = currentTime + JOYSTICK_UNPLUGGED_MESSAGE_INTERVAL;
}
}
/**
* Get the value of the axis on a joystick.
* 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.
*/
float DriverStation::GetStickAxis(uint32_t stick, uint32_t axis)
{
if (stick >= kJoystickPorts)
{
wpi_setWPIError(BadJoystickIndex);
return 0;
}
HALJoystickAxes joystickAxes;
HALGetJoystickAxes(stick, &joystickAxes);
if (axis >= joystickAxes.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 = joystickAxes.axes[axis];
if(value < 0)
{
return value / 128.0f;
}
else
{
return value / 127.0f;
}
}
/**
* 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) {
if (stick >= kJoystickPorts)
{
wpi_setWPIError(BadJoystickIndex);
return 0;
}
HALJoystickPOVs joystickPOVs;
HALGetJoystickPOVs(stick, &joystickPOVs);
if (pov >= joystickPOVs.count)
{
if (pov >= kMaxJoystickPOVs)
wpi_setWPIError(BadJoystickAxis);
else
ReportJoystickUnpluggedError("WARNING: Joystick POV missing, check if all controllers are plugged in\n");
return 0;
}
return joystickPOVs.povs[pov];
}
/**
* The state of the buttons on the joystick.
*
* @param stick The joystick to read.
* @return The state of the buttons on the joystick.
*/
bool DriverStation::GetStickButton(uint32_t stick, uint8_t button)
{
if (stick >= kJoystickPorts)
{
wpi_setWPIError(BadJoystickIndex);
return 0;
}
HALJoystickButtons joystickButtons;
HALGetJoystickButtons(stick, &joystickButtons);
if(button > joystickButtons.count)
{
ReportJoystickUnpluggedError("WARNING: Joystick Button missing, check if all controllers are plugged in\n");
return false;
}
return ((0x1 << (button-1)) & joystickButtons.buttons) !=0;
}
bool DriverStation::IsEnabled()
{
HALControlWord controlWord;
memset(&controlWord, 0, sizeof(controlWord));
HALGetControlWord(&controlWord);
return controlWord.enabled && controlWord.dsAttached;
}
bool DriverStation::IsDisabled()
{
HALControlWord controlWord;
memset(&controlWord, 0, sizeof(controlWord));
HALGetControlWord(&controlWord);
return !(controlWord.enabled && controlWord.dsAttached);
}
bool DriverStation::IsAutonomous()
{
HALControlWord controlWord;
memset(&controlWord, 0, sizeof(controlWord));
HALGetControlWord(&controlWord);
return controlWord.autonomous;
}
bool DriverStation::IsOperatorControl()
{
HALControlWord controlWord;
memset(&controlWord, 0, sizeof(controlWord));
HALGetControlWord(&controlWord);
return !(controlWord.autonomous || controlWord.test);
}
bool DriverStation::IsTest()
{
HALControlWord controlWord;
HALGetControlWord(&controlWord);
return controlWord.test;
}
bool DriverStation::IsDSAttached()
{
HALControlWord controlWord;
memset(&controlWord, 0, sizeof(controlWord));
HALGetControlWord(&controlWord);
return controlWord.dsAttached;
}
bool DriverStation::IsSysActive()
{
int32_t status = 0;
bool retVal = HALGetSystemActive(&status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
return retVal;
}
bool DriverStation::IsSysBrownedOut()
{
int32_t status = 0;
bool retVal = HALGetBrownedOut(&status);
wpi_setErrorWithContext(status, getHALErrorMessage(status));
return retVal;
}
/**
* 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 get the intended behavior
* @return True if the control data has been updated since the last call.
*/
bool DriverStation::IsNewControlData()
{
return tryTakeSemaphore(m_newControlData) == 0;
}
/**
* Is the driver station attached to a Field Management System?
* Note: This does not work with the Blue DS.
* @return True if the robot is competing on a field being controlled by a Field Management System
*/
bool DriverStation::IsFMSAttached()
{
HALControlWord controlWord;
HALGetControlWord(&controlWord);
return controlWord.fmsAttached;
}
/**
* Return the alliance that the driver station says it is on.
* This could return kRed or kBlue
* @return The Alliance enum
*/
DriverStation::Alliance DriverStation::GetAlliance()
{
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;
}
}
/**
* Return the driver station location on the field
* This could return 1, 2, or 3
* @return The location of the driver station
*/
uint32_t DriverStation::GetLocation()
{
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;
}
}
/**
* Wait until a new packet comes from the driver station
* 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
*/
void DriverStation::WaitForData()
{
takeMultiWait(m_waitForDataSem, m_waitForDataMutex, SEMAPHORE_WAIT_FOREVER);
}
/**
* Return the approximate match time
* The FMS does not currently send the official match time to the robots
* This returns the time since the enable signal sent from the Driver Station
* At the beginning of autonomous, the time is reset to 0.0 seconds
* At the beginning of teleop, the time is reset to +15.0 seconds
* If the robot is disabled, this returns 0.0 seconds
* Warning: This is not an official time (so it cannot be used to argue with referees)
* @return Match time in seconds since the beginning of autonomous
*/
double DriverStation::GetMatchTime()
{
float matchTime;
HALGetMatchTime(&matchTime);
return (double)matchTime;
}
Pass errors to DS in C++ and Java Squashed commit of the following: commit f317b3522e312cf7e7bb9eb0494f2f96a7f6363c Author: Kevin O'Connor <koconnor@usfirst.org> Date: Mon Oct 20 17:15:46 2014 -0400 Send unhandled exceptions back to the DS. Change-Id: I0e658fdb6d43593ee20457f20f71f4f4cd2d21c3 commit f834ef8c791945697ad483c27b4167eb917ac242 Author: Kevin O'Connor <koconnor@usfirst.org> Date: Mon Oct 20 16:05:24 2014 -0400 Add StackTrace to Java errors Change-Id: I83b162afcc5f294703705770fbcd8623b0895539 commit 02e040b0c79067ce046ada29e26004e0460fceb0 Author: Kevin O'Connor <koconnor@usfirst.org> Date: Mon Oct 20 15:07:44 2014 -0400 HAL Errors to DS in Java Change-Id: I5fb51e4066bbc26ea59ca513c03c5ec5ace98831 commit 03775ddc42b129c27fdf403f17f0796009311c3c Author: Kevin O'Connor <koconnor@usfirst.org> Date: Mon Oct 20 13:38:18 2014 -0400 Update AnalogInput to report errors for getting and setting sample rate Change-Id: I00eb78f52fc5b17a60bc84456f0ec9842cc40ef7 commit 4c10cb79612ae81e3cbb6bd4d6da8cf3b8955821 Author: Kevin O'Connor <koconnor@usfirst.org> Date: Mon Oct 20 11:46:03 2014 -0400 Define errors in HAL Change-Id: I96595472e42ba61f0f3d0da17caf01a748d0422a commit 56cb5dcd93e5e849a016f63ac9d0dc245a23eb2b Author: Kevin O'Connor <koconnor@usfirst.org> Date: Fri Oct 17 10:59:29 2014 -0400 Throttle errors (1 report per second per error code) and fix issue with GetTime conflicting with GetTime from Timer.h/Timer.cpp Change-Id: Ibe4dc2e400fc4671b240b876a46959256ea65ad7 commit 71c78826e548682ecd0c1548255f8a6552cece32 Author: Kevin O'Connor <koconnor@usfirst.org> Date: Thu Oct 16 16:41:04 2014 -0400 Feed errors to DS from C++ Change-Id: I009a7798499fd93e9fdd976ff00aa74c0bd094ae commit 81030c6cee7f18a5ddf0e95c4e402a6cf7b5de6c Author: Kevin O'Connor <koconnor@usfirst.org> Date: Thu Oct 16 16:40:50 2014 -0400 Don't try to de-mangle lines without any symbols in them Change-Id: Icea02494b68f2ec9116d6cbf20a35a3a132234f8 Change-Id: If7717025b03914183736ccd95da5c9d49819a6f3
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.
*/
void DriverStation::ReportError(std::string error)
{
std::cout << error << std::endl;
HALSetErrorData(error.c_str(), error.size(), 0);
}