以ros为媒介进行数据采集
最近在学习usvsim,是在ros环境下进行的,所以就正好学习ros
目的是将无人船行进数据采集到,所以要在他的环境包里再建一个subscriber
关于数据查看 利用topic rostopic list 查看topic功能菜单 链接: ROS学习笔记五:理解ROS topics. 可以看到/gazebo/model_states 这个就是船的位置信息 只要能显示出来就是能传出来。
建subscriber 链接: ROS中编写Publisher和Subscriber的方法(Python版).
链接: ROS Publisher c++ 与 Python 实现.
链接: ROS编译 Python 文件(详细说明).
下面是我自己整合及遇到的问题,我是py
cd ~/catkin_ws/src
catkin_create_pkg beginner_tutorials std_msgs rospy roscpp
roscd beginner_tutorials
mkdir scripts
cd scripts
然后编译一下
catkin_make -DCATKIN_WHITELIST_PACKAGES="my_pkg"
UGVinformation_xy.py
#!/usr/bin/env python
from gazebo_msgs.msg import ModelStates
import rospy
import numpy as np
from new_thread_test import Job
def callback(msg):
global current_state
current_state = rospy.Time.to_time(rospy.Time.now())
current_state = np.append(current_state, msg)
if __name__ == "__main__":
rospy.init_node("UAVInformation")
UAV_state = rospy.Subscriber("/gazebo/model_states", ModelStates, callback)
current_state = []
add_thread = Job()
add_thread.start()
add_thread.pause()
list_all = []
while True:
add_thread.resume()
last_time = rospy.Time.now()
while True:
if rospy.Time.now() - last_time > rospy.Duration(5).__div__(10):
list_all.append(current_state)
print current_state
break
if len(list_all) > 100:
break
with open("information.txt", "w") as f:
for _ in list_all:
f.write(str(_))
f.write("\r")
在里面新建一个文档,复制粘贴进
UAVinformation_xyz.py
#!/usr/bin/env python
from gazebo_msgs.msg import ModelStates
import rospy
import numpy as np
from new_thread_test import Job
def callback(msg):
global current_state
current_state = rospy.Time.to_time(rospy.Time.now())
current_state = np.append(current_state, msg)
if __name__ == "__main__":
rospy.init_node("UAVInformation")
UAV_state = rospy.Subscriber("/gazebo/model_states", ModelStates, callback)
current_state = []
add_thread = Job()
add_thread.start()
add_thread.pause()
while True:
add_thread.resume()
last_time = rospy.Time.now()
while True:
if rospy.Time.now() - last_time > rospy.Duration(4):
print current_state
break
new_thread_test.py
#!/usr/bin/env python
# coding: utf-8
import threading
import time
import rospy
class Job(threading.Thread):
def __init__(self, *args, **kwargs):
super(Job, self).__init__(*args, **kwargs)
self.__flag = threading.Event() # 用于暂停线程的标识
self.__flag.set() # 设置为True
self.__running = threading.Event() # 用于停止线程的标识
self.__running.set() # 将running设置为True
def run(self):
while self.__running.isSet():
self.__flag.wait() # 为True时立即返回, 为False时阻塞直到内部的标识位为True后返
rospy.spin()
def pause(self):
self.__flag.clear() # 设置为False, 让线程阻塞
def resume(self):
self.__flag.set() # 设置为True, 让线程停止阻塞
def stop(self):
self.__flag.set() # 将线程从暂停状态恢复, 如何已经暂停的话
self.__running.clear() # 设置为False
# a = Job()
# a.start()
# print 'start'
# time.sleep(3)
# a.pause()
# print 'pause'
# time.sleep(3)
# a.resume()
# print 'resume'
# time.sleep(3)
# a.pause()
# print 'pause'
# time.sleep(2)
# a.stop()
# print 'stop'
cd ~/catkin_ws/src
source ~/catkin_ws/devel/setup.bash
rosrun beginner_tutorials UAVinformation_xyz.py
运行一下,记得更改权限为可执行文件。
|