ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

extract the time from /tf topic, between two specific frames

asked 2022-06-28 16:48:07 -0500

Delbina gravatar image

updated 2022-06-29 07:02:51 -0500

Hii,

I want to extract the time from the following message between frame_id, child_frame_id. would you please help me in writing a python code?

~/catkin_ws$ rosmsg show tf2_msgs/TFMessage
geometry_msgs/TransformStamped[] transforms
  std_msgs/Header header
    uint32 seq
    time stamp
    string frame_id
  string child_frame_id
  geometry_msgs/Transform transform
    geometry_msgs/Vector3 translation
      float64 x
      float64 y
      float64 z
    geometry_msgs/Quaternion rotation
      float64 x
      float64 y
      float64 z
      float64 w


#Example:
transforms: 
  - 
    header: 
      seq: 0
      stamp: 
        secs: 1649243817
        nsecs: 881501198
      frame_id: "odom"
    child_frame_id: "base_link"
    transform: 
      translation: 
        x: 269.211298755
        y: -263.069342463
        z: 0.0
      rotation: 
        x: 0.0
        y: 0.0
        z: -0.175106142185
        w: 0.984549561459
---

I want to extract:
secs: 1649243817 nsecs: 881501198 for the time that frame_id is odom and child_frame_id is base_link.

thanks

edit retag flag offensive close merge delete

Comments

@hank880907

#!/usr/bin/env python
from sqlite3 import Row
import rospy
import tf
from tf2_msgs.msg import TFMessage
from std_msgs.msg import Int32, Float32, String, Time
import csv


class tf_frames:
    def __init__(self, frame_id, child_frame_id):
        self.frame_id = frame_id
        self.child_frame_id = child_frame_id
        #listener = tf.TransformListener()
        TEXTFILE = open("data_publisher_47.csv", "w")
        TEXTFILE.truncate()
        def callback(msg):
            rospy.Subscriber('/tf', TFMessage, callback)
            if frame_id == "camera_color_optical_frame" and child_frame_id=="object_47":    
                time_stamp = msg.header.stamp
                row = [time_stamp]
                print (time_stamp)
                with open('data_publisher_47.csv', 'a') as csvfile:
                    writer = csv.writer(csvfile)
                    writer.writerow(row)
                    csvfile.close

        if __name__ == '__main__':
            tf_frames()
Delbina gravatar image Delbina  ( 2022-06-29 06:10:43 -0500 )edit

I have wriiten the upper code. but it is not working!!!

Delbina gravatar image Delbina  ( 2022-06-29 06:12:55 -0500 )edit

of course it won't work. You are not even initializing the ros node correctly... have you take a look at how to write a node in ros? https://wiki.ros.org/ROS/Tutorials/Wr...

hank880907 gravatar image hank880907  ( 2022-07-01 21:52:32 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-06-28 17:02:22 -0500

hank880907 gravatar image

Just write a python node to listen to the tf topic.

Here's the tutorial for writing a python node in ROS.

http://wiki.ros.org/ROS/Tutorials/Wri...

edit flag offensive delete link more

Comments

thanks, but this is not enough for me! I want to use those frame-id and child_frame_id as well.and then specify the time. when you run /tf topic, it publishes the tf between all the frames. i want the tf between two specific frames.

Delbina gravatar image Delbina  ( 2022-06-28 17:17:32 -0500 )edit

you could just put a filter in the python node to filter out the unwanted tfs.

it will be something like:

python if frame_id =="odom" # do something

hank880907 gravatar image hank880907  ( 2022-06-28 17:57:36 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2022-06-28 16:48:07 -0500

Seen: 122 times

Last updated: Jun 29 '22