设置镜像源
sudo vim /etc/apt/sources.list
#阿里源
deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
搞定,接下来执行这两个命令就完事啦。
sudo apt-get update
sudo apt-get upgrade
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 update && sudo apt install curl gnupg lsb-release
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 $(source /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-humble-desktop
source /opt/ros/humble/setup.bash
echo " source /opt/ros/humble/setup.bash" >> ~/.bashrc
至此,ROS2就已经在系统中安装好了。
为了验证ROS2安装成功,我们可以通过以下示例进行测试。
示例一:命令行示例
先来试试ROS2最为重要的底层通信系统DDS是否正常吧。
启动第一个终端,通过以下命令启动一个数据的发布者节点:
ros2 run demo_nodes_cpp talker
ros2 run demo_nodes_py listener
如果“Hello World”字符串在两个终端中正常传输,说明通信系统没有问题。
示例二:小海龟仿真示例
再来试一试ROS中的经典示例——小海龟仿真器。
启动两个终端,分别运行如下指令:
ros2 run turtlesim turtlesim_node
ros2 run turtlesim turtle_teleop_key
第一句指令将启动一个蓝色背景的海龟仿真器,第二句指令将启动一个键盘控制节点,在该终端中点击键盘上的“上下左右”按键,就可以控制小海龟运动啦。
发布话题消息
想要控制海龟动起来,我们还可以直接通过命令行发布话题指令:
ros2 topic pub --rate 1 /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: 1.8}}"