[hal] Add a unified PCM object (#3331)

This commit is contained in:
Thad House
2021-06-05 22:36:39 -07:00
committed by GitHub
parent dea841103d
commit 0e702eb799
103 changed files with 2643 additions and 5676 deletions

View File

@@ -0,0 +1,83 @@
// 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 "../PortsInternal.h"
#include "CTREPCMDataInternal.h"
using namespace hal;
namespace hal::init {
void InitializeCTREPCMData() {
static CTREPCMData spd[kNumCTREPCMModules];
::hal::SimCTREPCMData = spd;
}
} // namespace hal::init
CTREPCMData* hal::SimCTREPCMData;
void CTREPCMData::ResetData() {
for (int i = 0; i < kNumCTRESolenoidChannels; i++) {
solenoidOutput[i].Reset(false);
}
initialized.Reset(false);
compressorOn.Reset(false);
closedLoopEnabled.Reset(true);
pressureSwitch.Reset(false);
compressorCurrent.Reset(0.0);
}
extern "C" {
void HALSIM_ResetCTREPCMData(int32_t index) {
SimCTREPCMData[index].ResetData();
}
#define DEFINE_CAPI(TYPE, CAPINAME, LOWERNAME) \
HAL_SIMDATAVALUE_DEFINE_CAPI(TYPE, HALSIM, CTREPCM##CAPINAME, \
SimCTREPCMData, LOWERNAME)
HAL_SIMDATAVALUE_DEFINE_CAPI_CHANNEL(HAL_Bool, HALSIM, CTREPCMSolenoidOutput,
SimCTREPCMData, solenoidOutput)
DEFINE_CAPI(HAL_Bool, Initialized, initialized)
DEFINE_CAPI(HAL_Bool, CompressorOn, compressorOn)
DEFINE_CAPI(HAL_Bool, ClosedLoopEnabled, closedLoopEnabled)
DEFINE_CAPI(HAL_Bool, PressureSwitch, pressureSwitch)
DEFINE_CAPI(double, CompressorCurrent, compressorCurrent)
void HALSIM_GetCTREPCMAllSolenoids(int32_t index, uint8_t* values) {
auto& data = SimCTREPCMData[index].solenoidOutput;
uint8_t ret = 0;
for (int i = 0; i < kNumCTRESolenoidChannels; i++) {
ret |= (data[i] << i);
}
*values = ret;
}
void HALSIM_SetCTREPCMAllSolenoids(int32_t index, uint8_t values) {
auto& data = SimCTREPCMData[index].solenoidOutput;
for (int i = 0; i < kNumCTRESolenoidChannels; i++) {
data[i] = (values & 0x1) != 0;
values >>= 1;
}
}
#define REGISTER(NAME) \
SimCTREPCMData[index].NAME.RegisterCallback(callback, param, initialNotify)
void HALSIM_RegisterCTREPCMAllNonSolenoidCallbacks(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
REGISTER(initialized);
REGISTER(compressorOn);
REGISTER(closedLoopEnabled);
REGISTER(pressureSwitch);
REGISTER(compressorCurrent);
}
void HALSIM_RegisterCTREPCMAllSolenoidCallbacks(int32_t index, int32_t channel,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
REGISTER(solenoidOutput[channel]);
}
} // extern "C"

View File

@@ -5,40 +5,29 @@
#pragma once
#include "../PortsInternal.h"
#include "hal/simulation/PCMData.h"
#include "hal/simulation/CTREPCMData.h"
#include "hal/simulation/SimDataValue.h"
namespace hal {
class PCMData {
HAL_SIMDATAVALUE_DEFINE_NAME(SolenoidInitialized)
class CTREPCMData {
HAL_SIMDATAVALUE_DEFINE_NAME(Initialized)
HAL_SIMDATAVALUE_DEFINE_NAME(SolenoidOutput)
HAL_SIMDATAVALUE_DEFINE_NAME(AnySolenoidInitialized)
HAL_SIMDATAVALUE_DEFINE_NAME(CompressorInitialized)
HAL_SIMDATAVALUE_DEFINE_NAME(CompressorOn)
HAL_SIMDATAVALUE_DEFINE_NAME(ClosedLoopEnabled)
HAL_SIMDATAVALUE_DEFINE_NAME(PressureSwitch)
HAL_SIMDATAVALUE_DEFINE_NAME(CompressorCurrent)
static LLVM_ATTRIBUTE_ALWAYS_INLINE constexpr HAL_Bool
GetSolenoidInitializedDefault() {
return false;
}
static LLVM_ATTRIBUTE_ALWAYS_INLINE constexpr HAL_Bool
GetSolenoidOutputDefault() {
return false;
}
public:
SimDataValue<HAL_Bool, HAL_MakeBoolean, GetSolenoidInitializedName,
GetSolenoidInitializedDefault>
solenoidInitialized[kNumSolenoidChannels];
SimDataValue<HAL_Bool, HAL_MakeBoolean, GetInitializedName> initialized{
false};
SimDataValue<HAL_Bool, HAL_MakeBoolean, GetSolenoidOutputName,
GetSolenoidOutputDefault>
solenoidOutput[kNumSolenoidChannels];
SimDataValue<HAL_Bool, HAL_MakeBoolean, GetAnySolenoidInitializedName>
anySolenoidInitialized{false};
SimDataValue<HAL_Bool, HAL_MakeBoolean, GetCompressorInitializedName>
compressorInitialized{false};
solenoidOutput[kNumCTRESolenoidChannels];
SimDataValue<HAL_Bool, HAL_MakeBoolean, GetCompressorOnName> compressorOn{
false};
SimDataValue<HAL_Bool, HAL_MakeBoolean, GetClosedLoopEnabledName>
@@ -50,5 +39,5 @@ class PCMData {
virtual void ResetData();
};
extern PCMData* SimPCMData;
extern CTREPCMData* SimCTREPCMData;
} // namespace hal

View File

@@ -1,89 +0,0 @@
// 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 "../PortsInternal.h"
#include "PCMDataInternal.h"
using namespace hal;
namespace hal::init {
void InitializePCMData() {
static PCMData spd[kNumPCMModules];
::hal::SimPCMData = spd;
}
} // namespace hal::init
PCMData* hal::SimPCMData;
void PCMData::ResetData() {
for (int i = 0; i < kNumSolenoidChannels; i++) {
solenoidInitialized[i].Reset(false);
solenoidOutput[i].Reset(false);
}
anySolenoidInitialized.Reset(false);
compressorInitialized.Reset(false);
compressorOn.Reset(false);
closedLoopEnabled.Reset(true);
pressureSwitch.Reset(false);
compressorCurrent.Reset(0.0);
}
extern "C" {
void HALSIM_ResetPCMData(int32_t index) {
SimPCMData[index].ResetData();
}
#define DEFINE_CAPI(TYPE, CAPINAME, LOWERNAME) \
HAL_SIMDATAVALUE_DEFINE_CAPI(TYPE, HALSIM, PCM##CAPINAME, SimPCMData, \
LOWERNAME)
HAL_SIMDATAVALUE_DEFINE_CAPI_CHANNEL(HAL_Bool, HALSIM, PCMSolenoidInitialized,
SimPCMData, solenoidInitialized)
HAL_SIMDATAVALUE_DEFINE_CAPI_CHANNEL(HAL_Bool, HALSIM, PCMSolenoidOutput,
SimPCMData, solenoidOutput)
DEFINE_CAPI(HAL_Bool, AnySolenoidInitialized, anySolenoidInitialized)
DEFINE_CAPI(HAL_Bool, CompressorInitialized, compressorInitialized)
DEFINE_CAPI(HAL_Bool, CompressorOn, compressorOn)
DEFINE_CAPI(HAL_Bool, ClosedLoopEnabled, closedLoopEnabled)
DEFINE_CAPI(HAL_Bool, PressureSwitch, pressureSwitch)
DEFINE_CAPI(double, CompressorCurrent, compressorCurrent)
void HALSIM_GetPCMAllSolenoids(int32_t index, uint8_t* values) {
auto& data = SimPCMData[index].solenoidOutput;
uint8_t ret = 0;
for (int i = 0; i < kNumSolenoidChannels; i++) {
ret |= (data[i] << i);
}
*values = ret;
}
void HALSIM_SetPCMAllSolenoids(int32_t index, uint8_t values) {
auto& data = SimPCMData[index].solenoidOutput;
for (int i = 0; i < kNumSolenoidChannels; i++) {
data[i] = (values & 0x1) != 0;
values >>= 1;
}
}
#define REGISTER(NAME) \
SimPCMData[index].NAME.RegisterCallback(callback, param, initialNotify)
void HALSIM_RegisterPCMAllNonSolenoidCallbacks(int32_t index,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
REGISTER(compressorInitialized);
REGISTER(compressorOn);
REGISTER(closedLoopEnabled);
REGISTER(pressureSwitch);
REGISTER(compressorCurrent);
}
void HALSIM_RegisterPCMAllSolenoidCallbacks(int32_t index, int32_t channel,
HAL_NotifyCallback callback,
void* param,
HAL_Bool initialNotify) {
REGISTER(solenoidInitialized[channel]);
REGISTER(solenoidOutput[channel]);
}
} // extern "C"