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

Python script for controlling the AR Drone doesn't work

asked 2019-06-12 08:54:51 -0500

Cipek gravatar image

I have written a simple python script to takeoff the drone.

#! /usr/bin/env python
import rospy
from std_msgs.msg import Empty

rospy.init_node('cipek')

empty = Empty()
takeoff = rospy.Publisher('/ardrone/takeoff', Empty, queue_size=1)
land = rospy.Publisher('/ardrone/land', Empty, queue_size=1)

takeoff.publish(empty)
print("works")

I have created a package with catkin. In one terminal I run roscore, in the other rosrun ardrone_autonomy ardrone_driver and in third rosrun my_package script.py. The python script is runnable (I have run chmod +x on it), and it works because it print out "works". The problem is that the drone doesn't react at all. However, when I run rostopic pub ardrone/takeoff std_msgs/empty "{}" --once in the console it works perfectly. Any ideas what is wrong?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-06-12 09:01:16 -0500

gvdhoorn gravatar image

updated 2019-06-12 10:18:55 -0500

Any ideas what is wrong?

your script terminates before ROS has any chance of getting your message to any potential subscribers. Setting up subscriptions takes time.

It would be best to wait for subscribers to have connected using get_num_connections() and only publish(..) when there is at least a single subscriber.

After that, wait a little and only then exit the script.


Edit: I would add a sleep in there, just so we don't hog the CPU while waiting for a subscriber:

while takeoff.get_num_connections() < 1:
    rospy.loginfo_throttle(2, "Waiting for subscribers on /ardrone/takeoff ..")
    rospy.sleep(0.1)
takeoff.publish(empty)

 # give subscriber 2 seconds to receive the message (this much time is not needed,
 # but just to be on the safe side)
 rospy.sleep(2.0)

Notice also the rospy.loginfo_throttle(..) to inform the user about what is going on.

edit flag offensive delete link more

Comments

Could you modify my code so it works as you said? I want to make sure I have it correctly.

Cipek gravatar image Cipek  ( 2019-06-12 09:04:32 -0500 )edit

Nevermind. Got it running with: while takeoff.get_num_connections() >= 1: pass takeoff.publish(empty)

Cipek gravatar image Cipek  ( 2019-06-12 09:13:02 -0500 )edit

Should I do a similar loop before every command, like fly forward, land, etc?

Cipek gravatar image Cipek  ( 2019-06-12 10:58:13 -0500 )edit

No, not necessarily.

Right now you're using a time-based approach. What would be better would be a state-based approach: send a command and then, instead of assuming it will be executed, verify that is has been executed by checking the state of the system you're trying to control.

It's unfortunate the ardrone interface is topic based for these things, as topics don't guarantee delivery or processing of messages, but you'll have to make do, as that is what it is.

gvdhoorn gravatar image gvdhoorn  ( 2019-06-12 11:18:31 -0500 )edit

Do you know how to check the state of the drone?

Cipek gravatar image Cipek  ( 2019-06-12 11:20:04 -0500 )edit

I have almost zero experience with drones, much less with ardrones, so I wouldn't know.

gvdhoorn gravatar image gvdhoorn  ( 2019-06-12 11:21:21 -0500 )edit

Okay. No worries. I'll do some research then.

Cipek gravatar image Cipek  ( 2019-06-12 12:41:15 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2019-06-12 08:54:51 -0500

Seen: 878 times

Last updated: Jun 12 '19