• ros学习笔记12——python实现发布和接收ros topic


    一、简单demo

    1、工作空间是存放工程开发的相关文件的文件夹

    • src:代码空间
    • build:编译空间
    • devel:开发空间
    • install:安装空间

    2、创作工作空间指令

    mkdir -p ~/catkin_ws/src
    cd ~/catkin_ws/src
    catkin_init_workspace
    
    • 1
    • 2
    • 3

    编译工作空间(注意编译工作空间一定要在工作空间的根目录下)

    cd ~/catkin_ws/
    catkin_make
    
    • 1
    • 2

    设置环境变量

    source devel/setup.bash
    
    • 1

    检查环境变量

    echo $ROS_PACKAGE_PATH
    
    • 1

    3 、创建功能包

    创建功能包指令

    cd ~/catkin_ws/src
    catkin_create_pkg python_test_pkg std_msgs rospy roscpp
    
    • 1
    • 2

    编译功能包

    cd ~/catkin_ws
    catkin_make
    source ~/catkin_ws/devel/setup.bash
    
    • 1
    • 2
    • 3

    4. 创建Topic的订阅发布方法

    创建源码文件夹

    mkdir ~/catkin_ws/src/python_test_pkg/scripts
    
    • 1

    创建publisher的python文件

    cd ~/catkin_ws/src/python_test_pkg/scripts
    vim talker.py
    
    • 1
    • 2

    运行

    python可以不需要编译CMakeList.txt文件
    直接运行roscore,以及listener.py,talker.py
    运行ros程序

    roscore
    
    • 1

    打开新的Terminal,运行 talker

    cd ~/catkin_ws/src/python_test_pkg/scripts
    python talker.py
    
    • 1
    • 2

    打开新的Terminal,运行 listener

    cd ~/catkin_ws/src/python_test_pkg/scripts
    python listener.py
    
    • 1
    • 2

    运行结果
    在这里插入图片描述

    二、接收自定义msg格式rostopic

    这里就需要CMakeList.txt文件和 package.xml文件

    工程架构
    在这里插入图片描述

    CMakeLists.txt

    cmake_minimum_required(VERSION 2.8.3)
    project(ros_to_deepstream)
    
    find_package(catkin REQUIRED COMPONENTS
      rospy
      std_msgs
      geometry_msgs
      trajectory_msgs
      message_generation
    )
    
    add_message_files(
    	FILES
    	video_info_msg.msg
    	video_chan_info.msg
    )
    
    generate_messages(
      DEPENDENCIES
      std_msgs
      geometry_msgs
      trajectory_msgs
    )
    
    catkin_package(
      #INCLUDE_DIRS include
      #LIBRARIES test_py
      CATKIN_DEPENDS rospy message_runtime
      DEPENDS system_lib
    )
    
    include_directories(
      ${catkin_INCLUDE_DIRS}
    )
    #############
    ## Install ##
    #############
    
    #############
    ## Testing ##
    #############
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    package.xml

    <?xml version="1.0"?>
    <package>
      <name>ros_to_deepstream</name>
      <version>0.0.0</version>
      <description>The ros_to_deepstream package</description>
    
      <maintainer email="aa@aaaa.aaaa">aaa</maintainer>
    
      <license>TODO</license>
    
      <buildtool_depend>catkin</buildtool_depend>
    
      <build_depend>rospy</build_depend>
      <build_depend>std_msgs</build_depend>
      <build_depend>message_generation</build_depend>
    
      <run_depend>rospy</run_depend>
      <run_depend>std_msgs</run_depend>
      <run_depend>message_runtime</run_depend>
    
    </package>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    msg

    在这里插入图片描述

    python_ros_listener.py

    在这里插入图片描述

    #!/usr/bin/env python
     
    import rospy
    from std_msgs.msg import String
    from ros_to_deepstream.msg import video_info_msg
    import sys
    
    
    
    def callback(data):
        rospy.loginfo(rospy.get_caller_id() + 'I heard %s', data.header)
        rospy.loginfo('Debug info')
        rospy.loginfo(data.Transmission_diff_Cur)
        
        print("运行到行号:",sys._getframe().f_lineno)
        
        
        
    def listener():
     
        # In ROS, nodes are uniquely named. If two nodes with the same
        # name are launched, the previous one is kicked off. The
        # anonymous=True flag means that rospy will choose a unique
        # name for our 'listener' node so that multiple listeners can
        # run simultaneously.
        rospy.init_node('listener', anonymous=True)
     
        #rospy.Subscriber('/perception/traffic_light_recognition/output/rois', String, callback)
        rospy.Subscriber('/deepstream_ros_bridge/video_info', video_info_msg, callback)
     
        # spin() simply keeps python from exiting until this node is stopped
        rospy.spin()
     
    if __name__ == '__main__':
        listener()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    编译运行

    catkin_make
    python src/ros_to_deepstream/src/python_ros_listener.py 
    
    • 1
    • 2

    在这里插入图片描述

    问题

    问题1:ImportError: No module named video_info_msg.msg

    就是你写的msg路径找不到。

    ├── CMakeLists.txt
    ├── launch
    │ └── face_detection.launch
    ├── my_messages
    │ ├── TwoPoints.msg
    │ └── pt.msg
    ├── nodes
    │ └── face_detection_node
    ├── package.xml
    ├── README.md
    ├── setup.py
    ├── src
    │ └── face_detection
    │ └── face_detection_lib.py

    如您所见,自定义消息存储在my_messages目录中。所以,你的import陈述看起来像

    from face_detection.my_messages import TwoPoints
    
    • 1

    参考:https://answers.ros.org/question/277706/importerror-no-module-named-msg/

    参考

    1、https://blog.csdn.net/h1534589653/article/details/123795327
    2、https://blog.csdn.net/a237072751/article/details/121629169

  • 相关阅读:
    Docker中将静态页面部署nginx
    黑客技术(网络安全)——自学思路
    武汉新时标文化传媒抖音小店官网功能大揭秘
    OR59 字符串中找出连续最长的数字串 - 题解
    【版本发布公告】HMS Core 6.6.0来啦
    [附源码]计算机毕业设计基于SpringBoot的实验填报管理系统
    感知机算法之Python代码实现
    2023扬州大学计算机考研信息汇总
    Python删除字符串中空格方法
    基于springboot零食商城管理系统
  • 原文地址:https://blog.csdn.net/mao_hui_fei/article/details/126033625