ros2怎么和ros1共存安装
时间: 2025-01-04 15:34:09 浏览: 5
### ROS1 和 ROS2 共存安装配置方法
为了实现Ubuntu下ROS1与ROS2的共存安装,可以采用特定的方法来确保两个版本不会相互冲突并能正常工作[^1]。
#### 安装前准备
确保系统的更新至最新状态:
```bash
sudo apt update && sudo apt upgrade -y
```
#### 安装ROS1 Noetic
通过官方源安装ROS1 Noetic版本,这一步骤会设置好ROS1的工作环境变量等必要组件:
```bash
sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
sudo apt update
sudo apt install ros-noetic-desktop-full -y
source /opt/ros/noetic/setup.bash
echo "source /opt/ros/noetic/setup.bash" >> ~/.bashrc
```
#### 安装ROS2 Foxy Fitzroy
接着按照如下命令行操作完成ROS2 Foxy版本的安装,此过程同样包含了必要的依赖项以及初始化rosdep工具:
```bash
sudo apt update && sudo apt install locales -y
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 software-properties-common -y
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 -y
sudo apt install ros-foxy-desktop -y
source /opt/ros/foxy/setup.bash
echo "source /opt/ros/foxy/setup.bash" >> ~/.bashrc
sudo apt install python3-colcon-common-extensions python3-argcomplete -y
```
#### 配置多ROS环境切换脚本
创建一个简单的shell函数用于快速切换当前使用的ROS版本:
编辑`~/.bashrc`文件,在其中加入以下内容以便于随时切换ROS环境:
```bash
function set_ros_version() {
if [ "$1" == "noetic" ]; then
source /opt/ros/noetic/setup.bash
export ROS_VERSION=1
echo "Switched to ROS1 (Noetic)"
elif [ "$1" == "foxy" ]; then
source /opt/ros/foxy/setup.bash
unset ROS_MASTER_URI ROS_HOSTNAME TURTLEBOT3_MODEL GAZEBO_MASTER_IP GAZEBO_MASTER_PORT ROBOT_NAME
export ROS_DOMAIN_ID=0
export RMW_IMPLEMENTATION=rmw_cyclonedds_cpp
export ROS_VERSION=2
echo "Switched to ROS2 (Foxy)"
else
echo "Unknown version"
fi
}
```
保存更改后的`.bashrc`文件,并重新加载它使新定义生效:
```bash
source ~/.bashrc
```
现在可以通过执行`set_ros_version noetic`或`set_ros_version foxy`来进行不同ROS版本间的无缝切换。
阅读全文