ROS study notes 5 (programming implementation of Publisher Publisher)

Topic model

The picture below is from Gu Yue "Introduction to ROS 21 Lectures"
Insert picture description here

Take Little Turtle as an example. Two nodes are managed through ROS Master. Subscriber is the Turtlesim simulator node, and Publisher is the publisher node. Through the topic of type /turtle1/cmd_vel, the publisher and the subscriber communicate with each other to achieve The purpose of controlling turtles.

Create feature pack

Insert picture description hereThe function package name is learning_topic, and the added dependencies are roscpp rospy std_msgs geometry_msgs turtlesim.

Create Publisher C++ file

Use the touch command to create a .cpp file in the src folder of the function package learning_topic.
Insert picture description here

Introduce the header file

#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
//包含ros api文件
//包含Twist定义消息

ROS node initialization

//ROS节点初始化
ros::init(argc, argv, "velocity_publisher");

Create node handle

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

Create Publisher

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

Set cycle frequency

//设置循环的频率
ros::Rate loop_rate(10);

Package data and publish

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("Publish turtle velocity command[%0.2f m/s, %0.2f raf/s]", vel_msg.linear.x, vel_msg.angular.z);
    	
    //按循环频率延时
    loop_rate.sleep();
}

Complete code

/**
 * 该例程将发布turtle1/cmd_vel话题,消息类型为geometry_msgs::Twist
 */

#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
//包含ros api文件
//包含Twist定义消息

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("Publish turtle velocity command[%0.2f m/s, %0.2f raf/s]", vel_msg.linear.x, vel_msg.angular.z);
    	
    	//按循环频率延时
    	loop_rate.sleep();
    }
    
    return 0;
    
}

Process combing

  • Initialize the ROS node
  • Register node information with ROS Master, including the name of the published topic and the message type in the topic
  • Create message data
  • Circulate news at a certain frequency

Configure publisher code compilation rules

How to configure compilation rules in CMakeLists.txt

  • Set the code to be compiled and the executable file generated
  • Set up the link library
add_executable(velocity_publisher src/velocity_publisher.cpp)
target_link_libraries(velocity_publisher${catkin_LIBRARIES})
  • add_executable is used to describe which program file is compiled into which executable file, that is, src/velocity_publisher.cpp file is compiled into velocity_publisher executable file
  • target_link_libraries is used to link our executable files with ROS related libraries

Compile and run the publisher

$ cd~/catkin_ws
$ catkin_make
$ source devel/setup.bash
$ roscore
$ rosrun turtlesim turtlesim_node
$ rosrun learning_topic velocity_publisher

The results are as follows:
Insert picture description here

Python implementation code

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 该例程将发布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


When running Python files in ROS, you should pay attention to the file permissions.
As shown in the figure:
Insert picture description here
Python files do not need to be compiled and used directly

$ rosrun learning_topic velocity_publisher.py

To run.

Part of the code in this article comes from Gu Yue "21 Lectures on ROS Introduction"

Previous link

ROS study notes four (creating workspaces and functional packages)
ROS study notes three (use of ROS command line tools)
ROS study notes two (the core concepts of ROS )
ROS study notes one (basic Linux system operation)

Guess you like

Origin blog.csdn.net/weixin_44911075/article/details/114178580