ROS:利用cpp代码输出小乌龟的位姿

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值