小鱼 ROS 2 新书上线!点击链接查看, 新书配套视频点击链接查看。
提问前必看的发帖注意事项—— 提问前必看!不符合要求的问题拒绝回答!!
社区使用指南—如何添加标签修改密码
ros2话题之rclcpp实现的运行测试报错
-
Starting >>> example_topic_rclcpp
--- stderr: example_topic_rclcpp
Error parsing '/home/tzx/d2lros2/chapt3/chapt3_ws/src/example_topic_rclcpp/package.xml':
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/catkin_pkg/package.py", line 610, in parse_package_string
root = dom.parseString(data)
File "/usr/lib/python3.10/xml/dom/minidom.py", line 2000, in parseString
return expatbuilder.parseString(string)
File "/usr/lib/python3.10/xml/dom/expatbuilder.py", line 925, in parseString
return builder.parseString(string)
File "/usr/lib/python3.10/xml/dom/expatbuilder.py", line 223, in parseString
parser.Parse(string, True)
xml.parsers.expat.ExpatError: junk after document element: line 21, column 2During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/opt/ros/humble/share/ament_cmake_core/cmake/core/package_xml_2_cmake.py", line 150, in <module>
main()
File "/opt/ros/humble/share/ament_cmake_core/cmake/core/package_xml_2_cmake.py", line 53, in main
raise e
File "/opt/ros/humble/share/ament_cmake_core/cmake/core/package_xml_2_cmake.py", line 49, in main
package = parse_package_string(
File "/usr/lib/python3/dist-packages/catkin_pkg/package.py", line 612, in parse_package_string
raise InvalidPackage('The manifest contains invalid XML:\n%s' % ex, filename)
catkin_pkg.package.InvalidPackage: Error(s) in package '/home/tzx/d2lros2/chapt3/chapt3_ws/src/example_topic_rclcpp/package.xml':
The manifest contains invalid XML:
junk after document element: line 21, column 2
CMake Error at /opt/ros/humble/share/ament_cmake_core/cmake/core/ament_package_xml.cmake:95 (message):
execute_process(/usr/bin/python3
/opt/ros/humble/share/ament_cmake_core/cmake/core/package_xml_2_cmake.py
/home/tzx/d2lros2/chapt3/chapt3_ws/src/example_topic_rclcpp/package.xml
/home/tzx/d2lros2/chapt3/chapt3_ws/build/example_topic_rclcpp/ament_cmake_core/package.cmake)
returned error code 1
Call Stack (most recent call first):
/opt/ros/humble/share/ament_cmake_core/cmake/core/ament_package_xml.cmake:49 (_ament_package_xml)
/opt/ros/humble/share/ament_lint_auto/cmake/ament_lint_auto_find_test_dependencies.cmake:31 (ament_package_xml)
CMakeLists.txt:21 (ament_lint_auto_find_test_dependencies)gmake: *** [Makefile:267:cmake_check_build_system] 错误 1
Failed <<< example_topic_rclcpp [1.13s, exited with code 2]
Summary: 0 packages finished [1.62s]
1 package failed: example_topic_rclcpp
1 package had stderr output: example_topic_rclcpp
以上这是运行报错内容cmake_minimum_required(VERSION 3.8)
project(example_topic_rclcpp)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(rclcpp REQUIRED)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()
add_executable(topic_publisher_01 src/topic_publisher_01.cpp)
ament_target_dependencies(topic_publisher_01 rclcpp)install(TARGETS
topic_publisher_01
DESTINATION lib/${PROJECT_NAME}
)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)add_executable(topic_publisher_01 src/topic_publisher_01.cpp)
ament_target_dependencies(topic_publisher_01 rclcpp std_msgs)
以上这是cmakelist内容
<?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>example_topic_rclcpp</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="tzx@todo.todo">tzx</maintainer>
<license>TODO: License declaration</license><buildtool_depend>ament_cmake</buildtool_depend>
<depend>rclcpp</depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend><export>
<build_type>ament_cmake</build_type>
</export>
</package>
<buildtool_depend>ament_cmake</buildtool_depend><depend>rclcpp</depend>
<depend>std_msgs</depend><test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
以上是package文件内容
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"class TopicPublisher01 : public rclcpp::Node
{
public:
// 构造函数,有一个参数为节点名称
TopicPublisher01(std::string name) : Node(name)
{
RCLCPP_INFO(this->get_logger(), "大家好,我是%s.", name.c_str());
// 创建发布者
command_publisher_ = this->create_publisher<std_msgs::msg::String>("command", 10);
// 创建定时器,500ms为周期,定时发布
timer_ = this->create_wall_timer(std::chrono::milliseconds(500), std::bind(&TopicPublisher01::timer_callback, this));
}private:
void timer_callback()
{
// 创建消息
std_msgs::msg::String message;
message.data = "forward";
// 日志打印
RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str());
// 发布消息
command_publisher_->publish(message);
}
// 声名定时器指针
rclcpp::TimerBase::SharedPtr timer_;
// 声明话题发布者指针
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr command_publisher_;
};
int main(int argc,char **argv)
{
rclcpp::init(argc,argv);
auto node=std::make_shared<TopicPublisher01>("topic_publisher_01");
rclcpp::spin(node);
rclcpp::shutdown();
return 0;
}
以上是主程序内容 -
从您提供的错误信息和文件内容来看,主要问题在于
package.xml
文件中有重复的依赖声明,并且这些重复的声明位于文件末尾,这导致了XML解析错误。此外,您的CMakeLists.txt
文件中有一些不必要的重复内容,这可能会导致构建过程中的混淆。解决
package.xml
问题您的
package.xml
文件中,<buildtool_depend>
,<depend>
, 和<test_depend>
标签被重复了。您应该只在文件中声明一次这些依赖。修改后的package.xml
应该如下所示:<?xml version="1.0"?> <package format="3"> <name>example_topic_rclcpp</name> <version>0.0.0</version> <description>TODO: Package description</description> <maintainer email="tzx@todo.todo">tzx</maintainer> <license>TODO: License declaration</license> <buildtool_depend>ament_cmake</buildtool_depend> <depend>rclcpp</depend> <depend>std_msgs</depend> <test_depend>ament_lint_auto</test_depend> <test_depend>ament_lint_common</test_depend> <export> <build_type>ament_cmake</build_type> </export> </package>
解决
CMakeLists.txt
问题在
CMakeLists.txt
中,您重复了find_package
和add_executable
的调用。此外,std_msgs
的find_package
应该在创建发布者之前调用。修改后的CMakeLists.txt
应该如下所示:cmake_minimum_required(VERSION 3.8) project(example_topic_rclcpp) if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") add_compile_options(-Wall -Wextra -Wpedantic) endif() find_package(ament_cmake REQUIRED) find_package(rclcpp REQUIRED) find_package(std_msgs REQUIRED) # 确保在创建发布者之前找到std_msgs if(BUILD_TESTING) find_package(ament_lint_auto REQUIRED) # 跳过版权和linter检查的设置(仅当实际添加版权和版权文件时取消注释) # set(ament_cmake_copyright_FOUND TRUE) # set(ament_cmake_cpplint_FOUND TRUE) ament_lint_auto_find_test_dependencies() endif() ament_package() add_executable(topic_publisher_01 src/topic_publisher_01.cpp) ament_target_dependencies(topic_publisher_01 rclcpp std_msgs) install(TARGETS topic_publisher_01 DESTINATION lib/${PROJECT_NAME} )
其他建议
- 确保在
package.xml
和CMakeLists.txt
中添加了必要的许可证和描述信息。 - 在代码中添加适当的注释和文档,以提高可读性和可维护性。
- 如果可能的话,使用版本控制系统(如Git)来管理您的ROS 2项目。
完成这些修改后,您应该能够成功构建和运行您的ROS 2节点。
以上为生成回答,仅供参考~
- 确保在
-
@1362377531 如果是学习ROS2,请换到最新的教程和书籍:https://www.bilibili.com/video/BV1GW42197Ck/ 上来,之前的动手学ROS2相关教程停止答疑,经历有限,只能维护一份,还请理解。