ROS(4)发布订阅-服务客户-广播监听总结

发布服务

Publisher topic(/turtle1/cmd_vel) Subscriber

Twist linear.x .y .z angular.x .y .z

publisher.publish(Twist)

Publisher topic(/turtle1/pose) Subscriber

Pose .x .y .theta

CallBack(Pose::ConstPtr & msg)

Publisher

ros::Publisher my_publisher = node.advertise<数据类型>(“话题名称”,队列长度);

my_publisher.publish(发布数据);

ros::Rate rate(10); rate.sleep(); 设置循环频率 按照循环频率延时

Subscriber

ros::Subscriber my_subscriber = node.subscriber<数据类型>(“话题名称”,1,CallBack);

ros::spin(); 循环处理回调函数

服务客户

Server service(/turtle_command) Client

rosserver call /turtle_command “{}” 命令调用服务

Server service(/spawn) Client

turtlesim::Spawn spawn

client.call(spawn); 客户端调用服务

Server

ros::ServiceServer my_server = node.advertiseService(“服务名称”,CallBack);

bool Callback(数据类型::Request & req, 数据类型::Response & res)

ros::spinOnce(); 一次一次处理回调函数

Client

ros::service::waitForService(“服务名称”);

ros::ServiceClient my_client = node.serviceClient<数据类型>(“服务名称”);

数据类型 msg;

my_client.call(msg);

广播监听

Broadcaster

static tf::TransformBroadcaster br;

br.sendTransform();

Listener

tf::TransformListener my_listener;

my_listener.waitForTransform(“/turtle2”,”/turtle1”,ros::Time(0),ros::Duration(3.0));

ros::Time(0) 使用缓冲中最新的tf数据

my_listener.lookupTransform(“/turtle2”,”turtle1”,ros::Time(0),transform);

你可能感兴趣的:(机器人)