Valor 6800 1.0
Loading...
Searching...
No Matches
valor::Loggable Class Reference

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

#include <Loggable.h>

Inheritance diagram for valor::Loggable:
valor::BaseSensor< int > valor::BaseSensor< double > valor::BaseSensor< bool > valor::BaseSensor< LaserProximityData > valor::BaseSensor< units::millimeter_t > valor::BaseSensor< frc::Pose3d > valor::BaseController valor::BaseSensor< T > valor::BaseSubsystem valor::SwerveModule valor::WorldAlign

Public Member Functions

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 setLoggingPeriod (units::millisecond_t period)
 Set the minimum period between LoggablePeriodic() invocations.
 
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< boolReadLogImpl (nt::Subscriber *sub)
 

Static Public Member Functions

static units::millisecond_t GetLoggingTime ()
 

Protected Member Functions

 Loggable (std::string_view name)
 Construct a Loggable that registers a top-level table name.
 
 Loggable ()
 Default construct an uninitialized Loggable.
 
virtual ~Loggable ()=default
 Virtual destructor.
 
virtual void OnLoggingStart ()
 Hook invoked when logging is started for this object.
 
virtual void LoggablePeriodic ()
 Periodic callback for logging updates.
 
template<typename T >
T WriteLog (std::string_view field, const T &data)
 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.
 

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/2]

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

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/2]

valor::Loggable::Loggable ( )
protected

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::NeoController, valor::PhoenixController< VelocityOutput, PositionOutput >, valor::Swerve, valor::SwerveModule, valor::AprilTagsSensor, valor::GamePieceSensor, 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 = {} )
inlineprotected

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.

◆ setLoggingPeriod()

void valor::Loggable::setLoggingPeriod ( units::millisecond_t period)
inline

Set the minimum period between LoggablePeriodic() invocations.

When set to a value greater than zero, LoggablePeriodic() will only execute when at least this much time has elapsed since the last invocation. A value of 0ms (the default) means LoggablePeriodic() runs every scheduler cycle (~20ms).

Parameters
periodMinimum time between LoggablePeriodic() calls.

◆ WriteLog()

template<typename T >
T valor::Loggable::WriteLog ( std::string_view field,
const T & data )
inlineprotected

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.
Returns
The value of the data

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