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

一、简单demo

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

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

2、创作工作空间指令

mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace

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

cd ~/catkin_ws/

设置环境变量

source devel/setup.bash

检查环境变量

echo $ROS_PACKAGE_PATH

3 、创建功能包

创建功能包指令

cd ~/catkin_ws/src
catkin_create_pkg python_test_pkg std_msgs rospy roscpp

编译功能包

cd ~/catkin_ws
catkin_make
source ~/catkin_ws/devel/setup.bash

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

创建源码文件夹

mkdir ~/catkin_ws/src/python_test_pkg/scripts

创建publisher的python文件

cd ~/catkin_ws/src/python_test_pkg/scripts
vim talker.py

运行

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

roscore

打开新的Terminal,运行 talker

cd ~/catkin_ws/src/python_test_pkg/scripts
python talker.py

打开新的Terminal,运行 listener

cd ~/catkin_ws/src/python_test_pkg/scripts
python listener.py

运行结果

ros学习笔记12——python实现发布和接收ros topic_工作空间

二、接收自定义msg格式rostopic

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

工程架构

ros学习笔记12——python实现发布和接收ros topic_工作空间_02

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}
)

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>

msg

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

python_ros_listener.py

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

#!/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.
# name are launched, the previous one is kicked off.
# anonymous=True flag means that rospy will choose a unique
# name for'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
rospy.spin()

if __name__ == '__main__':
listener()

编译运行

catkin_make
python src/ros_to_deepstream/src/python_ros_listener.py

ros学习笔记12——python实现发布和接收ros topic_学习_05

问题

问题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

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