BASCORRO
LearningROS 2 Fundamentals

ROS 2 CLI & Workspace | ROS 2 CLI & Workspace Setup

Pelajari command line tools dan setup workspace ROS 2 | Learn ROS 2 command line tools and workspace setup

Apa itu ROS 2?
0 dari 5 halaman selesai
In Progress
Scroll sampai 80% untuk menandai halaman selesai.

ROS 2 CLI & Workspace

๐ŸŽฏ Learning Objectives

Setelah materi ini: (1) Menguasai ROS 2 CLI commands, (2) Membuat dan build workspace, (3) Membuat package Python/C++.


๐Ÿ–ฅ๏ธ ROS 2 CLI Overview

ROS 2 CLI menggunakan pattern: ros2 <verb> <resource> [options]

# Common verbs
ros2 run      # Run a node
ros2 launch   # Launch multiple nodes
ros2 topic    # Interact with topics
ros2 service  # Interact with services
ros2 node     # Interact with nodes
ros2 param    # Manage parameters
ros2 bag      # Record/playback data

๐Ÿ“‚ Workspace Structure

ros2_ws/                    # Workspace root
โ”œโ”€โ”€ src/                    # Source packages
โ”‚   โ”œโ”€โ”€ my_package/
โ”‚   โ”œโ”€โ”€ another_package/
โ”‚   โ””โ”€โ”€ ...
โ”œโ”€โ”€ build/                  # Build artifacts (auto-generated)
โ”œโ”€โ”€ install/                # Installed packages (auto-generated)
โ””โ”€โ”€ log/                    # Build logs (auto-generated)

Creating a Workspace

# Create workspace directory
mkdir -p ~/ros2_ws/src
cd ~/ros2_ws

# Build (even if empty)
colcon build

# Source the workspace
source install/setup.bash

๐Ÿ“ฆ Creating Packages

Python Package

cd ~/ros2_ws/src

ros2 pkg create --build-type ament_python \
    --node-name my_node \
    my_python_pkg

# Structure:
# my_python_pkg/
# โ”œโ”€โ”€ package.xml
# โ”œโ”€โ”€ setup.py
# โ”œโ”€โ”€ setup.cfg
# โ”œโ”€โ”€ my_python_pkg/
# โ”‚   โ”œโ”€โ”€ __init__.py
# โ”‚   โ””โ”€โ”€ my_node.py
# โ””โ”€โ”€ resource/

C++ Package

ros2 pkg create --build-type ament_cmake \
    --node-name my_node \
    my_cpp_pkg

๐Ÿ”จ Building Packages

cd ~/ros2_ws

# Build all packages
colcon build

# Build specific package
colcon build --packages-select my_package

# Build with symlinks (for Python - faster dev)
colcon build --symlink-install

๐Ÿ’ก Pro Tip: Gunakan --symlink-install untuk Python packages agar tidak perlu rebuild setelah edit code.


๐Ÿƒ Running Nodes

# Source workspace first!
source ~/ros2_ws/install/setup.bash

# Run a node
ros2 run my_package my_node

# Run with parameters
ros2 run my_package my_node --ros-args -p param_name:=value

# Run with remapping
ros2 run my_package my_node --ros-args -r old_topic:=new_topic

๐Ÿ”ง Essential CLI Commands

Topic Commands

ros2 topic list                    # List all topics
ros2 topic echo /topic_name        # Print topic data
ros2 topic hz /topic_name          # Check publish rate
ros2 topic info /topic_name        # Topic info
ros2 topic pub /topic msg_type "data"  # Publish once

Node Commands

ros2 node list                     # List running nodes
ros2 node info /node_name          # Node details

Service Commands

ros2 service list                  # List services
ros2 service call /srv srv_type "args"  # Call service

Parameter Commands

ros2 param list                    # List all parameters
ros2 param get /node param_name    # Get parameter
ros2 param set /node param_name val # Set parameter

๐Ÿงช Quick Exercise

# Terminal 1: Run turtlesim
ros2 run turtlesim turtlesim_node

# Terminal 2: Explore!
ros2 node list
ros2 topic list
ros2 topic echo /turtle1/pose
ros2 service list

โญ๏ธ Next Steps

On this page