Logo

SelectScript_OpenRAVE

1. Introduction

With SelectScript we wanted to implement a general interface for various simulation environments. Retrieving data, information, and also abstractions from a simulation environment should be as easy as querying a database with SQL, without paying attention to the underlying interfaces ... The following video shall give a little impression about the usage of SelectScript. This version, called SelectScript_OpenRAVE, was especially developed for OpenRAVE, but as we will show, it is easy to adapt the language and to integrate new functionalities ... Because our query language is under steady development, we will adapt also this tutorial step by step to allow more insights...

In [1]:
from IPython.display import YouTubeVideo
YouTubeVideo('R_PThP0gwOc', width=640, height=430)
Out[1]:

1. Contact

If you have comments, suggestions, hints, etc. (or even problems), feel free to contact us.

André Dietrich, dietrich@ivs.cs.uni-magdeburg.de, http://eos.cs.ovgu.de/dietrich/

Sebastian Zug, zug@ivs.cs.uni-magdeburg.de, http://eos.cs.ovgu.de/zug/

2. Installation

This extension of SelectScript for OpenRAVE was implemented in Python2.7 and with the help of ANTLR3 (everything was tested on Ubuntu 12.04). There are different ways to install SelectScript_OpenRAVE, but first of all you will have to install OpenRAVE itself on your system, follow the installation instructions at http://openrave.org/docs/latest_stable/install/#install, we recommend to use the latest stable version 0.9.0. Afterwards follow one of the 3 steps listed below:

2.1 Python-Pip

We recommend the usage of the python install tool (pip), which automatically downloads and installs all required sources and dependencies.

pip install SelectScript_OpenRAVE

2.2 Tar.Gz

Download the ta.gz from: https://pypi.python.org/packages/source/S/SelectScript_OpenRAVE/SelectScript_OpenRAVE-1.tar.gz

and install it manually with: python setup.py install

2.3 Subversion

Checkout our project from http://sourceforge.net/projects/selectscript/

svn checkout svn://svn.code.sf.net/p/selectscript/code/trunk/ selectscript

This will download the whole implementation of SelectScript containing the base implementation, grammar files, and other derived implementations for different simulations, etc. Afterwards install the packages according to section 2.2. This will be the most frequently updated version ...

2.4 First Run

We added some basic examples (demo0.py, demo1.py, demo2.py, demo3.py, demo4.py) to demonstrate and present the aspects of our approach. All the demos can be started with the following 2 lines of code ... (you should see a running simulation at this point, or something with the installation went wrong)

In []:
from SelectScript_OpenRAVE.examples import demo0
demo0.start()

3. Initialization

To initialize all required modules run the following commands, which will load OpenRAVE, some further requirements as well as the SelectScript parser and the interpreter for OpenRAVE (ssRAVE).

In [2]:
from openravepy import *; import numpy, os
from SelectScript.SelectScript import SelectScript
import SelectScript_OpenRAVE.interpreter as ssRAVE

Initialize a new OpenRAVE environment and loud our kitchen-example:

In [3]:
env = Environment()     # create the environment
env.Load(os.path.dirname(ssRAVE.__file__)+"/examples/resources/kitchen.env.xml")  # load a model
Out[3]:
False

Afterwards initialize the SelectScript parser and then the interpreter. The parser is the same for every language implementation and generates some kind of simplified byte code, which is easier to interpret. But the interpreter itself has to be adapted, to work with different simulations environments; we will describe this later in more detail.

In [4]:
ssPar = SelectScript(None, None)
ssInt = ssRAVE.interpreter()

To start the visualization run the following command.

In [5]:
env.SetViewer('qtcoin') # start the viewer
Out[5]:
True

We will use an additional feature of OpenRAVE to place screenshots of the simulation into this notebook. The following code is only used for visualization purposes ...

In [6]:
%pylab inline
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = (10.0, 8.0)
def screenshot():
    env.GetViewer().SendCommand('SetFiguresInCamera 1')
    I = env.GetViewer().GetCameraImage(640,480,  env.GetViewer().GetCameraTransform(),[640,640,320,240])
    plt.imshow(I)
    plt.show()
Populating the interactive namespace from numpy and matplotlib

WARNING: pylab import has clobbered these variables: ['log']
`%matplotlib` prevents importing * from pylab and numpy

In [7]:
screenshot() # show a screenshot of the environment ...

4. Grammar

SelectScript is intended to be used for queries only, that is why you can call only externally defined functions (parameters have to be separated with commas), apply basic arithmetic and logic operations, store values in variables, and last but not least define queries (similar to SQL). Every expression has to end with a semicolon and parenthesis is supported.

