Examples

A simple example

from controller import Supervisor
import PuPy

# checks
checks = []
checks.append(PuPy.RevertTumbled(grace_time_ms=2000))
checks.append(PuPy.RevertMaxIter(max_duration_ms=20*50*2*20))

# set up supervisor
s = PuPy.supervisorBuilder(Supervisor, 20, checks)

# run
s.run()
from controller import Robot
import PuPy
import os.path

# gait params
gait_params = {
    'bound_1Hz': {
            'frequency' : (1.0, 1.0, 1.0, 1.0),
            'offset'    : ( -0.23, -0.23, -0.37, -0.37),
            'amplitude' : ( 0.56, 0.56, 0.65, 0.65),
            'phase'     : (0.0, 0.0, 0.5, 0.5)
        },
    'bound_left_1Hz': {
            'frequency' : (1.0, 1.0, 1.0, 1.0),
            'offset'    : ( -0.23, -0.23, -0.37,-0.37),
            'amplitude' : ( 0.38, 0.65, 0.47, 0.65),
            'phase'     : (0.1141, 0.0, 0.611155, 0.5)
        },
    'bound_right_1Hz': {
            'frequency' : (1.0, 1.0, 1.0, 1.0),
            'offset'    : ( -0.23, -0.23, -0.37, -0.37),
            'amplitude' : ( 0.65, 0.38, 0.65, 0.47),
            'phase'     : (0.0, 0.1141, 0.5, 0.611155)
        }
}

# gaits
gaits = [PuPy.Gait(gait_params[g], name=g) for g in gait_params]

# actor
actor = PuPy.RandomGaitControl(gaits)
#actor = PuPy.ConstantGaitControl(gaits[2])
observer = PuPy.RobotCollector(actor, expfile='/tmp/puppy_sim.hdf5')

# robot
r = PuPy.robotBuilder(Robot, observer, sampling_period_ms=20, noise_ctrl=None, noise_obs=None)

# run
r.run()

Try out the code by storing the excerpts on your disk and execute:

world_builder -c <pth/to/robot> -s <pth/to/supervisor> -t styrofoam /tmp/webots_test

A custom collector

It’s basically the same example as above. The main difference is that the observation file contains a new dataset ‘random’ (with just random values). It has the same length as the other datasets but ten columns (instead of the usual single one).

from controller import Robot
import PuPy
import os.path
import numpy as np

# gait params
gait_params = {
    'frequency' : (1.0, 1.0, 1.0, 1.0),
    'offset'    : ( -0.23, -0.23, -0.37, -0.37),
    'amplitude' : ( 0.56, 0.56, 0.65, 0.65),
    'phase'     : (0.0, 0.0, 0.5, 0.5)
}

# gaits
gait = PuPy.Gait(gait_params)

# Multidimensional collector
class MyCollector(PuPy.RobotCollector):
    def __call__(self, epoch, time_start_ms, time_end_ms, step_size):
        if len(epoch) > 0:
            epoch['random'] = np.random.normal(size=(100,10))
        return super(MyCollector, self).__call__(epoch, time_start_ms, time_end_ms, step_size)


# actor
actor = PuPy.ConstantGaitControl(gait)
observer = MyCollector(actor, expfile='/tmp/puppy_sim.hdf5')


# robot
r = PuPy.robotBuilder(Robot, observer, sampling_period_ms=20, noise_ctrl=None, noise_obs=None)

# run
r.run()

Supervisor to Robot communication

The robot will walk unspectacularly but a message will be printed to the console every 100ms. The message is ‘Emitting <nr>‘, with <nr> a multiple of 100.

from controller import Supervisor
import PuPy

# emitter checks
class EmitterCheck(PuPy.SupervisorCheck):
    def __call__(self, supervisor):
        supervisor.emitter.send('Emitting ' + str(supervisor.num_iter))

checks = []
checks.append(EmitterCheck())

# set up supervisor
PuPy.supervisorBuilder(Supervisor, 100, checks).run()
from controller import Robot
import PuPy
import os.path
import numpy as np

# gait params
gait_params = {
    'frequency' : (1.0, 1.0, 1.0, 1.0),
    'offset'    : ( -0.23, -0.23, -0.37, -0.37),
    'amplitude' : ( 0.56, 0.56, 0.65, 0.65),
    'phase'     : (0.0, 0.0, 0.5, 0.5)
}

# gaits
gait = PuPy.Gait(gait_params)

# receiver callback
def recv1(robot, epoch, current_time, msg):
    print "recv1", msg

def recv2(robot, epoch, current_time, msg):
    print "recv2", msg

# actor
actor = PuPy.ConstantGaitControl(gait)


# robot
r = PuPy.robotBuilder(Robot, actor, event_period_ms=50, event_handlers=[recv1, recv2])
r.run()

Table Of Contents

Previous topic

Utils

Next topic

Download

This Page