ROS 实践——用Python发布一个自定义消息,用C++订阅这个自定义消息

本文详细介绍了在ROS项目中,如何使用Python发布自定义消息(如PathDrop_msg)并与C++节点订阅。包括创建自定义消息文件、Python代码示例、C++回调函数以及必要的文件配置和依赖管理。
摘要由CSDN通过智能技术生成

背景:(background)

当一个ros项目由多种编程语言编写,为了实现不同编程程语言的码块之间的通讯,他们之间需要通讯。有很多案例等待着介绍和实施。本文从最基础的地方开始讲起(When a ROS project is written in multiple programming languages, in order to achieve communication between code blocks in different programming languages, they need to communicate with each other. There are so many cases waiting to introduce and implement。Let’s start from the basics。)

如何使用Python发布一个自定义消息,并用C++订阅这个自定义消息 ?(How to publish a custom message with python and subscribe the message with C++ ?)

  1. 自定义消息(Custom Message)
  2. Python发布自定义消息 (Publish the custom message uing the python)
  3. C++ 订阅自定义消息 (Subscribe the custom message using the C++)
  4. 文件配置(File Configuration)
  5. 参考 (References)

自定义消息 (Custom Message)

建立ros工作空间和软件包之后,建立一个自定义消息:PathDrop_msg.msg. (create a custom message named PathDrop_msg.msg after building the workspace and pacage of ros)
在软件包下建立一个名为msg的文件夹 在内部建立一个名为PathDrop_msg.msg的自定义文件,然后在上面给出基于ros 基本消息类型的自定义消息
(make a directory named msg in the package of ros and touch a file named PathDrop_msg.msg. give your custom message inside.)
使用以下命令验证是否成功构建自定义消息:

$ rosmsg show ros_demo_pkg/demo_msg
# the start point dropping point and end point of the trajectory planning
float64 start_point_x
float64 start_point_y
float64 dropping_point_x
float64 dropping_point_y
float64 end_point_x
float64 end_point_y

Python发布自定义消息 (Publish the custom message uing the python)

#!/usr/bin/env python3
# -*- coding: utf-8 -*- #
"""
Created on Thu Jan 30 20:45:17 2024
"""
import os
import sys
PKG_PATH = os.path.expanduser('~/rosws1/src/trajectoryplanning/') # corresponding the workspace name and package name 
sys.path.append(PKG_PATH + 'scripts/')
from trajectoryplanning.msg import PathDrop_msg # trajectoryplanning is the package name and PathDrop_msg is message file name
import rospy
import numpy as np
import matplotlib.pyplot as plt
import random
import math

class MultipleFireSpotsTrajectoryPlanning():
    "how to get the optimal trajectory to extinguish the forest fire"
    def __init__(self):
        self.pathpoints = PathDrop_msg()
        print("initialization done for fire spots and home locaiton!")
    
    def run(self):
        self.path_point_pub = rospy.Publisher("trajectoryplanning/key_path_points", PathDrop_msg, queue_size=10)
        rospy.sleep(2.0)
        #### for trajectory plannnnig -------------
        publish_n = 0  # only publish once !!!
        
        while not rospy.is_shutdown() and (publish_n == 0):
        
            self.pathpoints.start_point_x = 1
            self.pathpoints.start_point_y = 2
            self.pathpoints.dropping_point_x = 3
            self.pathpoints.dropping_point_y = 4
            self.pathpoints.end_point_x = 5
            self.pathpoints.end_point_y = 6
            self.path_point_pub.publish(self.pathpoints)
            

            rospy.Rate(15).sleep()
            publish_n = publish_n + 1;
  

if __name__ == '__main__':
    #rospy.init_node("RANSAC_Based_Trajectory_Planning", anonymous=True)
    rospy.init_node("ransac_path_points_node", anonymous=True)
    tp = MultipleFireSpotsTrajectoryPlanning()
    tp.run()

关键语句解释 (Interpretation)

  1. 导入自定义文件 (import custom message from pakage)
from trajectoryplanning.msg import PathDrop_msg 

trajectoryplanning 对应软件包名,PathDrop_msg 对应自定义消息名
( trajectoryplanning is the package name and PathDrop_msg is custom message file name)

  1. 实例化一个消息变量 (Instantiate a variable)
self.pathpoints = PathDrop_msg()
  1. 运行py 文件之前你必须授权他们为可执行文件( you must chmod the .py file before running it !)
