How to get a robot position (x,y) in a map instead of Odometry in python?
I'd like to record robot position (x,y) in a map instead of Odometry in python because my Odometry is not accurate, I asked yesterday about this issue here. So, I need to record them from the map.
when I am using this command in a terminal, I see a good accuracy
rosrun tf tf_echo /map /base_link
how to get information of this command in python
my code when I use the Odometry:
#!/usr/bin/env python
import rospy
from nav_msgs.msg import Odometry
class OdometryModifier:
def __init__(self):
self.sub = rospy.Subscriber("odom", Odometry, self.callback)
def callback(self, data):
x = data.pose.pose.position.x
y = data.pose.pose.position.y
with open("target.csv", "a") as text_file:
text_file.write("\n%.3f" % (x))
text_file.write("\t%.3f" % (y))
if __name__ == '__main__':
rospy.init_node('move_turtlebot', anonymous=True)
try:
odom = OdometryModifier()
rospy.spin()
except KeyboardInterrupt:
rospy.loginfo('Shutting down')
how to get only information print position, quaternion
Update1
I updated my question after @billy's answer
the issue not in using google, tf isn't familiar with me.
import rospy
from tf import TransformListener
from geometry_msgs.msg import PointStamped
# from nav_msgs.msg import OccupancyGrid
class myNode:
def __init__(self):
self.tf = TransformListener()
self.sub = rospy.Subscriber("/tf", PointStamped, self.some_method)
# rospy.Subscriber("/map", TransformStamped, )
# ...
def some_method(self, point_stamped):
print point_stamped
if self.tf.frameExists("/base_link") and self.tf.frameExists("/map"):
t = self.tf.getLatestCommonTime("/base_link", "/map")
position, quaternion = self.tf.lookupTransform("/base_link", "/map", t)
print position, quaternion
if __name__ == '__main__':
rospy.init_node('move_turtlebot', anonymous=True)
try:
odom = myNode()
rospy.spin()
except KeyboardInterrupt:
rospy.loginfo('Shutting down')
the output print point_stamped
.....
....
....
transforms:
-
header:
seq: 0
stamp:
secs: 1578296497
nsecs: 454806328
frame_id: "odom"
child_frame_id: "base_link"
transform:
translation:
x: -0.000629110541654
y: 7.50070441544e-06
z: 0.0
rotation:
x: 0.0
y: 0.0
z: 0.0161632095368
w: 0.999869366796
transforms:
-
header:
seq: 0
stamp:
secs: 1578296498
nsecs: 123375996
frame_id: "map"
child_frame_id: "odom"
transform:
translation:
x: -0.0020397693567
y: 0.00738271227423
z: 0.0
rotation:
x: 0.0
y: 0.0
z: -0.0116367688543
w: 0.999932290513transforms:
-
header:
seq: 0
stamp:
secs: 1578296497
nsecs: 971281007
frame_id: "/camera_link"
child_frame_id: "/camera_depth_frame"
transform:
translation:
x: 0.0
y: -0.02
z: 0.0
rotation:
x: 0.0
y: 0.0
z: 0.0
w: 1.0
......
....
Update2
The output print self.tf.allFramesAsString()
I see Frame odom exists with parent map
.
Frame RIA_TOP_FRAME exists with parent RIA_BASE.
Frame RIA_BASE exists with parent base_link.
Frame base_link exists with parent odom.
Frame camera_link exists with parent base_link.
Frame imu_link exists with parent base_link.
Frame laser_link exists with parent base_link.
Frame camera_depth_frame exists with parent camera_link.
Frame camera_depth_optical_frame exists with parent camera_depth_frame.
Frame camera_rgb_frame exists with parent camera_link.
Frame camera_rgb_optical_frame exists with parent camera_rgb_frame.
Frame rear_left_wheel exists with parent base_link.
Frame rear_right_wheel exists with parent base_link.
Frame map_ned exists with parent map.
Frame odom_ned exists with parent odom.
Frame odom exists with parent map.
Frame base_link_frd exists with parent base_link.
Frame front_left_wheel exists with parent base_link.
Frame front_right_wheel ...