Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with Using Time (Python) #3080

Closed
Klarinemend opened this issue Oct 5, 2022 · 3 comments
Closed

Problems with Using Time (Python) #3080

Klarinemend opened this issue Oct 5, 2022 · 3 comments
Labels

Comments

@Klarinemend
Copy link

https://docs.ros.org/en/humble/Tutorials/Intermediate/Tf2/Learning-About-Tf2-And-Time-Py.html

When I try to fix the node, using what you gave (and removing the word raise):

trans = self._tf_buffer.lookup_transform(
to_frame_rel,
from_frame_rel,
now,
timeout=rclpy.duration.Duration(seconds=1.0))

Appear a problem saying that "_tf_buffer" is wrong, and it should be "tf_buffer". But after change it, another error came saying "now" is not defined and asked if I meant pow. After that, I remove the word "now" and it was said that was missing one sentence.

I am using the last version of Humble Hawksbill, and I even try to use the commands of Foxy Fitzroy, but didn't work either.

@clalancette
Copy link
Contributor

Yes, you are right. That whole block should be:

trans = self.tf_buffer.lookup_transform(
   to_frame_rel,
   from_frame_rel,
   rclpy.time.Time())

If you'd like to propose a pull request with that change, I'd be happy to review it.

@Klarinemend
Copy link
Author

But in this way I wouldn't be able to use time, I try but it gives the following error:
image

This is how my files turned out:
import math

from geometry_msgs.msg import Twist

import rclpy
from rclpy.node import Node

from tf2_ros import TransformException
from tf2_ros.buffer import Buffer
from tf2_ros.transform_listener import TransformListener
from tf2_ros import LookupException, ConnectivityException, ExtrapolationException

from turtlesim.srv import Spawn

class FrameListener(Node):

def __init__(self):
    super().__init__('turtle_tf2_frame_listener')

    # Declare and acquire `target_frame` parameter
    self.target_frame = self.declare_parameter(
      'target_frame', 'turtle1').get_parameter_value().string_value

    self.tf_buffer = Buffer()
    self.tf_listener = TransformListener(self.tf_buffer, self)

    # Create a client to spawn a turtle
    self.spawner = self.create_client(Spawn, 'spawn')
    # Boolean values to store the information
    # if the service for spawning turtle is available
    self.turtle_spawning_service_ready = False
    # if the turtle was successfully spawned
    self.turtle_spawned = False

    # Create turtle2 velocity publisher
    self.publisher = self.create_publisher(Twist, 'turtle2/cmd_vel', 1)

    # Call on_timer function every second
    self.timer = self.create_timer(1.0, self.on_timer)

def on_timer(self):
    # Store frame names in variables that will be used to
    # compute transformations
    from_frame_rel = self.target_frame
    to_frame_rel = 'turtle2'

    if self.turtle_spawning_service_ready:
        if self.turtle_spawned:
            # Look up for the transformation between target_frame and turtle2 frames
            # and send velocity commands for turtle2 to reach target_frame
            try:
              trans = self.tf_buffer.lookup_transform(
              to_frame_rel,
              from_frame_rel,
              rclpy.time.Time(seconds=2.0))
            except TransformException as ex:
                self.get_logger().info(
                    f'Could not transform {to_frame_rel} to {from_frame_rel}: {ex}')
            except (LookupException, ConnectivityException, ExtrapolationException):
                self.get_logger().info('transform not ready')
                return

            msg = Twist()
            scale_rotation_rate = 1.0
            msg.angular.z = scale_rotation_rate * math.atan2(
                trans.transform.translation.y,
                trans.transform.translation.x)

            scale_forward_speed = 0.5
            msg.linear.x = scale_forward_speed * math.sqrt(
                trans.transform.translation.x ** 2 +
                trans.transform.translation.y ** 2)

            self.publisher.publish(msg)
        else:
            if self.result.done():
                self.get_logger().info(
                    f'Successfully spawned {self.result.result().name}')
                self.turtle_spawned = True
            else:
                self.get_logger().info('Spawn is not finished')
    else:
        if self.spawner.service_is_ready():
            # Initialize request with turtle name and coordinates
            # Note that x, y and theta are defined as floats in turtlesim/srv/Spawn
            request = Spawn.Request()
            request.name = 'turtle2'
            request.x = float(4)
            request.y = float(2)
            request.theta = float(0)
            # Call request
            self.result = self.spawner.call_async(request)
            self.turtle_spawning_service_ready = True
        else:
            # Check if the service is ready
            self.get_logger().info('Service is not ready')

def main():
rclpy.init()
node = FrameListener()
try:
rclpy.spin(node)
except KeyboardInterrupt:
pass

rclpy.shutdown()

@clalancette
Copy link
Contributor

We "fixed" this by removing the tutorial in #4384, so closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants