编译完ros2程序后,我们会发现install目录下有两个脚本local_setup.bashsetup.bash。执行程序前,通常需要source一下install目录下的脚本,以便环境变量准备就绪。这样ros2 runros2 launch就能找到对应的执行文件和依赖。

下面的内容是从setup.bash中截取出来的一段。可以发现它会先source一下/opt/ros/galactic目录再source一下/home/ubuntu/turtlebot3_ws/install目录,最后再source一下当前目录。这样就会把外部的多个工作空间囊括进来。如果多个工作空间中有相同名字的功能包可能就会互相冲突。

# source chained prefixes
# setting COLCON_CURRENT_PREFIX avoids determining the prefix in the sourced script
COLCON_CURRENT_PREFIX="/opt/ros/galactic"
_colcon_prefix_chain_bash_source_script "$COLCON_CURRENT_PREFIX/local_setup.bash"
# setting COLCON_CURRENT_PREFIX avoids determining the prefix in the sourced script
COLCON_CURRENT_PREFIX="/home/ubuntu/turtlebot3_ws/install"
_colcon_prefix_chain_bash_source_script "$COLCON_CURRENT_PREFIX/local_setup.bash"

# source this prefix
# setting COLCON_CURRENT_PREFIX avoids determining the prefix in the sourced script
COLCON_CURRENT_PREFIX="$(builtin cd "`dirname "${BASH_SOURCE[0]}"`" > /dev/null && pwd)"
_colcon_prefix_chain_bash_source_script "$COLCON_CURRENT_PREFIX/local_setup.bash"

unset COLCON_CURRENT_PREFIX
unset _colcon_prefix_chain_bash_source_script

local_setup.bash只会source脚本所在目录。这样只会查找当前install目录下的执行文件和依赖。

所以如果系统里有多个ROS工作空间,谨慎一点的方法是:

1、先source一下/opt/ros/galactic/local_setup.bash

2、在source一下某个用户的工作空间。

如果系统里就一个自己建的用户工作空间。那就直接source用户工作空间中install目录下的setup.bash。一切都搞定了。

它会帮你先source一下/opt/ros/galactic/local_setup.bash,这样就有系统的安装的ros执行程序和依赖了。然后再帮你source本用户空间的install/local_setup.bash

参考:

https://colcon.readthedocs.io/en/released/developer/environment.html#workspace-level

https://answers.ros.org/question/292566/what-is-the-difference-between-local_setupbash-and-setupbash/


觉得有用就点赞吧!

我是首飞,一个帮大家填坑的机器人开发攻城狮。

另外在公众号《首飞》内回复“机器人”获取精心推荐的C/C++,Python,Docker,Qt,ROS1/2等机器人行业常用技术资料。

公众号