diff --git a/build.gradle b/build.gradle index 5048e6efd9..632d947574 100644 --- a/build.gradle +++ b/build.gradle @@ -166,8 +166,13 @@ subprojects { doLast { binaries.all { tasks.withType(CppCompile) { - linker.args << '-L' + niLibraryPath - linker.args.addAll(niLibraryArgs) + // desktop version doesn't use all the NI libraries + // so only do this for arm libraries + String architecture = targetPlatform.architecture + if (architecture.contains('arm')){ + linker.args << '-L' + niLibraryPath + linker.args.addAll(niLibraryArgs) + } } } model { diff --git a/hal/build.gradle b/hal/build.gradle index 9c5642cf39..3dfe30313c 100644 --- a/hal/build.gradle +++ b/hal/build.gradle @@ -1,3 +1,7 @@ +// There are two hal libraries that are built +// - Desktop which is used by simulation (gcc/msvc) +// - Athena which is used by the roborio (arm) + apply plugin: 'cpp' model { @@ -10,11 +14,30 @@ model { sources { cpp { source { - srcDirs = ["lib/Athena", "lib/Athena/FRC_FPGA_ChipObject"] + srcDirs = ["lib/Athena", "lib/Athena/FRC_FPGA_ChipObject", "lib/Shared"] includes = ["**/*.cpp"] } exportedHeaders { - srcDirs = ["include", "lib/Athena", "lib/Athena/FRC_FPGA_ChipObject"] + srcDirs = ["include", "lib/Athena", "lib/Athena/FRC_FPGA_ChipObject", "lib/Shared"] + } + } + } + } + HALDesktop(NativeLibrarySpec) { + binaries.all { + if (toolChain in Gcc){ + cppCompiler.args "-std=c++1y" + } + } + + sources { + cpp { + source { + srcDirs = ["lib/Desktop", "lib/Shared"] + includes = ["**/*.cpp"] + } + exportedHeaders { + srcDirs = ["include", "lib/Desktop", "lib/Shared"] } } } diff --git a/hal/include/HAL/HAL.hpp b/hal/include/HAL/HAL.hpp index 01698642a7..586739175c 100644 --- a/hal/include/HAL/HAL.hpp +++ b/hal/include/HAL/HAL.hpp @@ -164,8 +164,8 @@ enum HALAllianceStationID { /* The maximum number of axes that will be stored in a single HALJoystickAxes struct. This is used for allocating buffers, not bounds checking, since there are usually less axes in practice. */ -static constexpr size_t kMaxJoystickAxes = 12; -static constexpr size_t kMaxJoystickPOVs = 12; +static const size_t kMaxJoystickAxes = 12; +static const size_t kMaxJoystickPOVs = 12; struct HALJoystickAxes { uint16_t count; @@ -234,8 +234,8 @@ extern "C" int HALSetJoystickOutputs(uint8_t joystickNum, uint32_t outputs, uint16_t leftRumble, uint16_t rightRumble); int HALGetMatchTime(float *matchTime); - void HALSetNewDataSem(MULTIWAIT_ID sem); - + void HALSetNewDataSem(NATIVE_MULTIWAIT_ID sem); + bool HALGetSystemActive(int32_t *status); bool HALGetBrownedOut(int32_t *status); diff --git a/hal/include/HAL/Semaphore.hpp b/hal/include/HAL/Semaphore.hpp index e9bb8ba6f9..9cdcf48398 100644 --- a/hal/include/HAL/Semaphore.hpp +++ b/hal/include/HAL/Semaphore.hpp @@ -1,42 +1,24 @@ #pragma once #include -#include -#include +#include +#include -typedef pthread_mutex_t* MUTEX_ID; -typedef sem_t* SEMAPHORE_ID; -typedef pthread_cond_t* MULTIWAIT_ID; +typedef std::mutex* MUTEX_ID; +typedef std::condition_variable* MULTIWAIT_ID; +typedef std::condition_variable::native_handle_type NATIVE_MULTIWAIT_ID; extern "C" { - extern const uint32_t SEMAPHORE_Q_FIFO; - extern const uint32_t SEMAPHORE_Q_PRIORITY; - extern const uint32_t SEMAPHORE_DELETE_SAFE; - extern const uint32_t SEMAPHORE_INVERSION_SAFE; - - extern const int32_t SEMAPHORE_NO_WAIT; - extern const int32_t SEMAPHORE_WAIT_FOREVER; - - extern const uint32_t SEMAPHORE_EMPTY; - extern const uint32_t SEMAPHORE_FULL; - - MUTEX_ID initializeMutexRecursive(); MUTEX_ID initializeMutexNormal(); void deleteMutex(MUTEX_ID sem); - int8_t takeMutex(MUTEX_ID sem); - int8_t tryTakeMutex(MUTEX_ID sem); - int8_t giveMutex(MUTEX_ID sem); - - SEMAPHORE_ID initializeSemaphore(uint32_t initial_value); - void deleteSemaphore(SEMAPHORE_ID sem); - int8_t takeSemaphore(SEMAPHORE_ID sem); - int8_t tryTakeSemaphore(SEMAPHORE_ID sem); - int8_t giveSemaphore(SEMAPHORE_ID sem); + void takeMutex(MUTEX_ID sem); + bool tryTakeMutex(MUTEX_ID sem); + void giveMutex(MUTEX_ID sem); MULTIWAIT_ID initializeMultiWait(); void deleteMultiWait(MULTIWAIT_ID sem); - int8_t takeMultiWait(MULTIWAIT_ID sem, MUTEX_ID m, int32_t timeout); - int8_t giveMultiWait(MULTIWAIT_ID sem); + void takeMultiWait(MULTIWAIT_ID sem, MUTEX_ID m); + void giveMultiWait(MULTIWAIT_ID sem); } diff --git a/hal/include/HAL/cpp/priority_condition_variable.h b/hal/include/HAL/cpp/priority_condition_variable.h index 4a74d3091a..9b5b0201ea 100644 --- a/hal/include/HAL/cpp/priority_condition_variable.h +++ b/hal/include/HAL/cpp/priority_condition_variable.h @@ -14,7 +14,7 @@ #include "priority_mutex.h" class priority_condition_variable { - typedef pthread_cond_t* native_handle_type; + typedef std::condition_variable::native_handle_type native_handle_type; typedef std::chrono::system_clock clock_t; public: @@ -99,7 +99,7 @@ class priority_condition_variable { struct Unlock { explicit Unlock(Lock& lk) : m_lock(lk) { lk.unlock(); } - ~Unlock() noexcept(false) { + ~Unlock() /*noexcept(false)*/ { if (std::uncaught_exception()) { try { m_lock.lock(); } catch(...) {} } diff --git a/hal/lib/Athena/HAL.cpp b/hal/lib/Athena/HALAthena.cpp similarity index 71% rename from hal/lib/Athena/HAL.cpp rename to hal/lib/Athena/HALAthena.cpp index 185f2be5f8..d41370ec02 100644 --- a/hal/lib/Athena/HAL.cpp +++ b/hal/lib/Athena/HALAthena.cpp @@ -62,16 +62,16 @@ const char* getHALErrorMessage(int32_t code) return NiFpga_Status_FifoTimeout_MESSAGE; case NiFpga_Status_TransferAborted: return NiFpga_Status_TransferAborted_MESSAGE; - case NiFpga_Status_MemoryFull: + case NiFpga_Status_MemoryFull: return NiFpga_Status_MemoryFull_MESSAGE; case NiFpga_Status_SoftwareFault: return NiFpga_Status_SoftwareFault_MESSAGE; case NiFpga_Status_InvalidParameter: return NiFpga_Status_InvalidParameter_MESSAGE; case NiFpga_Status_ResourceNotFound: - return NiFpga_Status_ResourceNotFound_MESSAGE; + return NiFpga_Status_ResourceNotFound_MESSAGE; case NiFpga_Status_ResourceNotInitialized: - return NiFpga_Status_ResourceNotInitialized_MESSAGE; + return NiFpga_Status_ResourceNotInitialized_MESSAGE; case NiFpga_Status_HardwareFault: return NiFpga_Status_HardwareFault_MESSAGE; case NiFpga_Status_IrqTimeout: @@ -186,107 +186,12 @@ int HALSetErrorData(const char *errors, int errorsLength, int wait_ms) return setErrorData(errors, errorsLength, wait_ms); } -int HALGetControlWord(HALControlWord *data) -{ - return FRC_NetworkCommunication_getControlWord((ControlWord_t*) data); -} - -int HALGetAllianceStation(enum HALAllianceStationID *allianceStation) -{ - return FRC_NetworkCommunication_getAllianceStation((AllianceStationID_t*) allianceStation); -} - -int HALGetJoystickAxes(uint8_t joystickNum, HALJoystickAxes *axes) -{ - return FRC_NetworkCommunication_getJoystickAxes(joystickNum, (JoystickAxes_t*) axes, kMaxJoystickAxes); -} - -int HALGetJoystickPOVs(uint8_t joystickNum, HALJoystickPOVs *povs) -{ - return FRC_NetworkCommunication_getJoystickPOVs(joystickNum, (JoystickPOV_t*) povs, kMaxJoystickPOVs); -} - -int HALGetJoystickButtons(uint8_t joystickNum, HALJoystickButtons *buttons) -{ - return FRC_NetworkCommunication_getJoystickButtons(joystickNum, &buttons->buttons, &buttons->count); -} - -int HALGetJoystickDescriptor(uint8_t joystickNum, HALJoystickDescriptor *desc) -{ - return FRC_NetworkCommunication_getJoystickDesc(joystickNum, &desc->isXbox, &desc->type, (char *)(&desc->name), - &desc->axisCount, (uint8_t *)&desc->axisTypes, &desc->buttonCount, &desc->povCount); -} - -int HALGetJoystickIsXbox(uint8_t joystickNum) -{ - HALJoystickDescriptor joystickDesc; - if(HALGetJoystickDescriptor(joystickNum, &joystickDesc)<0) - { - return 0; - }else - { - return joystickDesc.isXbox; - } -} - -int HALGetJoystickType(uint8_t joystickNum) -{ - HALJoystickDescriptor joystickDesc; - if(HALGetJoystickDescriptor(joystickNum, &joystickDesc)<0) - { - return -1; - } else - { - return joystickDesc.type; - } -} - -const char* HALGetJoystickName(uint8_t joystickNum) -{ - HALJoystickDescriptor joystickDesc; - if(HALGetJoystickDescriptor(joystickNum, &joystickDesc)<0) - { - const char* retval = ""; - return retval; - } else - { - const char* retval(joystickDesc.name); - return retval; - } -} - -int HALGetJoystickAxisType(uint8_t joystickNum, int axis) -{ - HALJoystickDescriptor joystickDesc; - if(HALGetJoystickDescriptor(joystickNum, &joystickDesc)<0) - { - return -1; - } else - { - return joystickDesc.axisTypes[axis]; - } -} - -int HALSetJoystickOutputs(uint8_t joystickNum, uint32_t outputs, uint16_t leftRumble, uint16_t rightRumble) -{ - return FRC_NetworkCommunication_setJoystickOutputs(joystickNum, outputs, leftRumble, rightRumble); -} - -int HALGetMatchTime(float *matchTime) -{ - return FRC_NetworkCommunication_getMatchTime(matchTime); -} - -void HALSetNewDataSem(MULTIWAIT_ID sem) -{ - setNewDataSem(sem); -} bool HALGetSystemActive(int32_t *status) { return watchdog->readStatus_SystemActive(status); } - + bool HALGetBrownedOut(int32_t *status) { return !(watchdog->readStatus_PowerAlive(status)); @@ -360,31 +265,6 @@ int HALInitialize(int mode) return 1; } -void HALNetworkCommunicationObserveUserProgramStarting(void) -{ - FRC_NetworkCommunication_observeUserProgramStarting(); -} - -void HALNetworkCommunicationObserveUserProgramDisabled(void) -{ - FRC_NetworkCommunication_observeUserProgramDisabled(); -} - -void HALNetworkCommunicationObserveUserProgramAutonomous(void) -{ - FRC_NetworkCommunication_observeUserProgramAutonomous(); -} - -void HALNetworkCommunicationObserveUserProgramTeleop(void) -{ - FRC_NetworkCommunication_observeUserProgramTeleop(); -} - -void HALNetworkCommunicationObserveUserProgramTest(void) -{ - FRC_NetworkCommunication_observeUserProgramTest(); -} - uint32_t HALReport(uint8_t resource, uint8_t instanceNumber, uint8_t context, const char *feature) { diff --git a/hal/lib/Athena/Semaphore.cpp b/hal/lib/Athena/Semaphore.cpp index b08727c2c5..d24bba0fa8 100644 --- a/hal/lib/Athena/Semaphore.cpp +++ b/hal/lib/Athena/Semaphore.cpp @@ -9,128 +9,34 @@ TLogLevel semaphoreLogLevel = logDEBUG; if (level > semaphoreLogLevel) ; \ else Log().Get(level) -// See: http://www.vxdev.com/docs/vx55man/vxworks/ref/semMLib.html -const uint32_t SEMAPHORE_Q_FIFO= 0x01; // TODO: Support -const uint32_t SEMAPHORE_Q_PRIORITY = 0x01; // TODO: Support -const uint32_t SEMAPHORE_DELETE_SAFE = 0x04; // TODO: Support -const uint32_t SEMAPHORE_INVERSION_SAFE = 0x08; // TODO: Support +MUTEX_ID initializeMutexNormal() { return new std::mutex; } -const int32_t SEMAPHORE_NO_WAIT = 0; -const int32_t SEMAPHORE_WAIT_FOREVER = -1; - -const uint32_t SEMAPHORE_EMPTY = 0; -const uint32_t SEMAPHORE_FULL = 1; - -MUTEX_ID initializeMutexRecursive() -{ - pthread_mutexattr_t attr; - pthread_mutexattr_init(&attr); - pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); - MUTEX_ID sem = new pthread_mutex_t(); - pthread_mutex_init(sem, &attr); - pthread_mutexattr_destroy(&attr); - return sem; -} - -MUTEX_ID initializeMutexNormal() -{ - pthread_mutexattr_t attr; - pthread_mutexattr_init(&attr); - pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL); - MUTEX_ID sem = new pthread_mutex_t(); - pthread_mutex_init(sem, &attr); - pthread_mutexattr_destroy(&attr); - return sem; -} - -void deleteMutex(MUTEX_ID sem) -{ - pthread_mutex_destroy(sem); - delete sem; -} +void deleteMutex(MUTEX_ID sem) { delete sem; } /** - * Lock the semaphore, blocking until it's available. + * Lock the mutex, blocking until it's available. + */ +void takeMutex(MUTEX_ID mutex) { mutex->lock(); } + +/** + * Attempt to lock the mutex. + * @return true if succeeded in locking the mutex, false otherwise. + */ +bool tryTakeMutex(MUTEX_ID mutex) { return mutex->try_lock(); } + +/** + * Unlock the mutex. * @return 0 for success, -1 for error. If -1, the error will be in errno. */ -int8_t takeMutex(MUTEX_ID sem) -{ - return pthread_mutex_lock(sem); +void giveMutex(MUTEX_ID mutex) { mutex->unlock(); } + +MULTIWAIT_ID initializeMultiWait() { return new std::condition_variable; } + +void deleteMultiWait(MULTIWAIT_ID cond) { delete cond; } + +void takeMultiWait(MULTIWAIT_ID cond, MUTEX_ID m) { + std::unique_lock lock(*m); + cond->wait(lock); } -int8_t tryTakeMutex(MUTEX_ID sem) -{ - return pthread_mutex_trylock(sem); -} - -/** - * Unlock the semaphore. - * @return 0 for success, -1 for error. If -1, the error will be in errno. - */ -int8_t giveMutex(MUTEX_ID sem) -{ - return pthread_mutex_unlock(sem); -} - -SEMAPHORE_ID initializeSemaphore(uint32_t initial_value) { - SEMAPHORE_ID sem = new sem_t; - sem_init(sem, 0, initial_value); - return sem; -} - -void deleteSemaphore(SEMAPHORE_ID sem) { - sem_destroy(sem); - delete sem; -} - -/** - * Lock the semaphore, blocking until it's available. - * @return 0 for success, -1 for error. If -1, the error will be in errno. - */ -int8_t takeSemaphore(SEMAPHORE_ID sem) -{ - return sem_wait(sem); -} - -int8_t tryTakeSemaphore(SEMAPHORE_ID sem) -{ - return sem_trywait(sem); -} - -/** - * Unlock the semaphore. - * @return 0 for success, -1 for error. If -1, the error will be in errno. - */ -int8_t giveSemaphore(SEMAPHORE_ID sem) -{ - return sem_post(sem); -} - - -MULTIWAIT_ID initializeMultiWait() { - pthread_condattr_t attr; - pthread_condattr_init(&attr); - MULTIWAIT_ID cond = new pthread_cond_t(); - pthread_cond_init(cond, &attr); - pthread_condattr_destroy(&attr); - return cond; -} - -void deleteMultiWait(MULTIWAIT_ID sem) { - pthread_cond_destroy(sem); - delete sem; -} - -/** - * @param timeout Not implemented. - */ -int8_t takeMultiWait(MULTIWAIT_ID sem, MUTEX_ID m, int32_t timeout) { - takeMutex(m); - int8_t val = pthread_cond_wait(sem, m); - giveMutex(m); - return val; -} - -int8_t giveMultiWait(MULTIWAIT_ID sem) { - return pthread_cond_broadcast(sem); -} +void giveMultiWait(MULTIWAIT_ID cond) { cond->notify_all(); } diff --git a/hal/lib/Desktop/HALDesktop.cpp b/hal/lib/Desktop/HALDesktop.cpp new file mode 100644 index 0000000000..7860708750 --- /dev/null +++ b/hal/lib/Desktop/HALDesktop.cpp @@ -0,0 +1 @@ +//nothing here yet! \ No newline at end of file diff --git a/hal/lib/Shared/HAL.cpp b/hal/lib/Shared/HAL.cpp new file mode 100644 index 0000000000..c600a99e25 --- /dev/null +++ b/hal/lib/Shared/HAL.cpp @@ -0,0 +1,124 @@ +//This file must compile on ALL PLATFORMS. Be very careful what you put in here. +#include "HAL/HAL.hpp" +#include "NetworkCommunication/FRCComm.h" + +int HALGetControlWord(HALControlWord *data) +{ + return FRC_NetworkCommunication_getControlWord((ControlWord_t*) data); +} + +void HALSetNewDataSem(NATIVE_MULTIWAIT_ID sem) +{ + setNewDataSem(sem); +} + +int HALGetAllianceStation(enum HALAllianceStationID *allianceStation) +{ + return FRC_NetworkCommunication_getAllianceStation((AllianceStationID_t*) allianceStation); +} + +int HALGetJoystickAxes(uint8_t joystickNum, HALJoystickAxes *axes) +{ + return FRC_NetworkCommunication_getJoystickAxes(joystickNum, (JoystickAxes_t*) axes, kMaxJoystickAxes); +} + +int HALGetJoystickPOVs(uint8_t joystickNum, HALJoystickPOVs *povs) +{ + return FRC_NetworkCommunication_getJoystickPOVs(joystickNum, (JoystickPOV_t*) povs, kMaxJoystickPOVs); +} + +int HALGetJoystickButtons(uint8_t joystickNum, HALJoystickButtons *buttons) +{ + return FRC_NetworkCommunication_getJoystickButtons(joystickNum, &buttons->buttons, &buttons->count); +} + +int HALGetJoystickDescriptor(uint8_t joystickNum, HALJoystickDescriptor *desc) +{ + return FRC_NetworkCommunication_getJoystickDesc(joystickNum, &desc->isXbox, &desc->type, (char *)(&desc->name), + &desc->axisCount, (uint8_t *)&desc->axisTypes, &desc->buttonCount, &desc->povCount); +} + +int HALGetJoystickIsXbox(uint8_t joystickNum) +{ + HALJoystickDescriptor joystickDesc; + if(HALGetJoystickDescriptor(joystickNum, &joystickDesc)<0) + { + return 0; + }else + { + return joystickDesc.isXbox; + } +} + +int HALGetJoystickType(uint8_t joystickNum) +{ + HALJoystickDescriptor joystickDesc; + if(HALGetJoystickDescriptor(joystickNum, &joystickDesc)<0) + { + return -1; + } else + { + return joystickDesc.type; + } +} + +const char* HALGetJoystickName(uint8_t joystickNum) +{ + HALJoystickDescriptor joystickDesc; + if(HALGetJoystickDescriptor(joystickNum, &joystickDesc)<0) + { + const char* retval = ""; + return retval; + } else + { + const char* retval(joystickDesc.name); + return retval; + } +} + +int HALGetJoystickAxisType(uint8_t joystickNum, int axis) +{ + HALJoystickDescriptor joystickDesc; + if(HALGetJoystickDescriptor(joystickNum, &joystickDesc)<0) + { + return -1; + } else + { + return joystickDesc.axisTypes[axis]; + } +} + +int HALSetJoystickOutputs(uint8_t joystickNum, uint32_t outputs, uint16_t leftRumble, uint16_t rightRumble) +{ + return FRC_NetworkCommunication_setJoystickOutputs(joystickNum, outputs, leftRumble, rightRumble); +} + +int HALGetMatchTime(float *matchTime) +{ + return FRC_NetworkCommunication_getMatchTime(matchTime); +} + +void HALNetworkCommunicationObserveUserProgramStarting(void) +{ + FRC_NetworkCommunication_observeUserProgramStarting(); +} + +void HALNetworkCommunicationObserveUserProgramDisabled(void) +{ + FRC_NetworkCommunication_observeUserProgramDisabled(); +} + +void HALNetworkCommunicationObserveUserProgramAutonomous(void) +{ + FRC_NetworkCommunication_observeUserProgramAutonomous(); +} + +void HALNetworkCommunicationObserveUserProgramTeleop(void) +{ + FRC_NetworkCommunication_observeUserProgramTeleop(); +} + +void HALNetworkCommunicationObserveUserProgramTest(void) +{ + FRC_NetworkCommunication_observeUserProgramTest(); +} diff --git a/hal/lib/Athena/NetworkCommunication/FRCComm.h b/hal/lib/Shared/NetworkCommunication/FRCComm.h similarity index 89% rename from hal/lib/Athena/NetworkCommunication/FRCComm.h rename to hal/lib/Shared/NetworkCommunication/FRCComm.h index ed094aa7c8..54c9463dcc 100644 --- a/hal/lib/Athena/NetworkCommunication/FRCComm.h +++ b/hal/lib/Shared/NetworkCommunication/FRCComm.h @@ -11,20 +11,21 @@ * *************************************************************/ +//This file must compile on ALL PLATFORMS. Be very careful what you put in here. + #ifndef __FRC_COMM_H__ #define __FRC_COMM_H__ -#ifdef SIMULATION -#include -#ifdef USE_THRIFT -#define EXPORT_FUNC +#ifdef _WIN32 + #ifdef USE_THRIFT + #define EXPORT_FUNC + #else + #define EXPORT_FUNC __declspec(dllexport) __cdecl + #endif #else -#define EXPORT_FUNC __declspec(dllexport) __cdecl -#endif -#else -#include -#include -#define EXPORT_FUNC + #include + #include + #define EXPORT_FUNC #endif #define ERR_FRCSystem_NetCommNotResponding -44049 diff --git a/simulation/SimDS/pom.xml b/simulation/SimDS/pom.xml deleted file mode 100644 index aba62cdda8..0000000000 --- a/simulation/SimDS/pom.xml +++ /dev/null @@ -1,92 +0,0 @@ - - - 4.0.0 - edu.wpi.first.wpilibj.simulation - SimDS - jar - 0.1.0-SNAPSHOT - - - - docline-java8-disable - - [1.8, - - - - - - org.apache.maven.plugins - maven-javadoc-plugin - - -Xdoclint:none - - - - - - - - - - net.java.jinput - jinput - 2.0.5 - - - org.gazebosim - JavaGazebo - 0.1.0-SNAPSHOT - - - - - - - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.1 - - 1.7 - 1.7 - - - - org.apache.maven.plugins - maven-jar-plugin - 2.5 - - - - edu.wpi.first.wpilibj.simulation.ds.Main - - - - - - - org.apache.maven.plugins - maven-shade-plugin - 2.3 - - - - - - package - - shade - - - - - - - diff --git a/wpilibc/wpilibC++/include/LiveWindow/LiveWindow.h b/wpilibc/wpilibC++/include/LiveWindow/LiveWindow.h index ba7381f697..8eb5bf6ce3 100644 --- a/wpilibc/wpilibC++/include/LiveWindow/LiveWindow.h +++ b/wpilibc/wpilibC++/include/LiveWindow/LiveWindow.h @@ -58,8 +58,13 @@ class LiveWindow { LiveWindowSendable &component); void AddActuator(const std::string &subsystem, const std::string &name, std::shared_ptr component); + #if !defined(_MSC_VER) -[[deprecated]] + [[deprecated( + "Raw pointers are deprecated; pass the component using shared_ptr " + "instead.")]] +#else + __declspec(deprecated("**Raw pointers are deprecated; pass the component using shared_ptr instead**")) #endif void AddSensor(std::string type, int channel, LiveWindowSendable *component); void AddActuator(std::string type, int channel, diff --git a/wpilibc/wpilibC++Devices/include/DriverStation.h b/wpilibc/wpilibC++Devices/include/DriverStation.h index f2918f2d53..23ee095693 100644 --- a/wpilibc/wpilibC++Devices/include/DriverStation.h +++ b/wpilibc/wpilibC++Devices/include/DriverStation.h @@ -101,10 +101,8 @@ class DriverStation : public SensorBase, public RobotStateInterface { HALJoystickPOVs m_joystickPOVs[kJoystickPorts]; HALJoystickButtons m_joystickButtons[kJoystickPorts]; HALJoystickDescriptor m_joystickDescriptor[kJoystickPorts]; -#ifndef FRC_SIMULATOR Task m_task; std::atomic m_isRunning{false}; -#endif mutable Semaphore m_newControlData{Semaphore::kEmpty}; mutable priority_condition_variable m_packetDataAvailableCond; priority_mutex m_packetDataAvailableMutex; diff --git a/wpilibc/wpilibC++Sim/include/DriverStation.h b/wpilibc/wpilibC++Sim/include/DriverStation.h index ba3261aa45..f15985cd11 100644 --- a/wpilibc/wpilibC++Sim/include/DriverStation.h +++ b/wpilibc/wpilibC++Sim/include/DriverStation.h @@ -1,142 +1,112 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) FIRST 2008. All Rights Reserved. */ +/* 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. */ /*----------------------------------------------------------------------------*/ #pragma once -#include "simulation/gz_msgs/msgs.h" - -#ifdef _WIN32 - // Ensure that Winsock2.h is included before Windows.h, which can get - // pulled in by anybody (e.g., Boost). - #include -#endif - -#include #include "SensorBase.h" #include "RobotState.h" -#include +#include "HAL/HAL.hpp" +#include "HAL/cpp/Semaphore.hpp" +#include "HAL/cpp/priority_mutex.h" +#include "HAL/cpp/priority_condition_variable.h" #include -struct HALCommonControlData; +struct HALControlWord; class AnalogInput; -using namespace gazebo; - /** - * Provide access to the network communication data to / from the Driver Station. + * Provide access to the network communication data to / from the Driver + * Station. */ -class DriverStation : public SensorBase, public RobotStateInterface -{ -public: - enum Alliance - { - kRed, - kBlue, - kInvalid - }; +class DriverStation : public SensorBase, public RobotStateInterface { + public: + enum Alliance { kRed, kBlue, kInvalid }; - virtual ~DriverStation() = default; - static DriverStation *GetInstance(); - static void ReportError(std::string error); + virtual ~DriverStation(); + static DriverStation &GetInstance(); + static void ReportError(std::string error); - static const uint32_t kBatteryChannel = 7; - static const uint32_t kJoystickPorts = 4; - static const uint32_t kJoystickAxes = 6; + static const uint32_t kJoystickPorts = 6; - float GetStickAxis(uint32_t stick, uint32_t axis); - bool GetStickButton(uint32_t stick, uint32_t button); - short GetStickButtons(uint32_t stick); + float GetStickAxis(uint32_t stick, uint32_t axis); + int GetStickPOV(uint32_t stick, uint32_t pov); + uint32_t GetStickButtons(uint32_t stick) const; + bool GetStickButton(uint32_t stick, uint8_t button); - float GetAnalogIn(uint32_t channel); - bool GetDigitalIn(uint32_t channel); - void SetDigitalOut(uint32_t channel, bool value); - bool GetDigitalOut(uint32_t channel); + int GetStickAxisCount(uint32_t stick) const; + int GetStickPOVCount(uint32_t stick) const; + int GetStickButtonCount(uint32_t stick) const; - bool IsEnabled() const; - bool IsDisabled() const; - bool IsAutonomous() const; - bool IsOperatorControl() const; - bool IsTest() const; - bool IsFMSAttached() const; + bool GetJoystickIsXbox(uint32_t stick) const; + int GetJoystickType(uint32_t stick) const; + std::string GetJoystickName(uint32_t stick) const; + int GetJoystickAxisType(uint32_t stick, uint8_t axis) const; - uint32_t GetPacketNumber() const; - Alliance GetAlliance() const; - uint32_t GetLocation() const; - void WaitForData(); - double GetMatchTime() const; - float GetBatteryVoltage() const; - uint16_t GetTeamNumber() const; + bool IsEnabled() const override; + bool IsDisabled() const override; + bool IsAutonomous() const override; + bool IsOperatorControl() const override; + bool IsTest() const override; + bool IsDSAttached() const; + bool IsNewControlData() const; + bool IsFMSAttached() const; + bool IsSysActive() const; + bool IsSysBrownedOut() const; + Alliance GetAlliance() const; + uint32_t GetLocation() const; + void WaitForData(); + double GetMatchTime() const; + float GetBatteryVoltage() const; + /** Only to be used to tell the Driver Station what code you claim to be + * executing + * for diagnostic purposes only + * @param entering If true, starting disabled code; if false, leaving disabled + * code */ + void InDisabled(bool entering) { m_userInDisabled = entering; } + /** Only to be used to tell the Driver Station what code you claim to be + * executing + * for diagnostic purposes only + * @param entering If true, starting autonomous code; if false, leaving + * autonomous code */ + void InAutonomous(bool entering) { m_userInAutonomous = entering; } + /** Only to be used to tell the Driver Station what code you claim to be + * executing + * for diagnostic purposes only + * @param entering If true, starting teleop code; if false, leaving teleop + * code */ + void InOperatorControl(bool entering) { m_userInTeleop = entering; } + /** Only to be used to tell the Driver Station what code you claim to be + * executing + * for diagnostic purposes only + * @param entering If true, starting test code; if false, leaving test code */ + void InTest(bool entering) { m_userInTest = entering; } - void IncrementUpdateNumber() - { - m_updateNumber++; - } + protected: + DriverStation(); - /** Only to be used to tell the Driver Station what code you claim to be executing - * for diagnostic purposes only - * @param entering If true, starting disabled code; if false, leaving disabled code */ - void InDisabled(bool entering) - { - m_userInDisabled = entering; - } - /** Only to be used to tell the Driver Station what code you claim to be executing - * for diagnostic purposes only - * @param entering If true, starting autonomous code; if false, leaving autonomous code */ - void InAutonomous(bool entering) - { - m_userInAutonomous = entering; - } - /** Only to be used to tell the Driver Station what code you claim to be executing - * for diagnostic purposes only - * @param entering If true, starting teleop code; if false, leaving teleop code */ - void InOperatorControl(bool entering) - { - m_userInTeleop = entering; - } - /** Only to be used to tell the Driver Station what code you claim to be executing - * for diagnostic purposes only - * @param entering If true, starting test code; if false, leaving test code */ - void InTest(bool entering) - { - m_userInTest = entering; - } + void GetData(); -protected: - DriverStation(); + private: + static DriverStation *m_instance; + void ReportJoystickUnpluggedError(std::string message); + void Run(); -private: - static void InitTask(DriverStation *ds); - static DriverStation *m_instance; - static uint8_t m_updateNumber; - ///< TODO: Get rid of this and use the semaphore signaling - static const float kUpdatePeriod; - - void stateCallback(const msgs::ConstDriverStationPtr &msg); - void joystickCallback(const msgs::ConstFRCJoystickPtr &msg, int i); - void joystickCallback0(const msgs::ConstFRCJoystickPtr &msg); - void joystickCallback1(const msgs::ConstFRCJoystickPtr &msg); - void joystickCallback2(const msgs::ConstFRCJoystickPtr &msg); - void joystickCallback3(const msgs::ConstFRCJoystickPtr &msg); - void joystickCallback4(const msgs::ConstFRCJoystickPtr &msg); - void joystickCallback5(const msgs::ConstFRCJoystickPtr &msg); - - uint8_t m_digitalOut = 0; - std::condition_variable m_waitForDataCond; - std::mutex m_waitForDataMutex; - mutable std::recursive_mutex m_stateMutex; - std::recursive_mutex m_joystickMutex; - double m_approxMatchTimeOffset = 0; - bool m_userInDisabled = false; - bool m_userInAutonomous = false; - bool m_userInTeleop = false; - bool m_userInTest = false; - - transport::SubscriberPtr stateSub; - transport::SubscriberPtr joysticksSub[6]; - msgs::DriverStationPtr state; - msgs::FRCJoystickPtr joysticks[6]; + HALJoystickAxes m_joystickAxes[kJoystickPorts]; + HALJoystickPOVs m_joystickPOVs[kJoystickPorts]; + HALJoystickButtons m_joystickButtons[kJoystickPorts]; + HALJoystickDescriptor m_joystickDescriptor[kJoystickPorts]; + mutable Semaphore m_newControlData{Semaphore::kEmpty}; + mutable priority_condition_variable m_packetDataAvailableCond; + priority_mutex m_packetDataAvailableMutex; + std::condition_variable_any m_waitForDataCond; + priority_mutex m_waitForDataMutex; + bool m_userInDisabled = false; + bool m_userInAutonomous = false; + bool m_userInTeleop = false; + bool m_userInTest = false; + double m_nextMessageTime = 0; }; diff --git a/wpilibc/wpilibC++Sim/include/Joystick.h b/wpilibc/wpilibC++Sim/include/Joystick.h index 45a036261f..0922359bf3 100644 --- a/wpilibc/wpilibC++Sim/include/Joystick.h +++ b/wpilibc/wpilibC++Sim/include/Joystick.h @@ -1,5 +1,6 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) FIRST 2008. All Rights Reserved. */ +/* 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. */ /*----------------------------------------------------------------------------*/ @@ -7,7 +8,9 @@ #ifndef JOYSTICK_H_ #define JOYSTICK_H_ +#include #include +#include #include "GenericHID.h" #include "ErrorBase.h" @@ -15,64 +18,101 @@ class DriverStation; /** * Handle input from standard Joysticks connected to the Driver Station. - * This class handles standard input that comes from the Driver Station. Each time a value is requested - * the most recent value is returned. There is a single class instance for each joystick and the mapping + * This class handles standard input that comes from the Driver Station. Each + * time a value is requested + * the most recent value is returned. There is a single class instance for each + * joystick and the mapping * of ports to hardware buttons depends on the code in the driver station. */ -class Joystick : public GenericHID, public ErrorBase -{ -public: - static const uint32_t kDefaultXAxis = 1; - static const uint32_t kDefaultYAxis = 2; - static const uint32_t kDefaultZAxis = 3; - static const uint32_t kDefaultTwistAxis = 4; - static const uint32_t kDefaultThrottleAxis = 3; - typedef enum - { - kXAxis, kYAxis, kZAxis, kTwistAxis, kThrottleAxis, kNumAxisTypes - } AxisType; - static const uint32_t kDefaultTriggerButton = 1; - static const uint32_t kDefaultTopButton = 2; - typedef enum - { - kTriggerButton, kTopButton, kNumButtonTypes - } ButtonType; +class Joystick : public GenericHID, public ErrorBase { + public: + static const uint32_t kDefaultXAxis = 0; + static const uint32_t kDefaultYAxis = 1; + static const uint32_t kDefaultZAxis = 2; + static const uint32_t kDefaultTwistAxis = 2; + static const uint32_t kDefaultThrottleAxis = 3; + typedef enum { + kXAxis, + kYAxis, + kZAxis, + kTwistAxis, + kThrottleAxis, + kNumAxisTypes + } AxisType; + static const uint32_t kDefaultTriggerButton = 1; + static const uint32_t kDefaultTopButton = 2; + typedef enum { kTriggerButton, kTopButton, kNumButtonTypes } ButtonType; + typedef enum { kLeftRumble, kRightRumble } RumbleType; + typedef enum { + kUnknown = -1, + kXInputUnknown = 0, + kXInputGamepad = 1, + kXInputWheel = 2, + kXInputArcadeStick = 3, + kXInputFlightStick = 4, + kXInputDancePad = 5, + kXInputGuitar = 6, + kXInputGuitar2 = 7, + kXInputDrumKit = 8, + kXInputGuitar3 = 11, + kXInputArcadePad = 19, + kHIDJoystick = 20, + kHIDGamepad = 21, + kHIDDriving = 22, + kHIDFlight = 23, + kHID1stPerson = 24 + } HIDType; + explicit Joystick(uint32_t port); + Joystick(uint32_t port, uint32_t numAxisTypes, uint32_t numButtonTypes); + virtual ~Joystick() = default; - explicit Joystick(uint32_t port); - Joystick(uint32_t port, uint32_t numAxisTypes, uint32_t numButtonTypes); - virtual ~Joystick() = default; + Joystick(const Joystick&) = delete; + Joystick& operator=(const Joystick&) = delete; - Joystick(const Joystick&) = delete; - Joystick& operator=(const Joystick&) = delete; + uint32_t GetAxisChannel(AxisType axis) const; + void SetAxisChannel(AxisType axis, uint32_t channel); - uint32_t GetAxisChannel(AxisType axis); - void SetAxisChannel(AxisType axis, uint32_t channel); + virtual float GetX(JoystickHand hand = kRightHand) const override; + virtual float GetY(JoystickHand hand = kRightHand) const override; + virtual float GetZ() const override; + virtual float GetTwist() const override; + virtual float GetThrottle() const override; + virtual float GetAxis(AxisType axis) const; + float GetRawAxis(uint32_t axis) const override; - virtual float GetX(JoystickHand hand = kRightHand) const override; - virtual float GetY(JoystickHand hand = kRightHand) const override; - virtual float GetZ() const override; - virtual float GetTwist() const override; - virtual float GetThrottle() const override; - virtual float GetAxis(AxisType axis) const; - float GetRawAxis(uint32_t axis) const override; + virtual bool GetTrigger(JoystickHand hand = kRightHand) const override; + virtual bool GetTop(JoystickHand hand = kRightHand) const override; + virtual bool GetBumper(JoystickHand hand = kRightHand) const override; + virtual bool GetRawButton(uint32_t button) const override; + virtual int GetPOV(uint32_t pov = 0) const override; + bool GetButton(ButtonType button) const; + static Joystick *GetStickForPort(uint32_t port); - virtual bool GetTrigger(JoystickHand hand = kRightHand) const override; - virtual bool GetTop(JoystickHand hand = kRightHand) const override; - virtual bool GetBumper(JoystickHand hand = kRightHand) const override; - virtual bool GetRawButton(uint32_t button) const override; - virtual int GetPOV(uint32_t pov = 1) const override; - bool GetButton(ButtonType button) const; - static Joystick* GetStickForPort(uint32_t port); + virtual float GetMagnitude() const; + virtual float GetDirectionRadians() const; + virtual float GetDirectionDegrees() const; - virtual float GetMagnitude() const; - virtual float GetDirectionRadians() const; - virtual float GetDirectionDegrees() const; + bool GetIsXbox() const; + Joystick::HIDType GetType() const; + std::string GetName() const; + int GetAxisType(uint8_t axis) const; -private: - DriverStation *m_ds = nullptr; - uint32_t m_port; - std::unique_ptr m_axes; - std::unique_ptr m_buttons; + int GetAxisCount() const; + int GetButtonCount() const; + int GetPOVCount() const; + + void SetRumble(RumbleType type, float value); + void SetOutput(uint8_t outputNumber, bool value); + void SetOutputs(uint32_t value); + + private: + DriverStation &m_ds; + uint32_t m_port; + ::std::vector m_axes; + ::std::vector m_buttons; + uint32_t m_outputs = 0; + uint16_t m_leftRumble = 0; + uint16_t m_rightRumble = 0; }; #endif diff --git a/wpilibc/wpilibC++Sim/include/NetworkCommunication/FRCComm.h b/wpilibc/wpilibC++Sim/include/NetworkCommunication/FRCComm.h new file mode 100644 index 0000000000..588c8ca62b --- /dev/null +++ b/wpilibc/wpilibC++Sim/include/NetworkCommunication/FRCComm.h @@ -0,0 +1,130 @@ +/************************************************************* + * NOTICE + * + * These are the only externally exposed functions to the + * NetworkCommunication library + * + * This is an implementation of FRC Spec for Comm Protocol + * Revision 4.5, June 30, 2008 + * + * Copyright (c) National Instruments 2008. All Rights Reserved. + * + *************************************************************/ + +#ifndef __FRC_COMM_H__ +#define __FRC_COMM_H__ + +#ifdef SIMULATION +#include +#ifdef USE_THRIFT +#define EXPORT_FUNC +#else +#define EXPORT_FUNC __declspec(dllexport) __cdecl +#endif +#else +#include +#include +#define EXPORT_FUNC +#endif + +#define ERR_FRCSystem_NetCommNotResponding -44049 +#define ERR_FRCSystem_NoDSConnection -44018 + +enum AllianceStationID_t { + kAllianceStationID_red1, + kAllianceStationID_red2, + kAllianceStationID_red3, + kAllianceStationID_blue1, + kAllianceStationID_blue2, + kAllianceStationID_blue3, +}; + +enum MatchType_t { + kMatchType_none, + kMatchType_practice, + kMatchType_qualification, + kMatchType_elimination, +}; + +struct ControlWord_t { + uint32_t enabled : 1; + uint32_t autonomous : 1; + uint32_t test : 1; + uint32_t eStop : 1; + uint32_t fmsAttached : 1; + uint32_t dsAttached : 1; + uint32_t control_reserved : 26; +}; + +struct JoystickAxes_t { + uint16_t count; + int16_t axes[1]; +}; + +struct JoystickPOV_t { + uint16_t count; + int16_t povs[1]; +}; + +#ifdef __cplusplus +extern "C" { +#endif +int EXPORT_FUNC FRC_NetworkCommunication_Reserve(void *instance); +#ifndef SIMULATION +void EXPORT_FUNC +getFPGAHardwareVersion(uint16_t *fpgaVersion, uint32_t *fpgaRevision); +#endif +int EXPORT_FUNC setStatusData(float battery, uint8_t dsDigitalOut, + uint8_t updateNumber, const char *userDataHigh, + int userDataHighLength, const char *userDataLow, + int userDataLowLength, int wait_ms); +int EXPORT_FUNC setErrorData(const char *errors, int errorsLength, int wait_ms); + +#ifdef SIMULATION +void EXPORT_FUNC setNewDataSem(HANDLE); +#else +void EXPORT_FUNC setNewDataSem(pthread_cond_t *); +#endif + +// this uint32_t is really a LVRefNum +int EXPORT_FUNC setNewDataOccurRef(uint32_t refnum); + +int EXPORT_FUNC +FRC_NetworkCommunication_getControlWord(struct ControlWord_t *controlWord); +int EXPORT_FUNC FRC_NetworkCommunication_getAllianceStation( + enum AllianceStationID_t *allianceStation); +int EXPORT_FUNC FRC_NetworkCommunication_getMatchTime(float *matchTime); +int EXPORT_FUNC +FRC_NetworkCommunication_getJoystickAxes(uint8_t joystickNum, + struct JoystickAxes_t *axes, + uint8_t maxAxes); +int EXPORT_FUNC FRC_NetworkCommunication_getJoystickButtons(uint8_t joystickNum, + uint32_t *buttons, + uint8_t *count); +int EXPORT_FUNC +FRC_NetworkCommunication_getJoystickPOVs(uint8_t joystickNum, + struct JoystickPOV_t *povs, + uint8_t maxPOVs); +int EXPORT_FUNC +FRC_NetworkCommunication_setJoystickOutputs(uint8_t joystickNum, + uint32_t hidOutputs, + uint16_t leftRumble, + uint16_t rightRumble); +int EXPORT_FUNC +FRC_NetworkCommunication_getJoystickDesc(uint8_t joystickNum, uint8_t *isXBox, + uint8_t *type, char *name, + uint8_t *axisCount, uint8_t *axisTypes, + uint8_t *buttonCount, + uint8_t *povCount); + +void EXPORT_FUNC FRC_NetworkCommunication_getVersionString(char *version); +int EXPORT_FUNC FRC_NetworkCommunication_observeUserProgramStarting(void); +void EXPORT_FUNC FRC_NetworkCommunication_observeUserProgramDisabled(void); +void EXPORT_FUNC FRC_NetworkCommunication_observeUserProgramAutonomous(void); +void EXPORT_FUNC FRC_NetworkCommunication_observeUserProgramTeleop(void); +void EXPORT_FUNC FRC_NetworkCommunication_observeUserProgramTest(void); +#ifdef __cplusplus +} +#endif + +#endif diff --git a/wpilibc/wpilibC++Sim/include/RobotBase.h b/wpilibc/wpilibC++Sim/include/RobotBase.h index 984b019ac1..a88dbb2f29 100644 --- a/wpilibc/wpilibC++Sim/include/RobotBase.h +++ b/wpilibc/wpilibC++Sim/include/RobotBase.h @@ -46,7 +46,7 @@ protected: RobotBase(const RobotBase&) = delete; RobotBase& operator=(const RobotBase&) = delete; - DriverStation *m_ds; + DriverStation &m_ds; private: static RobotBase *m_instance; diff --git a/wpilibc/wpilibC++Sim/src/DriverStation.cpp b/wpilibc/wpilibC++Sim/src/DriverStation.cpp index 9a0bf2034d..39802abe04 100644 --- a/wpilibc/wpilibC++Sim/src/DriverStation.cpp +++ b/wpilibc/wpilibC++Sim/src/DriverStation.cpp @@ -1,81 +1,227 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) FIRST 2008. All Rights Reserved. */ +/* 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 "Timer.h" -#include "simulation/MainNode.h" -//#include "MotorSafetyHelper.h" +#include "NetworkCommunication/FRCComm.h" +#include "MotorSafetyHelper.h" #include "Utility.h" #include "WPIErrors.h" #include #include "Log.hpp" -#include "boost/mem_fn.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) + if (level > dsLogLevel) ; \ + else Log().Get(level) -const uint32_t DriverStation::kBatteryChannel; const uint32_t DriverStation::kJoystickPorts; -const uint32_t DriverStation::kJoystickAxes; -const float DriverStation::kUpdatePeriod = 0.02; -uint8_t DriverStation::m_updateNumber = 0; /** - * DriverStation contructor. + * DriverStation constructor. * * This is only called once the first time GetInstance() is called */ DriverStation::DriverStation() { - state = msgs::DriverStationPtr(new msgs::DriverStation()); - stateSub = MainNode::Subscribe("~/ds/state", - &DriverStation::stateCallback, this); - // TODO: for loop + boost bind - joysticks[0] = msgs::FRCJoystickPtr(new msgs::FRCJoystick()); - joysticksSub[0] = MainNode::Subscribe("~/ds/joysticks/0", - &DriverStation::joystickCallback0, this); - joysticks[1] = msgs::FRCJoystickPtr(new msgs::FRCJoystick()); - joysticksSub[1] = MainNode::Subscribe("~/ds/joysticks/1", - &DriverStation::joystickCallback1, this); - joysticks[2] = msgs::FRCJoystickPtr(new msgs::FRCJoystick()); - joysticksSub[2] = MainNode::Subscribe("~/ds/joysticks/2", - &DriverStation::joystickCallback2, this); - joysticks[3] = msgs::FRCJoystickPtr(new msgs::FRCJoystick()); - joysticksSub[3] = MainNode::Subscribe("~/ds/joysticks/5", - &DriverStation::joystickCallback3, this); - joysticks[4] = msgs::FRCJoystickPtr(new msgs::FRCJoystick()); - joysticksSub[4] = MainNode::Subscribe("~/ds/joysticks/4", - &DriverStation::joystickCallback4, this); - joysticks[5] = msgs::FRCJoystickPtr(new msgs::FRCJoystick()); - joysticksSub[5] = MainNode::Subscribe("~/ds/joysticks/5", - &DriverStation::joystickCallback5, this); + // 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. + HALSetNewDataSem(m_packetDataAvailableCond.native_handle()); - AddToSingletonList(); + AddToSingletonList(); + +} + +void DriverStation::Run() { + int period = 0; + while (true) { + { + std::unique_lock lock(m_packetDataAvailableMutex); + m_packetDataAvailableCond.wait(lock); + } + GetData(); + m_waitForDataCond.notify_all(); + + 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. + * Return a reference to the singleton DriverStation. + * @return Pointer to the DS instance */ -DriverStation* DriverStation::GetInstance() -{ - static DriverStation instance; - return &instance; +DriverStation &DriverStation::GetInstance() { + static DriverStation instance; + return instance; } /** - * Read the battery voltage. Hardcoded to 12 volts for Simulation. + * 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. + */ +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]); + } + m_newControlData.give(); +} + +/** + * Read the battery voltage. * - * @return The battery voltage. + * @return The battery voltage in Volts. */ -float DriverStation::GetBatteryVoltage() const -{ - return 12.0; // 12 volts all the time! +float DriverStation::GetBatteryVoltage() const { + int32_t status = 0; + float voltage = getVinVoltage(&status); + wpi_setErrorWithContext(status, "getVinVoltage"); + + return voltage; +} + +/** + * Reports errors related to unplugged joysticks + * Throttles the errors so that they don't overwhelm the DS + */ +void DriverStation::ReportJoystickUnpluggedError(std::string message) { + double currentTime = Timer::GetFPGATimestamp(); + if (currentTime > m_nextMessageTime) { + ReportError(message); + m_nextMessageTime = currentTime + JOYSTICK_UNPLUGGED_MESSAGE_INTERVAL; + } +} + +/** + * Returns the number of axes on a given joystick port + * + * @param stick The joystick port number + * @return The number of axes on the indicated joystick + */ +int DriverStation::GetStickAxisCount(uint32_t stick) const { + if (stick >= kJoystickPorts) { + wpi_setWPIError(BadJoystickIndex); + return 0; + } + HALJoystickAxes joystickAxes; + HALGetJoystickAxes(stick, &joystickAxes); + return joystickAxes.count; +} + +/** + * Returns the name of the joystick at the given port + * + * @param stick The joystick port number + * @return The name of the joystick at the given port + */ +std::string DriverStation::GetJoystickName(uint32_t stick) const { + if (stick >= kJoystickPorts) { + wpi_setWPIError(BadJoystickIndex); + } + std::string retVal(m_joystickDescriptor[0].name); + return retVal; +} + +/** + * Returns the type of joystick at a given port + * + * @param stick The joystick port number + * @return The HID type of joystick at the given port + */ +int DriverStation::GetJoystickType(uint32_t stick) const { + if (stick >= kJoystickPorts) { + wpi_setWPIError(BadJoystickIndex); + return -1; + } + return (int)m_joystickDescriptor[stick].type; +} + +/** + * Returns a boolean indicating if the controller is an xbox controller. + * + * @param stick The joystick port number + * @return A boolean that is true if the controller is an xbox controller. + */ +bool DriverStation::GetJoystickIsXbox(uint32_t stick) const { + if (stick >= kJoystickPorts) { + wpi_setWPIError(BadJoystickIndex); + return false; + } + return (bool)m_joystickDescriptor[stick].isXbox; +} + +/** + * Returns the types of Axes on a given joystick port + * + * @param stick The joystick port number and the target axis + * @return What type of axis the axis is reporting to be + */ +int DriverStation::GetJoystickAxisType(uint32_t stick, uint8_t axis) const { + if (stick >= kJoystickPorts) { + wpi_setWPIError(BadJoystickIndex); + return -1; + } + return m_joystickDescriptor[stick].axisTypes[axis]; +} + +/** + * Returns the number of POVs on a given joystick port + * + * @param stick The joystick port number + * @return The number of POVs on the indicated joystick + */ +int DriverStation::GetStickPOVCount(uint32_t stick) const { + if (stick >= kJoystickPorts) { + wpi_setWPIError(BadJoystickIndex); + return 0; + } + HALJoystickPOVs joystickPOVs; + HALGetJoystickPOVs(stick, &joystickPOVs); + return joystickPOVs.count; +} + +/** + * Returns the number of buttons on a given joystick port + * + * @param stick The joystick port number + * @return The number of buttons on the indicated joystick + */ +int DriverStation::GetStickButtonCount(uint32_t stick) const { + if (stick >= kJoystickPorts) { + wpi_setWPIError(BadJoystickIndex); + return 0; + } + HALJoystickButtons joystickButtons; + HALGetJoystickButtons(stick, &joystickButtons); + return joystickButtons.count; } /** @@ -86,285 +232,287 @@ float DriverStation::GetBatteryVoltage() const * @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 (axis < 0 || axis > (kJoystickAxes - 1)) - { - wpi_setWPIError(BadJoystickAxis); - return 0.0; - } - if (stick < 0 || stick > 5) - { - wpi_setWPIError(BadJoystickIndex); - return 0.0; - } +float DriverStation::GetStickAxis(uint32_t stick, uint32_t axis) { + if (stick >= kJoystickPorts) { + wpi_setWPIError(BadJoystickIndex); + return 0; + } - std::unique_lock lock(m_joystickMutex); - if (joysticks[stick] == nullptr || axis >= joysticks[stick]->axes().size()) - { - return 0.0; - } - return joysticks[stick]->axes(axis); + 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; + } } /** - * The state of a specific button (1 - 12) on the joystick. - * This method only works in simulation, but is more efficient than GetStickButtons. + * Get the state of a POV on the joystick. * - * @param stick The joystick to read. - * @param button The button number to check. - * @return If the button is pressed. + * @return the angle of the POV in degrees, or -1 if the POV is not pressed. */ -bool DriverStation::GetStickButton(uint32_t stick, uint32_t button) -{ - if (stick < 0 || stick >= 6) - { - wpi_setWPIErrorWithContext(ParameterOutOfRange, "stick must be between 0 and 5"); - return false; - } +int DriverStation::GetStickPOV(uint32_t stick, uint32_t pov) { + if (stick >= kJoystickPorts) { + wpi_setWPIError(BadJoystickIndex); + return -1; + } - std::unique_lock lock(m_joystickMutex); - if (joysticks[stick] == nullptr || button >= joysticks[stick]->buttons().size()) - { - return false; - } - return joysticks[stick]->buttons(button-1); + 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; + } + + return m_joystickPOVs[stick].povs[pov]; } /** * The state of the buttons on the joystick. - * 12 buttons (4 msb are unused) from the joystick. * * @param stick The joystick to read. * @return The state of the buttons on the joystick. */ -short DriverStation::GetStickButtons(uint32_t stick) -{ - if (stick < 0 || stick >= 6) - { - wpi_setWPIErrorWithContext(ParameterOutOfRange, "stick must be between 0 and 5"); - return false; - } - short btns = 0, btnid; +uint32_t DriverStation::GetStickButtons(uint32_t stick) const { + if (stick >= kJoystickPorts) { + wpi_setWPIError(BadJoystickIndex); + return 0; + } - std::unique_lock lock(m_joystickMutex); - msgs::FRCJoystickPtr joy = joysticks[stick]; - for (btnid = 0; btnid < joy->buttons().size() && btnid < 12; btnid++) - { - if (joysticks[stick]->buttons(btnid)) - { - btns |= (1 << btnid); - } - } - return btns; + return m_joystickButtons[stick].buttons; } -// 5V divided by 10 bits -#define kDSAnalogInScaling ((float)(5.0 / 1023.0)) - /** - * Get an analog voltage from the Driver Station. - * The analog values are returned as voltage values for the Driver Station analog inputs. - * These inputs are typically used for advanced operator interfaces consisting of potentiometers - * or resistor networks representing values on a rotary switch. + * The state of one joystick button. Button indexes begin at 1. * - * @param channel The analog input channel on the driver station to read from. Valid range is 1 - 4. - * @return The analog voltage on the input. + * @param stick The joystick to read. + * @param button The button index, beginning at 1. + * @return The state of the joystick button. */ -float DriverStation::GetAnalogIn(uint32_t channel) -{ - wpi_setWPIErrorWithContext(UnsupportedInSimulation, "GetAnalogIn"); - return 0.0; +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; } /** - * Get values from the digital inputs on the Driver Station. - * Return digital values from the Drivers Station. These values are typically used for buttons - * and switches on advanced operator interfaces. - * @param channel The digital input to get. Valid range is 1 - 8. + * Check if the DS has enabled the robot + * @return True if the robot is enabled and the DS is connected */ -bool DriverStation::GetDigitalIn(uint32_t channel) -{ - wpi_setWPIErrorWithContext(UnsupportedInSimulation, "GetDigitalIn"); - return false; +bool DriverStation::IsEnabled() const { + HALControlWord controlWord; + memset(&controlWord, 0, sizeof(controlWord)); + HALGetControlWord(&controlWord); + return controlWord.enabled && controlWord.dsAttached; } /** - * Set a value for the digital outputs on the Driver Station. - * - * Control digital outputs on the Drivers Station. These values are typically used for - * giving feedback on a custom operator station such as LEDs. - * - * @param channel The digital output to set. Valid range is 1 - 8. - * @param value The state to set the digital output. + * Check if the robot is disabled + * @return True if the robot is explicitly disabled or the DS is not connected */ -void DriverStation::SetDigitalOut(uint32_t channel, bool value) -{ - wpi_setWPIErrorWithContext(UnsupportedInSimulation, "SetDigitalOut"); +bool DriverStation::IsDisabled() const { + HALControlWord controlWord; + memset(&controlWord, 0, sizeof(controlWord)); + HALGetControlWord(&controlWord); + return !(controlWord.enabled && controlWord.dsAttached); } /** - * Get a value that was set for the digital outputs on the Driver Station. - * @param channel The digital ouput to monitor. Valid range is 1 through 8. - * @return A digital value being output on the Drivers Station. + * Check if the DS is commanding autonomous mode + * @return True if the robot is being commanded to be in autonomous mode */ -bool DriverStation::GetDigitalOut(uint32_t channel) -{ - wpi_setWPIErrorWithContext(UnsupportedInSimulation, "GetDigitalOut"); - return false; +bool DriverStation::IsAutonomous() const { + HALControlWord controlWord; + memset(&controlWord, 0, sizeof(controlWord)); + HALGetControlWord(&controlWord); + return controlWord.autonomous; } -bool DriverStation::IsEnabled() const -{ - std::unique_lock lock(m_stateMutex); - return state != nullptr ? state->enabled() : false; +/** + * Check if the DS is commanding teleop mode + * @return True if the robot is being commanded to be in teleop mode + */ +bool DriverStation::IsOperatorControl() const { + HALControlWord controlWord; + memset(&controlWord, 0, sizeof(controlWord)); + HALGetControlWord(&controlWord); + return !(controlWord.autonomous || controlWord.test); } -bool DriverStation::IsDisabled() const -{ - return !IsEnabled(); +/** + * Check if the DS is commanding test mode + * @return True if the robot is being commanded to be in test mode + */ +bool DriverStation::IsTest() const { + HALControlWord controlWord; + HALGetControlWord(&controlWord); + return controlWord.test; } -bool DriverStation::IsAutonomous() const -{ - std::unique_lock lock(m_stateMutex); - return state != nullptr ? - state->state() == msgs::DriverStation_State_AUTO : false; +/** + * Check if the DS is attached + * @return True if the DS is connected to the robot + */ +bool DriverStation::IsDSAttached() const { + HALControlWord controlWord; + memset(&controlWord, 0, sizeof(controlWord)); + HALGetControlWord(&controlWord); + return controlWord.dsAttached; } -bool DriverStation::IsOperatorControl() const -{ - return !(IsAutonomous() || IsTest()); +/** + * @return always true in simulation + */ +bool DriverStation::IsSysActive() const { + return true; } -bool DriverStation::IsTest() const -{ - std::unique_lock lock(m_stateMutex); - return state != nullptr ? - state->state() == msgs::DriverStation_State_TEST : false; +/** + * @return always false in simulation + */ +bool DriverStation::IsSysBrownedOut() const { + return false; +} + +/** + * 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 behaviour. + * @return True if the control data has been updated since the last call. + */ +bool DriverStation::IsNewControlData() const { + return m_newControlData.tryTake() == false; } /** * 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 + * @return True if the robot is competing on a field being controlled by a Field + * Management System */ -bool DriverStation::IsFMSAttached() const -{ - return false; // No FMS in simulation +bool DriverStation::IsFMSAttached() const { + 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 + * @return The Alliance enum (kRed, kBlue or kInvalid) */ -DriverStation::Alliance DriverStation::GetAlliance() const -{ - // if (m_controlData->dsID_Alliance == 'R') return kRed; - // if (m_controlData->dsID_Alliance == 'B') return kBlue; - // wpi_assert(false); - return kInvalid; // TODO: Support alliance colors +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; + } } /** * Return the driver station location on the field * This could return 1, 2, or 3 - * @return The location of the driver station + * @return The location of the driver station (1-3, 0 for invalid) */ -uint32_t DriverStation::GetLocation() const -{ - return -1; // TODO: Support locations +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; + } } /** * 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 + * This is a good way to delay processing until there is new driver station data + * to act on */ -void DriverStation::WaitForData() -{ - std::unique_lock lock(m_waitForDataMutex); - m_waitForDataCond.wait(lock); +void DriverStation::WaitForData() { + std::unique_lock lock(m_waitForDataMutex); + m_waitForDataCond.wait(lock); } /** * 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 + * 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 + * will trigger before the match ends) + * The Practice Match function of the DS approximates the behaviour seen on the + * field. + * @return Time remaining in current match period (auto or teleop) */ -double DriverStation::GetMatchTime() const -{ - if (m_approxMatchTimeOffset < 0.0) - return 0.0; - return Timer::GetFPGATimestamp() - m_approxMatchTimeOffset; +double DriverStation::GetMatchTime() const { + float matchTime; + HALGetMatchTime(&matchTime); + return (double)matchTime; } /** * 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; -} +void DriverStation::ReportError(std::string error) { + std::cout << error << std::endl; -/** - * Return the team number that the Driver Station is configured for - * @return The team number - */ -uint16_t DriverStation::GetTeamNumber() const -{ - return 348; -} - -void DriverStation::stateCallback(const msgs::ConstDriverStationPtr &msg) -{ - { - std::unique_lock lock(m_stateMutex); - *state = *msg; - } - m_waitForDataCond.notify_all(); -} - -void DriverStation::joystickCallback(const msgs::ConstFRCJoystickPtr &msg, - int i) -{ - std::unique_lock lock(m_joystickMutex); - *(joysticks[i]) = *msg; -} - -void DriverStation::joystickCallback0(const msgs::ConstFRCJoystickPtr &msg) -{ - joystickCallback(msg, 0); -} - -void DriverStation::joystickCallback1(const msgs::ConstFRCJoystickPtr &msg) -{ - joystickCallback(msg, 1); -} - -void DriverStation::joystickCallback2(const msgs::ConstFRCJoystickPtr &msg) -{ - joystickCallback(msg, 2); -} - -void DriverStation::joystickCallback3(const msgs::ConstFRCJoystickPtr &msg) -{ - joystickCallback(msg, 3); -} - -void DriverStation::joystickCallback4(const msgs::ConstFRCJoystickPtr &msg) -{ - joystickCallback(msg, 4); -} - -void DriverStation::joystickCallback5(const msgs::ConstFRCJoystickPtr &msg) -{ - joystickCallback(msg, 5); + HALControlWord controlWord; + HALGetControlWord(&controlWord); + if (controlWord.dsAttached) { + HALSetErrorData(error.c_str(), error.size(), 0); + } } diff --git a/wpilibc/wpilibC++Sim/src/IterativeRobot.cpp b/wpilibc/wpilibC++Sim/src/IterativeRobot.cpp index 4c3863bca7..e18d40be15 100644 --- a/wpilibc/wpilibC++Sim/src/IterativeRobot.cpp +++ b/wpilibc/wpilibC++Sim/src/IterativeRobot.cpp @@ -164,7 +164,7 @@ void IterativeRobot::StartCompetition() } } // wait for driver station data so the loop doesn't hog the CPU - m_ds->WaitForData(); + m_ds.WaitForData(); } } diff --git a/wpilibc/wpilibC++Sim/src/Joystick.cpp b/wpilibc/wpilibC++Sim/src/Joystick.cpp index 6344d2417d..a7a299ced8 100644 --- a/wpilibc/wpilibC++Sim/src/Joystick.cpp +++ b/wpilibc/wpilibC++Sim/src/Joystick.cpp @@ -1,14 +1,16 @@ /*----------------------------------------------------------------------------*/ -/* Copyright (c) FIRST 2008. All Rights Reserved. */ +/* 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 "Joystick.h" #include "DriverStation.h" +//#include "NetworkCommunication/UsageReporting.h" #include "WPIErrors.h" #include -#include +#include const uint32_t Joystick::kDefaultXAxis; const uint32_t Joystick::kDefaultYAxis; @@ -24,19 +26,21 @@ static bool joySticksInitialized = false; * Construct an instance of a joystick. * The joystick index is the usb port on the drivers station. * - * @param port The port on the driver station that the joystick is plugged into. + * @param port The port on the driver station that the joystick is plugged into + * (0-5). */ Joystick::Joystick(uint32_t port) - : Joystick(port, kNumAxisTypes, kNumButtonTypes) -{ - m_axes[kXAxis] = kDefaultXAxis; - m_axes[kYAxis] = kDefaultYAxis; - m_axes[kZAxis] = kDefaultZAxis; - m_axes[kTwistAxis] = kDefaultTwistAxis; - m_axes[kThrottleAxis] = kDefaultThrottleAxis; + : Joystick(port, kNumAxisTypes, kNumButtonTypes) { + m_axes[kXAxis] = kDefaultXAxis; + m_axes[kYAxis] = kDefaultYAxis; + m_axes[kZAxis] = kDefaultZAxis; + m_axes[kTwistAxis] = kDefaultTwistAxis; + m_axes[kThrottleAxis] = kDefaultThrottleAxis; - m_buttons[kTriggerButton] = kDefaultTriggerButton; - m_buttons[kTopButton] = kDefaultTopButton; + m_buttons[kTriggerButton] = kDefaultTriggerButton; + m_buttons[kTopButton] = kDefaultTopButton; + + HALReport(HALUsageReporting::kResourceType_Joystick, port); } /** @@ -49,111 +53,108 @@ Joystick::Joystick(uint32_t port) * @param numAxisTypes The number of axis types in the enum. * @param numButtonTypes The number of button types in the enum. */ -Joystick::Joystick(uint32_t port, uint32_t numAxisTypes, uint32_t numButtonTypes) - : m_port (port) -{ - if ( !joySticksInitialized ) - { - for (unsigned i = 0; i < DriverStation::kJoystickPorts; i++) - joysticks[i] = nullptr; - joySticksInitialized = true; - } - joysticks[m_port] = this; - - m_ds = DriverStation::GetInstance(); - m_axes = std::make_unique(numAxisTypes); - m_buttons = std::make_unique(numButtonTypes); +Joystick::Joystick(uint32_t port, uint32_t numAxisTypes, + uint32_t numButtonTypes) + : m_ds(DriverStation::GetInstance()), + m_port(port), + m_axes(numAxisTypes), + m_buttons(numButtonTypes) { + if (!joySticksInitialized) { + for (auto& joystick : joysticks) joystick = nullptr; + joySticksInitialized = true; + } + if (m_port >= DriverStation::kJoystickPorts) { + wpi_setWPIError(BadJoystickIndex); + } else { + joysticks[m_port] = this; + } } -Joystick * Joystick::GetStickForPort(uint32_t port) -{ - Joystick *stick = joysticks[port]; - if (stick == nullptr) - { - stick = new Joystick(port); - joysticks[port] = stick; - } - return stick; +Joystick *Joystick::GetStickForPort(uint32_t port) { + Joystick *stick = joysticks[port]; + if (stick == nullptr) { + stick = new Joystick(port); + joysticks[port] = stick; + } + return stick; } /** * Get the X value of the joystick. * This depends on the mapping of the joystick connected to the current port. + * @param hand This parameter is ignored for the Joystick class and is only here + * to complete the GenericHID interface. */ -float Joystick::GetX(JoystickHand hand) const -{ - return GetRawAxis(m_axes[kXAxis]); +float Joystick::GetX(JoystickHand hand) const { + return GetRawAxis(m_axes[kXAxis]); } /** * Get the Y value of the joystick. * This depends on the mapping of the joystick connected to the current port. + * @param hand This parameter is ignored for the Joystick class and is only here + * to complete the GenericHID interface. */ -float Joystick::GetY(JoystickHand hand) const -{ - return GetRawAxis(m_axes[kYAxis]); +float Joystick::GetY(JoystickHand hand) const { + return GetRawAxis(m_axes[kYAxis]); } /** * Get the Z value of the current joystick. * This depends on the mapping of the joystick connected to the current port. */ -float Joystick::GetZ() const -{ - return GetRawAxis(m_axes[kZAxis]); -} +float Joystick::GetZ() const { return GetRawAxis(m_axes[kZAxis]); } /** * Get the twist value of the current joystick. * This depends on the mapping of the joystick connected to the current port. */ -float Joystick::GetTwist() const -{ - return GetRawAxis(m_axes[kTwistAxis]); -} +float Joystick::GetTwist() const { return GetRawAxis(m_axes[kTwistAxis]); } /** * Get the throttle value of the current joystick. * This depends on the mapping of the joystick connected to the current port. */ -float Joystick::GetThrottle() const -{ - return GetRawAxis(m_axes[kThrottleAxis]); +float Joystick::GetThrottle() const { + return GetRawAxis(m_axes[kThrottleAxis]); } /** * Get the value of the axis. * - * @param axis The axis to read [1-6]. + * @param axis The axis to read, starting at 0. * @return The value of the axis. */ -float Joystick::GetRawAxis(uint32_t axis) const -{ - return m_ds->GetStickAxis(m_port, axis); +float Joystick::GetRawAxis(uint32_t axis) const { + return m_ds.GetStickAxis(m_port, axis); } /** * For the current joystick, return the axis determined by the argument. * - * This is for cases where the joystick axis is returned programatically, otherwise one of the + * This is for cases where the joystick axis is returned programatically, + * otherwise one of the * previous functions would be preferable (for example GetX()). * * @param axis The axis to read. * @return The value of the axis. */ -float Joystick::GetAxis(AxisType axis) const -{ - switch(axis) - { - case kXAxis: return this->GetX(); - case kYAxis: return this->GetY(); - case kZAxis: return this->GetZ(); - case kTwistAxis: return this->GetTwist(); - case kThrottleAxis: return this->GetThrottle(); - default: - wpi_setWPIError(BadJoystickAxis); - return 0.0; - } +float Joystick::GetAxis(AxisType axis) const { + switch (axis) { + case kXAxis: + return this->GetX(); + case kYAxis: + return this->GetY(); + case kZAxis: + return this->GetZ(); + case kTwistAxis: + return this->GetTwist(); + case kThrottleAxis: + return this->GetThrottle(); + default: + wpi_setWPIError(BadJoystickAxis); + return 0.0; + } } /** @@ -161,12 +162,12 @@ float Joystick::GetAxis(AxisType axis) const * * Look up which button has been assigned to the trigger and read its state. * - * @param hand This parameter is ignored for the Joystick class and is only here to complete the GenericHID interface. + * @param hand This parameter is ignored for the Joystick class and is only here + * to complete the GenericHID interface. * @return The state of the trigger. */ -bool Joystick::GetTrigger(JoystickHand hand) const -{ - return GetRawButton(m_buttons[kTriggerButton]); +bool Joystick::GetTrigger(JoystickHand hand) const { + return GetRawButton(m_buttons[kTriggerButton]); } /** @@ -174,45 +175,45 @@ bool Joystick::GetTrigger(JoystickHand hand) const * * Look up which button has been assigned to the top and read its state. * - * @param hand This parameter is ignored for the Joystick class and is only here to complete the GenericHID interface. + * @param hand This parameter is ignored for the Joystick class and is only here + * to complete the GenericHID interface. * @return The state of the top button. */ -bool Joystick::GetTop(JoystickHand hand) const -{ - return GetRawButton(m_buttons[kTopButton]); +bool Joystick::GetTop(JoystickHand hand) const { + return GetRawButton(m_buttons[kTopButton]); } /** * This is not supported for the Joystick. * This method is only here to complete the GenericHID interface. */ -bool Joystick::GetBumper(JoystickHand hand) const -{ - // Joysticks don't have bumpers. - return false; +bool Joystick::GetBumper(JoystickHand hand) const { + // Joysticks don't have bumpers. + return false; } /** - * Get the button value for buttons 1 through 12. + * Get the button value (starting at button 1) * - * The buttons are returned in a single 16 bit value with one bit representing the state + * The buttons are returned in a single 16 bit value with one bit representing + * the state * of each button. The appropriate button is returned as a boolean value. * - * @param button The button number to be read. + * @param button The button number to be read (starting at 1) * @return The state of the button. **/ -bool Joystick::GetRawButton(uint32_t button) const -{ - return m_ds->GetStickButton(m_port, button); +bool Joystick::GetRawButton(uint32_t button) const { + return m_ds.GetStickButton(m_port, button); } /** -* 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. -*/ + * Get the state of a POV on the joystick. + * + * @param pov The index of the POV to read (starting at 0) + * @return the angle of the POV in degrees, or -1 if the POV is not pressed. + */ int Joystick::GetPOV(uint32_t pov) const { - return 0; // TODO + return m_ds.GetStickPOV(m_port, pov); } /** @@ -223,27 +224,75 @@ int Joystick::GetPOV(uint32_t pov) const { * @param button The type of button to read. * @return The state of the button. */ -bool Joystick::GetButton(ButtonType button) const -{ - switch (button) - { - case kTriggerButton: return GetTrigger(); - case kTopButton: return GetTop(); - default: - return false; - } +bool Joystick::GetButton(ButtonType button) const { + switch (button) { + case kTriggerButton: + return GetTrigger(); + case kTopButton: + return GetTop(); + default: + return false; + } } +/** + * Get the number of axis for a joystick + * + * @return the number of axis for the current joystick + */ +int Joystick::GetAxisCount() const { return m_ds.GetStickAxisCount(m_port); } + +/** + * Get the value of isXbox for the joystick. + * + * @return A boolean that is true if the joystick is an xbox controller. + */ +bool Joystick::GetIsXbox() const { return m_ds.GetJoystickIsXbox(m_port); } + +/** + * Get the HID type of the controller. + * + * @return the HID type of the controller. + */ +Joystick::HIDType Joystick::GetType() const { + return static_cast(m_ds.GetJoystickType(m_port)); +} + +/** + * Get the name of the joystick. + * + * @return the name of the controller. + */ +std::string Joystick::GetName() const { return m_ds.GetJoystickName(m_port); } + +// int Joystick::GetAxisType(uint8_t axis) const +//{ +// return m_ds.GetJoystickAxisType(m_port, axis); +//} + +/** + * Get the number of axis for a joystick + * +* @return the number of buttons on the current joystick + */ +int Joystick::GetButtonCount() const { + return m_ds.GetStickButtonCount(m_port); +} + +/** + * Get the number of axis for a joystick + * + * @return then umber of POVs for the current joystick + */ +int Joystick::GetPOVCount() const { return m_ds.GetStickPOVCount(m_port); } + /** * Get the channel currently associated with the specified axis. * * @param axis The axis to look up the channel for. * @return The channel fr the axis. */ -uint32_t Joystick::GetAxisChannel(AxisType axis) -{ - return m_axes[axis]; -} +uint32_t Joystick::GetAxisChannel(AxisType axis) const { return m_axes[axis]; } /** * Set the channel associated with a specified axis. @@ -251,9 +300,8 @@ uint32_t Joystick::GetAxisChannel(AxisType axis) * @param axis The axis to set the channel for. * @param channel The channel to set the axis to. */ -void Joystick::SetAxisChannel(AxisType axis, uint32_t channel) -{ - m_axes[axis] = channel; +void Joystick::SetAxisChannel(AxisType axis, uint32_t channel) { + m_axes[axis] = channel; } /** @@ -263,7 +311,7 @@ void Joystick::SetAxisChannel(AxisType axis, uint32_t channel) * @return The magnitude of the direction vector */ float Joystick::GetMagnitude() const { - return sqrt(pow(GetX(),2) + pow(GetY(),2) ); + return sqrt(pow(GetX(), 2) + pow(GetY(), 2)); } /** @@ -272,19 +320,58 @@ float Joystick::GetMagnitude() const { * * @return The direction of the vector in radians */ -float Joystick::GetDirectionRadians() const { - return atan2(GetX(), -GetY()); -} +float Joystick::GetDirectionRadians() const { return atan2(GetX(), -GetY()); } /** * Get the direction of the vector formed by the joystick and its origin * in degrees * - * uses acos(-1) to represent Pi due to absence of readily accessable Pi + * uses acos(-1) to represent Pi due to absence of readily accessible Pi * constant in C++ * * @return The direction of the vector in degrees */ float Joystick::GetDirectionDegrees() const { - return (180/acos(-1))*GetDirectionRadians(); + return (180 / acos(-1)) * GetDirectionRadians(); +} + +/** + * Set the rumble output for the joystick. The DS currently supports 2 rumble + * values, + * left rumble and right rumble + * @param type Which rumble value to set + * @param value The normalized value (0 to 1) to set the rumble to + */ +void Joystick::SetRumble(RumbleType type, float value) { + if (value < 0) + value = 0; + else if (value > 1) + value = 1; + if (type == kLeftRumble) + m_leftRumble = value * 65535; + else + m_rightRumble = value * 65535; + HALSetJoystickOutputs(m_port, m_outputs, m_leftRumble, m_rightRumble); +} + +/** + * Set a single HID output value for the joystick. + * @param outputNumber The index of the output to set (1-32) + * @param value The value to set the output to + */ + +void Joystick::SetOutput(uint8_t outputNumber, bool value) { + m_outputs = + (m_outputs & ~(1 << (outputNumber - 1))) | (value << (outputNumber - 1)); + + HALSetJoystickOutputs(m_port, m_outputs, m_leftRumble, m_rightRumble); +} + +/** + * Set all HID output values for the joystick. + * @param value The 32 bit output value (1 bit for each output) + */ +void Joystick::SetOutputs(uint32_t value) { + m_outputs = value; + HALSetJoystickOutputs(m_port, m_outputs, m_leftRumble, m_rightRumble); } diff --git a/wpilibc/wpilibC++Sim/src/MotorSafetyHelper.cpp b/wpilibc/wpilibC++Sim/src/MotorSafetyHelper.cpp index e0aab7d551..fa1b7096e9 100644 --- a/wpilibc/wpilibC++Sim/src/MotorSafetyHelper.cpp +++ b/wpilibc/wpilibC++Sim/src/MotorSafetyHelper.cpp @@ -91,9 +91,10 @@ bool MotorSafetyHelper::IsAlive() const { * its value is * updated again. */ -void MotorSafetyHelper::Check() { - DriverStation* ds = DriverStation::GetInstance(); - if (!m_enabled || ds->IsDisabled() || ds->IsTest()) return; +void MotorSafetyHelper::Check() +{ + DriverStation &ds = DriverStation::GetInstance(); + if (!m_enabled || ds.IsDisabled() || ds.IsTest()) return; std::unique_lock sync(m_syncMutex); if (m_stopTime < Timer::GetFPGATimestamp()) { diff --git a/wpilibc/wpilibC++Sim/src/RobotBase.cpp b/wpilibc/wpilibC++Sim/src/RobotBase.cpp index 3e0b8f88be..1fa5ac74e5 100644 --- a/wpilibc/wpilibC++Sim/src/RobotBase.cpp +++ b/wpilibc/wpilibC++Sim/src/RobotBase.cpp @@ -31,10 +31,9 @@ RobotBase &RobotBase::getInstance() * This must be used to ensure that the communications code starts. In the future it would be * nice to put this code into it's own task that loads on boot so ensure that it runs. */ -RobotBase::RobotBase() +RobotBase::RobotBase() : m_ds(DriverStation::GetInstance()) { - m_ds = DriverStation::GetInstance(); - RobotState::SetImplementation(*DriverStation::GetInstance()); + RobotState::SetImplementation(DriverStation::GetInstance()); transport::SubscriberPtr time_pub = MainNode::Subscribe("time", &wpilib::internal::time_callback); } @@ -44,7 +43,7 @@ RobotBase::RobotBase() */ bool RobotBase::IsEnabled() const { - return m_ds->IsEnabled(); + return m_ds.IsEnabled(); } /** @@ -53,7 +52,7 @@ bool RobotBase::IsEnabled() const */ bool RobotBase::IsDisabled() const { - return m_ds->IsDisabled(); + return m_ds.IsDisabled(); } /** @@ -62,7 +61,7 @@ bool RobotBase::IsDisabled() const */ bool RobotBase::IsAutonomous() const { - return m_ds->IsAutonomous(); + return m_ds.IsAutonomous(); } /** @@ -71,7 +70,7 @@ bool RobotBase::IsAutonomous() const */ bool RobotBase::IsOperatorControl() const { - return m_ds->IsOperatorControl(); + return m_ds.IsOperatorControl(); } /** @@ -80,18 +79,9 @@ bool RobotBase::IsOperatorControl() const */ bool RobotBase::IsTest() const { - return m_ds->IsTest(); + return m_ds.IsTest(); } -/** - * Indicates if new data is available from the driver station. - * @return Has new data arrived over the network since the last time this function was called? - */ -// bool RobotBase::IsNewDataAvailable() -// { -// return m_ds->IsNewControlData(); -// } - /** * This class exists for the sole purpose of getting its destructor called when the module unloads. * Before the module is done unloading, we need to delete the RobotBase derived singleton. This should delete diff --git a/wpilibc/wpilibC++Sim/src/SampleRobot.cpp b/wpilibc/wpilibC++Sim/src/SampleRobot.cpp index 366045cfe6..91a80c70b6 100644 --- a/wpilibc/wpilibC++Sim/src/SampleRobot.cpp +++ b/wpilibc/wpilibC++Sim/src/SampleRobot.cpp @@ -124,33 +124,33 @@ void SampleRobot::StartCompetition() { if (IsDisabled()) { - m_ds->InDisabled(true); + m_ds.InDisabled(true); Disabled(); - m_ds->InDisabled(false); - while (IsDisabled()) sleep(1); //m_ds->WaitForData(); + m_ds.InDisabled(false); + while (IsDisabled()) sleep(1); //m_ds.WaitForData(); } else if (IsAutonomous()) { - m_ds->InAutonomous(true); + m_ds.InAutonomous(true); Autonomous(); - m_ds->InAutonomous(false); - while (IsAutonomous() && IsEnabled()) sleep(1); //m_ds->WaitForData(); + m_ds.InAutonomous(false); + while (IsAutonomous() && IsEnabled()) sleep(1); //m_ds.WaitForData(); } else if (IsTest()) { lw.SetEnabled(true); - m_ds->InTest(true); - Test(); - m_ds->InTest(false); - while (IsTest() && IsEnabled()) sleep(1); //m_ds->WaitForData(); + m_ds.InTest(true); + Test(); + m_ds.InTest(false); + while (IsTest() && IsEnabled()) sleep(1); //m_ds.WaitForData(); lw.SetEnabled(false); } else { - m_ds->InOperatorControl(true); + m_ds.InOperatorControl(true); OperatorControl(); - m_ds->InOperatorControl(false); - while (IsOperatorControl() && IsEnabled()) sleep(1); //m_ds->WaitForData(); + m_ds.InOperatorControl(false); + while (IsOperatorControl() && IsEnabled()) sleep(1); //m_ds.WaitForData(); } } } diff --git a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/DriverStation.java b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/DriverStation.java index b226755c3d..b047e3f108 100644 --- a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/DriverStation.java +++ b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/DriverStation.java @@ -123,7 +123,7 @@ public class DriverStation implements RobotState.Interface { private void task() { int safetyCounter = 0; while (m_thread_keepalive) { - HALUtil.takeMultiWait(m_packetDataAvailableSem, m_packetDataAvailableMutex, 0); + HALUtil.takeMultiWait(m_packetDataAvailableSem, m_packetDataAvailableMutex); synchronized (this) { getData(); } diff --git a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/hal/HALUtil.java b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/hal/HALUtil.java index e1d6b8d9bf..ac00a004fd 100644 --- a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/hal/HALUtil.java +++ b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/hal/HALUtil.java @@ -30,7 +30,7 @@ public class HALUtil extends JNIWrapper { public static native void deleteMultiWait(ByteBuffer sem); - public static native byte takeMultiWait(ByteBuffer sem, ByteBuffer m, int timeOut); + public static native byte takeMultiWait(ByteBuffer sem, ByteBuffer m); public static native short getFPGAVersion(IntBuffer status); diff --git a/wpilibj/wpilibJavaJNI/lib/FRCNetworkCommunicationsLibrary.cpp b/wpilibj/wpilibJavaJNI/lib/FRCNetworkCommunicationsLibrary.cpp index 5c2297c6c9..7f0024db5b 100644 --- a/wpilibj/wpilibJavaJNI/lib/FRCNetworkCommunicationsLibrary.cpp +++ b/wpilibj/wpilibJavaJNI/lib/FRCNetworkCommunicationsLibrary.cpp @@ -399,9 +399,9 @@ JNIEXPORT jstring JNICALL Java_edu_wpi_first_wpilibj_communication_FRCNetworkCom JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_communication_FRCNetworkCommunicationsLibrary_setNewDataSem (JNIEnv * env, jclass, jobject id ) { - MULTIWAIT_ID* javaId = (MULTIWAIT_ID*)env->GetDirectBufferAddress(id); - NETCOMM_LOG(logDEBUG) << "Mutex Ptr = " << *javaId; - HALSetNewDataSem(*javaId); + MULTIWAIT_ID javaId = (MULTIWAIT_ID)env->GetDirectBufferAddress(id); + NETCOMM_LOG(logDEBUG) << "Mutex Ptr = " << javaId; + HALSetNewDataSem(javaId->native_handle()); } /* @@ -640,11 +640,11 @@ JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_communication_FRCNetworkCommun { const char * errorStr = env->GetStringUTFChars(error, NULL); jsize length = env->GetStringUTFLength(error); - + NETCOMM_LOG(logDEBUG) << "Set Error: " << errorStr; NETCOMM_LOG(logDEBUG) << "Length: " << length; jint returnValue = HALSetErrorData(errorStr, (jint) length, 0); env->ReleaseStringUTFChars(error,errorStr); return returnValue; } - + diff --git a/wpilibj/wpilibJavaJNI/lib/HALUtil.cpp b/wpilibj/wpilibJavaJNI/lib/HALUtil.cpp index b9a46f7027..c6e19db0e9 100644 --- a/wpilibj/wpilibJavaJNI/lib/HALUtil.cpp +++ b/wpilibj/wpilibJavaJNI/lib/HALUtil.cpp @@ -10,8 +10,8 @@ TLogLevel halUtilLogLevel = logWARNING; #define HALUTIL_LOG(level) \ - if (level > halUtilLogLevel) ; \ - else Log().Get(level) + if (level > halUtilLogLevel) ; \ + else Log().Get(level) // @@ -19,9 +19,9 @@ TLogLevel halUtilLogLevel = logWARNING; // JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { - // set our logging level - Log::ReportingLevel() = logDEBUG; - return JNI_VERSION_1_6; + // set our logging level + Log::ReportingLevel() = logDEBUG; + return JNI_VERSION_1_6; } /* @@ -29,14 +29,14 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) * Method: initializeMutex * Signature: (I)Ljava/nio/ByteBuffer; */ -JNIEXPORT jobject JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_initializeMutexNormal + JNIEXPORT jobject JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_initializeMutexNormal (JNIEnv * env, jclass) { - HALUTIL_LOG(logDEBUG) << "Calling HALUtil initializeMutex"; - MUTEX_ID* mutexPtr = (MUTEX_ID*)new unsigned char[sizeof(MUTEX_ID)]; - *mutexPtr = initializeMutexNormal(); - HALUTIL_LOG(logDEBUG) << "Mutex Ptr = " << *mutexPtr; - return env->NewDirectByteBuffer(mutexPtr, sizeof(MUTEX_ID)); + HALUTIL_LOG(logDEBUG) << "Calling HALUtil initializeMutex"; + MUTEX_ID mutexPtr = (MUTEX_ID)new unsigned char[sizeof(MUTEX_ID)]; + mutexPtr = initializeMutexNormal(); + HALUTIL_LOG(logDEBUG) << "Mutex Ptr = " << mutexPtr; + return env->NewDirectByteBuffer(mutexPtr, sizeof(MUTEX_ID)); } /* @@ -44,14 +44,14 @@ JNIEXPORT jobject JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_initializeMutex * Method: deleteMutex * Signature: (Ljava/nio/ByteBuffer;)V */ -JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_deleteMutex + JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_deleteMutex (JNIEnv * env, jclass, jobject id ) { - HALUTIL_LOG(logDEBUG) << "Calling HALUtil deleteMutex"; - MUTEX_ID* javaId = (MUTEX_ID*)env->GetDirectBufferAddress(id); - HALUTIL_LOG(logDEBUG) << "Mutex Ptr = " << *javaId; - deleteMutex( *javaId ); - delete[] javaId; + HALUTIL_LOG(logDEBUG) << "Calling HALUtil deleteMutex"; + MUTEX_ID javaId = (MUTEX_ID)env->GetDirectBufferAddress(id); + HALUTIL_LOG(logDEBUG) << "Mutex Ptr = " << javaId; + deleteMutex( javaId ); + delete[] javaId; } /* @@ -59,15 +59,15 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_deleteMutex * Method: takeMutex * Signature: (Ljava/nio/ByteBuffer;I)B */ -JNIEXPORT jbyte JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_takeMutex + JNIEXPORT jbyte JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_takeMutex (JNIEnv * env, jclass, jobject id) { - //HALUTIL_LOG(logDEBUG) << "Calling HALUtil takeMutex"; - MUTEX_ID* javaId = (MUTEX_ID*)env->GetDirectBufferAddress(id); - //HALUTIL_LOG(logDEBUG) << "Mutex Ptr = " << *javaId; - jbyte returnValue = takeMutex(*javaId); - //HALUTIL_LOG(logDEBUG) << "Take Result = " << (void*)returnValue; - return returnValue; + //HALUTIL_LOG(logDEBUG) << "Calling HALUtil takeMutex"; + MUTEX_ID javaId = (MUTEX_ID)env->GetDirectBufferAddress(id); + //HALUTIL_LOG(logDEBUG) << "Mutex Ptr = " << *javaId; + takeMutex(javaId); + //HALUTIL_LOG(logDEBUG) << "Take Result = " << (void*)returnValue; + return 0; } /* @@ -75,14 +75,14 @@ JNIEXPORT jbyte JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_takeMutex * Method: initializeMultiWait * Signature: ()Ljava/nio/ByteBuffer; */ -JNIEXPORT jobject JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_initializeMultiWait + JNIEXPORT jobject JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_initializeMultiWait (JNIEnv * env, jclass) { - HALUTIL_LOG(logDEBUG) << "Calling HALUtil initializeMultiWait"; - MULTIWAIT_ID* multiWaitPtr = (MULTIWAIT_ID*)new unsigned char[4]; - *multiWaitPtr = initializeMultiWait(); - HALUTIL_LOG(logDEBUG) << "MultiWait Ptr = " << *multiWaitPtr; - return env->NewDirectByteBuffer( multiWaitPtr, 4); + HALUTIL_LOG(logDEBUG) << "Calling HALUtil initializeMultiWait"; + MULTIWAIT_ID multiWaitPtr = (MULTIWAIT_ID)new unsigned char[4]; + multiWaitPtr = initializeMultiWait(); + HALUTIL_LOG(logDEBUG) << "MultiWait Ptr = " << multiWaitPtr; + return env->NewDirectByteBuffer( multiWaitPtr, 4); } /* @@ -90,13 +90,13 @@ JNIEXPORT jobject JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_initializeMulti * Method: deleteMultiWait * Signature: (Ljava/nio/ByteBuffer;)V */ -JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_deleteMultiWait + JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_deleteMultiWait (JNIEnv * env, jclass, jobject id) { - HALUTIL_LOG(logDEBUG) << "Calling HALUtil deleteMultiWait"; - MULTIWAIT_ID* javaId = (MULTIWAIT_ID*)env->GetDirectBufferAddress(id); - HALUTIL_LOG(logDEBUG) << "MultiWait Ptr = " << *javaId; - deleteMultiWait( *javaId ); + HALUTIL_LOG(logDEBUG) << "Calling HALUtil deleteMultiWait"; + MULTIWAIT_ID javaId = (MULTIWAIT_ID)env->GetDirectBufferAddress(id); + HALUTIL_LOG(logDEBUG) << "MultiWait Ptr = " << javaId; + deleteMultiWait( javaId ); } /* @@ -104,13 +104,13 @@ JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_deleteMultiWait * Method: takeMultiWait * Signature: (Ljava/nio/ByteBuffer;Ljava/nio/ByteBuffer;I)B */ -JNIEXPORT jbyte JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_takeMultiWait - (JNIEnv * env, jclass, jobject multiWaitId, jobject mutexId, jint timeout) + JNIEXPORT jbyte JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_takeMultiWait +(JNIEnv * env, jclass, jobject multiWaitId, jobject mutexId) { - MULTIWAIT_ID* javaMultiWaitId = (MULTIWAIT_ID*)env->GetDirectBufferAddress(multiWaitId); - MUTEX_ID* javaMutexId = (MUTEX_ID*)env->GetDirectBufferAddress(mutexId); - jbyte returnValue = takeMultiWait(*javaMultiWaitId, *javaMutexId, (int32_t) timeout); - return returnValue; + MULTIWAIT_ID javaMultiWaitId = (MULTIWAIT_ID)env->GetDirectBufferAddress(multiWaitId); + MUTEX_ID javaMutexId = (MUTEX_ID)env->GetDirectBufferAddress(mutexId); + takeMultiWait(javaMultiWaitId, javaMutexId); + return 0; } /* @@ -118,15 +118,15 @@ JNIEXPORT jbyte JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_takeMultiWait * Method: getFPGAVersion * Signature: (Ljava/nio/IntBuffer;)S */ -JNIEXPORT jshort JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_getFPGAVersion - (JNIEnv * env, jclass, jobject status) + JNIEXPORT jshort JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_getFPGAVersion +(JNIEnv * env, jclass, jobject status) { - HALUTIL_LOG(logDEBUG) << "Calling HALUtil getFPGAVersion"; - jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); - jshort returnValue = getFPGAVersion( statusPtr ); - HALUTIL_LOG(logDEBUG) << "Status = " << *statusPtr; - HALUTIL_LOG(logDEBUG) << "FPGAVersion = " << returnValue; - return returnValue; + HALUTIL_LOG(logDEBUG) << "Calling HALUtil getFPGAVersion"; + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + jshort returnValue = getFPGAVersion( statusPtr ); + HALUTIL_LOG(logDEBUG) << "Status = " << *statusPtr; + HALUTIL_LOG(logDEBUG) << "FPGAVersion = " << returnValue; + return returnValue; } /* @@ -134,15 +134,15 @@ JNIEXPORT jshort JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_getFPGAVersion * Method: getFPGARevision * Signature: (Ljava/nio/IntBuffer;)I */ -JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_getFPGARevision - (JNIEnv * env, jclass, jobject status) + JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_getFPGARevision +(JNIEnv * env, jclass, jobject status) { - HALUTIL_LOG(logDEBUG) << "Calling HALUtil getFPGARevision"; - jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); - jint returnValue = getFPGARevision( statusPtr ); - HALUTIL_LOG(logDEBUG) << "Status = " << *statusPtr; - HALUTIL_LOG(logDEBUG) << "FPGARevision = " << returnValue; - return returnValue; + HALUTIL_LOG(logDEBUG) << "Calling HALUtil getFPGARevision"; + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + jint returnValue = getFPGARevision( statusPtr ); + HALUTIL_LOG(logDEBUG) << "Status = " << *statusPtr; + HALUTIL_LOG(logDEBUG) << "FPGARevision = " << returnValue; + return returnValue; } /* @@ -150,15 +150,15 @@ JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_getFPGARevision * Method: getFPGATime * Signature: (Ljava/nio/IntBuffer;)I */ -JNIEXPORT jlong JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_getFPGATime - (JNIEnv * env, jclass, jobject status) + JNIEXPORT jlong JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_getFPGATime +(JNIEnv * env, jclass, jobject status) { - //HALUTIL_LOG(logDEBUG) << "Calling HALUtil getFPGATime"; - jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); - jlong returnValue = getFPGATime( statusPtr ); - //HALUTIL_LOG(logDEBUG) << "Status = " << *statusPtr; - //HALUTIL_LOG(logDEBUG) << "FPGATime = " << returnValue; - return returnValue; + //HALUTIL_LOG(logDEBUG) << "Calling HALUtil getFPGATime"; + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + jlong returnValue = getFPGATime( statusPtr ); + //HALUTIL_LOG(logDEBUG) << "Status = " << *statusPtr; + //HALUTIL_LOG(logDEBUG) << "FPGATime = " << returnValue; + return returnValue; } @@ -167,15 +167,15 @@ JNIEXPORT jlong JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_getFPGATime * Method: getFPGAButton * Signature: (Ljava/nio/IntBuffer;)I */ -JNIEXPORT jboolean JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_getFPGAButton - (JNIEnv * env, jclass, jobject status) + JNIEXPORT jboolean JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_getFPGAButton +(JNIEnv * env, jclass, jobject status) { - //HALUTIL_LOG(logDEBUG) << "Calling HALUtil getFPGATime"; - jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); - jboolean returnValue = getFPGAButton( statusPtr ); - //HALUTIL_LOG(logDEBUG) << "Status = " << *statusPtr; - //HALUTIL_LOG(logDEBUG) << "FPGATime = " << returnValue; - return returnValue; + //HALUTIL_LOG(logDEBUG) << "Calling HALUtil getFPGATime"; + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + jboolean returnValue = getFPGAButton( statusPtr ); + //HALUTIL_LOG(logDEBUG) << "Status = " << *statusPtr; + //HALUTIL_LOG(logDEBUG) << "FPGATime = " << returnValue; + return returnValue; } @@ -184,12 +184,12 @@ JNIEXPORT jboolean JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_getFPGAButton * Method: getHALErrorMessage * Signature: (I)Ljava/lang/String; */ -JNIEXPORT jstring JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_getHALErrorMessage - (JNIEnv * paramEnv, jclass, jint paramId) + JNIEXPORT jstring JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_getHALErrorMessage +(JNIEnv * paramEnv, jclass, jint paramId) { - const char * msg = getHALErrorMessage(paramId); - HALUTIL_LOG(logDEBUG) << "Calling HALUtil getHALErrorMessage id=" << paramId << " msg=" << msg; - return paramEnv->NewStringUTF(msg); + const char * msg = getHALErrorMessage(paramId); + HALUTIL_LOG(logDEBUG) << "Calling HALUtil getHALErrorMessage id=" << paramId << " msg=" << msg; + return paramEnv->NewStringUTF(msg); } /* @@ -197,10 +197,10 @@ JNIEXPORT jstring JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_getHALErrorMess * Method: getHALErrno * Signature: ()I */ -JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_getHALErrno - (JNIEnv *, jclass) + JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_getHALErrno +(JNIEnv *, jclass) { - return errno; + return errno; } /* @@ -208,15 +208,15 @@ JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_getHALErrno * Method: getHALstrerror * Signature: (I)Ljava/lang/String; */ -JNIEXPORT jstring JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_getHALstrerror - (JNIEnv * env, jclass, jint errorCode) + JNIEXPORT jstring JNICALL Java_edu_wpi_first_wpilibj_hal_HALUtil_getHALstrerror +(JNIEnv * env, jclass, jint errorCode) { - const char * msg = strerror(errno); - HALUTIL_LOG(logDEBUG) << "Calling HALUtil getHALstrerror errorCode=" << errorCode << " msg=" << msg; - return env->NewStringUTF(msg); + const char * msg = strerror(errno); + HALUTIL_LOG(logDEBUG) << "Calling HALUtil getHALstrerror errorCode=" << errorCode << " msg=" << msg; + return env->NewStringUTF(msg); } JNIEXPORT jint JNICALL - Java_edu_wpi_first_wpilibj_hal_HALUtil_pointerSize(JNIEnv*, jclass) { +Java_edu_wpi_first_wpilibj_hal_HALUtil_pointerSize(JNIEnv*, jclass) { return sizeof(void*); }