In [8]:
expr = "1+2*3-4/5.;"       # expression is defined within a string, supported datatypes are bool, int, float, string, list
prog = ssPar.compile(expr) # some bytecode is generated
print ssInt.eval(prog)     # and interpreted ...
6.2

In [9]:
expr = """ 2 * 2;    // multible commands can defined within a script, comments are in C style ...
           a = False; """  # the last expression of a script defines the return value of the whole script
prog = ssPar.compile(expr)  
print ssInt.eval(prog)     
False

In [10]:
expr = """ b = (2+2)*3.45; 
           a = False or True;
           [b, a, "a string", 'or another string']; """  # use lists to define more complex return values ...
prog = ssPar.compile(expr)
print ssInt.eval(prog)
[13.8, True, u'a string', u'or another string']

Variables are stored persistently within the SelectScript interpreter and can also be called from outside with the following methods.

In [11]:
print ssInt.callVariable('b')
13.8

In [12]:
ssInt.addVariable("c", 1.234) # attach a new variable 
print ssInt.callVariable('c')
1.234

The next parts will describe how to use functions and of course, how to use queries and how to extend the language according to different requirements.

5. Get Help

Functions are mostly used to define relations, to get a first overview on available functions simply call help() the result is a list of all defined functions so far ...

In [13]:
expr = "help();"
prog = ssPar.compile(expr)
ssInt.eval(prog)
Out[13]:
['sensingEnvIDs',
 'help',
 'getTime',
 'within',
 'environmentID',
 'id',
 'isEnabled',
 'below',
 'to',
 'above',
 'var',
 'sensor',
 'isRobot',
 'isSensor',
 'pose',
 'volume',
 'position',
 'distance',
 'isKinbody',
 'obj',
 'kinbody',
 'type',
 'clear',
 'robot',
 'sensingAmount',
 'isVisible',
 'y',
 'x',
 'z',
 'isSensing']

To get help for a specific function-call use it in the following way.

In [14]:
expr = "help('isKinbody');"
prog = ssPar.compile(expr)
print ssInt.eval(prog)
isKinbody
Checks if object is of type Kinbody.
Usage: isKinbody(object) -> bool

In [15]:
expr = "help('obj');"
prog = ssPar.compile(expr)
print ssInt.eval(prog)
obj
Returns the object itself.
Usage: obj(object) -> object

6. First Queries

Before we can query the simulation environment, we have to add under a specific name to the interpreter. This is done in the same way as you would attach any other ordinary variable...

In [16]:
# add the environment as variable to the interpreter
ssInt.addVariable('kitchen', env)
Out[16]:
RaveGetEnvironment(1)

Starting with a simple query, where we might only interested in the positions and identifiers from entities within the kitchen environment that fulfill the property of being a robot. The result is given back as list of python dictionaries, similar to a table in databases.

In [17]:
expr = "SELECT id, position, type FROM kitchen WHERE isRobot(this);"
prog = ssPar.compile(expr)
for element in ssInt.eval(prog):
    print element['id'], ": \t",  numpy.round(element['position'],3), "\t", element['type']
fridge : 	[-0.87 -2.    1.55] 	<class 'openravepy._openravepy_.openravepy_int.Robot'>
cooker : 	[ 1.98  1.72  0.75] 	<class 'openravepy._openravepy_.openravepy_int.Robot'>
barrett_4918 : 	[ 0.42   1.3    0.892] 	<class 'openravepy._openravepy_.openravepy_int.Robot'>
roomba_625x : 	[ 1.8   0.    0.17] 	<class 'openravepy._openravepy_.openravepy_int.Robot'>

The return value of a SELECT query can be defined with the key word AS. Further supported abstractions are value and list, the python dict is default and does not have to be defined ...

In [18]:
expr = """// stores the table objects within a variable
          table = SELECT obj FROM kitchen WHERE id(this) == 'table' AS value;

          // select all volumes (AS list) of objects that are above the object 
          // table an with an volume smaller than 0.001
          SELECT volume 
          FROM kitchen 
          WHERE above(table, this) and volume(this) < 0.001 
          AS list; 
       """ 
prog = ssPar.compile(expr)
ssInt.eval(prog)
Out[18]:
[0.00011874852207310614,
 0.00011874852207310614,
 0.00011874852207310614,
 0.00015723991637302734,
 0.00015723991637302772,
 0.00015723991637302755,
 0.00015723991637302712]
In [19]:
screenshot()

Something similar can be also done in the opposite way ...

In [20]:
expr = "SELECT obj, volume FROM kitchen WHERE below(table, this) AS dict;"
prog = ssPar.compile(expr)
ssInt.eval(prog)
Out[20]:
[{u'obj': RaveGetEnvironment(1).GetKinBody('chair_6b56'),
  u'volume': 0.030726317000293702},
 {u'obj': RaveGetEnvironment(1).GetKinBody('chair_083c'),
  u'volume': 0.025596275454522633}]

