小鱼 ROS 2 新书上线!点击链接查看, 新书配套视频点击链接查看。
提问前必看的发帖注意事项—— 提问前必看!不符合要求的问题拒绝回答!!
社区使用指南—如何添加标签修改密码
4.2.1自定义服务接口 编译chapt4_interfaces报错
-
问题描述:
跟随视频学习4.2.1自定义服务接口时,编译chapt4_interfaces时报错
报错信息如下:
xothschild@LAPTOP-LNO5LL2V:~/YuXiangROS2/chapt4/chatp4_ws$ colcon build Starting >>> chapt4_interfaces --- stderr: chapt4_interfaces CMake Error at /opt/ros/humble/share/rosidl_adapter/cmake/rosidl_adapt_interfaces.cmake:59 (message): execute_process(/usr/bin/python3 -m rosidl_adapter --package-name chapt4_interfaces --arguments-file /home/xothschild/YuXiangROS2/chapt4/chatp4_ws/build/chapt4_interfaces/rosidl_adapter__arguments__chapt4_interfaces.json --output-dir /home/xothschild/YuXiangROS2/chapt4/chatp4_ws/build/chapt4_interfaces/rosidl_adapter/chapt4_interfaces --output-file /home/xothschild/YuXiangROS2/chapt4/chatp4_ws/build/chapt4_interfaces/rosidl_adapter/chapt4_interfaces.idls) returned error code 1: Traceback (most recent call last): File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "/usr/lib/python3.10/runpy.py", line 86, in _run_code exec(code, run_globals) File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_adapter/__main__.py", line 19, in <module> sys.exit(main()) File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_adapter/main.py", line 53, in main abs_idl_file = convert_to_idl( File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_adapter/__init__.py", line 24, in convert_to_idl return convert_srv_to_idl( File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_adapter/srv/__init__.py", line 28, in convert_srv_to_idl srv = parse_service_string(package_name, input_file.stem, content) File "/opt/ros/humble/local/lib/python3.10/dist-packages/rosidl_adapter/parser.py", line 850, in parse_service_string raise InvalidServiceSpecification( rosidl_adapter.parser.InvalidServiceSpecification: Could not find separator '---' between request and response Call Stack (most recent call first): /opt/ros/humble/share/rosidl_cmake/cmake/rosidl_generate_interfaces.cmake:130 (rosidl_adapt_interfaces) CMakeLists.txt:13 (rosidl_generate_interfaces) --- Failed <<< chapt4_interfaces [1.30s, exited with code 1] Summary: 0 packages finished [1.46s] 1 package failed: chapt4_interfaces 1 package had stderr output: chapt4_interfaces
查了一下论坛,路径中没有中文,代码也没有写错,想问一下这个怎么改?
谢谢!
-
从您提供的错误信息来看,问题出现在
rosidl_adapter
试图解析服务定义文件时,因为它没有找到分隔请求和响应部分的---
标记。这通常意味着您的服务定义文件(.srv
文件)格式可能不正确。在 ROS 2 中,服务定义文件(
.srv
文件)必须严格遵循以下格式:# 请求部分 int64 seq string data --- # 响应部分 bool success string message
这里的
---
是必需的,它将请求参数和响应参数分隔开。解决步骤
-
检查服务定义文件:
打开您的服务定义文件(通常在chapt4_interfaces/srv/
目录下),确保它包含正确的---
分隔符,并且格式正确。例如:# MyService.srv int64 seq string data --- bool success string message
-
检查文件名和路径:
确保文件名和路径中没有特殊字符或不必要的空格,这可能会影响解析。 -
清理和重新构建:
在您的工作空间中运行以下命令来清理旧的构建文件并重新构建:colcon build --packages-select chapt4_interfaces --packages-up-to chapt4_interfaces --cmake-clean-first
这个命令会仅构建
chapt4_interfaces
包,并在构建前清理旧的 CMake 文件。 -
检查 ROS 2 环境:
确保您的 ROS 2 环境设置正确,所有依赖项都已正确安装。 -
查看详细日志:
如果问题仍然存在,尝试运行构建命令时添加更多的调试输出,以获取更多关于错误的信息:colcon build --packages-select chapt4_interfaces --packages-up-to chapt4_interfaces --cmake-verbose
按照这些步骤操作后,您应该能够解决编译时遇到的问题。如果问题仍然存在,请仔细检查
.srv
文件的内容,确保没有语法错误或格式问题。
以上为生成回答,仅供参考~
-
-
@739223875 完整代码贴出来看看
-
FaceDetector.srv
sensor_msgs/Image Image # 人脸图像 --- # 横杠分割,上面是请求部分,下面是结果 int16 number float32 use_time int32[] top int32[] right int32[] bottom int32[] left
CMakeLists.txt
cmake_minimum_required(VERSION 3.8) project(chapt4_interfaces) if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") add_compile_options(-Wall -Wextra -Wpedantic) endif() # find dependencies find_package(ament_cmake REQUIRED) find_package(sensor_msgs REQUIRED) find_package(rosidl_default_generators REQUIRED) rosidl_generate_interfaces(${PROJECT_NAME} "srv/FaceDetector.srv" DEPENDENCIES sensor_msgs ) if(BUILD_TESTING) find_package(ament_lint_auto REQUIRED) # the following line skips the linter which checks for copyrights # comment the line when a copyright and license is added to all source files set(ament_cmake_copyright_FOUND TRUE) # the following line skips cpplint (only works in a git repo) # comment the line when this package is in a git repo and when # a copyright and license is added to all source files set(ament_cmake_cpplint_FOUND TRUE) ament_lint_auto_find_test_dependencies() endif() ament_package()
package.xml
<?xml version="1.0"?> <?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?> <package format="3"> <name>chapt4_interfaces</name> <version>0.0.0</version> <description>TODO: Package description</description> <maintainer email="Jiahao.X@outlook.com">xothschild</maintainer> <license>Apache-2.0</license> <buildtool_depend>ament_cmake</buildtool_depend> <member_of_group>rosidl_interface_packages</member_of_group> <depend>sensor_msgs</depend> <depend>rosidl_default_generators</depend> <test_depend>ament_lint_auto</test_depend> <test_depend>ament_lint_common</test_depend> <export> <build_type>ament_cmake</build_type> </export> </package>
-
@739223875 在 4.2.1自定义服务接口 编译chapt4_interfaces报错 中说:
rosidl_adapter.parser.InvalidServiceSpecification: Could not find separator
'---' between request and response@739223875 在 4.2.1自定义服务接口 编译chapt4_interfaces报错 中说:
--- # 横杠分割,上面是请求部分,下面是结果
后面注释删掉