背景:(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++ ?)
- 自定义消息(Custom Message)
- Python发布自定义消息 (Publish the custom message uing the python)
- C++ 订阅自定义消息 (Subscribe the custom message using the C++)
- 文件配置(File Configuration)
- 参考 (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()