Or to check which objects are within another object ...

In [21]:
expr = "microwave = SELECT obj FROM kitchen WHERE id(this) == 'microwave' AS value; \
SELECT obj, position FROM kitchen WHERE within(microwave, this) AS list;"
prog = ssPar.compile(expr)
print ssInt.eval(prog)
[RaveGetEnvironment(1).GetKinBody('plate_b4e7'), [-1.1299999999999999, 2.0099999999999998, 1.1399999999999999]]

In [22]:
screenshot()

7. Complex Queries

With the following Python command we simply switch on the lights or in other words, switch on the sensory systems and start to render the sensory measurements.

In [23]:
for sensor in env.GetSensors():
    sensor.Configure(Sensor.ConfigureCommand.PowerOn)
    sensor.Configure(Sensor.ConfigureCommand.RenderDataOn)
In [24]:
screenshot()

To check which of those sensors are currently measuring a robot, we can apply the following select query. The function to in this example is only used to translate the function name and thus the key of the dictionary from id to sensor and robot. This query simply evaluates the cross product of kitchen and kitchen, which is translated into e1 and e2.

Another remarkable aspect in this query is the new keyword this which is applied to place objects into the appropriate place, if functions are called with more than one parameter. So far there was no need for a this pointer, because all called functions required only one parameter, which is automatically substituted with this. For example the function-call volume after a select statement can also be written as volume(this). And if we use more than one "list" have to apply the appropriate name in front of the this pointer, such as e1.this and e2.this.

In [25]:
expr = "SELECT to(id(e1.this), 'sensor'), to(id(e2.this), 'robot')                   \
        FROM e1=kitchen, e2=kitchen                                                  \
        WHERE isSensor(e1.this) AND isRobot(e2.this) AND isSensing(e1.this, e2.this) \
        AS dict;"
prog = ssPar.compile(expr)
ssInt.eval(prog)
Out[25]:
[{u'robot': u'roomba_625x', u'sensor': u'barrett_4918_laser_scaner'}]

The same query can also be simplified with multiple queries, which in the first case queries only for sensor systems and stores the results within a list structure. The same is done for robots afterwards, before these results are combined within the last query.

In [26]:
expr = "sensor = SELECT obj FROM kitchen WHERE isSensor(this) AS list;     \
        robot  = SELECT obj FROM kitchen WHERE isRobot(this)  AS list;     \
                                                                           \
        SELECT to(id(sensor.this), 'sensor'), to(id(robot.this), 'robot')  \
        FROM sensor, robot                                                 \
        WHERE isSensing(sensor.this, robot.this)                           \
        AS dict;"
prog = ssPar.compile(expr)
ssInt.eval(prog)
Out[26]:
[{u'robot': u'roomba_625x', u'sensor': u'barrett_4918_laser_scaner'}]

Or as another possibility, we can simply place queries wherever we want ...

In [27]:
expr = "SELECT to(id(e1.this), 'sensor'), to(id(e2.this), 'robot')                   \
        FROM e1=(SELECT obj FROM kitchen WHERE isSensor(this) AS list),              \
             e2=(SELECT obj FROM kitchen WHERE isRobot(this) AS list)                \
        WHERE isSensor(e1.this) AND isRobot(e2.this) AND isSensing(e1.this, e2.this) \
        AS dict;"
prog = ssPar.compile(expr)
ssInt.eval(prog)
Out[27]:
[{u'robot': u'roomba_625x', u'sensor': u'barrett_4918_laser_scaner'}]

8. Interaction between Languages

The following examples is used to demonstrate how both languages Python a SelectScript can be appropriately combined. At first we might be interested in defining a new relation (a function) to the interpreter that checks whether an object is placed on another. The following function can be used to check this, whereby the programmer has to take care about the data types. o1 and o2 have to be objects of type kinbody or robot, otherwise the call of this function will cause an error. Therefore, be aware to use on only with the appropriate data types or insert some additional checks ...

In [28]:
def on(o1, o2):
    if o1 == o2: return False
    
    ab  = o1.ComputeAABB()
    pos = o2.GetTransform()[:3,3]
    
    if ab.pos()[0] - ab.extents()[0] <= pos[0] <= ab.pos()[0] + ab.extents()[0]:
        if ab.pos()[1] - ab.extents()[1] <= pos[1] <= ab.pos()[1] + ab.extents()[1]:
            if pos[2] >= ab.pos()[2] + ab.extents()[2]:
                return True
    
    return False

Attaching this function to the SelectScript interpreter is rather simple. Use the following method and include some additional information about the appropriate usage.

