data_collection.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 
18 import time
19 import csv
20 import datetime
21 import rospy
22 
23 import abc
24 
25 from sensor_msgs.msg import JointState
26 from std_msgs.msg import Float64MultiArray
27 from geometry_msgs.msg import TwistStamped
28 from geometry_msgs.msg import Twist
29 from nav_msgs.msg import Odometry
30 from cob_control_msgs.msg import ObstacleDistances
31 import tf
32 
33 DEL = ','
34 QUOTE = '"'
35 DATE_TIME_FMT = '%d.%m.%Y %H:%M:%S.%f' # should not consist the DEL char!!!
36 
37 
38 class DataKraken(object):
39 
40  __metaclass__ = abc.ABCMeta
41 
42  def __init__(self, file_path, topic_name, data_class):
43  self.file_path_ = file_path
44  self.csv_writer_ = None
45  self.topic_name_ = topic_name
46  self.data_class_ = data_class
47  self.data_rows_ = []
48  self.subscriber_ = None
49  self.start_ = None
50 
51  def open(self):
52  success = False
53  try:
54  with open(self.file_path_, 'wt') as csvfile:
55  self.csv_writer_ = csv.writer(
56  csvfile, delimiter = DEL, quotechar = QUOTE)
57  success = self.csv_writer_ is not None
58  if success:
59  self.subscriber_ = rospy.Subscriber(
60  self.topic_name_, self.data_class_, self.callback)
61  except IOError as ioe:
62  rospy.logerr(str(ioe))
63  return success
64 
65  def writeAllData(self):
66  success = False
67  if len(self.data_rows_) > 0:
68  with open(self.file_path_, 'wt') as csvfile:
69  self.csv_writer_ = csv.writer(
70  csvfile, delimiter = DEL, quotechar = QUOTE)
71  self.csv_writer_.writerows(self.data_rows_)
72  success = True
73  rospy.loginfo(
74  self.__class__.__name__ + ': Successfully wrote data into file: ' + self.file_path_)
75  else:
76  rospy.logerr(self.__class__.__name__ +
77  ': Could not find any data to write into file: ' + self.file_path_)
78  return success
79 
80  @abc.abstractmethod
81  def callback(self, data):
82  raise NotImplementedError
83 
84 
86 
87  def __init__(self, file_path):
88  super(JointStateDataKraken, self).__init__(
89  file_path, 'joint_states', JointState)
90  self.init_ = True
91  self.jointstate_sub_ = None
92 
93  def callback(self, data):
94  if self.init_:
95  self.start_ = datetime.datetime.now()
96  header = []
97  header.append('time stamp')
98  header.append('time delta')
99  for name in data.name:
100  header.append(name + '_position')
101  for name in data.name:
102  header.append(name + '_velocity')
103  self.data_rows_.append(header)
104  self.init_ = False
105 
106  now = datetime.datetime.now()
107  delta = now - self.start_
108 
109  data_row = []
110  data_row.append(now.strftime(DATE_TIME_FMT))
111  data_row.append('%.3f' % delta.total_seconds())
112  data_row.extend(list(data.position))
113  data_row.extend(list(data.velocity))
114  self.data_rows_.append(data_row)
115 
116 
118 
119  def __init__(self, file_path):
120  super(ObstacleDistanceDataKraken, self).__init__(
121  file_path, 'obstacle_distance', ObstacleDistances)
122  self.init_ = True
123 
124  def callback(self, data):
125  if self.init_:
126  self.start_ = datetime.datetime.now()
127  header = []
128  header.append('time stamp')
129  header.append('time delta')
130  header.append('link_of_interest')
131  header.append('obstacle')
132  header.append('distance')
133  header.append('frame_vector_x')
134  header.append('frame_vector_y')
135  header.append('frame_vector_z')
136  header.append('np_frame_vector_x')
137  header.append('np_frame_vector_y')
138  header.append('np_frame_vector_z')
139  header.append('np_obstacle_vector_x')
140  header.append('np_obstacle_vector_y')
141  header.append('np_obstacle_vector_z')
142  self.data_rows_.append(header)
143  self.init_ = False
144 
145  now = datetime.datetime.now()
146  delta = now - self.start_
147  for elem in data.distances:
148  data_row = [now.strftime(DATE_TIME_FMT), '%.3f' % delta.total_seconds()]
149  data_row.append(elem.link_of_interest)
150  data_row.append(elem.obstacle_id)
151  data_row.append(elem.distance)
152  data_row.append(elem.frame_vector.x)
153  data_row.append(elem.frame_vector.y)
154  data_row.append(elem.frame_vector.z)
155  data_row.append(elem.nearest_point_frame_vector.x)
156  data_row.append(elem.nearest_point_frame_vector.y)
157  data_row.append(elem.nearest_point_frame_vector.z)
158  data_row.append(elem.nearest_point_obstacle_vector.x)
159  data_row.append(elem.nearest_point_obstacle_vector.y)
160  data_row.append(elem.nearest_point_obstacle_vector.z)
161  self.data_rows_.append(data_row)
162 
163 
165 
166  def __init__(self, file_path, for_base = False, use_twist_stamped = True):
167  self.use_twist_stamped_ = use_twist_stamped
168 
169  if for_base:
170  super(TwistDataKraken, self).__init__(
171  file_path, '/base/twist_controller/command', Twist)
172  else:
173  if self.use_twist_stamped_:
174  super(TwistDataKraken, self).__init__(
175  file_path, 'twist_controller/command_twist_stamped', TwistStamped)
176  else:
177  super(TwistDataKraken, self).__init__(
178  file_path, 'twist_controller/command_twist', Twist)
179  self.init_ = True
180 
181  def callback(self, data):
182  if self.use_twist_stamped_:
183  frame_id = data.header.frame_id + '_'
184  twist_data = data.twist
185  else:
186  frame_id = ''
187  twist_data = data
188 
189  if self.init_:
190  self.start_ = datetime.datetime.now()
191  header = []
192 
193  header.append(frame_id + 'time stamp')
194  header.append(frame_id + 'time delta')
195  header.append(frame_id + 'linear_x')
196  header.append(frame_id + 'linear_y')
197  header.append(frame_id + 'linear_z')
198  header.append(frame_id + 'angular_x')
199  header.append(frame_id + 'angular_y')
200  header.append(frame_id + 'angular_z')
201  self.data_rows_.append(header)
202  self.init_ = False
203  data_row = []
204 
205  now = datetime.datetime.now()
206  delta = now - self.start_
207  data_row.append(now.strftime(DATE_TIME_FMT))
208  data_row.append('%.3f' % delta.total_seconds())
209  data_row.append(twist_data.linear.x)
210  data_row.append(twist_data.linear.y)
211  data_row.append(twist_data.linear.z)
212  data_row.append(twist_data.angular.x)
213  data_row.append(twist_data.angular.y)
214  data_row.append(twist_data.angular.z)
215  self.data_rows_.append(data_row)
216 
217 
219 
220  def __init__(self, file_path):
221  super(JointVelocityDataKraken, self).__init__(
222  file_path, 'joint_group_velocity_controller/command', Float64MultiArray)
223  self.init_ = True
224 
225  def callback(self, data):
226  if self.init_:
227  self.start_ = datetime.datetime.now()
228  header = []
229  header.append('time stamp')
230  header.append('time delta')
231  for idx, jointvel in enumerate(data.data):
232  header.append(str(idx + 1))
233  self.data_rows_.append(header)
234  self.init_ = False
235  data_row = []
236  now = datetime.datetime.now()
237  delta = now - self.start_
238  data_row.append(now.strftime(DATE_TIME_FMT))
239  data_row.append('%.3f' % delta.total_seconds())
240 
241  for jointvel in data.data:
242  data_row.append(jointvel)
243  self.data_rows_.append(data_row)
244 
245 
247 
248  def __init__(self, file_path, root_frame, chain_tip_link, tracking_frame):
249  super(FrameTrackingDataKraken, self).__init__(
250  file_path, None, None)
251  self.init_ = True
252  self.timed_transform_ = None
253  self.tf_ = None
254  self.root_frame_ = root_frame
255  self.chain_tip_link_ = chain_tip_link
256  self.tracking_frame_ = tracking_frame
257 
258  def open(self):
259  success = False
260  try:
261  with open(self.file_path_, 'wt') as csvfile:
262  self.csv_writer_ = csv.writer(
263  csvfile, delimiter = DEL, quotechar = QUOTE)
264  success = self.csv_writer_ is not None
265  if success:
266  self.tf_ = tf.TransformListener()
267  self.timed_transform_ = rospy.Timer(rospy.Duration.from_sec(0.1), self.callback)
268  except IOError as ioe:
269  rospy.logerr(str(ioe))
270  return success
271 
272  def writeAllData(self):
273  self.timed_transform_.shutdown()
274  self.timed_transform_.join(5.0)
275  return super(FrameTrackingDataKraken, self).writeAllData()
276 
277  def callback(self, data):
278  if self.init_:
279  self.start_ = datetime.datetime.now()
280  header = []
281  header.append('time stamp')
282  header.append('time delta')
283  for frame in (self.chain_tip_link_, self.tracking_frame_):
284  header.append('x Trans ' + self.root_frame_ + ' to ' + frame)
285  header.append('y Trans ' + self.root_frame_ + ' to ' + frame)
286  header.append('z Trans ' + self.root_frame_ + ' to ' + frame)
287  header.append('x Quat ' + self.root_frame_ + ' to ' + frame)
288  header.append('y Quat ' + self.root_frame_ + ' to ' + frame)
289  header.append('z Quat ' + self.root_frame_ + ' to ' + frame)
290  header.append('w Quat ' + self.root_frame_ + ' to ' + frame)
291  header.append('roll ' + self.root_frame_ + ' to ' + frame)
292  header.append('pitch ' + self.root_frame_ + ' to ' + frame)
293  header.append('yaw ' + self.root_frame_ + ' to ' + frame)
294  self.data_rows_.append(header)
295  self.init_ = False
296 
297  trans_root_tip, quat_root_tip, rpy_root_tip = None, None, None
298  try:
299  # tf_time = t = self.tf_.getLatestCommonTime(self.root_frame_, self.tracking_frame_)
300  (trans_root_tip, quat_root_tip) = self.tf_.lookupTransform(self.root_frame_, self.chain_tip_link_, rospy.Time(0))
301  rpy_root_tip = tf.transformations.euler_from_quaternion(quat_root_tip)
302  except Exception as e:
303  print('Exception chain_tip_link_: ' + str(e))
304 
305  trans_root_track, quat_root_track, rpy_root_track = None, None, None
306  try:
307  # tf_time = t = self.tf_.getLatestCommonTime(self.root_frame_, self.tracking_frame_)
308  # if self.tf_.waitForTransform(self.root_frame_, self.tracking_frame_, tf_time, rospy.Duration(0.5)):
309  (trans_root_track, quat_root_track) = self.tf_.lookupTransform(self.root_frame_, self.tracking_frame_, rospy.Time(0))
310  rpy_root_track = tf.transformations.euler_from_quaternion(quat_root_track)
311 
312  except Exception as e:
313  print('Exception tracking_frame_: ' + str(e))
314 
315 
316  if rpy_root_tip is not None and rpy_root_track is not None:
317  data_row = []
318 
319  now = datetime.datetime.now()
320  delta = now - self.start_
321  data_row.append(now.strftime(DATE_TIME_FMT))
322  data_row.append('%.3f' % delta.total_seconds())
323 
324  data_row.extend(trans_root_tip)
325  data_row.extend(quat_root_tip)
326  data_row.extend(rpy_root_tip)
327 
328  data_row.extend(trans_root_track)
329  data_row.extend(quat_root_track)
330  data_row.extend(rpy_root_track)
331 
332  self.data_rows_.append(data_row)
333 
334 
336 
337  def __init__(self, file_path):
338  super(OdometryDataKraken, self).__init__(
339  file_path, '/base/odometry_controller/odometry', Odometry)
340  self.init_ = True
341 
342  def callback(self, data):
343  if self.init_:
344  self.start_ = datetime.datetime.now()
345  header = []
346  header.append(data.header.frame_id + '_time stamp')
347  header.append(data.header.frame_id + '_time delta')
348  header.append(data.header.frame_id + '_pos_x')
349  header.append(data.header.frame_id + '_pos_y')
350  header.append(data.header.frame_id + '_pos_z')
351  header.append(data.header.frame_id + '_quat_x')
352  header.append(data.header.frame_id + '_quat_y')
353  header.append(data.header.frame_id + '_quat_z')
354  header.append(data.header.frame_id + '_quat_w')
355  header.append(data.header.frame_id + '_roll')
356  header.append(data.header.frame_id + '_pitch')
357  header.append(data.header.frame_id + '_yaw')
358  header.append(data.header.frame_id + '_twist_linear_x')
359  header.append(data.header.frame_id + '_twist_linear_y')
360  header.append(data.header.frame_id + '_twist_linear_z')
361  header.append(data.header.frame_id + '_twist_angular_x')
362  header.append(data.header.frame_id + '_twist_angular_y')
363  header.append(data.header.frame_id + '_twist_angular_z')
364  self.data_rows_.append(header)
365  self.init_ = False
366  data_row = []
367 
368  now = datetime.datetime.now()
369  delta = now - self.start_
370  data_row.append(now.strftime(DATE_TIME_FMT))
371  data_row.append('%.3f' % delta.total_seconds())
372 
373  rpy = tf.transformations.euler_from_quaternion((data.pose.pose.orientation.x, data.pose.pose.orientation.y, data.pose.pose.orientation.z, data.pose.pose.orientation.w,))
374  data_row.append(data.pose.pose.position.x)
375  data_row.append(data.pose.pose.position.y)
376  data_row.append(data.pose.pose.position.z)
377  data_row.append(data.pose.pose.orientation.x)
378  data_row.append(data.pose.pose.orientation.y)
379  data_row.append(data.pose.pose.orientation.z)
380  data_row.append(data.pose.pose.orientation.w)
381  data_row.append(rpy[0])
382  data_row.append(rpy[1])
383  data_row.append(rpy[2])
384  data_row.append(data.twist.twist.linear.x)
385  data_row.append(data.twist.twist.linear.y)
386  data_row.append(data.twist.twist.linear.z)
387  data_row.append(data.twist.twist.angular.x)
388  data_row.append(data.twist.twist.angular.y)
389  data_row.append(data.twist.twist.angular.z)
390  self.data_rows_.append(data_row)
391 
392 
def __init__(self, file_path, root_frame, chain_tip_link, tracking_frame)
def __init__(self, file_path, topic_name, data_class)
def __init__(self, file_path, for_base=False, use_twist_stamped=True)


cob_twist_controller
Author(s): Felix Messmer , Marco Bezzon , Christoph Mark , Francisco Moreno
autogenerated on Thu Apr 8 2021 02:40:00