Valkyrie 2026
Loading...
Searching...
No Matches
valor::Loggable Class Reference

Base helper for publishing and subscribing values to NetworkTables. More...

#include <valkyrie/Loggable.h>

Inheritance diagram for valor::Loggable:
valor::sensors::BaseSensor< std::optional< PoseEstimate > > valor::sensors::BaseSensor< double > valor::sensors::BaseSensor< int > valor::sensors::BaseSensor< units::ampere_t > valor::sensors::BaseSensor< bool > valor::sensors::BaseSensor< std::optional< frc::Pose3d > > valor::sensors::BaseSensor< LaserProximityData > valor::sensors::BaseSensor< units::millimeter_t > valor::BaseController valor::BaseSubsystem valor::WorldAlign valor::cameras::Limelight valor::sensors::BaseSensor< T > valor::swerve::SwerveModule

Public Member Functions

 Loggable (std::string_view name)
 Construct a Loggable that registers a top-level table name.
 Loggable (std::shared_ptr< nt::NetworkTable > table)
 Construct a Loggable that uses an existing NetworkTable.
 Loggable ()
 Default construct an uninitialized Loggable.
void LogChild (std::string_view name, Loggable *child)
 Register a child Loggable under a named subtree.
void LogChild (std::string_view name, wpi::Sendable *child)
 Register a child Sendable under a named subtree.
void SetPeriodicLevel (LogLevel level)
 Set the logging level for LoggablePeriodic.
template<typename T>
WriteLog (std::string_view field, const T &data, LogLevel level=LogLevel::Normal)
 Publish a value to NetworkTables under the given field.
template<typename T>
ReadLog (std::string_view field, const T &defaultValue={})
 Read a value from NetworkTables for the given field.
template<>
decltype(Loggable::subscribers) ::iterator addSubscriber (std::string_view field, const std::vector< bool > &defaultValue)
template<>
void WriteLogImpl (nt::Publisher *pub, const std::vector< bool > &data)
template<>
std::vector< bool > ReadLogImpl (nt::Subscriber *sub)

Static Public Member Functions

static units::millisecond_t GetLoggingTime ()
static constexpr LoggableGetRoot ()

Protected Member Functions

virtual void OnLoggingStart ()
 Hook invoked when logging is started for this object.
virtual void LoggablePeriodic ()
 Periodic callback for logging updates.

Detailed Description

Base helper for publishing and subscribing values to NetworkTables.

The Loggable class provides a type-safe wrapper around NetworkTables publishers and subscribers. It lazily creates publishers/subscribers for fields on first use and handles mapping of C++ types (including unit types and container types) to the appropriate NetworkTables topic types.

Typical usage:

  • Use WriteLog<T>(field, value) to publish a value of type T.
  • Use ReadLog<T>(field, defaultValue) to subscribe and read a value of type T.

The class also supports hierarchical logging by exposing LogChild() to register child Loggable or Sendable objects under a named subtree.

Constructor & Destructor Documentation

◆ Loggable() [1/3]

valor::Loggable::Loggable ( std::string_view name)
explicit

Construct a Loggable that registers a top-level table name.

This constructor will create or acquire a NetworkTable for the specified name and later use it when publishing or subscribing fields.

Note: the implementation acquires the table under the "/LiveWindow" root (i.e. GetTable("/LiveWindow")->GetSubTable(name)).

Parameters
nameTable name to use for this Loggable instance.

◆ Loggable() [2/3]

valor::Loggable::Loggable ( std::shared_ptr< nt::NetworkTable > table)
explicit

Construct a Loggable that uses an existing NetworkTable.

Use this constructor when the caller has already created or acquired a NetworkTable and wants this Loggable to publish/subscribe directly on that table. The provided table is stored as a shared_ptr and the Loggable will call EnableLogging() during construction (see implementation in Loggable.cpp).

Notes:

  • The Loggable does not modify the provided table's path; it uses the table pointer as-is. If you want the Loggable to be placed under a particular namespace (for example /SmartDashboard/<name>), acquire the appropriate subtable before passing it in.
  • Ownership of the table is shared (std::shared_ptr). The caller may keep or drop their reference; the Loggable will retain its copy.