In [29]:
ssInt.addFunction('onX',   # a name for the function 
                  on,      # the function pointer
                  # some helpfull information
                  """Checks if the position of an object (o2) is above object (o1).
                     Usage: onX(o1, o2) -> bool """)

Afterwards it should also be possible to get some help about the newly defined function.

In [30]:
expr = "help('onX');"
prog = ssPar.compile(expr)
print ssInt.eval(prog)
onX
Checks if the position of an object (o2) is above object (o1).
                     Usage: onX(o1, o2) -> bool 

With the next query, we can simply identify all relevant parameters, which are required to fulfill the job of cleaning up the table.

In [31]:
robot = env.GetRobot("barrett_4918")
ssInt.addVariable('robot', robot)
expr  = "sink    = SELECT obj FROM kitchen WHERE id(this) == 'sink' AS value;"
expr += "table   = SELECT obj FROM kitchen WHERE id(this) == 'table' as value;"    
expr += "cleanUp = SELECT obj FROM kitchen WHERE onX(table, this) AND distance(robot, this) < 1.3 AS list;"
    
prog = ssPar.compile(expr)
ssInt.eval(prog)
Out[31]:
[RaveGetEnvironment(1).GetKinBody('cup_905a'),
 RaveGetEnvironment(1).GetKinBody('cup_58a2'),
 RaveGetEnvironment(1).GetKinBody('cup_b6af'),
 RaveGetEnvironment(1).GetKinBody('cup_d441')]

The example below shows how the results of a query can be easily applied within the program to solve the task of cleaning up the table...

In [32]:
from openravepy.examples.graspplanning import * # import the possibility to grasp
grasp = GraspPlanning(robot)
grasp.graspables = []
for object in ssInt.callVariable('cleanUp'):
    destination = grasp.setRandomDestinations( [object], 
                                                ssInt.callVariable('sink'), 
                                                randomize=True, preserverotation=False)
        
    gModel = databases.grasping.GraspingModel(robot=robot, target=object) 
    if not gModel.load():
        gModel.autogenerate()
    destination = [x.tolist() for x in destination[0]]        
    grasp.graspables.append([gModel,destination])

grasp.performGraspPlanning(withreplacement=True)
robot resolutions:  [ 0.00539782  0.00540015  0.01514345  0.01333564  0.06625722  0.06928872
  0.11318399  0.0446961   0.0446961   0.0446961   0.0302158 ]
robot weights:  [ 1.80578883  1.37230776  0.563284    0.34101693  0.13790135  0.1103156
  0.05769339  0.03253983  0.03253983  0.03253983  0.13534079]
searching for graspable objects (robot=3b8cffafe260780413124da7341b08b5)...
cup_905a is graspable
cup_58a2 is graspable
cup_b6af is graspable
cup_d441 is graspable
searching for destinations on table...
searching for destinations on sink...
searching for destinations on sink...
searching for destinations on sink...
searching for destinations on sink...
starting to pick and place random objects
grasping object cup_b6af
grasp 120 initial planning time: 1.730601
moving hand
current robot array([ -1.13996407e+00,   1.13589385e+00,   1.15916982e-16,
         1.06005976e+00,  -3.14159265e+00,  -9.45639038e-01,
        -2.71076040e+00,   0.00000000e+00,   0.00000000e+00,
         0.00000000e+00,   2.22044605e-16])
global direction array([ -2.88468562e-15,  -1.20880337e-15,  -1.00000000e+00]) [ 0.  0. -1.]
local direction [ 0.  0. -1.]
move hand up
planning to destination
failed to reach a goal, trying to move goal a little up openrave planning_error: 'MoveToHandPosition'
moving hand down
success:  120
grasping object cup_905a
grasp 284 initial planning time: 8.731741
moving hand
current robot array([ -7.66864614e-01,   1.73305242e+00,   2.22387527e+00,
        -4.06126613e-01,  -2.26437844e+00,  -5.00889999e-01,
         1.17509283e+00,   0.00000000e+00,   0.00000000e+00,
         0.00000000e+00,   2.22044605e-16])
global direction array([ 0.49713642, -0.8617768 ,  0.10097587]) [ 0.49713642 -0.8617768   0.10097587]
local direction [ 0.49713642 -0.8617768   0.10097587]
move hand up
planning to destination
moving hand down
success:  284
grasping object cup_58a2
grasp 454 initial planning time: 22.531033
moving hand
current robot array([  1.46221570e+00,  -1.59727151e+00,   1.68750079e-01,
        -8.10719390e-01,  -1.29556888e+00,   1.26036674e+00,
         2.72798152e+00,  -1.24900090e-16,   0.00000000e+00,
         0.00000000e+00,   1.13242749e-14])
