wpilibc: Remove direct CameraServer dependency (#1989)

This makes linking easier, particularly for third party vendors and other language wrappers.
This commit is contained in:
Thad House
2019-10-27 08:37:30 -07:00
committed by Peter Johnson
parent 8e333c0aad
commit 936627bd94
10 changed files with 142 additions and 62 deletions

View File

@@ -1,126 +0,0 @@
/*----------------------------------------------------------------------------*/
/* 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. */
/*----------------------------------------------------------------------------*/
#include "frc/RobotBase.h"
#include <cstdio>
#include <cameraserver/CameraServerShared.h>
#include <cscore.h>
#include <hal/HAL.h>
#include <networktables/NetworkTableInstance.h>
#include "WPILibVersion.h"
#include "frc/DriverStation.h"
#include "frc/RobotState.h"
#include "frc/Utility.h"
#include "frc/WPIErrors.h"
#include "frc/livewindow/LiveWindow.h"
#include "frc/smartdashboard/SmartDashboard.h"
using namespace frc;
int frc::RunHALInitialization() {
if (!HAL_Initialize(500, 0)) {
wpi::errs() << "FATAL ERROR: HAL could not be initialized\n";
return -1;
}
HAL_Report(HALUsageReporting::kResourceType_Language,
HALUsageReporting::kLanguage_CPlusPlus);
wpi::outs() << "\n********** Robot program starting **********\n";
return 0;
}
std::thread::id RobotBase::m_threadId;
namespace {
class WPILibCameraServerShared : public frc::CameraServerShared {
public:
void ReportUsbCamera(int id) override {
HAL_Report(HALUsageReporting::kResourceType_UsbCamera, id);
}
void ReportAxisCamera(int id) override {
HAL_Report(HALUsageReporting::kResourceType_AxisCamera, id);
}
void ReportVideoServer(int id) override {
HAL_Report(HALUsageReporting::kResourceType_PCVideoServer, id);
}
void SetCameraServerError(const wpi::Twine& error) override {
wpi_setGlobalWPIErrorWithContext(CameraServerError, error);
}
void SetVisionRunnerError(const wpi::Twine& error) override {
wpi_setGlobalErrorWithContext(-1, error);
}
void ReportDriverStationError(const wpi::Twine& error) override {
DriverStation::ReportError(error);
}
std::pair<std::thread::id, bool> GetRobotMainThreadId() const override {
return std::make_pair(RobotBase::GetThreadId(), true);
}
};
} // namespace
static void SetupCameraServerShared() {
SetCameraServerShared(std::make_unique<WPILibCameraServerShared>());
}
bool RobotBase::IsEnabled() const { return m_ds.IsEnabled(); }
bool RobotBase::IsDisabled() const { return m_ds.IsDisabled(); }
bool RobotBase::IsAutonomous() const { return m_ds.IsAutonomous(); }
bool RobotBase::IsOperatorControl() const { return m_ds.IsOperatorControl(); }
bool RobotBase::IsTest() const { return m_ds.IsTest(); }
bool RobotBase::IsNewDataAvailable() const { return m_ds.IsNewControlData(); }
std::thread::id RobotBase::GetThreadId() { return m_threadId; }
RobotBase::RobotBase() : m_ds(DriverStation::GetInstance()) {
if (!HAL_Initialize(500, 0)) {
wpi::errs() << "FATAL ERROR: HAL could not be initialized\n";
wpi::errs().flush();
std::terminate();
}
m_threadId = std::this_thread::get_id();
SetupCameraServerShared();
auto inst = nt::NetworkTableInstance::GetDefault();
inst.SetNetworkIdentity("Robot");
inst.StartServer("/home/lvuser/networktables.ini");
SmartDashboard::init();
if (IsReal()) {
std::FILE* file = nullptr;
file = std::fopen("/tmp/frc_versions/FRC_Lib_Version.ini", "w");
if (file != nullptr) {
std::fputs("C++ ", file);
std::fputs(GetWPILibVersion(), file);
std::fclose(file);
}
}
// First and one-time initialization
inst.GetTable("LiveWindow")
->GetSubTable(".status")
->GetEntry("LW Enabled")
.SetBoolean(false);
LiveWindow::GetInstance()->SetEnabled(false);
}
RobotBase::RobotBase(RobotBase&&) noexcept
: m_ds(DriverStation::GetInstance()) {}
RobotBase::~RobotBase() { cs::Shutdown(); }
RobotBase& RobotBase::operator=(RobotBase&&) noexcept { return *this; }

View File

@@ -1,5 +1,5 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) 2018-2019 FIRST. All Rights Reserved. */
/* 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. */
@@ -7,41 +7,30 @@
#include "frc/shuffleboard/SendableCameraWrapper.h"
#include <cscore_oo.h>
#include <functional>
#include <memory>
#include <string>
#include <wpi/DenseMap.h>
#include "frc/smartdashboard/SendableBuilder.h"
#include "frc/smartdashboard/SendableRegistry.h"
using namespace frc;
namespace {
constexpr const char* kProtocol = "camera_server://";
wpi::DenseMap<int, std::unique_ptr<SendableCameraWrapper>> wrappers;
} // namespace
SendableCameraWrapper& SendableCameraWrapper::Wrap(
const cs::VideoSource& source) {
return Wrap(source.GetHandle());
namespace frc {
namespace detail {
std::shared_ptr<SendableCameraWrapper>& GetSendableCameraWrapper(
CS_Source source) {
static wpi::DenseMap<int, std::shared_ptr<SendableCameraWrapper>> wrappers;
return wrappers[static_cast<int>(source)];
}
SendableCameraWrapper& SendableCameraWrapper::Wrap(CS_Source source) {
auto& wrapper = wrappers[static_cast<int>(source)];
if (!wrapper)
wrapper = std::make_unique<SendableCameraWrapper>(source, private_init{});
return *wrapper;
}
SendableCameraWrapper::SendableCameraWrapper(CS_Source source,
const private_init&)
: m_uri(kProtocol) {
CS_Status status = 0;
auto name = cs::GetSourceName(source, &status);
SendableRegistry::GetInstance().Add(this, name);
m_uri += name;
void AddToSendableRegistry(frc::Sendable* sendable, std::string name) {
SendableRegistry::GetInstance().Add(sendable, name);
}
} // namespace detail
void SendableCameraWrapper::InitSendable(SendableBuilder& builder) {
builder.AddStringProperty(".ShuffleboardURI", [this] { return m_uri; },
nullptr);
}
} // namespace frc

View File

@@ -11,7 +11,6 @@
#include <wpi/raw_ostream.h>
#include "frc/shuffleboard/ComplexWidget.h"
#include "frc/shuffleboard/SendableCameraWrapper.h"
#include "frc/shuffleboard/ShuffleboardComponent.h"
#include "frc/shuffleboard/ShuffleboardLayout.h"
#include "frc/shuffleboard/SimpleWidget.h"
@@ -75,11 +74,6 @@ ComplexWidget& ShuffleboardContainer::Add(const wpi::Twine& title,
return *ptr;
}
ComplexWidget& ShuffleboardContainer::Add(const wpi::Twine& title,
const cs::VideoSource& video) {
return Add(title, SendableCameraWrapper::Wrap(video));
}
ComplexWidget& ShuffleboardContainer::Add(Sendable& sendable) {
auto name = SendableRegistry::GetInstance().GetName(&sendable);
if (name.empty()) {
@@ -88,10 +82,6 @@ ComplexWidget& ShuffleboardContainer::Add(Sendable& sendable) {
return Add(name, sendable);
}
ComplexWidget& ShuffleboardContainer::Add(const cs::VideoSource& video) {
return Add(SendableCameraWrapper::Wrap(video));
}
SimpleWidget& ShuffleboardContainer::Add(
const wpi::Twine& title, std::shared_ptr<nt::Value> defaultValue) {
CheckTitle(title);