Parameters
tableShared pointer to an already-initialized NetworkTable to use for publishing and subscribing.

◆ Loggable() [3/3]

valor::Loggable::Loggable ( )

Default construct an uninitialized Loggable.

Use this constructor when the derived type will initialize the table at a later time or when the object is intended to act only as a container for children.

Member Function Documentation

◆ LogChild() [1/2]

void valor::Loggable::LogChild ( std::string_view name,
Loggable * child )

Register a child Loggable under a named subtree.

The child will be exposed under this object's table namespace and publishing/subscribing calls made by the child will use the child's table.

Parameters
nameThe name under which the child will be visible.
childPointer to the child Loggable instance. Ownership is not transferred; caller must ensure lifetime.

◆ LogChild() [2/2]

void valor::Loggable::LogChild ( std::string_view name,
wpi::Sendable * child )

Register a child Sendable under a named subtree.

This overload allows registering objects implementing wpi::Sendable so they can also be displayed/inspected in dashboards that consume NetworkTables data.

The implementation registers the sendable with the LiveWindow/sendable registry (via wpi::SendableRegistry::AddLW) and enables LiveWindow telemetry for the object (via frc::LiveWindow::EnableTelemetry).

Parameters
nameThe name under which the child will be visible.
childPointer to a wpi::Sendable instance. Ownership is not transferred; caller must ensure lifetime.

◆ LoggablePeriodic()

virtual void valor::Loggable::LoggablePeriodic ( )
inlineprotectedvirtual

Periodic callback for logging updates.

If a derived class needs to perform periodic updates to published values, override this method. It is intended to be called by the logging framework; this base class provides an empty implementation.

Reimplemented in valor::cameras::Limelight, valor::CorePhoenixController, valor::NeoController, valor::sensors::CurrentSensor, valor::swerve::Swerve, valor::swerve::SwerveModule, and valor::WorldAlign.

◆ OnLoggingStart()

virtual void valor::Loggable::OnLoggingStart ( )
inlineprotectedvirtual

Hook invoked when logging is started for this object.

Derived classes may override to perform initialization when the NetworkTables table becomes active for this Loggable.

Reimplemented in valor::BaseController.

◆ ReadLog()

template<typename T>
T valor::Loggable::ReadLog ( std::string_view field,
const T & defaultValue = {} )
inline

Read a value from NetworkTables for the given field.

If a subscriber for the specified field does not yet exist it is created and subscribed with the provided default value. The value is returned converted to the requested template type T.

Template Parameters
TThe requested return type. Must be compatible with the stored NetworkTables type; unit types and vectors are supported.
Parameters
fieldName of the entry (relative to this object's table).
defaultValueValue to return if the entry does not yet exist.
Returns
The value read from NetworkTables, or defaultValue if none.

◆ SetPeriodicLevel()

void valor::Loggable::SetPeriodicLevel ( LogLevel level)
inline

Set the logging level for LoggablePeriodic.

The logging level determines the minimum time between LoggablePeriodic calls, meant to help with network and CPU load on the RIO.

Parameters
levelLogging level for LoggablePeriodic

◆ WriteLog()

template<typename T>
T valor::Loggable::WriteLog ( std::string_view field,
const T & data,
LogLevel level = LogLevel::Normal )
inline

Publish a value to NetworkTables under the given field.

If a publisher for the specified field does not yet exist it is created automatically. The function is a template and supports primitive types, STL containers (e.g., std::vector), unit types (from the units library), and other serializable types supported by NetworkTables.

Template Parameters
TThe type of the value to publish. Must be compatible with NetworkTables (see nt_traits in the implementation).
Parameters
fieldName of the entry (relative to this object's table).
dataThe value to publish.
levelThe logging level to use for this log. This determines the minimum time since the last log, meant to help with CPU and network load on the RIO.
Returns
The value of the data

The documentation for this class was generated from the following file: