Robotics Programming with ROS2 and Python
By the end of this week, you will be able to:
| Directory | Purpose | Example |
|---|---|---|
| / | Root directory | Top of file hierarchy |
| /home | User home directories | /home/username |
| /bin | Essential binaries | ls, cp, mv commands |
| /etc | Configuration files | System settings |
| /var | Variable data | Log files, caches |
| /usr | User programs | Installed applications |
| /tmp | Temporary files | Cleared on reboot |
| /opt | Optional software | Third-party apps |
Opening the Terminal:
Understanding the Prompt:
username@hostname:~$
# username: Your login name
# hostname: Computer name
# ~: Current directory (home)
# $: Regular user (# means root/admin)
# Print Working Directory (where am I?)
pwd
# List files and directories
ls # Basic list
ls -l # Long format (detailed)
ls -a # Show hidden files
ls -la # Long format + hidden
# Change Directory
cd Documents # Go to Documents folder
cd .. # Go up one level
cd ~ # Go to home directory
cd / # Go to root directory
cd - # Go to previous directory
# Create empty file
touch myfile.txt
# Create directory
mkdir myfolder
mkdir -p parent/child/grandchild # Create nested directories
# Copy files/directories
cp file.txt backup.txt # Copy file
cp -r folder1 folder2 # Copy directory recursively
# Move/Rename files
mv oldname.txt newname.txt # Rename file
mv file.txt Documents/ # Move file
# Delete files/directories
rm file.txt # Delete file
rm -r folder # Delete directory
rm -rf folder # Force delete (use with caution!)
Permission Format: -rwxr-xr--
# Make file executable
chmod +x script.sh
# Set specific permissions
chmod 755 file.txt # rwxr-xr-x
chmod 644 file.txt # rw-r--r--
chmod 600 file.txt # rw-------
Create your first script:
#!/bin/bash
# My first ROS2 setup script
echo "Setting up ROS2 environment..."
source /opt/ros/jazzy/setup.bash
echo "ROS2 environment loaded!"
echo "ROS Distribution: $ROS_DISTRO"
echo "Current directory: $(pwd)"
To run the script:
chmod +x setup_ros2.sh
./setup_ros2.sh
Robot Operating System 2 (ROS2) is an open-source robotics middleware framework that provides tools, libraries, and conventions for building complex robot behaviors.
| Feature | ROS1 | ROS2 |
|---|---|---|
| Communication | Custom TCP/UDP | DDS Standard |
| Real-time | Not supported | Supported |
| Multi-robot | Complex setup | Built-in |
| Security | Basic | Advanced (SROS2) |
| Platforms | Linux only | Linux, Windows, macOS |
| Master Node | Required (SPOF) | Decentralized |
ROS2 uses a layered architecture design:
Independent processes that perform computation. Each node should have a single, well-defined purpose.
Named buses for asynchronous data streaming using publish-subscribe pattern.
Synchronous request-response communication for remote procedure calls.
Asynchronous, long-running tasks with feedback and ability to cancel.
locale
sudo apt update && sudo apt install locales
sudo locale-gen en_US en_US.UTF-8
sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
export LANG=en_US.UTF-8
sudo apt install software-properties-common
sudo add-apt-repository universe
sudo apt update && sudo apt install curl -y
sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key \
-o /usr/share/keyrings/ros-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) \
signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] \
http://packages.ros.org/ros2/ubuntu \
$(. /etc/os-release && echo $UBUNTU_CODENAME) main" | \
sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null
sudo apt update
sudo apt upgrade
sudo apt install ros-jazzy-desktop
sudo apt install ros-dev-tools
sudo apt install python3-colcon-common-extensions
sudo apt install python3-rosdep python3-pip
sudo rosdep init
rosdep update
# Source ROS2 (temporary)
source /opt/ros/jazzy/setup.bash
# Make it permanent
echo "source /opt/ros/jazzy/setup.bash" >> ~/.bashrc
source ~/.bashrc
ros2 --help
ros2 doctor
echo $ROS_DISTRO # Should output: jazzy
ros2 node # Work with nodes
ros2 topic # Work with topics
ros2 service # Work with services
ros2 action # Work with actions
ros2 param # Work with parameters
ros2 pkg # Work with packages
ros2 run # Run a node from a package
ros2 launch # Launch multiple nodes
ros2 interface # Show message/service/action definitions
ros2 bag # Record and play back data
ros2 doctor # Check system status
TurtleSim is a lightweight 2D simulator designed for learning ROS2 concepts.
# Install TurtleSim
sudo apt install ros-jazzy-turtlesim
# Run TurtleSim node
ros2 run turtlesim turtlesim_node
# In a NEW terminal, run teleop node
ros2 run turtlesim turtle_teleop_key
# Control with arrow keys
# Make turtle move forward
ros2 topic pub /turtle1/cmd_vel geometry_msgs/msg/Twist \
"{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.0}}"
# Make turtle rotate
ros2 topic pub /turtle1/cmd_vel geometry_msgs/msg/Twist \
"{linear: {x: 0.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 1.8}}"
# Spawn a new turtle
ros2 service call /spawn turtlesim/srv/Spawn \
"{x: 2.0, y: 2.0, theta: 0.2, name: 'turtle2'}"
# Change pen color
ros2 service call /turtle1/set_pen turtlesim/srv/SetPen \
"{r: 255, g: 0, b: 0, width: 5, off: 0}"
# Clear background
ros2 service call /clear std_srvs/srv/Empty
# List all nodes
ros2 node list
# List all topics
ros2 topic list
# Monitor turtle's position
ros2 topic echo /turtle1/pose
# List parameters
ros2 param list /turtlesim
| Component | Points |
|---|---|
| Linux Setup & Commands | 20 |
| Bash Script Quality | 20 |
| TurtleSim Basic Control | 20 |
| ROS2 Commands Mastery | 20 |
| Creative Challenge | 10 |
| Documentation Quality | 10 |
| Total | 100 |