2016-05-25 20:23:37 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-08-18 21:35:53 -07:00
|
|
|
/* Copyright (c) FIRST 2017. All Rights Reserved. */
|
2016-05-25 20:23:37 -07:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2016-12-02 00:32:01 -08:00
|
|
|
#include "HAL/DriverStation.h"
|
2017-08-18 21:35:53 -07:00
|
|
|
|
2017-08-19 23:12:24 -07:00
|
|
|
#ifdef __APPLE__
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-08-19 22:56:39 -07:00
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
2016-07-17 19:42:33 -07:00
|
|
|
#include "HAL/cpp/priority_condition_variable.h"
|
|
|
|
|
#include "HAL/cpp/priority_mutex.h"
|
2017-08-18 21:35:53 -07:00
|
|
|
#include "MockData/DriverStationDataInternal.h"
|
2016-05-21 01:42:16 -07:00
|
|
|
|
2017-05-11 21:25:22 -07:00
|
|
|
static hal::priority_mutex msgMutex;
|
|
|
|
|
static hal::priority_condition_variable newDSDataAvailableCond;
|
|
|
|
|
static hal::priority_mutex newDSDataAvailableMutex;
|
2017-05-08 20:21:47 -07:00
|
|
|
static int newDSDataAvailableCounter{0};
|
2016-07-17 19:42:33 -07:00
|
|
|
|
2017-08-18 21:35:53 -07:00
|
|
|
using namespace hal;
|
|
|
|
|
|
2015-11-26 00:08:32 -08:00
|
|
|
extern "C" {
|
2016-07-17 19:42:33 -07:00
|
|
|
int32_t HAL_SetErrorData(const char* errors, int32_t errorsLength,
|
2016-08-12 13:45:28 -07:00
|
|
|
int32_t waitMs) {
|
2017-08-18 21:35:53 -07:00
|
|
|
return 0;
|
2016-07-17 19:42:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t HAL_SendError(HAL_Bool isError, int32_t errorCode, HAL_Bool isLVCode,
|
|
|
|
|
const char* details, const char* location,
|
|
|
|
|
const char* callStack, HAL_Bool printMsg) {
|
|
|
|
|
// Avoid flooding console by keeping track of previous 5 error
|
|
|
|
|
// messages and only printing again if they're longer than 1 second old.
|
|
|
|
|
static constexpr int KEEP_MSGS = 5;
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_mutex> lock(msgMutex);
|
2016-08-12 13:45:28 -07:00
|
|
|
static std::string prevMsg[KEEP_MSGS];
|
2016-12-02 00:32:01 -08:00
|
|
|
static std::chrono::time_point<std::chrono::steady_clock>
|
|
|
|
|
prevMsgTime[KEEP_MSGS];
|
|
|
|
|
static bool initialized = false;
|
|
|
|
|
if (!initialized) {
|
|
|
|
|
for (int i = 0; i < KEEP_MSGS; i++) {
|
|
|
|
|
prevMsgTime[i] =
|
|
|
|
|
std::chrono::steady_clock::now() - std::chrono::seconds(2);
|
|
|
|
|
}
|
|
|
|
|
initialized = true;
|
|
|
|
|
}
|
2016-07-17 19:42:33 -07:00
|
|
|
|
2016-12-02 00:32:01 -08:00
|
|
|
auto curTime = std::chrono::steady_clock::now();
|
2016-07-17 19:42:33 -07:00
|
|
|
int i;
|
|
|
|
|
for (i = 0; i < KEEP_MSGS; ++i) {
|
2016-08-12 13:45:28 -07:00
|
|
|
if (prevMsg[i] == details) break;
|
2016-07-17 19:42:33 -07:00
|
|
|
}
|
|
|
|
|
int retval = 0;
|
2016-12-02 00:32:01 -08:00
|
|
|
if (i == KEEP_MSGS || (curTime - prevMsgTime[i]) >= std::chrono::seconds(1)) {
|
2017-08-18 21:35:53 -07:00
|
|
|
printMsg = true;
|
2016-07-17 19:42:33 -07:00
|
|
|
if (printMsg) {
|
|
|
|
|
if (location && location[0] != '\0') {
|
2017-08-18 21:35:53 -07:00
|
|
|
std::fprintf(stderr, "%s at %s: ", isError ? "Error" : "Warning",
|
|
|
|
|
location);
|
2016-07-17 19:42:33 -07:00
|
|
|
}
|
2017-08-18 21:35:53 -07:00
|
|
|
std::fprintf(stderr, "%s\n", details);
|
2016-07-17 19:42:33 -07:00
|
|
|
if (callStack && callStack[0] != '\0') {
|
2017-08-18 21:35:53 -07:00
|
|
|
std::fprintf(stderr, "%s\n", callStack);
|
2016-07-17 19:42:33 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (i == KEEP_MSGS) {
|
|
|
|
|
// replace the oldest one
|
|
|
|
|
i = 0;
|
2016-12-02 00:32:01 -08:00
|
|
|
auto first = prevMsgTime[0];
|
2016-07-17 19:42:33 -07:00
|
|
|
for (int j = 1; j < KEEP_MSGS; ++j) {
|
2016-08-12 13:45:28 -07:00
|
|
|
if (prevMsgTime[j] < first) {
|
|
|
|
|
first = prevMsgTime[j];
|
2016-07-17 19:42:33 -07:00
|
|
|
i = j;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-08-12 13:45:28 -07:00
|
|
|
prevMsg[i] = details;
|
2016-07-17 19:42:33 -07:00
|
|
|
}
|
2016-08-12 13:45:28 -07:00
|
|
|
prevMsgTime[i] = curTime;
|
2016-07-17 19:42:33 -07:00
|
|
|
}
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
2015-11-26 00:08:32 -08:00
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t HAL_GetControlWord(HAL_ControlWord* controlWord) {
|
2017-08-18 21:35:53 -07:00
|
|
|
controlWord->enabled = SimDriverStationData.GetEnabled();
|
|
|
|
|
controlWord->autonomous = SimDriverStationData.GetAutonomous();
|
|
|
|
|
controlWord->test = SimDriverStationData.GetTest();
|
|
|
|
|
controlWord->eStop = SimDriverStationData.GetEStop();
|
|
|
|
|
controlWord->fmsAttached = SimDriverStationData.GetFmsAttached();
|
|
|
|
|
controlWord->dsAttached = SimDriverStationData.GetDsAttached();
|
|
|
|
|
return 0;
|
2015-08-19 11:12:54 -04:00
|
|
|
}
|
|
|
|
|
|
2016-07-10 16:24:57 -07:00
|
|
|
HAL_AllianceStationID HAL_GetAllianceStation(int32_t* status) {
|
2017-08-18 21:35:53 -07:00
|
|
|
*status = 0;
|
|
|
|
|
return SimDriverStationData.GetAllianceStationId();
|
2015-08-19 11:12:54 -04:00
|
|
|
}
|
|
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t HAL_GetJoystickAxes(int32_t joystickNum, HAL_JoystickAxes* axes) {
|
2017-08-18 21:35:53 -07:00
|
|
|
return 0;
|
2015-08-19 11:12:54 -04:00
|
|
|
}
|
|
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t HAL_GetJoystickPOVs(int32_t joystickNum, HAL_JoystickPOVs* povs) {
|
2017-08-18 21:35:53 -07:00
|
|
|
return 0;
|
2015-08-19 11:12:54 -04:00
|
|
|
}
|
|
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t HAL_GetJoystickButtons(int32_t joystickNum,
|
|
|
|
|
HAL_JoystickButtons* buttons) {
|
2017-08-18 21:35:53 -07:00
|
|
|
return 0;
|
2015-08-19 11:12:54 -04:00
|
|
|
}
|
2016-01-15 13:13:02 -05:00
|
|
|
/**
|
|
|
|
|
* Retrieve the Joystick Descriptor for particular slot
|
2016-05-20 17:30:37 -07:00
|
|
|
* @param desc [out] descriptor (data transfer object) to fill in. desc is
|
|
|
|
|
* filled in regardless of success. In other words, if descriptor is not
|
|
|
|
|
* available, desc is filled in with default values matching the init-values in
|
|
|
|
|
* Java and C++ Driverstation for when caller requests a too-large joystick
|
|
|
|
|
* index.
|
2016-01-15 13:13:02 -05:00
|
|
|
*
|
2016-05-20 17:30:37 -07:00
|
|
|
* @return error code reported from Network Comm back-end. Zero is good,
|
|
|
|
|
* nonzero is bad.
|
2016-01-15 13:13:02 -05:00
|
|
|
*/
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t HAL_GetJoystickDescriptor(int32_t joystickNum,
|
|
|
|
|
HAL_JoystickDescriptor* desc) {
|
2017-08-18 21:35:53 -07:00
|
|
|
return 0;
|
2016-05-20 17:30:37 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-18 21:35:53 -07:00
|
|
|
HAL_Bool HAL_GetJoystickIsXbox(int32_t joystickNum) { return false; }
|
2016-05-20 17:30:37 -07:00
|
|
|
|
2017-08-18 21:35:53 -07:00
|
|
|
int32_t HAL_GetJoystickType(int32_t joystickNum) { return 0; }
|
2016-05-20 17:30:37 -07:00
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
char* HAL_GetJoystickName(int32_t joystickNum) {
|
2017-08-18 21:35:53 -07:00
|
|
|
char* name = static_cast<char*>(std::malloc(1));
|
|
|
|
|
name[0] = '\0';
|
|
|
|
|
return name;
|
2016-05-20 17:30:37 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-30 16:11:16 -07:00
|
|
|
void HAL_FreeJoystickName(char* name) { std::free(name); }
|
|
|
|
|
|
2017-08-18 21:35:53 -07:00
|
|
|
int32_t HAL_GetJoystickAxisType(int32_t joystickNum, int32_t axis) { return 0; }
|
2016-05-20 17:30:37 -07:00
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t HAL_SetJoystickOutputs(int32_t joystickNum, int64_t outputs,
|
|
|
|
|
int32_t leftRumble, int32_t rightRumble) {
|
2017-08-18 21:35:53 -07:00
|
|
|
return 0;
|
2016-05-20 17:30:37 -07:00
|
|
|
}
|
|
|
|
|
|
2016-07-13 19:25:57 -07:00
|
|
|
double HAL_GetMatchTime(int32_t* status) {
|
2017-08-18 21:35:53 -07:00
|
|
|
return SimDriverStationData.GetMatchTime();
|
2016-05-20 17:30:37 -07:00
|
|
|
}
|
|
|
|
|
|
2016-07-10 16:24:57 -07:00
|
|
|
void HAL_ObserveUserProgramStarting(void) {
|
2017-08-18 21:35:53 -07:00
|
|
|
// TODO
|
2016-05-20 17:30:37 -07:00
|
|
|
}
|
|
|
|
|
|
2016-07-10 16:24:57 -07:00
|
|
|
void HAL_ObserveUserProgramDisabled(void) {
|
2017-08-18 21:35:53 -07:00
|
|
|
// TODO
|
2016-05-20 17:30:37 -07:00
|
|
|
}
|
|
|
|
|
|
2016-07-10 16:24:57 -07:00
|
|
|
void HAL_ObserveUserProgramAutonomous(void) {
|
2017-08-18 21:35:53 -07:00
|
|
|
// TODO
|
2016-05-20 17:30:37 -07:00
|
|
|
}
|
|
|
|
|
|
2016-07-10 16:24:57 -07:00
|
|
|
void HAL_ObserveUserProgramTeleop(void) {
|
2017-08-18 21:35:53 -07:00
|
|
|
// TODO
|
2016-05-20 17:30:37 -07:00
|
|
|
}
|
|
|
|
|
|
2016-07-10 16:24:57 -07:00
|
|
|
void HAL_ObserveUserProgramTest(void) {
|
2017-08-18 21:35:53 -07:00
|
|
|
// TODO
|
2015-08-19 11:12:54 -04:00
|
|
|
}
|
2015-11-26 00:08:32 -08:00
|
|
|
|
2017-08-19 23:12:24 -07:00
|
|
|
#ifdef __APPLE__
|
|
|
|
|
static pthread_key_t lastCountKey;
|
|
|
|
|
static pthread_once_t lastCountKeyOnce = PTHREAD_ONCE_INIT;
|
|
|
|
|
|
|
|
|
|
static void InitLastCountKey() {
|
|
|
|
|
pthread_key_create(&lastCountKey, std::free);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-05-08 20:21:47 -07:00
|
|
|
bool HAL_IsNewControlData(void) {
|
|
|
|
|
// There is a rollover error condition here. At Packet# = n * (uintmax), this
|
|
|
|
|
// will return false when instead it should return true. However, this at a
|
|
|
|
|
// 20ms rate occurs once every 2.7 years of DS connected runtime, so not
|
|
|
|
|
// worth the cycles to check.
|
2017-08-19 23:12:24 -07:00
|
|
|
#ifdef __APPLE__
|
|
|
|
|
pthread_once(&lastCountKeyOnce, InitLastCountKey);
|
|
|
|
|
int* lastCountPtr = static_cast<int*>(pthread_getspecific(lastCountKey));
|
|
|
|
|
if (!lastCountPtr) {
|
|
|
|
|
lastCountPtr = static_cast<int*>(std::malloc(sizeof(int)));
|
|
|
|
|
*lastCountPtr = -1;
|
|
|
|
|
pthread_setspecific(lastCountKey, lastCountPtr);
|
|
|
|
|
}
|
|
|
|
|
int& lastCount = *lastCountPtr;
|
|
|
|
|
#else
|
2017-05-08 20:21:47 -07:00
|
|
|
thread_local int lastCount{-1};
|
2017-08-19 23:12:24 -07:00
|
|
|
#endif
|
2017-05-08 20:21:47 -07:00
|
|
|
int currentCount = 0;
|
|
|
|
|
{
|
2017-05-11 21:25:22 -07:00
|
|
|
std::unique_lock<hal::priority_mutex> lock(newDSDataAvailableMutex);
|
2017-05-08 20:21:47 -07:00
|
|
|
currentCount = newDSDataAvailableCounter;
|
|
|
|
|
}
|
|
|
|
|
if (lastCount == currentCount) return false;
|
|
|
|
|
lastCount = currentCount;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-17 19:42:33 -07:00
|
|
|
/**
|
|
|
|
|
* Waits for the newest DS packet to arrive. Note that this is a blocking call.
|
|
|
|
|
*/
|
2017-05-08 20:21:47 -07:00
|
|
|
void HAL_WaitForDSData(void) { HAL_WaitForDSDataTimeout(0); }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Waits for the newest DS packet to arrive. If timeout is <= 0, this will wait
|
|
|
|
|
* forever. Otherwise, it will wait until either a new packet, or the timeout
|
|
|
|
|
* time has passed. Returns true on new data, false on timeout.
|
|
|
|
|
*/
|
|
|
|
|
HAL_Bool HAL_WaitForDSDataTimeout(double timeout) {
|
|
|
|
|
auto timeoutTime =
|
|
|
|
|
std::chrono::steady_clock::now() + std::chrono::duration<double>(timeout);
|
|
|
|
|
|
2017-05-11 21:25:22 -07:00
|
|
|
std::unique_lock<hal::priority_mutex> lock(newDSDataAvailableMutex);
|
2017-05-08 20:21:47 -07:00
|
|
|
int currentCount = newDSDataAvailableCounter;
|
|
|
|
|
while (newDSDataAvailableCounter == currentCount) {
|
|
|
|
|
if (timeout > 0) {
|
|
|
|
|
auto timedOut = newDSDataAvailableCond.wait_until(lock, timeoutTime);
|
|
|
|
|
if (timedOut == std::cv_status::timeout) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
newDSDataAvailableCond.wait(lock);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2016-07-17 19:42:33 -07:00
|
|
|
}
|
|
|
|
|
|
2017-05-08 20:21:47 -07:00
|
|
|
// Constant number to be used for our occur handle
|
|
|
|
|
constexpr int32_t refNumber = 42;
|
|
|
|
|
|
|
|
|
|
static int32_t newDataOccur(uint32_t refNum) {
|
|
|
|
|
// Since we could get other values, require our specific handle
|
|
|
|
|
// to signal our threads
|
|
|
|
|
if (refNum != refNumber) return 0;
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_mutex> lock(newDSDataAvailableMutex);
|
2017-05-08 20:21:47 -07:00
|
|
|
// Nofify all threads
|
|
|
|
|
newDSDataAvailableCounter++;
|
|
|
|
|
newDSDataAvailableCond.notify_all();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Call this to initialize the driver station communication. This will properly
|
|
|
|
|
* handle multiple calls. However note that this CANNOT be called from a library
|
|
|
|
|
* that interfaces with LabVIEW.
|
|
|
|
|
*/
|
2016-07-17 19:42:33 -07:00
|
|
|
void HAL_InitializeDriverStation(void) {
|
2017-05-08 20:21:47 -07:00
|
|
|
static std::atomic_bool initialized{false};
|
2017-05-11 21:25:22 -07:00
|
|
|
static hal::priority_mutex initializeMutex;
|
2017-05-08 20:21:47 -07:00
|
|
|
// Initial check, as if it's true initialization has finished
|
|
|
|
|
if (initialized) return;
|
|
|
|
|
|
2017-05-11 21:25:22 -07:00
|
|
|
std::lock_guard<hal::priority_mutex> lock(initializeMutex);
|
2017-05-08 20:21:47 -07:00
|
|
|
// Second check in case another thread was waiting
|
|
|
|
|
if (initialized) return;
|
|
|
|
|
|
2017-08-18 21:35:53 -07:00
|
|
|
SimDriverStationData.ResetData();
|
2017-05-08 20:21:47 -07:00
|
|
|
|
|
|
|
|
initialized = true;
|
2016-07-17 19:42:33 -07:00
|
|
|
}
|
|
|
|
|
|
2017-05-08 20:21:47 -07:00
|
|
|
/*
|
|
|
|
|
* Releases the DS Mutex to allow proper shutdown of any threads that are
|
|
|
|
|
* waiting on it.
|
|
|
|
|
*/
|
|
|
|
|
void HAL_ReleaseDSMutex(void) { newDataOccur(refNumber); }
|
|
|
|
|
|
2015-11-26 00:08:32 -08:00
|
|
|
} // extern "C"
|