ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
I believe your issue is that you're creating Publisher
inside the callback function that handles your keypress. That is not going to work: setting up Publisher
s and getting Subscriber
s to notice them and subscribe to them is going to take some time.
You'll want to treat Publisher
s and Subscriber
s as persistent objects, that you create once, somewhere in the initialisation phase of your program, and then use inside your callback.
Second: never include any sort of blocking while
inside a callback: callbacks like this are supposed to quickly process an event and then return. Including a loop of any kind will make that impossible, and may even block your entire program. With Python this is sometimes hard to detect / notice, as it may spawn N
threads to process these things. In your case, that may lead to N
instances of Publisher
, all publish
ing Empty
msgs at 10 Hz to the bebop/takeoff
, even after you've let go of the key.
My advise would be to structure your code similar to how teleop_twist_key.py
is setup already, but making sure that the Empty
msg gets published by your Publisher
when the key you'd like to use is pressed. You'll have to make sure that the code doesn't get to this section if your keys are pressed, because otherwise it will also publish
a Twist
msg, which is probably not what you want (or it could be, if you'd like to exploit the "publish all-zeros Twist
if I haven't pressed any key" behaviour).
2 | No.2 Revision |
Edit: I think the suggestion by @jayess is actually a very good one: the repository he links you to contains a good example of a proper keyboard teleop / controller node that you could very likely use with your specific drone as well. See mikehamer/ardrone_tutorials/src/keyboard_controller.py.
I believe your issue is that you're creating Publisher
inside the callback function that handles your keypress. That is not going to work: setting up Publisher
s and getting Subscriber
s to notice them and subscribe to them is going to take some time.
You'll want to treat Publisher
s and Subscriber
s as persistent objects, that you create once, somewhere in the initialisation phase of your program, and then use inside your callback.
Second: never include any sort of blocking while
inside a callback: callbacks like this are supposed to quickly process an event and then return. Including a loop of any kind will make that impossible, and may even block your entire program. With Python this is sometimes hard to detect / notice, as it may spawn N
threads to process these things. In your case, that may lead to N
instances of Publisher
, all publish
ing Empty
msgs at 10 Hz to the bebop/takeoff
, even after you've let go of the key.
My advise would be to structure your code similar to how teleop_twist_key.py
is setup already, but making sure that the Empty
msg gets published by your Publisher
when the key you'd like to use is pressed. You'll have to make sure that the code doesn't get to this section if your keys are pressed, because otherwise it will also publish
a Twist
msg, which is probably not what you want (or it could be, if you'd like to exploit the "publish all-zeros Twist
if I haven't pressed any key" behaviour).