我们之前已经可以通过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

最低0.47元/天 解锁文章
1801

被折叠的 条评论
为什么被折叠?



