2016-01-02 03:02:34 -08:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2019-06-14 18:52:10 -07:00
|
|
|
/* Copyright (c) 2012-2019 FIRST. All Rights Reserved. */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* 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. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/livewindow/LiveWindow.h"
|
2016-09-25 16:50:13 -07:00
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
#include <algorithm>
|
2016-09-25 16:50:13 -07:00
|
|
|
|
2017-12-07 23:34:29 -08:00
|
|
|
#include <networktables/NetworkTable.h>
|
|
|
|
|
#include <networktables/NetworkTableEntry.h>
|
|
|
|
|
#include <networktables/NetworkTableInstance.h>
|
2018-04-29 23:33:19 -07:00
|
|
|
#include <wpi/DenseMap.h>
|
|
|
|
|
#include <wpi/SmallString.h>
|
|
|
|
|
#include <wpi/mutex.h>
|
|
|
|
|
#include <wpi/raw_ostream.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/commands/Scheduler.h"
|
|
|
|
|
#include "frc/smartdashboard/SendableBuilderImpl.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
using namespace frc;
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
using wpi::Twine;
|
2017-12-04 23:28:33 -08:00
|
|
|
|
|
|
|
|
struct LiveWindow::Impl {
|
|
|
|
|
Impl();
|
|
|
|
|
|
|
|
|
|
struct Component {
|
|
|
|
|
std::shared_ptr<Sendable> sendable;
|
|
|
|
|
Sendable* parent = nullptr;
|
|
|
|
|
SendableBuilderImpl builder;
|
|
|
|
|
bool firstTime = true;
|
|
|
|
|
bool telemetryEnabled = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
wpi::mutex mutex;
|
|
|
|
|
|
2018-04-29 23:33:19 -07:00
|
|
|
wpi::DenseMap<void*, Component> components;
|
2017-12-04 23:28:33 -08:00
|
|
|
|
|
|
|
|
std::shared_ptr<nt::NetworkTable> liveWindowTable;
|
|
|
|
|
std::shared_ptr<nt::NetworkTable> statusTable;
|
|
|
|
|
nt::NetworkTableEntry enabledEntry;
|
|
|
|
|
|
|
|
|
|
bool startLiveWindow = false;
|
|
|
|
|
bool liveWindowEnabled = false;
|
|
|
|
|
bool telemetryEnabled = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
LiveWindow::Impl::Impl()
|
|
|
|
|
: liveWindowTable(
|
|
|
|
|
nt::NetworkTableInstance::GetDefault().GetTable("LiveWindow")) {
|
|
|
|
|
statusTable = liveWindowTable->GetSubTable(".status");
|
|
|
|
|
enabledEntry = statusTable->GetEntry("LW Enabled");
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-20 17:30:37 -07:00
|
|
|
LiveWindow* LiveWindow::GetInstance() {
|
2015-06-24 04:25:10 -07:00
|
|
|
static LiveWindow instance;
|
Revert changes preventing old user code from compiling.
I'm not 100% sure whether we want these, but they are a quick
find and replace to do.
Basically, there are two primary things that we have done
this summer that break existing user code:
-Changing GetInstance() calls to return references instead
of pointers. This forces users to change from doing something
like LiveWindow::GetInstance()->AddSensor() to LiveWindow::GetInstance().AddSensor().
-Making PIDGet() and related calls const, forcing users to change
the function signatures wherever they override them.
The GetInstance() calls don't really matter to me either way,
especially since there are no real ownership issues going on there,
unlike the rest of the smart pointer-related changes.
For the const stuff, it is certainly more correct to mandate that
user PIDGet() functions be const and the such, but at the same time,
I'm not sure that there is any strong need for it, and the errors
generated are not the most helpful. While this wouldn't necessarily
be an issue for more experienced teams or completely new teams (who
don't have any old code to be reusing), it may cause issues for more
average teams who aren't familiar with the intricacies of C++ anything.
Change-Id: I6e7007982069292ea70e6d0fc8ca40203340df1b
2015-07-24 19:19:40 -04:00
|
|
|
return &instance;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
void LiveWindow::Add(std::shared_ptr<Sendable> sendable) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_impl->mutex);
|
2017-12-04 23:28:33 -08:00
|
|
|
auto& comp = m_impl->components[sendable.get()];
|
|
|
|
|
comp.sendable = sendable;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
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) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_impl->mutex);
|
2017-12-04 23:28:33 -08:00
|
|
|
auto& comp = m_impl->components[child];
|
|
|
|
|
comp.parent = parent;
|
|
|
|
|
comp.telemetryEnabled = false;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2019-08-03 02:47:17 -04:00
|
|
|
bool LiveWindow::Remove(Sendable* sendable) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_impl->mutex);
|
2019-08-03 02:47:17 -04:00
|
|
|
return m_impl->components.erase(sendable);
|
2017-12-04 23:28:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LiveWindow::EnableTelemetry(Sendable* sendable) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_impl->mutex);
|
2017-12-04 23:28:33 -08:00
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LiveWindow::DisableTelemetry(Sendable* sendable) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_impl->mutex);
|
2017-12-04 23:28:33 -08:00
|
|
|
auto i = m_impl->components.find(sendable);
|
|
|
|
|
if (i != m_impl->components.end()) i->getSecond().telemetryEnabled = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LiveWindow::DisableAllTelemetry() {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_impl->mutex);
|
2017-12-04 23:28:33 -08:00
|
|
|
m_impl->telemetryEnabled = false;
|
|
|
|
|
for (auto& i : m_impl->components) i.getSecond().telemetryEnabled = false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
bool LiveWindow::IsEnabled() const {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_impl->mutex);
|
2018-05-31 20:47:15 -07:00
|
|
|
return m_impl->liveWindowEnabled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LiveWindow::SetEnabled(bool enabled) {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_impl->mutex);
|
2018-05-31 20:47:15 -07:00
|
|
|
if (m_impl->liveWindowEnabled == enabled) return;
|
|
|
|
|
Scheduler* scheduler = Scheduler::GetInstance();
|
2018-12-07 22:38:22 -05:00
|
|
|
m_impl->startLiveWindow = enabled;
|
|
|
|
|
m_impl->liveWindowEnabled = enabled;
|
|
|
|
|
// Force table generation now to make sure everything is defined
|
|
|
|
|
UpdateValuesUnsafe();
|
2018-05-31 20:47:15 -07:00
|
|
|
if (enabled) {
|
|
|
|
|
scheduler->SetEnabled(false);
|
|
|
|
|
scheduler->RemoveAll();
|
|
|
|
|
} else {
|
|
|
|
|
for (auto& i : m_impl->components) {
|
|
|
|
|
i.getSecond().builder.StopLiveWindowMode();
|
|
|
|
|
}
|
|
|
|
|
scheduler->SetEnabled(true);
|
|
|
|
|
}
|
|
|
|
|
m_impl->enabledEntry.SetBoolean(enabled);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
void LiveWindow::UpdateValues() {
|
2019-07-08 22:58:39 -07:00
|
|
|
std::scoped_lock lock(m_impl->mutex);
|
2018-12-07 22:38:22 -05:00
|
|
|
UpdateValuesUnsafe();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LiveWindow::UpdateValuesUnsafe() {
|
2017-12-04 23:28:33 -08:00
|
|
|
// 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");
|
|
|
|
|
|
|
|
|
|
comp.firstTime = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_impl->startLiveWindow) comp.builder.StartLiveWindowMode();
|
|
|
|
|
comp.builder.UpdateTable();
|
2015-06-25 15:07:55 -04:00
|
|
|
}
|
|
|
|
|
}
|
2017-12-04 23:28:33 -08:00
|
|
|
|
|
|
|
|
m_impl->startLiveWindow = false;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
LiveWindow::LiveWindow() : m_impl(new Impl) {}
|