Inter Process Communication (IPC)
Redis
-
class RedisPublisher
RedisPublisher class. This class is a simple wrapper around the redis++ publisher, but it applies default settings for flyMS. It can be subscribed to using the RedisSubscriber class.
Exampe Usage:
RedisPublisher pub; std::string channel = "test_channel"; std::string message = "test_message"; pub.publish(channel, message);
Subclassed by flyMS::MavlinkRedisPub
Public Functions
-
inline RedisPublisher()
Construct a new Redis Publisher object.
-
inline void publish(std::string_view channel, std::string_view msg)
Publish a message to a redis channel.
- Parameters:
channel – The channel to publish to
msg – The contents of the message
-
inline void publish(std::string_view channel, const YAML::Node &msg)
Publish a YAML node to the redis channel. The yaml node is converted to a string using it’s << operator.
- Parameters:
channel – The channel to publish to
msg – The yaml node to stringify then publish
-
inline RedisPublisher()
-
class RedisSubscriber
RedisSubscriber class. This class is a simple wrapper around the redis++ subscriber, but it applies default settings for flyMS. It receives messages from the RedisPublisher class.
Example Usage:
std::mutex mutex; std::string my_received_message; auto callback = [&](auto channel, auto message) { std::unique_lock<std::mutex> lock(mutex); my_received_message = message; }; RedisSubscriber sub(callback, {"test_channel"}); while (true) { // Lock mutex and do stuff with my_received_message }
Subclassed by flyMS::MavlinkRedisSub
Public Functions
-
RedisSubscriber(std::function<void(std::string, std::string)> callback, const std::vector<std::string> &channels, std::function<void(const sw::redis::Error&)> error_callback = RedisSubscriber::default_timeout_callback)
Construct a new Redis Interface object.
- Parameters:
callback – The callback function to invoke when a message is received on one of the subscribed channels
channels – The channels to subscribe to
error_callback – The callback function to invoke when an error occurs, this includes timeouts when messages are not received within the expected period
-
~RedisSubscriber()
Destroy the RedisSubscriber object.
-
void set_error_callback(std::function<void(const sw::redis::Error)> callback)
Set the error callback function. This function will be called if the internal redis++ subscriber throws an exeption.
- Parameters:
callback – The callback function to invoke when an error occurs
-
RedisSubscriber(std::function<void(std::string, std::string)> callback, const std::vector<std::string> &channels, std::function<void(const sw::redis::Error&)> error_callback = RedisSubscriber::default_timeout_callback)
UART
-
class Uart
Send and receive data over a UART port. Baudrate is set to B115200.
Example Usage:
auto callback = [](const std::array<uint8_t, kBUF_SIZE>& bytes, size_t size) { // do something with the received bytes }; Uart uart("/dev/ttyUSB0", callback); uart.send({0x01, 0x02, 0x03});
Subclassed by flyMS::MavlinkUart
Public Functions
-
Uart(const std::filesystem::path &serial_dev, std::function<void(const std::array<uint8_t, kBUF_SIZE>&, size_t)> callback)
Construct a new Uart object.
- Parameters:
serial_dev – The path to the serial device
callback – The callback function that is called when new data is received
-
bool send(const std::vector<uint8_t> &bytes)
Send data over the UART port.
- Parameters:
bytes – The data to send
- Returns:
true Data was sent successfully
- Returns:
false There was an error sending data
-
Uart(const std::filesystem::path &serial_dev, std::function<void(const std::array<uint8_t, kBUF_SIZE>&, size_t)> callback)
Mavlink
-
class MavlinkParser
Decode a set of mavlink messages. That is, the de-serialization from
mavlink_message_t
to the custom mavlink message type. Different message types are registered with this class during initialization, and it will decode them and send them to the appropriate callback function. Serialized messages that need parsing should be sent to the MavlinkParser::receive_mavlink_msg function.Example usage:
MavlinkParser parser; parser.register_message<mavlink_odometry_t, MAVLINK_MSG_ID_ODOMETRY>( [](mavlink_odometry_t& typed_msg) { std::cout << "Received odometry message with timestamp: " << typed_msg.time_usec << std::endl; }, mavlink_msg_odometry_decode); // .. Receive a mavlink_message_t via UART, REDIS, etc. parser.receive_mavlink_msg(msg); // .. The callback function will be called with the decoded message
Subclassed by flyMS::MavlinkRedisSub, flyMS::MavlinkUart
Public Functions
-
template<typename MavType, uint32_t MsgId>
inline void register_message(std::function<void(MavType&)> callback, std::function<void(const mavlink_message_t*, MavType*)> decode_func) Registers a custom message type with this parser. A message cannot be parsed until it is registered. When a message gets parsed, the callback function will be called with the decoded message.
- Template Parameters:
MavType – The custom mavlink message type
MsgId – The ID of the custom mavlink message type
- Parameters:
callback – The callback function to send the decoded message to
decode_func – The function to decode the message. This function is provided by mavlink. It typically has the format: “mavlink_msg_<custom_msg_type>_decode”
-
inline bool receive_mavlink_msg(const mavlink_message_t &msg)
Receives a serialized message and parses it. The message will be decoded and sent to the appropriate callback.
- Parameters:
msg – The message to parse
- Returns:
true If the message was parsed successfully
- Returns:
false If the message was not parsed successfully
-
template<typename MavType, uint32_t MsgId>
-
class MavlinkRedisPub : private flyMS::RedisPublisher
Publish Mavlink messages to a Redis channel. Any type of Mavlink message can be published.
Example Usage:
MavlinkRedisPub pub; mavlink_odometry_t odom_msg; // Fill contents of odom_msg.. pub.publish("test_redis_channel", odom_msg, &mavlink_msg_odometry_encode);
Public Functions
-
template<typename MavType>
inline void publish(std::string_view channel, MavType &msg, std::function<uint16_t(uint8_t, uint8_t, mavlink_message_t*, const MavType*)> encode_func) Publish a Mavlink message to a Redis channel.
- Template Parameters:
MavType – The type of the mavlink message to publish
- Parameters:
channel – The Redis channel to publish the message to
msg – The message to publish
encode_func – The encode funtion for the mavlink message type. This is supplied by the mavlink library. It will have the form: ‘void mavlink_msg_<message_name>_encode(uint8_t system_id, uint8_t component_id, mavlink_message_t* msg, const mavlink_<message_name>_t* <message_name>)’
-
inline void publish(std::string_view channel, const mavlink_message_t &msg)
Publish a generic mavlink_message_t to a Redis channel.
- Parameters:
channel – The Redis channel to publish the message to
msg – The messsage to publish
-
inline void set_system_id(uint8_t system_id)
Set the System ID for the Mavlink messages. This is not required publish messages.
- Parameters:
system_id – The system ID to use
-
inline void set_component_id(uint8_t component_id)
Set the Component ID for the Mavlink messages. This is not required publish messages.
- Parameters:
component_id – The component ID to use
-
template<typename MavType>
-
class MavlinkRedisSub : public flyMS::RedisSubscriber, public flyMS::MavlinkParser
Subscribe to a Redis channel and parse the messages as Mavlink messages. The desired messages to parse must be registered with this object. ‘Registering’ requires the user to provide the mavlink decode function, and the callback function that will be invoked when messages are received.
Example Usage:
auto callback = [](mavlink_odometry_t& msg) { std::cout << "Received message: " << msg.x << std::endl; }; MavlinkRedisSub redis_sub("test_mavlink_channel"); redis_sub.register_message<mavlink_odometry_t, MAVLINK_MSG_ID_ODOMETRY>(callback, &mavlink_msg_odometry_decode); while (true) { // do something and wait for messages }
Subclassed by flyMS::MavlinkRedisSubQueue
-
class MavlinkRedisSubQueue : public flyMS::MavlinkRedisSub
Subscribe to a Redis channel and parse the messages as Mavlink messages. This class is built on top of MavlinkRedisSub, but adds the ability to queue messages instead of processing them via callback functions.
Example Usage:
MavlinkRedisSubQueue redis_sub("test_mavlink_channel"); auto queue = redis_sub.register_message_with_queue<mavlink_odometry_t, MAVLINK_MSG_ID_ODOMETRY>(&mavlink_msg_odometry_decode); while (true) { if (!queue->empty()) { auto msg = queue->pop_front(); // process message } // sleep or do something else }
Public Functions
-
inline MavlinkRedisSubQueue(const std::string &channel)
Construct a new MavlinkRedisSubQueue object.
- Parameters:
channel – The Redis channel to receive mavlink messages on
Register a message to be parsed by incoming mavlink messages via the Redis channel, and receive a queue of messages where the messages will be stored when they are received.
- Template Parameters:
MavType – The type of the mavlink message to parse
MsgId – The id of the mavlink message to parse
- Parameters:
decode – The decode function for the mavlink message
- Returns:
std::shared_ptr<RedisQueue<MavType>>
-
inline MavlinkRedisSubQueue(const std::string &channel)
-
class MavlinkUart : public flyMS::MavlinkParser, private flyMS::Uart
Public Functions
-
inline MavlinkUart(const std::filesystem::path &serial_dev)
Construct a new Redis Interface object.
-
inline MavlinkUart(const std::filesystem::path &serial_dev)