Important announcement

Dear visitor,

Please note that this OpenCourseWare platform will be replaced at the end of 2026 by a new platform (under the same url ocw.tudelft.nl).

Courses and their contents that are currently available the platform will partly be migrated. URL’s for this content will change and content might be unavailable for some time. In addition, some courses will not be available anymore.

If you are linking to specific courses on this platform (for instance for your students), please take timely measures to avoid problems with access. Possible actions are:

  • Download the materials to your local platform (laptop, server)
  • For educators only (including TU Delft lecturers): Contact us at ocw@tudelft.nl and inform us what course you are using in what context (institute, program, period of use, number of students). We will research (in collaboration with the authors of the course) and decide if we will migrate the course, and if yes, can supply you with a new url.

Thank you.

1.2.8 Subscriber ROS node with a custom message type

Course subject(s) Module 1. ROS Essentials

This lecture provides a step by a step guide to subscribe to a topic.

Subscriber node

Summary

Start a new CCS and source the workspace setup files.

Use the template and start editing it. We will first create a new file, and copy the contents of the example file into it.
$ roscd hrwros_week1/scripts
$ touch sensor_info_subscriber.py
$ cp template_subscriber_script.py sensor_info_subscriber.py

Next, we will take a look inside our new file. Open it in your favourite editor, and follow along with the lines we indicate. #L16 means line 16.

Import sensor information
#L36: from hrwros_msgs.msg import SensorInformation

Then give a meaningful names and log message (#L41,42,49)
#L51: rospy.init_node('sensor_info_subscriber', anonymous=False)
#L53: rospy.Subscriber('sensor_info', SensorInformation, sensorInfoCallback)
#L59: sensorInfoListener()

Make the subscriber script executable
chmod +x sensor_info_subscriber.py

To run our new subscriber node:
Run roscore in a new course command shell. Remember you need to source your workspace in every new shell!
$ source $HOME/hrwros_ws/devel/setup.bash
$ roscore
Then, in a separate CCS:
$ source $HOME/hrwros_ws/devel/setup.bash
$ rosrun hrwros_week1 sensor_info_subscriber.py

Check with rostopic list:
$ rostopic info /sensor_info

Note: Inspect the topic with the rostopic info when debugging your ROS problems.

To actually see the contents of the topic, you will need to publish it from a new CCS.
$ rosrun hwros_week1 sensor_info_publisher.py

Creative Commons License
Hello (Real) World with ROS - Robot Operating System by TU Delft OpenCourseWare is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Based on a work at https://online-learning.tudelft.nl/courses/hello-real-world-with-ros-robot-operating-systems//.
Back to top
[sg_popup id=6]