diff --git a/hal/src/main/native/include/MockData/RelayData.h b/hal/src/main/native/include/MockData/RelayData.h index a4e15521f1..baf9ef7c15 100644 --- a/hal/src/main/native/include/MockData/RelayData.h +++ b/hal/src/main/native/include/MockData/RelayData.h @@ -49,7 +49,7 @@ void HALSIM_CancelRelayReverseCallback(int32_t index, int32_t uid); HAL_Bool HALSIM_GetRelayReverse(int32_t index); void HALSIM_SetRelayReverse(int32_t index, HAL_Bool reverse); -void HALSIM_RegisterRelayAllCallcbaks(int32_t index, +void HALSIM_RegisterRelayAllCallbacks(int32_t index, HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify); diff --git a/hal/src/main/native/sim/MockData/RelayData.cpp b/hal/src/main/native/sim/MockData/RelayData.cpp index 5625271e62..a8c68ae3c6 100644 --- a/hal/src/main/native/sim/MockData/RelayData.cpp +++ b/hal/src/main/native/sim/MockData/RelayData.cpp @@ -261,7 +261,7 @@ void HALSIM_SetRelayReverse(int32_t index, HAL_Bool reverse) { SimRelayData[index].SetReverse(reverse); } -void HALSIM_RegisterRelayAllCallcbaks(int32_t index, +void HALSIM_RegisterRelayAllCallbacks(int32_t index, HAL_NotifyCallback callback, void* param, HAL_Bool initialNotify) { SimRelayData[index].RegisterInitializedForwardCallback(callback, param, diff --git a/settings.gradle b/settings.gradle index a0345054e4..853e1904fe 100644 --- a/settings.gradle +++ b/settings.gradle @@ -9,4 +9,5 @@ include 'wpilibjIntegrationTests' include 'wpilibjExamples' include 'myRobot' include 'simulation:halsim_print' +include 'simulation:halsim_lowfi' include 'simulation:adx_gyro_accelerometer' diff --git a/simulation/halsim_lowfi/build.gradle b/simulation/halsim_lowfi/build.gradle new file mode 100644 index 0000000000..7edf98bf5c --- /dev/null +++ b/simulation/halsim_lowfi/build.gradle @@ -0,0 +1,58 @@ +description = "A simulation shared object that publishes basic robot I/O to NetworkTables." + +apply plugin: 'edu.wpi.first.NativeUtils' +apply plugin: 'visual-studio' +apply plugin: 'cpp' + +if (!project.hasProperty('onlyAthena')) { + ext.skipAthena = true + + apply from: "../../config.gradle" + + + model { + dependencyConfigs { + ntcore(DependencyConfig) { + groupId = 'edu.wpi.first.ntcore' + artifactId = 'ntcore-cpp' + headerClassifier = 'headers' + ext = 'zip' + version = '4.+' + sharedConfigs = [ halsim_lowfi: [] ] + } + wpiutil(DependencyConfig) { + groupId = 'edu.wpi.first.wpiutil' + artifactId = 'wpiutil-cpp' + headerClassifier = 'headers' + ext = 'zip' + version = '3.+' + sharedConfigs = [ halsim_lowfi: [] ] + } + } + components { + halsim_lowfi(NativeLibrarySpec) { + sources { + cpp { + source { + srcDirs = [ 'src/main/native/cpp' ] + includes = ["**/*.cpp"] + } + exportedHeaders { + srcDirs = ["src/main/native/include"] + } + } + } + } + } + + binaries { + all { + project(':hal').addHalToLinker(it) + } + withType(StaticLibraryBinarySpec) { + it.buildable = false + } + } + } + apply from: 'publish.gradle' +} diff --git a/simulation/halsim_lowfi/publish.gradle b/simulation/halsim_lowfi/publish.gradle new file mode 100644 index 0000000000..b21497e4b3 --- /dev/null +++ b/simulation/halsim_lowfi/publish.gradle @@ -0,0 +1,94 @@ +apply plugin: 'maven-publish' +apply plugin: 'edu.wpi.first.wpilib.versioning.WPILibVersioningPlugin' + +if (!hasProperty('releaseType')) { + WPILibVersion { + releaseType = 'dev' + } +} + +def pubVersion = '' +if (project.hasProperty("publishVersion")) { + pubVersion = project.publishVersion +} else { + pubVersion = WPILibVersion.version +} + +def baseArtifactId = 'halsim-lowfi' +def artifactGroupId = 'edu.wpi.first.halsim' + +def outputsFolder = file("$project.buildDir/outputs") + +task cppSourcesZip(type: Zip) { + destinationDir = outputsFolder + baseName = 'halsim-lowfi' + classifier = "sources" + + from(licenseFile) { + into '/' + } + + from('src/main/native/cpp') { + into '/' + } +} + +task cppHeadersZip(type: Zip) { + destinationDir = outputsFolder + baseName = 'halsim-lowfi' + classifier = "headers" + + from(licenseFile) { + into '/' + } + + from('src/main/native/include') { + into '/' + } +} + +build.dependsOn cppSourcesZip +build.dependsOn cppHeadersZip + + +model { + publishing { + def pluginTaskList = createComponentZipTasks($.components, 'halsim_lowfi', 'zipcpp', Zip, project, { task, value-> + value.each { binary-> + if (binary.buildable) { + if (binary instanceof SharedLibraryBinarySpec) { + task.dependsOn binary.buildTask + task.from (binary.sharedLibraryFile) { + into getPlatformPath(binary) + '/shared' + } + } + } + } + }) + + def allTask + if (!project.hasProperty('jenkinsBuild')) { + allTask = createAllCombined(pluginTaskList, 'halsim_lowfi', 'zipcpp', Zip, project) + } + + publications { + cpp(MavenPublication) { + pluginTaskList.each { + artifact it + } + + if (!project.hasProperty('jenkinsBuild')) { + artifact allTask + } + + artifact cppHeadersZip + artifact cppSourcesZip + + + artifactId = baseArtifactId + groupId artifactGroupId + version pubVersion + } + } + } +} diff --git a/simulation/halsim_lowfi/src/main/native/cpp/HALSimLowFi.cpp b/simulation/halsim_lowfi/src/main/native/cpp/HALSimLowFi.cpp new file mode 100644 index 0000000000..c227ee67aa --- /dev/null +++ b/simulation/halsim_lowfi/src/main/native/cpp/HALSimLowFi.cpp @@ -0,0 +1,62 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2017 FIRST. 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 the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#include "HALSimLowFi.h" + +#include + +void HALSimLowFi::Initialize() { + table = nt::NetworkTableInstance::GetDefault().GetTable("sim"); +} + +void HALSimNTProvider::Inject(std::shared_ptr parentArg, + std::string tableNameArg) { + parent = parentArg; + tableName = std::move(tableNameArg); + table = parent->table->GetSubTable(tableName); + + this->Initialize(); +} + +void NTProviderBaseCallback(const char* name, void* param, + const struct HAL_Value* value) { + auto info = + static_cast(param); + uint32_t chan = static_cast(info->channel); + auto provider = info->provider; + auto table = info->table; + provider->OnCallback(chan, table); +} + +void HALSimNTProvider::InitializeDefault( + int numChannels, HALCbRegisterIndexedFunc registerFunc) { + this->numChannels = numChannels; + cbInfos.reserve(numChannels); + for (int i = 0; i < numChannels; i++) { + struct NTProviderCallbackInfo info = { + this, table->GetSubTable(tableName + llvm::Twine(i)), i}; + cbInfos.emplace_back(info); + } + + for (auto& info : cbInfos) { + registerFunc(info.channel, NTProviderBaseCallback, &info, true); + OnInitializedChannel(info.channel, info.table); + } +} + +void HALSimNTProvider::InitializeDefaultSingle( + HALCbRegisterSingleFunc registerFunc) { + struct NTProviderCallbackInfo info = {this, table, 0}; + cbInfos.push_back(info); + + for (auto& info : cbInfos) { + registerFunc(NTProviderBaseCallback, &info, true); + } +} + +void HALSimNTProvider::OnInitializedChannel( + uint32_t channel, std::shared_ptr table) {} diff --git a/simulation/halsim_lowfi/src/main/native/cpp/NTProvider_Analog.cpp b/simulation/halsim_lowfi/src/main/native/cpp/NTProvider_Analog.cpp new file mode 100644 index 0000000000..137d58715e --- /dev/null +++ b/simulation/halsim_lowfi/src/main/native/cpp/NTProvider_Analog.cpp @@ -0,0 +1,56 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2017 FIRST. 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 the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#include "NTProvider_Analog.h" + +#include +#include +#include + +void HALSimNTProviderAnalogIn::Initialize() { + InitializeDefault(HAL_GetNumAnalogInputs(), + HALSIM_RegisterAnalogInAllCallbacks); +} + +void HALSimNTProviderAnalogIn::OnCallback( + uint32_t chan, std::shared_ptr table) { + table->GetEntry("init?").SetBoolean(HALSIM_GetAnalogInInitialized(chan)); + table->GetEntry("avg_bits").SetDouble(HALSIM_GetAnalogInAverageBits(chan)); + table->GetEntry("oversample_bits") + .SetDouble(HALSIM_GetAnalogInOversampleBits(chan)); + table->GetEntry("voltage").SetDouble(HALSIM_GetAnalogInVoltage(chan)); + + auto accum = table->GetSubTable("accum"); + accum->GetEntry("init?").SetBoolean( + HALSIM_GetAnalogInAccumulatorInitialized(chan)); + accum->GetEntry("value").SetDouble(HALSIM_GetAnalogInAccumulatorValue(chan)); + accum->GetEntry("count").SetDouble(HALSIM_GetAnalogInAccumulatorCount(chan)); + accum->GetEntry("center").SetDouble( + HALSIM_GetAnalogInAccumulatorCenter(chan)); + accum->GetEntry("deadband") + .SetDouble(HALSIM_GetAnalogInAccumulatorDeadband(chan)); +} + +void HALSimNTProviderAnalogIn::OnInitializedChannel( + uint32_t chan, std::shared_ptr table) { + table->GetEntry("voltage").AddListener( + [=](const nt::EntryNotification& ev) -> void { + HALSIM_SetAnalogInVoltage(chan, ev.value->GetDouble()); + }, + NT_NotifyKind::NT_NOTIFY_UPDATE); +} + +void HALSimNTProviderAnalogOut::Initialize() { + InitializeDefault(HAL_GetNumAnalogOutputs(), + HALSIM_RegisterAnalogOutAllCallbacks); +} + +void HALSimNTProviderAnalogOut::OnCallback( + uint32_t chan, std::shared_ptr table) { + table->GetEntry("init?").SetBoolean(HALSIM_GetAnalogOutInitialized(chan)); + table->GetEntry("voltage").SetDouble(HALSIM_GetAnalogOutVoltage(chan)); +} diff --git a/simulation/halsim_lowfi/src/main/native/cpp/NTProvider_DIO.cpp b/simulation/halsim_lowfi/src/main/native/cpp/NTProvider_DIO.cpp new file mode 100644 index 0000000000..6bc4bbb4b4 --- /dev/null +++ b/simulation/halsim_lowfi/src/main/native/cpp/NTProvider_DIO.cpp @@ -0,0 +1,35 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2017 FIRST. 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 the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#include "NTProvider_DIO.h" + +#include +#include + +void HALSimNTProviderDIO::Initialize() { + InitializeDefault(HAL_GetNumDigitalChannels(), + HALSIM_RegisterDIOAllCallbacks); +} + +void HALSimNTProviderDIO::OnCallback(uint32_t chan, + std::shared_ptr table) { + table->GetEntry("init?").SetBoolean(HALSIM_GetDIOInitialized(chan)); + table->GetEntry("value").SetBoolean(HALSIM_GetDIOValue(chan)); + table->GetEntry("pulse_length").SetDouble(HALSIM_GetDIOPulseLength(chan)); + table->GetEntry("input?").SetBoolean(HALSIM_GetDIOIsInput(chan)); +} + +void HALSimNTProviderDIO::OnInitializedChannel( + uint32_t chan, std::shared_ptr table) { + table->GetEntry("value").AddListener( + [=](const nt::EntryNotification& ev) -> void { + if (HALSIM_GetDIOIsInput(chan)) { + HALSIM_SetDIOValue(chan, ev.value->GetBoolean()); + } + }, + NT_NotifyKind::NT_NOTIFY_UPDATE); +} diff --git a/simulation/halsim_lowfi/src/main/native/cpp/NTProvider_DriverStation.cpp b/simulation/halsim_lowfi/src/main/native/cpp/NTProvider_DriverStation.cpp new file mode 100644 index 0000000000..6cf6d1b6f6 --- /dev/null +++ b/simulation/halsim_lowfi/src/main/native/cpp/NTProvider_DriverStation.cpp @@ -0,0 +1,61 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2017 FIRST. 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 the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#include "NTProvider_DriverStation.h" + +#include +#include + +void HALSimNTProviderDriverStation::Initialize() { + InitializeDefaultSingle(HALSIM_RegisterDriverStationAllCallbacks); +} + +void HALSimNTProviderDriverStation::OnCallback( + uint32_t chan, std::shared_ptr table) { + bool auton = HALSIM_GetDriverStationAutonomous(), + test = HALSIM_GetDriverStationTest(), + enabled = HALSIM_GetDriverStationEnabled(); + + bool teleop = (!auton && !test && enabled); + + table->GetEntry("enabled?").SetBoolean(enabled); + table->GetEntry("autonomous?").SetBoolean(auton); + table->GetEntry("test?").SetBoolean(test); + table->GetEntry("teleop?").SetBoolean(teleop); + table->GetEntry("estop?").SetBoolean(HALSIM_GetDriverStationEStop()); + table->GetEntry("fms?").SetBoolean(HALSIM_GetDriverStationFmsAttached()); + table->GetEntry("ds?").SetBoolean(HALSIM_GetDriverStationDsAttached()); + table->GetEntry("match_time").SetDouble(HALSIM_GetDriverStationMatchTime()); + + // TODO: Joysticks + + auto alliance = table->GetSubTable("alliance"); + auto allianceValue = HALSIM_GetDriverStationAllianceStationId(); + alliance->GetEntry("color").SetString( + (allianceValue == HAL_AllianceStationID_kRed1 || + allianceValue == HAL_AllianceStationID_kRed2 || + allianceValue == HAL_AllianceStationID_kRed3) + ? "red" + : "blue"); + int station = 0; + + switch (allianceValue) { + case HAL_AllianceStationID_kRed1: + case HAL_AllianceStationID_kBlue1: + station = 1; + break; + case HAL_AllianceStationID_kRed2: + case HAL_AllianceStationID_kBlue2: + station = 2; + break; + case HAL_AllianceStationID_kRed3: + case HAL_AllianceStationID_kBlue3: + station = 3; + break; + } + alliance->GetEntry("station").SetDouble(station); +} diff --git a/simulation/halsim_lowfi/src/main/native/cpp/NTProvider_Encoder.cpp b/simulation/halsim_lowfi/src/main/native/cpp/NTProvider_Encoder.cpp new file mode 100644 index 0000000000..0d85723066 --- /dev/null +++ b/simulation/halsim_lowfi/src/main/native/cpp/NTProvider_Encoder.cpp @@ -0,0 +1,46 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2017 FIRST. 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 the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#include "NTProvider_Encoder.h" + +#include +#include + +void HALSimNTProviderEncoder::Initialize() { + InitializeDefault(HAL_GetNumEncoders(), HALSIM_RegisterEncoderAllCallbacks); +} + +void HALSimNTProviderEncoder::OnCallback( + uint32_t chan, std::shared_ptr table) { + table->GetEntry("init?").SetBoolean(HALSIM_GetEncoderInitialized(chan)); + table->GetEntry("count").SetDouble(HALSIM_GetEncoderCount(chan)); + table->GetEntry("period").SetDouble(HALSIM_GetEncoderPeriod(chan)); + table->GetEntry("reset?").SetBoolean(HALSIM_GetEncoderReset(chan)); + table->GetEntry("max_period").SetDouble(HALSIM_GetEncoderMaxPeriod(chan)); + table->GetEntry("direction").SetBoolean(HALSIM_GetEncoderDirection(chan)); + table->GetEntry("reverse_direction?") + .SetBoolean(HALSIM_GetEncoderReverseDirection(chan)); + table->GetEntry("samples_to_avg") + .SetDouble(HALSIM_GetEncoderSamplesToAverage(chan)); +} + +void HALSimNTProviderEncoder::OnInitializedChannel( + uint32_t chan, std::shared_ptr table) { + table->GetEntry("count").AddListener( + [=](const nt::EntryNotification& ev) -> void { + HALSIM_SetEncoderCount(chan, + static_cast(ev.value->GetDouble())); + }, + NT_NotifyKind::NT_NOTIFY_UPDATE); + + table->GetEntry("direction") + .AddListener( + [=](const nt::EntryNotification& ev) -> void { + HALSIM_SetEncoderDirection(chan, ev.value->GetBoolean()); + }, + NT_NotifyKind::NT_NOTIFY_UPDATE); +} diff --git a/simulation/halsim_lowfi/src/main/native/cpp/NTProvider_PWM.cpp b/simulation/halsim_lowfi/src/main/native/cpp/NTProvider_PWM.cpp new file mode 100644 index 0000000000..b68307f500 --- /dev/null +++ b/simulation/halsim_lowfi/src/main/native/cpp/NTProvider_PWM.cpp @@ -0,0 +1,25 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2017 FIRST. 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 the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#include "NTProvider_PWM.h" + +#include +#include + +void HALSimNTProviderPWM::Initialize() { + InitializeDefault(HAL_GetNumPWMChannels(), HALSIM_RegisterPWMAllCallbacks); +} + +void HALSimNTProviderPWM::OnCallback(uint32_t chan, + std::shared_ptr table) { + table->GetEntry("init?").SetBoolean(HALSIM_GetPWMInitialized(chan)); + table->GetEntry("speed").SetDouble(HALSIM_GetPWMSpeed(chan)); + table->GetEntry("position").SetDouble(HALSIM_GetPWMPosition(chan)); + table->GetEntry("raw").SetDouble(HALSIM_GetPWMRawValue(chan)); + table->GetEntry("period_scale").SetDouble(HALSIM_GetPWMPeriodScale(chan)); + table->GetEntry("zero_latch?").SetBoolean(HALSIM_GetPWMZeroLatch(chan)); +} diff --git a/simulation/halsim_lowfi/src/main/native/cpp/NTProvider_Relay.cpp b/simulation/halsim_lowfi/src/main/native/cpp/NTProvider_Relay.cpp new file mode 100644 index 0000000000..f56a2c1298 --- /dev/null +++ b/simulation/halsim_lowfi/src/main/native/cpp/NTProvider_Relay.cpp @@ -0,0 +1,25 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2017 FIRST. 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 the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#include "NTProvider_Relay.h" + +#include +#include + +void HALSimNTProviderRelay::Initialize() { + InitializeDefault(HAL_GetNumRelayHeaders(), HALSIM_RegisterRelayAllCallbacks); +} + +void HALSimNTProviderRelay::OnCallback( + uint32_t chan, std::shared_ptr table) { + table->GetEntry("init_fwd?") + .SetBoolean(HALSIM_GetRelayInitializedForward(chan)); + table->GetEntry("init_rvs?") + .SetBoolean(HALSIM_GetRelayInitializedReverse(chan)); + table->GetEntry("fwd?").SetBoolean(HALSIM_GetRelayForward(chan)); + table->GetEntry("rvs?").SetBoolean(HALSIM_GetRelayReverse(chan)); +} diff --git a/simulation/halsim_lowfi/src/main/native/cpp/NTProvider_RoboRIO.cpp b/simulation/halsim_lowfi/src/main/native/cpp/NTProvider_RoboRIO.cpp new file mode 100644 index 0000000000..fb3c7b17b7 --- /dev/null +++ b/simulation/halsim_lowfi/src/main/native/cpp/NTProvider_RoboRIO.cpp @@ -0,0 +1,51 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2017 FIRST. 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 the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#include "NTProvider_RoboRIO.h" + +#include +#include + +void HALSimNTProviderRoboRIO::Initialize() { + InitializeDefault(1, HALSIM_RegisterRoboRioAllCallbacks); +} + +void HALSimNTProviderRoboRIO::OnCallback( + uint32_t chan, std::shared_ptr table) { + table->GetEntry("fpga_button?").SetBoolean(HALSIM_GetRoboRioFPGAButton(chan)); + + table->GetEntry("vin_voltage").SetDouble(HALSIM_GetRoboRioVInVoltage(chan)); + table->GetEntry("vin_current").SetDouble(HALSIM_GetRoboRioVInCurrent(chan)); + + auto t6v = table->GetSubTable("6V"); + t6v->GetEntry("voltage").SetDouble(HALSIM_GetRoboRioUserVoltage6V(chan)); + t6v->GetEntry("current").SetDouble(HALSIM_GetRoboRioUserCurrent6V(chan)); + t6v->GetEntry("active?").SetBoolean(HALSIM_GetRoboRioUserActive6V(chan)); + t6v->GetEntry("faults").SetDouble(HALSIM_GetRoboRioUserFaults6V(chan)); + + auto t5v = table->GetSubTable("5V"); + t5v->GetEntry("voltage").SetDouble(HALSIM_GetRoboRioUserVoltage5V(chan)); + t5v->GetEntry("current").SetDouble(HALSIM_GetRoboRioUserCurrent5V(chan)); + t5v->GetEntry("active?").SetBoolean(HALSIM_GetRoboRioUserActive5V(chan)); + t5v->GetEntry("faults").SetDouble(HALSIM_GetRoboRioUserFaults5V(chan)); + + auto t3v3 = table->GetSubTable("3V3"); + t3v3->GetEntry("voltage").SetDouble(HALSIM_GetRoboRioUserVoltage3V3(chan)); + t3v3->GetEntry("current").SetDouble(HALSIM_GetRoboRioUserCurrent3V3(chan)); + t3v3->GetEntry("active?").SetBoolean(HALSIM_GetRoboRioUserActive3V3(chan)); + t3v3->GetEntry("faults").SetDouble(HALSIM_GetRoboRioUserFaults3V3(chan)); +} + +void HALSimNTProviderRoboRIO::OnInitializedChannel( + uint32_t chan, std::shared_ptr table) { + table->GetEntry("fpga_button?") + .AddListener( + [=](const nt::EntryNotification& ev) -> void { + HALSIM_SetRoboRioFPGAButton(chan, ev.value->GetBoolean()); + }, + NT_NotifyKind::NT_NOTIFY_UPDATE); +} diff --git a/simulation/halsim_lowfi/src/main/native/cpp/NTProvider_dPWM.cpp b/simulation/halsim_lowfi/src/main/native/cpp/NTProvider_dPWM.cpp new file mode 100644 index 0000000000..cb8cf807ac --- /dev/null +++ b/simulation/halsim_lowfi/src/main/native/cpp/NTProvider_dPWM.cpp @@ -0,0 +1,23 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2017 FIRST. 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 the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#include "NTProvider_dPWM.h" + +#include +#include + +void HALSimNTProviderDigitalPWM::Initialize() { + InitializeDefault(HAL_GetNumDigitalPWMOutputs(), + HALSIM_RegisterDigitalPWMAllCallbacks); +} + +void HALSimNTProviderDigitalPWM::OnCallback( + uint32_t chan, std::shared_ptr table) { + table->GetEntry("init?").SetBoolean(HALSIM_GetDigitalPWMInitialized(chan)); + table->GetEntry("dio_pin").SetDouble(HALSIM_GetDigitalPWMPin(chan)); + table->GetEntry("duty_cycle").SetDouble(HALSIM_GetDigitalPWMDutyCycle(chan)); +} diff --git a/simulation/halsim_lowfi/src/main/native/cpp/main.cpp b/simulation/halsim_lowfi/src/main/native/cpp/main.cpp new file mode 100644 index 0000000000..4dcb630d96 --- /dev/null +++ b/simulation/halsim_lowfi/src/main/native/cpp/main.cpp @@ -0,0 +1,56 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2017 FIRST. 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 the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static HALSimLowFi halsim_lowfi; + +static HALSimNTProviderPWM pwm_provider; +static HALSimNTProviderDigitalPWM dpwm_provider; +static HALSimNTProviderDIO dio_provider; +static HALSimNTProviderAnalogIn ai_provider; +static HALSimNTProviderAnalogOut ao_provider; +static HALSimNTProviderDriverStation ds_provider; +static HALSimNTProviderEncoder encoder_provider; +static HALSimNTProviderRelay relay_provider; +static HALSimNTProviderRoboRIO roborio_provider; + +extern "C" { +#if defined(WIN32) || defined(_WIN32) +__declspec(dllexport) +#endif + int HALSIM_InitExtension(void) { + std::cout << "NetworkTables LowFi Simulator Initializing." << std::endl; + halsim_lowfi.Initialize(); + halsim_lowfi.table->GetInstance().StartServer("networktables.ini"); + halsim_lowfi.table->GetInstance().SetUpdateRate(0.05); + auto lowfi = std::make_shared(halsim_lowfi); + + pwm_provider.Inject(lowfi, "PWM"); + dpwm_provider.Inject(lowfi, "dPWM"); + dio_provider.Inject(lowfi, "DIO"); + ai_provider.Inject(lowfi, "AI"); + ao_provider.Inject(lowfi, "AO"); + ds_provider.Inject(lowfi, "DriverStation"); + encoder_provider.Inject(lowfi, "Encoder"); + relay_provider.Inject(lowfi, "Relay"); + roborio_provider.Inject(lowfi, "RoboRIO"); + + std::cout << "NetworkTables LowFi Simulator Initialized!" << std::endl; + return 0; +} +} // extern "C" diff --git a/simulation/halsim_lowfi/src/main/native/include/HALSimLowFi.h b/simulation/halsim_lowfi/src/main/native/include/HALSimLowFi.h new file mode 100644 index 0000000000..c4f58dad49 --- /dev/null +++ b/simulation/halsim_lowfi/src/main/native/include/HALSimLowFi.h @@ -0,0 +1,58 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2017 FIRST. 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 the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#pragma once + +#include +#include +#include +#include + +#include +#include + +class HALSimLowFi { + public: + std::shared_ptr table; + void Initialize(); +}; + +typedef void (*HALCbRegisterIndexedFunc)(int32_t index, + HAL_NotifyCallback callback, + void* param, HAL_Bool initialNotify); +typedef void (*HALCbRegisterSingleFunc)(HAL_NotifyCallback callback, + void* param, HAL_Bool initialNotify); + +void NTProviderBaseCallback(const char* name, void* param, + const struct HAL_Value* value); + +class HALSimNTProvider { + public: + struct NTProviderCallbackInfo { + HALSimNTProvider* provider; + std::shared_ptr table; + int channel; + }; + + void Inject(std::shared_ptr parent, std::string table); + // Initialize is called by inject. + virtual void Initialize() = 0; + virtual void InitializeDefault(int numChannels, + HALCbRegisterIndexedFunc registerFunc); + virtual void InitializeDefaultSingle(HALCbRegisterSingleFunc registerFunc); + virtual void OnCallback(uint32_t channel, + std::shared_ptr table) = 0; + virtual void OnInitializedChannel(uint32_t channel, + std::shared_ptr table); + + int numChannels; + std::string tableName; + + std::shared_ptr parent; + std::shared_ptr table; + std::vector cbInfos; +}; diff --git a/simulation/halsim_lowfi/src/main/native/include/NTProvider_Analog.h b/simulation/halsim_lowfi/src/main/native/include/NTProvider_Analog.h new file mode 100644 index 0000000000..e03f8c62f8 --- /dev/null +++ b/simulation/halsim_lowfi/src/main/native/include/NTProvider_Analog.h @@ -0,0 +1,28 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2017 FIRST. 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 the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#pragma once + +#include + +#include + +class HALSimNTProviderAnalogIn : public HALSimNTProvider { + public: + void Initialize() override; + void OnCallback(uint32_t channel, + std::shared_ptr table) override; + void OnInitializedChannel(uint32_t channel, + std::shared_ptr table) override; +}; + +class HALSimNTProviderAnalogOut : public HALSimNTProvider { + public: + void Initialize() override; + void OnCallback(uint32_t channel, + std::shared_ptr table) override; +}; diff --git a/simulation/halsim_lowfi/src/main/native/include/NTProvider_DIO.h b/simulation/halsim_lowfi/src/main/native/include/NTProvider_DIO.h new file mode 100644 index 0000000000..9ca9e96466 --- /dev/null +++ b/simulation/halsim_lowfi/src/main/native/include/NTProvider_DIO.h @@ -0,0 +1,21 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2017 FIRST. 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 the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#pragma once + +#include + +#include + +class HALSimNTProviderDIO : public HALSimNTProvider { + public: + void Initialize() override; + void OnCallback(uint32_t channel, + std::shared_ptr table) override; + void OnInitializedChannel(uint32_t channel, + std::shared_ptr table) override; +}; diff --git a/simulation/halsim_lowfi/src/main/native/include/NTProvider_DriverStation.h b/simulation/halsim_lowfi/src/main/native/include/NTProvider_DriverStation.h new file mode 100644 index 0000000000..42c2c0992d --- /dev/null +++ b/simulation/halsim_lowfi/src/main/native/include/NTProvider_DriverStation.h @@ -0,0 +1,19 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2017 FIRST. 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 the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#pragma once + +#include + +#include + +class HALSimNTProviderDriverStation : public HALSimNTProvider { + public: + void Initialize() override; + void OnCallback(uint32_t channel, + std::shared_ptr table) override; +}; diff --git a/simulation/halsim_lowfi/src/main/native/include/NTProvider_Encoder.h b/simulation/halsim_lowfi/src/main/native/include/NTProvider_Encoder.h new file mode 100644 index 0000000000..c47309ca71 --- /dev/null +++ b/simulation/halsim_lowfi/src/main/native/include/NTProvider_Encoder.h @@ -0,0 +1,21 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2017 FIRST. 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 the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#pragma once + +#include + +#include + +class HALSimNTProviderEncoder : public HALSimNTProvider { + public: + void Initialize() override; + void OnCallback(uint32_t channel, + std::shared_ptr table) override; + void OnInitializedChannel(uint32_t channel, + std::shared_ptr table) override; +}; diff --git a/simulation/halsim_lowfi/src/main/native/include/NTProvider_PWM.h b/simulation/halsim_lowfi/src/main/native/include/NTProvider_PWM.h new file mode 100644 index 0000000000..0082a0c836 --- /dev/null +++ b/simulation/halsim_lowfi/src/main/native/include/NTProvider_PWM.h @@ -0,0 +1,19 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2017 FIRST. 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 the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#pragma once + +#include + +#include + +class HALSimNTProviderPWM : public HALSimNTProvider { + public: + void Initialize() override; + void OnCallback(uint32_t channel, + std::shared_ptr table) override; +}; diff --git a/simulation/halsim_lowfi/src/main/native/include/NTProvider_Relay.h b/simulation/halsim_lowfi/src/main/native/include/NTProvider_Relay.h new file mode 100644 index 0000000000..01f4400936 --- /dev/null +++ b/simulation/halsim_lowfi/src/main/native/include/NTProvider_Relay.h @@ -0,0 +1,19 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2017 FIRST. 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 the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#pragma once + +#include + +#include + +class HALSimNTProviderRelay : public HALSimNTProvider { + public: + void Initialize() override; + void OnCallback(uint32_t channel, + std::shared_ptr table) override; +}; diff --git a/simulation/halsim_lowfi/src/main/native/include/NTProvider_RoboRIO.h b/simulation/halsim_lowfi/src/main/native/include/NTProvider_RoboRIO.h new file mode 100644 index 0000000000..1b1ad69cbc --- /dev/null +++ b/simulation/halsim_lowfi/src/main/native/include/NTProvider_RoboRIO.h @@ -0,0 +1,21 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2017 FIRST. 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 the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#pragma once + +#include + +#include + +class HALSimNTProviderRoboRIO : public HALSimNTProvider { + public: + void Initialize() override; + void OnCallback(uint32_t channel, + std::shared_ptr table) override; + void OnInitializedChannel(uint32_t channel, + std::shared_ptr table) override; +}; diff --git a/simulation/halsim_lowfi/src/main/native/include/NTProvider_dPWM.h b/simulation/halsim_lowfi/src/main/native/include/NTProvider_dPWM.h new file mode 100644 index 0000000000..30146edcd4 --- /dev/null +++ b/simulation/halsim_lowfi/src/main/native/include/NTProvider_dPWM.h @@ -0,0 +1,19 @@ +/*----------------------------------------------------------------------------*/ +/* Copyright (c) 2017 FIRST. 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 the root directory of */ +/* the project. */ +/*----------------------------------------------------------------------------*/ + +#pragma once + +#include + +#include + +class HALSimNTProviderDigitalPWM : public HALSimNTProvider { + public: + void Initialize() override; + void OnCallback(uint32_t channel, + std::shared_ptr table) override; +};