mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-26 01:51:41 +00:00
Add Low Fidelity NetworkTables simulation extension (#823)
This commit is contained in:
62
simulation/halsim_lowfi/src/main/native/cpp/HALSimLowFi.cpp
Normal file
62
simulation/halsim_lowfi/src/main/native/cpp/HALSimLowFi.cpp
Normal file
@@ -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 <llvm/Twine.h>
|
||||
|
||||
void HALSimLowFi::Initialize() {
|
||||
table = nt::NetworkTableInstance::GetDefault().GetTable("sim");
|
||||
}
|
||||
|
||||
void HALSimNTProvider::Inject(std::shared_ptr<HALSimLowFi> 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<struct HALSimNTProvider::NTProviderCallbackInfo*>(param);
|
||||
uint32_t chan = static_cast<uint32_t>(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<nt::NetworkTable> table) {}
|
||||
@@ -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 <HAL/Ports.h>
|
||||
#include <MockData/AnalogInData.h>
|
||||
#include <MockData/AnalogOutData.h>
|
||||
|
||||
void HALSimNTProviderAnalogIn::Initialize() {
|
||||
InitializeDefault(HAL_GetNumAnalogInputs(),
|
||||
HALSIM_RegisterAnalogInAllCallbacks);
|
||||
}
|
||||
|
||||
void HALSimNTProviderAnalogIn::OnCallback(
|
||||
uint32_t chan, std::shared_ptr<nt::NetworkTable> 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<nt::NetworkTable> 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<nt::NetworkTable> table) {
|
||||
table->GetEntry("init?").SetBoolean(HALSIM_GetAnalogOutInitialized(chan));
|
||||
table->GetEntry("voltage").SetDouble(HALSIM_GetAnalogOutVoltage(chan));
|
||||
}
|
||||
@@ -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 <HAL/Ports.h>
|
||||
#include <MockData/DIOData.h>
|
||||
|
||||
void HALSimNTProviderDIO::Initialize() {
|
||||
InitializeDefault(HAL_GetNumDigitalChannels(),
|
||||
HALSIM_RegisterDIOAllCallbacks);
|
||||
}
|
||||
|
||||
void HALSimNTProviderDIO::OnCallback(uint32_t chan,
|
||||
std::shared_ptr<nt::NetworkTable> 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<nt::NetworkTable> 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);
|
||||
}
|
||||
@@ -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 <HAL/Ports.h>
|
||||
#include <MockData/DriverStationData.h>
|
||||
|
||||
void HALSimNTProviderDriverStation::Initialize() {
|
||||
InitializeDefaultSingle(HALSIM_RegisterDriverStationAllCallbacks);
|
||||
}
|
||||
|
||||
void HALSimNTProviderDriverStation::OnCallback(
|
||||
uint32_t chan, std::shared_ptr<nt::NetworkTable> 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);
|
||||
}
|
||||
@@ -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 <HAL/Ports.h>
|
||||
#include <MockData/EncoderData.h>
|
||||
|
||||
void HALSimNTProviderEncoder::Initialize() {
|
||||
InitializeDefault(HAL_GetNumEncoders(), HALSIM_RegisterEncoderAllCallbacks);
|
||||
}
|
||||
|
||||
void HALSimNTProviderEncoder::OnCallback(
|
||||
uint32_t chan, std::shared_ptr<nt::NetworkTable> 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<nt::NetworkTable> table) {
|
||||
table->GetEntry("count").AddListener(
|
||||
[=](const nt::EntryNotification& ev) -> void {
|
||||
HALSIM_SetEncoderCount(chan,
|
||||
static_cast<int32_t>(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);
|
||||
}
|
||||
@@ -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 <HAL/Ports.h>
|
||||
#include <MockData/PWMData.h>
|
||||
|
||||
void HALSimNTProviderPWM::Initialize() {
|
||||
InitializeDefault(HAL_GetNumPWMChannels(), HALSIM_RegisterPWMAllCallbacks);
|
||||
}
|
||||
|
||||
void HALSimNTProviderPWM::OnCallback(uint32_t chan,
|
||||
std::shared_ptr<nt::NetworkTable> 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));
|
||||
}
|
||||
@@ -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 <HAL/Ports.h>
|
||||
#include <MockData/RelayData.h>
|
||||
|
||||
void HALSimNTProviderRelay::Initialize() {
|
||||
InitializeDefault(HAL_GetNumRelayHeaders(), HALSIM_RegisterRelayAllCallbacks);
|
||||
}
|
||||
|
||||
void HALSimNTProviderRelay::OnCallback(
|
||||
uint32_t chan, std::shared_ptr<nt::NetworkTable> 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));
|
||||
}
|
||||
@@ -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 <HAL/Ports.h>
|
||||
#include <MockData/RoboRioData.h>
|
||||
|
||||
void HALSimNTProviderRoboRIO::Initialize() {
|
||||
InitializeDefault(1, HALSIM_RegisterRoboRioAllCallbacks);
|
||||
}
|
||||
|
||||
void HALSimNTProviderRoboRIO::OnCallback(
|
||||
uint32_t chan, std::shared_ptr<nt::NetworkTable> 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<nt::NetworkTable> table) {
|
||||
table->GetEntry("fpga_button?")
|
||||
.AddListener(
|
||||
[=](const nt::EntryNotification& ev) -> void {
|
||||
HALSIM_SetRoboRioFPGAButton(chan, ev.value->GetBoolean());
|
||||
},
|
||||
NT_NotifyKind::NT_NOTIFY_UPDATE);
|
||||
}
|
||||
@@ -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 <HAL/Ports.h>
|
||||
#include <MockData/DigitalPWMData.h>
|
||||
|
||||
void HALSimNTProviderDigitalPWM::Initialize() {
|
||||
InitializeDefault(HAL_GetNumDigitalPWMOutputs(),
|
||||
HALSIM_RegisterDigitalPWMAllCallbacks);
|
||||
}
|
||||
|
||||
void HALSimNTProviderDigitalPWM::OnCallback(
|
||||
uint32_t chan, std::shared_ptr<nt::NetworkTable> table) {
|
||||
table->GetEntry("init?").SetBoolean(HALSIM_GetDigitalPWMInitialized(chan));
|
||||
table->GetEntry("dio_pin").SetDouble(HALSIM_GetDigitalPWMPin(chan));
|
||||
table->GetEntry("duty_cycle").SetDouble(HALSIM_GetDigitalPWMDutyCycle(chan));
|
||||
}
|
||||
56
simulation/halsim_lowfi/src/main/native/cpp/main.cpp
Normal file
56
simulation/halsim_lowfi/src/main/native/cpp/main.cpp
Normal file
@@ -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 <iostream>
|
||||
|
||||
#include <HALSimLowFi.h>
|
||||
#include <NTProvider_Analog.h>
|
||||
#include <NTProvider_DIO.h>
|
||||
#include <NTProvider_DriverStation.h>
|
||||
#include <NTProvider_Encoder.h>
|
||||
#include <NTProvider_PWM.h>
|
||||
#include <NTProvider_Relay.h>
|
||||
#include <NTProvider_RoboRIO.h>
|
||||
#include <NTProvider_dPWM.h>
|
||||
|
||||
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<HALSimLowFi>(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"
|
||||
@@ -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 <cinttypes>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <MockData/NotifyListener.h>
|
||||
#include <networktables/NetworkTableInstance.h>
|
||||
|
||||
class HALSimLowFi {
|
||||
public:
|
||||
std::shared_ptr<nt::NetworkTable> 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<nt::NetworkTable> table;
|
||||
int channel;
|
||||
};
|
||||
|
||||
void Inject(std::shared_ptr<HALSimLowFi> 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<nt::NetworkTable> table) = 0;
|
||||
virtual void OnInitializedChannel(uint32_t channel,
|
||||
std::shared_ptr<nt::NetworkTable> table);
|
||||
|
||||
int numChannels;
|
||||
std::string tableName;
|
||||
|
||||
std::shared_ptr<HALSimLowFi> parent;
|
||||
std::shared_ptr<nt::NetworkTable> table;
|
||||
std::vector<NTProviderCallbackInfo> cbInfos;
|
||||
};
|
||||
@@ -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 <memory>
|
||||
|
||||
#include <HALSimLowFi.h>
|
||||
|
||||
class HALSimNTProviderAnalogIn : public HALSimNTProvider {
|
||||
public:
|
||||
void Initialize() override;
|
||||
void OnCallback(uint32_t channel,
|
||||
std::shared_ptr<nt::NetworkTable> table) override;
|
||||
void OnInitializedChannel(uint32_t channel,
|
||||
std::shared_ptr<nt::NetworkTable> table) override;
|
||||
};
|
||||
|
||||
class HALSimNTProviderAnalogOut : public HALSimNTProvider {
|
||||
public:
|
||||
void Initialize() override;
|
||||
void OnCallback(uint32_t channel,
|
||||
std::shared_ptr<nt::NetworkTable> table) override;
|
||||
};
|
||||
@@ -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 <memory>
|
||||
|
||||
#include <HALSimLowFi.h>
|
||||
|
||||
class HALSimNTProviderDIO : public HALSimNTProvider {
|
||||
public:
|
||||
void Initialize() override;
|
||||
void OnCallback(uint32_t channel,
|
||||
std::shared_ptr<nt::NetworkTable> table) override;
|
||||
void OnInitializedChannel(uint32_t channel,
|
||||
std::shared_ptr<nt::NetworkTable> table) override;
|
||||
};
|
||||
@@ -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 <memory>
|
||||
|
||||
#include <HALSimLowFi.h>
|
||||
|
||||
class HALSimNTProviderDriverStation : public HALSimNTProvider {
|
||||
public:
|
||||
void Initialize() override;
|
||||
void OnCallback(uint32_t channel,
|
||||
std::shared_ptr<nt::NetworkTable> table) override;
|
||||
};
|
||||
@@ -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 <memory>
|
||||
|
||||
#include <HALSimLowFi.h>
|
||||
|
||||
class HALSimNTProviderEncoder : public HALSimNTProvider {
|
||||
public:
|
||||
void Initialize() override;
|
||||
void OnCallback(uint32_t channel,
|
||||
std::shared_ptr<nt::NetworkTable> table) override;
|
||||
void OnInitializedChannel(uint32_t channel,
|
||||
std::shared_ptr<nt::NetworkTable> table) override;
|
||||
};
|
||||
@@ -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 <memory>
|
||||
|
||||
#include <HALSimLowFi.h>
|
||||
|
||||
class HALSimNTProviderPWM : public HALSimNTProvider {
|
||||
public:
|
||||
void Initialize() override;
|
||||
void OnCallback(uint32_t channel,
|
||||
std::shared_ptr<nt::NetworkTable> table) override;
|
||||
};
|
||||
@@ -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 <memory>
|
||||
|
||||
#include <HALSimLowFi.h>
|
||||
|
||||
class HALSimNTProviderRelay : public HALSimNTProvider {
|
||||
public:
|
||||
void Initialize() override;
|
||||
void OnCallback(uint32_t channel,
|
||||
std::shared_ptr<nt::NetworkTable> table) override;
|
||||
};
|
||||
@@ -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 <memory>
|
||||
|
||||
#include <HALSimLowFi.h>
|
||||
|
||||
class HALSimNTProviderRoboRIO : public HALSimNTProvider {
|
||||
public:
|
||||
void Initialize() override;
|
||||
void OnCallback(uint32_t channel,
|
||||
std::shared_ptr<nt::NetworkTable> table) override;
|
||||
void OnInitializedChannel(uint32_t channel,
|
||||
std::shared_ptr<nt::NetworkTable> table) override;
|
||||
};
|
||||
@@ -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 <memory>
|
||||
|
||||
#include <HALSimLowFi.h>
|
||||
|
||||
class HALSimNTProviderDigitalPWM : public HALSimNTProvider {
|
||||
public:
|
||||
void Initialize() override;
|
||||
void OnCallback(uint32_t channel,
|
||||
std::shared_ptr<nt::NetworkTable> table) override;
|
||||
};
|
||||
Reference in New Issue
Block a user