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

Callback for a Ardrone Navdata Subscriber doesn't work

asked 2019-06-18 11:49:52 -0600

Cipek gravatar image

updated 2019-06-18 15:28:43 -0600

I want to track the state of the AR Drone. For that, I have created a Subscriber, but for some reason, it doesn't go into my callback function. This is my code:

#include "ros/ros.h"
#include "std_msgs/Empty.h"
#include "std_msgs/String.h"
#include <iostream>
#include <ardrone_autonomy/Navdata.h>
using namespace std;
using namespace ros;
using namespace std_msgs;

//from ardrone_autonomy.msg import Navdata

ardrone_autonomy::Navdata data;
int state;

void receiveNavdata(const ardrone_autonomy::Navdata& data)
    cout<<"\nIN NAV"<<endl;
    cout<<"\nState: "<< data.state<<endl;   

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

  init(argc, argv, "talker");
    NodeHandle rospy;

    Subscriber navData = rospy.subscribe("/ardrone/navdata", 1, receiveNavdata);

while( state == -1){

Publisher takeoff = rospy.advertise<Empty>("/ardrone/takeoff", 1);

while(takeoff.getNumSubscribers() < 1){



    return 0;
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2019-06-18 12:10:29 -0600

jayess gravatar image

updated 2019-06-18 12:13:38 -0600

This node is going to finish after just over 10 seconds. You have no spin to call the callbacks. Your program will wait for 10 seconds then finish. This may not be enough time for the AR.Drone to publish anything. You need to add a spin so that the callback will continue to be called. See the roscpp subscriber tutorial.

edit flag offensive delete link more


You were right that spin(), but now I need to create a system which waits until the state is defined and do something. I wanted to do that: while(state == -1) ros::spin(); but spin() runs runs forever anyways. Is there sth similar to spin() that can be used for that?

Cipek gravatar image Cipek  ( 2019-06-18 12:53:07 -0600 )edit

spin blocks continuously until the shutdown signal is received so you may want to use spinOnce instead along with a sleep

jayess gravatar image jayess  ( 2019-06-18 12:58:48 -0600 )edit

it helped but now it blocks here while(takeoff.getNumSubcsribers() < 1){...}, but it doesn't when I do not do spinOnce before it.

Cipek gravatar image Cipek  ( 2019-06-18 13:06:51 -0600 )edit

That's not in the code that's in your question. Can you please update your question with the code that you're actually using?

jayess gravatar image jayess  ( 2019-06-18 14:08:46 -0600 )edit

I edited it. The problem is that it never prints out "take off". It stays in the while loop just before the cout. But as I said, it works if there is no spinOnce at the beginning.

Cipek gravatar image Cipek  ( 2019-06-18 15:09:01 -0600 )edit

Did you try outputting to the console between the loops and/or during the second loop? Maybe the /ardrone/takeoff never gets any subscribers.

jayess gravatar image jayess  ( 2019-06-20 03:27:23 -0600 )edit

Question Tools



Asked: 2019-06-18 11:49:52 -0600

Seen: 168 times

Last updated: Jun 18 '19