ROS 编写自定义消息(Python版)

ROS可以自定义消息类型,可以在节点之间传递自定义消息的类型。
下面以自定义的一个Person消息为例子

  • 生成功能包
#cd ~/catkin_ws/src
#catkin_create_pkg learncommunication std_msgs rospy roscpp
#cd ~/catkin_ws/src/learncommunication
#mkdir msg

上面这个过程是在src目录下创建一个名为learncommunicaiton的功能包,如果要创建自定义的消息类型,需要在这个功能包下创建一个名为msg的目录,自定义的消息文件就放在这个目录下。
功能包的目录
在这里插入图片描述
自定义一个消息类型文件Person.msg

string name
uint8  age
uint8  sex

uint8 unknown = 0
uint8 male    = 1
uint8 female  = 2

将来生成的消息类的类名就是Person。
在功能包的编译过程中,可以使用msg文件生成不同编程语言使用的代码文件。
要编译msg文件,需要修改CMakeList.txt文件和package.xml文件

  • 在package.xml文件中添加功能包依赖
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
  • 在CMakeLists.txt中添加编译选项
find_package(catkin REQUIRED COMPONENTS
  roscpp
  rospy
  std_msgs
  message_generation
)

 add_message_files(
   FILES
   Person.msg
 )

 generate_messages(
   DEPENDENCIES
   std_msgs

 )

catkin_package(
 
   CATKIN_DEPENDS roscpp rospy std_msgs message_runtime
 
)

这样自定义消息类的编译选项就做好了

  • 编写publisher节点
import rospy
from learncommunication.msg import Person

def velocity_publisher():
    # ROS节点初始化
    rospy.init_node('person_publisher', anonymous=True)

    # 创建一个Publisher,发布名为/person_info的topic,消息类型为learning_topic::Person,队列长度10
    person_info_pub = rospy.Publisher('/person_info', Person, queue_size=10)

    #设置循环的频率
    rate = rospy.Rate(10) 

    while not rospy.is_shutdown():
        # 初始化learning_topic::Person类型的消息
        person_msg = Person()
        person_msg.name = "Tom";
        person_msg.age  = 18;
        person_msg.sex  = Person.male;

        # 发布消息
        person_info_pub.publish(person_msg)
        rospy.loginfo("Publsh person message[%s, %d, %d]",
                person_msg.name, person_msg.age, person_msg.sex)

        # 按照循环频率延时
        rate.sleep()

if __name__ == '__main__':
    try:
        velocity_publisher()
    except rospy.ROSInterruptException:
        pass

在这里使用语句 from learncommunication.msg import Person 来导入自定义的消息类型

  • 编写subscriber节点
import rospy
from learncommunication.msg import Person

def personInfoCallback(msg):
    rospy.loginfo("Subcribe Person Info: name:%s  age:%d  sex:%d", 
             msg.name, msg.age, msg.sex)

def person_subscriber():
    # ROS节点初始化
    rospy.init_node('person_subscriber', anonymous=True)

    # 创建一个Subscriber,订阅名为/person_info的topic,注册回调函数personInfoCallback
    rospy.Subscriber("/person_info", Person, personInfoCallback)

    # 循环等待回调函数
    rospy.spin()

if __name__ == '__main__':
    person_subscriber()
  • 编译代码
# cd ~/catkin_ws
# catkin_make
# source /dev/setup.bash
# roscore
  • 运行代码
    由于是python程序,不需要使用rosrun,直接运行python脚本即可。
python person_publiser.py
python person_subscriber.py

注意要在不同的终端运行两个脚本

在这里插入图片描述

在这里插入图片描述
脚本运行的效果

  • 查看自定义的信息类型
    可以使用rosmsg show 命令来查看Person类型的信息

在这里插入图片描述
参考文档

https://www.guyuehome.com/

  • 5
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 在ROS2中,如果要发布自定义消息类型,需要进行以下几个步骤。首先,需要在消息定义文件中定义自定义消息类型,该文件需要保存在消息目录中,并使用.msg文件扩展名。\[1\]然后,在发布节点的源代码中,使用ROS2的Publisher类来创建发布者对象,并指定自定义消息类型作为模板参数。例如:ros2::Publisher<my_msgs::MyMessage> pub = node.create_publisher<my_msgs::MyMessage>("my_topic", 10);\[2\]接下来,编写发布消息的逻辑,并使用发布者对象的publish方法来发布消息。最后,运行发布节点,即可发布自定义消息类型的消息。 #### 引用[.reference_title] - *1* *2* [ROS笔记(6)——自定义消息类型](https://blog.csdn.net/weixin_40582034/article/details/129041958)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [ROS实践07 自定义消息调用python](https://blog.csdn.net/qq_42227760/article/details/130013586)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值