global direction array([ 0.86178901, -0.49714411,  0.1008337 ]) [ 0.86178901 -0.49714411  0.1008337 ]
local direction [ 0.86178901 -0.49714411  0.1008337 ]
move hand up
planning to destination
moving hand down
success:  454
grasping object cup_d441
grasp 461 initial planning time: 9.782652
moving hand
current robot array([ 2.01726634, -1.68979145, -0.87473952, -0.58246952, -3.80170288,
       -1.56600089, -0.46809433,  0.        ,  0.        ,  0.        ,  0.        ])
global direction array([  9.94888337e-01,  -4.83856759e-07,  -1.00981171e-01]) [  9.94888337e-01  -4.83856760e-07  -1.00981171e-01]
local direction [  9.94888337e-01  -4.83856760e-07  -1.00981171e-01]
move hand up
planning to destination
moving hand down
robot in collision, moving back a little
success:  461
grasping object cup_905a
failed to grasp object cup_905a

The video below shows the whole cleanup run ...

In [33]:
YouTubeVideo('jSaoCXRNVNg', width=640, height=430)
Out[33]:

9. Callbacks

In some cases it might be useful to execute a specific function if a situation has changed, or in other words, if the result of a SelectScript has changed. The callback from below simply receives the result of a SelectScript query and stores the results within a global variable.

In [34]:
cbResult = []
def callback(result):
    global cbResult
    cbResult = result

The script below was taken from the examples in section "Complex Queries". The only difference is, that we do not execute the program directly but add trigger, which is fired if the result of the query changes and thus executes function callback, whereby the input parameters are always the result of a query.

In [35]:
expr = "env  = kitchen;                                                              \
        SELECT to(id(e1.this), 'sensor'), to(id(e2.this), 'robot')                   \
        FROM e1 = env, e2 = env                                                      \
        WHERE isSensor(e1.this) AND isRobot(e2.this) AND isSensing(e1.this, e2.this) \
        AS dict;                                                                     "
prog = ssPar.compile(expr)
ssInt.addTrigger('sensing_sensors', prog, 1, callback)

The both examples below simply show the changing results of triggered query...

In [36]:
print cbResult
[{u'sensor': u'barrett_4918_laser_scaner', u'robot': u'roomba_625x'}]

In [37]:
screenshot()
In [38]:
print cbResult
[{u'sensor': u'fridge_webcam', u'robot': u'barrett_4918'}, {u'sensor': u'barrett_4918_laser_scaner', u'robot': u'roomba_625x'}]

In [39]:
screenshot()

The same technique was applied to a random walk of both robots. The callback function is executed whenever the set of currently measured robots changes. The callback function in demo2.py only switches on the rendering of sensors that currently perceive a robot.

In [40]:
YouTubeVideo('Bk8TaOQQdZM', width=640, height=430)
Out[40]:

10. Abstracting Results

Last but not least, by developing SelectScript, we also wanted to enable developers to be able to attach own abstraction, which are defined with the keyword AS at the end of every select query. But doing this is a bit more complicated, it requires to extend the interpreter class. In the following three examples, we show that the possibility for abstractions is quite big.

10.1 AS environment

The first abstraction we are going to define is a simple sub scene. The implementation can happen as follows.

In [41]:
class interpeter_environment(ssRAVE.interpreter):
    # a new child class of the previous SelectScript interpreter for OpenRAVE
    def __init__(self):
        ssRAVE.interpreter.__init__(self)
    # extend the evalAS method, if the keyword is environment, we will handle
    # this locally, or otherwise call the parent class ...
    def evalAs(self, AS, SELECT, FROM, FROM_n, WHERE):
        if AS == "environment":
            return self.environment(AS, SELECT, FROM, FROM_n, WHERE)
        return ssRAVE.interpreter.evalAs(self, AS, SELECT, FROM, FROM_n, WHERE)
    # this method simply creates a new environment with all entities that 
    # fulfill the defintions within the WHERE clause
    def environment(self, AS, SELECT, FROM, FROM_n, WHERE):
        mark = True
        newEnv = None
        for elem in FROM:
            if not newEnv: newEnv = elem[0].GetEnv().CloneSelf(1|8)
                
            if WHERE != []:
                mark = self.eval(WHERE, elem, FROM_n)            
                
            if not mark:
                object = self.eval(SELECT, elem, FROM_n)
                     
                if type(object) == Robot:
                    newEnv.Remove(newEnv.GetRobot(object.GetName()))
                elif type(object) == KinBody:
                    newEnv.Remove(newEnv.GetKinBody(object.GetName()))
                        
        return newEnv

The rest is straight forward ...

In [42]:
ssIntX = interpeter_environment()
ssIntX.addVariable('kitchen', env)
    
## Example 1: Query for information about sensors and robots 
expr  = "roomba = SELECT obj FROM kitchen WHERE id(this)=='roomba_625x' AS value;"
expr += "SELECT obj FROM kitchen WHERE distance(roomba, this) <= 1.5 AS environment;"
prog = ssPar.compile(expr)
    
newEnv = ssIntX.eval(prog)
#newEnv.SetViewer('qtcoin')   

See the running example from demo3.py at the YouTube video below ...

In [43]:
YouTubeVideo('k5NXVv6O3tU', width=640, height=430)
Out[43]:

10.2 AS map

Something totally different might be a map for a certain area.... The example from above uses extends therefor the previous class and attaches a new filter. To run this example, you will have to install our filter-plugin for OpenRAVE. This plugin can be found at the following repository: http://code.google.com/p/eos-openrave-plugins/

In [44]:
class interpeter_map(interpeter_environment):
    def __init__(self):
        interpeter_environment.__init__(self)
        RaveInitialize()
        RaveLoadPlugin("/home/andre/Workspace/Projects/ROS/eos-openrave-plugins/filter/lib/filter")
                
    def evalAs(self, AS, SELECT, FROM, FROM_n, WHERE):
        if AS == "roombaGrid":
            return self.roombaGrid(AS, SELECT, FROM, FROM_n, WHERE)
        return interpeter_environment.evalAs(self, AS, SELECT, FROM, FROM_n, WHERE)
        
    def roombaGrid(self, AS, SELECT, FROM, FROM_n, WHERE):
        # only for visualizing:
        env = FROM.next()[0].GetEnv()
        
        if WHERE == []:
            newEnv = FROM.next()[0].GetEnv()
        else:
            newEnv = self.environment(AS, SELECT, FROM, FROM_n, WHERE)
        envmin = []
        envmax = []
        for b in newEnv.GetBodies():
            ab = b.ComputeAABB()
            envmin.append(ab.pos()-ab.extents())
            envmax.append(ab.pos()+ab.extents())
            
        envmin = numpy.floor(numpy.min(numpy.array(envmin), 0)*100.) / 100
        envmax = numpy.ceil( numpy.max(numpy.array(envmax), 0)*100.) / 100 
        size   = numpy.ceil((envmax - envmin) / 0.025)
                
        ogMap = RaveCreateModule(env,'occupancygridmap')
        ogMap.SendCommand('SetTranslation %f %f 0.22' % (envmin[0], envmin[1]))
        ogMap.SendCommand('SetSize %i %i 0.025' %(size[0]+1, size[1]+1) )
        ogMap.SendCommand('SetLineWidth 2.0')
    
        render = ogMap.SendCommand('Scan')
        
        ogMap.SendCommand('Render')
        
        return numpy.fromstring(render, dtype=bool, count=-1, sep='').reshape(int(size[0]+1), int(size[1]+1))

Instantiate this new interpreter class ...

In [45]:
ssIntXX = interpeter_map()
ssIntXX.addVariable('kitchen', env)
Out[45]:
RaveGetEnvironment(1)

And generates some maps ...

In [46]:
## Example 1: Generate a map from the enitre environment 
expr = "SELECT obj FROM kitchen AS roombaGrid;"
prog = ssPar.compile(expr)
map = ssIntXX.eval(prog)    
    
# plot the map
plt.imshow(map.astype(numpy.float), cmap=plt.cm.gray)
plt.show()
In [47]:
## Example 2: Generate a map from all objects that are close to roomba 
expr  = "roomba = SELECT obj FROM kitchen WHERE id(this)=='roomba_625x' AS value;"
expr += "SELECT obj FROM kitchen WHERE distance(roomba, this) <= 1.5 AS roombaGrid;"
prog = ssPar.compile(expr)
map = ssIntXX.eval(prog)    
    
plt.imshow(map.astype(numpy.float), cmap=plt.cm.gray)
plt.show()

See the video below to get an impression, how our filters are applied to the simulation...

In [48]:
YouTubeVideo('MYQppe9E9Es', width=640, height=430)
Out[48]:

10.3 AS prolog

It might sound strange, but with a little piece of code, we can also translate the simulation into a set of prolog clauses.

