[wpilibc] Clean up C++ SysId routine (#6160)

This commit is contained in:
Tyler Veness
2024-01-05 15:22:52 -08:00
committed by GitHub
parent 4595f84719
commit 6bed82a18e
3 changed files with 69 additions and 57 deletions

View File

@@ -4,40 +4,43 @@
#include "frc/sysid/SysIdRoutineLog.h"
#include <fmt/format.h>
#include "frc/DataLogManager.h"
using namespace frc::sysid;
SysIdRoutineLog::SysIdRoutineLog(const std::string& logName)
SysIdRoutineLog::SysIdRoutineLog(std::string_view logName)
: m_logName(logName),
m_state(wpi::log::StringLogEntry{frc::DataLogManager::GetLog(),
"sysid-test-state" + logName}) {
m_state(wpi::log::StringLogEntry{
frc::DataLogManager::GetLog(),
fmt::format("sysid-test-state{}", logName)}) {
m_state.Append(StateEnumToString(State::kNone));
}
SysIdRoutineLog::MotorLog::MotorLog(const std::string& motorName,
const std::string& logName,
SysIdRoutineLog::MotorLog::MotorLog(std::string_view motorName,
std::string_view logName,
LogEntries* logEntries)
: m_motorName(motorName), m_logName(logName), m_logEntries(logEntries) {
(*logEntries)[motorName] = MotorEntries();
}
SysIdRoutineLog::MotorLog& SysIdRoutineLog::MotorLog::value(
const std::string& name, double value, const std::string& unit) {
std::string_view name, double value, std::string_view unit) {
auto& motorEntries = (*m_logEntries)[m_motorName];
if (!motorEntries.contains(name)) {
wpi::log::DataLog& log = frc::DataLogManager::GetLog();
motorEntries[name] = wpi::log::DoubleLogEntry(
log, name + "-" + m_motorName + "-" + m_logName, unit);
log, fmt::format("{}-{}-{}", name, m_motorName, m_logName), unit);
}
motorEntries[name].Append(value);
return *this;
}
SysIdRoutineLog::MotorLog SysIdRoutineLog::Motor(const std::string& motorName) {
SysIdRoutineLog::MotorLog SysIdRoutineLog::Motor(std::string_view motorName) {
return MotorLog{motorName, m_logName, &m_logEntries};
}

View File

@@ -5,6 +5,7 @@
#pragma once
#include <string>
#include <string_view>
#include <unordered_map>
#include <units/acceleration.h>
@@ -40,11 +41,10 @@ class SysIdRoutineLog {
public:
class MotorLog {
public:
MotorLog(const std::string& motorName, const std::string& logName,
MotorLog(std::string_view motorName, std::string_view logName,
LogEntries* logEntries);
MotorLog& value(const std::string& name, double value,
const std::string& unit);
MotorLog& value(std::string_view name, double value, std::string_view unit);
MotorLog& voltage(units::volt_t voltage) {
return value("voltage", voltage.value(), voltage.name());
@@ -83,15 +83,17 @@ class SysIdRoutineLog {
std::string m_logName;
LogEntries* m_logEntries;
};
/**
* Create a new logging utility for a SysId test routine.
*
* @param logName The name for the test routine in the log. Should be unique
* between complete test routines (quasistatic and dynamic, forward and
* reverse). The current state of this test (e.g. "quasistatic-forward") will
* appear in WPILog under the "sysid-test-state-logName" entry.
* between complete test routines (quasistatic and dynamic, forward and
* reverse). The current state of this test (e.g. "quasistatic-forward")
* will appear in WPILog under the "sysid-test-state-logName" entry.
*/
explicit SysIdRoutineLog(const std::string& logName);
explicit SysIdRoutineLog(std::string_view logName);
/**
* Records the current state of the SysId test routine. Should be called once
* per iteration during tests with the type of the current test, and once upon
@@ -101,13 +103,13 @@ class SysIdRoutineLog {
*/
void RecordState(State state);
MotorLog Motor(const std::string& motorName);
MotorLog Motor(std::string_view motorName);
static std::string StateEnumToString(State state);
private:
LogEntries m_logEntries{};
std::string m_logName{};
wpi::log::StringLogEntry m_state{};
LogEntries m_logEntries;
std::string m_logName;
wpi::log::StringLogEntry m_state;
};
} // namespace frc::sysid