Gazebo | Ignition | Community
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Pygazebo, message only being published if done inside an infinite loop?

Hello, Im trying to use pygazbo to interface with gazebo and am running into a problem with the following code

import asyncio
import pygazebo
import pygazebo.msg.factory_pb2
import pygazebo.msg.vector3d_pb2

async def publish_loop():
    manager = await pygazebo.connect()

    publisher = await manager.advertise(
        '/gazebo/default/factory',
        'gazebo.msgs.Factory')

    message = pygazebo.msg.factory_pb2.Factory()
    vec = pygazebo.msg.vector3d_pb2.Vector3d()
    vec.x = 0
    vec.y = 5
    vec.z = 9
    print(vec)
    # message.sdf = "/models/object.urdf"
    message.sdf_filename = "model://test"
    # message.pose = vec
    print(message)

    while True:
        await publisher.publish(message)
        await asyncio.sleep(1)




loop = asyncio.get_event_loop()
loop.run_until_complete(publish_loop())

The above code manages to spawn in the test object (a simple box) but it does so continuously due to the infinite loop. Ive made several attempts at just creating one box via code such as the following:

async def publish_loop():
    manager = await pygazebo.connect()

    publisher = await manager.advertise(
        '/gazebo/default/factory',
        'gazebo.msgs.Factory')

    message = pygazebo.msg.factory_pb2.Factory()
    vec = pygazebo.msg.vector3d_pb2.Vector3d()
    vec.x = 0
    vec.y = 5
    vec.z = 9
    print(vec)
    # message.sdf = "/models/object.urdf"
    message.sdf_filename = "model://test"

    # message.pose = vec
    print(message)
    await publisher.publish(message)
    await asyncio.sleep(1)

loop = asyncio.get_event_loop()
loop.run_until_complete(publish_loop())

But run into the same issue, namely that nothing happens at all.

Checking the topic visualizer in the GUI, the infinite loop version correctly registers but the single message publish version doesnt appear to be reaching the topic at all.

Does anyone have any insights as to why this is the case? Am i just misusing asyncio?

(also, does anyone know why I cant assign the pose field of the message like in the commented code without getting a protobuff error?)