VREP and MATLAB

Typically: "How do I... ", "How can I... " questions
Post Reply
formica
Posts: 52
Joined: 13 Mar 2013, 12:13

VREP and MATLAB

Post by formica » 18 Mar 2014, 12:29

I Marc,
I'm trying make a communication between Matlab R2013B and Vrep 3.1 in Ubuntu Linux (64bit).
In my workspace there are these few files:

Code: Select all

-rwxrwxr-- 1 formica formica 69203 mar 18 10:52 remApi.m
-rwxrwxr-- 1 formica formica 32868 mar 18 10:52 remoteApiProto.m
-rwxrwxr-- 1 formica formica 93878 mar 18 11:00 remoteApi.so
-rwxrwxr-- 1 formica formica  1997 mar 18 12:03 simpleTest.m
The remoteApi.so is the 64bit version as well.

Starting VREP and launching simpleTest.m I get clientID = -1 and the communication fails.
If I change the port number to 11311 (instead of 19999) I get clientID = 0 but the script does not terminate, obtaining this error
Connected to remote API server
Remote API function call returned with error code: 3
What I'm doing wrong?

Thanks in advance
Roberto

coppelia
Site Admin
Posts: 7441
Joined: 14 Dec 2012, 00:25

Re: VREP and MATLAB

Post by coppelia » 18 Mar 2014, 21:41

Hello Roberto,

You will not be able to connect from a remote API client, if the server side is not running. You can write a small non-threaded child script that will, at simulation start, start a remote API server service (with simExtRemoteApiStart). Or you could connect to port 19997, which is started by default when V-REP launches.

If you are running the 64bit version of Matlab, make sure to use the updated remote API code (which will be available in next release 3.1.1).

Cheers

formica
Posts: 52
Joined: 13 Mar 2013, 12:13

Re: VREP and MATLAB

Post by formica » 24 Mar 2014, 18:58

Hi coppelia, I'm trying to control an object (is a simple cube called QuadKinect, but is not the quadcopter model of VREP) using MATLAB. I retrieve the handles but I can't move the object.
Any idea?

Code: Select all

disp('Program started');
% vrep=remApi('remoteApi','extApi.h'); % using the header (requires a compiler)
vrep=remApi('remoteApi'); % using the prototype file (remoteApiProto.m)
vrep.simxFinish(-1); % just in case, close all opened connections
clientID=vrep.simxStart('127.0.0.1',19999,true,true,5000,5); % 19999 for a test
try
	if (clientID>-1)
		disp('Connected to remote API server');
		[res,objs]=vrep.simxGetObjects(clientID,vrep.sim_handle_all,vrep.simx_opmode_oneshot_wait);
		if (res==vrep.simx_error_noerror)
			fprintf('Number of objects in the scene: %d\n',length(objs));
		else
			fprintf('Remote API function call returned with error code: %d\n',res);
        end
        [errorCode,QuadHandle] = vrep.simxGetObjectHandle(clientID,'QuadKinect',vrep.simx_opmode_oneshot_wait);
	    [errorCode, KinectHandle]=vrep.simxGetObjectHandle(clientID,'kinect_visionSensor',vrep.simx_opmode_oneshot_wait);
        pause(1);
        errorCode = vrep.simxSetObjectPosition(clientID,QuadHandle,-1,[0,0,10], vrep.simx_opmode_oneshot);
            
  		vrep.simxFinish(clientID);
		else
			disp('Failed connecting to remote API server');
end
    
    %[errorCode,resolution,buffer]=simxGetVisionSensorDepthBuffer2(clientID,sensorHandle,operationMode)
    
        
        
	vrep.delete(); % call the destructor!
catch err
	vrep.simxFinish(clientID); % close the line if still open
	vrep.delete(); % call the destructor!
end;
	
disp('Program ended');

coppelia
Site Admin
Posts: 7441
Joined: 14 Dec 2012, 00:25

Re: VREP and MATLAB

