1

我有以下问题:我编写了一个简单的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
4

1 回答 1

0

不要修改 ROS2 的任何内部代码(甚至是批处理文件),请按照他们的规则启动 ros2 应用程序。ROS2 被设计在一个复杂的系统中,以便动态加载不同类型的包。我认为你已经通过将 dll 复制到文件夹来打破它的库搜索规则。

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

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

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

ros2 run <your-package> <executable>
于 2019-03-17T07:28:50.813 回答