ROS学习(四)--1.发布者与订阅者案例

目录

一、发布者

1.创建功能包 learning_topic

2.创建发布者代码(velocity_publisher.cpp) 

3.配置CMakeLists.txt中的编译规则

​编辑4.编译

5.设置环境变量

6.运行发布者

二、订阅者

1.创建pose_subscriber.cpp文件

2.配置CMakeLists.txt中的编译规则

​编辑 3.编译

4.设置环境变量

5.运行发布者

6.加上发布者


实现内容:通过发布话题控制海龟运动,并且通过订阅话题接受海龟实时位置


ROS发布话题与订阅话题https://download.csdn.net/download/m0_56451176/86398720?spm=1001.2014.3001.5503

一、发布者

1.创建功能包 learning_topic

cd ~/catkin_ws/src
catkin_create_pkg learning_topic roscpp rospy std_msgs geometry_msgs turtlesim

2.创建发布者代码(velocity_publisher.cpp) 

cd learning_topic/src
touch velocity_publisher.cpp
gedit velocity_publisher.cpp

代码内容如下: 

//该例程将发布turtle1/cmd_vel话题,消息类型geometry_msgs::Twist
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
int main(int argc, char **argv)
{
	// ROS节点初始化
	ros::init(argc, argv, "velocity_publisher");

	// 创建节点句柄
	ros::NodeHandle n;

	// 创建一个Publisher,发布名为/turtle1/cmd_vel的topic,消息类型为geometry_msgs::Twist,队列长度10
	ros::Publisher turtle_vel_pub = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel", 10);

	// 设置循环的频率
	ros::Rate loop_rate(10);
	int count = 0;
	while (ros::ok())
    {
	    // 初始化geometry_msgs::Twist类型的消息
		geometry_msgs::Twist vel_msg;
		vel_msg.linear.x = 0.5;
		vel_msg.angular.z = 0.2;

	    // 发布消息
		turtle_vel_pub.publish(vel_msg);
		ROS_INFO("Publsh turtle velocity command[%0.2f m/s, %0.2f rad/s]", 
		vel_msg.linear.x, vel_msg.angular.z);

	    // 按照循环频率延时
	    loop_rate.sleep();
	}
	return 0;
}

注:

vel_msg.linear.x = 0.5;  //设置海龟x方向线速度为0.5m/s
vel_msg.angular.z = 0.2; //设置海龟z方向角速度为0.2rad/s

3.配置CMakeLists.txt中的编译规则

CMakeLists.txt中build区域下加上:

add_executable(velocity_publisher src/velocity_publisher.cpp)
target_link_libraries(velocity_publisher ${catkin_LIBRARIES})

4.编译

cd ~/catkin_ws
catkin_make

5.设置环境变量

source devel/setup.bash		#需要配置环境变量,否则系统无法找到运行程序

6.运行发布者

运行ros核心 

roscore

启动海龟仿真器 

rosrun turtlesim turtlesim_node

 发布话题

注:打开新的终端需要加上source ~/catkin_ws/devel/setup.bash    

source ~/catkin_ws/devel/setup.bash
rosrun learning_topic velocity_publisher


二、订阅者

1.创建pose_subscriber.cpp文件

话题功能包已存在~/catkin_ws/src
在功能包的src文件夹建立pose_subscriber.cpp文件

cd ~/catkin_ws/src/learning_topic/src
touch pose_subscriber.cpp
gedit pose_subscriber.cpp

代码内容如下: 

//该例程将订阅/turtle1/pose话题,消息类型turtlesim::Pose
#include <ros/ros.h>
#include "turtlesim/Pose.h"
// 接收到订阅的消息后,会进入消息回调函数
void poseCallback(const turtlesim::Pose::ConstPtr& msg)
{
    // 将接收到的消息打印出来
    ROS_INFO("Turtle pose: x:%0.6f, y:%0.6f", msg->x, msg->y);
}

int main(int argc, char **argv)
{
    // 初始化ROS节点
    ros::init(argc, argv, "pose_subscriber");
    // 创建节点句柄,管理节点资源
    ros::NodeHandle n;
    // 创建一个Subscriber,订阅名为/turtle1/pose的topic,注册回调函数poseCallback
    ros::Subscriber pose_sub = n.subscribe("/turtle1/pose", 10, poseCallback);
    // 循环等待回调函数
    ros::spin();
    return 0;
}

2.配置CMakeLists.txt中的编译规则

CMakeLists.txt中build区域下加上:

add_executable(pose_subscriber src/pose_subscriber.cpp)
target_link_libraries(pose_subscriber ${catkin_LIBRARIES})

 3.编译

cd ~/catkin_ws
catkin_make

4.设置环境变量

source devel/setup.bash	

5.运行发布者

运行ros核心 

roscore

启动海龟仿真器 

rosrun turtlesim turtlesim_node

 发布话题

注:打开新的终端需要加上source ~/catkin_ws/devel/setup.bash    

source ~/catkin_ws/devel/setup.bash
rosrun learning_topic pose_subscriber

6.加上发布者

 注:打开新的终端需要加上source ~/catkin_ws/devel/setup.bash    

source ~/catkin_ws/devel/setup.bash	
rosrun learning_topic velocity_publisher


详细内容请看:ROS学习(四)发布者与订阅者

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dtge

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值