-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathexport_pose.py
106 lines (77 loc) · 2.28 KB
/
export_pose.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Author : Heethesh Vhavle
Email : [email protected]
Version : 1.0.1
Date : May 7, 2020
'''
# Python 2/3 compatibility
from __future__ import print_function, absolute_import, division
# Python modules
import os
import datetime
# External modules
import matplotlib.pyplot as plt
# ROS modules
import tf
import rospy
import message_filters
# ROS messages
from sensor_msgs.msg import Image
# Local python modules
import utils
# Global variables
POSE_DATA = []
listener = None
# Set these params
IMAGE_TOPIC = '/camera/rgb/image_color'
FRAME_1 = '/world'
FRAME_2 = '/kinect'
########################### Functions ###########################
def callback(image_msg):
try:
(trans, rot) = listener.lookupTransform(FRAME_1, FRAME_2, rospy.Time())
except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException):
return
timestamp_secs = image_msg.header.stamp.to_sec()
# Save poses
POSE_DATA.append([timestamp_secs, trans, rot])
def shutdown_hook():
filename = '/tmp/%s.txt' % datetime.datetime.now()
filename = filename.replace(' ', '-')
with open(filename, 'w') as f:
# Add any additional headers here
# f.write('header data\n')
# Save the pose data
for data in POSE_DATA:
f.write('%s %s %s %s %s %s %s %s\n' % (
data[0],
data[1][0],
data[1][1],
data[1][2],
data[2][0],
data[2][1],
data[2][2],
data[2][3],
))
rospy.loginfo('%d poses saved to %s' % (len(POSE_DATA), filename))
def run(**kwargs):
global listener
# Start node
rospy.init_node('pose_extractor', anonymous=True)
rospy.loginfo('Current PID: [%d]' % os.getpid())
listener = tf.TransformListener()
# Handle params and topics
image = rospy.get_param('~image', IMAGE_TOPIC)
# Subscribe to topics
info_sub = rospy.Subscriber(image, Image, callback)
# Shutdown hook
rospy.on_shutdown(shutdown_hook)
# Keep python from exiting until this node is stopped
try:
rospy.spin()
except rospy.ROSInterruptException:
rospy.loginfo('Shutting down')
if __name__ == '__main__':
run()