我们之前已经可以通过launch文件调用与rostopic echo相结合的方式输出小乌龟的位置和姿势,不过我们也可以通过编写cpp文件输出小乌龟的位置与姿势的信息。
我们首先在packge.xml中修改添加功能包,修改如下:
添加以下两端代码<build_depend>turtlesim</build_depend>
<exec_depend>turtlesim</exec_depend>
之后我们再在CMakeList.txt中find package中修改添加turtlesim来调用相应功能包。
之后我们就可以开始编译相应CPP文件了,创立pose.cpp文件,代码如下:
#include "ros/ros.h"
#include "turtlesim/Pose.h"
void doPose(const turtlesim::Pose::ConstPtr &pose){
ROS_INFO("Turtle's info are position:( %.2f , %.2f), theta:%.2f , linear volecity: %.2f ,angular volecity: %.2f",
pose->x,pose->y,pose->theta,pose->linear_velocity,pose->angular_velocity);
}
int main(int argc,char *argv[]){
setlocale(LC_ALL,"");
ros::init(argc,argv,"pose");
ros::NodeHandle n;
ros::Subscriber sub=n.subscribe("turtle1/pose",100,doPose);
ros::spin();
return 0;
}
之后再在CMakeList.txt中添加代码如下:add_executable(pose src/pose.cpp)
add_dependencies(pose ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(pose
${catkin_LIBRARIES}
)
先catkin_make编译,再source ./devel/setup.bash调用相应资源,然后先调用launch文件: roslaunch test01 turtle.launch,之后再rosrun test01 pose调用文件,便可以实时输出小乌龟的位置与姿势。