0
$\begingroup$

Rosanswers logo

Hi all,
I wrote a c++ script to send messages to a certain topic:

#include "std_msgs/String.h"
#include "ros/ros.h"

int main(int argc, char **argv){
    ros::init(argc, argv,"end_bag");
    ros::NodeHandle n;
    
    ros::Publisher chatter_pub = n.advertise("/end",1000);
    ros::Rate loop_rate(10);
    
    std_msgs::String msg;
    msg.data = "end of loop";
    chatter_pub.publish(msg);

    //ROS_INFO("Message sent: %s",msg.data.c_str());
    while(chatter_pub.getNumSubscribers()==0){
        ros::spinOnce();
    }
    //sleep(10);
    loop_rate.sleep();
    ROS_INFO("Message sent: %s",msg.data.c_str());
    return 0;
}

From a shell, I started recording messages sent to topic /end :

rosbag record -O end /end 

After sending of the messages, I analyzed bag file and I didn't find anything inside it. Do you have any idea why this happens? I tried also to substitute the while in the code with while(1) but no messages is received as well.

Thanks for support.


Originally posted by Jaco on ROS Answers with karma: 1 on 2012-12-19

Post score: 0


Original comments

Comment by Jaco on 2012-12-19:
thanks for answer. code fortunately compiles, it was just a copy-paste error. anyway, your answer was helpful. it works. after that, i played two bag files by writing rosbag play subset.bag end.bag. the execution of first bag file should last 3.45 mins but it seems not to finish. do you know reason?

Comment by Jaco on 2012-12-19:
... moreover i can't see the usual messages count down on screen, as it happens when i play bag files one by one

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

I doubt that the code above would compile. Your advertise call specifies type std_msgs::string, the correct type is std_msgs::String though. Please read the support guidelines before posting. Always copy-paste code and tag your questions.

You publish before rosbag or any other node could register. A quick fix would be either to use a latched topic or publish right after the while loop that checks for the number of publishers to be 0.

To use a latched topic, use the following advertise call:

ros::Publisher chatter_pub = n.advertise<std_msgs::String>("/end", 1000, true);

Originally posted by Lorenz with karma: 22731 on 2012-12-19

This answer was ACCEPTED on the original site

Post score: 0

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.