栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

ros学习笔记12——python实现发布和接收ros topic
  • 一、简单demo
    • 1、工作空间是存放工程开发的相关文件的文件夹
    • 2、创作工作空间指令
    • 3 、创建功能包
    • 4. 创建Topic的订阅发布方法
    • 运行
  • 二、接收自定义msg格式rostopic
    • CMakeLists.txt
    • package.xml
    • msg
    • python_ros_listener.py
    • 编译运行
    • 问题
    • 问题1:ImportError: No module named video_info_msg.msg
  • 参考

一、简单demo 1、工作空间是存放工程开发的相关文件的文件夹
  • src:代码空间
  • build:编译空间
  • devel:开发空间
  • install:安装空间
2、创作工作空间指令
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace

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

cd ~/catkin_ws/
catkin_make

设置环境变量

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

运行结果

二、接收自定义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 ##
#############

package.xml


  ros_to_deepstream
  0.0.0
  The ros_to_deepstream package

  aaa

  TODO

  catkin

  rospy
  std_msgs
  message_generation

  rospy
  std_msgs
  message_runtime



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()
编译运行
catkin_make
python src/ros_to_deepstream/src/python_ros_listener.py 

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

参考

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1029352.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号