A practical introduction to ROS 2 for Python developers: nodes, topics, services, launch files and simulation, with a minimal rclpy publisher node.
Writing software for a robot is different from writing a web application. A robot has many things happening at once: cameras streaming images, motors reporting their positions, a planner deciding where to go, a safety monitor watching for obstacles. If each of these is written as part of one large program, it quickly becomes impossible to test, reuse or debug.
The Robot Operating System 2 (ROS 2) addresses this. Despite the name, it is not an operating system but an open-source framework and set of tools for building robot software as many small, communicating programs. It is widely used in universities, research labs and companies building mobile robots, drones and robot arms. For Python developers, the rclpy client library makes it approachable. This article explains the core concepts and shows a first working node.
Why ROS 2, and how to set it up
ROS 2 is the successor to the original ROS. It was redesigned to use DDS (Data Distribution Service) for communication, which brings better support for real-time systems, multiple robots on one network, security features and production use. New projects should start with ROS 2.
ROS 2 is released as named distributions, some of which are long-term support (LTS) releases maintained for several years. Each distribution targets specific operating system versions, with Ubuntu Linux being the primary platform. When you start, pick the current LTS distribution, check which Ubuntu version it supports, and follow the official installation guide for that combination. Mixing versions is the most common source of setup problems.
Once installed, you source the setup script in each terminal so the ros2 command-line tool and libraries are available, then create a workspace and build packages with colcon.
Nodes: the building blocks
A node is a single process (or part of a process) with one clear responsibility: reading a lidar, estimating the robot's position, controlling the wheels. Nodes are organised into packages, which are the unit you build, share and install.
Keeping nodes small has practical benefits. You can restart one without stopping the robot, replace a real sensor driver with a simulated one, and write nodes in different languages, typically Python for high-level logic and C++ (with rclcpp) for performance-sensitive parts. They all communicate in the same way.
Topics: publishers and subscribers
Most data in ROS 2 flows over topics. A topic is a named channel such as /scan or /cmd_vel with a fixed message type. Nodes publish messages to a topic, and any number of nodes can subscribe to it. Publishers and subscribers do not need to know about each other, which keeps the system loosely coupled.
Message types are defined in interface packages. std_msgs contains simple types such as String, while sensor_msgs and geometry_msgs contain the standard types for sensor data and motion. Using the standard types where possible lets your nodes work with existing tools and drivers.
Here is a minimal publisher node in Python. It publishes a text message on the chatter topic twice a second:
import rclpy
from rclpy.node import Node
from std_msgs.msg import String
class Talker(Node):
def __init__(self):
super().__init__("talker")
self.publisher = self.create_publisher(String, "chatter", 10)
self.count = 0
self.timer = self.create_timer(0.5, self.publish_message)
def publish_message(self):
msg = String()
msg.data = f"Hello from ROS 2: {self.count}"
self.publisher.publish(msg)
self.get_logger().info(f"Publishing: {msg.data}")
self.count += 1
def main(args=None):
rclpy.init(args=args)
node = Talker()
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()
The 10 in create_publisher is the queue depth, part of the quality-of-service (QoS) settings that control how messages are buffered and delivered. rclpy.spin keeps the node running so its timer callbacks fire. After registering main as an entry point in your package's setup.py and building with colcon build, you can run it with ros2 run <package_name> <executable_name> and watch the messages from another terminal with ros2 topic echo /chatter.
A subscriber looks very similar: it calls create_subscription(String, "chatter", self.callback, 10) and handles each message in its callback.
Services and actions
Topics suit continuous streams of data. For request-and-response interactions, ROS 2 provides services. A client sends a request and the server returns a single response, for example "reset the odometry" or "what is the current battery level?". Services should be quick; they are not meant for tasks that take a long time.
For long-running goals, such as "navigate to the loading bay", ROS 2 has actions. An action client sends a goal, receives regular feedback (distance remaining, for instance) and can cancel the goal before it finishes. Understanding when to use each of these three patterns is one of the most useful early lessons:
- Topic: continuous data, many listeners, no reply expected.
- Service: a short request with one reply.
- Action: a long task with feedback and the option to cancel.
ROS 2 also has parameters, which let you configure a node (a speed limit, a sensor's frame name) without changing its code.
Launch files: starting a whole system
A real robot may run dozens of nodes. Starting each one by hand is impractical, so ROS 2 uses launch files, commonly written in Python, though XML and YAML are also supported. A launch file describes which nodes to start, with which parameters, topic remappings and namespaces. You run it with ros2 launch <package_name> <file_name>.
Launch files also make it easy to switch configurations, for example starting simulated sensors in development and real drivers on the robot, controlled by a launch argument.
Simulation with Gazebo
You do not need a physical robot to start. Gazebo is the simulator most closely associated with ROS. It models physics, sensors such as cameras and lidar, and the robot itself, described in URDF or SDF files. Through bridge packages, the simulated robot publishes and subscribes on the same ROS 2 topics as a real one, so your nodes run unchanged.
Note that there are two lines of Gazebo: the older "Gazebo Classic" and the newer Gazebo (previously called Ignition). Tutorials differ depending on which they use, so check which version matches your ROS 2 distribution. Alongside the simulator, RViz visualises what the robot "sees" and thinks, and ros2 bag records topic data so you can replay and analyse it later.
A sensible learning path is: run the demo talker and listener, write your own publisher and subscriber, add a service, write a launch file, then drive a simulated mobile robot in Gazebo using the Nav2 navigation stack.
How NextWebGen can help
NextWebGen is a software company, not a robot manufacturer, but robot projects need a lot of conventional software around them: web dashboards that show fleet status, APIs that pass data between ROS-based systems and business applications, and servers to store logs and sensor recordings. If your team needs that layer built or the infrastructure managed, see our server management services.



