Skip to content

Commit 9ecb734

Browse files
committed
optimize timestamping for latency measurement
departure latency right before publication, arrival latency first thing in callback
1 parent f1b8903 commit 9ecb734

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

include/mqtt_client/MqttClient.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,11 @@ class MqttClient : public nodelet::Nodelet,
186186
* - serialized timestamp (optional)
187187
* - serialized ROS message
188188
*
189-
* The MQTT payload is expected to carry a serialized ROS message.
190-
*
191-
* @param mqtt_msg MQTT message
189+
* @param mqtt_msg MQTT message
190+
* @param arrival_stamp arrival timestamp used for latency computation
192191
*/
193-
void mqtt2ros(mqtt::const_message_ptr mqtt_msg);
192+
void mqtt2ros(mqtt::const_message_ptr mqtt_msg,
193+
const ros::WallTime& arrival_stamp = ros::WallTime::now());
194194

195195
/**
196196
* @brief Callback for when the client has successfully connected to the

src/MqttClient.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,8 @@ void MqttClient::ros2mqtt(const topic_tools::ShapeShifter::ConstPtr& ros_msg,
463463
}
464464

465465

466-
void MqttClient::mqtt2ros(mqtt::const_message_ptr mqtt_msg) {
466+
void MqttClient::mqtt2ros(mqtt::const_message_ptr mqtt_msg,
467+
const ros::WallTime& arrival_stamp) {
467468

468469
std::string mqtt_topic = mqtt_msg->get_topic();
469470
Mqtt2RosInterface& mqtt2ros = mqtt2ros_[mqtt_topic];
@@ -492,8 +493,7 @@ void MqttClient::mqtt2ros(mqtt::const_message_ptr mqtt_msg) {
492493
ros::serialization::deserialize(stamp_stream, stamp);
493494

494495
// compute ROS2MQTT latency
495-
ros::WallTime now_wall = ros::WallTime::now();
496-
ros::Time now(now_wall.sec, now_wall.nsec);
496+
ros::Time now(arrival_stamp.sec, arrival_stamp.nsec);
497497
if (now.isZero())
498498
NODELET_WARN(
499499
"Cannot compute latency for MQTT topic %s when ROS time is 0, is a ROS "
@@ -574,6 +574,9 @@ bool MqttClient::isConnectedService(IsConnected::Request& request,
574574

575575
void MqttClient::message_arrived(mqtt::const_message_ptr mqtt_msg) {
576576

577+
// instantly take arrival timestamp
578+
ros::WallTime arrival_stamp = ros::WallTime::now();
579+
577580
std::string mqtt_topic = mqtt_msg->get_topic();
578581
NODELET_DEBUG("Received MQTT message on topic '%s'", mqtt_topic.c_str());
579582
auto& payload = mqtt_msg->get_payload_ref();
@@ -624,7 +627,7 @@ void MqttClient::message_arrived(mqtt::const_message_ptr mqtt_msg) {
624627

625628
// publish ROS message, if publisher initialized
626629
if (!mqtt2ros_[mqtt_topic].ros.publisher.getTopic().empty()) {
627-
mqtt2ros(mqtt_msg);
630+
mqtt2ros(mqtt_msg, arrival_stamp);
628631
} else {
629632
NODELET_WARN(
630633
"ROS publisher for data from MQTT topic '%s' is not yet initialized: "

0 commit comments

Comments
 (0)