小鱼 ROS 2 新书上线!点击链接查看, 新书配套视频点击链接查看。
提问前必看的发帖注意事项—— 提问前必看!不符合要求的问题拒绝回答!!
社区使用指南—如何添加标签修改密码
ros2 create_subscription出错
-
在一个节点中添加了一个subscriber时,使用create_subscription初始化失败,前后反复确认,发现该代码注释后可正常运行,由此锁定问题就在订阅初始化处。此外,通过ros2 topic list命令已经确认话题已正常发布并存在,仅仅是在此节点订阅出错误。
由上图知,报错信息为
"what(): this specialization of rclcpp::get_message_type_support_handle() should never be called"cpp文件涉及代码为
serial_receive_data_sub_ = this->create_subscription<rm_interfaces::msg::SerialReceiveData>( "/serial/receive", rclcpp::SensorDataQoS(), std::bind(&ArmorDetectorNode::serialdataCallback, this, std::placeholders::_1)); void ArmorDetectorNode::serialdataCallback(const rm_interfaces::msg::SerialReceiveData::ConstSharedPtr receive_data_) { float pitch = static_cast<float>(-receive_data_->pitch * M_PI / 180.0); float yaw = static_cast<float>(receive_data_->yaw * M_PI / 180.0); angle[1] = pitch; angle[2] = yaw; }
hpp文件涉及代码为
void serialdataCallback(const rm_interfaces::msg::SerialReceiveData::ConstSharedPtr receive_data_); rclcpp::Subscription<rm_interfaces::msg::SerialReceiveData>::SharedPtr serial_receive_data_sub_; float angle[3];
其中rm_interface为我自己定义的消息类型
注:
ubuntu版本为22.04.01
ros版本为ros2-humble -
此问题已经解决,发现是由于未包含对应消息的头文件所致,无法识别消息类型,从而无法创建相应对象。
此外,我使用clangd并开启了自动添加头文件功能,但由于其本身局限性,添加了错误路径(install下生成的文件),因此编译顺利通过,运行中才抛出错误 -
@3110379921 可能是依赖问题,你自定义的接口没有正确link上依赖,再次依赖就会出错。
-
@小鱼 具体应该如何解决呢?在package.xml文件中我已添加自定义的rm_interface依赖以及一些必要的依赖
下面是我的package.xml和CMakeLists.txt文件
<?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>armor_detector</name> <version>0.1.0</version> <description>A template for ROS packages.</description> <maintainer email="chen.junn@outlook.com">Chen Jun</maintainer> <license>BSD</license> <url type="website">https://github.com/chenjunnn/rm_auto_aim</url> <url type="bugtracker">https://github.com/chenjunnn/rm_auto_aim/issues</url> <author email="chen.junn@outlook.com">Chen Jun</author> <!-- buildtool_depend: dependencies of the build process --> <buildtool_depend>ament_cmake</buildtool_depend> <!-- depend: build, export, and execution dependency --> <depend>rclcpp</depend> <depend>rclcpp_components</depend> <depend>sensor_msgs</depend> <depend>geometry_msgs</depend> <depend>std_srvs</depend> <depend>visualization_msgs</depend> <depend>message_filters</depend> <depend>cv_bridge</depend> <depend>image_transport</depend> <depend>image_transport_plugins</depend> <depend>rm_interfaces</depend> <depend>vision_opencv</depend> <depend>tf2</depend> <depend>eigen</depend> <depend>fmt</depend> <depend>tf2_ros</depend> <test_depend>ament_lint_auto</test_depend> <test_depend>ament_lint_common</test_depend> <test_depend>ament_cmake_clang_format</test_depend> <export> <build_type>ament_cmake</build_type> </export> </package>
cmake_minimum_required(VERSION 3.10) project(armor_detector) ## Use C++14 set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) ## By adding -Wall and -Werror, the compiler does not ignore warnings anymore, ## enforcing cleaner code. add_definitions(-Wall -Werror) ## Export compile commands for clangd set(CMAKE_EXPORT_COMPILE_COMMANDS ON) ####################### ## Find dependencies ## ####################### find_package(ament_cmake_auto REQUIRED) find_package(OpenCV REQUIRED) find_package(g2o REQUIRED) find_package(fmt REQUIRED) find_package(Sophus REQUIRED) ament_auto_find_build_dependencies() ########### ## Build ## ########### ament_auto_add_library(${PROJECT_NAME} SHARED DIRECTORY src ) target_include_directories(${PROJECT_NAME} PUBLIC ${OpenCV_INCLUDE_DIRS} ${G2O_INCLUDE_DIRS} ${Sophus_INCLUDE_DIRS} ${EIGEN3_INCLUDE_DIRS} ) target_link_libraries(${PROJECT_NAME} ${catkin_LIBRARIES} ${OpenCV_LIBS} g2o_core g2o_types_slam3d g2o_solver_csparse g2o_stuff g2o_csparse_extension g2o_solver_dense fmt::fmt ) rclcpp_components_register_node(${PROJECT_NAME} PLUGIN rm_auto_aim::ArmorDetectorNode EXECUTABLE armor_detector_node ) ############# ## Testing ## ############# if(BUILD_TESTING) find_package(ament_lint_auto REQUIRED) list(APPEND AMENT_LINT_AUTO_EXCLUDE ament_cmake_copyright ament_cmake_uncrustify ament_cmake_cpplint ) ament_lint_auto_find_test_dependencies() find_package(ament_cmake_gtest) ament_add_gtest(test_node_startup test/test_node_startup.cpp) target_link_libraries(test_node_startup ${PROJECT_NAME}) ament_add_gtest(test_number_cls test/test_number_cls.cpp) target_link_libraries(test_number_cls ${PROJECT_NAME}) endif() ############# ## Install ## ############# ament_auto_package( INSTALL_TO_SHARE model )
rm_interface中包含自定义消息和服务,且确定消息与服务已正常生成,且在该发生错误节点下,可正常发起服务请求,并可订阅其他类型的话题,例如:
只有serial_receive_data_sub_处请求失败
-
此回复已被删除! -
-
@小鱼 能否看一下呢,还是没解决这个问题
-
@3110379921 在 ros2 create_subscription出错 中说:
ament_auto_find_build_dependencies()
###########
Build
###########
ament_auto_add_library(${PROJECT_NAME} SHARED
DIRECTORY src
)试试不用auto,用正常的指令
-
此问题已经解决,发现是由于未包含对应消息的头文件所致,无法识别消息类型,从而无法创建相应对象。
此外,我使用clangd并开启了自动添加头文件功能,但由于其本身局限性,添加了错误路径(install下生成的文件),因此编译顺利通过,运行中才抛出错误 -
-
此回复已被删除!