André Dietrich, Sebastian Zug, Luigi Nardi, and Jörg Kaiser
# loading the basic interpreter for ODE
import SelectScript
import SelectScript_ODE.Interpreter as ssODE
ssODE = ssODE()
expr = """ 2 + 44 * 16^0.5; """
prog = SelectScript.compile(expr)
print ssODE.eval(prog)
178.0
expr = """ True AND False OR True; """
prog = SelectScript.compile(expr)
print ssODE.eval(prog)
True
expr = """ help(); """
prog = SelectScript.compile(expr)
print ssODE.eval(prog)
['isRay', 'force', 'help', 'isTransform', 'getTime', 'torque', 'pos', 'collision', 'isHeightfield', 'isCylinder', 'linearVelocity', 'uniq', 'rot', 'id', 'isBox', 'isSphere', 'isPlane', 'isEnabled', 'isSpace', 'to', 'isCCylinder', 'max', 'print', 'isCapsule', 'hasBody', 'gravityMode', 'quat', 'aabb', 'volume', 'var', 'isTriMesh', 'mass', 'count', 'distance', 'obj', 'type', 'clear', 'min', 'posZ', 'posX', 'posY', 'velocity', 'sum', 'isGeom', 'quantum']
expr = """ help("mass"); """
prog = SelectScript.compile(expr)
print ssODE.eval(prog)
mass Returns the mass of an object. Usage: mass(object) -> float
expr = """ help("pos"); """
prog = SelectScript.compile(expr)
print ssODE.eval(prog)
pos Returns a list of the x,y,z coordinates of an object. Usage: pos(object, begin, end) pos(object) -> [x,y,z] pos(object, 0,2) -> [x,y]
from odeViz.examples import chaos
sim = chaos.Simulation(dt=0.01, dx=10)
sim.setParam(seed=18, gravity=(0,0,0), bounce=.3, Mu=500,
minmaxForce=10000, probExplode=.1, probInsert=.5)
sim.start(iren_start=False)
ssODE.addVariable('space', sim.space[0]) # < attaching
odeViz ====== VisualizationToolKit (vtk) for the OpenDynamicsEngine Key-Press-Events ---------------- Ctrl + c|C -> print Camera-settings Ctrl + h|H -> take a screensHot Ctrl + p|P -> Pause and run simulation Ctrl + q|Q -> Quit simualtion Ctrl + s|S -> enable/disable StereoScopic view Ctrl + v|V -> enable/disable Visualization
<ode.SimpleSpace at 0x7f131ffca230>
expr = """# comments start with #
SELECT mass, pos, id(this, space) FROM space
WHERE isSphere(this) and mass(this) > 50
AS dict; """
prog = SelectScript.compile(expr)
ssODE.eval(prog)
[{u'id': 1, u'mass': 114.833444135, u'pos': [-1.845835908038313, 4.05667753630782, -2.527388122688016]}, {u'id': 2, u'mass': 229.145666879, u'pos': [-1.8586010532033699, 1.8054802154798875, 4.448330135901688]}, {u'id': 7, u'mass': 195.796984949, u'pos': [4.55238975968467, -3.3190959080221063, -3.927483666491501]}, {u'id': 9, u'mass': 294.279994857, u'pos': [-1.5446579193112373, -0.8405677759554253, 3.725665010593456]}]
expr = """ # order by ... etc. ...
SELECT mass(this), pos(this) FROM space
WHERE isSphere(this) and mass(this) > 50
ORDER BY mass(this) DESC
AS dict; """
prog = SelectScript.compile(expr)
ssODE.eval(prog)
[{u'mass': 294.279994857, u'pos': [-3.4853716864431576, 1.8821166424901692, 4.034112812340737]}, {u'mass': 229.145666879, u'pos': [-0.834453278226569, 3.21875048219056, 4.440455975712224]}, {u'mass': 195.796984949, u'pos': [3.3897191467065912, -2.1293321409975188, -4.274666405528011]}, {u'mass': 139.608316446, u'pos': [3.6235939935925146, -2.59280154351911, 1.8471848671204192]}, {u'mass': 114.833444135, u'pos': [-0.2690789638414155, 3.1816379330457054, -4.354704284040153]}]
expr = """ # usage of variables ...
spheres = SELECT obj FROM space WHERE isSphere(this) AS list;
maxMass = max(SELECT mass FROM spheres AS list);"""
prog = SelectScript.compile(expr)
ssODE.eval(prog)
299.505976976
expr = """ # Select 2 dimensional projections ...
Mass = SELECT mass FROM space WHERE hasBody(this)
AS plane ("XY", [-5,-5], [100,100,0.1], 3);
Velocity = SELECT linearVelocity(this, 2) FROM space
WHERE hasBody(this)
AS plane("XY", [-5,-5], [100,100,0.1], 3);
[Mass, Velocity];"""
prog = SelectScript.compile(expr)
rslt = ssODE.eval(prog)
%pylab inline
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = (10.0, 8.0)
plt.figure(1); plt.subplot(221); plt.title("mass")
plt.imshow(rslt[0].astype(numpy.float), cmap=plt.cm.gist_ncar)
plt.colorbar(); plt.subplot(222); plt.title("z-velocity")
plt.imshow(rslt[1].astype(numpy.float), cmap=plt.cm.afmhot)
plt.colorbar()
sim.stop()
Populating the interactive namespace from numpy and matplotlib
if
, else
, print
, eval
, ...import SelectScript, SelectScript.Interpreter
def move(step, towers) :
if not towers or not towers [step[0]]: pass
elif not towers[step [1]] or towers [step[1]][-1] > towers[step[0]][-1]:
# append element on top from another tower top
towers [step[1]].append(towers[step[0]].pop())
return towers
return []
ss = SelectScript.Interpreter()
ss.addFunction("move", move)
expr = """ # vanilla approach (approx. 10 sec)
moves = [[0,1], [0,2], [1,0], [1,2], [2,0], [2,1]];
SELECT m1.this, m2.this, m3.this, m4.this, m5.this, m6.this, m7.this
FROM m1=moves, m2=moves, m3=moves, m4=moves, m5=moves, m6=moves, m7=moves
WHERE [[],[],[3 ,2 ,1]] == move(m7.this,
move(m6.this,
move(m5.this,
move(m4.this,
move(m3.this,
move(m2.this,
move(m1.this, [[3,2,1],[],[]] )))))))
AS list; """
ir = SelectScript.compile( expr )
ss.eval( ir )
[[0, 2], [0, 1], [2, 1], [0, 2], [1, 0], [1, 2], [0, 2]]
SELECT ... FROM ... WHERE ...
START WITH value = ... , ...
CONNECT BY ( NO CYCLE | UNIQUE | MEMORIZE int |
MAXIMUM int )
value = func(value, ...), ...
STOP WITH value > ... OR value ...
import time
def macro_TimeIt(self, line):
t = time.time()
rslt = eval(line)
print "TimeIt: %f sec." %(time.time()-t)
return rslt
ip = get_ipython()
ip.define_magic("TimeIt", macro_TimeIt)
expr = """ # basic hierarchical query ...
SELECT this FROM moves
WHERE [[],[],[3 ,2 ,1]] == move(this, tower)
START WITH tower = [[3 ,2 ,1],[] ,[]], level = 1
CONNECT BY tower = move(this, tower), level = level +1
STOP WITH level == 7 or [] == move(this, tower)
AS list;"""
ir = SelectScript.compile( expr )
%TimeIt ss.eval( ir )
TimeIt: 0.800377 sec.
[[[0, 2], [0, 1], [2, 1], [0, 2], [1, 0], [1, 2], [0, 2]]]
expr = """ # no cycles query ...
SELECT level, this FROM moves
WHERE [[],[],[3 ,2 ,1]] == move(this, tower)
START WITH tower = [[3 ,2 ,1],[] ,[]], level = 1
CONNECT BY NO CYCLE
tower = move(this, tower), level = level +1
STOP WITH level == 7 or [] == move(this, tower)
AS list;"""
ir = SelectScript.compile( expr )
%TimeIt ss.eval( ir )
TimeIt: 0.778390 sec.
[[1, [0, 2], 2, [0, 1], 3, [2, 1], 4, [0, 2], 5, [1, 0], 6, [1, 2], 7, [0, 2]]]
ss.addFunction("str", str) # attach additional helper
expr = """ # unique results only ...
SELECT to(this, "step"+str(level)), tower FROM moves
WHERE [[],[],[3 ,2 ,1]] == move(this, tower)
START WITH tower = [[3 ,2 ,1],[] ,[]], level = 1
CONNECT BY UNIQUE
tower = move(this, tower), level = level +1
STOP WITH level == 7 or [] == move(this, tower)
AS dict;"""
ir = SelectScript.compile( expr )
%TimeIt ss.eval( ir )
TimeIt: 0.201189 sec.
[[{u'step1': [0, 2], u'tower': [[3, 2], [], [1]]}, {u'step2': [0, 1], u'tower': [[3], [2], [1]]}, {u'step3': [2, 1], u'tower': [[3], [2, 1], []]}, {u'step4': [0, 2], u'tower': [[], [2, 1], [3]]}, {u'step5': [1, 0], u'tower': [[1], [2], [3]]}, {u'step6': [1, 2], u'tower': [[1], [], [3, 2]]}, {u'step7': [0, 2], u'tower': [[], [], [3, 2, 1]]}]]
expr = """ # bidiectional graph search
SELECT this, tower FROM moves WHERE [[],[],[4,3,2,1]] == move(this, tower)
START WITH tower = [[4,3,2,1],[],[]]
CONNECT BY MEMORIZE 15
tower = move(this, tower)
STOP WITH [] == move(this, tower)
AS list;"""
ir = SelectScript.compile( expr )
%TimeIt ss.eval( ir )
TimeIt: 0.145421 sec.
[[[[0, 1], [[4, 3, 2], [1], []]], [[0, 2], [[4, 3], [1], [2]]], [[1, 2], [[4, 3], [], [2, 1]]], [[0, 1], [[4], [3], [2, 1]]], [[2, 0], [[4, 1], [3], [2]]], [[2, 1], [[4, 1], [3, 2], []]], [[0, 1], [[4], [3, 2, 1], []]], [[0, 2], [[], [3, 2, 1], [4]]], [[1, 2], [[], [3, 2], [4, 1]]], [[1, 0], [[2], [3], [4, 1]]], [[2, 0], [[2, 1], [3], [4]]], [[1, 2], [[2, 1], [], [4, 3]]], [[0, 1], [[2], [1], [4, 3]]], [[0, 2], [[], [1], [4, 3, 2]]], [[1, 2], [[], [], [4, 3, 2, 1]]]]]
import SelectScript, SelectScript_OpenRAVE.Interpreter
from openravepy import *
# load OpenRAVE
env=Environment()
env.Load('res/scene.env.xml')
env.SetViewer('qtcoin')
# load the SelectScript interpreter and attach env
ssRave = SelectScript_OpenRAVE.Interpreter()
ssRave.addEnvironment(env)
#############################################################
# helper only, to draw lines ...
graphix = []
def draw_line(line, lwd=16, color=[(1, 0, 0), (1, 0, 0)]):
global graphix, env
graphix.append(env.drawlinestrip(points=numpy.array(line),
linewidth=lwd, colors=numpy.array(color)))
draw_line([( 9.5, 7.5, 0), (10.5, 8.5, 0)])
draw_line([( 9.5, 8.5, 0), (10.5, 7.5, 0)])
draw_line([(10.0, 8.0, 0), (10.0, 8.0, 3)])
from pygame import mixer
mixer.init()
def playSound(file):
mixer.Sound(file).play()
def list_add(a,b):
if isinstance(a, list):
rslt = []
for i in range(len(a)):
rslt.append(a[i] + b[i])
return rslt
return a + b
ssRave.addFunction('playSound', playSound)
ssRave.addFunction('add', list_add)
expr = """
robot = "YouBot";
start_pos = position(robot,0,2);
target_pos = [10.0, 8.0];
directions = [[0,1],[0,-1],[1,-1],[-1,-1],[1,0],[-1,0],[-1,1],[1,1]];
SELECT (this + cur_pos) FROM directions
WHERE target_pos == move(robot, this+cur_pos)
START WITH cur_pos=start_pos, level = 1
CONNECT BY MEMORIZE 25 MAXIMUM 1000
cur_pos = move(robot, cur_pos+this),
level = level + 1
STOP WITH target_pos == move(robot, this+cur_pos) OR
distance(target_pos, this+cur_pos) > 0.5*(25-level) OR
IF( checkCollision(robot);
playSound("/usr/share/sounds/ubuntu/stereo/bell.ogg"),
True) # ; else is not required here ...
AS list; """
task = SelectScript.compile(expr)
paths = ssRave.eval( task )
# switch of the robot's yellow traces...
env.GetSensor("YouBot_trace").Configure(Sensor.ConfigureCommand.PowerOff)
env.GetSensor("YouBot_trace").Configure(Sensor.ConfigureCommand.RenderDataOff)
0
for i, path in enumerate(paths):
color = (numpy.random.uniform(), numpy.random.uniform(), numpy.random.uniform())
plist = [(3, 9, 0.002 * i)]
clist = [color]
for point in path:
plist.append((point[0][0], point[0][1], 0.002*i))
clist.append(color)
draw_line(plist, 3, clist)
time.sleep(0.01)
expr = """ SELECT this FROM environment
WHERE isKinbody(this)
AS occupancygrid(0.2, 0.2); """
task = SelectScript.compile(expr)
oMap = ssRave.eval( task )
plt.imshow(oMap.astype(numpy.float), cmap=plt.cm.gray)
<matplotlib.image.AxesImage at 0x7f12f8f97490>
graphix = [] # delete lines drawn
expr = """ cams = SELECT obj FROM environment
WHERE isSensor(this) and "cam" in id(this)
AS list; """
task = SelectScript.compile(expr)
cams = ssRave.eval( task )
for cam in cams:
cam.Configure(Sensor.ConfigureCommand.PowerOn)
cam.Configure(Sensor.ConfigureCommand.RenderDataOn)
expr = """ SELECT obj(o.this), obj(s.this)
FROM o=environment, s=environment
WHERE isSensor(s.this) AND
isKinbody(o.this) OR isRobot(o.this)
AS sensorgrid(zPos=0.2, gridSize=0.2);"""
task = SelectScript.compile(expr)
sMap = ssRave.eval( task )
plt.imshow(sMap.astype(numpy.float), cmap=plt.cm.hot)
SetTranslation -6.240000 -0.210000 0.200000 SetSize 127 124 0.200000
<matplotlib.image.AxesImage at 0x7f12ec09b410>
Contact | André Dietrich |
---|---|
mailto:dietrich@ivs.cs.uni-magdeburg.de | |
Web | http://eos.cs.ovgu.de/crew/dietrich |
Downloads | https://gitlab.com/groups/OvGU-ESS |