Robotics StackExchange | Archived questions

Callback for a Ardrone Navdata Subscriber doesn't work

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;   
    state=data.state;   
}

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

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

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


while( state == -1){
    ros::spinOnce();
    ros::Duration(0.1).sleep();
}   

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

while(takeoff.getNumSubscribers() < 1){
    ros::Duration(0.1).sleep();
}

cout<<"\nTakeOff"<<endl;

takeoff.publish(empty);
ros::Duration(5).sleep();

    return 0;
}

Asked by Cipek on 2019-06-18 11:49:52 UTC

Comments

Answers

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.

Asked by jayess on 2019-06-18 12:10:29 UTC

Comments

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?

Asked by Cipek on 2019-06-18 12:53:07 UTC

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

Asked by jayess on 2019-06-18 12:58:48 UTC

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

Asked by Cipek on 2019-06-18 13:06:51 UTC

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?

Asked by jayess on 2019-06-18 14:08:46 UTC

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.

Asked by Cipek on 2019-06-18 15:09:01 UTC

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

Asked by jayess on 2019-06-20 03:27:23 UTC