2016-05-25 20:23:37 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2018-01-02 09:20:21 -08:00
|
|
|
/* Copyright (c) 2017-2018 FIRST. 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>
|
2017-11-01 21:58:44 -07:00
|
|
|
#include <cstring>
|
2017-08-19 22:56:39 -07:00
|
|
|
#include <string>
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
#include <wpi/condition_variable.h>
|
|
|
|
|
#include <wpi/mutex.h>
|
2017-11-13 09:51:48 -08:00
|
|
|
|
2018-05-13 22:02:47 -07:00
|
|
|
#include "HALInitializer.h"
|
2017-08-18 21:35:53 -07:00
|
|
|
#include "MockData/DriverStationDataInternal.h"
|
2017-10-28 00:00:52 -04:00
|
|
|
#include "MockData/MockHooks.h"
|
2016-05-21 01:42:16 -07:00
|
|
|
|
2017-11-13 09:51:48 -08:00
|
|
|
static wpi::mutex msgMutex;
|
2017-12-10 19:38:53 -08:00
|
|
|
static wpi::condition_variable* newDSDataAvailableCond;
|
2017-11-13 09:51:48 -08:00
|
|
|
static wpi::mutex newDSDataAvailableMutex;
|
2017-05-08 20:21:47 -07:00
|
|
|
static int newDSDataAvailableCounter{0};
|
2016-07-17 19:42:33 -07:00
|
|
|
|
2017-12-10 19:38:53 -08:00
|
|
|
namespace hal {
|
|
|
|
|
namespace init {
|
|
|
|
|
void InitializeDriverStation() {
|
|
|
|
|
static wpi::condition_variable nddaC;
|
|
|
|
|
newDSDataAvailableCond = &nddaC;
|
|
|
|
|
}
|
|
|
|
|
} // namespace init
|
|
|
|
|
} // namespace hal
|
|
|
|
|
|
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_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-11-13 09:51:48 -08:00
|
|
|
std::lock_guard<wpi::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-12-10 19:38:53 -08: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();
|
2017-08-18 21:35:53 -07:00
|
|
|
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;
|
2017-12-10 19:38:53 -08:00
|
|
|
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-12-10 19:38:53 -08:00
|
|
|
SimDriverStationData->GetJoystickAxes(joystickNum, 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-12-10 19:38:53 -08:00
|
|
|
SimDriverStationData->GetJoystickPOVs(joystickNum, 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-12-10 19:38:53 -08:00
|
|
|
SimDriverStationData->GetJoystickButtons(joystickNum, buttons);
|
2017-08-18 21:35:53 -07:00
|
|
|
return 0;
|
2015-08-19 11:12:54 -04:00
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
int32_t HAL_GetJoystickDescriptor(int32_t joystickNum,
|
|
|
|
|
HAL_JoystickDescriptor* desc) {
|
2017-12-10 19:38:53 -08:00
|
|
|
SimDriverStationData->GetJoystickDescriptor(joystickNum, desc);
|
2017-08-18 21:35:53 -07:00
|
|
|
return 0;
|
2016-05-20 17:30:37 -07:00
|
|
|
}
|
|
|
|
|
|
2017-11-12 19:33:43 -08:00
|
|
|
HAL_Bool HAL_GetJoystickIsXbox(int32_t joystickNum) {
|
|
|
|
|
HAL_JoystickDescriptor desc;
|
2017-12-10 19:38:53 -08:00
|
|
|
SimDriverStationData->GetJoystickDescriptor(joystickNum, &desc);
|
2017-11-12 19:33:43 -08:00
|
|
|
return desc.isXbox;
|
|
|
|
|
}
|
2016-05-20 17:30:37 -07:00
|
|
|
|
2017-11-12 19:33:43 -08:00
|
|
|
int32_t HAL_GetJoystickType(int32_t joystickNum) {
|
|
|
|
|
HAL_JoystickDescriptor desc;
|
2017-12-10 19:38:53 -08:00
|
|
|
SimDriverStationData->GetJoystickDescriptor(joystickNum, &desc);
|
2017-11-12 19:33:43 -08:00
|
|
|
return desc.type;
|
|
|
|
|
}
|
2016-05-20 17:30:37 -07:00
|
|
|
|
2016-07-12 10:45:14 -07:00
|
|
|
char* HAL_GetJoystickName(int32_t joystickNum) {
|
2017-11-12 19:33:43 -08:00
|
|
|
HAL_JoystickDescriptor desc;
|
2017-12-10 19:38:53 -08:00
|
|
|
SimDriverStationData->GetJoystickDescriptor(joystickNum, &desc);
|
2017-11-12 19:33:43 -08:00
|
|
|
size_t len = std::strlen(desc.name);
|
|
|
|
|
char* name = static_cast<char*>(std::malloc(len + 1));
|
|
|
|
|
std::strncpy(name, desc.name, len);
|
|
|
|
|
name[len] = '\0';
|
2017-08-18 21:35:53 -07:00
|
|
|
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-12-10 19:38:53 -08:00
|
|
|
SimDriverStationData->SetJoystickOutputs(joystickNum, outputs, leftRumble,
|
|
|
|
|
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-12-10 19:38:53 -08:00
|
|
|
return SimDriverStationData->GetMatchTime();
|
2016-05-20 17:30:37 -07:00
|
|
|
}
|
|
|
|
|
|
2018-07-04 00:18:18 -07:00
|
|
|
int32_t HAL_GetMatchInfo(HAL_MatchInfo* info) {
|
2017-12-10 19:38:53 -08:00
|
|
|
SimDriverStationData->GetMatchInfo(info);
|
2017-11-01 21:58:44 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_FreeMatchInfo(HAL_MatchInfo* info) {
|
2017-12-10 19:38:53 -08:00
|
|
|
SimDriverStationData->FreeMatchInfo(info);
|
2017-11-01 21:58:44 -07:00
|
|
|
}
|
|
|
|
|
|
2017-10-28 00:00:52 -04:00
|
|
|
void HAL_ObserveUserProgramStarting(void) { HALSIM_SetProgramStarted(); }
|
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;
|
|
|
|
|
|
2017-12-04 20:05:51 -08:00
|
|
|
static void InitLastCountKey(void) {
|
|
|
|
|
pthread_key_create(&lastCountKey, std::free);
|
|
|
|
|
}
|
2017-08-19 23:12:24 -07:00
|
|
|
#endif
|
|
|
|
|
|
2018-07-08 15:18:03 +10:00
|
|
|
HAL_Bool HAL_IsNewControlData(void) {
|
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-08-27 00:11:52 -07:00
|
|
|
// 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-05-08 20:21:47 -07:00
|
|
|
int currentCount = 0;
|
|
|
|
|
{
|
2017-11-13 09:51:48 -08:00
|
|
|
std::unique_lock<wpi::mutex> lock(newDSDataAvailableMutex);
|
2017-05-08 20:21:47 -07:00
|
|
|
currentCount = newDSDataAvailableCounter;
|
|
|
|
|
}
|
|
|
|
|
if (lastCount == currentCount) return false;
|
|
|
|
|
lastCount = currentCount;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_WaitForDSData(void) { HAL_WaitForDSDataTimeout(0); }
|
|
|
|
|
|
|
|
|
|
HAL_Bool HAL_WaitForDSDataTimeout(double timeout) {
|
|
|
|
|
auto timeoutTime =
|
|
|
|
|
std::chrono::steady_clock::now() + std::chrono::duration<double>(timeout);
|
|
|
|
|
|
2017-11-13 09:51:48 -08:00
|
|
|
std::unique_lock<wpi::mutex> lock(newDSDataAvailableMutex);
|
2017-05-08 20:21:47 -07:00
|
|
|
int currentCount = newDSDataAvailableCounter;
|
|
|
|
|
while (newDSDataAvailableCounter == currentCount) {
|
|
|
|
|
if (timeout > 0) {
|
2017-12-10 19:38:53 -08:00
|
|
|
auto timedOut = newDSDataAvailableCond->wait_until(lock, timeoutTime);
|
2017-05-08 20:21:47 -07:00
|
|
|
if (timedOut == std::cv_status::timeout) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2017-12-10 19:38:53 -08:00
|
|
|
newDSDataAvailableCond->wait(lock);
|
2017-05-08 20:21:47 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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-11-13 09:51:48 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(newDSDataAvailableMutex);
|
2017-05-08 20:21:47 -07:00
|
|
|
// Nofify all threads
|
|
|
|
|
newDSDataAvailableCounter++;
|
2017-12-10 19:38:53 -08:00
|
|
|
newDSDataAvailableCond->notify_all();
|
2017-05-08 20:21:47 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-17 19:42:33 -07:00
|
|
|
void HAL_InitializeDriverStation(void) {
|
2018-05-13 22:02:47 -07:00
|
|
|
hal::init::CheckInit();
|
2017-05-08 20:21:47 -07:00
|
|
|
static std::atomic_bool initialized{false};
|
2017-11-13 09:51:48 -08:00
|
|
|
static wpi::mutex initializeMutex;
|
2017-05-08 20:21:47 -07:00
|
|
|
// Initial check, as if it's true initialization has finished
|
|
|
|
|
if (initialized) return;
|
|
|
|
|
|
2017-11-13 09:51:48 -08:00
|
|
|
std::lock_guard<wpi::mutex> lock(initializeMutex);
|
2017-05-08 20:21:47 -07:00
|
|
|
// Second check in case another thread was waiting
|
|
|
|
|
if (initialized) return;
|
|
|
|
|
|
2017-12-10 19:38:53 -08: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
|
|
|
void HAL_ReleaseDSMutex(void) { newDataOccur(refNumber); }
|
|
|
|
|
|
2015-11-26 00:08:32 -08:00
|
|
|
} // extern "C"
|