ROS 入门:从基础到第一个功能包

Welcome to ROS! This is your first step into the Robot Operating System world. Check ROS Documentation for detailed info. If you encounter issues, find solutions in ROS Answers or ask on ROS GitHub.
Quick Start with ROS

  1. Install ROS (Take Noetic for Ubuntu 20.04 as Example)

Configure Ubuntu repositories

sudo add-apt-repository universe
sudo add-apt-repository multiverse
sudo add-apt-repository restricted

Add ROS key and source

sudo apt-key adv –keyserver ‘hkp://keyserver.ubuntu.com:80’ –recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C6547
echo “deb http://packages.ros.org/ros/ubuntu focal main” | sudo tee /etc/apt/sources.list.d/ros.list

Install ROS Noetic desktop

sudo apt update
sudo apt install ros-noetic-desktop-full

Set up environment variables

echo “source /opt/ros/noetic/setup.bash” >> ~/.bashrc
source ~/.bashrc

Install dependencies for package building

sudo apt install python3-rosdep python3-rosinstall python3-rosinstall-generator python3-wstool build-essential
sudo rosdep init
rosdep update

More info: ROS Installation Guide
2. Create a ROS Workspace

Create workspace directory

mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/

Build the workspace

catkin_make

Set up workspace environment

source devel/setup.bash

Permanent setup: echo “source ~/catkin_ws/devel/setup.bash” >> ~/.bashrc

More info: ROS Workspace Tutorial
3. Create a ROS Package

Enter src directory

cd ~/catkin_ws/src

Create package (depends on roscpp, rospy, std_msgs)

catkin_create_pkg my_first_ros_package roscpp rospy std_msgs

Build the package

cd ~/catkin_ws
catkin_make
source devel/setup.bash

More info: ROS Package Creation
4. Write a Simple Publisher Node (Python)
Create talker.py in ~/catkin_ws/src/my_first_ros_package/scripts/:
#!/usr/bin/env python3
import rospy
from std_msgs.msg import String

def talker():
# Initialize node “talker_node”
rospy.init_node(‘talker_node’, anonymous=True)
# Create publisher (topic: “chatter”, msg type: String, queue size: 10)
pub = rospy.Publisher(‘chatter’, String, queue_size=10)
rate = rospy.Rate(10) # 10Hz

while not rospy.is_shutdown():
    hello_str = "Hello ROS! Time: %s" % rospy.get_time()
    rospy.loginfo(hello_str)
    pub.publish(hello_str)
    rate.sleep()

if name == ‘main‘:
try:
talker()
except rospy.ROSInterruptException:
pass

Add execute permission:
chmod +x ~/catkin_ws/src/my_first_ros_package/scripts/talker.py

More info: ROS Publisher Tutorial
5. Run the Node

Terminal 1: Start ROS core

roscore

Terminal 2: Run talker node

source ~/catkin_ws/devel/setup.bash
rosrun my_first_ros_package talker.py

Terminal 3 (Optional): Listen to topic “chatter”

rostopic echo /chatter

More info: ROS Node Execution

机器人