Skip to content

Tutorial 3: Inserting a Robot

Margarita Rebolledo edited this page Sep 19, 2019 · 10 revisions

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

Step 1: Loading a Robot

  1. Withing 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
    #!/usr/bin/env python3
    
    import os
    import sys
    import asyncio
    from pyrevolve.SDF.math import Vector3
    from pyrevolve import revolve_bot, parser
    from pyrevolve.tol.manage import World
    from pyrevolve.util.supervisor.supervisor_multi import DynamicSimSupervisor
    from pyrevolve.evolution import fitness
    
    
    async def run():
        """
        The main coroutine, which is started below.
        """
        robot_file_path = "experiments/examples/yaml/spider.yaml"
    
        # 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)
    
        # Load a robot from yaml
        robot = revolve_bot.RevolveBot()
        robot.load_file(robot_file_path)
        robot.update_substrate()
        # robot._brain = BrainRLPowerSplines()
    
        # Connect to the simulator and pause
        connection = await World.create(settings, world_address=('127.0.0.1', settings.port_start))
        await asyncio.sleep(1)
    
        # Starts the simulation
        await connection.pause(False)
    
        # Insert the robot in the simulator
        robot_manager = await connection.insert_robot(robot, Vector3(0, 0, settings.z_start))
    
        # Start a run loop to do some stuff
        while True:
            # Print robot fitness every second
            status = 'dead' if robot_manager.dead else 'alive'
            print(f"Robot fitness ({status}) is: {fitness.displacement(robot_manager, robot)} \n")
            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

```
[2019-09-19 16:18:25,502     gazebo] INFO     Created Supervisor with:
	- simulator command: gazebo ['--verbose']
	- world file: worlds/plane.world
	- GAZEBO_PLUGIN_PATH: /Users/tarigarma/revolve/build/lib
	- GAZEBO_MODEL_PATH: /Users/tarigarma/revolve/models
[2019-09-19 16:18:25,502     gazebo] INFO     Launching the simulator...
[2019-09-19 16:18:26,648     gazebo] INFO     [starting] [Msg] Waiting for master.
[2019-09-19 16:18:26,649     gazebo] INFO     [Msg] Connected to gazebo master @ http://127.0.0.1:11345
[2019-09-19 16:18:26,649     gazebo] INFO     [Msg] Publicized address: 10.3.0.112
[2019-09-19 16:18:26,650     gazebo] INFO     World plugin loaded.
[2019-09-19 16:18:26,650     gazebo] ERROR    Gazebo multi-robot simulator, version 9.7.0
[2019-09-19 16:18:26,650     gazebo] ERROR    Copyright (C) 2012 Open Source Robotics Foundation.
[2019-09-19 16:18:26,650     gazebo] ERROR    Released under the Apache 2 License.
[2019-09-19 16:18:26,650     gazebo] ERROR    http://gazebosim.org
[2019-09-19 16:18:26,650     gazebo] ERROR    
[2019-09-19 16:18:26,650     gazebo] ERROR    Gazebo multi-robot simulator, version 9.7.0
[2019-09-19 16:18:26,650     gazebo] ERROR    Copyright (C) 2012 Open Source Robotics Foundation.
[2019-09-19 16:18:26,651     gazebo] ERROR    Released under the Apache 2 License.
[2019-09-19 16:18:26,651     gazebo] ERROR    http://gazebosim.org
[2019-09-19 16:18:26,651     gazebo] ERROR    
[2019-09-19 16:18:26,835     gazebo] INFO     Setting robot state update frequency to 5.
[2019-09-19 16:18:26,879     gazebo] ERROR    [Wrn] [Publisher.cc:141] Queue limit reached for topic     /gazebo/default/physics/contacts, deleting message. This warning is printed only once.
[2019-09-19 16:18:27,836    revolve] INFO     Resuming the world.
[2019-09-19 16:18:28,051     gazebo] INFO     Processing insert model request ID `168952315`.
[2019-09-19 16:18:28,450     gazebo] INFO     Model `example_spider` inserted, world now contains 1 models.
[2019-09-19 16:18:28,455    revolve] INFO     Registering robot example_spider.
Robot fitness (alive) is: 0.0 

Robot fitness (alive) is: 0.0018332006229095654 

Robot fitness (alive) is: 0.003082761382303827 

Robot fitness (alive) is: 0.005511332102695145 

Robot fitness (alive) is: 0.0029905039980699538 

Robot fitness (alive) is: 0.005352756534245831 


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