I would like to publish velocities for my ARDrone using /cmd_vel
topic using the below python script. But it does nothing. It does not publish the required information.
What is wrong in the below code?
#!/usr/bin/env python3
import numpy as np
import rospy
from geometry_msgs.msg import Twist
class KeyboardControl:
def __init__(self):
rospy.init_node('Script_controlling_ARDrone', anonymous=False)
publisher = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
twist = Twist()
twist.linear.x = 0
twist.linear.y = 0
twist.linear.z = -1
twist.angular.x = 0
twist.angular.y = 0
twist.angular.z = 0.5
publisher.publish(twist)
def main():
try:
kc = KeyboardControl()
rospy.spin()
except KeyboardInterrupt:
rospy.loginfo("Shutting down")
if __name__ == '__main__':
main()
This does actually do something, however it's probably not noticeable. This is because it only publishes something once when the object is created. If you would like you publish periodically you should use a main run loop like so:
import numpy as np
import rospy
from geometry_msgs.msg import Twist
class KeyboardControl:
def __init__(self):
rospy.init_node('Script_controlling_ARDrone', anonymous=False)
self.pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
self.rate = rospy.Rate(10) #10Hz
def main():
try:
kc = KeyboardControl()
twist = Twist()
twist.linear.x = 0
twist.linear.y = 0
twist.linear.z = -1
twist.angular.x = 0
twist.angular.y = 0
twist.angular.z = 0.5
while not rospy.is_shutdown():
#Do any needed edits to the twist message here
kc.pub.publish(twist)
kc.rate.sleep()
except KeyboardInterrupt:
rospy.loginfo("Shutting down")
if __name__ == '__main__':
main()