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

charlie92's profile - activity

2023-04-07 15:08:41 -0500 received badge  Famous Question (source)
2023-04-04 08:00:17 -0500 received badge  Notable Question (source)
2023-03-20 16:37:26 -0500 received badge  Popular Question (source)
2023-03-17 18:20:02 -0500 marked best answer [ROS2] asyncio await with timeout a service call in a callback

Hi! I am trying to use the asyncio syntax to await for a service to complete inside a callback function of a Node. A dummy node to test this:

#!/usr/bin/env python3
import rclpy
from rclpy.callback_groups import ReentrantCallbackGroup
from rclpy.executors import SingleThreadedExecutor
from rclpy.node import Node
from std_msgs.msg import Empty
from rcl_interfaces.srv import ListParameters
import asyncio

class TestNode(Node):
    def __init__(self) -> None:
        Node.__init__(
            self,
            "test_node",
        )
        self.callback_group = ReentrantCallbackGroup()
        self._sub = self.create_subscription(
            topic="test",
            msg_type=Empty,
            callback=self.cb_test,
            qos_profile=5,
            callback_group=self.callback_group,
        )

        self.client = self.create_client(
            ListParameters,
            "/test_node/list_parameters"
        )

    async def cb_test(self, msg: Empty) -> None:

        service_request = ListParameters.Request()
        srv_future = self.client.call_async(service_request)
        try:
            # result = await asyncio.wait_for(srv_future, timeout=10.0)
            result = await srv_future
        except Exception as e:
            self.get_logger().error(f"Service call failed! what(): {str(e)}")
        else:
            self.get_logger().info(f"Service call success! what(): {str(result)}")


def main(args=None):
    rclpy.init(args=args)

    node = TestNode()
    executor = SingleThreadedExecutor()

    rclpy.spin(node, executor)
    node.destroy_node()
    rclpy.try_shutdown()


if __name__ == "__main__":
    main()

You can test it with

ros2 topic pub -1 /test std_msgs/msg/Empty {}

That code works just fine. The callback function doesn't block any other callback functions and it returns the result of the service. Now If I want to add a timeout to that future with asyncio.wait_for(future, timeout=10.0) it fails with:

[ERROR][1679007966.209838027][test_node]: Service call failed! what(): no running event loop

I tried everything, spawning a new event_loop in a separate thread, calling event_loop.run_forever in the main thread, and spawning the rclpy.spin in a thread. Nothing works.

I tried this alternative approach:

    # ... another method from the previous code
    def run(self):

        self.event_loop = asyncio.get_event_loop()

        # this task will run forever to process ROS node events
        async def rosloop():
            while rclpy.ok():
                rclpy.spin_once(self, timeout_sec=0)
                await asyncio.sleep(0.01)

        # perform main event loop processing
        try:
            asyncio.ensure_future(rosloop())
            self.event_loop.run_forever()
        except KeyboardInterrupt:
            pass
        finally:
            self.event_loop.close()
            self.event_loop = None

def main(args=None):
    rclpy.init(args=args)

    node = TestNode()    
    node.run()
    rclpy.try_shutdown()

Now I get the error:

[ERROR][1679008951.750327026][test_node]: Service call failed! what(): await wasn't used with future

Does anyone know how to handle this? Any asyncio and rclpy expert for advice? I want to stick with a Singlethreaded executor

[UPDATE] PD: If I use the async_timeout package and the alternative approach for spinning the node it works, but I still don't want to depend on an external package

2023-03-17 18:18:25 -0500 answered a question [ROS2] asyncio await with timeout a service call in a callback

I find a workaround using asyncio.ensure_future as follows: class TestNode(Node): def __init__(self) -> None:

2023-03-17 18:18:25 -0500 received badge  Rapid Responder (source)
2023-03-17 10:36:43 -0500 commented answer [ROS2] asyncio await with timeout a service call in a callback

I think it has nothing to do with callback groups but the interaction of asyncio with ROS2 internals The example you sha

2023-03-17 10:36:24 -0500 commented answer [ROS2] asyncio await with timeout a service call in a callback

I think it has nothing to do with but the interaction of asyncio with ROS2 internals The example you shared looks exactl

2023-03-17 10:35:53 -0500 commented answer [ROS2] asyncio await with timeout a service call in a callback

I think it has nothing to do with but the interaction of asyncio with ROS2 internals The example you shared is looks exa

2023-03-17 10:34:02 -0500 received badge  Critic (source)
2023-03-17 10:26:50 -0500 edited question [ROS2] asyncio await with timeout a service call in a callback

[ROS2] asyncio await with timeout a service call in a callback Hi! I am trying to use the asyncio syntax to await for a

2023-03-17 10:26:50 -0500 received badge  Associate Editor (source)
2023-03-16 18:28:38 -0500 edited question [ROS2] asyncio await with timeout a service call in a callback

[ROS2] asyncio await with timeout a service call in a callback Hi! I am trying to use the asyncio syntax to await for a

2023-03-16 18:26:57 -0500 edited question [ROS2] asyncio await with timeout a service call in a callback

[ROS2] asyncio await with timeout a service call in a callback Hi! I am trying to use the asyncio syntax to await for a

2023-03-16 18:26:14 -0500 edited question [ROS2] asyncio await with timeout a service call in a callback

[ROS2] asyncio await with timeout a service call in a callback Hi! I am trying to use the asyncio syntax to await for a

