Writing a Publisher and Subscriber with a Custom Message(Python) Tutorial Not Working
I am trying to follow the tutorial as detailed in this link: (http://wiki.ros.org/ROS/Tutorials/Cus...)
I am using ROS Kinetic on ubuntu 16.04.
I was able to build the Person messages fine without errors and using rosmsg show command. ROS was able to find the message and show the following as output:
string name
int32 age
I created two different packages to run it. One is called the quadrotor_receive node while the other is called the transmit_thrust node. The transmit_thrust runs the custom_talker.py script while the quadrotor_receive runs the custom_listener.py.
I am having the following errors trying to run the custom_listener.py by itself. Below is my script on the quadrotor_receive node.
#!/usr/bin/env python
import rospy
from quadrotor_receive.msg import Person
def callback(data):
rospy.loginfo("%s is age: %d" % (data.name, data.age))
def listener():
rospy.init_node('custom_listener', anonymous=True)
rospy.Subscriber("custom_chatter", Person , callback)
# spin() simply keeps python from exiting until this node is stopped
rospy.spin()
if __name__=='__main__':
listener()
When I tried to run the above code I get the error
Traceback (most recent call last):
File "/home/ted/catkin_ws/src/quadrotor_receive/scripts/custom_listener.py", line 4, in <module> from quadrotor_receive.msg import Person ImportError: No module named quadrotor_receive.msg
I also tried to replace the line:
from quadrotor_receive.msg import Person
with the following:
import quadrotor_receive.msg
I then get the following error:
Traceback (most recent call last):
File "/home/ted/catkin_ws/src/quadrotor_receive/scripts/custom_listener.py", line 17, in <module> listener() File "/home/ted/catkin_ws/src/quadrotor_receive/scripts/custom_listener.py", line 11, in listener rospy.Subscriber("custom_chatter", Person, callback) NameError: global name 'Person' is not defined
So what can I do to make the code run? What did I do wrong?
Can you post your CMakeLists.txt and package.xml?
Just to check have you got three different packages with a single node in each? For a tutorial we would recommend keeping everything in a single package. If the custom message has been defined in a different package you will need an additional dependency in your CMakeLists.txt file to link to it.