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

Callback for a Ardrone Navdata Subscriber doesn't work

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

Cipek gravatar image

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

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;
}
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

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

jayess gravatar image

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

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

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?

Cipek gravatar image Cipek  ( 2019-06-18 12:53:07 -0500 )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 -0500 )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 -0500 )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 -0500 )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 -0500 )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 -0500 )edit

Question Tools

2 followers

Stats

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

Seen: 173 times

Last updated: Jun 18 '19