2023-03-16 18:10:27 -0500 edited question [ROS2] asyncio await with timeout a service call in a callback

[ROS2] asyncio await with timeout a service call in a callback Hi! I am trying to use the asyncio syntax to await for a

2023-03-16 18:08:34 -0500 asked a question [ROS2] asyncio await with timeout a service call in a callback

[ROS2] asyncio await with timeout a service call in a callback Hi! I am trying to use the asyncio syntax to await for a

2023-01-14 16:08:36 -0500 received badge  Famous Question (source)
2022-07-27 06:59:01 -0500 received badge  Famous Question (source)
2022-07-27 06:59:01 -0500 received badge  Notable Question (source)
2022-06-20 17:14:11 -0500 edited answer tf2::toMsg(tf2::Vector3) in ROS2 Foxy?

Later versions of ROS2 (galactic) brought back the non-stamped versions https://github.com/ros2/geometry2/blob/ros2/tf2_

2022-06-20 17:13:58 -0500 answered a question tf2::toMsg(tf2::Vector3) in ROS2 Foxy?

Later versions of ROS2 (galactic) brought make the non-stamped versions https://github.com/ros2/geometry2/blob/ros2/tf2_

2022-06-10 17:07:21 -0500 received badge  Notable Question (source)
2022-03-24 03:50:26 -0500 received badge  Famous Question (source)
2022-02-11 15:34:59 -0500 edited question Way of scaling the simulation in size for GPS plugin

Way of scaling the simulation in size for GPS plugin edit Currently, I am recreating a small part of my neighborhood in

2022-02-11 15:31:31 -0500 asked a question Way of scaling the simulation in size for GPS plugin

Way of scaling the simulation in size for GPS plugin Currently, I am recreating a small part of my neighborhood in gazeb

2022-02-11 15:30:33 -0500 asked a question Way of scaling the simulation in size for GPS plugin

Way of scaling the simulation in size for GPS plugin edit Currently, I am recreating a small part of my neighborhood in

2021-12-27 12:02:10 -0500 received badge  Necromancer (source)
2021-12-26 19:59:20 -0500 answered a question ros2 merging maps for multi-robot exploration

Hi Nick. I just finished porting multirobot_map_merge package to ROS2. You can check it out at https://github.com/robo-f

2021-12-13 10:26:44 -0500 received badge  Popular Question (source)
2021-11-30 04:02:29 -0500 received badge  Notable Question (source)
2021-11-29 17:30:59 -0500 received badge  Popular Question (source)
2021-11-29 17:05:09 -0500 edited question Best nav2 controller/planner for a near zero-turn differential-ackermann steer mixed rover

Best nav2 controller/planner for a near zero-turn differential-ackermann steer mixed rover Hi folks! I have a rover that

2021-11-29 14:52:10 -0500 received badge  Favorite Question (source)
2021-11-29 09:20:20 -0500 edited question Best nav2 controller/planner for a near zero-turn differential-ackermann steer mixed rover

Best nav2 controller/planner for a near zero-turn differential-ackermann steer mixed rover Hi folks! I have a rover that

2021-11-29 09:19:31 -0500 asked a question Best nav2 controller/planner for a near zero-turn differential-ackermann steer mixed rover

Best nav2 controller/planner for a near zero-turn differential-ackermann steer mixed rover Hi folks! I have a rover that

2021-11-25 09:13:13 -0500 commented answer Spawning multiple robots in Gazebo and assinging namespaces

Yes, I could manage to run multiple robots with multiple slam_toolbox instances that way, which is using a namespace for

2021-11-25 02:17:04 -0500 received badge  Necromancer (source)
2021-11-25 00:36:59 -0500 answered a question Spawning multiple robots in Gazebo and assinging namespaces

I think your problem is indeed with your tf tree. Each robot should have a separated tf tree, e.g /robot1/tf, /robot2/tf

2021-11-17 13:18:46 -0500 received badge  Necromancer (source)
2021-11-17 13:18:46 -0500 received badge  Teacher (source)
2021-11-17 00:26:36 -0500 edited answer [ROS2] How to get all topic names

The function is called get_topic_names_and_types`. For a more in depth answer go to this almost duplicated question ED

2021-11-17 00:23:26 -0500 edited answer [ROS2] How to get all topic names

The function is called get_topic_names_and_types`. For a more in depth answer go to this almost duplicated question ED

2021-11-15 22:46:08 -0500 answered a question [ROS2] How to get all topic names

The function is called get_topic_names_and_types`. For a more in depth answer go to this almost duplicated question

2021-10-29 16:54:24 -0500 marked best answer How to get nav2 BT state/running nodes

Is there any topic or service from where you can get the current state of the BT? How to know for example when the BT entered a recovery branch and its what iis the status of its childs?

2021-10-28 18:46:10 -0500 edited question How to get nav2 BT state/running nodes

How to get nav2 BT state/running nodes Is there any topic or service from where you can get the current state of the BT?

2021-10-28 18:46:02 -0500 edited question How to get nav2 BT state/running nodes

How to get nav2 BT state/running nodes Is there any topic or service from where you can get the current state of the BT?

2021-10-28 18:45:44 -0500 edited question How to get nav2 BT state/running nodes

How to get nav2 BT state/running plugins Is there any topic or service from where you can get the current state of the B

2021-10-28 18:44:26 -0500 edited question How to get nav2 BT state/running nodes

How to get nav2 BT state/running nodes Is there any topic or service from where you can get the current state of the BT?