0
$\begingroup$

Rosanswers logo

I am trying to follow the tutorial as detailed in this link: (http://wiki.ros.org/ROS/Tutorials/CustomMessagePublisherSubscriber%28python%29)

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 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 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?


Originally posted by haloted on ROS Answers with karma: 32 on 2019-09-24

Post score: 0


Original comments

Comment by LeoE on 2019-09-24:
Can you post your CMakeLists.txt and package.xml?

Comment by PeteBlackerThe3rd on 2019-09-24:\

I created two different packages to run it

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.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Thanks for replies, I solved my problem by following this tutorial: Custom Message What I did differently is add the line in the package.xml Also in CMakelist.txt I was being smart by uncommenting all the lines here instead of just leaving one line uncommented:

 catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES transmit_thrust
  CATKIN_DEPENDS message_runtime roscpp rospy std_msgs
#  DEPENDS system_lib
)

After changing these files I used just catkin_make at the roof directory omitting the install keyword at the end. And things worked.


Originally posted by haloted with karma: 32 on 2019-09-24

This answer was ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.