lookup_transform: TypeError: __init__() takes 1 positional argument but 2 were given
Hi all, I am using ROS2 Foxy in Ubuntu 20.04 LTS. I am trying to fetch the robot's pose with respect to the odom frame. Here is my code.
import rclpy
from rclpy.time import Time
from rclpy.node import Node
from rclpy.duration import Duration
from geometry_msgs.msg import Pose
from geometry_msgs.msg import TransformStamped
from tf2_ros.transform_listener import TransformListener
from tf2_ros.buffer import Buffer
from rclpy.duration import Duration
class Trial(Node):
def __init__(self):
super().__init__('trial_node')
self.pose = TransformStamped()
self._tf_buffer = Buffer()
self.listener = TransformListener(self._tf_buffer, self)
def get_global_pos(self):
now = Time()
try:
self.get_logger().info("Fetching transform of 'base_link' with reference to 'odom' frame.")
trans = self._tf_buffer.lookup_transform('odom', 'base_link', time=Duration(0))
print(trans)
except LookupError:
self.get_logger().info('Transform between odom and base_link not ready...')
return
def main():
rclpy.init()
node = Trial()
while not rclpy.shutdown():
try:
node.get_global_pos()
rclpy.spin_once(node)
except KeyboardInterrupt:
rclpy.shutdown()
return
if __name__=='__main__':
main()
I get the following error:
ros2 run trial trial
[INFO] [1630305124.566534260] [trial_node]: Fetching transform of 'base_link' with reference to 'odom' frame.
Traceback (most recent call last):
File "/home/adas/Documents/Ayush/dev/intman-proj/dev_ros/nokia_im_ws/install/trial/lib/trial/trial", line 11, in <module>
load_entry_point('trial==0.0.0', 'console_scripts', 'trial')()
File "/home/adas/Documents/Ayush/dev/intman-proj/dev_ros/nokia_im_ws/install/trial/lib/python3.8/site-packages/trial/trial.py", line 49, in main
pose = get_global_pose(tf_buffer, node)
File "/home/adas/Documents/Ayush/dev/intman-proj/dev_ros/nokia_im_ws/install/trial/lib/python3.8/site-packages/trial/trial.py", line 33, in get_global_pose
trans = _tf_buffer.lookup_transform('odom', 'base_link', Duration(0))
TypeError: __init__() takes 1 positional argument but 2 were given
The line seems to cause this error.
trans = self._tf_buffer.lookup_transform('odom', 'base_link', time=Duration(0))
I believe the error is thrown by the buffer.py file, but I am not too sure about it.
class Buffer(tf2.BufferCore, tf2_ros.BufferInterface):
"""
Standard implementation of the :class:`tf2_ros.BufferInterface` abstract data type.
Inherits from :class:`tf2_ros.buffer_interface.BufferInterface` and :class:`tf2.BufferCore`.
Stores known frames and offers a ROS service, "tf_frames", which responds to client requests
with a response containing a :class:`tf2_msgs.FrameGraph` representing the relationship of
known frames.
"""
def __init__(self, cache_time = None, node = None):
"""
Constructor.
:param cache_time: (Optional) How long to retain past information in BufferCore.
:param node: (Optional) If node create a tf2_frames service, It responses all frames as a yaml
"""
if cache_time is not None:
tf2.BufferCore.__init__(self, cache_time)
else:
tf2.BufferCore.__init__(self)
tf2_ros.BufferInterface.__init__(self)
self._new_data_callbacks = []
self._callbacks_to_remove = []
self._callbacks_lock = threading.RLock()
if node is not None:
self.srv = node.create_service(FrameGraph, 'tf2_frames', self.__get_frames)
def lookup_transform(self, target_frame, source_frame, time, timeout=Duration()):
"""
Get the transform from the source frame to the target frame.
:param target_frame: Name of the frame to transform into.
:param source_frame: Name of the input frame.
:param time: The time at which to get the transform. (0 will get the latest)
:param timeout: (Optional) Time to wait for the ...
please do not paraphrase or edit error messages.
Especially in Python, tracebacks contains all sorts of important information. By only showing the message, you make it harder for others to help you, as they now have to guess where that exception is thrown (and/or trust you got it right).
Just copy-paste the complete traceback into your question.
Hey gvdhoorn, apologies for the lack of clarity. I have edited the error as per your suggestion. Hope this gives more clarity.