Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow loop epochs to be logged to DataLog and published to NT #5375

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
f06b645
Loop epochs can be published to NT or logged to DataLog
Gold856 May 29, 2023
65e64f0
Manual format because Actions is broken
Gold856 Jun 2, 2023
5618114
Always have user specify NT topic and
Gold856 Jun 3, 2023
39f3110
Add epoch logging to CommandScheduler
Gold856 Jun 3, 2023
d39757d
Add half baked C++ code
Gold856 Jun 5, 2023
4306085
Remove default DataLog key, fix overloaded methods
Gold856 Jun 5, 2023
041ac28
Store NT instance instead of retrieving it every call
Gold856 Jun 5, 2023
0ed4134
Cache publishers and entry indices
Gold856 Jun 6, 2023
790656b
Remove old methods from CommandScheduler
Gold856 Jun 6, 2023
e39fc56
oops
Gold856 Jun 6, 2023
fde2c12
Add documentation for logging methods
Gold856 Jun 6, 2023
ef34b7b
Use references to pass the DataLog
Gold856 Jun 6, 2023
5d8e715
Format
Gold856 Jun 6, 2023
5bee441
Document and format
Gold856 Jun 9, 2023
70053b3
use shared_ptr for publisher cache
rzblue Jun 9, 2023
85759f9
Formatting fixes
github-actions[bot] Jun 9, 2023
25eba5f
Assign m_publishNT to false by default
Gold856 Jun 9, 2023
a07ab62
Rename methods to include LoopTimings in the name
Gold856 Jun 20, 2023
adcc011
Fix rebase error
Gold856 Aug 4, 2023
172a449
Remove trailing commas
Gold856 Sep 17, 2023
f9e4b56
Fix compilation
Gold856 Dec 2, 2023
9aa7feb
Run wpiformat
calcmogul Feb 28, 2024
6e848a8
Only allow epoch logging to be called once
Gold856 Mar 3, 2024
3666c5a
Close publisher
Gold856 Mar 3, 2024
7faa51d
Address review
Gold856 Mar 3, 2024
43809e5
Fixes
Gold856 Mar 3, 2024
ffce17a
Address review
Gold856 Mar 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import edu.wpi.first.hal.FRCNetComm.tInstances;
import edu.wpi.first.hal.FRCNetComm.tResourceType;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.util.datalog.DataLog;
import edu.wpi.first.util.sendable.Sendable;
import edu.wpi.first.util.sendable.SendableBuilder;
import edu.wpi.first.util.sendable.SendableRegistry;
Expand Down Expand Up @@ -124,6 +125,31 @@ public void close() {
LiveWindow.setDisabledListener(null);
}

/**
* Starts publishing loop timings to NetworkTables. Subsequent calls will do nothing.
*
* <p>This will publish how long it takes for methods in the command-based framework to execute;
* periodic methods in subsystems, execute methods in commands, etc.
*
* @param topicName The NetworkTables topic to publish to
*/
public void publishLoopTimingsToNetworkTables(String topicName) {
m_watchdog.publishToNetworkTables(topicName);
}

/**
* Starts logging loop timings to the data log. Subsequent calls will do nothing.
*
* <p>This will log how long it takes for methods in the command-based framework to execute;
* periodic methods in subsystems, execute methods in commands, etc.
*
* @param dataLog The data log to log loop timings to
* @param entry The name of the entry to log to
*/
public void startLoopTimingsDataLog(DataLog dataLog, String entry) {
m_watchdog.startDataLog(dataLog, entry);
}

/**
* Get the default button poll.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ void CommandScheduler::SetPeriod(units::second_t period) {
m_watchdog.SetTimeout(period);
}

void CommandScheduler::PublishLoopTimingsToNetworkTables(
std::string_view topicName) {
m_watchdog.PublishToNetworkTables(topicName);
}

void CommandScheduler::StartLoopTimingsDataLog(wpi::log::DataLog& dataLog,
std::string_view entry) {
m_watchdog.StartDataLog(dataLog, entry);
}

frc::EventLoop* CommandScheduler::GetActiveButtonLoop() const {
return m_impl->activeButtonLoop;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <frc/Watchdog.h>
#include <frc/event/EventLoop.h>
#include <units/time.h>
#include <wpi/DataLog.h>
#include <wpi/FunctionExtras.h>
#include <wpi/sendable/Sendable.h>
#include <wpi/sendable/SendableHelper.h>
Expand Down Expand Up @@ -58,6 +59,30 @@ class CommandScheduler final : public wpi::Sendable,
*/
void SetPeriod(units::second_t period);

/**
* Starts publishing loop timings to NetworkTables. Subsequent calls will do
* nothing.
*
* This will publish how long it takes for methods in the command-based
* framework to execute; periodic methods in subsystems, execute methods in
* commands, etc.
* @param topicName The NetworkTables topic to publish to
*/
void PublishLoopTimingsToNetworkTables(std::string_view topicName);

/**
* Starts logging loop timings to the data log. Subsequent calls will do
* nothing.
*
* This will log how long it takes for methods in the command-based
* framework to execute; periodic methods in subsystems, execute methods in
* commands, etc.
* @param dataLog The data log to log epochs to
* @param entry The name of the entry to log to
*/
void StartLoopTimingsDataLog(wpi::log::DataLog& dataLog,
std::string_view entry);

/**
* Get the active button poll.
*
Expand Down
10 changes: 10 additions & 0 deletions wpilibc/src/main/native/cpp/IterativeRobotBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ void IterativeRobotBase::TeleopExit() {}

void IterativeRobotBase::TestExit() {}

void IterativeRobotBase::PublishLoopTimingsToNetworkTables(
std::string_view topicName) {
m_watchdog.PublishToNetworkTables(topicName);
}

void IterativeRobotBase::StartLoopTimingsDataLog(wpi::log::DataLog& dataLog,
std::string_view entry) {
m_watchdog.StartDataLog(dataLog, entry);
}

void IterativeRobotBase::SetNetworkTablesFlushEnabled(bool enabled) {
m_ntFlushEnabled = enabled;
}
Expand Down
47 changes: 46 additions & 1 deletion wpilibc/src/main/native/cpp/Tracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ Tracer::Tracer() {
ResetTimer();
}

void Tracer::PublishToNetworkTables(std::string_view topicName) {
if (m_publishNT) {
return;
}
m_publishNT = true;
m_inst = nt::NetworkTableInstance::GetDefault();
m_ntTopic = topicName;
m_publisherCache = wpi::StringMap<std::shared_ptr<nt::IntegerPublisher>>(10);
}

void Tracer::StartDataLog(wpi::log::DataLog& dataLog, std::string_view entry) {
if (m_dataLogEnabled) {
return;
}
m_dataLogEnabled = true;
m_dataLog = &dataLog;
m_dataLogEntry = entry;
m_entryCache = wpi::StringMap<int>(10);
}

void Tracer::ResetTimer() {
m_startTime = hal::fpga_clock::now();
}
Expand All @@ -27,7 +47,32 @@ void Tracer::ClearEpochs() {

void Tracer::AddEpoch(std::string_view epochName) {
auto currentTime = hal::fpga_clock::now();
m_epochs[epochName] = currentTime - m_startTime;
auto epoch = currentTime - m_startTime;
m_epochs[epochName] = epoch;
if (m_publishNT) {
// Topics are cached with the epoch name as the key, and the publisher
// object as the value
if (m_publisherCache.count(epochName) == 0) {
// Create and prep the epoch publisher
auto topic =
m_inst.GetIntegerTopic(fmt::format("{}/{}", m_ntTopic, epochName));
auto pub = std::make_shared<nt::IntegerPublisher>(topic.Publish());
m_publisherCache.insert(std::pair(epochName, pub));
}
m_publisherCache.lookup(epochName)->Set(epoch.count());
}
if (m_dataLogEnabled) {
// Epochs are cached with the epoch name as the key, and the entry index as
// the value
if (m_entryCache.count(epochName) == 0) {
// Start a data log entry
int entryIndex = m_dataLog->Start(
fmt::format("{}/{}", m_dataLogEntry, epochName), "int64");
// Cache the entry index with the epoch name as the key
m_entryCache.insert(std::pair(epochName, entryIndex));
}
m_dataLog->AppendInteger(m_entryCache.lookup(epochName), epoch.count(), 0);
}
m_startTime = currentTime;
}

Expand Down
9 changes: 9 additions & 0 deletions wpilibc/src/main/native/cpp/Watchdog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ Watchdog& Watchdog::operator=(Watchdog&& rhs) {
return *this;
}

void Watchdog::PublishToNetworkTables(std::string_view topicName) {
m_tracer.PublishToNetworkTables(topicName);
}

void Watchdog::StartDataLog(wpi::log::DataLog& dataLog,
std::string_view entry) {
m_tracer.StartDataLog(dataLog, entry);
}

units::second_t Watchdog::GetTime() const {
return Timer::GetFPGATimestamp() - m_startTime;
}
Expand Down
29 changes: 29 additions & 0 deletions wpilibc/src/main/native/include/frc/IterativeRobotBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include <units/time.h>
#include <wpi/DataLog.h>

#include "frc/RobotBase.h"
#include "frc/Watchdog.h"
Expand Down Expand Up @@ -204,6 +205,34 @@ class IterativeRobotBase : public RobotBase {
*/
virtual void TestExit();

/**
* Starts publishing loop timings to NetworkTables. Subsequent calls will do
* nothing.
*
* This will publish the execution time of method calls in the robot loop;
* methods like robot periodic and init methods, various NT
* updates (SmartDashboard.update(), Shuffleboard.updateValues(),
* LiveWindow.updateValues()) etc.
*
* @param topicName The NetworkTables topic to publish to
*/
void PublishLoopTimingsToNetworkTables(std::string_view topicName);

/**
* Starts logging loop timings to the data log. Subsequent calls will do
* nothing.
*
* This will publish the execution time of method calls in the robot loop;
* methods like robot periodic and init methods, various NT
* updates (SmartDashboard.update(), Shuffleboard.updateValues(),
* LiveWindow.updateValues()) etc.
*
* @param dataLog The data log to log epochs to
* @param entry The name of the entry to log to
*/
void StartLoopTimingsDataLog(wpi::log::DataLog& dataLog,
std::string_view entry);

/**
* Enables or disables flushing NetworkTables every loop iteration.
* By default, this is enabled.
Expand Down
29 changes: 29 additions & 0 deletions wpilibc/src/main/native/include/frc/Tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
#pragma once

#include <chrono>
#include <memory>
#include <string_view>

#include <hal/cpp/fpga_clock.h>
#include <networktables/IntegerTopic.h>
#include <networktables/NetworkTableInstance.h>
#include <wpi/DataLog.h>
#include <wpi/StringMap.h>

namespace wpi {
Expand All @@ -30,6 +34,23 @@ class Tracer {
*/
Tracer();

/**
* Starts publishing added epochs to NetworkTables. Subsequent calls will do
* nothing.
*
* @param topicName The NetworkTables topic to publish to
*/
void PublishToNetworkTables(std::string_view topicName);

/**
* Starts logging added epochs to the data log. Subsequent calls will do
* nothing.
*
* @param dataLog The data log to log epochs to
* @param entry The name of the entry to log to
*/
void StartDataLog(wpi::log::DataLog& dataLog, std::string_view entry);

/**
* Restarts the epoch timer.
*/
Expand Down Expand Up @@ -67,6 +88,14 @@ class Tracer {

hal::fpga_clock::time_point m_startTime;
hal::fpga_clock::time_point m_lastEpochsPrintTime = hal::fpga_clock::epoch();
bool m_publishNT = false;
nt::NetworkTableInstance m_inst;
std::string_view m_ntTopic;
wpi::StringMap<std::shared_ptr<nt::IntegerPublisher>> m_publisherCache;
bool m_dataLogEnabled = false;
wpi::log::DataLog* m_dataLog;
std::string_view m_dataLogEntry;
wpi::StringMap<int> m_entryCache;

wpi::StringMap<std::chrono::nanoseconds> m_epochs;
};
Expand Down
17 changes: 17 additions & 0 deletions wpilibc/src/main/native/include/frc/Watchdog.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@ class Watchdog {
Watchdog(Watchdog&& rhs);
Watchdog& operator=(Watchdog&& rhs);

/**
* Starts publishing added epochs to NetworkTables. Subsequent calls will do
* nothing.
*
* @param topicName The NetworkTables topic to publish to
*/
void PublishToNetworkTables(std::string_view topicName);

/**
* Starts logging added epochs to the data log. Subsequent calls will do
* nothing.
*
* @param dataLog The data log to log epochs to
* @param entry The name of the entry to log to
*/
void StartDataLog(wpi::log::DataLog& dataLog, std::string_view entry);

/**
* Returns the time since the watchdog was last fed.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import edu.wpi.first.hal.FRCNetComm.tResourceType;
import edu.wpi.first.hal.HAL;
import edu.wpi.first.networktables.NetworkTableInstance;
import edu.wpi.first.util.datalog.DataLog;
import edu.wpi.first.wpilibj.livewindow.LiveWindow;
import edu.wpi.first.wpilibj.shuffleboard.Shuffleboard;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
Expand Down Expand Up @@ -250,6 +251,33 @@ public void teleopExit() {}
*/
public void testExit() {}

/**
* Starts publishing loop timings to NetworkTables. Subsequent calls will do nothing.
*
* <p>This will publish the execution time of method calls in the robot loop; methods like robot
* periodic and init methods, various NT updates (SmartDashboard.update(),
* Shuffleboard.updateValues(), LiveWindow.updateValues()) etc.
*
* @param topicName The NetworkTables topic to publish to
*/
public void publishLoopTimingsToNetworkTables(String topicName) {
m_watchdog.publishToNetworkTables(topicName);
}

/**
* Starts logging loop timings to the data log. Subsequent calls will do nothing.
*
* <p>This will log the execution time of method calls in the robot loop; methods like robot
* periodic and init methods, various NT updates (SmartDashboard.update(),
* Shuffleboard.updateValues(), LiveWindow.updateValues()) etc.
*
* @param dataLog The data log to log epochs to
* @param entry The name of the entry to log to
*/
public void startLoopTimingsDataLog(DataLog dataLog, String entry) {
m_watchdog.startDataLog(dataLog, entry);
}

/**
* Enables or disables flushing NetworkTables every loop iteration. By default, this is enabled.
*
Expand Down
Loading
Loading