# Aseba Playground

This document contains additional information on how to program robots in Playground, a multi-robot simulator bundled with Aseba.

# Events

## Local Events

### Proximity sensors

The code following the instruction `onevent ir_sensors` will be executed at regular intervals, just after the distance to obstacles (`prox`) has been updated.

### Camera

The code following the instruction `onevent camera` will be executed at regular intervals, just after the linear camera pixels `camR`, `camG`, `camB` have been updated.

## Emit

`emit name [arguments]`

`emit` specifies that a given robot should send an event of type name to all other robots. The event may contain up to 32 arguments. Note that you cannot choose to which robot the event will be sent, since just like a radio signal, it is broadcast to all robots (see the example below in [args] for how you can address individual robots). Note also that the name of the event and the number of arguments have to be declared in the Global Events pane of Aseba Studio.

# Internal Variables

## id

An identifier whose value is unique for every robot.

## source

The id of the robot from which the most recent event was received.

## args

The arguments of the most recent event received on this robot.

Example: This example shows how events can target individual robots even if the signal was broadcast to all robots. The idea is to use a vector in which each position is allocated to a specific robot: when receiving the event a robot will read only a specific part of this vector.

The robot 1 sends a move-ahead event to robot 2, waits, then sends a move-ahead event to robot 3 and a stop to robot 2, waits, and finally sends a general stop message.

Code for robot 1:

    # variables
    var counter = 0

    onevent ir_sensors

    if counter == 10 then
        emit move [1,0]
    end

    if counter == 100 then
        emit move [0,1]
    end

    counter += 1

Code for robot 2:

    # events
    onevent move
        if args[0] == 1 then
            leftSpeed = 400
            rightSpeed = 400
        else
            leftSpeed = 0
            rightSpeed = 0
        end

    onevent stop
        leftSpeed = 0
        rightSpeed = 0

Code for robot 3:

    # events
    onevent move
        if args[1] == 1 then
            leftSpeed = 400
            rightSpeed = 400
        else
            leftSpeed = 0
            rightSpeed = 0
        end

    onevent stop
        leftSpeed = 0
        rightSpeed = 0

## leftSpeed, rightSpeed

Wheel speed. Choose 400 for slow motion.

## colorR, colorG, colorB

The red, green and blue components of the robot's colour.

## prox

The distance to obstacles perceived by the sensors. Note that noise produces fluctuations in the measurements. See schema here.

## camR, camG, camB

Array of length 60. The red, green and blue components of values returned by the robot's camera.

## energy

Integer: [0 9]. The robot's energy level.

# Create your own environment

Playground allows specific simulation environments to be defined. For instance, the following code creates a box containing four robots:

    <!DOCTYPE aseba-playground>
    <aseba-playground>
        <color name="wall" r="0.9" g="0.9" b="0.9" />
        <color name="red" r="0.77" g="0.2" b="0.15" />
        <color name="green" r="0" g="0." b="0.17" />
        <color name="blue" r="0" g="0.38" b="0.61" />

        <world w="110.4" h="110.4" color="wall"/>

        <e-puck x="40" y="40"/>
        <e-puck x="40" y="60"/>
        <e-puck x="60" y="40"/>
        <e-puck x="60" y="60"/>
    </aseba-playground>

Example: Get here a model of the German Pavilion, 1929, designed by Mies van der Rohe.