FunnyWii
FunnyWii
Published on 2024-10-25 / 30 Visits
0
0

Ubuntu安装ROS2,以及与ROS1的共存

系统环境:Ubuntu 20.04

ROS2版本:Foxy,其中Foxy是LTS长期支持版本,Galactic不是,但是这两者都只能运行在Ubuntu20.04上。

ROS2的安装

先检查是否支持UTF-8,用locale命令

jetson@funnywii-orin:~$ locale
LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

如不支持:

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

再检查是否启用ubuntu Universe存储库:

etson@funnywii-orin:~$ apt-cache policy | grep universe
 500 http://ports.ubuntu.com/ubuntu-ports focal-security/universe arm64 Packages
     release v=20.04,o=Ubuntu,a=focal-security,n=focal,l=Ubuntu,c=universe,b=arm64
 100 http://ports.ubuntu.com/ubuntu-ports focal-backports/universe arm64 Packages
     release v=20.04,o=Ubuntu,a=focal-backports,n=focal,l=Ubuntu,c=universe,b=arm64
 500 http://ports.ubuntu.com/ubuntu-ports focal-updates/universe arm64 Packages
     release v=20.04,o=Ubuntu,a=focal-updates,n=focal,l=Ubuntu,c=universe,b=arm64
 500 http://ports.ubuntu.com/ubuntu-ports focal/universe arm64 Packages
     release v=20.04,o=Ubuntu,a=focal,n=focal,l=Ubuntu,c=universe,b=arm64

如果没启用不会有输出,没有输出的话就运行:

sudo apt install software-properties-common
sudo add-apt-repository universe

然后设置密钥:

sudo apt update && sudo apt install curl gnupg2 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 install ros-foxy-desktop

安装完成后配置环境:

echo "source /opt/ros/foxy/setup.bash" >> ~/.bashrc
source ~/.bashrc

然后运行demo:

ros2 run demo_nodes_cpp talker
ros2 run demo_nodes_cpp listener

ros2_pub_sub.png

如果你之前安装了ROS1 Noetic,那么这步可能有些问题,可能会报错:undefined symbol: ZNK12classloader11ClassLoader14getLibraryPathB5cxx11Ev 先别慌,再看看小乌龟能不能正常运行:

ros2 run turtlesim turtlesim_node
ros2 run turtlesim turtle_teleop_key

我遇到了上面报错,但是小乌龟能够正常运行和操控。猜测是ROS1和ROS2的版本冲突问题。

ROS1和ROS2的共存和切换

打开~/.bashrc 文件,可以看到ROS1和ROS2的环境路径:

source /opt/ros/noetic/setup.bash
source /opt/ros/foxy/setup.bash

先注释掉这两行,然后在bashrc末尾添加:

# >>> fishros initialize >>>
echo "ros: noetic(1) foxy(2)?"
read choose
case $choose in
1) source  /opt/ros/noetic/setup.bash;;
2) source  /opt/ros/foxy/setup.bash;;
esac
# <<< fishros initialize <<<

这样每次进入终端都会先询问使用哪个版本的ROS,输入1则使用ROS Noetic,输入2使用ROS2 Foxy。

如果不想每次进入终端都确认ROS版本,也可以使用下面的方法:

# >>> type in ros to choose which to initialize >>>
ros(){
  echo "ros: noetic(1) foxy(2)?"
  read choose
  case $choose in
  1) source  /opt/ros/noetic/setup.bash;;
  2) source  /opt/ros/foxy/setup.bash;;
  esac
}
# <<< type in ros/ros2 to initialize <<<

只有在终端中输入ros ,才需要确认ROS版本。

ros1_ros2.png

参考文章

[1] https://blog.csdn.net/feichangyanse/article/details/132687042

[2] https://blog.csdn.net/aniclever/article/details/134790299


Comment