tuw_record.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import os
4 import rospy
5 import tf
6 
7 import math
8 import numpy
9 
10 from marker_msgs.msg import MarkerDetection
11 from geometry_msgs.msg import PoseStamped
12 
13 def get_vector_position(position):
14  return (position.x, position.y, position.z)
15 
16 def get_vector_orientation(orientation):
17  return (orientation.x, orientation.y, orientation.z, orientation.w)
18 
19 class tuw_record:
20  def __init__(self):
21  # get and store parameters
22  self.input = rospy.get_param('~input')
23  self.output_dir = rospy.get_param('~output_dir')
24  self.odom = rospy.get_param('~frame_id_odom', 'odom')
25  if self.output_dir[-1:] != '/':
26  self.output_dir = self.output_dir + '/'
27  self.output = self.output_dir + 'record.csv'
28 
29  # listen to transformations and subscribe to topics
30  self.tf_listener = tf.TransformListener()
31  rospy.Subscriber('markers', MarkerDetection, self.cb_marker)
32 
33  self.markers = {}
34  with open(self.input, 'r') as f:
35  for line in f:
36  if line == 'id;x;y;z;roll;pitch;yaw\n':
37  continue
38 
39  tmp = line.split(';')
40  if len(tmp) != 7:
41  raise ValueError('Not a valid line: ' + line)
42 
43  # Note: + 0.15 and + math.pi/2 is because of the gazebo link offset
44  msg = PoseStamped()
45  msg.header.frame_id = rospy.resolve_name(self.odom)[1:]
46  msg.pose.position.x = float(tmp[1]) + 0.15
47  msg.pose.position.y = float(tmp[2])
48  msg.pose.position.z = float(tmp[3]) + 0.15
49  q = tf.transformations.quaternion_from_euler(float(tmp[4]) + math.pi/2, float(tmp[5]), float(tmp[6]))
50  msg.pose.orientation.x = q[0]
51  msg.pose.orientation.y = q[1]
52  msg.pose.orientation.z = q[2]
53  msg.pose.orientation.w = q[3]
54  self.markers[int(tmp[0])] = msg
55 
56  def cb_marker(self, msg):
57  try:
58  # prepare output file
59  with open(self.output, 'a+') as f:
60  if os.path.getsize(self.output) == 0:
61  f.write('exp_length;exp_angle;exp_orientation;length;angle;orientation\n')
62 
63  for marker in msg.markers:
64  # get prediction
65  predmsg = self.tf_listener.transformPose(msg.header.frame_id, self.markers[marker.ids[0]])
66 
67  # get cartesian & sperical representation of prediction translation
68  (x_, y_, z_) = (predmsg.pose.position.x, predmsg.pose.position.y, predmsg.pose.position.z)
69  l_ = math.sqrt(x_*x_ + z_*z_)
70  a_ = math.atan2(x_, z_)
71 
72  # get euler representation of prediction orientation
73  (roll_, pitch_, yaw_) = tf.transformations.euler_from_quaternion(get_vector_orientation(predmsg.pose.orientation))
74 
75  # get cartesian & sperical representation of observation translation
76  (x, y, z) = (marker.pose.position.x, marker.pose.position.y, marker.pose.position.z)
77  l = math.sqrt(x*x + z*z)
78  a = math.atan2(x, z)
79 
80  # get euler representation of observation orientation
81  (roll, pitch, yaw) = tf.transformations.euler_from_quaternion(get_vector_orientation(marker.pose.orientation))
82 
83  # write out transformed pose using euler representation
84  if msg.distance_min < l and l < msg.distance_max and -msg.fov_horizontal/2 < a and a < msg.fov_horizontal/2:
85  f.write('{};{};{};{};{};{}\n'.format(l_, a_, pitch_, l, a, pitch))
86 
87  except tf.Exception as ex:
88  rospy.logwarn('[%s cb_marker] %s', rospy.get_name(), ex)
89 
90 if __name__ == '__main__':
91  # initialize ros node
92  rospy.init_node('tuw_record', anonymous=True)
93 
94  # create tuw_record object
95  record = tuw_record()
96 
97  # keep python from exiting until this node is stopped
98  rospy.spin()
99 
def get_vector_orientation(orientation)
Definition: tuw_record.py:16
def cb_marker(self, msg)
Definition: tuw_record.py:56
def get_vector_position(position)
Definition: tuw_record.py:13


tuw_marker_noise
Author(s): Markus Macsek
autogenerated on Mon Jun 10 2019 15:39:06