Hello,
This thread was also helpful to me. However I have a follow-up question:
I'm using Python to implement a navigation interface between the ROS nav stack and my own motor controllers. I have created a Driver object that subscribes to the 'cmd_vel' topic and receives Twist messages from this topic. My callback function from the subscriber determines motor commands based on the info in each Twist message it receives.
A confusing problem I've found is that I need to hand my callback function not just the Twist message but also the 'self' object, so that the callback function has access to some global parameters associated with the Driver object. I cannot find a way to pass both pieces of information to the callback function when it's called within the Subscriber command.
Code:
class Driver:
def __init__(self):
# node initialized and constants described here
def subscribe_cmd_vel(self):
rospy.Subscriber("cmd_vel", Twist, self.callback(self))
rospy.spin()
def callback(self, msg):
print "Linear x of Twist message: " + str(msg.linear.x)
# do some calculations with contents of msg
if __name__ == "__main__"
dr = Driver()
dr.subscribe_cmd_vel()
(end code)
I am consistently only passing the self object to my callback, not the Twist message. When this code hits the print line in the callback function, I get an error saying something like "The Driver object has no 'linear' field.". This means that my msg pointer is being pointed to the self object and my actual Twist message is being lost. Please tell me if you know what mistakes I've made here.
Many thanks,
Khiya
Thanks, that's basically what I've discovered. I had just never heard of it before and they're incredibly useful.