Here Area is the double variable which I need to publish it over the topic /auc. I am not able to build this file as don't know how to enter the Area variable into the areaValue.

网友答案:

If you look at the documentation for std_msgs::Float64, it shows that it contains a single data field, which is called data and will be of type double in C++.

So, in your code, you just do:

areaValue.data = Area

assuming that Area is a double.

I suggest that you take a look at the basic Writing a simple Publisher and Subscriber tutorial on the ROS wiki.

EDIT

If what is in the original post is the entirety of your code, then it's probably not going to do exactly what you think it does. If you use the default constructor for a publisher, messages published on them are emitted once immediately. Any nodes subscribed to that topic at the moment of publishing will get the message, and then the topic will be clear. If you want any node that subscribes to the topic to receive the last message published on it, do the following:

That last bool true tells the publisher that it should latch messages that are published.

But you have yet another problem, presuming this is all your code: You never spin, so your node starts up, advertises its topic, publishes one message on it, and then shuts down, taking the topic with it (assuming nothing was subscribed to the topic). You need to add the following before the end of your main:

ros::spin();

This will keep the node active (and thus the topic alive) until ros::ok() returns false, I.E. until the node is killed.

Of course, your message is still only going to be published once, but the topic will at least stay alive. If you want the message to be broadcast periodically, use a ros::Timer and put the pulish call inside the timer's callback.

But seriously, please read the tutorials, they'll walk you through all of this stuff.

网友答案:

I finally found the solution for the problem. I had multiple ros:spinOnce() in the code. And also in the snippet

The publisher Auc_pub was created and destroyed ( As I had included the publisher in a function... my bad). Instead, i included the publisher in the main function where the publisher is created only once and stays alive until the execution completes. And, thanks to @aruisdante your suggestion helped.