In [49]:
class interpeter_prolog(ssRAVE.interpreter):
    def __init__(self):
        ssRAVE.interpreter.__init__(self)
                
    def evalAs(self, AS, SELECT, FROM, FROM_n, WHERE):
        if AS == "prolog":
            return self.prolog(AS, SELECT, FROM, FROM_n, WHERE)
        return ssRAVE.interpreter.evalAs(self, AS, SELECT, FROM, FROM_n, WHERE)
        
    def prolog(self, AS, SELECT, FROM, FROM_n, WHERE):
        results = set()
        mark = True 
        for elem in FROM:
            if WHERE != []:
                mark = self.eval(WHERE, elem, FROM_n)
            if mark:
                for f in SELECT:
                    if f[1] == 'to' :
                        result, relation = self.eval(f, elem, FROM_n)
                        pp = f[2][0][2]
                    else:
                        relation = f[1]
                        result = self.eval(f, elem, FROM_n)
                        pp = f[2]
                        
                    if result != False:
                        clause = relation + "("                    
                        for p in pp:
                            p = self.eval(p, elem, FROM_n)
                            if isinstance(p, (openravepy_int.Robot, openravepy_int.Sensor, openravepy_int.KinBody)):
                                p = p.GetName()
                            clause += str(p) + ","                
                        clause = clause[:-1] + ")"
             
                        if result != True:
                            clause = clause[:-1] + ","+str(result)+")"                   
                        
                        results.add(clause)
  
        return sorted(results)

A query afterwards can look like this

In [50]:
ssRave = interpeter_prolog()
ssRave.addVariable('kitchen', env)
expr = "SELECT  above(a.this, b.this),                    \
                isEnabled(a.this),                        \
                volume(a.this),                           \
                position(a.this),                         \
                isRobot(a.this),                          \
                isKinbody(a.this)                         \
         FROM a=kitchen, b=kitchen                        \
         WHERE not (isSensor(a.this) or isSensor(b.this)) \
         AS prolog;"
prog = ssPar.compile(expr)
clauses = ssRave.eval(prog)

To show that this works, we can open an interface to SWI-Prolog and attach the retrieved facts with the example below...

In [51]:
from pyswip import Prolog
prolog = Prolog()
for result in clauses:
    print result
    prolog.assertz(result)
above(microwave,pin_a091)
above(microwave,pin_c671)
above(table,cup_58a2)
above(table,cup_905a)
above(table,cup_b6af)
above(table,cup_d441)
above(table,plate_496f)
above(table,plate_835a)
above(table,plate_fa76)
isEnabled(barrett_4918)
isEnabled(can)
isEnabled(chair_083c)
isEnabled(chair_12d0)
isEnabled(chair_2e39)
isEnabled(chair_6b56)
isEnabled(coffe_machine)
isEnabled(cooker)
isEnabled(cup_58a2)
isEnabled(cup_905a)
isEnabled(cup_b6af)
isEnabled(cup_d441)
isEnabled(fridge)
isEnabled(kitchen)
isEnabled(knife_block)
isEnabled(microwave)
isEnabled(mixer)
isEnabled(pin_371e)
isEnabled(pin_a091)
isEnabled(pin_c671)
isEnabled(plate_496f)
isEnabled(plate_835a)
isEnabled(plate_b4e7)
isEnabled(plate_fa76)
isEnabled(roomba_625x)
isEnabled(shaker)
isEnabled(sink)
isEnabled(table)
isEnabled(toaster)
isKinbody(can)
isKinbody(chair_083c)
isKinbody(chair_12d0)
isKinbody(chair_2e39)
isKinbody(chair_6b56)
isKinbody(coffe_machine)
isKinbody(cup_58a2)
isKinbody(cup_905a)
isKinbody(cup_b6af)
isKinbody(cup_d441)
isKinbody(kitchen)
isKinbody(knife_block)
isKinbody(microwave)
isKinbody(mixer)
isKinbody(pin_371e)
isKinbody(pin_a091)
isKinbody(pin_c671)
isKinbody(plate_496f)
isKinbody(plate_835a)
isKinbody(plate_b4e7)
isKinbody(plate_fa76)
isKinbody(shaker)
isKinbody(sink)
isKinbody(table)
isKinbody(toaster)
isRobot(barrett_4918)
isRobot(cooker)
isRobot(fridge)
isRobot(roomba_625x)
position(barrett_4918,[0.41999999999999998, 1.3, 0.89200000000000002])
position(can,[-1.25, -0.54000000000000004, 0.96999999999999997])
position(chair_083c,[0.52000000000000002, -0.59999999999999998, 0.0])
position(chair_12d0,[0.43229000000000001, 0.17535999999999999, 0.0])
position(chair_2e39,[0.94999999999999996, -1.1499999999999999, 0.0])
position(chair_6b56,[1.3400000000000001, 0.27000000000000002, 0.0])
position(coffe_machine,[-1.335, 0.95999999999999996, 0.96999999999999997])
position(cooker,[1.98, 1.72, 0.75])
position(cup_58a2,[0.60999999999999999, 0.45000000000000001, 0.92200000000000004])
position(cup_905a,[1.0, 0.433, 0.92200000000000004])
position(cup_b6af,[0.76000000000000001, 0.55000000000000004, 0.92200000000000004])
position(cup_d441,[0.96999999999999997, 0.57999999999999996, 0.92200000000000004])
position(fridge,[-0.87, -2.0, 1.55])
position(kitchen,[0.0, 0.0, 0.0])
position(knife_block,[-0.51000000000000001, 2.2999999999999998, 0.96799999999999997])
position(microwave,[-1.1000000000000001, 2.0, 0.96299999999999997])
position(mixer,[1.6899999999999999, 2.2999999999999998, 0.95599999999999996])
position(pin_371e,[-1.2, 1.3999999999999999, 0.95999999999999996])
position(pin_a091,[-1.466, 2.27, 1.6699999999999999])
position(pin_c671,[-1.1100000000000001, 2.286, 1.6699999999999999])
position(plate_496f,[0.67000000000000004, -0.45556000000000002, 0.92200000000000004])
position(plate_835a,[1.0600000000000001, -0.72999999999999998, 0.92200000000000004])
position(plate_b4e7,[-1.1299999999999999, 2.0099999999999998, 1.1399999999999999])
position(plate_fa76,[1.1399999999999999, 0.0, 0.92200000000000004])
position(roomba_625x,[1.8, 0.0, 0.17000000000000001])
position(shaker,[2.1299999999999999, 2.2599999999999998, 0.97999999999999998])
position(sink,[0.34999999999999998, 2.1499999999999999, -0.10000000000000001])
position(table,[0.91703999999999997, -0.12644, 0.0])
position(toaster,[-1.26, -1.1799999999999999, 0.95999999999999996])
volume(can,0.00419759182722)
volume(chair_083c,0.0255962754545)
volume(chair_12d0,0.0375463244818)
volume(chair_2e39,0.0374099325687)
volume(chair_6b56,0.0307263170003)
volume(coffe_machine,0.00355618574541)
volume(cup_58a2,0.000157239916373)
volume(cup_905a,0.000157239916373)
volume(cup_b6af,0.000157239916373)
volume(cup_d441,0.000157239916373)
volume(kitchen,5.32756277229)
volume(knife_block,0.00249344905026)
volume(microwave,0.0352801549276)
volume(mixer,0.00545596624698)
volume(pin_371e,0.0007409003122)
volume(pin_a091,0.0007409003122)
volume(pin_c671,0.0007409003122)
volume(plate_496f,0.000118748522073)
volume(plate_835a,0.000118748522073)
volume(plate_b4e7,0.000118748522073)
volume(plate_fa76,0.000118748522073)
volume(shaker,0.00566537815594)
volume(sink,2e-05)
volume(table,0.163113262069)
volume(toaster,0.00643460745159)

And start to query in Prolog syntax ...

In [52]:
print list(prolog.query("above(table, X)"))
[{'X': 'cup_58a2'}, {'X': 'cup_905a'}, {'X': 'cup_b6af'}, {'X': 'cup_d441'}, {'X': 'plate_496f'}, {'X': 'plate_835a'}, {'X': 'plate_fa76'}]

In [53]:
print list(prolog.query("volume(Obj, V), V > 1.0"))
[{'Obj': 'kitchen', 'V': 5.32756277229}]

In [54]:
print list(prolog.query("position(_, P)"))
[{'P': [0.42, 1.3, 0.892]}, {'P': [-1.25, -0.54, 0.97]}, {'P': [0.52, -0.6, 0.0]}, {'P': [0.43229, 0.17536, 0.0]}, {'P': [0.95, -1.15, 0.0]}, {'P': [1.34, 0.27, 0.0]}, {'P': [-1.335, 0.96, 0.97]}, {'P': [1.98, 1.72, 0.75]}, {'P': [0.61, 0.45, 0.922]}, {'P': [1.0, 0.433, 0.922]}, {'P': [0.76, 0.55, 0.922]}, {'P': [0.97, 0.58, 0.922]}, {'P': [-0.87, -2.0, 1.55]}, {'P': [0.0, 0.0, 0.0]}, {'P': [-0.51, 2.3, 0.968]}, {'P': [-1.1, 2.0, 0.963]}, {'P': [1.69, 2.3, 0.956]}, {'P': [-1.2, 1.4, 0.96]}, {'P': [-1.466, 2.27, 1.67]}, {'P': [-1.11, 2.286, 1.67]}, {'P': [0.67, -0.45556, 0.922]}, {'P': [1.06, -0.73, 0.922]}, {'P': [-1.13, 2.01, 1.14]}, {'P': [1.14, 0.0, 0.922]}, {'P': [1.8, 0.0, 0.17]}, {'P': [2.13, 2.26, 0.98]}, {'P': [0.35, 2.15, -0.1]}, {'P': [0.91704, -0.12644, 0.0]}, {'P': [-1.26, -1.18, 0.96]}]