Post by coppelia » 25 Mar 2014, 10:48

you are calling simxFinish just after sending out a command: very probably the command never gets sent/received (it needs some travel time over the network until the command has arrived in V-REP). Remember that simx_opmode_oneshot is non-blocking, meaning that a command will be sent (or prepared to be sent), but the function returns immediately, not waiting for any reply from the server (V-REP).

Cheers

enddl22
Posts: 5
Joined: 05 May 2015, 13:05

Re: VREP and MATLAB

Post by enddl22 » 05 May 2015, 13:55

Hello, Coppelia robotics
I am using remote API within Ubuntu 14.04.02 (64bits) with MATLAB2015a and VREP 3.2.0(rev6).
When I run simpleTest.m, it looks fine.
However, I faced the following error after running "test.m" file (not always.. it happens quite often.) and MATLAB got stuck.
I have to reboot PC and it works the first time but keep spitting the following error.
It would be very helpful if you could give some insight of VREP and MATLAB interface.
Best regards,
Inkyu

Updates: The following codes work fine in MATLAB2015a and VREP 3.2.0(rev6) within MACOS (Yosemite 10.10.3).
The above symtoms only happen in Ubuntu.

Code: Select all

>> simpleTest
Program started
Running Matlab glnxa64
Note: always make sure you use the corresponding remoteApi library
(i.e. 32bit Matlab will not work with 64bit remoteApi, and vice-versa)
Connected to remote API server
Remote API function call returned with error code: 3
test.m

Code: Select all

clear all;
addpath(genpath('../MATLAB_VREP_MATLAB_multiple_goals_0004'));
vrep=remApi('remoteApi'); % using the prototype file (remoteApiProto.m)
vrep.simxFinish(-1); % just in case, close all opened connections
clientID=vrep.simxStart('127.0.0.1',19997,true,true,5000,5);
try
	if (clientID>-1)
	disp('Connected to remote API server');
        disp('Starting simulation');
        returnCode=vrep.simxStartSimulation(clientID,vrep.simx_opmode_oneshot);
        pause(2);
        if(returnCode==0 || returnCode==1)
            init_pos=[-0.9852,1.0174,1.2453];
            sendPositionCmd(clientID,vrep,init_pos);
            pause(13);   
            testPose=-0.1+0.1*rand(10,3);
            for i=1:size(testPose,1)
                sendPositionCmd(clientID,vrep,testPose(i,:));
                pause(1)
            end
	    returnCode=vrep.simxStopSimulation(clientID,vrep.simx_opmode_oneshot);
            pause(0.5);
            vrep.simxFinish(clientID); % close the line if still open
            pause(0.5);
	else
		disp('Failed connecting to remote API server');
	end
	vrep.delete(); % call the destructor!
catch err
	vrep.simxFinish(clientID); % close the line if still open
	vrep.delete(); % call the destructor!
end;
vrep.simxFinish(clientID); % close the line if still open
vrep.delete(); % call the destructor!
disp('Program ended');
sendPositionCmd.m

Code: Select all

    positionString=vrep.simxPackFloats(position);
    errorCode=vrep.simxSetStringSignal(clientID,'PositionEndeffector',positionString,vrep.simx_opmode_oneshot);
    pause(0.5);

coppelia
Site Admin
Posts: 7441
Joined: 14 Dec 2012, 00:25

Re: VREP and MATLAB

Post by coppelia » 06 May 2015, 07:52

Hello,

when using the operation mode simx_opmode_oneshot, then no data is returned. The return code indicates how things went (it is bit-coded). Bit0 (i.e. 1) means simx_return_novalue_flag, which is correct. Bit1 (i.e. 2) means simx_return_timeout_flag which is surprizing, since simx_opmode_oneshot is non-blocking.

If you want to be sure a command has been received, or if you need the return value of the function call, then use simx_opmode_oneshot_wait instead. Or a streaming mode, if you continuously need to retrieve specific values.

Cheers

Post Reply