2020-12-26 14:31:24 -08:00
|
|
|
// 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.
|
2020-12-23 00:07:44 -05:00
|
|
|
|
|
|
|
|
#include "glass/networktables/NTPIDController.h"
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
#include <wpi/StringExtras.h>
|
|
|
|
|
|
2020-12-23 00:07:44 -05:00
|
|
|
using namespace glass;
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
NTPIDControllerModel::NTPIDControllerModel(std::string_view path)
|
2020-12-23 00:07:44 -05:00
|
|
|
: NTPIDControllerModel(nt::GetDefaultInstance(), path) {}
|
|
|
|
|
|
|
|
|
|
NTPIDControllerModel::NTPIDControllerModel(NT_Inst instance,
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string_view path)
|
2020-12-23 00:07:44 -05:00
|
|
|
: m_nt(instance),
|
2021-06-06 16:13:58 -07:00
|
|
|
m_name(m_nt.GetEntry(fmt::format("{}/.name", path))),
|
|
|
|
|
m_controllable(m_nt.GetEntry(fmt::format("{}/.controllable", path))),
|
|
|
|
|
m_p(m_nt.GetEntry(fmt::format("{}/p", path))),
|
|
|
|
|
m_i(m_nt.GetEntry(fmt::format("{}/i", path))),
|
|
|
|
|
m_d(m_nt.GetEntry(fmt::format("{}/d", path))),
|
|
|
|
|
m_setpoint(m_nt.GetEntry(fmt::format("{}/setpoint", path))),
|
|
|
|
|
m_pData(fmt::format("NTPIDCtrlP:{}", path)),
|
|
|
|
|
m_iData(fmt::format("NTPIDCtrlI:{}", path)),
|
|
|
|
|
m_dData(fmt::format("NTPIDCtrlD:{}", path)),
|
|
|
|
|
m_setpointData(fmt::format("NTPIDCtrlStpt:{}", path)),
|
|
|
|
|
m_nameValue(wpi::rsplit(path, '/').second) {
|
2020-12-23 00:07:44 -05:00
|
|
|
m_nt.AddListener(m_name);
|
2021-01-05 21:33:05 -05:00
|
|
|
m_nt.AddListener(m_controllable);
|
2020-12-23 00:07:44 -05:00
|
|
|
m_nt.AddListener(m_p);
|
|
|
|
|
m_nt.AddListener(m_i);
|
|
|
|
|
m_nt.AddListener(m_d);
|
|
|
|
|
m_nt.AddListener(m_setpoint);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NTPIDControllerModel::SetP(double value) {
|
|
|
|
|
nt::SetEntryValue(m_p, nt::NetworkTableValue::MakeDouble(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NTPIDControllerModel::SetI(double value) {
|
|
|
|
|
nt::SetEntryValue(m_i, nt::NetworkTableValue::MakeDouble(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NTPIDControllerModel::SetD(double value) {
|
|
|
|
|
nt::SetEntryValue(m_d, nt::NetworkTableValue::MakeDouble(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NTPIDControllerModel::SetSetpoint(double value) {
|
|
|
|
|
nt::SetEntryValue(m_setpoint, nt::NetworkTableValue::MakeDouble(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NTPIDControllerModel::Update() {
|
|
|
|
|
for (auto&& event : m_nt.PollListener()) {
|
|
|
|
|
if (event.entry == m_name) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (event.value && event.value->IsString()) {
|
2020-12-23 00:07:44 -05:00
|
|
|
m_nameValue = event.value->GetString();
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2020-12-23 00:07:44 -05:00
|
|
|
} else if (event.entry == m_p) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (event.value && event.value->IsDouble()) {
|
2020-12-23 00:07:44 -05:00
|
|
|
m_pData.SetValue(event.value->GetDouble());
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2020-12-23 00:07:44 -05:00
|
|
|
} else if (event.entry == m_i) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (event.value && event.value->IsDouble()) {
|
2020-12-23 00:07:44 -05:00
|
|
|
m_iData.SetValue(event.value->GetDouble());
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2020-12-23 00:07:44 -05:00
|
|
|
} else if (event.entry == m_d) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (event.value && event.value->IsDouble()) {
|
2020-12-23 00:07:44 -05:00
|
|
|
m_dData.SetValue(event.value->GetDouble());
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2020-12-23 00:07:44 -05:00
|
|
|
} else if (event.entry == m_setpoint) {
|
2020-12-28 12:58:06 -08:00
|
|
|
if (event.value && event.value->IsDouble()) {
|
2020-12-23 00:07:44 -05:00
|
|
|
m_setpointData.SetValue(event.value->GetDouble());
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2021-01-05 21:33:05 -05:00
|
|
|
} else if (event.entry == m_controllable) {
|
|
|
|
|
if (event.value && event.value->IsBoolean()) {
|
|
|
|
|
m_controllableValue = event.value->GetBoolean();
|
|
|
|
|
}
|
2020-12-23 00:07:44 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool NTPIDControllerModel::Exists() {
|
|
|
|
|
return m_nt.IsConnected() && nt::GetEntryType(m_setpoint) != NT_UNASSIGNED;
|
|
|
|
|
}
|