10.发布者Publisher的编程实现

学习视频:https://www.bilibili.com/video/BV1zt411G7Vn?t=1225&p=10

目标:通过程序发布指令让小海龟运动

一、创建工作空间和功能包

1.创建工作空间

mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace
cd ..
catkin_make
catkin_make install

2.创建功能包

cd src/
catkin_create_pkg learning_topic roscpp rospy std_msgs geometry_msgs turtlesim

二、用C++创建发布者代码

1.创建代码

gedit ~/catkin_ws/src/learning_topic/src/velocity_publisher.cpp

运行指令,打开velocity_publisher.cpp文件。

/***********************************************************************
Copyright 2020 GuYueHome (www.guyuehome.com).
***********************************************************************/

/**
 * 该例程将发布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;
}

将上面代码复制到velocity_publisher.cpp文件中,保存关闭。
2.配置发布者代码编译规则

gedit ~/catkin_ws/src/learning_topic/CMakeLists.txt 

运行指令,打开CMakeList.txt文件。

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

将上面两行代码加入CMakeList.txt文件中,如下图所示,保存退出。
在这里插入图片描述
add_executable作用:编译成velocity_publisher可执行文件
target_link_libraries作用:让可执行文件与ROS接口作连接

三、用python创建发布者代码

1.创建发布者代码

mkdir -p ~/catkin_ws/src/learning_topic/scripts
gedit ~/catkin_ws/src/learning_topic/scripts/velocity_publisher.py

运行指令,打开velocity_publisher.py文件。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
 
########################################################################
####          Copyright 2020 GuYueHome (www.guyuehome.com).          ###
########################################################################
 
# 该例程将发布turtle1/cmd_vel话题,消息类型geometry_msgs::Twist
 
import rospy
from geometry_msgs.msg import Twist
 
def velocity_publisher():
	# ROS节点初始化
    rospy.init_node('velocity_publisher', anonymous=True)
 
	# 创建一个Publisher,发布名为/turtle1/cmd_vel的topic,消息类型为geometry_msgs::Twist,队列长度10
    turtle_vel_pub = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10)
 
	#设置循环的频率
    rate = rospy.Rate(10) 
 
    while not rospy.is_shutdown():
		# 初始化geometry_msgs::Twist类型的消息
        vel_msg = Twist()
        vel_msg.linear.x = 0.5
        vel_msg.angular.z = 0.2
 
		# 发布消息
        turtle_vel_pub.publish(vel_msg)
        rospy.loginfo("Publsh 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

将上面代码复制到velocity_publisher.py文件中,保存关闭。
2.设置velocity_publisher.py 文件运行权限

cd ~/catkin_ws/src/learning_topic/scripts
chmod +x velocity_publisher.py 

四、编译工作空间(python不需要本步骤)

命令行输入

cd ~/catkin_ws/
catkin_make

编译之后会在工作空间/devel/lib/learning_topic目录下生成velocity_publisher可执行文件
在这里插入图片描述

五、运行程序

1.运行roscore

roscore

2.打开新的命令窗,运行小海龟仿真器

rosrun turtlesim turtlesim_node 

3.设置环境变量:使用新的命名窗需要重新设置环境变量

cd ~/catkin_ws 
source devel/setup.bash

补充:永久设置环境变量

打开命令窗,输入指令:

gedit ~/.bashrc

将下面一行代码放在.bashrc文件的最后一行

source ~/catkin_ws/devel/setup.bash

如下图所示,保存退出。
在这里插入图片描述

4.打开新的命令窗,运行velocity_publisher.py脚本

rosrun learning_topic velocity_publisher.py

或(运行velocity_publisher可执行文件)

rosrun learning_topic velocity_publisher

5.运行效果图如下
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值