Ask Your Question
0

Publisher only publishing two of my three variables

asked 2020-01-21 17:04:21 -0600

ZuberA gravatar image

Hi,

I've been playing around with publishers and subscribers, challenging myself with modification on the tutorial pub/sub examples just to gain a foundation.

#include <ros/ros.h>
#include <std_msgs/Float64.h>

int main(int argc, char **argv)
{
    ros::init(argc, argv, "project_one_publisher"); //publisher node name
    ros::NodeHandle n;
    ros::Publisher p_one_pub = n.advertise<std_msgs::Float64>("project_one", 1);

    std_msgs::Float64 tens;
    std_msgs::Float64 hundreds;
    std_msgs::Float64 thousands;

    ros::Rate naptime(1.0);

    tens.data=0.0;
    hundreds.data=0.0;
    thousands.data=0.0;

    while (ros::ok())

{
     p_one_pub.publish(tens);
     tens.data=tens.data + 10;
     p_one_pub.publish(hundreds);
     hundreds.data=hundreds.data + 100;
     p_one_pub.publish(thousands);
     thousands.data=thousands.data + 1000;

     naptime.sleep();
    }
}

I have no problems running my code however when I use rostopic echo <topic> it only prints the hundreds and thousands value, it doesn't display the tens value.

I also created a subscriber to subscribe to incoming data however that only reads the thousands value while randomly reading the hundreds value, ex:

[ INFO] [1579646242.765639960]: recieved value is: 264000.000000
[ INFO] [1579646243.804282174]: recieved value is: 265000.000000
[ INFO] [1579646244.774907455]: recieved value is: 266000.000000
[ INFO] [1579646245.809710083]: recieved value is: 2670.000000
[ INFO] [1579646245.809943092]: recieved value is: 267000.000000
[ INFO] [1579646246.763998267]: recieved value is: 268000.000000
[ INFO] [1579646247.766197402]: recieved value is: 269000.000000

So ya, my question is why on the echo topic, it's only showing the hundreds and thousands value but not the tens value and on the subscriber side, why it's mostly using the thousands value.

Thanks

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2020-01-21 17:23:19 -0600

My best guess is that this error is caused by you setting the queue_size to 1. Even though you are sleeping for a full second, you are trying to publish very quickly (three messages right in a row). If you setup your publisher to have a bit more of a queue, it will have time to publish all messages in the queue before trying to publish again on the next loop.

Read more here: http://wiki.ros.org/roscpp/Overview/P...

edit flag offensive delete link more

Comments

That did the trick! I had to match the queue size on the subscriber too and got it to read properly, thank you!

ZuberA gravatar imageZuberA ( 2020-01-21 20:31:11 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2020-01-21 17:04:21 -0600

Seen: 27 times

Last updated: Jan 21