# ROS 2

**Phase 4 — Track B — Module 5.6** · Application Development

> **Focus:** Master **ROS 2** from fundamentals through advanced navigation, real-time safety, and **Jetson Orin Nano** edge deployment—so your robot software is production-grade, deterministic, and GPU-accelerated.

**Hub:** [5. Application Development](../Guide.md)

---


## 1. ROS 2 fundamentals

### ROS 2 architecture

- **Nodes, Topics, Services, Actions:** Master the core ROS 2 communication paradigms. Understand publish-subscribe (topics), request-response (services), and long-running tasks (actions).
- **DDS (Data Distribution Service):** Learn how ROS 2 uses DDS as its middleware. Understand quality of service (QoS) settings for reliable, real-time, or best-effort communication.
- **Workspace and Package Structure:** Set up ROS 2 workspaces, create packages, and organize your robot software with the colcon build system.

### Programming with ROS 2

- **Python and C++ Clients:** Write ROS 2 nodes in both Python and C++. Understand the `rclpy` and `rclcpp` client libraries.
- **Launch Files and Parameters:** Create launch files to start multiple nodes and configure parameters for different deployments.
- **Lifecycle Nodes:** Explore managed nodes for state-controlled startup and shutdown in production systems.

---

## 2. Robot navigation and control

### Navigation Stack (Nav2)

- **Costmaps and Path Planning:** Learn costmap generation, global planners (NavFn, Smac Planner), and local planners for obstacle avoidance.
- **Recovery Behaviors:** Implement recovery behaviors when the robot gets stuck or loses its path.
- **Multi-Robot Coordination:** Explore Nav2 for coordinating multiple robots in shared environments.

### Sensor integration

- **Camera and LiDAR Drivers:** Integrate camera and LiDAR sensors with ROS 2. Use `sensor_msgs` and point cloud processing.
- **TF2 and Robot State:** Master TF2 for coordinate transforms and robot state publishing (joint states, odometry).

---

## 3. Edge deployment on Jetson

### ROS 2 on Jetson Orin Nano

- **Cross-Compilation and Native Build:** Build ROS 2 packages for Jetson. Optimize for ARM architecture and GPU acceleration.
- **Real-Time Performance:** Tune ROS 2 and DDS for low-latency, deterministic behavior on embedded hardware.
- **Containerization:** Use Docker / ROS 2 containers for reproducible deployment on Jetson.

### AI and ROS 2 integration

- **TensorRT and ROS 2:** Run TensorRT-optimized models in ROS 2 nodes for perception (object detection, segmentation).
- **Sensor Fusion with ROS 2:** Combine camera, IMU, and other sensor data in ROS 2 pipelines for robust perception.

---

## 4. Advanced ROS 2 development

### Custom interfaces and middleware

- **Custom Messages and Services:** Design domain-specific ROS 2 message types (`.msg`, `.srv`, `.action`) with appropriate field types and serialization. Understand how interface changes affect downstream packages.
- **DDS QoS Tuning:** Configure DDS Quality of Service (QoS) policies—reliability (RELIABLE vs. BEST_EFFORT), durability, history depth, deadline, and lifespan—to match application requirements for latency, bandwidth, and reliability.
- **Custom DDS Middleware:** Swap DDS implementations (FastDDS, CycloneDDS, Connext DDS) and tune middleware configuration for specific use cases (real-time, high throughput, or low-power embedded systems).

### ROS 2 executors and concurrency

- **Single-Threaded vs. Multi-Threaded Executors:** Understand the implications of executor choice on callback scheduling, latency, and thread safety. Implement callback groups for fine-grained concurrency control.
- **Custom Executors:** Write custom executors for specialized scheduling requirements—priority-based execution, time-triggered callbacks, or WCET-bounded execution for real-time systems.
- **Intra-Process Communication:** Enable zero-copy intra-process communication for nodes running in the same process, dramatically reducing latency and memory copies for high-bandwidth data (cameras, LiDAR).

### ROS 2 component architecture

- **Composable Nodes:** Refactor ROS 2 nodes as composable components that can run in a shared process (component container) for lower overhead and intra-process communication.
- **Managed Lifecycle Nodes:** Implement lifecycle nodes (`rclcpp_lifecycle`) for production-grade state management—configure, activate, deactivate, and cleanup transitions for deterministic startup and shutdown.
- **Plugin-Based Architecture:** Use `pluginlib` to define plugin interfaces (e.g., for planners, controllers, filters) and dynamically load implementations at runtime without recompilation.

---

## 5. Advanced navigation, planning, and behavior

### Nav2 deep dive

- **Custom Planners and Controllers:** Implement Nav2 plugin interfaces to create custom global planners (e.g., hybrid A* with kinematic constraints) and local controllers (e.g., MPPI — Model Predictive Path Integral).
- **Behavior Trees:** Master BehaviorTree.CPP and Nav2's behavior tree integration. Design hierarchical behavior trees for complex autonomous behaviors—exploration, docking, multi-goal navigation.
- **Dynamic Obstacle Avoidance:** Integrate dynamic obstacle layers into costmaps using LiDAR or camera detections. Implement predictive obstacle avoidance with velocity obstacles (VO/RVO).

### SLAM and localization

- **Advanced SLAM:** Go beyond 2D SLAM to 3D SLAM using packages like LIO-SAM, LOAM, or RTAB-Map with LiDAR or RGB-D cameras. Understand loop closure detection and pose graph optimization.
- **Multi-Session and Multi-Map:** Implement multi-session SLAM for persistent mapping across power cycles. Merge maps from multiple sessions or robots using map merging packages.
- **Semantic SLAM:** Combine geometric SLAM with semantic understanding—object detection integrated into the map for semantic place recognition and targeted navigation.

