Simplify Sendable interface (#1864)

This removes the name and subsystem from individual objects, and instead
puts this data into a new singleton class, SendableRegistry.  Much of
LiveWindow has been refactored into SendableRegistry.

In C++, a new CRTP helper class, SendableHelper, has been added to provide
move and destruction functionality.

Shims for GetName, SetName, GetSubsystem, and SetSubsystem have been added
to Command and Subsystem (both old and new), and also to SendableHelper to
prevent code breakage.

This deprecates SendableBase in preparation for future removal.
This commit is contained in:
Peter Johnson
2019-09-14 15:22:54 -05:00
committed by GitHub
parent 1d8c4d016f
commit 471f375a38
216 changed files with 2448 additions and 1433 deletions

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2008-2019 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. */
@@ -10,6 +10,7 @@
#include <hal/HAL.h>
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -22,7 +23,8 @@ ADXL345_I2C::ADXL345_I2C(I2C::Port port, Range range, int deviceAddress)
HAL_Report(HALUsageReporting::kResourceType_ADXL345,
HALUsageReporting::kADXL345_I2C, 0);
SetName("ADXL345_I2C", port);
SendableRegistry::GetInstance().AddLW(this, "ADXL345_I2C", port);
}
void ADXL345_I2C::SetRange(Range range) {

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2008-2019 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. */
@@ -10,6 +10,7 @@
#include <hal/HAL.h>
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -32,7 +33,7 @@ ADXL345_SPI::ADXL345_SPI(SPI::Port port, ADXL345_SPI::Range range)
HAL_Report(HALUsageReporting::kResourceType_ADXL345,
HALUsageReporting::kADXL345_SPI);
SetName("ADXL345_SPI", port);
SendableRegistry::GetInstance().AddLW(this, "ADXL345_SPI", port);
}
void ADXL345_SPI::SetRange(Range range) {

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2008-2019 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. */
@@ -11,6 +11,7 @@
#include "frc/DriverStation.h"
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -62,7 +63,7 @@ ADXL362::ADXL362(SPI::Port port, Range range) : m_spi(port) {
HAL_Report(HALUsageReporting::kResourceType_ADXL362, port);
SetName("ADXL362", port);
SendableRegistry::GetInstance().AddLW(this, "ADXL362", port);
}
void ADXL362::SetRange(Range range) {

View File

@@ -11,6 +11,7 @@
#include "frc/DriverStation.h"
#include "frc/Timer.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -49,7 +50,8 @@ ADXRS450_Gyro::ADXRS450_Gyro(SPI::Port port) : m_spi(port) {
Calibrate();
HAL_Report(HALUsageReporting::kResourceType_ADXRS450, port);
SetName("ADXRS450_Gyro", port);
SendableRegistry::GetInstance().AddLW(this, "ADXRS450_Gyro", port);
}
static bool CalcParity(int v) {

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2008-2019 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. */
@@ -11,12 +11,13 @@
#include "frc/WPIErrors.h"
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
AnalogAccelerometer::AnalogAccelerometer(int channel)
: AnalogAccelerometer(std::make_shared<AnalogInput>(channel)) {
AddChild(m_analogInput);
SendableRegistry::GetInstance().AddChild(this, m_analogInput.get());
}
AnalogAccelerometer::AnalogAccelerometer(AnalogInput* channel)
@@ -58,5 +59,7 @@ void AnalogAccelerometer::InitSendable(SendableBuilder& builder) {
void AnalogAccelerometer::InitAccelerometer() {
HAL_Report(HALUsageReporting::kResourceType_Accelerometer,
m_analogInput->GetChannel());
SetName("Accelerometer", m_analogInput->GetChannel());
SendableRegistry::GetInstance().AddLW(this, "Accelerometer",
m_analogInput->GetChannel());
}

View File

@@ -17,12 +17,13 @@
#include "frc/AnalogInput.h"
#include "frc/Timer.h"
#include "frc/WPIErrors.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
AnalogGyro::AnalogGyro(int channel)
: AnalogGyro(std::make_shared<AnalogInput>(channel)) {
AddChild(m_analog);
SendableRegistry::GetInstance().AddChild(this, m_analog.get());
}
AnalogGyro::AnalogGyro(AnalogInput* channel)
@@ -41,7 +42,7 @@ AnalogGyro::AnalogGyro(std::shared_ptr<AnalogInput> channel)
AnalogGyro::AnalogGyro(int channel, int center, double offset)
: AnalogGyro(std::make_shared<AnalogInput>(channel), center, offset) {
AddChild(m_analog);
SendableRegistry::GetInstance().AddChild(this, m_analog.get());
}
AnalogGyro::AnalogGyro(std::shared_ptr<AnalogInput> channel, int center,
@@ -148,7 +149,9 @@ void AnalogGyro::InitGyro() {
}
HAL_Report(HALUsageReporting::kResourceType_Gyro, m_analog->GetChannel());
SetName("AnalogGyro", m_analog->GetChannel());
SendableRegistry::GetInstance().AddLW(this, "AnalogGyro",
m_analog->GetChannel());
}
void AnalogGyro::Calibrate() {

View File

@@ -18,6 +18,7 @@
#include "frc/Timer.h"
#include "frc/WPIErrors.h"
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -42,7 +43,8 @@ AnalogInput::AnalogInput(int channel) {
}
HAL_Report(HALUsageReporting::kResourceType_AnalogChannel, channel);
SetName("AnalogInput", channel);
SendableRegistry::GetInstance().AddLW(this, "AnalogInput", channel);
}
AnalogInput::~AnalogInput() { HAL_FreeAnalogInputPort(m_port); }

View File

@@ -16,6 +16,7 @@
#include "frc/SensorUtil.h"
#include "frc/WPIErrors.h"
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -42,7 +43,7 @@ AnalogOutput::AnalogOutput(int channel) {
}
HAL_Report(HALUsageReporting::kResourceType_AnalogOutput, m_channel);
SetName("AnalogOutput", m_channel);
SendableRegistry::GetInstance().AddLW(this, "AnalogOutput", m_channel);
}
AnalogOutput::~AnalogOutput() { HAL_FreeAnalogOutputPort(m_port); }

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2016-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2016-2019 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. */
@@ -9,26 +9,29 @@
#include "frc/RobotController.h"
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
AnalogPotentiometer::AnalogPotentiometer(int channel, double fullRange,
double offset)
: m_analog_input(std::make_shared<AnalogInput>(channel)),
m_fullRange(fullRange),
m_offset(offset) {
AddChild(m_analog_input);
: AnalogPotentiometer(std::make_shared<AnalogInput>(channel), fullRange,
offset) {
SendableRegistry::GetInstance().AddChild(this, m_analog_input.get());
}
AnalogPotentiometer::AnalogPotentiometer(AnalogInput* input, double fullRange,
double offset)
: m_analog_input(input, NullDeleter<AnalogInput>()),
m_fullRange(fullRange),
m_offset(offset) {}
: AnalogPotentiometer(
std::shared_ptr<AnalogInput>(input, NullDeleter<AnalogInput>()),
fullRange, offset) {}
AnalogPotentiometer::AnalogPotentiometer(std::shared_ptr<AnalogInput> input,
double fullRange, double offset)
: m_analog_input(input), m_fullRange(fullRange), m_offset(offset) {}
: m_analog_input(input), m_fullRange(fullRange), m_offset(offset) {
SendableRegistry::GetInstance().AddLW(this, "AnalogPotentiometer",
m_analog_input->GetChannel());
}
double AnalogPotentiometer::Get() const {
return (m_analog_input->GetVoltage() / RobotController::GetVoltage5V()) *

View File

@@ -13,13 +13,14 @@
#include "frc/AnalogInput.h"
#include "frc/WPIErrors.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
AnalogTrigger::AnalogTrigger(int channel)
: AnalogTrigger(new AnalogInput(channel)) {
m_ownsAnalog = true;
AddChild(m_analogInput);
SendableRegistry::GetInstance().AddChild(this, m_analogInput);
}
AnalogTrigger::AnalogTrigger(AnalogInput* input) {
@@ -36,7 +37,8 @@ AnalogTrigger::AnalogTrigger(AnalogInput* input) {
m_index = index;
HAL_Report(HALUsageReporting::kResourceType_AnalogTrigger, input->m_channel);
SetName("AnalogTrigger", input->GetChannel());
SendableRegistry::GetInstance().AddLW(this, "AnalogTrigger",
input->GetChannel());
}
AnalogTrigger::~AnalogTrigger() {
@@ -50,7 +52,7 @@ AnalogTrigger::~AnalogTrigger() {
AnalogTrigger::AnalogTrigger(AnalogTrigger&& rhs)
: ErrorBase(std::move(rhs)),
SendableBase(std::move(rhs)),
SendableHelper(std::move(rhs)),
m_index(std::move(rhs.m_index)),
m_trigger(std::move(rhs.m_trigger)) {
std::swap(m_analogInput, rhs.m_analogInput);
@@ -59,7 +61,7 @@ AnalogTrigger::AnalogTrigger(AnalogTrigger&& rhs)
AnalogTrigger& AnalogTrigger::operator=(AnalogTrigger&& rhs) {
ErrorBase::operator=(std::move(rhs));
SendableBase::operator=(std::move(rhs));
SendableHelper::operator=(std::move(rhs));
m_index = std::move(rhs.m_index);
m_trigger = std::move(rhs.m_trigger);

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2014-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2014-2019 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. */
@@ -12,6 +12,7 @@
#include "frc/WPIErrors.h"
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -20,7 +21,7 @@ BuiltInAccelerometer::BuiltInAccelerometer(Range range) {
HAL_Report(HALUsageReporting::kResourceType_Accelerometer, 0, 0,
"Built-in accelerometer");
SetName("BuiltInAccel", 0);
SendableRegistry::GetInstance().AddLW(this, "BuiltInAccel");
}
void BuiltInAccelerometer::SetRange(Range range) {

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2014-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2014-2019 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. */
@@ -14,6 +14,7 @@
#include "frc/WPIErrors.h"
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -28,7 +29,7 @@ Compressor::Compressor(int pcmID) : m_module(pcmID) {
SetClosedLoopControl(true);
HAL_Report(HALUsageReporting::kResourceType_Compressor, pcmID);
SetName("Compressor", pcmID);
SendableRegistry::GetInstance().AddLW(this, "Compressor", pcmID);
}
void Compressor::Start() {

View File

@@ -15,6 +15,7 @@
#include "frc/DigitalInput.h"
#include "frc/WPIErrors.h"
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -26,7 +27,7 @@ Counter::Counter(Mode mode) {
SetMaxPeriod(.5);
HAL_Report(HALUsageReporting::kResourceType_Counter, m_index, mode);
SetName("Counter", m_index);
SendableRegistry::GetInstance().AddLW(this, "Counter", m_index);
}
Counter::Counter(int channel) : Counter(kTwoPulse) {
@@ -95,7 +96,7 @@ Counter::~Counter() {
void Counter::SetUpSource(int channel) {
if (StatusIsFatal()) return;
SetUpSource(std::make_shared<DigitalInput>(channel));
AddChild(m_upSource);
SendableRegistry::GetInstance().AddChild(this, m_upSource.get());
}
void Counter::SetUpSource(AnalogTrigger* analogTrigger,
@@ -159,7 +160,7 @@ void Counter::ClearUpSource() {
void Counter::SetDownSource(int channel) {
if (StatusIsFatal()) return;
SetDownSource(std::make_shared<DigitalInput>(channel));
AddChild(m_downSource);
SendableRegistry::GetInstance().AddChild(this, m_downSource.get());
}
void Counter::SetDownSource(AnalogTrigger* analogTrigger,

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2008-2019 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. */
@@ -9,6 +9,8 @@
#include <hal/HAL.h>
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
DMC60::DMC60(int channel) : PWMSpeedController(channel) {
@@ -32,5 +34,5 @@ DMC60::DMC60(int channel) : PWMSpeedController(channel) {
SetZeroLatch();
HAL_Report(HALUsageReporting::kResourceType_DigilentDMC60, GetChannel());
SetName("DMC60", GetChannel());
SendableRegistry::GetInstance().SetName(this, "DMC60", GetChannel());
}

View File

@@ -20,6 +20,7 @@
#include "frc/SensorUtil.h"
#include "frc/Utility.h"
#include "frc/WPIErrors.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -38,7 +39,8 @@ DigitalGlitchFilter::DigitalGlitchFilter() {
HAL_Report(HALUsageReporting::kResourceType_DigitalGlitchFilter,
m_channelIndex);
SetName("DigitalGlitchFilter", m_channelIndex);
SendableRegistry::GetInstance().AddLW(this, "DigitalGlitchFilter",
m_channelIndex);
}
DigitalGlitchFilter::~DigitalGlitchFilter() {
@@ -49,13 +51,13 @@ DigitalGlitchFilter::~DigitalGlitchFilter() {
}
DigitalGlitchFilter::DigitalGlitchFilter(DigitalGlitchFilter&& rhs)
: ErrorBase(std::move(rhs)), SendableBase(std::move(rhs)) {
: ErrorBase(std::move(rhs)), SendableHelper(std::move(rhs)) {
std::swap(m_channelIndex, rhs.m_channelIndex);
}
DigitalGlitchFilter& DigitalGlitchFilter::operator=(DigitalGlitchFilter&& rhs) {
ErrorBase::operator=(std::move(rhs));
SendableBase::operator=(std::move(rhs));
SendableHelper::operator=(std::move(rhs));
std::swap(m_channelIndex, rhs.m_channelIndex);

View File

@@ -17,6 +17,7 @@
#include "frc/SensorUtil.h"
#include "frc/WPIErrors.h"
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -40,7 +41,7 @@ DigitalInput::DigitalInput(int channel) {
}
HAL_Report(HALUsageReporting::kResourceType_DigitalInput, channel);
SetName("DigitalInput", channel);
SendableRegistry::GetInstance().AddLW(this, "DigitalInput", channel);
}
DigitalInput::~DigitalInput() {

View File

@@ -17,6 +17,7 @@
#include "frc/SensorUtil.h"
#include "frc/WPIErrors.h"
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -41,7 +42,7 @@ DigitalOutput::DigitalOutput(int channel) {
}
HAL_Report(HALUsageReporting::kResourceType_DigitalOutput, channel);
SetName("DigitalOutput", channel);
SendableRegistry::GetInstance().AddLW(this, "DigitalOutput", channel);
}
DigitalOutput::~DigitalOutput() {

View File

@@ -16,6 +16,7 @@
#include "frc/SensorUtil.h"
#include "frc/WPIErrors.h"
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -75,7 +76,9 @@ DoubleSolenoid::DoubleSolenoid(int moduleNumber, int forwardChannel,
m_moduleNumber);
HAL_Report(HALUsageReporting::kResourceType_Solenoid, m_reverseChannel,
m_moduleNumber);
SetName("DoubleSolenoid", m_moduleNumber, m_forwardChannel);
SendableRegistry::GetInstance().AddLW(this, "DoubleSolenoid", m_moduleNumber,
m_forwardChannel);
}
DoubleSolenoid::~DoubleSolenoid() {

View File

@@ -16,6 +16,7 @@
#include "frc/DigitalInput.h"
#include "frc/WPIErrors.h"
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -24,8 +25,9 @@ Encoder::Encoder(int aChannel, int bChannel, bool reverseDirection,
m_aSource = std::make_shared<DigitalInput>(aChannel);
m_bSource = std::make_shared<DigitalInput>(bChannel);
InitEncoder(reverseDirection, encodingType);
AddChild(m_aSource);
AddChild(m_bSource);
auto& registry = SendableRegistry::GetInstance();
registry.AddChild(this, m_aSource.get());
registry.AddChild(this, m_bSource.get());
}
Encoder::Encoder(DigitalSource* aSource, DigitalSource* bSource,
@@ -201,7 +203,7 @@ double Encoder::PIDGet() {
void Encoder::SetIndexSource(int channel, Encoder::IndexingType type) {
// Force digital input if just given an index
m_indexSource = std::make_shared<DigitalInput>(channel);
AddChild(m_indexSource);
SendableRegistry::GetInstance().AddChild(this, m_indexSource.get());
SetIndexSource(*m_indexSource.get(), type);
}
@@ -250,7 +252,8 @@ void Encoder::InitEncoder(bool reverseDirection, EncodingType encodingType) {
HAL_Report(HALUsageReporting::kResourceType_Encoder, GetFPGAIndex(),
encodingType);
SetName("Encoder", m_aSource->GetChannel());
SendableRegistry::GetInstance().AddLW(this, "Encoder",
m_aSource->GetChannel());
}
double Encoder::DecodingScaleFactor() const {

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2008-2019 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. */
@@ -8,6 +8,7 @@
#include "frc/GearTooth.h"
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -15,20 +16,22 @@ constexpr double GearTooth::kGearToothThreshold;
GearTooth::GearTooth(int channel, bool directionSensitive) : Counter(channel) {
EnableDirectionSensing(directionSensitive);
SetName("GearTooth", channel);
SendableRegistry::GetInstance().SetName(this, "GearTooth", channel);
}
GearTooth::GearTooth(DigitalSource* source, bool directionSensitive)
: Counter(source) {
EnableDirectionSensing(directionSensitive);
SetName("GearTooth", source->GetChannel());
SendableRegistry::GetInstance().SetName(this, "GearTooth",
source->GetChannel());
}
GearTooth::GearTooth(std::shared_ptr<DigitalSource> source,
bool directionSensitive)
: Counter(source) {
EnableDirectionSensing(directionSensitive);
SetName("GearTooth", source->GetChannel());
SendableRegistry::GetInstance().SetName(this, "GearTooth",
source->GetChannel());
}
void GearTooth::EnableDirectionSensing(bool directionSensitive) {

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2008-2019 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. */
@@ -9,6 +9,8 @@
#include <hal/HAL.h>
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
Jaguar::Jaguar(int channel) : PWMSpeedController(channel) {
@@ -26,5 +28,5 @@ Jaguar::Jaguar(int channel) : PWMSpeedController(channel) {
SetZeroLatch();
HAL_Report(HALUsageReporting::kResourceType_Jaguar, GetChannel());
SetName("Jaguar", GetChannel());
SendableRegistry::GetInstance().SetName(this, "Jaguar", GetChannel());
}

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2017-2019 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. */
@@ -10,13 +10,15 @@
#include <hal/HAL.h>
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
NidecBrushless::NidecBrushless(int pwmChannel, int dioChannel)
: m_dio(dioChannel), m_pwm(pwmChannel) {
AddChild(&m_dio);
AddChild(&m_pwm);
auto& registry = SendableRegistry::GetInstance();
registry.AddChild(this, &m_dio);
registry.AddChild(this, &m_pwm);
SetExpiration(0.0);
SetSafetyEnabled(false);
@@ -25,7 +27,7 @@ NidecBrushless::NidecBrushless(int pwmChannel, int dioChannel)
m_dio.EnablePWM(0.5);
HAL_Report(HALUsageReporting::kResourceType_NidecBrushless, pwmChannel);
SetName("Nidec Brushless", pwmChannel);
registry.AddLW(this, "Nidec Brushless", pwmChannel);
}
void NidecBrushless::Set(double speed) {

View File

@@ -14,6 +14,7 @@
#include "frc/PIDOutput.h"
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -27,8 +28,7 @@ PIDBase::PIDBase(double Kp, double Ki, double Kd, PIDSource& source,
: PIDBase(Kp, Ki, Kd, 0.0, source, output) {}
PIDBase::PIDBase(double Kp, double Ki, double Kd, double Kf, PIDSource& source,
PIDOutput& output)
: SendableBase(false) {
PIDOutput& output) {
m_P = Kp;
m_I = Ki;
m_D = Kd;
@@ -44,7 +44,7 @@ PIDBase::PIDBase(double Kp, double Ki, double Kd, double Kf, PIDSource& source,
static int instances = 0;
instances++;
HAL_Report(HALUsageReporting::kResourceType_PIDController, instances);
SetName("PIDController", instances);
SendableRegistry::GetInstance().Add(this, "PIDController", instances);
}
double PIDBase::Get() const {

View File

@@ -17,6 +17,7 @@
#include "frc/Utility.h"
#include "frc/WPIErrors.h"
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -46,7 +47,7 @@ PWM::PWM(int channel) {
wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
HAL_Report(HALUsageReporting::kResourceType_PWM, channel);
SetName("PWM", channel);
SendableRegistry::GetInstance().AddLW(this, "PWM", channel);
SetSafetyEnabled(false);
}

View File

@@ -9,6 +9,8 @@
#include <hal/HAL.h>
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
PWMSparkMax::PWMSparkMax(int channel) : PWMSpeedController(channel) {
@@ -26,5 +28,5 @@ PWMSparkMax::PWMSparkMax(int channel) : PWMSpeedController(channel) {
SetZeroLatch();
HAL_Report(HALUsageReporting::kResourceType_RevSparkMaxPWM, GetChannel());
SetName("PWMSparkMax", GetChannel());
SendableRegistry::GetInstance().SetName(this, "PWMSparkMax", GetChannel());
}

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2008-2019 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. */
@@ -9,6 +9,8 @@
#include <hal/HAL.h>
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
PWMTalonSRX::PWMTalonSRX(int channel) : PWMSpeedController(channel) {
@@ -30,5 +32,5 @@ PWMTalonSRX::PWMTalonSRX(int channel) : PWMSpeedController(channel) {
SetZeroLatch();
HAL_Report(HALUsageReporting::kResourceType_PWMTalonSRX, GetChannel());
SetName("PWMTalonSRX", GetChannel());
SendableRegistry::GetInstance().SetName(this, "PWMTalonSRX", GetChannel());
}

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2008-2019 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. */
@@ -9,6 +9,8 @@
#include <hal/HAL.h>
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
PWMVictorSPX::PWMVictorSPX(int channel) : PWMSpeedController(channel) {
@@ -30,5 +32,5 @@ PWMVictorSPX::PWMVictorSPX(int channel) : PWMSpeedController(channel) {
SetZeroLatch();
HAL_Report(HALUsageReporting::kResourceType_PWMVictorSPX, GetChannel());
SetName("PWMVictorSPX", GetChannel());
SendableRegistry::GetInstance().SetName(this, "PWMVictorSPX", GetChannel());
}

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2014-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2014-2019 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. */
@@ -16,6 +16,7 @@
#include "frc/SensorUtil.h"
#include "frc/WPIErrors.h"
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -34,7 +35,7 @@ PowerDistributionPanel::PowerDistributionPanel(int module) {
}
HAL_Report(HALUsageReporting::kResourceType_PDP, module);
SetName("PowerDistributionPanel", module);
SendableRegistry::GetInstance().AddLW(this, "PowerDistributionPanel", module);
}
double PowerDistributionPanel::GetVoltage() const {

View File

@@ -17,6 +17,7 @@
#include "frc/SensorUtil.h"
#include "frc/WPIErrors.h"
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -76,7 +77,7 @@ Relay::Relay(int channel, Relay::Direction direction)
}
}
SetName("Relay", m_channel);
SendableRegistry::GetInstance().AddLW(this, "Relay", m_channel);
}
Relay::~Relay() {

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2008-2019 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. */
@@ -9,6 +9,8 @@
#include <hal/HAL.h>
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
SD540::SD540(int channel) : PWMSpeedController(channel) {
@@ -31,5 +33,5 @@ SD540::SD540(int channel) : PWMSpeedController(channel) {
SetZeroLatch();
HAL_Report(HALUsageReporting::kResourceType_MindsensorsSD540, GetChannel());
SetName("SD540", GetChannel());
SendableRegistry::GetInstance().SetName(this, "SD540", GetChannel());
}

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2008-2019 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. */
@@ -10,6 +10,7 @@
#include <hal/HAL.h>
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -27,7 +28,7 @@ Servo::Servo(int channel) : PWM(channel) {
SetPeriodMultiplier(kPeriodMultiplier_4X);
HAL_Report(HALUsageReporting::kResourceType_Servo, channel);
SetName("Servo", channel);
SendableRegistry::GetInstance().SetName(this, "Servo", channel);
}
void Servo::Set(double value) { SetPosition(value); }

View File

@@ -16,6 +16,7 @@
#include "frc/SensorUtil.h"
#include "frc/WPIErrors.h"
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -47,7 +48,8 @@ Solenoid::Solenoid(int moduleNumber, int channel)
HAL_Report(HALUsageReporting::kResourceType_Solenoid, m_channel,
m_moduleNumber);
SetName("Solenoid", m_moduleNumber, m_channel);
SendableRegistry::GetInstance().AddLW(this, "Solenoid", m_moduleNumber,
m_channel);
}
Solenoid::~Solenoid() { HAL_FreeSolenoidPort(m_solenoidHandle); }

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2008-2019 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. */
@@ -9,6 +9,8 @@
#include <hal/HAL.h>
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
Spark::Spark(int channel) : PWMSpeedController(channel) {
@@ -31,5 +33,5 @@ Spark::Spark(int channel) : PWMSpeedController(channel) {
SetZeroLatch();
HAL_Report(HALUsageReporting::kResourceType_RevSPARK, GetChannel());
SetName("Spark", GetChannel());
SendableRegistry::GetInstance().SetName(this, "Spark", GetChannel());
}

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2008-2019 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. */
@@ -9,6 +9,8 @@
#include <hal/HAL.h>
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
Talon::Talon(int channel) : PWMSpeedController(channel) {
@@ -31,5 +33,5 @@ Talon::Talon(int channel) : PWMSpeedController(channel) {
SetZeroLatch();
HAL_Report(HALUsageReporting::kResourceType_Talon, GetChannel());
SetName("Talon", GetChannel());
SendableRegistry::GetInstance().SetName(this, "Talon", GetChannel());
}

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2008-2019 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. */
@@ -16,6 +16,7 @@
#include "frc/Utility.h"
#include "frc/WPIErrors.h"
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -31,8 +32,9 @@ Ultrasonic::Ultrasonic(int pingChannel, int echoChannel, DistanceUnit units)
m_counter(m_echoChannel) {
m_units = units;
Initialize();
AddChild(m_pingChannel);
AddChild(m_echoChannel);
auto& registry = SendableRegistry::GetInstance();
registry.AddChild(this, m_pingChannel.get());
registry.AddChild(this, m_echoChannel.get());
}
Ultrasonic::Ultrasonic(DigitalOutput* pingChannel, DigitalInput* echoChannel,
@@ -189,7 +191,8 @@ void Ultrasonic::Initialize() {
static int instances = 0;
instances++;
HAL_Report(HALUsageReporting::kResourceType_Ultrasonic, instances);
SetName("Ultrasonic", m_echoChannel->GetChannel());
SendableRegistry::GetInstance().AddLW(this, "Ultrasonic",
m_echoChannel->GetChannel());
}
void Ultrasonic::UltrasonicChecker() {

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2008-2019 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. */
@@ -9,6 +9,8 @@
#include <hal/HAL.h>
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
Victor::Victor(int channel) : PWMSpeedController(channel) {
@@ -32,5 +34,5 @@ Victor::Victor(int channel) : PWMSpeedController(channel) {
SetZeroLatch();
HAL_Report(HALUsageReporting::kResourceType_Victor, GetChannel());
SetName("Victor", GetChannel());
SendableRegistry::GetInstance().SetName(this, "Victor", GetChannel());
}

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2008-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2008-2019 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. */
@@ -9,6 +9,8 @@
#include <hal/HAL.h>
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
VictorSP::VictorSP(int channel) : PWMSpeedController(channel) {
@@ -31,5 +33,5 @@ VictorSP::VictorSP(int channel) : PWMSpeedController(channel) {
SetZeroLatch();
HAL_Report(HALUsageReporting::kResourceType_VictorSP, GetChannel());
SetName("VictorSP", GetChannel());
SendableRegistry::GetInstance().SetName(this, "VictorSP", GetChannel());
}

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2011-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2011-2019 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. */
@@ -16,6 +16,7 @@
#include "frc/commands/Scheduler.h"
#include "frc/livewindow/LiveWindow.h"
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -31,7 +32,7 @@ Command::Command(Subsystem& subsystem) : Command("", -1.0) {
Requires(&subsystem);
}
Command::Command(const wpi::Twine& name, double timeout) : SendableBase(false) {
Command::Command(const wpi::Twine& name, double timeout) {
// We use -1.0 to indicate no timeout.
if (timeout < 0.0 && timeout != -1.0)
wpi_setWPIErrorWithContext(ParameterOutOfRange, "timeout < 0.0");
@@ -41,9 +42,10 @@ Command::Command(const wpi::Twine& name, double timeout) : SendableBase(false) {
// If name contains an empty string
if (name.isTriviallyEmpty() ||
(name.isSingleStringRef() && name.getSingleStringRef().empty())) {
SetName("Command_" + wpi::Twine(typeid(*this).name()));
SendableRegistry::GetInstance().Add(
this, "Command_" + wpi::Twine(typeid(*this).name()));
} else {
SetName(name);
SendableRegistry::GetInstance().Add(this, name);
}
}
@@ -228,9 +230,27 @@ void Command::StartRunning() {
void Command::StartTiming() { m_startTime = Timer::GetFPGATimestamp(); }
std::string Command::GetName() const {
return SendableRegistry::GetInstance().GetName(this);
}
void Command::SetName(const wpi::Twine& name) {
SendableRegistry::GetInstance().SetName(this, name);
}
std::string Command::GetSubsystem() const {
return SendableRegistry::GetInstance().GetSubsystem(this);
}
void Command::SetSubsystem(const wpi::Twine& name) {
SendableRegistry::GetInstance().SetSubsystem(this, name);
}
void Command::InitSendable(SendableBuilder& builder) {
builder.SetSmartDashboardType("Command");
builder.AddStringProperty(".name", [=]() { return GetName(); }, nullptr);
builder.AddStringProperty(
".name", [=]() { return SendableRegistry::GetInstance().GetName(this); },
nullptr);
builder.AddBooleanProperty("running", [=]() { return IsRunning(); },
[=](bool value) {
if (value) {

View File

@@ -21,6 +21,7 @@
#include "frc/commands/Command.h"
#include "frc/commands/Subsystem.h"
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -182,8 +183,9 @@ void Scheduler::InitSendable(SendableBuilder& builder) {
if (m_impl->runningCommandsChanged) {
m_impl->commandsBuf.resize(0);
m_impl->idsBuf.resize(0);
auto& registry = SendableRegistry::GetInstance();
for (const auto& command : m_impl->commands) {
m_impl->commandsBuf.emplace_back(command->GetName());
m_impl->commandsBuf.emplace_back(registry.GetName(command));
m_impl->idsBuf.emplace_back(command->GetID());
}
nt::NetworkTableEntry(namesEntry).SetStringArray(m_impl->commandsBuf);
@@ -195,7 +197,7 @@ void Scheduler::InitSendable(SendableBuilder& builder) {
Scheduler::Scheduler() : m_impl(new Impl) {
HAL_Report(HALUsageReporting::kResourceType_Command,
HALUsageReporting::kCommand_Scheduler);
SetName("Scheduler");
SendableRegistry::GetInstance().AddLW(this, "Scheduler");
}
Scheduler::~Scheduler() {}

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2011-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2011-2019 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. */
@@ -12,11 +12,12 @@
#include "frc/commands/Scheduler.h"
#include "frc/livewindow/LiveWindow.h"
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
Subsystem::Subsystem(const wpi::Twine& name) {
SetName(name, name);
SendableRegistry::GetInstance().AddLW(this, name, name);
Scheduler::GetInstance()->RegisterSubsystem(this);
}
@@ -46,7 +47,7 @@ Command* Subsystem::GetDefaultCommand() {
wpi::StringRef Subsystem::GetDefaultCommandName() {
Command* defaultCommand = GetDefaultCommand();
if (defaultCommand) {
return defaultCommand->GetName();
return SendableRegistry::GetInstance().GetName(defaultCommand);
} else {
return wpi::StringRef();
}
@@ -62,7 +63,7 @@ Command* Subsystem::GetCurrentCommand() const { return m_currentCommand; }
wpi::StringRef Subsystem::GetCurrentCommandName() const {
Command* currentCommand = GetCurrentCommand();
if (currentCommand) {
return currentCommand->GetName();
return SendableRegistry::GetInstance().GetName(currentCommand);
} else {
return wpi::StringRef();
}
@@ -72,6 +73,22 @@ void Subsystem::Periodic() {}
void Subsystem::InitDefaultCommand() {}
std::string Subsystem::GetName() const {
return SendableRegistry::GetInstance().GetName(this);
}
void Subsystem::SetName(const wpi::Twine& name) {
SendableRegistry::GetInstance().SetName(this, name);
}
std::string Subsystem::GetSubsystem() const {
return SendableRegistry::GetInstance().GetSubsystem(this);
}
void Subsystem::SetSubsystem(const wpi::Twine& name) {
SendableRegistry::GetInstance().SetSubsystem(this, name);
}
void Subsystem::AddChild(const wpi::Twine& name,
std::shared_ptr<Sendable> child) {
AddChild(name, *child);
@@ -82,8 +99,9 @@ void Subsystem::AddChild(const wpi::Twine& name, Sendable* child) {
}
void Subsystem::AddChild(const wpi::Twine& name, Sendable& child) {
child.SetName(GetSubsystem(), name);
LiveWindow::GetInstance()->Add(&child);
auto& registry = SendableRegistry::GetInstance();
registry.AddLW(&child, registry.GetSubsystem(this), name);
registry.AddChild(this, &child);
}
void Subsystem::AddChild(std::shared_ptr<Sendable> child) { AddChild(*child); }
@@ -91,8 +109,10 @@ void Subsystem::AddChild(std::shared_ptr<Sendable> child) { AddChild(*child); }
void Subsystem::AddChild(Sendable* child) { AddChild(*child); }
void Subsystem::AddChild(Sendable& child) {
child.SetSubsystem(GetSubsystem());
LiveWindow::GetInstance()->Add(&child);
auto& registry = SendableRegistry::GetInstance();
registry.SetSubsystem(&child, registry.GetSubsystem(this));
registry.EnableLiveWindow(&child);
registry.AddChild(this, &child);
}
void Subsystem::ConfirmCommand() {

View File

@@ -13,16 +13,17 @@
#include <hal/HAL.h>
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc2;
PIDController::PIDController(double Kp, double Ki, double Kd,
units::second_t period)
: frc::SendableBase(false), m_Kp(Kp), m_Ki(Ki), m_Kd(Kd), m_period(period) {
: m_Kp(Kp), m_Ki(Ki), m_Kd(Kd), m_period(period) {
static int instances = 0;
instances++;
HAL_Report(HALUsageReporting::kResourceType_PIDController, instances);
SetName("PIDController", instances);
frc::SendableRegistry::GetInstance().Add(this, "PIDController", instances);
}
void PIDController::SetP(double Kp) { m_Kp = Kp; }

View File

@@ -14,17 +14,19 @@
#include "frc/SpeedController.h"
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
DifferentialDrive::DifferentialDrive(SpeedController& leftMotor,
SpeedController& rightMotor)
: m_leftMotor(leftMotor), m_rightMotor(rightMotor) {
AddChild(&m_leftMotor);
AddChild(&m_rightMotor);
auto& registry = SendableRegistry::GetInstance();
registry.AddChild(this, &m_leftMotor);
registry.AddChild(this, &m_rightMotor);
static int instances = 0;
++instances;
SetName("DifferentialDrive", instances);
registry.AddLW(this, "DifferentialDrive", instances);
}
void DifferentialDrive::ArcadeDrive(double xSpeed, double zRotation,

View File

@@ -15,6 +15,7 @@
#include "frc/SpeedController.h"
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -35,12 +36,13 @@ KilloughDrive::KilloughDrive(SpeedController& leftMotor,
std::sin(rightMotorAngle * (wpi::math::pi / 180.0))};
m_backVec = {std::cos(backMotorAngle * (wpi::math::pi / 180.0)),
std::sin(backMotorAngle * (wpi::math::pi / 180.0))};
AddChild(&m_leftMotor);
AddChild(&m_rightMotor);
AddChild(&m_backMotor);
auto& registry = SendableRegistry::GetInstance();
registry.AddChild(this, &m_leftMotor);
registry.AddChild(this, &m_rightMotor);
registry.AddChild(this, &m_backMotor);
static int instances = 0;
++instances;
SetName("KilloughDrive", instances);
registry.AddLW(this, "KilloughDrive", instances);
}
void KilloughDrive::DriveCartesian(double ySpeed, double xSpeed,

View File

@@ -16,6 +16,7 @@
#include "frc/SpeedController.h"
#include "frc/drive/Vector2d.h"
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -27,13 +28,14 @@ MecanumDrive::MecanumDrive(SpeedController& frontLeftMotor,
m_rearLeftMotor(rearLeftMotor),
m_frontRightMotor(frontRightMotor),
m_rearRightMotor(rearRightMotor) {
AddChild(&m_frontLeftMotor);
AddChild(&m_rearLeftMotor);
AddChild(&m_frontRightMotor);
AddChild(&m_rearRightMotor);
auto& registry = SendableRegistry::GetInstance();
registry.AddChild(this, &m_frontLeftMotor);
registry.AddChild(this, &m_rearLeftMotor);
registry.AddChild(this, &m_frontRightMotor);
registry.AddChild(this, &m_rearRightMotor);
static int instances = 0;
++instances;
SetName("MecanumDrive", instances);
registry.AddLW(this, "MecanumDrive", instances);
}
void MecanumDrive::DriveCartesian(double ySpeed, double xSpeed,

View File

@@ -24,6 +24,14 @@ using namespace frc2;
Command::~Command() { CommandScheduler::GetInstance().Cancel(this); }
Command::Command(const Command& rhs) : ErrorBase(rhs) {}
Command& Command::operator=(const Command& rhs) {
ErrorBase::operator=(rhs);
m_isGrouped = false;
return *this;
}
void Command::Initialize() {}
void Command::Execute() {}
void Command::End(bool interrupted) {}

View File

@@ -8,20 +8,14 @@
#include "frc2/command/CommandBase.h"
#include <frc/smartdashboard/SendableBuilder.h>
#include <frc/smartdashboard/SendableRegistry.h>
#include <frc2/command/CommandScheduler.h>
#include <frc2/command/SetUtilities.h>
using namespace frc2;
CommandBase::CommandBase() {
m_name = Command::GetName();
m_subsystem = "Unknown";
}
CommandBase::CommandBase(const CommandBase& other) : Sendable{}, Command{} {
m_name = other.m_name;
m_subsystem = other.m_subsystem;
m_requirements = other.m_requirements;
frc::SendableRegistry::GetInstance().AddLW(this, GetTypeName(*this));
}
void CommandBase::AddRequirements(
@@ -37,14 +31,20 @@ wpi::SmallSet<Subsystem*, 4> CommandBase::GetRequirements() const {
return m_requirements;
}
void CommandBase::SetName(const wpi::Twine& name) { m_name = name.str(); }
void CommandBase::SetName(const wpi::Twine& name) {
frc::SendableRegistry::GetInstance().SetName(this, name);
}
std::string CommandBase::GetName() const { return m_name; }
std::string CommandBase::GetName() const {
return frc::SendableRegistry::GetInstance().GetName(this);
}
std::string CommandBase::GetSubsystem() const { return m_subsystem; }
std::string CommandBase::GetSubsystem() const {
return frc::SendableRegistry::GetInstance().GetSubsystem(this);
}
void CommandBase::SetSubsystem(const wpi::Twine& subsystem) {
m_subsystem = subsystem.str();
frc::SendableRegistry::GetInstance().SetSubsystem(this, subsystem);
}
void CommandBase::InitSendable(frc::SendableBuilder& builder) {

View File

@@ -11,6 +11,7 @@
#include <frc/WPIErrors.h>
#include <frc/commands/Scheduler.h>
#include <frc/smartdashboard/SendableBuilder.h>
#include <frc/smartdashboard/SendableRegistry.h>
#include <frc2/command/CommandGroupBase.h>
#include <frc2/command/Subsystem.h>
@@ -22,7 +23,9 @@ static bool ContainsKey(const TMap& map, TKey keyToCheck) {
return map.find(keyToCheck) != map.end();
}
CommandScheduler::CommandScheduler() { SetName("Scheduler"); }
CommandScheduler::CommandScheduler() {
frc::SendableRegistry::GetInstance().AddLW(this, "Scheduler");
}
CommandScheduler& CommandScheduler::GetInstance() {
static CommandScheduler scheduler;

View File

@@ -7,15 +7,15 @@
#include "frc2/command/SubsystemBase.h"
#include <frc/livewindow/LiveWindow.h>
#include <frc/smartdashboard/SendableBuilder.h>
#include <frc/smartdashboard/SendableRegistry.h>
#include <frc2/command/Command.h>
#include <frc2/command/CommandScheduler.h>
using namespace frc2;
SubsystemBase::SubsystemBase() {
m_name = GetTypeName(*this);
frc::SendableRegistry::GetInstance().AddLW(this, GetTypeName(*this));
CommandScheduler::GetInstance().RegisterSubsystem({this});
}
@@ -43,15 +43,24 @@ void SubsystemBase::InitSendable(frc::SendableBuilder& builder) {
nullptr);
}
std::string SubsystemBase::GetName() const { return m_name; }
std::string SubsystemBase::GetName() const {
return frc::SendableRegistry::GetInstance().GetName(this);
}
void SubsystemBase::SetName(const wpi::Twine& name) { m_name = name.str(); }
void SubsystemBase::SetName(const wpi::Twine& name) {
frc::SendableRegistry::GetInstance().SetName(this, name);
}
std::string SubsystemBase::GetSubsystem() const { return GetName(); }
std::string SubsystemBase::GetSubsystem() const {
return frc::SendableRegistry::GetInstance().GetSubsystem(this);
}
void SubsystemBase::SetSubsystem(const wpi::Twine& name) { SetName(name); }
void SubsystemBase::SetSubsystem(const wpi::Twine& name) {
frc::SendableRegistry::GetInstance().SetSubsystem(this, name);
}
void SubsystemBase::AddChild(std::string name, frc::Sendable* child) {
child->SetName(name);
frc::LiveWindow::GetInstance()->Add(child);
auto& registry = frc::SendableRegistry::GetInstance();
registry.AddLW(child, GetSubsystem(), name);
registry.AddChild(this, child);
}

View File

@@ -11,7 +11,7 @@ using namespace frc2;
WaitCommand::WaitCommand(units::second_t duration) : m_duration{duration} {
auto durationStr = std::to_string(duration.to<double>());
SetName(wpi::Twine(m_name) + ": " + wpi::Twine(durationStr) + " seconds");
SetName(wpi::Twine(GetName()) + ": " + wpi::Twine(durationStr) + " seconds");
}
void WaitCommand::Initialize() {

View File

@@ -7,18 +7,14 @@
#include "frc/livewindow/LiveWindow.h"
#include <algorithm>
#include <networktables/NetworkTable.h>
#include <networktables/NetworkTableEntry.h>
#include <networktables/NetworkTableInstance.h>
#include <wpi/DenseMap.h>
#include <wpi/SmallString.h>
#include <wpi/mutex.h>
#include <wpi/raw_ostream.h>
#include "frc/commands/Scheduler.h"
#include "frc/smartdashboard/SendableBuilderImpl.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -28,8 +24,6 @@ struct LiveWindow::Impl {
Impl();
struct Component {
std::shared_ptr<Sendable> sendable;
Sendable* parent = nullptr;
SendableBuilderImpl builder;
bool firstTime = true;
bool telemetryEnabled = true;
@@ -37,7 +31,8 @@ struct LiveWindow::Impl {
wpi::mutex mutex;
wpi::DenseMap<void*, Component> components;
SendableRegistry& registry;
int dataHandle;
std::shared_ptr<nt::NetworkTable> liveWindowTable;
std::shared_ptr<nt::NetworkTable> statusTable;
@@ -46,64 +41,57 @@ struct LiveWindow::Impl {
bool startLiveWindow = false;
bool liveWindowEnabled = false;
bool telemetryEnabled = true;
std::shared_ptr<Component> GetOrAdd(Sendable* sendable);
};
LiveWindow::Impl::Impl()
: liveWindowTable(
: registry(SendableRegistry::GetInstance()),
dataHandle(registry.GetDataHandle()),
liveWindowTable(
nt::NetworkTableInstance::GetDefault().GetTable("LiveWindow")) {
statusTable = liveWindowTable->GetSubTable(".status");
enabledEntry = statusTable->GetEntry("LW Enabled");
}
std::shared_ptr<LiveWindow::Impl::Component> LiveWindow::Impl::GetOrAdd(
Sendable* sendable) {
auto data = std::static_pointer_cast<Component>(
registry.GetData(sendable, dataHandle));
if (!data) {
data = std::make_shared<Component>();
registry.SetData(sendable, dataHandle, data);
}
return data;
}
LiveWindow* LiveWindow::GetInstance() {
static LiveWindow instance;
return &instance;
}
void LiveWindow::Add(std::shared_ptr<Sendable> sendable) {
std::scoped_lock lock(m_impl->mutex);
auto& comp = m_impl->components[sendable.get()];
comp.sendable = sendable;
}
void LiveWindow::Add(Sendable* sendable) {
Add(std::shared_ptr<Sendable>(sendable, NullDeleter<Sendable>()));
}
void LiveWindow::AddChild(Sendable* parent, std::shared_ptr<Sendable> child) {
AddChild(parent, child.get());
}
void LiveWindow::AddChild(Sendable* parent, void* child) {
std::scoped_lock lock(m_impl->mutex);
auto& comp = m_impl->components[child];
comp.parent = parent;
comp.telemetryEnabled = false;
}
bool LiveWindow::Remove(Sendable* sendable) {
std::scoped_lock lock(m_impl->mutex);
return m_impl->components.erase(sendable);
}
void LiveWindow::EnableTelemetry(Sendable* sendable) {
std::scoped_lock lock(m_impl->mutex);
// Re-enable global setting in case DisableAllTelemetry() was called.
m_impl->telemetryEnabled = true;
auto i = m_impl->components.find(sendable);
if (i != m_impl->components.end()) i->getSecond().telemetryEnabled = true;
m_impl->GetOrAdd(sendable)->telemetryEnabled = true;
}
void LiveWindow::DisableTelemetry(Sendable* sendable) {
std::scoped_lock lock(m_impl->mutex);
auto i = m_impl->components.find(sendable);
if (i != m_impl->components.end()) i->getSecond().telemetryEnabled = false;
m_impl->GetOrAdd(sendable)->telemetryEnabled = false;
}
void LiveWindow::DisableAllTelemetry() {
std::scoped_lock lock(m_impl->mutex);
m_impl->telemetryEnabled = false;
for (auto& i : m_impl->components) i.getSecond().telemetryEnabled = false;
m_impl->registry.ForeachLiveWindow(
m_impl->dataHandle, [&](Sendable*, wpi::StringRef, wpi::StringRef,
Sendable*, std::shared_ptr<void>& data) {
if (!data) data = std::make_shared<Impl::Component>();
std::static_pointer_cast<Impl::Component>(data)->telemetryEnabled =
false;
});
}
bool LiveWindow::IsEnabled() const {
@@ -123,9 +111,13 @@ void LiveWindow::SetEnabled(bool enabled) {
scheduler->SetEnabled(false);
scheduler->RemoveAll();
} else {
for (auto& i : m_impl->components) {
i.getSecond().builder.StopLiveWindowMode();
}
m_impl->registry.ForeachLiveWindow(
m_impl->dataHandle, [&](Sendable*, wpi::StringRef, wpi::StringRef,
Sendable*, std::shared_ptr<void>& data) {
if (data)
std::static_pointer_cast<Impl::Component>(data)
->builder.StopLiveWindowMode();
});
scheduler->SetEnabled(true);
}
m_impl->enabledEntry.SetBoolean(enabled);
@@ -140,37 +132,42 @@ void LiveWindow::UpdateValuesUnsafe() {
// Only do this if either LiveWindow mode or telemetry is enabled.
if (!m_impl->liveWindowEnabled && !m_impl->telemetryEnabled) return;
for (auto& i : m_impl->components) {
auto& comp = i.getSecond();
if (comp.sendable && !comp.parent &&
(m_impl->liveWindowEnabled || comp.telemetryEnabled)) {
if (comp.firstTime) {
// By holding off creating the NetworkTable entries, it allows the
// components to be redefined. This allows default sensor and actuator
// values to be created that are replaced with the custom names from
// users calling setName.
auto name = comp.sendable->GetName();
if (name.empty()) continue;
auto subsystem = comp.sendable->GetSubsystem();
auto ssTable = m_impl->liveWindowTable->GetSubTable(subsystem);
std::shared_ptr<NetworkTable> table;
// Treat name==subsystem as top level of subsystem
if (name == subsystem)
table = ssTable;
else
table = ssTable->GetSubTable(name);
table->GetEntry(".name").SetString(name);
comp.builder.SetTable(table);
comp.sendable->InitSendable(comp.builder);
ssTable->GetEntry(".type").SetString("LW Subsystem");
m_impl->registry.ForeachLiveWindow(
m_impl->dataHandle,
[&](Sendable* sendable, wpi::StringRef name, wpi::StringRef subsystem,
Sendable* parent, std::shared_ptr<void>& data) {
if (!sendable || parent) return;
comp.firstTime = false;
}
if (!data) data = std::make_shared<Impl::Component>();
if (m_impl->startLiveWindow) comp.builder.StartLiveWindowMode();
comp.builder.UpdateTable();
}
}
auto& comp = *std::static_pointer_cast<Impl::Component>(data);
if (!m_impl->liveWindowEnabled && !comp.telemetryEnabled) return;
if (comp.firstTime) {
// By holding off creating the NetworkTable entries, it allows the
// components to be redefined. This allows default sensor and actuator
// values to be created that are replaced with the custom names from
// users calling setName.
if (name.empty()) return;
auto ssTable = m_impl->liveWindowTable->GetSubTable(subsystem);
std::shared_ptr<NetworkTable> table;
// Treat name==subsystem as top level of subsystem
if (name == subsystem)
table = ssTable;
else
table = ssTable->GetSubTable(name);
table->GetEntry(".name").SetString(name);
comp.builder.SetTable(table);
sendable->InitSendable(comp.builder);
ssTable->GetEntry(".type").SetString("LW Subsystem");
comp.firstTime = false;
}
if (m_impl->startLiveWindow) comp.builder.StartLiveWindowMode();
comp.builder.UpdateTable();
});
m_impl->startLiveWindow = false;
}

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2018-2019 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. */
@@ -11,6 +11,7 @@
#include <wpi/DenseMap.h>
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -33,10 +34,10 @@ SendableCameraWrapper& SendableCameraWrapper::Wrap(CS_Source source) {
SendableCameraWrapper::SendableCameraWrapper(CS_Source source,
const private_init&)
: SendableBase(false), m_uri(kProtocol) {
: m_uri(kProtocol) {
CS_Status status = 0;
auto name = cs::GetSourceName(source, &status);
SetName(name);
SendableRegistry::GetInstance().Add(this, name);
m_uri += name;
}

View File

@@ -15,6 +15,7 @@
#include "frc/shuffleboard/ShuffleboardComponent.h"
#include "frc/shuffleboard/ShuffleboardLayout.h"
#include "frc/shuffleboard/SimpleWidget.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -80,10 +81,11 @@ ComplexWidget& ShuffleboardContainer::Add(const wpi::Twine& title,
}
ComplexWidget& ShuffleboardContainer::Add(Sendable& sendable) {
if (sendable.GetName().empty()) {
auto name = SendableRegistry::GetInstance().GetName(&sendable);
if (name.empty()) {
wpi::outs() << "Sendable must have a name\n";
}
return Add(sendable.GetName(), sendable);
return Add(name, sendable);
}
ComplexWidget& ShuffleboardContainer::Add(const cs::VideoSource& video) {

View File

@@ -7,62 +7,13 @@
#include "frc/smartdashboard/SendableBase.h"
#include <utility>
#include "frc/livewindow/LiveWindow.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
SendableBase::SendableBase(bool addLiveWindow) {
if (addLiveWindow) LiveWindow::GetInstance()->Add(this);
}
SendableBase::SendableBase(SendableBase&& other)
: m_name(std::move(other.m_name)),
m_subsystem(std::move(other.m_subsystem)) {
auto&& lw = LiveWindow::GetInstance();
if (lw->Remove(&other)) {
lw->Add(this);
}
}
SendableBase& SendableBase::operator=(SendableBase&& other) {
m_name = std::move(other.m_name);
m_subsystem = std::move(other.m_subsystem);
auto&& lw = LiveWindow::GetInstance();
if (lw->Remove(&other)) {
lw->Add(this);
}
return *this;
}
SendableBase::~SendableBase() { LiveWindow::GetInstance()->Remove(this); }
std::string SendableBase::GetName() const { return m_name; }
void SendableBase::SetName(const wpi::Twine& name) { m_name = name.str(); }
std::string SendableBase::GetSubsystem() const { return m_subsystem; }
void SendableBase::SetSubsystem(const wpi::Twine& subsystem) {
m_subsystem = subsystem.str();
}
void SendableBase::AddChild(std::shared_ptr<Sendable> child) {
LiveWindow::GetInstance()->AddChild(this, child);
}
void SendableBase::AddChild(void* child) {
LiveWindow::GetInstance()->AddChild(this, child);
}
void SendableBase::SetName(const wpi::Twine& moduleType, int channel) {
SetName(moduleType + wpi::Twine('[') + wpi::Twine(channel) + wpi::Twine(']'));
}
void SendableBase::SetName(const wpi::Twine& moduleType, int moduleNumber,
int channel) {
SetName(moduleType + wpi::Twine('[') + wpi::Twine(moduleNumber) +
wpi::Twine(',') + wpi::Twine(channel) + wpi::Twine(']'));
if (addLiveWindow)
SendableRegistry::GetInstance().AddLW(this, "");
else
SendableRegistry::GetInstance().Add(this, "");
}

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved. */
/* Copyright (c) 2017-2019 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. */
@@ -7,9 +7,12 @@
#include "frc/smartdashboard/SendableChooserBase.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
std::atomic_int SendableChooserBase::s_instances{0};
SendableChooserBase::SendableChooserBase()
: SendableBase(false), m_instance{s_instances++} {}
SendableChooserBase::SendableChooserBase() : m_instance{s_instances++} {
SendableRegistry::GetInstance().Add(this, "SendableChooser", m_instance);
}

View File

@@ -0,0 +1,263 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2019 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 "frc/smartdashboard/SendableRegistry.h"
#include <wpi/DenseMap.h>
#include <wpi/SmallVector.h>
#include <wpi/mutex.h>
using namespace frc;
struct SendableRegistry::Impl {
struct Component {
Sendable* sendable = nullptr;
std::string name;
std::string subsystem = "Ungrouped";
Sendable* parent = nullptr;
bool liveWindow = false;
wpi::SmallVector<std::shared_ptr<void>, 2> data;
void SetName(const wpi::Twine& moduleType, int channel) {
name =
(moduleType + wpi::Twine('[') + wpi::Twine(channel) + wpi::Twine(']'))
.str();
}
void SetName(const wpi::Twine& moduleType, int moduleNumber, int channel) {
name = (moduleType + wpi::Twine('[') + wpi::Twine(moduleNumber) +
wpi::Twine(',') + wpi::Twine(channel) + wpi::Twine(']'))
.str();
}
};
wpi::mutex mutex;
wpi::DenseMap<void*, Component> components;
int nextDataHandle = 0;
Component& GetOrAdd(Sendable* sendable);
};
SendableRegistry::Impl::Component& SendableRegistry::Impl::GetOrAdd(
Sendable* sendable) {
auto& comp = components[sendable];
comp.sendable = sendable;
return comp;
}
SendableRegistry& SendableRegistry::GetInstance() {
static SendableRegistry instance;
return instance;
}
void SendableRegistry::Add(Sendable* sendable, const wpi::Twine& name) {
std::scoped_lock lock(m_impl->mutex);
auto& comp = m_impl->GetOrAdd(sendable);
comp.name = name.str();
}
void SendableRegistry::Add(Sendable* sendable, const wpi::Twine& moduleType,
int channel) {
std::scoped_lock lock(m_impl->mutex);
auto& comp = m_impl->GetOrAdd(sendable);
comp.SetName(moduleType, channel);
}
void SendableRegistry::Add(Sendable* sendable, const wpi::Twine& moduleType,
int moduleNumber, int channel) {
std::scoped_lock lock(m_impl->mutex);
auto& comp = m_impl->GetOrAdd(sendable);
comp.SetName(moduleType, moduleNumber, channel);
}
void SendableRegistry::Add(Sendable* sendable, const wpi::Twine& subsystem,
const wpi::Twine& name) {
std::scoped_lock lock(m_impl->mutex);
auto& comp = m_impl->GetOrAdd(sendable);
comp.name = name.str();
comp.subsystem = subsystem.str();
}
void SendableRegistry::AddLW(Sendable* sendable, const wpi::Twine& name) {
std::scoped_lock lock(m_impl->mutex);
auto& comp = m_impl->GetOrAdd(sendable);
comp.liveWindow = true;
comp.name = name.str();
}
void SendableRegistry::AddLW(Sendable* sendable, const wpi::Twine& moduleType,
int channel) {
std::scoped_lock lock(m_impl->mutex);
auto& comp = m_impl->GetOrAdd(sendable);
comp.liveWindow = true;
comp.SetName(moduleType, channel);
}
void SendableRegistry::AddLW(Sendable* sendable, const wpi::Twine& moduleType,
int moduleNumber, int channel) {
std::scoped_lock lock(m_impl->mutex);
auto& comp = m_impl->GetOrAdd(sendable);
comp.liveWindow = true;
comp.SetName(moduleType, moduleNumber, channel);
}
void SendableRegistry::AddLW(Sendable* sendable, const wpi::Twine& subsystem,
const wpi::Twine& name) {
std::scoped_lock lock(m_impl->mutex);
auto& comp = m_impl->GetOrAdd(sendable);
comp.liveWindow = true;
comp.name = name.str();
comp.subsystem = subsystem.str();
}
void SendableRegistry::AddChild(Sendable* parent, void* child) {
std::scoped_lock lock(m_impl->mutex);
auto& comp = m_impl->components[child];
comp.parent = parent;
}
bool SendableRegistry::Remove(Sendable* sendable) {
std::scoped_lock lock(m_impl->mutex);
return m_impl->components.erase(sendable);
}
void SendableRegistry::Move(Sendable* to, Sendable* from) {
std::scoped_lock lock(m_impl->mutex);
auto it = m_impl->components.find(from);
if (it == m_impl->components.end()) return;
Impl::Component old = std::move(it->getSecond());
m_impl->components.erase(it);
m_impl->components[to] = std::move(old);
m_impl->components[to].sendable = to;
}
bool SendableRegistry::Contains(const Sendable* sendable) const {
std::scoped_lock lock(m_impl->mutex);
return m_impl->components.count(sendable) != 0;
}
std::string SendableRegistry::GetName(const Sendable* sendable) const {
std::scoped_lock lock(m_impl->mutex);
auto it = m_impl->components.find(sendable);
if (it == m_impl->components.end()) return std::string{};
return it->getSecond().name;
}
void SendableRegistry::SetName(Sendable* sendable, const wpi::Twine& name) {
std::scoped_lock lock(m_impl->mutex);
auto it = m_impl->components.find(sendable);
if (it == m_impl->components.end()) return;
it->getSecond().name = name.str();
}
void SendableRegistry::SetName(Sendable* sendable, const wpi::Twine& moduleType,
int channel) {
std::scoped_lock lock(m_impl->mutex);
auto it = m_impl->components.find(sendable);
if (it == m_impl->components.end()) return;
it->getSecond().SetName(moduleType, channel);
}
void SendableRegistry::SetName(Sendable* sendable, const wpi::Twine& moduleType,
int moduleNumber, int channel) {
std::scoped_lock lock(m_impl->mutex);
auto it = m_impl->components.find(sendable);
if (it == m_impl->components.end()) return;
it->getSecond().SetName(moduleType, moduleNumber, channel);
}
void SendableRegistry::SetName(Sendable* sendable, const wpi::Twine& subsystem,
const wpi::Twine& name) {
std::scoped_lock lock(m_impl->mutex);
auto it = m_impl->components.find(sendable);
if (it == m_impl->components.end()) return;
it->getSecond().name = name.str();
it->getSecond().subsystem = subsystem.str();
}
std::string SendableRegistry::GetSubsystem(const Sendable* sendable) const {
std::scoped_lock lock(m_impl->mutex);
auto it = m_impl->components.find(sendable);
if (it == m_impl->components.end()) return std::string{};
return it->getSecond().subsystem;
}
void SendableRegistry::SetSubsystem(Sendable* sendable,
const wpi::Twine& subsystem) {
std::scoped_lock lock(m_impl->mutex);
auto it = m_impl->components.find(sendable);
if (it == m_impl->components.end()) return;
it->getSecond().subsystem = subsystem.str();
}
int SendableRegistry::GetDataHandle() {
std::scoped_lock lock(m_impl->mutex);
return m_impl->nextDataHandle++;
}
std::shared_ptr<void> SendableRegistry::SetData(Sendable* sendable, int handle,
std::shared_ptr<void> data) {
assert(handle >= 0);
std::scoped_lock lock(m_impl->mutex);
auto it = m_impl->components.find(sendable);
if (it == m_impl->components.end()) return nullptr;
auto& comp = it->getSecond();
std::shared_ptr<void> rv;
if (static_cast<size_t>(handle) < comp.data.size())
rv = std::move(comp.data[handle]);
else
comp.data.resize(handle + 1);
comp.data[handle] = std::move(data);
return rv;
}
std::shared_ptr<void> SendableRegistry::GetData(Sendable* sendable,
int handle) {
assert(handle >= 0);
std::scoped_lock lock(m_impl->mutex);
auto it = m_impl->components.find(sendable);
if (it == m_impl->components.end()) return nullptr;
auto& comp = it->getSecond();
if (static_cast<size_t>(handle) >= comp.data.size()) return nullptr;
return comp.data[handle];
}
void SendableRegistry::EnableLiveWindow(Sendable* sendable) {
std::scoped_lock lock(m_impl->mutex);
auto it = m_impl->components.find(sendable);
if (it == m_impl->components.end()) return;
it->getSecond().liveWindow = true;
}
void SendableRegistry::DisableLiveWindow(Sendable* sendable) {
std::scoped_lock lock(m_impl->mutex);
auto it = m_impl->components.find(sendable);
if (it == m_impl->components.end()) return;
it->getSecond().liveWindow = false;
}
void SendableRegistry::ForeachLiveWindow(
int dataHandle,
wpi::function_ref<void(Sendable* sendable, wpi::StringRef name,
wpi::StringRef subsystem, Sendable* parent,
std::shared_ptr<void>& data)>
callback) const {
assert(dataHandle >= 0);
std::scoped_lock lock(m_impl->mutex);
for (auto& i : m_impl->components) {
auto& comp = i.getSecond();
if (comp.sendable && comp.liveWindow) {
if (static_cast<size_t>(dataHandle) >= comp.data.size())
comp.data.resize(dataHandle + 1);
callback(comp.sendable, comp.name, comp.subsystem, comp.parent,
comp.data[dataHandle]);
}
}
}
SendableRegistry::SendableRegistry() : m_impl(new Impl) {}

View File

@@ -14,8 +14,8 @@
#include <wpi/mutex.h>
#include "frc/WPIErrors.h"
#include "frc/smartdashboard/Sendable.h"
#include "frc/smartdashboard/SendableBuilderImpl.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
@@ -115,7 +115,8 @@ void SmartDashboard::PutData(Sendable* value) {
wpi_setGlobalWPIErrorWithContext(NullParameter, "value");
return;
}
PutData(value->GetName(), value);
auto name = SendableRegistry::GetInstance().GetName(value);
if (!name.empty()) PutData(name, value);
}
Sendable* SmartDashboard::GetData(wpi::StringRef key) {