工区/工作区/工作空间(workspace简写为ws)是机器人操作系统核心概念之一。  
工作空间是包含ROS 2软件的文件夹。在使用ROS 2之前,必须在正在使用的终端中配置ROS 2工作区(包括系统和局部),使ROS 2的软件包可在此终端中使用。  
  如上图所示,foxy/noetic分别为ros2和ros1的系统工作区,使用如下命令可以顺利加载配置:
  • source /opt/ros/foxy/setup.bash
    如上文件夹下都是使用apt安装在系统目录下的软件包。 如果自定义机器人软件程序,通常在home文件夹下,新建诸如ros_ws/src文件夹,然后编写代码,编译生成可执行文件使用。 以博客中之前讲解过的mobot为例,此案例正在dashing/eloquent/foxy进行测试。 如果需要编译工区源码,需要使用如下命令安装所需功能包:
  • rosdep install -i --from-path src --rosdistro foxy -y
接着使用colcon build:
  • colcon build
    这样就完成了一个自定义工区的编译,使用如下命令加载环境:
  • source install/setup.bash
    install:     如果没有正确加载的话,此工区的功能包是无法发现和使用的!     其他注意事项:
  1. You also have the option of sourcing an “overlay” – a secondary workspace where you can add new packages without interfering with the existing ROS 2 workspace that you’re extending, or “underlay”. Your underlay must contain the dependencies of all the packages in your overlay. Packages in your overlay will override packages in the underlay. It’s also possible to have several layers of underlays and overlays, with each successive overlay using the packages of its parent underlays.
  2. Sourcing the local_setup of the overlay will only add the packages available in the overlay to your environment. setup sources the overlay as well as the underlay it was created in, allowing you to utilize both workspaces.So, sourcing your main ROS 2 installation’s setup and then the dev_ws overlay’s local_setup, like you just did, is the same as just sourcing dev_ws’s setup, because that includes the environment of the underlay it was created in.
  如果自定义包和系统包重名,或者下载了系统包源码自定义,请掌握环境配置方法,确保启动的是需要的节点和应用,而非系统默认的应用。   参考视频教程链接如下:
  • https://www.bilibili.com/video/bv1dv411B7KY