### Task and mission planning

- **BT-Based Mission Planning:** Use behavior trees to implement high-level mission planning—room-by-room cleaning, inspection routes, item delivery with pick-up and drop-off sequences.
- **Task Allocation in Multi-Robot Systems:** Implement auction-based or optimization-based task allocation for multi-robot systems. Use ROS 2 services or action servers for task assignment and status tracking.
- **ROS 2 with PlanSys2:** Use PlanSys2 (PDDL-based planning in ROS 2) for formal task planning with preconditions and effects, enabling high-level reasoning about robot capabilities and world state.

---

## 6. Real-time and safety-critical ROS 2

### Real-time Linux with ROS 2

- **PREEMPT_RT Kernel:** Build and configure a Linux kernel with the PREEMPT_RT patch for fully preemptible execution. Measure latency improvements for ROS 2 control loops with `cyclictest`.
- **CPU Isolation and IRQ Affinity:** Isolate CPU cores for real-time ROS 2 nodes using `isolcpus` and configure IRQ affinity to prevent interrupt-induced jitter on real-time cores.
- **Memory Locking:** Use `mlockall()` to lock process memory pages, preventing page faults from causing latency spikes in real-time nodes.

### ROS 2 for safety-critical applications

- **ROS 2 Safety Certification:** Understand the landscape of safety certification for ROS 2—Apex.AI's Apex.OS (ASIL-D certified), safety-certified DDS, and formal verification approaches.
- **Watchdog and Fault Detection:** Implement system-level watchdog nodes that monitor topic heartbeats, detect silent node failures, and trigger safe-state behaviors.
- **Deterministic Execution:** Design ROS 2 systems with deterministic timing—fixed-rate publishers, deadline QoS, and timer-driven execution to enable WCET analysis.

### ROS 2 testing and CI/CD

- **ROS 2 Test Framework:** Write unit tests for ROS 2 nodes using `ros_testing` with `launch_pytest`. Test node behavior under various conditions using parameterized launch files.
- **Integration Testing:** Design integration tests that spin up complete multi-node systems and validate end-to-end behavior (e.g., a perception-to-control pipeline).
- **CI/CD with ROS 2:** Set up GitHub Actions or Jenkins pipelines for automated building, linting, and testing of ROS 2 packages. Integrate with ROS 2 Industrial CI for cross-platform builds.

---

## 7. Projects

### Fundamentals

- **Robot Navigation with Nav2:** Build an autonomous mobile robot that navigates in a known environment using Nav2, with costmaps and path planning.
- **Multi-Robot System:** Create a system with multiple robots (real or simulated) that coordinate via ROS 2 topics and services.
- **Edge AI Robot:** Deploy a ROS 2-based robot on Jetson Orin Nano with TensorRT-accelerated perception and Nav2 for navigation.

### Advanced

- **Composable Node Pipeline:** Refactor a multi-node perception pipeline (camera driver → preprocessing → detector → tracker) into composable nodes in a single container with intra-process communication.
- **Real-Time Executor:** Implement a priority-based custom executor for a control loop node and measure callback jitter with and without real-time scheduling.
- **QoS Benchmark:** Compare latency and packet loss for RELIABLE vs. BEST_EFFORT QoS on a high-frequency sensor topic under CPU load.

### Navigation and planning

- **Custom Nav2 Controller Plugin:** Implement an MPPI controller as a Nav2 controller plugin. Benchmark trajectory quality and obstacle avoidance against DWB.
- **3D LiDAR SLAM:** Run LIO-SAM or LOAM on a Jetson Orin Nano with a 3D LiDAR, building a 3D map of an indoor environment with loop closure.
- **Multi-Robot Task Allocation:** Implement a centralized task allocator for 3 simulated robots in Gazebo, assigning navigation goals via ROS 2 action clients and tracking completion.

### Real-time and safety

- **Real-Time Control Loop:** Implement a 1 kHz control loop in a ROS 2 node on a PREEMPT_RT kernel. Measure and document jitter with and without CPU isolation.
- **Fault-Tolerant Robot System:** Build a ROS 2 system with a watchdog node that detects sensor node failures and transitions the robot to a safe stop behavior.
- **Full CI/CD Pipeline:** Set up a GitHub Actions pipeline for a ROS 2 package with colcon build, unit tests, and integration tests running in a Docker container.

---

## 8. Resources

| Topic | Resource |
|-------|----------|
| **Official docs** | [ROS 2 Documentation](https://docs.ros.org/) — tutorials and API references |
| **Nav2** | [Navigation 2](https://navigation.ros.org/) — navigation stack guide |
| **NVIDIA + ROS 2** | NVIDIA guides for running ROS 2 on Jetson platforms |
| **ROS 2 design** | [ROS 2 Design](https://design.ros2.org/) — architecture decisions, DDS integration, QoS rationale |
| **Real-time ROS 2** | "A Systematic Approach to Real-Time ROS 2" (Apex.AI) — deterministic ROS 2 on RT systems |
| **FastDDS** | eProsima FastDDS configuration reference (default ROS 2 middleware) |
| **Nav2 theory** | *Behavior Trees in Robotics and AI* — Colledanchise and Ögren |
| **SLAM theory** | *Probabilistic Robotics* — Thrun, Burgard, and Fox |
| **Testing** | `ros_testing` — official ROS 2 testing framework for node unit and integration tests |
