bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
bitsim::sensors::backends::MQTT_SensorChannel Class Reference

The topic a sensor on a board publishes its readings to. More...

#include <mqtt_sensor_channel.h>

Classes

struct  Config

Public Types

using Clock = std::chrono::steady_clock

Public Member Functions

 MQTT_SensorChannel ()=default
 A channel that makes its own subscriber when connect() is called.
 MQTT_SensorChannel (network::MqttSubscriber &subscriber)
 A channel that reads the given subscriber. It must outlive the channel.
const Config & config () const noexcept
void set_config (const Config &config)
void connect ()
 Connect to the broker, unless the subscriber already is.
bool is_connected () const
template<typename Receive>
void read (Receive &&receive)
 Pass every message that has arrived to receive, oldest first, without waiting. receive returns true if it accepted the message.
void mark_received ()
 Record that a valid reading arrived now. read() does it for the messages receive accepts; call it when feeding messages by hand.
bool is_alive () const
uint_t n_received () const noexcept
uint_t n_rejected () const noexcept

Detailed Description

The topic a sensor on a board publishes its readings to.

The channel either uses a subscriber the application connected, or makes and connects its own from config() when connect() is called. Reading the channel never waits: it hands over every message that has arrived since the last read, so a simulation that reads it every step keeps up with a board that publishes often, and sees the latest reading.

Member Typedef Documentation

◆ Clock

using bitsim::sensors::backends::MQTT_SensorChannel::Clock = std::chrono::steady_clock

Constructor & Destructor Documentation

◆ MQTT_SensorChannel() [1/2]

bitsim::sensors::backends::MQTT_SensorChannel::MQTT_SensorChannel ( )
default

A channel that makes its own subscriber when connect() is called.

◆ MQTT_SensorChannel() [2/2]

bitsim::sensors::backends::MQTT_SensorChannel::MQTT_SensorChannel ( network::MqttSubscriber & subscriber)
explicit

A channel that reads the given subscriber. It must outlive the channel.

Member Function Documentation

◆ config()

const Config & bitsim::sensors::backends::MQTT_SensorChannel::config ( ) const
inlinenoexcept

◆ connect()

void bitsim::sensors::backends::MQTT_SensorChannel::connect ( )

Connect to the broker, unless the subscriber already is.

Exceptions
std::logic_errorIf the channel has no topic to subscribe to

◆ is_alive()

bool bitsim::sensors::backends::MQTT_SensorChannel::is_alive ( ) const
Returns
True if a reading arrived within the timeout

◆ is_connected()

bool bitsim::sensors::backends::MQTT_SensorChannel::is_connected ( ) const

◆ mark_received()

void bitsim::sensors::backends::MQTT_SensorChannel::mark_received ( )

Record that a valid reading arrived now. read() does it for the messages receive accepts; call it when feeding messages by hand.

◆ n_received()

uint_t bitsim::sensors::backends::MQTT_SensorChannel::n_received ( ) const
inlinenoexcept

◆ n_rejected()

uint_t bitsim::sensors::backends::MQTT_SensorChannel::n_rejected ( ) const
inlinenoexcept

◆ read()

template<typename Receive>
void bitsim::sensors::backends::MQTT_SensorChannel::read ( Receive && receive)

Pass every message that has arrived to receive, oldest first, without waiting. receive returns true if it accepted the message.

Exceptions
std::logic_errorIf the channel is not connected

◆ set_config()

void bitsim::sensors::backends::MQTT_SensorChannel::set_config ( const Config & config)
inline

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