Timestamp synchronization between two devices

Here are the brief steps to configure NTP on two machines:
Install NTP on two machines:

sudo apt-get update
sudo apt-get install ntp

Configure NTP server:
On machine A, configure it as an NTP server so that its time becomes the time base within the network.

Edit the NTP configuration file:

sudo nano /etc/ntp.conf

Configure the server as an NTP server. Find the following line and comment it out (add # before the line):

arduino

# pool <server> iburst

Then add the following lines:

server 127.127.1.0 iburst
fudge 127.127.1.0 stratum 10

Start NTP server:

sudo service ntp start

Configure NTP client:
On machine B, configure it as an NTP client to synchronize time with machine A.

Edit the NTP configuration file:

sudo nano /etc/ntp.conf

Find the following line and comment it out (add # before the line):

pool <server> iburst

Then add the following line, replacing <machineA_IP> with the IP address of machine A:

server <machineA_IP> iburst

Start the NTP client:

sudo service ntp start

After completing the above steps, NTP will help you synchronize the system time between the two machines. You can then use ROS 2's time synchronization features, such as TimeSynchronizer, on both machines to ensure correct synchronization of timestamps. Please ensure that machine A and machine B are within the same network and can access and communicate with each other.

Based on the above situation, how to synchronize the timestamps of two vehicles?

In ROS 2, you can use ROS 2's time synchronization feature to ensure that the timestamps on both machines remain in sync. ROS 2 provides a tool called TimeSynchronizer, which can help you synchronize the timestamps of two topics. Here are the steps to synchronize timestamps between two machines:

  1. Publish a message on machine A:
#include <rclcpp/rclcpp.hpp>
#include <geometry_msgs/msg/transform_stamped.hpp>
#include <tf2_ros/transform_broadcaster.h>

int main(int argc, char** argv) {
    
    
    rclcpp::init(argc, argv);
    auto node = rclcpp::Node::make_shared("tf_publisher");

    tf2_ros::TransformBroadcaster tf_broadcaster(node);

    rclcpp::Rate rate(10.0); // 设置发布频率

    while (rclcpp::ok()) {
    
    
        // 假设你有一个变换信息,表示从 frame1 到 frame2 的变换
        geometry_msgs::msg::TransformStamped transform;
        transform.header.stamp = node->now();
        transform.header.frame_id = "frame1";
        transform.child_frame_id = "frame2";
        transform.transform.translation.x = 1.0; // 假设x方向平移了1米
        transform.transform.rotation.w = 1.0;    // 假设没有旋转

        tf_broadcaster.sendTransform(transform);

        rate.sleep();
    }

    rclcpp::shutdown();
    return 0;
}

  1. Receive messages on machine B and synchronize timestamps:
#include <rclcpp/rclcpp.hpp>
#include <tf2_ros/transform_listener.h>
#include <message_filters/subscriber.h>
#include <tf2_ros/message_filter.h>
#include <geometry_msgs/msg/transform_stamped.hpp>

void callback(const geometry_msgs::msg::TransformStamped::SharedPtr& msg) {
    
    
    // 在这里处理接收到的同步后的消息
    RCLCPP_INFO(rclcpp::get_logger("tf_subscriber"), "Received Transform - Time: %f",
                msg->header.stamp.sec + (msg->header.stamp.nanosec / 1e9));
}

int main(int argc, char** argv) {
    
    
    rclcpp::init(argc, argv);
    auto node = rclcpp::Node::make_shared("tf_subscriber");

    // 订阅来自机器A发布的TransformStamped消息
    auto subscriber = node->create_subscription<geometry_msgs::msg::TransformStamped>(
        "tf_topic", rclcpp::QoS(10),
        [](const geometry_msgs::msg::TransformStamped::SharedPtr msg) {
    
    
            callback(msg);
        });

    // 使用TimeSynchronizer同步消息时间戳
    message_filters::Subscriber<geometry_msgs::msg::TransformStamped> tf_subscriber(node, "tf_topic");
    tf2_ros::MessageFilter<geometry_msgs::msg::TransformStamped> tf_filter(
        tf_subscriber, node, "target_frame", 10);
    tf_filter.registerCallback(callback);

    rclcpp::spin(node);
    rclcpp::shutdown();
    return 0;
}

In the subscribing node of machine B, we use TimeSynchronizer to synchronize timestamps. You need to replace "target_frame" in the above code with the name of the target frame you want to synchronize. When the publisher on machine A publishes tf messages, the TimeSynchronizer will receive these messages on machine B and ensure that their timestamps remain in sync. The synchronized message will be passed to the callback function for processing.

This way you can synchronize timestamps between the two machines and correctly handle tf information published from machine A on machine B.

Guess you like

Origin blog.csdn.net/CCCrunner/article/details/131964984