ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Writing a Publisher and Subscriber with a Custom Message(Python) Tutorial Not Working

asked 2019-09-24 09:14:51 -0500

haloted gravatar image

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?

edit retag flag offensive close merge delete

Comments

Can you post your CMakeLists.txt and package.xml?

LeoE gravatar image LeoE  ( 2019-09-24 09:50:30 -0500 )edit

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.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2019-09-24 10:41:58 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2019-09-24 11:48:40 -0500

haloted gravatar image

Thanks for replies, I solved my problem by following this tutorial: Custom Message What I did differently is add the <build export="" dependencies=""> 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.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2019-09-24 09:14:51 -0500

Seen: 2,127 times

Last updated: Sep 24 '19