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的目录,自定义的消息文件就放在这个目录下。
功能包的目录

ROS 编写自定义消息(Python版)
自定义一个消息类型文件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

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

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

ROS 编写自定义消息(Python版)
脚本运行的效果
  • 查看自定义的信息类型
    可以使用rosmsg show 命令来查看Person类型的信息

ROS 编写自定义消息(Python版)
参考文档

https://www.guyuehome.com/

Original: https://blog.csdn.net/dfman1978/article/details/122102727
Author: 想游泳的鱼
Title: ROS 编写自定义消息(Python版)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/559566/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球