简体   繁体   English

如何从ROS实时获取Python中的数据?

[英]How to get data in python from ROS in real-time?

I am begginer in ROS and I don't understand how to do "publish" and "subscriber" in Python. 我是ROS的初学者,我不了解如何在Python中进行“发布”和“订阅”。 I have a file .bag and I play this file with rosbag play "file.bag" . 我有一个文件.bag,我用rosbag play "file.bag"此文件rosbag play "file.bag"

I get the values of topic with rostopic echo /"topic" or rostopic echo -n 1 /"topic" in a xterm. 我在xterm中使用rostopic echo /"topic"rostopic echo -n 1 /"topic"获得topic的值。

However, I can't see this values in real time with python to show in a QT GUI. 但是,我无法使用python实时看到此值以显示在QT GUI中。 I have read the tutorial http://wiki.ros.org/rospy/Overview/Publishers%20and%20Subscribers but I don't understand it. 我已经阅读了http://wiki.ros.org/rospy/Overview/Publishers%20and%20Subscribers教程,但我不理解。

Could I have a example with /odom [nav_msgs/Odometry] or /back_laser/filtered [sensor_msgs/LaserScan] 我可以有一个例子/odom [nav_msgs/Odometry]/back_laser/filtered [sensor_msgs/LaserScan]

Take a closer look at the tutorials. 仔细看一下教程。 Especially http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28python%29 特别是http://wiki.ros.org/ROS/Tutorials/WritingPublisherSubscriber%28python%29

cd into youd source folder cd到youd源文件夹

and create new package: 并创建新包:

catkin_create_pkg playground

cd in that directory 该目录中的cd

cd playground

create a file listener.py and and add following content: 创建一个文件listener.py并添加以下内容:

#!/usr/bin/env python
import rospy
from std_msgs.msg import String
from nav_msgs.msg import Odometry
def callback(data):
    rospy.loginfo(rospy.get_caller_id()+"I heard %s",data)

def listener():
    rospy.init_node('listener', anonymous=True)
    rospy.Subscriber("odom", Odometry, callback)
    rospy.spin()

if __name__ == '__main__':
    listener()

make executable: 使可执行文件:

chmod +x listener.py

start core: 启动核心:

roscore

new terminal, run listener node: 新终端,运行侦听器节点:

rosrun playground listener.py

new terminal publis some odometry data: 新终端发布了一些里程表数据:

rostopic pub -r1 /odom nav_msgs/Odometry {}

Now you should see some output on your terminal where your listener was started!catki 现在您应该在启动监听器的终端上看到一些输出!catki

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM