Skip to content

Broken tutorial 2

Jie Luo edited this page Jan 23, 2021 · 2 revisions

TUTORIAL BROKEN FROM HERE ON

Step 2: Inserting a model programatically

To create SDF models, Revolve has sdfbuilder submodule. It defines all of the mentioned elements and makes it easy to define and nest them.

  1. Replace with the following piece of code to the script:

    #!/usr/bin/env python3
    import asyncio
    import os
    import sys
    
    here = os.path.dirname(os.path.abspath(__file__))
    rvpath = os.path.abspath(os.path.join(here, '..', 'revolve'))
    sys.path.append(os.path.dirname(os.path.abspath(__file__)))
    
    from pyrevolve.gazebo.manage import WorldManager as World
    from pyrevolve.sdfbuilder import Model, Link, SDF
    from pyrevolve.sdfbuilder.math import Vector3
    
    
    async def run():
    
        world = await World.create()
        if world:
            print("Connected to the simulator world.")
        
        model = Model(
                name='sdf_model',
                static=True,
        )
        model.set_position(position=Vector3(0, 0, 1))
        link = Link('sdf_link')
        link.make_sphere(
                mass=10e10,
                radius=0.5,
        )
        link.make_color(0.7, 0.2, 0.0, 1.0)
        
        model.add_element(link)
        sdf_model = SDF(elements=[model])
        
        await world.insert_model(sdf_model)
        await world.pause(True)
        
        while True:
            await asyncio.sleep(10.0)
    
    
    def main():
        loop = asyncio.get_event_loop()
        loop.run_until_complete(run())
    
    
    if __name__ == "__main__":
        main()
  2. Make sure you are using the virtual environment (.venv).

  3. Run the script:

    (.venv) ./revolve.py --simulator-cmd=gazebo --manager ./tutorial2.py

The output should be:

We can see that it is not the same sphere as above. We changed the position and the colour. Take a look in the code on how it is done.

Final script

The final script should look like:

#!/usr/bin/env python3
import asyncio
import os
import sys

here = os.path.dirname(os.path.abspath(__file__))
rvpath = os.path.abspath(os.path.join(here, '..', 'revolve'))
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from pygazebo.pygazebo import DisconnectError

from pyrevolve.gazebo.manage import WorldManager as World
from pyrevolve.sdfbuilder import Model, Link, SDF
from pyrevolve.sdfbuilder.math import Vector3


async def run():
    world = await World.create()
    if world:
        print("Connected to the simulator world.")

    model = Model(
            name='sdf_model',
            static=True,
    )
    model.set_position(position=Vector3(0, 0, 1))
    link = Link('sdf_link')
    link.make_sphere(
            mass=10e10,
            radius=0.5,
    )
    link.make_color(0.7, 0.2, 0.0, 1.0)

    model.add_element(link)
    sdf_model = SDF(elements=[model])

    await world.insert_model(sdf_model)
    await world.pause(True)
    while True:
        await asyncio.sleep(10.0)


def main():
    def handler(loop, context):
        exc = context['exception']
        if isinstance(exc, DisconnectError) \
                or isinstance(exc, ConnectionResetError):
            print("Got disconnect / connection reset - shutting down.")
            sys.exit(0)
        raise context['exception']

    try:
        loop = asyncio.get_event_loop()
        loop.set_exception_handler(handler)
        loop.run_until_complete(run())
    except KeyboardInterrupt:
        print("Got Ctrl+C, shutting down.")


if __name__ == "__main__":
    main()

You can find this example with other tutorial scripts within Revolve experiments/examples directory.

This tutorial gives you some basic idea of how it all works. Now, let us insert some robots into the world!

_________________
/ Premature      \
| optimization   |
| is the root of |
| all evil.      |
|                |
\ -- D.E. Knuth  /
-----------------
    \   ^__^
     \  (oo)\_______
        (__)\       )\/\
            ||----w |
            ||     ||
Clone this wiki locally