关于apriltag_ros订阅内容网上参考资料较少,整理了一下过程中踩的坑,给大家排一下雷。 参考文章古月居 参考链接
1.环境
ubuntu 18.04
ROS melodic
2.代码
#include "ros/ros.h"
#include "apriltag_ros/AprilTagDetectionArray.h"
#include "iostream"
using namespace std;
ros::Subscriber ar_sub_;
class Localizer
{
public:
Localizer(ros::NodeHandle& nh)
{
ar_sub_ = nh.subscribe<apriltag_ros::AprilTagDetectionArray>("/tag_detections", 1, &Localizer::number_callback, this);
}
void number_callback(const apriltag_ros::AprilTagDetectionArray::ConstPtr& msg)
{
if(msg->detections.size()>0)
{
float getX = msg->detections[0].pose.pose.pose.position.x;
cout<<"Point position:"<<endl;
cout<<"cam_getX: "<<getX<<endl; }
else
{cout<<"未检测到tag"<<endl;}
}
};
int main(int argc, char **argv)
{
ros::init(argc, argv,"apriltag_detector_subscriber");
ros::NodeHandle node_obj;
Localizer localizer(node_obj);
ROS_INFO("节点开始");
ros::spin();
return 0;
}
3.代码解析,CMake配置
头文件
#include "ros/ros.h"
#include "apriltag_ros/AprilTagDetectionArray.h"
#include "iostream"
此时如果报找不到相关头文件 要去改CMakeLists.txt 文件 在find_package处加入相关的功能包即可 除此之外,其他配置如下所示
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
apriltag_ros
)
add_executable(apriltag_sub src/apriltag_sub.cpp)
add_dependencies(apriltag_sub ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(apriltag_sub
${catkin_LIBRARIES}
)
4.小结
总结了一下订阅节点的笔记 首先找到想到订阅的话题 通过
rostopic list
找到/tag_detections 话题 查看话题的消息类型
rostopic type /tag_detections
发现是apriltag_ros/AprilTagDetectionArray类型 就可以写订阅节点了
|