ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

How to spin ROS2 publisher node and non-ROS function asynchronously?

asked 2022-01-05 21:09:59 -0600

iHany gravatar image

Note: Dup of

Hi, I'd like to spin a publisher node with non-ros code (in this case, MAVSDK-Python) asynchronously.

So far, I cannot find a way to do so.

Is there any way to achieve it?


import rclpy
from rclpy.node import Node

from std_msgs.msg import String

import asyncio
from mavsdk import System

async def run(node):
    drone = System()
    await drone.connect(system_address="serial:///dev/ttyACM0")
    asyncio.ensure_future(print_battery(drone, node))

async def print_battery(drone, node):
    async for battery in drone.telemetry.battery():
        battery_stat = battery.remaining_percent
        node.battery_percent = battery_stat  # update
        print(f"battery: {node.battery_percent}, i: {node.i}")

class MinimalPublisher(Node):
    def __init__(self):
        self.publisher_ = self.create_publisher(String, 'topic', 10)
        timer_period = 0.5  # seconds
        self.timer = self.create_timer(timer_period, self.timer_callback)
        self.i = 0
        self.battery_percent = None
    def timer_callback(self):
        msg = String() = 'Hello World: %d' % self.i
        self.get_logger().info('Publishing: "%s"' %
        self.i += 1

async def spin_once(minimal_publisher):

def main(args=None):

    minimal_publisher = MinimalPublisher()

    # rclpy.spin_once(minimal_publisher)


    # Destroy the node explicitly
    # (optional - otherwise it will be done automatically
    # when the garbage collector destroys the node object)

if __name__ == '__main__':

output (what I expected: printing battery condition and Hello World with increasing iteration number):

➜  ~ python3
[INFO] [1641399256.034609091] [minimal_publisher]: Publishing: "Hello World: 0"
battery: -0.009999999776482582, i: 1
battery: -0.009999999776482582, i: 1
battery: -0.009999999776482582, i: 1
battery: -0.009999999776482582, i: 1
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2022-01-06 08:16:04 -0600

iHany gravatar image

updated 2022-01-10 08:10:11 -0600

I finally found a way to do it.

It might not be the best way as I'm a newbie to asynchronous programming.

To avoid duplication, I leave a link of a working example (a github comment in MAVSDK-Python) here.

EDIT: still, the asynchronous example in the shared link seems "block" other functions while spinning a node, which makes slow other functions. Is it possible to "spin asynchronously"? EDIT2: I found the solution; see

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2022-01-05 21:09:59 -0600

Seen: 431 times

Last updated: Jan 10 '22