1.2 First ROS2 node

1.2 First ROS2 node#

As you’ve seen in the presentation, one of the core elements of ROS2 are nodes. These are just small executables that connect with the ROS2 API. In this tutorial you will learn how to create your first ROS node. In VSCode you can open a new file and save the following code as my_node.py:

 1import rclpy
 2from rclpy.node import Node
 3
 4def main():
 5  rclpy.init()
 6  my_node = Node('hello_world_node')
 7  my_node.get_logger().info("Hello world")
 8  try:
 9     rclpy.spin(my_node)
10  except KeyboardInterrupt:
11     return
12
13if __name__ == '__main__':
14  main()

By pressing play in the top right, you should see the string ‘Hello world’ being printed on the screen once.

info

Pressing play is similar to running the following command:

$ python3 my_node.py

If you did it correctly the node is still spinning, so the terminal is still busy. You can check that the node is indeed running by opening another terminal:

/my_first_node
$ ros2 node list

This should give you the name of the node (‘hello_world_node’) as set in line 6. Congratulations, you have your first ROS2 node running. But this is not the best way of doing this.

Help
  • I get the error ‘No module named rclpy’:
    • Please make sure that you have installed ros-dev-tools.

  • I get the error ‘ros2 not found’:
    • Make sure that you have sourced ROS2.