ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

When I subscribed to nav_msgs/Odometry data which is published at a frequency of 100hz, the callback processed the data slowly and the data was lost, thanks.

asked 2023-08-02 21:21:41 -0500

zyt gravatar image

Hi, when I subscribed to nav_msgs/Odometry data which is published at a frequency of 100hz, the callback processed the data slowly and the data was lost. Even if it do nothing with the data, just print it to the screen. Here is my code, thanks. Details are below the code.

subscriber.cpp

#include "ros/ros.h"
#include "nav_msgs/Odometry.h"

void Callback(const nav_msgs::Odometry::ConstPtr &data)
{
    ROS_INFO("x: %f", data->pose.pose.position.x);
}

int main(int argc, char *argv[])
{
    ros::init(argc, argv, "sub");
    ros::NodeHandle nh;
    ros::Subscriber sub = nh.subscribe("data", 1, Callback);
    ros::spin();

    return 0;
}

publisher.cpp

#include "ros/ros.h"
#include "nav_msgs/Odometry.h"
int main(int argc, char *argv[])
{
    ros::init(argc, argv, "pub");
    ros::NodeHandle nh;
    ros::Publisher pub = nh.advertise<nav_msgs::Odometry>("data", 1);
    nav_msgs::Odometry data;
    data.pose.pose.position.x = 0;
    data.pose.pose.position.y = 0;
    data.pose.pose.position.z = 0;
    data.twist.twist.linear.x = 0;
    data.twist.twist.linear.y = 0;
    data.twist.twist.linear.z = 0;
    ros::Rate rate(100);
    while (ros::ok())
    {
        pub.publish(data);
        data.pose.pose.position.x += 1.0;
        rate.sleep();
        ros::spinOnce();
    }

    return 0;
}

After i run the code, the info output by the callback like this. The difference between the data before and after 1 second is 100, which is correct, but the changes in the data are not continuous, about two-thirds of the data is lost, after I change the published and subscribed data to a simple int type, the info output by the callback was not lost. Could someone tell me why.

[ INFO] [1691025304.970631250]: x: 534.000000
[ INFO] [1691025305.014777988]: x: 538.000000
[ INFO] [1691025305.019294864]: x: 539.000000
[ INFO] [1691025305.029349545]: x: 540.000000
[ INFO] [1691025305.070614103]: x: 544.000000
[ INFO] [1691025305.114707116]: x: 548.000000
[ INFO] [1691025305.158679485]: x: 552.000000
[ INFO] [1691025305.202648991]: x: 557.000000
[ INFO] [1691025305.246669446]: x: 561.000000
[ INFO] [1691025305.249411401]: x: 562.000000
[ INFO] [1691025305.259341235]: x: 563.000000
[ INFO] [1691025305.302654749]: x: 567.000000
[ INFO] [1691025305.346705878]: x: 571.000000
[ INFO] [1691025305.390708431]: x: 576.000000
[ INFO] [1691025305.434621099]: x: 580.000000
[ INFO] [1691025305.439409861]: x: 581.000000
[ INFO] [1691025305.449302088]: x: 582.000000
[ INFO] [1691025305.490661031]: x: 586.000000
[ INFO] [1691025305.534639046]: x: 590.000000
[ INFO] [1691025305.582676911]: x: 595.000000
[ INFO] [1691025305.626683167]: x: 599.000000
[ INFO] [1691025305.629425701]: x: 600.000000
[ INFO] [1691025305.639365812]: x: 601.000000
[ INFO] [1691025305.682694151]: x: 605.000000
[ INFO] [1691025305.726678524]: x: 609.000000
[ INFO] [1691025305.770639589]: x: 613.000000
[ INFO] [1691025305.770734517]: x: 614.000000
[ INFO] [1691025305.814676002]: x: 617.000000
[ INFO] [1691025305.814776174]: x: 618.000000
[ INFO] [1691025305.819332395]: x: 619.000000
[ INFO] [1691025305.829267456]: x: 620.000000
[ INFO] [1691025305.870585413]: x: 623.000000
[ INFO] [1691025305.870677599]: x: 624.000000
[ INFO] [1691025305.914597913]: x: 628.000000
[ INFO] [1691025305.958604223]: x: 631.000000
[ INFO] [1691025305.958706574]: x: 632.000000
edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
1

answered 2023-08-02 21:53:43 -0500

Increase your queue size on the subscriber to ~10, 1 is probably why some get replaced in the queue

edit flag offensive delete link more

Comments

Thank you so much for your endless help!

zyt gravatar image zyt  ( 2023-08-05 06:17:45 -0500 )edit
1

answered 2023-08-04 13:55:26 -0500

updated 2023-08-04 15:29:33 -0500

Printing the data on the screen is actually not "nothing". It is more time consuming than several other operations that you could do, try storing it to a another variable and print all of them after the designated time. You should be able to receive the data at much higher rates with roscpp. Increasing the queue size should also help in case of sporadic transport fluctuations.

edit flag offensive delete link more

Comments

Thank you so much for your endless help!

zyt gravatar image zyt  ( 2023-08-05 06:18:21 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2023-08-02 21:21:41 -0500

Seen: 62 times

Last updated: Aug 04 '23