[sysid] Load DataLog files directly for analysis (#6103)

Co-authored-by: Oblarg <emichaelbrnett@gmail.com>
This commit is contained in:
Peter Johnson
2024-01-05 16:24:31 -08:00
committed by GitHub
parent f94e3d81b9
commit 7c26bc70ab
33 changed files with 1086 additions and 2013 deletions

View File

@@ -24,21 +24,20 @@
#include <wpigui_openurl.h>
#include "sysid/view/Analyzer.h"
#include "sysid/view/JSONConverter.h"
#include "sysid/view/Logger.h"
#include "sysid/view/DataSelector.h"
#include "sysid/view/LogLoader.h"
#include "sysid/view/UILayout.h"
namespace gui = wpi::gui;
static std::unique_ptr<glass::WindowManager> gWindowManager;
glass::Window* gLoggerWindow;
glass::Window* gLogLoaderWindow;
glass::Window* gDataSelectorWindow;
glass::Window* gAnalyzerWindow;
glass::Window* gProgramLogWindow;
static glass::MainMenuBar gMainMenu;
std::unique_ptr<sysid::JSONConverter> gJSONConverter;
glass::LogData gLog;
wpi::Logger gLogger;
@@ -103,11 +102,23 @@ void Application(std::string_view saveDir) {
gWindowManager = std::make_unique<glass::WindowManager>(storage);
gWindowManager->GlobalInit();
gLoggerWindow = gWindowManager->AddWindow(
"Logger", std::make_unique<sysid::Logger>(storage, gLogger));
auto logLoader = std::make_unique<sysid::LogLoader>(storage, gLogger);
auto dataSelector = std::make_unique<sysid::DataSelector>(storage, gLogger);
auto analyzer = std::make_unique<sysid::Analyzer>(storage, gLogger);
gAnalyzerWindow = gWindowManager->AddWindow(
"Analyzer", std::make_unique<sysid::Analyzer>(storage, gLogger));
logLoader->unload.connect([ds = dataSelector.get()] { ds->Reset(); });
dataSelector->testdata = [_analyzer = analyzer.get()](auto data) {
_analyzer->m_data = data;
_analyzer->AnalyzeData();
};
gLogLoaderWindow =
gWindowManager->AddWindow("Log Loader", std::move(logLoader));
gDataSelectorWindow =
gWindowManager->AddWindow("Data Selector", std::move(dataSelector));
gAnalyzerWindow = gWindowManager->AddWindow("Analyzer", std::move(analyzer));
gProgramLogWindow = gWindowManager->AddWindow(
"Program Log", std::make_unique<glass::LogView>(&gLog));
@@ -115,10 +126,16 @@ void Application(std::string_view saveDir) {
// Set default positions and sizes for windows.
// Logger window position/size
gLoggerWindow->SetDefaultPos(sysid::kLoggerWindowPos.x,
sysid::kLoggerWindowPos.y);
gLoggerWindow->SetDefaultSize(sysid::kLoggerWindowSize.x,
sysid::kLoggerWindowSize.y);
gLogLoaderWindow->SetDefaultPos(sysid::kLogLoaderWindowPos.x,
sysid::kLogLoaderWindowPos.y);
gLogLoaderWindow->SetDefaultSize(sysid::kLogLoaderWindowSize.x,
sysid::kLogLoaderWindowSize.y);
// Data selector window position/size
gDataSelectorWindow->SetDefaultPos(sysid::kDataSelectorWindowPos.x,
sysid::kDataSelectorWindowPos.y);
gDataSelectorWindow->SetDefaultSize(sysid::kDataSelectorWindowSize.x,
sysid::kDataSelectorWindowSize.y);
// Analyzer window position/size
gAnalyzerWindow->SetDefaultPos(sysid::kAnalyzerWindowPos.x,
@@ -133,8 +150,6 @@ void Application(std::string_view saveDir) {
sysid::kProgramLogWindowSize.y);
gProgramLogWindow->DisableRenamePopup();
gJSONConverter = std::make_unique<sysid::JSONConverter>(gLogger);
// Configure save file.
gui::ConfigurePlatformSaveFile("sysid.ini");
@@ -157,15 +172,6 @@ void Application(std::string_view saveDir) {
ImGui::EndMenu();
}
bool toCSV = false;
if (ImGui::BeginMenu("JSON Converters")) {
if (ImGui::MenuItem("JSON to CSV Converter")) {
toCSV = true;
}
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Docs")) {
if (ImGui::MenuItem("Online documentation")) {
wpi::gui::OpenURL(
@@ -178,19 +184,6 @@ void Application(std::string_view saveDir) {
ImGui::EndMainMenuBar();
if (toCSV) {
ImGui::OpenPopup("SysId JSON to CSV Converter");
toCSV = false;
}
if (ImGui::BeginPopupModal("SysId JSON to CSV Converter")) {
gJSONConverter->DisplayCSVConvert();
if (ImGui::Button("Close")) {
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
}
if (about) {
ImGui::OpenPopup("About");
about = false;

View File

@@ -5,24 +5,39 @@
#include "sysid/analysis/AnalysisManager.h"
#include <cmath>
#include <cstddef>
#include <functional>
#include <stdexcept>
#include <fmt/format.h>
#include <units/angle.h>
#include <units/math.h>
#include <wpi/MathExtras.h>
#include <wpi/MemoryBuffer.h>
#include <wpi/StringExtras.h>
#include <wpi/StringMap.h>
#include "sysid/Util.h"
#include "sysid/analysis/FilteringUtils.h"
#include "sysid/analysis/JSONConverter.h"
#include "sysid/analysis/TrackWidthAnalysis.h"
using namespace sysid;
static double Lerp(units::second_t time,
std::vector<MotorData::Run::Sample<double>>& data) {
auto next = std::find_if(data.begin(), data.end(), [&](const auto& entry) {
return entry.time > time;
});
if (next == data.begin()) {
next++;
}
if (next == data.end()) {
next--;
}
const auto prev = next - 1;
return wpi::Lerp(prev->measurement, next->measurement,
(time - prev->time) / (next->time - prev->time));
}
/**
* Converts a raw data vector into a PreparedData vector with only the
* timestamp, voltage, position, and velocity fields filled out.
@@ -38,18 +53,25 @@ using namespace sysid;
*
* @return A PreparedData vector
*/
template <size_t S, size_t Timestamp, size_t Voltage, size_t Position,
size_t Velocity>
static std::vector<PreparedData> ConvertToPrepared(
const std::vector<std::array<double, S>>& data) {
static std::vector<PreparedData> ConvertToPrepared(const MotorData& data) {
std::vector<PreparedData> prepared;
for (int i = 0; i < static_cast<int>(data.size()) - 1; ++i) {
const auto& pt1 = data[i];
const auto& pt2 = data[i + 1];
prepared.emplace_back(PreparedData{
units::second_t{pt1[Timestamp]}, pt1[Voltage], pt1[Position],
pt1[Velocity], units::second_t{pt2[Timestamp] - pt1[Timestamp]}});
// assume we've selected down to a single contiguous run by this point
auto run = data.runs[0];
for (int i = 0; i < static_cast<int>(run.voltage.size()) - 1; ++i) {
const auto& currentVoltage = run.voltage[i];
const auto& nextVoltage = run.voltage[i + 1];
auto currentPosition = Lerp(currentVoltage.time, run.position);
auto currentVelocity = Lerp(currentVoltage.time, run.velocity);
prepared.emplace_back(PreparedData{currentVoltage.time,
currentVoltage.measurement.value(),
currentPosition, currentVelocity,
nextVoltage.time - currentVoltage.time});
}
return prepared;
}
@@ -62,18 +84,16 @@ static std::vector<PreparedData> ConvertToPrepared(
*
* @param dataset A reference to the dataset being used
*/
template <size_t S>
static void CopyRawData(
wpi::StringMap<std::vector<std::array<double, S>>>* dataset) {
static void CopyRawData(wpi::StringMap<MotorData>* dataset) {
auto& data = *dataset;
// Loads the Raw Data
for (auto& it : data) {
auto key = it.first();
auto& dataset = it.getValue();
auto& motorData = it.getValue();
if (!wpi::contains(key, "raw")) {
data[fmt::format("raw-{}", key)] = dataset;
data[fmt::format("original-raw-{}", key)] = dataset;
data[fmt::format("raw-{}", key)] = motorData;
data[fmt::format("original-raw-{}", key)] = motorData;
}
}
}
@@ -94,416 +114,73 @@ static Storage CombineDatasets(const std::vector<PreparedData>& slowForward,
}
void AnalysisManager::PrepareGeneralData() {
using Data = std::array<double, 4>;
wpi::StringMap<std::vector<Data>> data;
wpi::StringMap<std::vector<PreparedData>> preparedData;
// Store the raw data columns.
constexpr size_t kTimeCol = 0;
constexpr size_t kVoltageCol = 1;
constexpr size_t kPosCol = 2;
constexpr size_t kVelCol = 3;
WPI_INFO(m_logger, "{}", "Reading JSON data.");
// Get the major components from the JSON and store them inside a StringMap.
for (auto&& key : AnalysisManager::kJsonDataKeys) {
data[key] = m_json.at(key).get<std::vector<Data>>();
}
WPI_INFO(m_logger, "{}", "Preprocessing raw data.");
// Ensure that voltage and velocity have the same sign. Also multiply
// positions and velocities by the factor.
for (auto it = data.begin(); it != data.end(); ++it) {
for (auto&& pt : it->second) {
pt[kVoltageCol] = std::copysign(pt[kVoltageCol], pt[kVelCol]);
pt[kPosCol] *= m_factor;
pt[kVelCol] *= m_factor;
}
}
WPI_INFO(m_logger, "{}", "Copying raw data.");
CopyRawData(&data);
CopyRawData(&m_data.motorData);
WPI_INFO(m_logger, "{}", "Converting raw data to PreparedData struct.");
// Convert data to PreparedData structs
for (auto& it : data) {
for (auto& it : m_data.motorData) {
auto key = it.first();
preparedData[key] =
ConvertToPrepared<4, kTimeCol, kVoltageCol, kPosCol, kVelCol>(
data[key]);
preparedData[key] = ConvertToPrepared(m_data.motorData[key]);
WPI_INFO(m_logger, "SAMPLES {}", preparedData[key].size());
}
// Store the original datasets
m_originalDataset[static_cast<int>(
AnalysisManager::Settings::DrivetrainDataset::kCombined)] =
CombineDatasets(preparedData["original-raw-slow-forward"],
preparedData["original-raw-slow-backward"],
preparedData["original-raw-fast-forward"],
preparedData["original-raw-fast-backward"]);
m_originalDataset =
CombineDatasets(preparedData["original-raw-quasistatic-forward"],
preparedData["original-raw-quasistatic-reverse"],
preparedData["original-raw-dynamic-forward"],
preparedData["original-raw-dynamic-reverse"]);
WPI_INFO(m_logger, "{}", "Initial trimming and filtering.");
sysid::InitialTrimAndFilter(&preparedData, &m_settings, m_positionDelays,
m_velocityDelays, m_minStepTime, m_maxStepTime,
m_unit);
m_data.distanceUnit);
WPI_INFO(m_logger, "{}", m_minStepTime);
WPI_INFO(m_logger, "{}", m_maxStepTime);
WPI_INFO(m_logger, "{}", "Acceleration filtering.");
sysid::AccelFilter(&preparedData);
WPI_INFO(m_logger, "{}", "Storing datasets.");
// Store the raw datasets
m_rawDataset[static_cast<int>(
AnalysisManager::Settings::DrivetrainDataset::kCombined)] =
CombineDatasets(
preparedData["raw-slow-forward"], preparedData["raw-slow-backward"],
preparedData["raw-fast-forward"], preparedData["raw-fast-backward"]);
m_rawDataset = CombineDatasets(preparedData["raw-quasistatic-forward"],
preparedData["raw-quasistatic-reverse"],
preparedData["raw-dynamic-forward"],
preparedData["raw-dynamic-reverse"]);
// Store the filtered datasets
m_filteredDataset[static_cast<int>(
AnalysisManager::Settings::DrivetrainDataset::kCombined)] =
CombineDatasets(
preparedData["slow-forward"], preparedData["slow-backward"],
preparedData["fast-forward"], preparedData["fast-backward"]);
m_filteredDataset = CombineDatasets(
preparedData["quasistatic-forward"], preparedData["quasistatic-reverse"],
preparedData["dynamic-forward"], preparedData["dynamic-reverse"]);
m_startTimes = {preparedData["raw-slow-forward"][0].timestamp,
preparedData["raw-slow-backward"][0].timestamp,
preparedData["raw-fast-forward"][0].timestamp,
preparedData["raw-fast-backward"][0].timestamp};
}
void AnalysisManager::PrepareAngularDrivetrainData() {
using Data = std::array<double, 9>;
wpi::StringMap<std::vector<Data>> data;
wpi::StringMap<std::vector<PreparedData>> preparedData;
// Store the relevant raw data columns.
constexpr size_t kTimeCol = 0;
constexpr size_t kLVoltageCol = 1;
constexpr size_t kRVoltageCol = 2;
constexpr size_t kLPosCol = 3;
constexpr size_t kRPosCol = 4;
constexpr size_t kLVelCol = 5;
constexpr size_t kRVelCol = 6;
constexpr size_t kAngleCol = 7;
constexpr size_t kAngularRateCol = 8;
WPI_INFO(m_logger, "{}", "Reading JSON data.");
// Get the major components from the JSON and store them inside a StringMap.
for (auto&& key : AnalysisManager::kJsonDataKeys) {
data[key] = m_json.at(key).get<std::vector<Data>>();
}
WPI_INFO(m_logger, "{}", "Preprocessing raw data.");
// Ensure that voltage and velocity have the same sign. Also multiply
// positions and velocities by the factor.
for (auto it = data.begin(); it != data.end(); ++it) {
for (auto&& pt : it->second) {
pt[kLPosCol] *= m_factor;
pt[kRPosCol] *= m_factor;
pt[kLVelCol] *= m_factor;
pt[kRVelCol] *= m_factor;
// Stores the average voltages in the left voltage column.
// This aggregates the left and right voltages into a single voltage
// column for the ConvertToPrepared() method. std::copysign() ensures the
// polarity of the voltage matches the direction the robot turns.
pt[kLVoltageCol] = std::copysign(
(std::abs(pt[kLVoltageCol]) + std::abs(pt[kRVoltageCol])) / 2,
pt[kAngularRateCol]);
// ω = (v_r - v_l) / trackwidth
// v = ωr => v = ω * trackwidth / 2
// (v_r - v_l) / trackwidth * (trackwidth / 2) = (v_r - v_l) / 2
// However, since we know this is an angular test, the left and right
// wheel velocities will have opposite signs, allowing us to add their
// absolute values and get the same result (in terms of magnitude).
// std::copysign() is used to make sure the direction of the wheel
// velocities matches the direction the robot turns.
pt[kAngularRateCol] =
std::copysign((std::abs(pt[kRVelCol]) + std::abs(pt[kLVelCol])) / 2,
pt[kAngularRateCol]);
}
}
WPI_INFO(m_logger, "{}", "Calculating trackwidth");
// Aggregating all the deltas from all the tests
double leftDelta = 0.0;
double rightDelta = 0.0;
double angleDelta = 0.0;
for (const auto& it : data) {
auto key = it.first();
auto& trackWidthData = data[key];
leftDelta += std::abs(trackWidthData.back()[kLPosCol] -
trackWidthData.front()[kLPosCol]);
rightDelta += std::abs(trackWidthData.back()[kRPosCol] -
trackWidthData.front()[kRPosCol]);
angleDelta += std::abs(trackWidthData.back()[kAngleCol] -
trackWidthData.front()[kAngleCol]);
}
m_trackWidth = sysid::CalculateTrackWidth(leftDelta, rightDelta,
units::radian_t{angleDelta});
WPI_INFO(m_logger, "{}", "Copying raw data.");
CopyRawData(&data);
WPI_INFO(m_logger, "{}", "Converting to PreparedData struct.");
// Convert raw data to prepared data
for (const auto& it : data) {
auto key = it.first();
preparedData[key] = ConvertToPrepared<9, kTimeCol, kLVoltageCol, kAngleCol,
kAngularRateCol>(data[key]);
}
// Create the distinct datasets and store them
m_originalDataset[static_cast<int>(
AnalysisManager::Settings::DrivetrainDataset::kCombined)] =
CombineDatasets(preparedData["original-raw-slow-forward"],
preparedData["original-raw-slow-backward"],
preparedData["original-raw-fast-forward"],
preparedData["original-raw-fast-backward"]);
WPI_INFO(m_logger, "{}", "Applying trimming and filtering.");
sysid::InitialTrimAndFilter(&preparedData, &m_settings, m_positionDelays,
m_velocityDelays, m_minStepTime, m_maxStepTime);
WPI_INFO(m_logger, "{}", "Acceleration filtering.");
sysid::AccelFilter(&preparedData);
WPI_INFO(m_logger, "{}", "Storing datasets.");
// Create the distinct datasets and store them
m_rawDataset[static_cast<int>(
AnalysisManager::Settings::DrivetrainDataset::kCombined)] =
CombineDatasets(
preparedData["raw-slow-forward"], preparedData["raw-slow-backward"],
preparedData["raw-fast-forward"], preparedData["raw-fast-backward"]);
m_filteredDataset[static_cast<int>(
AnalysisManager::Settings::DrivetrainDataset::kCombined)] =
CombineDatasets(
preparedData["slow-forward"], preparedData["slow-backward"],
preparedData["fast-forward"], preparedData["fast-backward"]);
m_startTimes = {preparedData["slow-forward"][0].timestamp,
preparedData["slow-backward"][0].timestamp,
preparedData["fast-forward"][0].timestamp,
preparedData["fast-backward"][0].timestamp};
}
void AnalysisManager::PrepareLinearDrivetrainData() {
using Data = std::array<double, 9>;
wpi::StringMap<std::vector<Data>> data;
wpi::StringMap<std::vector<PreparedData>> preparedData;
// Store the relevant raw data columns.
constexpr size_t kTimeCol = 0;
constexpr size_t kLVoltageCol = 1;
constexpr size_t kRVoltageCol = 2;
constexpr size_t kLPosCol = 3;
constexpr size_t kRPosCol = 4;
constexpr size_t kLVelCol = 5;
constexpr size_t kRVelCol = 6;
// Get the major components from the JSON and store them inside a StringMap.
WPI_INFO(m_logger, "{}", "Reading JSON data.");
for (auto&& key : AnalysisManager::kJsonDataKeys) {
data[key] = m_json.at(key).get<std::vector<Data>>();
}
// Ensure that voltage and velocity have the same sign. Also multiply
// positions and velocities by the factor.
WPI_INFO(m_logger, "{}", "Preprocessing raw data.");
for (auto it = data.begin(); it != data.end(); ++it) {
for (auto&& pt : it->second) {
pt[kLVoltageCol] = std::copysign(pt[kLVoltageCol], pt[kLVelCol]);
pt[kRVoltageCol] = std::copysign(pt[kRVoltageCol], pt[kRVelCol]);
pt[kLPosCol] *= m_factor;
pt[kRPosCol] *= m_factor;
pt[kLVelCol] *= m_factor;
pt[kRVelCol] *= m_factor;
}
}
WPI_INFO(m_logger, "{}", "Copying raw data.");
CopyRawData(&data);
// Convert data to PreparedData
WPI_INFO(m_logger, "{}", "Converting to PreparedData struct.");
for (auto& it : data) {
auto key = it.first();
preparedData[fmt::format("left-{}", key)] =
ConvertToPrepared<9, kTimeCol, kLVoltageCol, kLPosCol, kLVelCol>(
data[key]);
preparedData[fmt::format("right-{}", key)] =
ConvertToPrepared<9, kTimeCol, kRVoltageCol, kRPosCol, kRVelCol>(
data[key]);
}
// Create the distinct raw datasets and store them
auto originalSlowForward = AnalysisManager::DataConcat(
preparedData["left-original-raw-slow-forward"],
preparedData["right-original-raw-slow-forward"]);
auto originalSlowBackward = AnalysisManager::DataConcat(
preparedData["left-original-raw-slow-backward"],
preparedData["right-original-raw-slow-backward"]);
auto originalFastForward = AnalysisManager::DataConcat(
preparedData["left-original-raw-fast-forward"],
preparedData["right-original-raw-fast-forward"]);
auto originalFastBackward = AnalysisManager::DataConcat(
preparedData["left-original-raw-fast-backward"],
preparedData["right-original-raw-fast-backward"]);
m_originalDataset[static_cast<int>(
AnalysisManager::Settings::DrivetrainDataset::kCombined)] =
CombineDatasets(originalSlowForward, originalSlowBackward,
originalFastForward, originalFastBackward);
m_originalDataset[static_cast<int>(
AnalysisManager::Settings::DrivetrainDataset::kLeft)] =
CombineDatasets(preparedData["left-original-raw-slow-forward"],
preparedData["left-original-raw-slow-backward"],
preparedData["left-original-raw-fast-forward"],
preparedData["left-original-raw-fast-backward"]);
m_originalDataset[static_cast<int>(
AnalysisManager::Settings::DrivetrainDataset::kRight)] =
CombineDatasets(preparedData["right-original-raw-slow-forward"],
preparedData["right-original-raw-slow-backward"],
preparedData["right-original-raw-fast-forward"],
preparedData["right-original-raw-fast-backward"]);
WPI_INFO(m_logger, "{}", "Applying trimming and filtering.");
sysid::InitialTrimAndFilter(&preparedData, &m_settings, m_positionDelays,
m_velocityDelays, m_minStepTime, m_maxStepTime);
auto slowForward = AnalysisManager::DataConcat(
preparedData["left-slow-forward"], preparedData["right-slow-forward"]);
auto slowBackward = AnalysisManager::DataConcat(
preparedData["left-slow-backward"], preparedData["right-slow-backward"]);
auto fastForward = AnalysisManager::DataConcat(
preparedData["left-fast-forward"], preparedData["right-fast-forward"]);
auto fastBackward = AnalysisManager::DataConcat(
preparedData["left-fast-backward"], preparedData["right-fast-backward"]);
WPI_INFO(m_logger, "{}", "Acceleration filtering.");
sysid::AccelFilter(&preparedData);
WPI_INFO(m_logger, "{}", "Storing datasets.");
// Create the distinct raw datasets and store them
auto rawSlowForward =
AnalysisManager::DataConcat(preparedData["left-raw-slow-forward"],
preparedData["right-raw-slow-forward"]);
auto rawSlowBackward =
AnalysisManager::DataConcat(preparedData["left-raw-slow-backward"],
preparedData["right-raw-slow-backward"]);
auto rawFastForward =
AnalysisManager::DataConcat(preparedData["left-raw-fast-forward"],
preparedData["right-raw-fast-forward"]);
auto rawFastBackward =
AnalysisManager::DataConcat(preparedData["left-raw-fast-backward"],
preparedData["right-raw-fast-backward"]);
m_rawDataset[static_cast<int>(
AnalysisManager::Settings::DrivetrainDataset::kCombined)] =
CombineDatasets(rawSlowForward, rawSlowBackward, rawFastForward,
rawFastBackward);
m_rawDataset[static_cast<int>(
AnalysisManager::Settings::DrivetrainDataset::kLeft)] =
CombineDatasets(preparedData["left-raw-slow-forward"],
preparedData["left-raw-slow-backward"],
preparedData["left-raw-fast-forward"],
preparedData["left-raw-fast-backward"]);
m_rawDataset[static_cast<int>(
AnalysisManager::Settings::DrivetrainDataset::kRight)] =
CombineDatasets(preparedData["right-raw-slow-forward"],
preparedData["right-raw-slow-backward"],
preparedData["right-raw-fast-forward"],
preparedData["right-raw-fast-backward"]);
// Create the distinct filtered datasets and store them
m_filteredDataset[static_cast<int>(
AnalysisManager::Settings::DrivetrainDataset::kCombined)] =
CombineDatasets(slowForward, slowBackward, fastForward, fastBackward);
m_filteredDataset[static_cast<int>(
AnalysisManager::Settings::DrivetrainDataset::kLeft)] =
CombineDatasets(preparedData["left-slow-forward"],
preparedData["left-slow-backward"],
preparedData["left-fast-forward"],
preparedData["left-fast-backward"]);
m_filteredDataset[static_cast<int>(
AnalysisManager::Settings::DrivetrainDataset::kRight)] =
CombineDatasets(preparedData["right-slow-forward"],
preparedData["right-slow-backward"],
preparedData["right-fast-forward"],
preparedData["right-fast-backward"]);
m_startTimes = {
rawSlowForward.front().timestamp, rawSlowBackward.front().timestamp,
rawFastForward.front().timestamp, rawFastBackward.front().timestamp};
m_startTimes = {preparedData["raw-quasistatic-forward"][0].timestamp,
preparedData["raw-quasistatic-reverse"][0].timestamp,
preparedData["raw-dynamic-forward"][0].timestamp,
preparedData["raw-dynamic-reverse"][0].timestamp};
}
AnalysisManager::AnalysisManager(Settings& settings, wpi::Logger& logger)
: m_logger{logger},
m_settings{settings},
m_type{analysis::kSimple},
m_unit{"Meters"},
m_factor{1} {}
: m_logger{logger}, m_settings{settings} {}
AnalysisManager::AnalysisManager(std::string_view path, Settings& settings,
AnalysisManager::AnalysisManager(TestData data, Settings& settings,
wpi::Logger& logger)
: m_logger{logger}, m_settings{settings} {
{
// Read JSON from the specified path
std::error_code ec;
std::unique_ptr<wpi::MemoryBuffer> fileBuffer =
wpi::MemoryBuffer::GetFile(path, ec);
if (fileBuffer == nullptr || ec) {
throw FileReadingError(path);
}
m_json = wpi::json::parse(fileBuffer->GetCharBuffer());
WPI_INFO(m_logger, "Read {}", path);
}
// Check that we have a sysid JSON
if (m_json.find("sysid") == m_json.end()) {
// If it's not a sysid JSON, try converting it from frc-char format
std::string newPath = sysid::ConvertJSON(path, logger);
// Read JSON from the specified path
std::error_code ec;
std::unique_ptr<wpi::MemoryBuffer> fileBuffer =
wpi::MemoryBuffer::GetFile(path, ec);
if (fileBuffer == nullptr || ec) {
throw FileReadingError(newPath);
}
m_json = wpi::json::parse(fileBuffer->GetCharBuffer());
WPI_INFO(m_logger, "Read {}", newPath);
}
WPI_INFO(m_logger, "Parsing initial data of {}", path);
// Get the analysis type from the JSON.
m_type = sysid::analysis::FromName(m_json.at("test").get<std::string>());
// Get the rotation -> output units factor from the JSON.
m_unit = m_json.at("units").get<std::string>();
m_factor = m_json.at("unitsPerRotation").get<double>();
WPI_DEBUG(m_logger, "Parsing units per rotation as {} {} per rotation",
m_factor, m_unit);
: m_data{std::move(data)}, m_logger{logger}, m_settings{settings} {
// Reset settings for Dynamic Test Limits
m_settings.stepTestDuration = units::second_t{0.0};
m_settings.motionThreshold = std::numeric_limits<double>::infinity();
}
void AnalysisManager::PrepareData() {
WPI_INFO(m_logger, "Preparing {} data", m_type.name);
if (m_type == analysis::kDrivetrain) {
PrepareLinearDrivetrainData();
} else if (m_type == analysis::kDrivetrainAngular) {
PrepareAngularDrivetrainData();
} else {
PrepareGeneralData();
}
// WPI_INFO(m_logger, "Preparing {} data", m_data.mechanismType.name);
PrepareGeneralData();
WPI_INFO(m_logger, "{}", "Finished Preparing Data");
}
@@ -515,8 +192,9 @@ AnalysisManager::FeedforwardGains AnalysisManager::CalculateFeedforward() {
WPI_INFO(m_logger, "{}", "Calculating Gains");
// Calculate feedforward gains from the data.
const auto& ff = sysid::CalculateFeedforwardGains(GetFilteredData(), m_type);
FeedforwardGains ffGains = {ff, m_trackWidth};
const auto& ff = sysid::CalculateFeedforwardGains(
GetFilteredData(), m_data.mechanismType, false);
FeedforwardGains ffGains = {ff};
const auto& Ks = ff.coeffs[0];
const auto& Kv = ff.coeffs[1];
@@ -542,27 +220,20 @@ sysid::FeedbackGains AnalysisManager::CalculateFeedback(
if (m_settings.type == FeedbackControllerLoopType::kPosition) {
fb = sysid::CalculatePositionFeedbackGains(
m_settings.preset, m_settings.lqr, Kv, Ka,
m_settings.convertGainsToEncTicks
? m_settings.gearing * m_settings.cpr * m_factor
: 1);
m_settings.convertGainsToEncTicks ? m_settings.gearing * m_settings.cpr
: 1);
} else {
fb = sysid::CalculateVelocityFeedbackGains(
m_settings.preset, m_settings.lqr, Kv, Ka,
m_settings.convertGainsToEncTicks
? m_settings.gearing * m_settings.cpr * m_factor
: 1);
m_settings.convertGainsToEncTicks ? m_settings.gearing * m_settings.cpr
: 1);
}
return fb;
}
void AnalysisManager::OverrideUnits(std::string_view unit,
double unitsPerRotation) {
m_unit = unit;
m_factor = unitsPerRotation;
void AnalysisManager::OverrideUnits(std::string_view unit) {
m_data.distanceUnit = unit;
}
void AnalysisManager::ResetUnitsFromJSON() {
m_unit = m_json.at("units").get<std::string>();
m_factor = m_json.at("unitsPerRotation").get<double>();
}
void AnalysisManager::ResetUnitsFromJSON() {}

View File

@@ -7,12 +7,6 @@
using namespace sysid;
AnalysisType sysid::analysis::FromName(std::string_view name) {
if (name == "Drivetrain") {
return sysid::analysis::kDrivetrain;
}
if (name == "Drivetrain (Angular)") {
return sysid::analysis::kDrivetrainAngular;
}
if (name == "Elevator") {
return sysid::analysis::kElevator;
}

View File

@@ -153,32 +153,24 @@ sysid::TrimStepVoltageData(std::vector<PreparedData>* data,
minStepTime = std::min(data->at(0).timestamp - firstTimestamp, minStepTime);
// If step duration hasn't been set yet, calculate a default (find the entry
// before the acceleration first hits zero)
if (settings->stepTestDuration <= minStepTime) {
// Get noise floor
const double accelNoiseFloor = GetNoiseFloor(
*data, kNoiseMeanWindow, [](auto&& pt) { return pt.acceleration; });
// Find latest element with nonzero acceleration
auto endIt = std::find_if(
data->rbegin(), data->rend(), [&](const PreparedData& entry) {
return std::abs(entry.acceleration) > accelNoiseFloor;
});
// Find maximum speed reached
const auto maxSpeed =
GetMaxSpeed(*data, [](auto&& pt) { return pt.velocity; });
// Find place where 90% of maximum speed exceeded
auto endIt =
std::find_if(data->begin(), data->end(), [&](const PreparedData& entry) {
return std::abs(entry.velocity) > maxSpeed * 0.9;
});
if (endIt != data->rend()) {
// Calculate default duration
settings->stepTestDuration = std::min(
endIt->timestamp - data->front().timestamp + minStepTime + 1_s,
maxStepTime);
} else {
settings->stepTestDuration = maxStepTime;
}
if (endIt != data->end()) {
settings->stepTestDuration = std::min(
endIt->timestamp - data->front().timestamp + minStepTime, maxStepTime);
}
// Find first entry greater than the step test duration
auto maxIt =
std::find_if(data->begin(), data->end(), [&](PreparedData entry) {
return entry.timestamp - data->front().timestamp + minStepTime >
return entry.timestamp - data->front().timestamp >
settings->stepTestDuration;
});
@@ -186,6 +178,7 @@ sysid::TrimStepVoltageData(std::vector<PreparedData>* data,
if (maxIt != data->end()) {
data->erase(maxIt, data->end());
}
return std::make_tuple(minStepTime, positionDelay, velocityDelay);
}
@@ -204,6 +197,16 @@ double sysid::GetNoiseFloor(
return std::sqrt(sum / (data.size() - step));
}
double sysid::GetMaxSpeed(
const std::vector<PreparedData>& data,
std::function<double(const PreparedData&)> accessorFunction) {
double max = 0.0;
for (size_t i = 0; i < data.size(); i++) {
max = std::max(max, std::abs(accessorFunction(data[i])));
}
return max;
}
units::second_t sysid::GetMeanTimeDelta(const std::vector<PreparedData>& data) {
std::vector<units::second_t> dts;
@@ -301,7 +304,7 @@ static units::second_t GetMaxStepTime(
auto key = it.first();
auto& dataset = it.getValue();
if (IsRaw(key) && wpi::contains(key, "fast")) {
if (IsRaw(key) && wpi::contains(key, "dynamic")) {
auto duration = dataset.back().timestamp - dataset.front().timestamp;
if (duration > maxStepTime) {
maxStepTime = duration;
@@ -327,7 +330,7 @@ void sysid::InitialTrimAndFilter(
for (auto& it : preparedData) {
auto key = it.first();
auto& dataset = it.getValue();
if (wpi::contains(key, "slow")) {
if (wpi::contains(key, "quasistatic")) {
settings->motionThreshold =
std::min(settings->motionThreshold,
GetNoiseFloor(dataset, kNoiseMeanWindow,
@@ -342,7 +345,7 @@ void sysid::InitialTrimAndFilter(
// Trim quasistatic test data to remove all points where voltage is zero or
// velocity < motion threshold.
if (wpi::contains(key, "slow")) {
if (wpi::contains(key, "quasistatic")) {
dataset.erase(std::remove_if(dataset.begin(), dataset.end(),
[&](const auto& pt) {
return std::abs(pt.voltage) <= 0 ||
@@ -366,7 +369,7 @@ void sysid::InitialTrimAndFilter(
PrepareMechData(&dataset, unit);
// Trims filtered Dynamic Test Data
if (IsFiltered(key) && wpi::contains(key, "fast")) {
if (IsFiltered(key) && wpi::contains(key, "dynamic")) {
// Get the filtered dataset name
auto filteredKey = RemoveStr(key, "raw-");

View File

@@ -1,164 +0,0 @@
// 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 "sysid/analysis/JSONConverter.h"
#include <stdexcept>
#include <string>
#include <fmt/core.h>
#include <fmt/format.h>
#include <wpi/Logger.h>
#include <wpi/MemoryBuffer.h>
#include <wpi/fmt/raw_ostream.h>
#include <wpi/json.h>
#include <wpi/raw_ostream.h>
#include "sysid/Util.h"
#include "sysid/analysis/AnalysisManager.h"
#include "sysid/analysis/AnalysisType.h"
// Sizes of the arrays for new sysid data.
inline constexpr size_t kDrivetrainSize = 9;
inline constexpr size_t kGeneralSize = 4;
// Indices for the old data.
inline constexpr size_t kTimestampCol = 0;
inline constexpr size_t kLVoltsCol = 3;
inline constexpr size_t kRVoltsCol = 4;
inline constexpr size_t kLPosCol = 5;
inline constexpr size_t kRPosCol = 6;
inline constexpr size_t kLVelCol = 7;
inline constexpr size_t kRVelCol = 8;
static wpi::json GetJSON(std::string_view path, wpi::Logger& logger) {
std::error_code ec;
std::unique_ptr<wpi::MemoryBuffer> fileBuffer =
wpi::MemoryBuffer::GetFile(path, ec);
if (fileBuffer == nullptr || ec) {
throw std::runtime_error(fmt::format("Unable to read: {}", path));
}
wpi::json json = wpi::json::parse(fileBuffer->GetCharBuffer());
WPI_INFO(logger, "Read frc-characterization JSON from {}", path);
return json;
}
std::string sysid::ConvertJSON(std::string_view path, wpi::Logger& logger) {
wpi::json ojson = GetJSON(path, logger);
auto type = sysid::analysis::FromName(ojson.at("test").get<std::string>());
auto factor = ojson.at("unitsPerRotation").get<double>();
auto unit = ojson.at("units").get<std::string>();
wpi::json json;
for (auto&& key : AnalysisManager::kJsonDataKeys) {
if (type == analysis::kDrivetrain) {
// Get the old data; create a vector for the new data; reserve the
// appropriate size for the new data.
auto odata = ojson.at(key).get<std::vector<std::array<double, 10>>>();
std::vector<std::array<double, kDrivetrainSize>> data;
data.reserve(odata.size());
// Transfer the data.
for (auto&& pt : odata) {
data.push_back(std::array<double, kDrivetrainSize>{
pt[kTimestampCol], pt[kLVoltsCol], pt[kRVoltsCol], pt[kLPosCol],
pt[kRPosCol], pt[kLVelCol], pt[kRVelCol], 0.0, 0.0});
}
json[key] = data;
} else {
// Get the old data; create a vector for the new data; reserve the
// appropriate size for the new data.
auto odata = ojson.at(key).get<std::vector<std::array<double, 10>>>();
std::vector<std::array<double, kGeneralSize>> data;
data.reserve(odata.size());
// Transfer the data.
for (auto&& pt : odata) {
data.push_back(std::array<double, kGeneralSize>{
pt[kTimestampCol], pt[kLVoltsCol], pt[kLPosCol], pt[kLVelCol]});
}
json[key] = data;
}
}
json["units"] = unit;
json["unitsPerRotation"] = factor;
json["test"] = type.name;
json["sysid"] = true;
// Write the new file with "_new" appended to it.
path.remove_suffix(std::string_view{".json"}.size());
std::string loc = fmt::format("{}_new.json", path);
sysid::SaveFile(json.dump(2), std::filesystem::path{loc});
WPI_INFO(logger, "Wrote new JSON to: {}", loc);
return loc;
}
std::string sysid::ToCSV(std::string_view path, wpi::Logger& logger) {
wpi::json json = GetJSON(path, logger);
auto type = sysid::analysis::FromName(json.at("test").get<std::string>());
auto factor = json.at("unitsPerRotation").get<double>();
auto unit = json.at("units").get<std::string>();
std::string_view abbreviation = GetAbbreviation(unit);
std::error_code ec;
// Naming: {sysid-json-name}(Test, Units).csv
path.remove_suffix(std::string_view{".json"}.size());
std::string loc = fmt::format("{} ({}, {}).csv", path, type.name, unit);
wpi::raw_fd_ostream outputFile{loc, ec};
if (ec) {
throw std::runtime_error("Unable to write to: " + loc);
}
fmt::print(outputFile, "Timestamp (s),Test,");
if (type == analysis::kDrivetrain || type == analysis::kDrivetrainAngular) {
fmt::print(
outputFile,
"Left Volts (V),Right Volts (V),Left Position ({0}),Right "
"Position ({0}),Left Velocity ({0}/s),Right Velocity ({0}/s),Gyro "
"Position (deg),Gyro Rate (deg/s)\n",
abbreviation);
} else {
fmt::print(outputFile, "Volts (V),Position({0}),Velocity ({0}/s)\n",
abbreviation);
}
outputFile << "\n";
for (auto&& key : AnalysisManager::kJsonDataKeys) {
if (type == analysis::kDrivetrain || type == analysis::kDrivetrainAngular) {
auto tempData =
json.at(key).get<std::vector<std::array<double, kDrivetrainSize>>>();
for (auto&& pt : tempData) {
fmt::print(outputFile, "{},{},{},{},{},{},{},{},{},{}\n",
pt[0], // Timestamp
key, // Test
pt[1], pt[2], // Left and Right Voltages
pt[3] * factor, pt[4] * factor, // Left and Right Positions
pt[5] * factor, pt[6] * factor, // Left and Right Velocity
pt[7], pt[8] // Gyro Position and Velocity
);
}
} else {
auto tempData =
json.at(key).get<std::vector<std::array<double, kGeneralSize>>>();
for (auto&& pt : tempData) {
fmt::print(outputFile, "{},{},{},{},{}\n",
pt[0], // Timestamp,
key, // Test
pt[1], // Voltage
pt[2] * factor, // Position
pt[3] * factor // Velocity
);
}
}
}
outputFile.flush();
WPI_INFO(logger, "Wrote CSV to: {}", loc);
return loc;
}

View File

@@ -1,275 +0,0 @@
// 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 "sysid/telemetry/TelemetryManager.h"
#include <algorithm>
#include <cctype> // for ::tolower
#include <numbers>
#include <stdexcept>
#include <string>
#include <utility>
#include <fmt/chrono.h>
#include <networktables/NetworkTableInstance.h>
#include <wpi/Logger.h>
#include <wpi/SmallVector.h>
#include <wpi/StringExtras.h>
#include <wpi/raw_ostream.h>
#include <wpi/timestamp.h>
#include "sysid/Util.h"
#include "sysid/analysis/AnalysisType.h"
using namespace sysid;
TelemetryManager::TelemetryManager(const Settings& settings,
wpi::Logger& logger,
nt::NetworkTableInstance instance)
: m_settings(settings), m_logger(logger), m_inst(instance) {}
void TelemetryManager::BeginTest(std::string_view name) {
// Create a new test params instance for this test.
m_params =
TestParameters{name.starts_with("fast"), name.ends_with("forward"),
m_settings.mechanism == analysis::kDrivetrainAngular,
State::WaitingForEnable};
// Add this test to the list of running tests and set the running flag.
m_tests.push_back(std::string{name});
m_isRunningTest = true;
// Set the Voltage Command Entry
m_voltageCommand.Set((m_params.fast ? m_settings.stepVoltage
: m_settings.quasistaticRampRate) *
(m_params.forward ? 1 : -1));
// Set the test type
m_testType.Set(m_params.fast ? "Dynamic" : "Quasistatic");
// Set the rotate entry
m_rotate.Set(m_params.rotate);
// Set the current mechanism in NT.
m_mechanism.Set(m_settings.mechanism.name);
// Set Overflow to False
m_overflowPub.Set(false);
// Set Mechanism Error to False
m_mechErrorPub.Set(false);
m_inst.Flush();
// Display the warning message.
for (auto&& func : m_callbacks) {
func(
"Please enable the robot in autonomous mode, and then "
"disable it "
"before it runs out of space. \n Note: The robot will "
"continue "
"to move until you disable it - It is your "
"responsibility to "
"ensure it does not hit anything!");
}
WPI_INFO(m_logger, "Started {} test.", m_tests.back());
}
void TelemetryManager::EndTest() {
// If there is no test running, this is a no-op
if (!m_isRunningTest) {
return;
}
// Disable the running flag and store the data in the JSON.
m_isRunningTest = false;
m_data[m_tests.back()] = m_params.data;
// Call the cancellation callbacks.
for (auto&& func : m_callbacks) {
std::string msg;
if (m_params.mechError) {
msg +=
"\nERROR: The robot indicated that you are using the wrong project "
"for characterizing your mechanism. \nThis most likely means you "
"are trying to characterize a mechanism like a Drivetrain with a "
"deployed config for a General Mechanism (e.g. Arm, Flywheel, and "
"Elevator) or vice versa. Please double check your settings and "
"try again.";
} else if (!m_params.data.empty()) {
std::string units = m_settings.units;
std::transform(m_settings.units.begin(), m_settings.units.end(),
units.begin(), ::tolower);
if (std::string_view{m_settings.mechanism.name}.starts_with(
"Drivetrain")) {
double p = (m_params.data.back()[3] - m_params.data.front()[3]) *
m_settings.unitsPerRotation;
double s = (m_params.data.back()[4] - m_params.data.front()[4]) *
m_settings.unitsPerRotation;
double g = m_params.data.back()[7] - m_params.data.front()[7];
msg = fmt::format(
"The left and right encoders traveled {} {} and {} {} "
"respectively.\nThe gyro angle delta was {} degrees.",
p, units, s, units, g * 180.0 / std::numbers::pi);
} else {
double p = (m_params.data.back()[2] - m_params.data.front()[2]) *
m_settings.unitsPerRotation;
msg = fmt::format("The encoder reported traveling {} {}.", p, units);
}
if (m_params.overflow) {
msg +=
"\nNOTE: the robot stopped recording data early because the entry "
"storage was exceeded.";
}
} else {
msg = "No data was detected.";
}
func(msg);
}
// Remove previously run test from list of tests if no data was detected.
if (m_params.data.empty()) {
m_tests.pop_back();
}
// Send a zero command over NT.
m_voltageCommand.Set(0.0);
m_inst.Flush();
}
void TelemetryManager::Update() {
// If there is no test running, these is nothing to update.
if (!m_isRunningTest) {
return;
}
// Update the NT entries that we're reading.
int currAckNumber = m_ackNumberSub.Get();
std::string telemetryValue;
// Get the FMS Control Word.
for (auto tsValue : m_fmsControlData.ReadQueue()) {
uint32_t ctrl = tsValue.value;
m_params.enabled = ctrl & 0x01;
}
// Get the string in the data field.
for (auto tsValue : m_telemetry.ReadQueue()) {
telemetryValue = tsValue.value;
}
// Get the overflow flag
for (auto tsValue : m_overflowSub.ReadQueue()) {
m_params.overflow = tsValue.value;
}
// Get the mechanism error flag
for (auto tsValue : m_mechErrorSub.ReadQueue()) {
m_params.mechError = tsValue.value;
}
// Go through our state machine.
if (m_params.state == State::WaitingForEnable) {
if (m_params.enabled) {
m_params.enableStart = wpi::Now() * 1E-6;
m_params.state = State::RunningTest;
m_ackNumber = currAckNumber;
WPI_INFO(m_logger, "{}", "Transitioned to running test state.");
}
}
if (m_params.state == State::RunningTest) {
// If for some reason we've disconnected, end the test.
if (!m_inst.IsConnected()) {
WPI_WARNING(m_logger, "{}",
"NT connection was dropped when executing the test. The test "
"has been canceled.");
EndTest();
}
// If the robot has disabled, then we can move on to the next step.
if (!m_params.enabled) {
m_params.disableStart = wpi::Now() * 1E-6;
m_params.state = State::WaitingForData;
WPI_INFO(m_logger, "{}", "Transitioned to waiting for data.");
}
}
if (m_params.state == State::WaitingForData) {
double now = wpi::Now() * 1E-6;
m_voltageCommand.Set(0.0);
m_inst.Flush();
// Process valid data
if (!telemetryValue.empty() && m_ackNumber < currAckNumber) {
m_params.raw = std::move(telemetryValue);
m_ackNumber = currAckNumber;
}
// We have the data that we need, so we can parse it and end the test.
if (!m_params.raw.empty() &&
wpi::starts_with(m_params.raw, m_tests.back())) {
// Remove test type from start of string
m_params.raw.erase(0, m_params.raw.find(';') + 1);
// Clean up the string -- remove spaces if there are any.
m_params.raw.erase(
std::remove_if(m_params.raw.begin(), m_params.raw.end(), ::isspace),
m_params.raw.end());
// Split the string into individual components.
wpi::SmallVector<std::string_view, 16> res;
wpi::split(m_params.raw, res, ',');
// Convert each string to double.
std::vector<double> values;
values.reserve(res.size());
for (auto&& str : res) {
values.push_back(wpi::parse_float<double>(str).value());
}
// Add the values to our result vector.
for (size_t i = 0; i < values.size() - m_settings.mechanism.rawDataSize;
i += m_settings.mechanism.rawDataSize) {
std::vector<double> d(m_settings.mechanism.rawDataSize);
std::copy_n(std::make_move_iterator(values.begin() + i),
m_settings.mechanism.rawDataSize, d.begin());
m_params.data.push_back(std::move(d));
}
WPI_INFO(m_logger,
"Received data with size: {} for the {} test in {} seconds.",
m_params.data.size(), m_tests.back(),
m_params.data.back()[0] - m_params.data.front()[0]);
m_ackNumberPub.Set(++m_ackNumber);
EndTest();
}
// If we timed out, end the test and let the user know.
if (now - m_params.disableStart > 5.0) {
WPI_WARNING(m_logger, "{}",
"TelemetryManager did not receieve data 5 seconds after "
"completing the test...");
EndTest();
}
}
}
std::string TelemetryManager::SaveJSON(std::string_view location) {
m_data["test"] = m_settings.mechanism.name;
m_data["units"] = m_settings.units;
m_data["unitsPerRotation"] = m_settings.unitsPerRotation;
m_data["sysid"] = true;
std::string loc = fmt::format("{}/sysid_data{:%Y%m%d-%H%M%S}.json", location,
std::chrono::system_clock::now());
sysid::SaveFile(m_data.dump(2), std::filesystem::path{loc});
WPI_INFO(m_logger, "Wrote JSON to: {}", loc);
return loc;
}

View File

@@ -28,7 +28,7 @@
using namespace sysid;
Analyzer::Analyzer(glass::Storage& storage, wpi::Logger& logger)
: m_location(""), m_logger(logger) {
: m_logger(logger) {
// Fill the StringMap with preset values.
m_presets["Default"] = presets::kDefault;
m_presets["WPILib (2020-)"] = presets::kWPILibNew;
@@ -48,16 +48,14 @@ Analyzer::Analyzer(glass::Storage& storage, wpi::Logger& logger)
void Analyzer::UpdateFeedforwardGains() {
WPI_INFO(m_logger, "{}", "Gain calc");
try {
const auto& [ff, trackWidth] = m_manager->CalculateFeedforward();
const auto& [ff] = m_manager->CalculateFeedforward();
m_ff = ff.coeffs;
m_accelRSquared = ff.rSquared;
m_accelRMSE = ff.rmse;
m_trackWidth = trackWidth;
m_settings.preset.measurementDelay =
m_settings.type == FeedbackControllerLoopType::kPosition
? m_manager->GetPositionDelay()
: m_manager->GetVelocityDelay();
m_conversionFactor = m_manager->GetFactor();
PrepareGraphs();
} catch (const sysid::InvalidDataError& e) {
m_state = AnalyzerState::kGeneralDataError;
@@ -81,6 +79,7 @@ void Analyzer::UpdateFeedforwardGains() {
}
void Analyzer::UpdateFeedbackGains() {
WPI_INFO(m_logger, "{}", "Updating feedback gains");
if (m_ff[1] > 0 && m_ff[2] > 0) {
const auto& fb = m_manager->CalculateFeedback(m_ff);
m_timescale = units::second_t{m_ff[2] / m_ff[1]};
@@ -119,27 +118,9 @@ bool Analyzer::IsDataErrorState() {
m_state == AnalyzerState::kGeneralDataError;
}
void Analyzer::DisplayFileSelector() {
// Get the current width of the window. This will be used to scale
// our UI elements.
float width = ImGui::GetContentRegionAvail().x;
// Show the file location along with an option to choose.
if (ImGui::Button("Select")) {
m_selector = std::make_unique<pfd::open_file>(
"Select Data", "",
std::vector<std::string>{"JSON File", SYSID_PFD_JSON_EXT});
}
ImGui::SameLine();
ImGui::SetNextItemWidth(width - ImGui::CalcTextSize("Select").x -
ImGui::GetFontSize() * 5);
ImGui::InputText("##location", &m_location, ImGuiInputTextFlags_ReadOnly);
}
void Analyzer::ResetData() {
m_plot.ResetData();
m_manager = std::make_unique<AnalysisManager>(m_settings, m_logger);
m_location = "";
m_ff = std::vector<double>{1, 1, 1};
UpdateFeedbackGains();
}
@@ -152,38 +133,15 @@ bool Analyzer::DisplayResetAndUnitOverride() {
ImGui::SameLine(width - ImGui::CalcTextSize("Reset").x);
if (ImGui::Button("Reset")) {
ResetData();
m_state = AnalyzerState::kWaitingForJSON;
m_state = AnalyzerState::kWaitingForData;
return true;
}
if (type == analysis::kDrivetrain) {
ImGui::SetNextItemWidth(ImGui::GetFontSize() * kTextBoxWidthMultiple);
if (ImGui::Combo("Dataset", &m_dataset, kDatasets, 3)) {
m_settings.dataset =
static_cast<AnalysisManager::Settings::DrivetrainDataset>(m_dataset);
PrepareData();
}
ImGui::SameLine();
} else {
m_settings.dataset =
AnalysisManager::Settings::DrivetrainDataset::kCombined;
}
ImGui::Spacing();
ImGui::Text(
"Units: %s\n"
"Units Per Rotation: %.4f\n"
"Type: %s",
std::string(unit).c_str(), m_conversionFactor, type.name);
if (type == analysis::kDrivetrainAngular) {
ImGui::SameLine();
sysid::CreateTooltip(
"Here, the units and units per rotation represent what the wheel "
"positions and velocities were captured in. The track width value "
"will reflect the unit selected here. However, the Kv and Ka will "
"always be in Vs/rad and Vs^2 / rad respectively.");
}
std::string(unit).c_str(), type.name);
if (ImGui::Button("Override Units")) {
ImGui::OpenPopup("Override Units");
@@ -197,24 +155,11 @@ bool Analyzer::DisplayResetAndUnitOverride() {
IM_ARRAYSIZE(kUnits));
unit = kUnits[m_selectedOverrideUnit];
if (unit == "Degrees") {
m_conversionFactor = 360.0;
} else if (unit == "Radians") {
m_conversionFactor = 2 * std::numbers::pi;
} else if (unit == "Rotations") {
m_conversionFactor = 1.0;
}
bool isRotational = m_selectedOverrideUnit > 2;
ImGui::SetNextItemWidth(ImGui::GetFontSize() * 7);
ImGui::InputDouble(
"Units Per Rotation", &m_conversionFactor, 0.0, 0.0, "%.4f",
isRotational ? ImGuiInputTextFlags_ReadOnly : ImGuiInputTextFlags_None);
if (ImGui::Button("Close")) {
ImGui::CloseCurrentPopup();
m_manager->OverrideUnits(unit, m_conversionFactor);
m_manager->OverrideUnits(unit);
PrepareData();
}
@@ -234,22 +179,21 @@ void Analyzer::ConfigParamsOnFileSelect() {
WPI_INFO(m_logger, "{}", "Configuring Params");
m_stepTestDuration = m_settings.stepTestDuration.to<float>();
// Estimate qp as 1/8 * units-per-rot
m_settings.lqr.qp = 0.125 * m_manager->GetFactor();
// Estimate qp as 1/10 native distance unit
m_settings.lqr.qp = 0.1;
// Estimate qv as 1/4 * max velocity = 1/4 * (12V - kS) / kV
m_settings.lqr.qv = 0.25 * (12.0 - m_ff[0]) / m_ff[1];
}
void Analyzer::Display() {
DisplayFileSelector();
DisplayGraphs();
switch (m_state) {
case AnalyzerState::kWaitingForJSON: {
case AnalyzerState::kWaitingForData: {
ImGui::Text(
"SysId is currently in theoretical analysis mode.\n"
"To analyze recorded test data, select a "
"data JSON.");
"data file (.wpilog).");
sysid::CreateTooltip(
"Theoretical feedback gains can be calculated from a "
"physical model of the mechanism being controlled. "
@@ -295,7 +239,7 @@ void Analyzer::Display() {
case AnalyzerState::kFileError: {
CreateErrorPopup(m_errorPopup, m_exception);
if (!m_errorPopup) {
m_state = AnalyzerState::kWaitingForJSON;
m_state = AnalyzerState::kWaitingForData;
return;
}
break;
@@ -313,20 +257,10 @@ void Analyzer::Display() {
break;
}
}
// Periodic functions
try {
SelectFile();
} catch (const AnalysisManager::FileReadingError& e) {
m_state = AnalyzerState::kFileError;
HandleError(e.what());
} catch (const wpi::json::exception& e) {
m_state = AnalyzerState::kFileError;
HandleError(e.what());
}
}
void Analyzer::PrepareData() {
WPI_INFO(m_logger, "{}", "Preparing data");
try {
m_manager->PrepareData();
UpdateFeedforwardGains();
@@ -379,9 +313,6 @@ void Analyzer::PrepareGraphs() {
void Analyzer::HandleError(std::string_view msg) {
m_exception = msg;
m_errorPopup = true;
if (m_state == AnalyzerState::kFileError) {
m_location = "";
}
PrepareRawGraphs();
}
@@ -458,23 +389,12 @@ void Analyzer::DisplayGraphs() {
ImGui::End();
}
void Analyzer::SelectFile() {
// If the selector exists and is ready with a result, we can store it.
if (m_selector && m_selector->ready() && !m_selector->result().empty()) {
// Store the location of the file and reset the selector.
WPI_INFO(m_logger, "Opening File: {}", m_selector->result()[0]);
m_location = m_selector->result()[0];
m_selector.reset();
WPI_INFO(m_logger, "{}", "Opened File");
m_manager =
std::make_unique<AnalysisManager>(m_location, m_settings, m_logger);
PrepareData();
m_dataset = 0;
m_settings.dataset =
AnalysisManager::Settings::DrivetrainDataset::kCombined;
ConfigParamsOnFileSelect();
UpdateFeedbackGains();
}
void Analyzer::AnalyzeData() {
m_manager = std::make_unique<AnalysisManager>(m_data, m_settings, m_logger);
PrepareData();
m_dataset = 0;
ConfigParamsOnFileSelect();
UpdateFeedbackGains();
}
void Analyzer::AbortDataPrep() {
@@ -625,8 +545,6 @@ void Analyzer::DisplayFeedforwardGains(float beginX, float beginY) {
"This is the angle offset which, when added to the angle measurement, "
"zeroes it out when the arm is horizontal. This is needed for the arm "
"feedforward to work.");
} else if (m_trackWidth) {
DisplayGain("Track Width", &*m_trackWidth);
}
double endY = ImGui::GetCursorPosY();
@@ -790,7 +708,7 @@ void Analyzer::DisplayFeedbackGains() {
IM_ARRAYSIZE(kLoopTypes))) {
m_settings.type =
static_cast<FeedbackControllerLoopType>(m_selectedLoopType);
if (m_state == AnalyzerState::kWaitingForJSON) {
if (m_state == AnalyzerState::kWaitingForData) {
m_settings.preset.measurementDelay = 0_ms;
} else {
if (m_settings.type == FeedbackControllerLoopType::kPosition) {
@@ -817,7 +735,7 @@ void Analyzer::DisplayFeedbackGains() {
if (m_selectedLoopType == 0) {
std::string unit;
if (m_state != AnalyzerState::kWaitingForJSON) {
if (m_state != AnalyzerState::kWaitingForData) {
unit = fmt::format(" ({})", GetAbbreviation(m_manager->GetUnit()));
}
@@ -831,7 +749,7 @@ void Analyzer::DisplayFeedbackGains() {
}
std::string unit;
if (m_state != AnalyzerState::kWaitingForJSON) {
if (m_state != AnalyzerState::kWaitingForData) {
unit = fmt::format(" ({}/s)", GetAbbreviation(m_manager->GetUnit()));
}

View File

@@ -0,0 +1,244 @@
// 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 "sysid/view/DataSelector.h"
#include <fmt/format.h>
#include <glass/support/DataLogReaderThread.h>
#include <imgui.h>
#include <wpi/DataLogReader.h>
#include <wpi/Logger.h>
#include <wpi/StringExtras.h>
#include "sysid/Util.h"
#include "sysid/analysis/AnalysisType.h"
#include "sysid/analysis/Storage.h"
using namespace sysid;
static constexpr const char* kAnalysisTypes[] = {"Elevator", "Arm", "Simple"};
static bool EmitEntryTarget(const char* name, bool isString,
const glass::DataLogReaderEntry** entry) {
if (*entry) {
auto text =
fmt::format("{}: {} ({})", name, (*entry)->name, (*entry)->type);
ImGui::TextUnformatted(text.c_str());
} else {
ImGui::Text("%s: <none (DROP HERE)> (%s)", name,
isString ? "string" : "number");
}
bool rv = false;
if (ImGui::BeginDragDropTarget()) {
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload(
isString ? "DataLogEntryString" : "DataLogEntry")) {
assert(payload->DataSize == sizeof(const glass::DataLogReaderEntry*));
*entry = *static_cast<const glass::DataLogReaderEntry**>(payload->Data);
rv = true;
}
ImGui::EndDragDropTarget();
}
return rv;
}
void DataSelector::Display() {
using namespace std::chrono_literals;
// building test data is modal (due to async access)
if (m_testdataFuture.valid()) {
if (m_testdataFuture.wait_for(0s) == std::future_status::ready) {
TestData data = m_testdataFuture.get();
for (auto&& motordata : data.motorData) {
m_testdataStats.emplace_back(
fmt::format("Test State: {}", motordata.first()));
int i = 0;
for (auto&& run : motordata.second.runs) {
m_testdataStats.emplace_back(fmt::format(
" Run {} samples: {} Volt {} Pos {} Vel", ++i,
run.voltage.size(), run.position.size(), run.velocity.size()));
}
}
if (testdata) {
testdata(std::move(data));
}
}
ImGui::Text("Loading data...");
return;
}
if (!m_testdataStats.empty()) {
for (auto&& line : m_testdataStats) {
ImGui::TextUnformatted(line.c_str());
}
if (ImGui::Button("Ok")) {
m_testdataStats.clear();
}
return;
}
if (EmitEntryTarget("Test State", true, &m_testStateEntry)) {
m_testsFuture =
std::async(std::launch::async, [testStateEntry = m_testStateEntry] {
return LoadTests(*testStateEntry);
});
}
if (!m_testStateEntry) {
return;
}
if (m_testsFuture.valid() &&
m_testsFuture.wait_for(0s) == std::future_status::ready) {
m_tests = m_testsFuture.get();
}
if (m_tests.empty()) {
if (m_testsFuture.valid()) {
ImGui::TextUnformatted("Reading tests...");
} else {
ImGui::TextUnformatted("No tests found");
}
return;
}
#if 0
// Test filtering
if (ImGui::BeginCombo("Test", m_selectedTest.c_str())) {
for (auto&& test : m_tests) {
if (ImGui::Selectable(test.first.c_str(), test.first == m_selectedTest)) {
m_selectedTest = test.first;
}
}
ImGui::EndCombo();
}
#endif
ImGui::Combo("Analysis Type", &m_selectedAnalysis, kAnalysisTypes,
IM_ARRAYSIZE(kAnalysisTypes));
// DND targets
EmitEntryTarget("Velocity", false, &m_velocityEntry);
EmitEntryTarget("Position", false, &m_positionEntry);
EmitEntryTarget("Voltage", false, &m_voltageEntry);
ImGui::SetNextItemWidth(ImGui::GetFontSize() * 7);
ImGui::Combo("Units", &m_selectedUnit, kUnits, IM_ARRAYSIZE(kUnits));
ImGui::InputDouble("Velocity scaling", &m_velocityScale);
ImGui::InputDouble("Position scaling", &m_positionScale);
if (/*!m_selectedTest.empty() &&*/ m_velocityEntry && m_positionEntry &&
m_voltageEntry) {
if (ImGui::Button("Load")) {
m_testdataFuture =
std::async(std::launch::async, [this] { return BuildTestData(); });
}
}
}
void DataSelector::Reset() {
m_testsFuture = {};
m_tests.clear();
m_selectedTest.clear();
m_testStateEntry = nullptr;
m_velocityEntry = nullptr;
m_positionEntry = nullptr;
m_voltageEntry = nullptr;
m_testdataFuture = {};
}
DataSelector::Tests DataSelector::LoadTests(
const glass::DataLogReaderEntry& testStateEntry) {
Tests tests;
for (auto&& range : testStateEntry.ranges) {
std::string_view prevState;
Runs* curRuns = nullptr;
wpi::log::DataLogReader::iterator lastStart = range.begin();
for (auto it = range.begin(), end = range.end(); it != end; ++it) {
std::string_view testState;
if (it->GetEntry() != testStateEntry.entry ||
!it->GetString(&testState)) {
continue;
}
// track runs as iterator ranges of the same test
if (testState != prevState) {
if (curRuns) {
curRuns->emplace_back(lastStart, it);
}
lastStart = it;
}
prevState = testState;
if (testState == "none") {
curRuns = nullptr;
continue;
}
auto [testName, direction] = wpi::rsplit(testState, '-');
auto testIt = tests.find(testName);
if (testIt == tests.end()) {
testIt = tests.emplace(std::string{testName}, State{}).first;
}
auto stateIt = testIt->second.find(testState);
if (stateIt == testIt->second.end()) {
stateIt = testIt->second.emplace(std::string{testState}, Runs{}).first;
}
curRuns = &stateIt->second;
}
if (curRuns) {
curRuns->emplace_back(lastStart, range.end());
}
}
return tests;
}
template <typename T>
static void AddSample(std::vector<MotorData::Run::Sample<T>>& samples,
const wpi::log::DataLogRecord& record, bool isDouble,
double scale) {
if (isDouble) {
double val;
if (record.GetDouble(&val)) {
samples.emplace_back(units::second_t{record.GetTimestamp() * 1.0e-6},
T{val * scale});
}
} else {
float val;
if (record.GetFloat(&val)) {
samples.emplace_back(units::second_t{record.GetTimestamp() * 1.0e-6},
T{static_cast<double>(val * scale)});
}
}
}
TestData DataSelector::BuildTestData() {
TestData data;
data.distanceUnit = kUnits[m_selectedUnit];
data.mechanismType = analysis::FromName(kAnalysisTypes[m_selectedAnalysis]);
bool voltageDouble = m_voltageEntry->type == "double";
bool positionDouble = m_positionEntry->type == "double";
bool velocityDouble = m_velocityEntry->type == "double";
for (auto&& test : m_tests) {
for (auto&& state : test.second) {
auto& motorData = data.motorData[state.first];
for (auto&& range : state.second) {
auto& run = motorData.runs.emplace_back();
for (auto&& record : range) {
if (record.GetEntry() == m_voltageEntry->entry) {
AddSample(run.voltage, record, voltageDouble, 1.0);
} else if (record.GetEntry() == m_positionEntry->entry) {
AddSample(run.position, record, positionDouble, m_positionScale);
} else if (record.GetEntry() == m_velocityEntry->entry) {
AddSample(run.velocity, record, velocityDouble, m_velocityScale);
}
}
}
}
}
return data;
}

View File

@@ -1,64 +0,0 @@
// 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 "sysid/analysis/JSONConverter.h"
#include "sysid/view/JSONConverter.h"
#include <exception>
#include <imgui.h>
#include <portable-file-dialogs.h>
#include <wpi/timestamp.h>
#include "sysid/Util.h"
using namespace sysid;
void JSONConverter::DisplayConverter(
const char* tooltip,
std::function<std::string(std::string_view, wpi::Logger&)> converter) {
if (ImGui::Button(tooltip)) {
m_opener = std::make_unique<pfd::open_file>(
tooltip, "", std::vector<std::string>{"JSON File", SYSID_PFD_JSON_EXT});
}
if (m_opener && m_opener->ready()) {
if (!m_opener->result().empty()) {
m_location = m_opener->result()[0];
try {
converter(m_location, m_logger);
m_timestamp = wpi::Now() * 1E-6;
} catch (const std::exception& e) {
ImGui::OpenPopup("Exception Caught!");
m_exception = e.what();
}
}
m_opener.reset();
}
if (wpi::Now() * 1E-6 - m_timestamp < 5) {
ImGui::SameLine();
ImGui::Text("Saved!");
}
// Handle exceptions.
ImGui::SetNextWindowSize(ImVec2(480.f, 0.0f));
if (ImGui::BeginPopupModal("Exception Caught!")) {
ImGui::PushTextWrapPos(0.0f);
ImGui::Text(
"An error occurred when parsing the JSON. This most likely means that "
"the JSON data is incorrectly formatted.");
ImGui::TextColored(ImVec4(1.0f, 0.4f, 0.4f, 1.0f), "%s",
m_exception.c_str());
ImGui::PopTextWrapPos();
if (ImGui::Button("Close")) {
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
}
}
void JSONConverter::DisplayCSVConvert() {
DisplayConverter("Select SysId JSON", sysid::ToCSV);
}

View File

@@ -0,0 +1,208 @@
// 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 "sysid/view/LogLoader.h"
#include <algorithm>
#include <memory>
#include <span>
#include <string_view>
#include <glass/support/DataLogReaderThread.h>
#include <imgui.h>
#include <imgui_stdlib.h>
#include <portable-file-dialogs.h>
#include <wpi/SpanExtras.h>
#include <wpi/StringExtras.h>
#include <wpi/fs.h>
using namespace sysid;
LogLoader::LogLoader(glass::Storage& storage, wpi::Logger& logger) {}
LogLoader::~LogLoader() = default;
void LogLoader::Display() {
if (ImGui::Button("Open data log file...")) {
m_opener = std::make_unique<pfd::open_file>(
"Select Data Log", "",
std::vector<std::string>{"DataLog Files", "*.wpilog"});
}
// Handle opening the file
if (m_opener && m_opener->ready(0)) {
if (!m_opener->result().empty()) {
m_filename = m_opener->result()[0];
std::error_code ec;
auto buf = wpi::MemoryBuffer::GetFile(m_filename, ec);
if (ec) {
ImGui::OpenPopup("Error");
m_error = fmt::format("Could not open file: {}", ec.message());
return;
}
wpi::log::DataLogReader reader{std::move(buf)};
if (!reader.IsValid()) {
ImGui::OpenPopup("Error");
m_error = "Not a valid datalog file";
return;
}
unload();
m_reader =
std::make_unique<glass::DataLogReaderThread>(std::move(reader));
m_entryTree.clear();
}
m_opener.reset();
}
// Handle errors
ImGui::SetNextWindowSize(ImVec2(480.f, 0.0f));
if (ImGui::BeginPopupModal("Error")) {
ImGui::PushTextWrapPos(0.0f);
ImGui::TextUnformatted(m_error.c_str());
ImGui::PopTextWrapPos();
if (ImGui::Button("Close")) {
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
}
if (!m_reader) {
return;
}
// Summary info
ImGui::TextUnformatted(fs::path{m_filename}.stem().string().c_str());
ImGui::Text("%u records, %u entries%s", m_reader->GetNumRecords(),
m_reader->GetNumEntries(),
m_reader->IsDone() ? "" : " (working)");
if (!m_reader->IsDone()) {
return;
}
bool refilter = ImGui::InputText("Filter", &m_filter);
// Display tree of entries
if (m_entryTree.empty() || refilter) {
RebuildEntryTree();
}
ImGui::BeginTable(
"Entries", 2,
ImGuiTableFlags_Borders | ImGuiTableFlags_SizingStretchProp);
ImGui::TableSetupColumn("Name");
ImGui::TableSetupColumn("Type");
// ImGui::TableSetupColumn("Metadata");
ImGui::TableHeadersRow();
DisplayEntryTree(m_entryTree);
ImGui::EndTable();
}
void LogLoader::RebuildEntryTree() {
m_entryTree.clear();
wpi::SmallVector<std::string_view, 16> parts;
m_reader->ForEachEntryName([&](const glass::DataLogReaderEntry& entry) {
// only show double/float/string entries (TODO: support struct/protobuf)
if (entry.type != "double" && entry.type != "float" &&
entry.type != "string") {
return;
}
// filter on name
if (!m_filter.empty() && !wpi::contains_lower(entry.name, m_filter)) {
return;
}
parts.clear();
// split on first : if one is present
auto [prefix, mainpart] = wpi::split(entry.name, ':');
if (mainpart.empty() || wpi::contains(prefix, '/')) {
mainpart = entry.name;
} else {
parts.emplace_back(prefix);
}
wpi::split(mainpart, parts, '/', -1, false);
// ignore a raw "/" key
if (parts.empty()) {
return;
}
// get to leaf
auto nodes = &m_entryTree;
for (auto part : wpi::drop_back(std::span{parts.begin(), parts.end()})) {
auto it =
std::find_if(nodes->begin(), nodes->end(),
[&](const auto& node) { return node.name == part; });
if (it == nodes->end()) {
nodes->emplace_back(part);
// path is from the beginning of the string to the end of the current
// part; this works because part is a reference to the internals of
// entry.name
nodes->back().path.assign(
entry.name.data(), part.data() + part.size() - entry.name.data());
it = nodes->end() - 1;
}
nodes = &it->children;
}
auto it = std::find_if(nodes->begin(), nodes->end(), [&](const auto& node) {
return node.name == parts.back();
});
if (it == nodes->end()) {
nodes->emplace_back(parts.back());
// no need to set path, as it's identical to entry.name
it = nodes->end() - 1;
}
it->entry = &entry;
});
}
static void EmitEntry(const std::string& name,
const glass::DataLogReaderEntry& entry) {
ImGui::TableNextColumn();
ImGui::Selectable(name.c_str());
if (ImGui::BeginDragDropSource()) {
auto entryPtr = &entry;
ImGui::SetDragDropPayload(
entry.type == "string" ? "DataLogEntryString" : "DataLogEntry",
&entryPtr,
sizeof(entryPtr)); // NOLINT
ImGui::TextUnformatted(entry.name.data(),
entry.name.data() + entry.name.size());
ImGui::EndDragDropSource();
}
ImGui::TableNextColumn();
ImGui::TextUnformatted(entry.type.data(),
entry.type.data() + entry.type.size());
#if 0
ImGui::TableNextColumn();
ImGui::TextUnformatted(entry.metadata.data(),
entry.metadata.data() + entry.metadata.size());
#endif
}
void LogLoader::DisplayEntryTree(const std::vector<EntryTreeNode>& tree) {
for (auto&& node : tree) {
if (node.entry) {
EmitEntry(node.name, *node.entry);
}
if (!node.children.empty()) {
ImGui::TableNextColumn();
bool open = ImGui::TreeNodeEx(node.name.c_str(),
ImGuiTreeNodeFlags_SpanFullWidth);
ImGui::TableNextColumn();
#if 0
ImGui::TableNextColumn();
#endif
if (open) {
DisplayEntryTree(node.children);
ImGui::TreePop();
}
}
}
}

View File

@@ -1,222 +0,0 @@
// 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 "sysid/view/Logger.h"
#include <exception>
#include <numbers>
#include <glass/Context.h>
#include <glass/Storage.h>
#include <imgui.h>
#include <imgui_internal.h>
#include <imgui_stdlib.h>
#include <networktables/NetworkTable.h>
#include <units/angle.h>
#include <wpigui.h>
#include "sysid/Util.h"
#include "sysid/analysis/AnalysisType.h"
#include "sysid/view/UILayout.h"
using namespace sysid;
Logger::Logger(glass::Storage& storage, wpi::Logger& logger)
: m_logger{logger}, m_ntSettings{"sysid", storage} {
wpi::gui::AddEarlyExecute([&] { m_ntSettings.Update(); });
m_ntSettings.EnableServerOption(false);
}
void Logger::Display() {
// Get the current width of the window. This will be used to scale
// our UI elements.
float width = ImGui::GetContentRegionAvail().x;
// Add team number input and apply button for NT connection.
m_ntSettings.Display();
// Reset and clear the internal manager state.
ImGui::SameLine();
if (ImGui::Button("Reset Telemetry")) {
m_settings = TelemetryManager::Settings{};
m_manager = std::make_unique<TelemetryManager>(m_settings, m_logger);
m_settings.mechanism = analysis::FromName(kTypes[m_selectedType]);
}
// Add NT connection indicator.
static ImVec4 kColorDisconnected{1.0f, 0.4f, 0.4f, 1.0f};
static ImVec4 kColorConnected{0.2f, 1.0f, 0.2f, 1.0f};
ImGui::SameLine();
bool ntConnected = nt::NetworkTableInstance::GetDefault().IsConnected();
ImGui::TextColored(ntConnected ? kColorConnected : kColorDisconnected,
ntConnected ? "NT Connected" : "NT Disconnected");
// Create a Section for project configuration
ImGui::Separator();
ImGui::Spacing();
ImGui::Text("Project Parameters");
// Add a dropdown for mechanism type.
ImGui::SetNextItemWidth(ImGui::GetFontSize() * kTextBoxWidthMultiple);
if (ImGui::Combo("Mechanism", &m_selectedType, kTypes,
IM_ARRAYSIZE(kTypes))) {
m_settings.mechanism = analysis::FromName(kTypes[m_selectedType]);
}
// Add Dropdown for Units
ImGui::SetNextItemWidth(ImGui::GetFontSize() * kTextBoxWidthMultiple);
if (ImGui::Combo("Unit Type", &m_selectedUnit, kUnits,
IM_ARRAYSIZE(kUnits))) {
m_settings.units = kUnits[m_selectedUnit];
}
sysid::CreateTooltip(
"This is the type of units that your gains will be in. For example, if "
"you want your flywheel gains in terms of radians, then use the radians "
"unit. On the other hand, if your drivetrain will use gains in meters, "
"choose meters.");
// Rotational units have fixed Units per rotations
m_isRotationalUnits =
(m_settings.units == "Rotations" || m_settings.units == "Degrees" ||
m_settings.units == "Radians");
if (m_settings.units == "Degrees") {
m_settings.unitsPerRotation = 360.0;
} else if (m_settings.units == "Radians") {
m_settings.unitsPerRotation = 2 * std::numbers::pi;
} else if (m_settings.units == "Rotations") {
m_settings.unitsPerRotation = 1.0;
}
// Units Per Rotations entry
ImGui::SetNextItemWidth(ImGui::GetFontSize() * kTextBoxWidthMultiple);
ImGui::InputDouble("Units Per Rotation", &m_settings.unitsPerRotation, 0.0f,
0.0f, "%.4f",
m_isRotationalUnits ? ImGuiInputTextFlags_ReadOnly
: ImGuiInputTextFlags_None);
sysid::CreateTooltip(
"The logger assumes that the code will be sending recorded motor shaft "
"rotations over NetworkTables. This value will then be multiplied by the "
"units per rotation to get the measurement in the units you "
"specified.\n\nFor non-rotational units (e.g. meters), this value is "
"usually the wheel diameter times pi (should not include gearing).");
// Create a section for voltage parameters.
ImGui::Separator();
ImGui::Spacing();
ImGui::Text("Voltage Parameters");
auto CreateVoltageParameters = [this](const char* text, double* data,
float min, float max) {
ImGui::SetNextItemWidth(ImGui::GetFontSize() * 6);
ImGui::PushItemFlag(ImGuiItemFlags_Disabled,
m_manager && m_manager->IsActive());
float value = static_cast<float>(*data);
if (ImGui::SliderFloat(text, &value, min, max, "%.2f")) {
*data = value;
}
ImGui::PopItemFlag();
};
CreateVoltageParameters("Quasistatic Ramp Rate (V/s)",
&m_settings.quasistaticRampRate, 0.10f, 0.60f);
sysid::CreateTooltip(
"This is the rate at which the voltage will increase during the "
"quasistatic test.");
CreateVoltageParameters("Dynamic Step Voltage (V)", &m_settings.stepVoltage,
0.0f, 10.0f);
sysid::CreateTooltip(
"This is the voltage that will be applied for the "
"dynamic voltage (acceleration) tests.");
// Create a section for tests.
ImGui::Separator();
ImGui::Spacing();
ImGui::Text("Tests");
auto CreateTest = [this, width](const char* text, const char* itext) {
// Display buttons if we have an NT connection.
if (nt::NetworkTableInstance::GetDefault().IsConnected()) {
// Create button to run tests.
if (ImGui::Button(text)) {
// Open the warning message.
ImGui::OpenPopup("Warning");
m_manager->BeginTest(itext);
m_opened = text;
}
if (m_opened == text && ImGui::BeginPopupModal("Warning")) {
ImGui::TextWrapped("%s", m_popupText.c_str());
if (ImGui::Button(m_manager->IsActive() ? "End Test" : "Close")) {
m_manager->EndTest();
ImGui::CloseCurrentPopup();
m_opened = "";
}
ImGui::EndPopup();
}
} else {
// Show disabled text when there is no connection.
ImGui::TextDisabled("%s", text);
}
// Show whether the tests were run or not.
bool run = m_manager->HasRunTest(itext);
ImGui::SameLine(width * 0.7);
ImGui::Text(run ? "Run" : "Not Run");
};
CreateTest("Quasistatic Forward", "slow-forward");
CreateTest("Quasistatic Backward", "slow-backward");
CreateTest("Dynamic Forward", "fast-forward");
CreateTest("Dynamic Backward", "fast-backward");
m_manager->RegisterDisplayCallback(
[this](const auto& str) { m_popupText = str; });
// Display the path to where the JSON will be saved and a button to select the
// location.
ImGui::Separator();
ImGui::Spacing();
ImGui::Text("Save Location");
if (ImGui::Button("Choose")) {
m_selector = std::make_unique<pfd::select_folder>("Select Folder");
}
ImGui::SameLine();
ImGui::InputText("##savelocation", &m_jsonLocation,
ImGuiInputTextFlags_ReadOnly);
// Add button to save.
ImGui::SameLine(width * 0.9);
if (ImGui::Button("Save")) {
try {
m_manager->SaveJSON(m_jsonLocation);
} catch (const std::exception& e) {
ImGui::OpenPopup("Exception Caught!");
m_exception = e.what();
}
}
// Handle exceptions.
if (ImGui::BeginPopupModal("Exception Caught!")) {
ImGui::Text("%s", m_exception.c_str());
if (ImGui::Button("Close")) {
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
}
// Run periodic methods.
SelectDataFolder();
m_ntSettings.Update();
m_manager->Update();
}
void Logger::SelectDataFolder() {
// If the selector exists and is ready with a result, we can store it.
if (m_selector && m_selector->ready()) {
m_jsonLocation = m_selector->result();
m_selector.reset();
}
}