Tag: ROS2 Publisher Subscriber

  • ROS2 Basics (C++) – Basic ROS2

    This comprehensive 4-month self-study course is meticulously designed to guide motivated beginners and intermediate learners through the fundamentals of ROS2 (Robot Operating System 2) using C++. If you’re looking to build a strong foundation in modern robotics, mastering these ROS2 basics is your essential first step. You will acquire the practical, high-performance skills necessary for developing robust and scalable robotic applications. The curriculum spans from understanding core ROS2 concepts to implementing advanced communication patterns and building complete robotic system simulations. Through engaging lessons, clear explanations, and hands-on examples, you will confidently master the tools and techniques required to work effectively with ROS2 across various robotics domains, from autonomous vehicles to industrial automation.

    Primary Learning Objectives

    Upon successful completion of this course, you will be able to:

    Understand the fundamental architecture and core concepts of ROS2.
    Develop efficient and clean ROS2 nodes in C++ for diverse robotic functionalities.
    Effectively utilize ROS2 communication mechanisms: topics, services, actions, and parameters.
    Create and manage ROS2 packages and workspaces with professional workflows.
    Debug and introspect ROS2 applications using command-line tools and RViz.
    Integrate real-world hardware components seamlessly with ROS2.
    Simulate complex robotic systems using Gazebo and URDF for realistic testing.
    Apply ROS2 principles to solve practical, real-world robotics challenges.

    Necessary Materials

    A computer with a Linux distribution (Ubuntu 22.04 LTS is highly recommended).
    A reliable internet connection for downloading ROS2 and related software packages.
    A foundational understanding of C++ programming (variables, loops, functions, classes).
    Basic familiarity with navigating the Linux command line.
    Optional: A physical robot or hardware platform for real-world application (e.g., TurtleBot3, Raspberry Pi with sensors, Arduino). Simulations will be provided, so a physical robot is not mandatory to start.

    Month 1: Getting Started with ROS2 Basics

    Lesson 1: Introduction to ROS2 and Setting Up Your Environment

    Learning Objectives: Understand what ROS2 is and its significance in robotics. Learn the key differences between ROS1 and ROS2. Set up a functional ROS2 development environment.
    Key Vocabulary:
    ROS2: Robot Operating System 2, an open-source middleware framework for robotics.
    DDS (Data Distribution Service): The industrial-grade, real-time middleware that powers ROS2’s communication layer, providing enhanced performance and security.
    Workspace: A dedicated directory that contains your ROS2 packages.
    Source: The command used to load the ROS2 environment variables into your terminal session.

    ROS2 provides a flexible framework for writing robot software. It’s not an operating system in the traditional sense, but a comprehensive collection of tools, libraries, and conventions designed to simplify the creation of complex and robust robot behaviors. A key differentiator from its predecessor, ROS1, is ROS2’s foundation on DDS. This isn’t just a minor update; it fundamentally improves real-time performance, introduces robust security features, and enables more complex multi-robot capabilities right out of the box.

    Setting up your ROS2 environment is the crucial first step. We will focus on installing a Long-Term Support (LTS) distribution like ROS2 Humble Hawksbill on Ubuntu 22.04. This ensures stability and community support for your projects. Once installed, you will learn to source your ROS2 setup file, a command that makes all the powerful ROS2 tools and libraries accessible in your terminal. This lesson provides a detailed walkthrough to ensure a smooth and correct installation.

    Hands-on Example: Install ROS2 Humble Hawksbill on your Ubuntu system by following the official documentation. Source your environment and verify the installation by running `ros2 –version` and the diagnostic tool `ros2 doctor`.

    Lesson 2: Your First ROS2 C++ Node – Publishers and Subscribers

    Learning Objectives: Understand the core concept of a ROS2 node. Create a basic C++ ROS2 publisher node. Create a basic C++ ROS2 subscriber node. Grasp the function of ROS2 topics.
    Key Vocabulary:
    Node: An executable process within ROS2 that performs a specific, modular task.
    Topic: A named communication bus over which nodes exchange messages.
    Publisher: A node that sends messages on a specific topic.
    Subscriber: A node that receives messages from a topic.
    Message: A typed data structure for exchanging information between nodes.

    At the heart of ROS2’s architecture are nodes. Think of each node as a small, specialized worker. One node might control a wheel motor, another might process camera data, and a third could handle navigation logic. These nodes communicate through topics. Imagine a topic as a public radio channel: a publisher node broadcasts messages on a specific channel, and any number of subscriber nodes can tune in to receive that broadcast. This publish-subscribe model is one of the most important ROS2 basics because it creates a decoupled and modular system, allowing different components to be developed, tested, and run independently.

    In this lesson, you will create two simple C++ nodes: a talker that publishes a Hello, World string message to a topic, and a listener that subscribes to that topic and prints the message. This exercise introduces the fundamental structure of a ROS2 C++ node, including header files, ROS2 initialization (`rclcpp::init`), creating publishers and subscribers, and spinning the node with `rclcpp::spin()` to process incoming data.

    Hands-on Example: Create a new ROS2 package. Inside it, develop a C++ publisher node (`talker.cpp`) publishing `std_msgs::msg::String` messages to a `/chatter` topic. Then, create a C++ subscriber node (`listener.cpp`) that listens to `/chatter` and prints the messages. Build your workspace using `colcon build` and run both nodes to observe the real-time communication.

    Lesson 3: ROS2 Services and Actions

    Learning Objectives: Understand ROS2 services for request/response communication. Create a C++ ROS2 service server and client. Understand ROS2 actions for managing long-running, goal-oriented tasks. Create a C++ ROS2 action server and client.
    Key Vocabulary:
    Service: A synchronous request-response communication mechanism.
    Service Server: A node that provides a specific service.
    Service Client: A node that requests and waits for a response from a service.
    Action: An asynchronous, long-running communication mechanism with feedback and cancellation.
    Action Server: A node that provides and manages an action.
    Action Client: A node that requests an action and receives ongoing feedback and a final result.

    While topics are great for continuous data streams, sometimes you need a different communication pattern. A ROS2 service is like a vending machine: you make a single request (insert money, press a button) and wait for a single, immediate response (get a snack). It’s a synchronous, blocking call perfect for quick queries like What is the current battery voltage?.

    In contrast, a ROS2 action is like ordering a pizza delivery. You send a goal (order a large pepperoni), receive periodic updates on the progress (feedback: the order is being prepared, it’s in the oven), and finally get a definitive result (the pizza arrives). You can also cancel the order mid-process. Actions are ideal for long-running tasks like navigating to a location or executing a multi-step robotic arm movement. Understanding when to use topics, services, or actions is fundamental to designing clean and effective robotic systems.

    * Hands-on Example: Define a custom service (`AddTwoInts.srv`). Implement a C++ service server that takes two integers and returns their sum. Create a client node that calls this service and prints the result. This will solidify your understanding of synchronous communication.