chmod +x listener.py

C++订阅自定义消息 (Subscribe the custom message using the C++)

创建一个名为 getPathpoints.cpp的文件 在src 文件下下 (buid a file named getPathpoints.cpp in the src directory )


#include <ros/ros.h>
#include "std_msgs/Float64.h"
#include "trajectoryplanning/PathDrop_msg.h"

// 接收到订阅的消息后,会进入消息回调函数
void personInfoCallback(const trajectoryplanning::PathDrop_msg::ConstPtr& msg)
{
	ROS_INFO("Subcribe start_point_x:%f start_point_y:%f", msg->start_point_x, msg->start_point_y);		// 将接收到的消息打印出来
	ROS_INFO("Subcribe dropping_point_x:%f dropping_point_y:%f", msg->dropping_point_x, msg->dropping_point_y);		// 将接收到的消息打印出来
	ROS_INFO("Subcribe end_point_x:%f end_point_y:%f", msg->end_point_x, msg->end_point_y);		// 将接收到的消息打印出来
 }
int main(int argc, char **argv)
{
	ros::init(argc, argv, "getPathpoints");		// 初始化ROS节点
	ros::NodeHandle n;								// 创建节点句柄
	ros::Subscriber person_info_sub = n.subscribe("trajectoryplanning/key_path_points", 10, personInfoCallback);
						// 创建一个Subscriber,订阅名为/person_info的topic,注册回调函数personInfoCallback
						
	ros::spin();  		// ros::spin()循环等待回调函数,用于调用所有可触发的回调函数,将进入循环,不会返回,
						// 类似于在循环里反复调用spinOnce(),而ros::spinOnce()只会去触发一次
	return 0;
}

关键语句解释 (Interpretation)

  1. 导入自定义文件 (include the h files corresponding to the custom message file)
#include "trajectoryplanning/PathDrop_msg.h"
  1. 回调函数输出订阅数据 (print the date of subscriping in the callback function)
void personInfoCallback(const trajectoryplanning::PathDrop_msg::ConstPtr& msg)
// trajectoryplanning::PathDrop_msg 对应软件包名和消息文件名
ROS_INFO("Subcribe start_point_x:%f start_point_y:%f", msg->start_point_x, msg->start_point_y);	
// msg->start_point_x 消息分量
  1. CMakeLists文件里面加入对应的C++文件,生成可执行文件
add_executable(getPathpoints src/getPathpoints.cpp)		# 生成可执行文件person_subscriber
target_link_libraries(getPathpoints ${catkin_LIBRARIES})	# 链接

文件配置(File Configuration)

  1. 设置 package.xml 文件 (set the package.xml file)
    添加依赖包:(add some depend packages if need)

  <build_depend>roscpp</build_depend>
  <build_depend>rosmsg</build_depend>
  <build_depend>rospy</build_depend>
  <build_depend>std_msgs</build_depend>
  <build_depend>message_generation</build_depend>
  
  <build_export_depend>roscpp</build_export_depend>
  <build_export_depend>rosmsg</build_export_depend>
  <build_export_depend>rospy</build_export_depend>
  <build_export_depend>std_msgs</build_export_depend>
  
  <exec_depend>roscpp</exec_depend>
  <exec_depend>rosmsg</exec_depend>
  <exec_depend>rospy</exec_depend>
  <exec_depend>message_generation</exec_depend>
  <exec_depend>message_runtime</exec_depend>
  <exec_depend>std_msgs</exec_depend>
  
  1. 设置 CMakelist 文件 (set the CMakelist file)
find_package(catkin REQUIRED COMPONENTS
  roscpp
  rosmsg
  rospy
  std_msgs
  message_generation
)
add_message_files(
  FILES
  PathDrop_msg.msg

)

generate_messages(
  DEPENDENCIES
  std_msgs
)
catkin_package(
	CATKIN_DEPENDS
	roscpp
	rospy
	std_msgs
	message_runtime
#  INCLUDE_DIRS include
#  LIBRARIES trajectoryplanning
#  CATKIN_DEPENDS roscpp rosmsg rospy
#  DEPENDS system_lib
)

文章最后留个问题:
命令行如何查看ros message 基本类型?

参考 (References)

自定义消息1
如何查看自定义消息内容
自定义消息2
ros 消息总结
c++ 和python 相互发布和和订阅

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值