发布者publisher编成实现

简介

通过编程控制海龟移动,编写Publisher模块
在这里插入图片描述

创建功能包

catkin_create_pkg learning_topic roscpp  rospy std_msgs geometry_msgs turtlesim

在这里插入图片描述

c++代码编写

  • c++
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
int main(int avgc, char** argv)
{
	//ros节点初始化
	ros::init(avgc, argv, "vecocity_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())
	{
		//初始化geomotry_msgs::Twist类型
		//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("Publish turtle velocity command[%0.2f m/s, %0.2f rad/s]",vel_msg.linear,vel_msg.angular.z);
		
		//按照循环频率延时
		loop_rate.sleep();
	}
	
	return 0;
}

节点名字不能重复

总结一下编成实现发布者实现步骤

  1. 初始化ROS节点
  2. 向ROS Master注册节点信息,包括发布的话题名和话题中的类型
  3. 创建消息数据
  4. 按照一定频率循环发布消息

编译执行

  1. CMakeLists.txt是用于文件的编译连接等
    需要向功能包的CMakeLists.txt中添加
add_executable(velocity_publisher src/velocity_publisher.cpp)
target_link_libraries(velocity_publisher ${catkin_LIBRARIES})

第一句的意思是根据src/velocity_publisher.cpp生成可执行文件velocity_publisher
第二句的意思是链接
位置在build中
在这里插入图片描述
2. 回到工作空间,进行编译

catkin_make

在这里插入图片描述
3. 运行前一定要改变环境变量
source devel/setup.bash
可以通过改变添加环境变量的方式
通过ctrl+h的方式显示主文件夹中的隐藏文件,找到.bashrc
在这里插入图片描述
向.bashrc中加入source /home/catkin_ws/devel/setup.bash根据自己setup.bah的路径进行添加
4. 运行小海龟

5. 执行程序

rosrun learning_topic velocity_publisher

在这里插入图片描述

python 代码编写

#!/usr/bin/env python3
# -*- coding utf-8 -*-
import rospy
from geometry_msgs.msg import Twist

def velocity_publisher():
	rospy.init_node ('velocity_publisher',anonymous=True)
	turtle_vel_pub = rospy.Publisher('/turtle1/cmd_vel',Twist,queue_size = 10)
	rate = rospy.Rate(10)
	while not rospy.is_shutdown():
		vel_msg = Twist()
		vel_msg.linear.x = 0.5
		vel_msg.angular.z = 0.2
		turtle_vel_pub.publish(vel_msg)
		rospy.loginfo("Publish turtle velocity command[%0.2f m/s,%0.2f rad/s]",vel_msg.linear.x,vel_msg.angular.z)
		rate.sleep()
if __name__ == '__main__':
	try:
		velocity_publisher()
	except rospy.ROSInterruptException:
		pass

注意
#!/usr/bin/env python3

#-*- coding utf-8 -*-
是必须的,设置解释器和解码方式

编译

  1. 回到工作空间
    cd ~/catkin_ws

  2. 编译
    catkin_make

  3. 设置环境变量
    source devel/setup.bash

  4. 让python文件具有可执行的权限
    右键文件->属性->权限->句上允许执行文件
    在这里插入图片描述

  5. 运行程序
    rosrun learning_topic velocity_publisher

  6. 最终小海龟匀速运动画圆

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

wuming先生

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

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

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

打赏作者

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

抵扣说明:

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

余额充值