diff --git a/simulation/halsim_ws_client/src/main/native/cpp/HALSimWSClient.cpp b/simulation/halsim_ws_client/src/main/native/cpp/HALSimWSClient.cpp index 007c038064..dfa54d29f4 100644 --- a/simulation/halsim_ws_client/src/main/native/cpp/HALSimWSClient.cpp +++ b/simulation/halsim_ws_client/src/main/native/cpp/HALSimWSClient.cpp @@ -5,16 +5,19 @@ #include "HALSimWSClient.h" #include +#include #include #include #include #include #include #include +#include #include #include #include #include +#include #include #include @@ -34,6 +37,7 @@ bool HALSimWSClient::Initialize() { providers.Add(key, provider); }; + HALSimWSProviderAddressableLED::Initialize(registerFunc); HALSimWSProviderAnalogIn::Initialize(registerFunc); HALSimWSProviderAnalogOut::Initialize(registerFunc); HALSimWSProviderBuiltInAccelerometer::Initialize(registerFunc); @@ -42,9 +46,11 @@ bool HALSimWSClient::Initialize() { HALSimWSProviderDriverStation::Initialize(registerFunc); HALSimWSProviderEncoder::Initialize(registerFunc); HALSimWSProviderJoystick::Initialize(registerFunc); + HALSimWSProviderPCM::Initialize(registerFunc); HALSimWSProviderPWM::Initialize(registerFunc); HALSimWSProviderRelay::Initialize(registerFunc); HALSimWSProviderRoboRIO::Initialize(registerFunc); + HALSimWSProviderSolenoid::Initialize(registerFunc); simDevices.Initialize(loop); diff --git a/simulation/halsim_ws_core/doc/hardware_ws_api.md b/simulation/halsim_ws_core/doc/hardware_ws_api.md index 243c868d91..b2b40fc8b4 100644 --- a/simulation/halsim_ws_core/doc/hardware_ws_api.md +++ b/simulation/halsim_ws_core/doc/hardware_ws_api.md @@ -87,6 +87,7 @@ The “hardware“ (which might be a full-fledged 3D simulation engine, a physic | [``"Joystick"``][] | Joystick data | Joystick number | | [``"PWM"``][] | PWM output | Port index, e.g. "1", "2" | | [``"Relay"``][] | Relay output | Port index, e.g. "1", "2" | +| [``"Solenoid"``][] | Solenoid output | Module +Port index, e.g. "0,1", "2,5" | #### Accelerometer ("Accel") @@ -237,6 +238,17 @@ PWMs may be used to control either speed controllers or servos. Typically only | ``" +#include + +#define REGISTER(halsim, jsonid, ctype, haltype) \ + HALSIM_RegisterAddressableLED##halsim##Callback( \ + m_channel, \ + [](const char* name, void* param, const struct HAL_Value* value) { \ + static_cast(param) \ + ->ProcessHalCallback( \ + {{jsonid, static_cast(value->data.v_##haltype)}}); \ + }, \ + this, true) +namespace wpilibws { +void HALSimWSProviderAddressableLED::Initialize( + WSRegisterFunc webRegisterFunc) { + CreateProviders( + "AddressableLED", HAL_GetNumAddressableLEDs(), webRegisterFunc); +} + +HALSimWSProviderAddressableLED::~HALSimWSProviderAddressableLED() { + DoCancelCallbacks(); +} + +void HALSimWSProviderAddressableLED::RegisterCallbacks() { + m_initCbKey = REGISTER(Initialized, "(param); + + size_t numLeds = count / sizeof(HAL_AddressableLEDData); + const HAL_AddressableLEDData* data = + reinterpret_cast(buffer); + + std::vector jsonData; + + for (size_t i = 0; i < numLeds; ++i) { + jsonData.push_back( + {{"r", data[i].r}, {"g", data[i].g}, {"b", data[i].b}}); + } + + wpi::json payload; + payload[">data"] = jsonData; + + provider->ProcessHalCallback(payload); + }, + this); +} + +void HALSimWSProviderAddressableLED::CancelCallbacks() { + DoCancelCallbacks(); +} + +void HALSimWSProviderAddressableLED::DoCancelCallbacks() { + HALSIM_CancelAddressableLEDInitializedCallback(m_channel, m_initCbKey); + HALSIM_CancelAddressableLEDOutputPortCallback(m_channel, m_outputPortCbKey); + HALSIM_CancelAddressableLEDLengthCallback(m_channel, m_lengthCbKey); + HALSIM_CancelAddressableLEDRunningCallback(m_channel, m_runningCbKey); + HALSIM_CancelAddressableLEDDataCallback(m_channel, m_dataCbKey); + + m_initCbKey = 0; + m_outputPortCbKey = 0; + m_lengthCbKey = 0; + m_runningCbKey = 0; + m_dataCbKey = 0; +} +} // namespace wpilibws diff --git a/simulation/halsim_ws_core/src/main/native/cpp/WSProvider_PCM.cpp b/simulation/halsim_ws_core/src/main/native/cpp/WSProvider_PCM.cpp new file mode 100644 index 0000000000..1b129595d2 --- /dev/null +++ b/simulation/halsim_ws_core/src/main/native/cpp/WSProvider_PCM.cpp @@ -0,0 +1,56 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +#include "WSProvider_PCM.h" + +#include +#include + +#define REGISTER_PCM(halsim, jsonid, ctype, haltype) \ + HALSIM_RegisterPCM##halsim##Callback( \ + m_channel, \ + [](const char* name, void* param, const struct HAL_Value* value) { \ + static_cast(param)->ProcessHalCallback( \ + {{jsonid, static_cast(value->data.v_##haltype)}}); \ + }, \ + this, true) +namespace wpilibws { +void HALSimWSProviderPCM::Initialize(WSRegisterFunc webRegisterFunc) { + CreateProviders("PCM", HAL_GetNumPCMModules(), + webRegisterFunc); +} + +HALSimWSProviderPCM::~HALSimWSProviderPCM() { + DoCancelCallbacks(); +} + +void HALSimWSProviderPCM::RegisterCallbacks() { + m_initCbKey = REGISTER_PCM(CompressorInitialized, "on", bool, boolean); + m_closedLoopCbKey = + REGISTER_PCM(ClosedLoopEnabled, "pressure_switch", bool, boolean); + m_currentCbKey = REGISTER_PCM(CompressorCurrent, ">current", double, double); +} + +void HALSimWSProviderPCM::CancelCallbacks() { + DoCancelCallbacks(); +} + +void HALSimWSProviderPCM::DoCancelCallbacks() { + HALSIM_CancelPCMCompressorInitializedCallback(m_channel, m_initCbKey); + HALSIM_CancelPCMCompressorOnCallback(m_channel, m_onCbKey); + HALSIM_CancelPCMClosedLoopEnabledCallback(m_channel, m_closedLoopCbKey); + HALSIM_CancelPCMPressureSwitchCallback(m_channel, m_pressureSwitchCbKey); + HALSIM_CancelPCMCompressorCurrentCallback(m_channel, m_currentCbKey); + + m_initCbKey = 0; + m_onCbKey = 0; + m_closedLoopCbKey = 0; + m_pressureSwitchCbKey = 0; + m_currentCbKey = 0; +} + +} // namespace wpilibws diff --git a/simulation/halsim_ws_core/src/main/native/cpp/WSProvider_Solenoid.cpp b/simulation/halsim_ws_core/src/main/native/cpp/WSProvider_Solenoid.cpp new file mode 100644 index 0000000000..4c77d4bcd1 --- /dev/null +++ b/simulation/halsim_ws_core/src/main/native/cpp/WSProvider_Solenoid.cpp @@ -0,0 +1,67 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +#include "WSProvider_Solenoid.h" + +#include +#include + +#define REGISTER_SOLENOID(halsim, jsonid, ctype, haltype) \ + HALSIM_RegisterPCMSolenoid##halsim##Callback( \ + m_pcmIndex, m_solenoidIndex, \ + [](const char* name, void* param, const struct HAL_Value* value) { \ + static_cast(param)->ProcessHalCallback( \ + {{jsonid, static_cast(value->data.v_##haltype)}}); \ + }, \ + this, true) + +namespace wpilibws { +void HALSimWSProviderSolenoid::Initialize(WSRegisterFunc webRegisterFunc) { + for (int32_t pcmIndex = 0; pcmIndex < HAL_GetNumPCMModules(); ++pcmIndex) { + for (int32_t solenoidIndex = 0; + solenoidIndex < HAL_GetNumSolenoidChannels(); ++solenoidIndex) { + auto key = + ("Solenoid/" + wpi::Twine(pcmIndex) + "," + wpi::Twine(solenoidIndex)) + .str(); + auto ptr = std::make_unique( + pcmIndex, solenoidIndex, key, "Solenoid"); + webRegisterFunc(key, std::move(ptr)); + } + } +} + +HALSimWSProviderSolenoid::HALSimWSProviderSolenoid(int32_t pcmChannel, + int32_t solenoidChannel, + const std::string& key, + const std::string& type) + : HALSimWSHalProvider(key, type), + m_pcmIndex(pcmChannel), + m_solenoidIndex(solenoidChannel) { + m_deviceId = + std::to_string(m_pcmIndex) + "," + std::to_string(solenoidChannel); +} + +HALSimWSProviderSolenoid::~HALSimWSProviderSolenoid() { + DoCancelCallbacks(); +} + +void HALSimWSProviderSolenoid::RegisterCallbacks() { + m_initCbKey = REGISTER_SOLENOID(Initialized, " + +#include "WSHalProviders.h" + +namespace wpilibws { +class HALSimWSProviderPCM : public HALSimWSHalChanProvider { + public: + static void Initialize(WSRegisterFunc webRegisterFunc); + + using HALSimWSHalChanProvider::HALSimWSHalChanProvider; + ~HALSimWSProviderPCM(); + + protected: + void RegisterCallbacks() override; + void CancelCallbacks() override; + void DoCancelCallbacks(); + + private: + int32_t m_initCbKey = 0; + int32_t m_onCbKey = 0; + int32_t m_closedLoopCbKey = 0; + int32_t m_pressureSwitchCbKey = 0; + int32_t m_currentCbKey = 0; +}; +} // namespace wpilibws diff --git a/simulation/halsim_ws_core/src/main/native/include/WSProvider_PDP.h b/simulation/halsim_ws_core/src/main/native/include/WSProvider_PDP.h new file mode 100644 index 0000000000..270e6a00b4 --- /dev/null +++ b/simulation/halsim_ws_core/src/main/native/include/WSProvider_PDP.h @@ -0,0 +1,51 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +#pragma once + +#include + +#include "WSHalProviders.h" + +namespace wpilibws { +class HALSimWSProviderPDP : public HALSimWSHalChanProvider { + public: + static void Initialize(WSRegisterFunc webRegisterFunc); + + using HALSimWSHalChanProvider::HALSimWSHalChanProvider; + ~HALSimWSProviderPDP(); + + protected: + void RegisterCallbacks() override; + void CancelCallbacks() override; + void DoCancelCallbacks(); + + private: + int32_t m_initCbKey = 0; + int32_t m_temperatureCbKey = 0; + int32_t m_voltageCbKey = 0; +}; + +class HALSimWSProviderPDPChannelCurrent : public HALSimWSHalProvider { + public: + static void Initialize(WSRegisterFunc webRegisterFunc); + + explicit HALSimWSProviderPDPChannelCurrent(int32_t pdpChannel, + int32_t currentChannel, + const std::string& key, + const std::string& type); + ~HALSimWSProviderPDPChannelCurrent(); + + protected: + void RegisterCallbacks() override; + void CancelCallbacks() override; + void DoCancelCallbacks(); + + private: + int32_t m_pdpIndex = 0; + int32_t m_channelIndex = 0; + + int32_t m_currentCbKey = 0; +}; +} // namespace wpilibws diff --git a/simulation/halsim_ws_core/src/main/native/include/WSProvider_Solenoid.h b/simulation/halsim_ws_core/src/main/native/include/WSProvider_Solenoid.h new file mode 100644 index 0000000000..e288345953 --- /dev/null +++ b/simulation/halsim_ws_core/src/main/native/include/WSProvider_Solenoid.h @@ -0,0 +1,33 @@ +// Copyright (c) FIRST and other WPILib contributors. +// Open Source Software; you can modify and/or share it under the terms of +// the WPILib BSD license file in the root directory of this project. + +#pragma once + +#include + +#include "WSHalProviders.h" + +namespace wpilibws { +class HALSimWSProviderSolenoid : public HALSimWSHalProvider { + public: + static void Initialize(WSRegisterFunc webRegisterFunc); + + explicit HALSimWSProviderSolenoid(int32_t pcmChannel, int32_t solenoidChannel, + const std::string& key, + const std::string& type); + ~HALSimWSProviderSolenoid(); + + protected: + void RegisterCallbacks() override; + void CancelCallbacks() override; + void DoCancelCallbacks(); + + private: + int32_t m_pcmIndex = 0; + int32_t m_solenoidIndex = 0; + + int32_t m_initCbKey = 0; + int32_t m_outputCbKey = 0; +}; +} // namespace wpilibws diff --git a/simulation/halsim_ws_server/src/main/native/cpp/HALSimWSServer.cpp b/simulation/halsim_ws_server/src/main/native/cpp/HALSimWSServer.cpp index decbebb261..9ecccb049c 100644 --- a/simulation/halsim_ws_server/src/main/native/cpp/HALSimWSServer.cpp +++ b/simulation/halsim_ws_server/src/main/native/cpp/HALSimWSServer.cpp @@ -5,16 +5,19 @@ #include "HALSimWSServer.h" #include +#include #include #include #include #include #include #include +#include #include #include #include #include +#include #include using namespace wpilibws; @@ -33,6 +36,7 @@ bool HALSimWSServer::Initialize() { providers.Add(key, provider); }; + HALSimWSProviderAddressableLED::Initialize(registerFunc); HALSimWSProviderAnalogIn::Initialize(registerFunc); HALSimWSProviderAnalogOut::Initialize(registerFunc); HALSimWSProviderBuiltInAccelerometer::Initialize(registerFunc); @@ -41,9 +45,11 @@ bool HALSimWSServer::Initialize() { HALSimWSProviderDriverStation::Initialize(registerFunc); HALSimWSProviderEncoder::Initialize(registerFunc); HALSimWSProviderJoystick::Initialize(registerFunc); + HALSimWSProviderPCM::Initialize(registerFunc); HALSimWSProviderPWM::Initialize(registerFunc); HALSimWSProviderRelay::Initialize(registerFunc); HALSimWSProviderRoboRIO::Initialize(registerFunc); + HALSimWSProviderSolenoid::Initialize(registerFunc); simDevices.Initialize(loop);