Skip to content

Tutorial 3: Insert a Robot

Aart Stuurman edited this page Jul 19, 2021 · 2 revisions

Here we will learn how the process of adding a robot into a Gazebo world works.

Step 1: Loading a Robot

  1. Within the project root create an empty file tutorial3.py.
  2. Make sure it is executable:
    sudo chmod +x tutorial3.py
  3. Open the file with a text editor, add this piece of code to the script and save it
    import asyncio
    import logging
    import sys
    import os
    
    from pyrevolve import parser
    from pyrevolve.custom_logging import logger
    
    from pyrevolve.tol.manage import World
    from pyrevolve.util.supervisor.supervisor_multi import DynamicSimSupervisor
    
    from pyrevolve.revolve_bot import RevolveBot
    from pyrevolve.evolution import fitness
    from pyrevolve.SDF.math import Vector3
    
    
    async def run():
        """
        The main coroutine, which is started below
        """
        log = logger.create_logger('experiment', handlers=[
            logging.StreamHandler(sys.stdout),
        ])
    
        # Set debug level to DEBUG
        log.setLevel(logging.DEBUG)
    
        # Parse command line / file input arguments
        settings = parser.parse_args()
    
        # Start Simulator
        if settings.simulator_cmd != 'debug':
            simulator_supervisor = DynamicSimSupervisor(
                world_file=settings.world,
                simulator_cmd=settings.simulator_cmd,
                simulator_args=["--verbose"],
                plugins_dir_path=os.path.join('.', 'build', 'lib'),
                models_dir_path=os.path.join('.', 'models'),
                simulator_name='gazebo'
            )
        await simulator_supervisor.launch_simulator(port=settings.port_start)
        await asyncio.sleep(0.1)
    
        # Connect to the simulator and pause
        connection = await World.create(settings, world_address=('127.0.0.1', settings.port_start))
        await asyncio.sleep(1)
    
        # initialization finished
    
        # load robot file
        robot = RevolveBot(_id=settings.test_robot)
        robot.load_file("experiments/examples/yaml/spider.yaml", conf_type='yaml')
        robot.save_file(f'{"experiments/examples/yaml/spider.yaml"}.sdf', conf_type='sdf')
    
        # insert robot into the simulator
        robot_manager = await connection.insert_robot(robot, Vector3(0, 0, 0.25), life_timeout=None)
        await asyncio.sleep(1.0)
    
        # Start the main life loop
        while True:
            # Print robot fitness every second
            status = 'dead' if robot_manager.dead else 'alive'
            print(f"Robot fitness ({status}) is \n"
                  f" OLD:     {fitness.online_old_revolve(robot_manager)}\n"
                  f" DISPLAC: {fitness.displacement(robot_manager, robot)}\n"
                  f" DIS_VEL: {fitness.displacement_velocity(robot_manager, robot)}")
            await asyncio.sleep(1.0)

NOTE: When the virtual environment is activated, you will see (.venv) designation appearing on the active line within your terminal window. Revolve works within this environment in order to keep your installed Python isolated from unexpected changes. Otherwise, if you feel proficient enough, you can install Revolve as part of your system. For now, we will show examples from the virtual environment.

  1. Run the script:
    (.venv) ./revolve.py --simulator-cmd=gazebo --manager ./tutorial3.py

You should get an output like this (the robot in the picture should be different):

Gazebo screen capture

```
Created Supervisor with:
    - manager command: /home/matteo/projects/revolve/experiments/examples/manager.py ['--simulator', 'gazebo', '--manager', '/home/matteo/projects/revolve/experiments/examples/manager.py', '--restore-directory', 'output/20190404182437']
    - simulator command: gazebo ['--verbose']
    - world file: worlds/gait-learning.world
    - simulator plugin dir: /home/matteo/projects/revolve/build/lib
    - simulator models dir: /home/matteo/projects/revolve/models
Launching all processes...
Launching the simulator...
[simulator-launch] Gazebo multi-robot simulator, version 9.0.0
[simulator-launch] Copyright (C) 2012 Open Source Robotics Foundation.
[simulator-launch] Released under the Apache 2 License.
[simulator-launch] http://gazebosim.org
[simulator-launch] 
[simulator-launch] Gazebo multi-robot simulator, version 9.0.0
[simulator-launch] Copyright (C) 2012 Open Source Robotics Foundation.
[simulator-launch] Released under the Apache 2 License.
[simulator-launch] http://gazebosim.org
[simulator-launch] 
[simulator-launch] [simulator-launch] [Wrn] [Publisher.cc:141] [Msg] Waiting for master.Queue limit reached for topic 
/gazebo/default/physics/contacts, deleting message. This warning is printed only once.
[simulator-launch] [simulator-launch] [Msg] Connected to gazebo master @ http://127.0.0.1:11345
[simulator-launch] [Msg] Publicized address: 145.108.172.94
[simulator-launch] World plugin loaded.
Launching experiment manager...
[manager] STARTING
[simulator] Processing insert model request ID `154482062`.
[simulator] Warning [parser.cc:759] XML Attribute[id] in element[joint] not defined in SDF, ignoring.
[simulator] Warning [parser.cc:759] XML Attribute[id] in element[joint] not defined in SDF, ignoring.
[simulator] Warning [parser.cc:759] XML Attribute[id] in element[joint] not defined in SDF, ignoring.
[simulator] Warning [parser.cc:759] XML Attribute[id] in element[joint] not defined in SDF, ignoring.
[simulator] Warning [parser.cc:759] XML Attribute[id] in element[joint] not defined in SDF, ignoring.
[simulator] Warning [parser.cc:759] XML Attribute[id] in element[joint] not defined in SDF, ignoring.
[simulator] Warning [parser.cc:759] XML Attribute[id] in element[joint] not defined in SDF, ignoring.
[simulator] Warning [parser.cc:759] XML Attribute[id] in element[joint] not defined in SDF, ignoring.
[simulator] Warning [parser.cc:759] XML Attribute[xmlns:rv] in element[plugin] not defined in SDF, ignoring.
[simulator] Model `example_spider` inserted, world now contains 2 models.
[manager] calculating robot size: 17
[manager] Robot fitness is 0.0
[manager] Robot fitness is 0.0
[manager] Robot fitness is 0.0
[manager] Robot fitness is 0.0
```

See next: Tutorial 4: Prepare the Population Configuration


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