赞
踩
#include <ros/ros.h>
#include <turtlesim/Pose.h>
#include <iomanip>
void poseMessageReceived ( const turtlesim::Pose& msg )
{
ROS_INFO_STREAM( std::setprecision ( 2 ) << std::fixed
<< " position =(" << msg.x << " , " << msg.y << " ) "
<< " *direction=" << msg.theta ) ;
}
int main ( int argc , char ** argv )
{
// Initialize the ROS system and become a node .
ros::init ( argc , argv , "subscribe_to_pose" ) ;
ros::NodeHandle nh ;
// Create a subscri ber object .
ros::Subscriber sub = nh.subscribe ( "turtle1/pose" ,1000,
&poseMessageReceived ) ;
// Let ROS take over .
ros::spin ( ) ;
}
poseMessageReceived 为订阅消息的回调函数
ros::Subscriber sub = nh.subscribe ( "turtle1/pose" ,1000,
&poseMessageReceived ) ;
为创建一个订阅对象
一.CMakeList.txt中增加如下代码:
- find_package(catkin REQUIRED COMPONENTS roscpp turtlesim)
- catkin_package()
- include_directories(include ${catkin_INCLUDE_DIRS})
- add_executable(subpose subpose.cpp)
- target_link_libraries(subpose ${catkin_LIBRARIES})
二.package.xml 中增加如下代码:
- <build_depend>roscpp</build_depend>
- <exec_depend>roscpp</exec_depend>
- <build_depend>turtlesim</build_depend>
- <exec_depend>turtlesim</exec_depend>
三。运行程序
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。