【ROS】创建工作空间与功能包,编程实现发布者Publisher

文章目录

  • 一、创建工作空间
  • 二、创建功能包
  • 三、编程实现发布者Publisher
    • 1.创建功能包
    • 2.创建发布者代码
    • 3.配置发布者代码编译规则
    • 4.编译并运行发布者

一、创建工作空间

工作空间是一个存放过程开发相关文件的文件夹,一般包括四个目录空间

1) src,代码空间,存储所有ROS功能包的源码文件;

2) build ,编译空间,存储空间编译过程中产生的缓存信息和中间文件;

3) devel;开发空间,用来放置编译生成的可执行文件;

4) install:安装空间,可在终端中运行这些可执行文件;

创建工作空间----生成src文件夹

mkdir -p catkin_ws/src
cd catkin_ws/src
catkin_init_workspace

编译工作空间----生成build、devel文件夹

cd ~/catkin_ws
catkin_make

安装工作空间----生成install文件夹

catkin_make install

设置环境变量

source devel/setup.bash

检查环境变量

echo $ROSPACKAGE_PATH

二、创建功能包

代码不能直接放在src下,创建功能包放代码 创建功能包
cd ~/catkin_ws/src
catkin_create_pkg test_pkg std_msgs rospy roscpp

编译功能包

cd ~/catkin_ws
catkin_make
source devel/setup.bash

三、编程实现发布者Publisher

【ROS】创建工作空间与功能包,编程实现发布者Publisher_第1张图片

1.创建功能包

创建learning_topic功能包

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

2.创建发布者代码

cd ~/catkin_ws/src/learning_topic/src
touch velocity_publisher.cpp
gedit velocity_publisher.cpp
/**
 * 该publisher将发布turtler1/cmd_vel话题,消息类型geometry_msgs::Twist
**/

#include 
#include 

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;
}

3.配置发布者代码编译规则

打开learning_topic下的CMakeLists.txt在合适位置加入一下两句:


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


【ROS】创建工作空间与功能包,编程实现发布者Publisher_第2张图片

4.编译并运行发布者

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

【ROS】创建工作空间与功能包,编程实现发布者Publisher_第3张图片


Python代码实现

#!/usr/bin/env python 
# -*- coding:utf-8 -*-
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.angular.z=0.2
        vel_msg.linear.x=0.5
	#发布消息
        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

你可能感兴趣的:(ROS,自动驾驶,c++,python)