小鱼 ROS 2 新书上线!点击链接查看, 新书配套视频点击链接查看。
提问前必看的发帖注意事项—— 提问前必看!不符合要求的问题拒绝回答!!
社区使用指南—如何添加标签修改密码
launch启动报错
-
标题:launch 启动报错 Caught multiple exceptions when trying to load file of format [py](ROS2 HUMBLE)
提问模板(建议直接复制粘贴):
背景(可选):
此为练习程序,我写了一个robot_tracker包用来读取robot_sensor的数据并滤波,编译能够通过,但是启动时报了以下错误
问题描述:
[在这里详细描述您遇到的问题。请确保您提供了足够的背景信息,以便其他人能够理解您的问题。]
具体细节和上下文:
launch 启动报错 Caught multiple exceptions when trying to load file of format [py](ROS2 HUMBLE)
使用了VsCode的Ros插件启动launch文件
启动运行之前使用了以下脚本进行工程的构建if [ "$SHELL" = "/bin/zsh" ]; then echo "source /opt/ros/humble/setup.zsh" source /opt/ros/humble/setup.zsh elif [ "$SHELL" = "/bin/bash" ]; then echo "source /opt/ros/humble/setup.bash" source /opt/ros/humble/setup.bash else echo "source /opt/ros/humble/setup.sh" source /opt/ros/humble/setup.sh fi # 清理robot_tracker的旧文件 echo "清理robot_tracker的旧文件" rm -rf build/robot_tracker install/robot_tracker rm -rf src/robot_tracker/launch/__pycache__ mkdir -p build/robot_tracker install/robot_tracker # 清理robot_interfaces的旧文件 echo "清理robot_interfaces的旧文件" rm -rf build/robot_interfaces install/robot_interfaces mkdir -p build/robot_interfaces install/robot_interfaces # 清理robot_sensor的旧文件 echo "清理robot_sensor的旧文件" rm -rf build/robot_sensor install/robot_sensor mkdir -p build/robot_sensor install/robot_sensor # 构建 robot_interfaces echo "构建 robot_interfaces..." colcon build --packages-select robot_interfaces if [ "$SHELL" = "/bin/zsh" ]; then echo ". install/setup.zsh" . install/setup.zsh elif [ "$SHELL" = "/bin/bash" ]; then echo ". install/setup.bash" . install/setup.bash else echo ". install/setup.sh" . install/setup.sh fi # 构建 robot_sensor echo "构建 robot_sensor..."g colcon build --packages-select robot_sensor --symlink-install --event-handlers console_cohesion+ --cmake-args -DCMAKE_BUILD_TYPE=Debug if [ "$SHELL" = "/bin/zsh" ]; then echo ". install/setup.zsh" . install/setup.zsh elif [ "$SHELL" = "/bin/bash" ]; then echo ". install/setup.bash" . install/setup.bash else echo ". install/setup.sh" . install/setup.sh fi # 构建 robot_tracker echo "构建 robot_tracker..."g colcon build --packages-select robot_tracker --symlink-install --event-handlers console_cohesion+ --cmake-args -DCMAKE_BUILD_TYPE=Debug if [ "$SHELL" = "/bin/zsh" ]; then echo ". install/setup.zsh" . install/setup.zsh elif [ "$SHELL" = "/bin/bash" ]; then echo ". install/setup.bash" . install/setup.bash else echo ". install/setup.sh" . install/setup.sh fi echo "构建完成。"
task.json中有如下任务:
{ "label": "build:debug_robot_tracker", "type": "shell", "command": ". script/build_debug_robot_tracker.sh", "options": { "cwd": "${workspaceFolder}" // 设置工作目录为当前工作区 }, "presentation": { "reveal": "always", "panel": "shared", "clear": false }, "group": "build" },
luanch.json中有如下启动任务:
{ "name" : "ROS2_Debug: robot_tracker", "type" : "ros", "request" : "debug_launch", "target" : "${workspaceFolder}/install/robot_tracker/share/robot_tracker/launch/debug_robot_tracker.launch.py", "preLaunchTask": "build:debug_robot_tracker", },
尝试过的解决方法:
- [描述您已经尝试过的任何解决方法或方法。这可以帮助其他人了解您遇到问题的具体情况,并避免给出与您已经尝试过的解决方案相同的答案。]
# *************************************************** # *******Copyright (C),2019-2099,Coder******* # * 文件名 : debug_robot_tracker.launch.py # * 文件功能 : Robot Sensor包Debug版本的launch文件 # * 编程者 : Coder # * 日期 : 2024.11.05 # * 编号-----作者---------日期-------注释 # * 00 Coder 2024.11.05 创建 # * 说明:Robot Sensor包Debug版本的launch文件 # *************************************************** import os from launch import LaunchDescription from launch_ros.actions import Node from launch.events import Shutdown from launch.event_handlers import OnProcessExit from launch.substitutions import LaunchConfiguration from ament_index_python.packages import get_package_share_directory from launch.actions import DeclareLaunchArgument, EmitEvent, RegisterEventHandler def generate_launch_description(): robot_tracker_share_directory = get_package_share_directory('robot_tracker') robot_sensor_share_directory = get_package_share_directory('robot_sensor') rviz_config_file = LaunchConfiguration('rviz_config') # RViz配置文件相对路径 robot_tracker_yaml_config_file = LaunchConfiguration('robot_tracker_yaml_config') # RViz配置文件相对路径 robot_sensor_yaml_config_file = LaunchConfiguration('robot_sensor_yaml_config') # RViz配置文件相对路径 declare_robot_tracker_yaml_config_file_cmd = DeclareLaunchArgument( 'robot_tracker_yaml_config', default_value = os.path.join(robot_tracker_share_directory, 'config', 'robot_tracker.yaml'), description='Full path to the robot_tracker YAML config file to use') declare_robot_sensor_yaml_config_file_cmd = DeclareLaunchArgument( 'robot_sensor_yaml_config', default_value = os.path.join(robot_sensor_share_directory, 'config', 'robot_sensor.yaml'), description='Full path to the robot_sensor YAML config file to use') declare_rviz_config_file_cmd = DeclareLaunchArgument( 'rviz_config', default_value = os.path.join(robot_tracker_yaml_config_file, 'rviz', 'robot_tracker.rviz'), description='Full path to the RVIZ config file to use') start_robot_sensor_cmd = Node( package='robot_sensor', # Robot Sensor软件包 executable='robot_sensor_node', # 可执行文件为robot_tracker_node name='robot_sensor_node', # 节点名称为robot_tracker parameters=[robot_sensor_yaml_config_file]) start_robot_tracker_cmd = Node( package='robot_tracker', # Robot Sensor软件包 executable='robot_tracker_node', # 可执行文件为robot_tracker_node name='robot_tracker_node', # 节点名称为robot_tracker parameters=[robot_tracker_yaml_config_file]) # 启动RViz start_rviz_cmd = Node( package='rviz2', # RViz2软件包 executable='rviz2', # 可执行文件为rviz2 arguments=['-d', rviz_config_file], output='screen',) # 输出到屏幕 # 退出事件处理器 exit_event_handler = RegisterEventHandler( event_handler=OnProcessExit( target_action=start_rviz_cmd, # 关联的目标动作是启动RViz on_exit=EmitEvent(event=Shutdown(reason='rviz exited')))) # 退出时触发关闭事件 # 创建启动描述并填充 ld = LaunchDescription() ld.add_action(declare_robot_tracker_yaml_config_file_cmd) # 添加YAML配置文件参数 ld.add_action(declare_robot_sensor_yaml_config_file_cmd) # 添加YAML配置文件参数 ld.add_action(declare_rviz_config_file_cmd) # 添加RViz配置文件参数 ld.add_action(start_robot_sensor_cmd) # 启动Robot Sensor ld.add_action(start_robot_tracker_cmd) # 启动Robot Sensor ld.add_action(start_rviz_cmd) # 启动RViz ld.add_action(exit_event_handler) # 添加退出事件处理器 return ld # 返回构建好的启动描述 if __name__ == '__main__': generate_launch_description()
环境如下
Executing dumper with the following environment:
{
"SHELL": "/bin/zsh",
"ROS_VERSION": "2",
"LSCOLORS": "Gxfxcxdxbxegedabagacad",
"LESS": "-R",
"APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL": "1",
"ROS_PYTHON_VERSION": "3",
"ELECTRON_RUN_AS_NODE": "1",
"PWD": "/home/lixinlong/robot_ws",
"LOGNAME": "lixinlong",
"XDG_SESSION_TYPE": "tty",
"VSCODE_ESM_ENTRYPOINT": "vs/workbench/api/node/extensionHostProcess",
"MOTD_SHOWN": "pam",
"HOME": "/home/lixinlong",
"AUTOJUMP_ERROR_PATH": "/home/lixinlong/.local/share/autojump/errors.log",
"LANG": "C",
"LS_COLORS": "",
"SSL_CERT_DIR": "/usr/lib/ssl/certs",
"AMENT_PREFIX_PATH": "/home/lixinlong/robot_ws/install/robot_sensor:/home/lixinlong/robot_ws/install/robot_interfaces:/opt/ros/humble",
"CMAKE_PREFIX_PATH": "/home/lixinlong/robot_ws/install/robot_sensor:/home/lixinlong/robot_ws/install/robot_interfaces",
"VSCODE_AGENT_FOLDER": "/home/lixinlong/.vscode-server",
"VSCODE_L10N_BUNDLE_LOCATION": "vscode-local:/c%3A/Users/11233/.vscode/extensions/ms-ceintl.vscode-language-pack-zh-hans-1.95.2024103009/translations/extensions/vscode.json-language-features.i18n.json",
"AUTOENV_HOME": "/home/lixinlong/.autoenv",
"COLCON_PREFIX_PATH": "/home/lixinlong/robot_ws/install",
"SSH_CONNECTION": "192.168.196.1 11987 192.168.196.128 22",
"XDG_SESSION_CLASS": "user",
"VSCODE_HANDLES_SIGPIPE": "true",
"PYTHONPATH": "/home/lixinlong/robot_ws/install/robot_interfaces/local/lib/python3.10/dist-packages:/opt/ros/humble/lib/python3.10/site-packages:/opt/ros/humble/local/lib/python3.10/dist-packages",
"ZSH": "/home/lixinlong/.oh-my-zsh",
"USER": "lixinlong",
"AUTOJUMP_SOURCED": "1",
"DISPLAY": "192.168.10.4:0.0",
"SHLVL": "0",
"PAGER": "less",
"VSCODE_CWD": "/home/lixinlong",
"XDG_SESSION_ID": "3",
"LD_LIBRARY_PATH": "/home/lixinlong/robot_ws/install/robot_interfaces/lib:/opt/ros/humble/opt/rviz_ogre_vendor/lib:/opt/ros/humble/lib/x86_64-linux-gnu:/opt/ros/humble/lib",
"XDG_RUNTIME_DIR": "/run/user/1000",
"SSL_CERT_FILE": "/usr/lib/ssl/certs/ca-certificates.crt",
"ROS_LOCALHOST_ONLY": "0",
"SSH_CLIENT": "192.168.196.1 11987 22",
"VSCODE_CLI_REQUIRE_TOKEN": "7fcae6c1-9ef5-477b-8ca8-391800a5fede",
"LC_ALL": "en_US.UTF-8",
"XDG_DATA_DIRS": "/usr/share/gnome:/usr/local/share:/usr/share:/var/lib/snapd/desktop",
"BROWSER": "/home/lixinlong/.vscode-server/cli/servers/Stable-e8653663e8840adaf45af01eab5c627a5af81807/server/bin/helpers/browser.sh",
"PATH": "/home/lixinlong/.local/bin:/home/lixinlong/.local/bin:/home/lixinlong/.vscode-server/cli/servers/Stable-e8653663e8840adaf45af01eab5c627a5af81807/server/bin/remote-cli:/opt/ros/humble/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin",
"DBUS_SESSION_BUS_ADDRESS": "unix:path=/run/user/1000/bus",
"VSCODE_NLS_CONFIG": "{"userLocale":"zh-cn","osLocale":"zh-cn","resolvedLanguage":"zh-cn","defaultMessagesFile":"/home/lixinlong/.vscode-server/cli/servers/Stable-e8653663e8840adaf45af01eab5c627a5af81807/server/out/nls.messages.json","languagePack":{"translationsConfigFile":"/home/lixinlong/.vscode-server/data/clp/992791cd017d0441f609c48108aa8c37.zh-cn/tcf.json","messagesFile":"/home/lixinlong/.vscode-server/data/clp/992791cd017d0441f609c48108aa8c37.zh-cn/e8653663e8840adaf45af01eab5c627a5af81807/nls.messages.json","corruptMarkerFile":"/home/lixinlong/.vscode-server/data/clp/992791cd017d0441f609c48108aa8c37.zh-cn/corrupted.info"},"locale":"zh-cn","availableLanguages":{"*":"zh-cn"},"_languagePackId":"992791cd017d0441f609c48108aa8c37.zh-cn","_languagePackSupport":true,"_translationsConfigFile":"/home/lixinlong/.vscode-server/data/clp/992791cd017d0441f609c48108aa8c37.zh-cn/tcf.json","_cacheRoot":"/home/lixinlong/.vscode-server/data/clp/992791cd017d0441f609c48108aa8c37.zh-cn","_resolvedLanguagePackCoreLocation":"/home/lixinlong/.vscode-server/data/clp/992791cd017d0441f609c48108aa8c37.zh-cn/e8653663e8840adaf45af01eab5c627a5af81807","corruptedFile":"/home/lixinlong/.vscode-server/data/clp/992791cd017d0441f609c48108aa8c37.zh-cn/corrupted.info"}",
"ROS_DISTRO": "humble",
"VSCODE_HANDLES_UNCAUGHT_ERRORS": "true",
"OLDPWD": "/home/lixinlong",
"VSCODE_IPC_HOOK_CLI": "/run/user/1000/vscode-ipc-891140e9-a245-4594-a6b3-063bb34445e3.sock",
"": "/usr/bin/env"
}错误时日志:
cd /home/lixinlong/robot_ws ; /usr/bin/env /usr/bin/python3.10 /home/lixinlong/.vscode-server/extensions/ms-python.debugpy-2024.12.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher 41575 -- /home/lixinlong/.vscode-server/extensions/ms-iot.vscode-ros-0.9.2/assets/scripts/ros2_launch_dumper.py /home/lixinlong/robot_ws/install/robot_tracker/share/robot_tracker/launch/debug_robot_tracker.launch.py
Caught multiple exceptions when trying to load file of format [py]:- PackageNotFoundError: "package 'robot_tracker' not found, searching: ['/home/lixinlong/robot_ws/install/robot_sensor', '/home/lixinlong/robot_ws/install/robot_interfaces', '/opt/ros/humble']"
- InvalidFrontendLaunchFileError: The launch file may have a syntax error, or its format is unknown
-
根据您提供的错误信息和代码,这里有几个可能导致启动文件
debug_robot_tracker.launch.py
报错的问题。我将逐一分析并提供可能的解决方案:-
包未找到错误(PackageNotFoundError):
错误信息PackageNotFoundError: "package 'robot_tracker' not found, searching: ['/home/lixinlong/robot_ws/install/robot_sensor', '/home/lixinlong/robot_ws/install/robot_interfaces', '/opt/ros/humble']
表明系统无法在指定的路径中找到robot_tracker
包。解决方案:
- 确保
robot_tracker
包已经正确构建并安装在 ROS 2 工作空间中。检查robot_ws/src/
目录下是否存在robot_tracker
包的源代码。 - 确认你的工作空间已经被正确设置并源代码已经编译。你可以使用
colcon build
命令来构建工作空间。 - 确保
source install/setup.bash
已经被执行,以便设置环境变量,让 ROS 2 能够找到新安装的包。
- 确保
-
RViz配置文件路径错误:
在代码中,declare_rviz_config_file_cmd
的default_value
使用了robot_tracker_yaml_config_file
变量,这是不正确的,因为robot_tracker_yaml_config_file
是一个指向 YAML 配置文件的路径,而不是一个目录。解决方案:
- 修改
declare_rviz_config_file_cmd
的default_value
以正确指向 RViz 配置文件的路径。例如:declare_rviz_config_file_cmd = DeclareLaunchArgument( 'rviz_config', default_value=os.path.join(robot_tracker_share_directory, 'rviz', 'robot_tracker.rviz'), description='Full path to the RVIZ config file to use')
- 修改
-
代码格式和语法:
你的代码在 Python 脚本的末尾有一个语法错误。if name == 'main':
应该是if __name__ == '__main__':
。解决方案:
- 修改代码末尾的
if
语句为if __name__ == '__main__':
。
- 修改代码末尾的
-
使用正确的 Python 解释器和环境:
确保你在使用正确的 Python 解释器和 ROS 2 环境。有时候,特别是在使用 VSCode 或其他 IDE 时,可能需要手动配置 Python 解释器路径。
应用以上解决方案后,重新尝试运行你的启动文件。如果问题仍然存在,可能需要检查 ROS 2 的日志输出以获取更多信息。
以上为生成回答,仅供参考~
-
-
@1123347532 差个source
-
@小鱼 在 launch启动报错 中说:
@1123347532 差个source
脚本里面写. install/setup.zsh了但是还是找不到,即便是在终端里把脚本按顺序敲一遍也没有,但是install目录下能看到robot_tracker这个编译的包
-
@1123347532 把环境变量都检查一遍
-
问题已解决,原因是环境变量AMENT_PREFIX_PATH错误,虽然没有找到这个变量为什么出错导致查找不到robot_tracker包,但我通过在lauch.json配置中手动修复该环境变量后启动正常,修改前配置为
// robot_tracker_launch_task { "name" : "ROS2_Debug: robot_tracker", "type" : "ros", "request" : "launch", "target" : "${workspaceFolder}/install/robot_tracker/share/robot_tracker/launch/debug_robot_tracker.launch.py", "preLaunchTask": "build:debug_robot_tracker", "launch": ["rviz"] },
修改后配置为
// robot_tracker_launch_task { "name" : "ROS2_Debug: robot_tracker", "type" : "ros", "request" : "launch", "target" : "${workspaceFolder}/install/robot_tracker/share/robot_tracker/launch/debug_robot_tracker.launch.py", "preLaunchTask": "build:debug_robot_tracker", "launch": ["rviz"], "env":{ "AMENT_PREFIX_PATH" : "/opt/ros/humble:${workspaceFolder}/install/robot_tracker" } },
@小鱼 感谢大佬