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

A real ultrasonic sensor on a board that publishes its readings over MQTT, as an UltrasoundMessage: More...

#include <mqtt_ultrasound_backend.h>

Inheritance diagram for bitsim::sensors::backends::MQTT_UltrasonicBackend:
Collaboration diagram for bitsim::sensors::backends::MQTT_UltrasonicBackend:

Public Member Functions

 MQTT_UltrasonicBackend ()
 A backend that subscribes on its own, see connect(). Give it a topic with set_channel_config() or load().
 MQTT_UltrasonicBackend (const MQTT_SensorChannel::Config &config)
 A backend that subscribes on its own to the given broker and topic, see connect().
 MQTT_UltrasonicBackend (network::MqttSubscriber &subscriber)
 A backend that reads a subscriber the application connected. The subscriber must outlive the backend.
void load (const nlohmann::json &j) override
 Read the settings, see RangeSensorBackendBase::load(). Also reads broker, topic and timeout [s], all optional.
void set_channel_config (const MQTT_SensorChannel::Config &config)
 Set where the readings come from. Call it before connect().
void connect ()
 Connect to the broker, unless the subscriber already is.
std::vector< real_t > read_values () override
 Take the readings that have arrived.
bool receive (const std::string &text)
 Take one message, as read_values() does for each message that arrives. Use it to feed the backend without a broker.
bool is_alive () const
const MQTT_SensorChannel & channel () const noexcept
void set_position (const RealColVec3d &pos)
 Set the position of the sensor on the robot. The backend does not use it.
const RealColVec3d & position () const
const UltrasoundMessage & last_value_read () const
Public Member Functions inherited from bitsim::sensors::backends::RangeSensorBackendBase
real_t max_distance () const noexcept
void set_max_distance (real_t max_distance) noexcept
 Set the maximum distance the range sensor can read.
real_t min_distance () const noexcept
void set_min_distance (real_t min_distance) noexcept
 Set the minimum distance the range sensor can read.
virtual std::string sensor_units () const
void set_sensor_units (const std::string &units)
 Set the sensor units.
void load_from_json (const std::string &filename) override
 Read the settings from a JSON file, see load().
virtual void add_visual_shape (const std::unordered_map< std::string, std::any > &shape_properties) override
 Add a visual shape for the sensor in order to be visualized with Irrlicht. Concrete classes may choose not to implement this. In this case the sensor will not be visible.
Public Member Functions inherited from bitsim::sensors::backends::SensorBackendBase
virtual ~SensorBackendBase ()=default
 Destructor.
real_t sampling_period () const noexcept
 Returns the sampling period of the sensor.
void set_sampling_period (real_t period)
 Set the sampling period of the backend.
uint_t n_read_values () const noexcept
std::string backend_type_str () const noexcept
 Return a string that specifies the type of the backend.
void set_chrono_body (std::shared_ptr< chrono::ChBody > body)
 Set the pointer to the body that this sensor is attached.

Static Public Attributes

static const std::string BACKEND_TYPE = "MQTT"
 The type of the backend.

Additional Inherited Members

Static Public Member Functions inherited from bitsim::sensors::backends::RangeSensorBackendBase
static std::shared_ptr< RangeSensorBackendBase > create (const std::string &backend_type)
 Create a backend by the name a robot file gives it.
Protected Member Functions inherited from bitsim::sensors::backends::RangeSensorBackendBase
 RangeSensorBackendBase (const std::string &backend_type)
 Constructor.
real_t clamp_distance (real_t distance) const noexcept
Protected Member Functions inherited from bitsim::sensors::backends::SensorBackendBase
 SensorBackendBase (const std::string &backend_type)
 Constructor.
void set_n_read_values (uint_t n) noexcept
 Set how many values the implementation returns.
Protected Attributes inherited from bitsim::sensors::backends::SensorBackendBase
std::shared_ptr< chrono::ChBody > body_ {nullptr}
 The body on which the sensor sits.

Detailed Description

A real ultrasonic sensor on a board that publishes its readings over MQTT, as an UltrasoundMessage:

{"distance": 62.0, "unit": "cm"}

read_values() never waits for the board. It takes every message that has arrived and reports the latest distance, in metres. Until the first reading arrives it reports max_distance(), as if nothing were in range, so check is_alive() before trusting a reading: a board that has gone quiet keeps reporting its last distance.

Constructor & Destructor Documentation

◆ MQTT_UltrasonicBackend() [1/3]

bitsim::sensors::backends::MQTT_UltrasonicBackend::MQTT_UltrasonicBackend ( )

A backend that subscribes on its own, see connect(). Give it a topic with set_channel_config() or load().

◆ MQTT_UltrasonicBackend() [2/3]

bitsim::sensors::backends::MQTT_UltrasonicBackend::MQTT_UltrasonicBackend ( const MQTT_SensorChannel::Config & config)
explicit

A backend that subscribes on its own to the given broker and topic, see connect().

◆ MQTT_UltrasonicBackend() [3/3]

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

A backend that reads a subscriber the application connected. The subscriber must outlive the backend.

Member Function Documentation

◆ channel()

const MQTT_SensorChannel & bitsim::sensors::backends::MQTT_UltrasonicBackend::channel ( ) const
inlinenoexcept
Returns
The topic the readings come from, and its statistics

◆ connect()

void bitsim::sensors::backends::MQTT_UltrasonicBackend::connect ( )
inline

Connect to the broker, unless the subscriber already is.

◆ is_alive()

bool bitsim::sensors::backends::MQTT_UltrasonicBackend::is_alive ( ) const
inline
Returns
True if a reading arrived within the timeout of the channel

◆ last_value_read()

const UltrasoundMessage & bitsim::sensors::backends::MQTT_UltrasonicBackend::last_value_read ( ) const
inline
Returns
A read reference to the last UltrasoundMessage that was read

◆ load()

void bitsim::sensors::backends::MQTT_UltrasonicBackend::load ( const nlohmann::json & j)
overridevirtual

Read the settings, see RangeSensorBackendBase::load(). Also reads broker, topic and timeout [s], all optional.

Reimplemented from bitsim::sensors::backends::RangeSensorBackendBase.

◆ position()

const RealColVec3d & bitsim::sensors::backends::MQTT_UltrasonicBackend::position ( ) const
inline
Returns
A read reference of the position of the sensor on the robot

◆ read_values()

std::vector< real_t > bitsim::sensors::backends::MQTT_UltrasonicBackend::read_values ( )
overridevirtual

Take the readings that have arrived.

Returns
The latest distance [m], clamped to [min_distance(), max_distance()]
Exceptions
std::logic_errorIf the backend is not connected

Implements bitsim::sensors::backends::SensorBackendBase.

◆ receive()

bool bitsim::sensors::backends::MQTT_UltrasonicBackend::receive ( const std::string & text)

Take one message, as read_values() does for each message that arrives. Use it to feed the backend without a broker.

Returns
True if it was a valid UltrasoundMessage

◆ set_channel_config()

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

Set where the readings come from. Call it before connect().

◆ set_position()

void bitsim::sensors::backends::MQTT_UltrasonicBackend::set_position ( const RealColVec3d & pos)
inline

Set the position of the sensor on the robot. The backend does not use it.

Parameters
pos

Member Data Documentation

◆ BACKEND_TYPE

const std::string bitsim::sensors::backends::MQTT_UltrasonicBackend::BACKEND_TYPE = "MQTT"
static

The type of the backend.


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