[glass, wpilib] Rewrite Mechanism2d (#3281)

Substantially improves Mechanism2d by moving it to NetworkTables and adding
a robot API to create the mechanism elements, instead of requiring a JSON file.

Co-authored-by: Peter Johnson <johnson.peter@gmail.com>
This commit is contained in:
Starlight220
2021-04-30 23:43:59 +03:00
committed by GitHub
parent ee0eed143a
commit ff52f207cc
28 changed files with 1780 additions and 479 deletions

View File

@@ -0,0 +1,55 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "frc/smartdashboard/Mechanism2d.h"
#include <cstdio>
#include "frc/smartdashboard/SendableBuilder.h"
using namespace frc;
static constexpr char kBackgroundColor[] = "backgroundColor";
static constexpr char kDims[] = "dims";
Mechanism2d::Mechanism2d(double width, double height,
const Color8Bit& backgroundColor)
: m_width{width}, m_height{height} {
SetBackgroundColor(backgroundColor);
}
MechanismRoot2d* Mechanism2d::GetRoot(wpi::StringRef name, double x, double y) {
auto& obj = m_roots[name];
if (obj) {
return obj.get();
}
obj = std::make_unique<MechanismRoot2d>(name, x, y,
MechanismRoot2d::private_init{});
if (m_table) {
obj->Update(m_table->GetSubTable(name));
}
return obj.get();
}
void Mechanism2d::SetBackgroundColor(const Color8Bit& color) {
std::snprintf(m_color, sizeof(m_color), "#%02X%02X%02X", color.red,
color.green, color.blue);
if (m_table) {
m_table->GetEntry(kBackgroundColor).SetString(m_color);
}
}
void Mechanism2d::InitSendable(SendableBuilder& builder) {
builder.SetSmartDashboardType("Mechanism2d");
m_table = builder.GetTable();
m_table->GetEntry(kDims).SetDoubleArray({m_width, m_height});
m_table->GetEntry(kBackgroundColor).SetString(m_color);
std::scoped_lock lock(m_mutex);
for (const auto& entry : m_roots) {
const auto& root = entry.getValue().get();
root->Update(m_table->GetSubTable(entry.getKey()));
}
}