使用ROS2环境变量创建C ++项目

时间:2019-03-15 13:15:07

标签: c++ batch-file environment-variables ros ros2

我遇到以下问题:我编写了一个简单的 ROS2 / Qt5 (机器人操作系统)项目,以在GUI中展示发布者和订阅者。该项目编译正常,将所有Qt5和ROS dll复制到可执行文件目录后,应用程序启动,但随后迅速关闭/崩溃,而没有给我任何错误。

尝试2:打开控制台并获取ROS2安装的源代码(通过运行批处理脚本,我相信该脚本会将环境变量加载到控制台中,对吗?)。 如果我现在从ROS2控制台中启动可执行文件,一切正常。

所以我的假设是,当我编译我的项目并尝试仅启动它时,它会丢失所有环境变量,然后崩溃。

有没有一种方法可以避免必须启动ROS2控制台?

获取ROS2安装程序的批处理脚本对我来说似乎也很复杂。 A无法真正了解它加载的是哪种变量。有没有一种方法可以使用该批处理脚本并将其“挂钩”到我的可执行文件中,这样我就不必找出此脚本的具体功能了吗?这是批处理脚本:

:: generated from colcon_core/shell/template/prefix.bat.em
@echo off

:: This script extends the environment with all packages contained in this
:: prefix path.

:: add this prefix to the COLCON_PREFIX_PATH
call:_colcon_prefix_bat_prepend_unique_value COLCON_PREFIX_PATH "%%~dp0"

:: get all packages in topological order
call:_colcon_get_ordered_packages _ordered_packages "%~dp0"

:: source packages
if "%_ordered_packages%" NEQ "" (
  for %%p in ("%_ordered_packages:;=";"%") do (
    call:_colcon_prefix_bat_call_script "%~dp0share\%%~p\package.bat"
  )
  set "_ordered_packages="
)

goto:eof


:: function to prepend a value to a variable
:: which uses semicolons as separators
:: duplicates as well as trailing separators are avoided
:: first argument: the name of the result variable
:: second argument: the value to be prepended
:_colcon_prefix_bat_prepend_unique_value
  setlocal enabledelayedexpansion
  :: arguments
  set "listname=%~1"
  set "value=%~2"

  :: get values from variable
  set "values=!%listname%!"
  :: start with the new value
  set "all_values=%value%"
  :: skip loop if values is empty
  if "%values%" NEQ "" (
    :: iterate over existing values in the variable
    for %%v in ("%values:;=";"%") do (
      :: ignore empty strings
      if "%%~v" NEQ "" (
        :: ignore duplicates of value
        if "%%~v" NEQ "%value%" (
          :: keep non-duplicate values
          set "all_values=!all_values!;%%~v"
        )
      )
    )
  )
  :: set result variable in parent scope
  endlocal & (
    set "%~1=%all_values%"
  )
goto:eof


:: Get the package names in topological order
:: using semicolons as separators and avoiding leading separators.
:: first argument: the name of the result variable
:: second argument: the base path to look for packages
:_colcon_get_ordered_packages
  setlocal enabledelayedexpansion

  :: check environment variable for custom Python executable
  if "%COLCON_PYTHON_EXECUTABLE%" NEQ "" (
    if not exist "%COLCON_PYTHON_EXECUTABLE%" (
      echo error: COLCON_PYTHON_EXECUTABLE '%COLCON_PYTHON_EXECUTABLE%' doesn't exist
      exit /b 1
    )
    set "_colcon_python_executable=%COLCON_PYTHON_EXECUTABLE%"
  ) else (
    :: use the Python executable known at configure time
    set "_colcon_python_executable=c:\python37\python.exe"
    :: if it doesn't exist try a fall back
    if not exist "!_colcon_python_executable!" (
      python --version > NUL 2> NUL
      if errorlevel 1 (
        echo error: unable to find python executable
        exit /b 1
      )
      set "_colcon_python_executable=python"
    )
  )

  set "_colcon_ordered_packages="
  for /f %%p in ('""%_colcon_python_executable%" "%~dp0_local_setup_util.py" --merged-install"') do (
    if "!_colcon_ordered_packages!" NEQ "" set "_colcon_ordered_packages=!_colcon_ordered_packages!;"
    set "_colcon_ordered_packages=!_colcon_ordered_packages!%%p"
  )
  endlocal & (
    :: set result variable in parent scope
    set "%~1=%_colcon_ordered_packages%"
  )
goto:eof


:: call the specified batch file and output the name when tracing is requested
:: first argument: the batch file
:_colcon_prefix_bat_call_script
  if exist "%~1" (
    if "%COLCON_TRACE%" NEQ "" echo call "%~1"
    call "%~1%"
  ) else (
    echo not found: "%~1" 1>&2
  )
goto:eof

1 个答案:

答案 0 :(得分:0)

请勿修改ROS2(甚至批处理文件)中的任何内部代码,请遵循其规则来启动ros2应用程序。 ROS2是在复杂的系统中设计的,以便动态加载不同种类的软件包。我认为您已将dll复制到该文件夹​​中,从而打破了它的库搜索规则。

我建议您通过调用..来编写另一个更简单的批处理文件。

call <path-to-your-ros2-ws>/setup.bat

并使用官方的ros2命令行工具运行该应用程序。

ros2 run <your-package> <executable>