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

how to send empty message?

asked 2013-01-22 02:29:55 -0600

updated 2013-01-22 04:04:26 -0600

KruseT gravatar image

Hey,

I'm working with an Ar Drone and i would like to send from my node a command to the helicopter to take off. The command in terminal is given(works): rostopic pub /ardrone/takeoff std_msgs/Empty

I try to send an empty msg from my node but the drone does not take off and i do not get an echo of the msg. What am I missing? or what is wrong with my code? My code is:

#include <std_msgs/Empty.h>

int main(int argc, char **argv){

init(argc, argv, "takeoff_fly");
Nodehandle n;
std_msg::Empty myMsg;
Publisher takeOff=n.advertise<std_msgs::Empty>("/ardrone/takeoff",1);
takeOff.publish(myMsg);
spinOnce();

return 0;
}
edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
6

answered 2013-01-22 03:55:17 -0600

Try sleeping a bit before sending the message, or sending the message multiple times. This is happening because your publisher probably hasn't finished registering itself when you send the message. In this particular situation though, it might make more sense to do this as an action or a service, which both have ways of ensuring the other end is connected, and allow the other end to send feedback.

edit flag offensive delete link more

Comments

1

Definitely sounds like a case for a service/action!

dornhege gravatar image dornhege  ( 2013-01-22 06:16:34 -0600 )edit

+1 for service/action

Martin Peris gravatar image Martin Peris  ( 2013-01-22 12:16:36 -0600 )edit

Is there any way to know the status of takeoff action?.

saikishor gravatar image saikishor  ( 2017-05-05 14:16:40 -0600 )edit
0

answered 2013-01-22 05:34:55 -0600

updated 2013-02-26 22:49:41 -0600

I got it, It must be used the latch parameter of the Publisher class. The 3rd param is the latch, by default it is false, if it is true, than the msg has a transitory stat/ not instant.

Publisher takeOff=n.advertise<std_msgs::Empty>("/ardrone/takeoff",1,true);

Or:

 system( "rostopic pub /ardrone/land std_msgs/Empty");
edit flag offensive delete link more

Comments

1

This is not guaranteed to work. It probably only works because the latched one discovers your node when starting in your tests.

dornhege gravatar image dornhege  ( 2013-01-22 06:15:59 -0600 )edit

Imho latching should always work for this case. It should "guarantee" that every subscriber receives the latest published message after connecting. @dornhege: in which case do you think would that not work?

Dirk Thomas gravatar image Dirk Thomas  ( 2013-01-23 15:19:53 -0600 )edit

The problem is that the program just quits. I.e. there might not be any subscriber, yet.

dornhege gravatar image dornhege  ( 2013-01-23 23:13:37 -0600 )edit

Oh I see what do you mean, however this program is just an example. The take off and landing command is part of a bigger project, where the helicopter driver is running before my application starts. So there 'll be a listener all the time, but thanks for the help

zweistein gravatar image zweistein  ( 2013-02-26 22:46:26 -0600 )edit
0

answered 2013-01-22 06:11:49 -0600

dornhege gravatar image

updated 2013-01-22 23:20:21 -0600

You should wait until pub.getNumSubcribers() > 0 in a while loop before you send your message.

edit flag offensive delete link more

Comments

1

I agree with dornhege, but I would go a bit further: Instead of looping before exiting the program, I would loop before sending the message.

Martin Peris gravatar image Martin Peris  ( 2013-01-22 12:15:39 -0600 )edit

Thanks, this is exactly what I wanted to say and the correct way to wait!

dornhege gravatar image dornhege  ( 2013-01-22 23:19:18 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2013-01-22 02:29:55 -0600

Seen: 15,573 times

Last updated: Feb 26 '13