# Build a powerful robot simulator with OpenRave and Matlab/Octave

Matlab Jan 24, 2013

Everybody who works with robots requires a simulator from time to time. I  “discovered” to be very useful tool in this context. Thanks to Rosen  Diankov for such a great tool. It is focused on manipulators but also  supports mobile platforms. OpenRave provides programming interfaces to  C, C++, and Python. For Matlab and Octave it exists a text server. In  contrast to the other languages the last one is not very well  documented. I prepared a small example to simplify the first contact.

The small scenario integrates two walls and a differential driven robot  equipped with a laser scanner. The robot drives around based on random  velocity commands. If the laser scanner measures a distance smaller than  1 meter the application stops. The Matlab code is presented below, but  you can download the complete example with all XML files that describe  the robot and the environment.

addpath("/usr/share/openrave-0.8/octave/")

% pay attention to this line, I found different commands to switch on
% the physics engine but just 'physics ode' works fine
orEnvSetOptions('physics ode')
orEnvSetOptions('debug 0')
orEnvSetOptions('gravity 0 0 -9.8')
orEnvSetOptions('timestep 0.001')
orEnvSetOptions('simulation stop')
orEnvSetOptions('simulation start')

logid = orEnvCreateProblem('logging')
orProblemSendCommand('savescene filename myscene.env.xml',logid)

robots = orEnvGetRobots()
robotid=robots{1,1}.id

% switch on "all" sensors (at the moment we have just one)
sensor = orRobotGetAttachedSensors(robotid);
for i=0:length(sensor)
orRobotSensorConfigure(robotid, i, 'poweron');
orRobotSensorConfigure(robotid, i, 'renderdataon');
end

% we want to use the existing implementation of a differential
% driven robot (two wheels located on one axes)
success = orRobotControllerSet(robotid, 'odevelocity')
success = orRobotControllerSend(robotid, 'setvelocity 1 1')

while(1)
pause(0.5)
velocities=2*rand(1,orRobotGetActiveDOF(robotid));
success = orRobotControllerSend(robotid, ...
['setvelocity ', num2str(velocities)]);
data = orRobotSensorGetData(robotid,0);
% check the completeness of a laser measurement
if min(size(data.laserrange))~=0
% calculate the range from distance in all dimensions
range=sqrt(data.laserrange(1,:).^2+...
data.laserrange(2,:).^2+...
data.laserrange(3,:).^2);
% filtering "0" values (No idea were they come from)
range(range==0)=NaN;
min(range)
if min(range)<1.0
break
end
end
end
% switch off the robot
success = orRobotControllerSend(robotid,...
'setvelocity 0 0');
disp('Aus Maus')

The picture shows a screen shot of the running simulation. The corresponding files are available at:

Please adapt the location of the XML file in the Matlab code according  to your file system. The same has to be done in the environment file KARYON.env.xml for the robot description.