Ubuntu20.04安装ROS2-foxy
一、设置时区及语言
locale # check for UTF-8
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
locale # verify settings
二、添加ros2源
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
三、安装ros2
sudo apt update
sudo apt install ros-foxy-desktop
#如果电脑已有ros1,就不要把下面命令加入.bashrc中
source /opt/ros/foxy/setup.bash
四、新一代包构建工具colcon
colcon独立于ros,没有包含在ros的功能包中,需要我们自己安装
sudo apt install python3-colcon-common-extensions
#添加colcon_cd环境变量到shell,可不设,用处不大,只是快速切换到包目录
echo "source /usr/share/colcon_cd/function/colcon_cd.sh" >> ~/.bashrc
echo "export _colcon_cd_root=/opt/ros/foxy/" >> ~/.bashrc
#设置colcon命令补全,作用很大,提高效率
echo "source /usr/share/colcon_argcomplete/hook/colcon-argcomplete.bash" >> ~/.bashrc
#Tips
#1、如果不想构建特定的包,请在包目录中放置一个名为COLCON_IGNORE的空文件,它将不会被索引。
五、创建功能包及用colcon编译
mkdir -p ros2/src
cd ros2/src
ros2 pkg create --dependencies std_msgs rclcpp --node-name my_node my_package
cd ..
colcon build
#编译指定包
colcon build --packages-up-to pkg_name
source install/setup.bash
ros2 run my_package my_node