MATLAB-VREP Hokuyo Communication

Typically: "How do I... ", "How can I... " questions
prasanna.routray
Posts: 34
Joined: 20 Apr 2015, 20:11

Re: MATLAB-VREP Hokuyo Communication

Post by prasanna.routray »

'V-rep side code added'[*]

Code: Select all

local packedData=simPackFloats(laserData)
simSetStringSignal('measuredData',packedData)
***************************************************************
***************************************************************
'Matlab Side code '

Code: Select all

clc, clear all,close all
disp('Program started');
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);
dataMain=[];
try
if (clientID>-1)

disp('Connected to remote API server');
vrep.simxStartSimulation(clientID,vrep.simx_opmode_oneshot_wait);
% Initialization phase:

[err,signal]=vrep.simxGetStringSignal(clientID,'measuredData',vrep.simx_opmode_streaming);
% while we are connected:
while (vrep.simxGetConnectionId(clientID)~=-1)
[err,signal]=vrep.simxGetStringSignal(clientID,'measuredData',vrep.simx_opmode_buffer);
if (err==vrep.simx_return_ok)

for i=1:10000
data=simxUnpackFloats(signal);
dataMain=[dataMain;data];
dlmwrite('myFile.txt', dataMain);
end
end
end
vrep.simxFinish(clientID);
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;
*******************************************************************************
Thanks
*******************************************************************************

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

Re: MATLAB-VREP Hokuyo Communication

Post by coppelia »

You are doing the streaming wrong!

Have a look at following example:

Code: Select all

function simpleTest()
	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);

	if (clientID>-1)
		disp('Connected to remote API server');
			
		% Now try to retrieve data in a blocking fashion (i.e. a service call):
		[res,objs]=vrep.simxGetObjects(clientID,vrep.sim_handle_all,vrep.simx_opmode_oneshot_wait);
		if (res==vrep.simx_return_ok)
			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
			
		pause(2);
	
		% Now retrieve streaming data (i.e. in a non-blocking fashion):
		t=clock;
		startTime=t(6);
		currentTime=t(6);
		vrep.simxGetIntegerParameter(clientID,vrep.sim_intparam_mouse_x,vrep.simx_opmode_streaming); % Initialize streaming
		while (currentTime-startTime < 5)	
			[returnCode,data]=vrep.simxGetIntegerParameter(clientID,vrep.sim_intparam_mouse_x,vrep.simx_opmode_buffer); % Try to retrieve the streamed data
			if (returnCode==vrep.simx_return_ok) % After initialization of streaming, it will take a few ms before the first value arrives, so check the return code
				fprintf('Mouse position x: %d\n',data); % Mouse position x is actualized when the cursor is over V-REP's window
			end
			t=clock;
			currentTime=t(6);
		end
			
		% Now send some data to V-REP in a non-blocking fashion:
		vrep.simxAddStatusbarMessage(clientID,'Hello V-REP!',vrep.simx_opmode_oneshot);

		% Before closing the connection to V-REP, make sure that the last command sent out had time to arrive. You can guarantee this with (for example):
		vrep.simxGetPingTime(clientID);

		% Now close the connection to V-REP:	
		vrep.simxFinish(clientID);
	else
		disp('Failed connecting to remote API server');
	end
	vrep.delete(); % call the destructor!
	
	disp('Program ended');
end
Cheers

prasanna.routray
Posts: 34
Joined: 20 Apr 2015, 20:11

Re: MATLAB-VREP Hokuyo Communication

Post by prasanna.routray »

can you please specify in which step I'm doing wrong. I'm new to V-rep and LUA Script. if you can let me know specifically where I'm doing wrong It'll be bit easier for me.

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

Re: MATLAB-VREP Hokuyo Communication

Post by coppelia »

Please make sure you have read and understood this page. Then it will be easier.
Basically, it is normal the a call with simx_opmode_buffer doesn't return simx_return_ok right after streaming was initialized.

Cheers

Jay
Posts: 11
Joined: 14 Oct 2015, 16:34

Re: MATLAB-VREP Hokuyo Communication

Post by Jay »

Hi Admin,

I am trying to read all the positon data of the Center of mass (COM) of my system from vrep to MATALB. I read through all your posts in this question, and followed the solution you gave in your first post trying to do this as follows:

In vrep non-threaded side:

Code: Select all

        local c={centerOfMassOfModel[1],centerOfMassOfModel[2],centerOfMassOfModel[3]}
      --print to console window and send the COM position data to MATLAB
        print(c[1], c[2], c[3])

        local packedData=simPackFloats(c)
        local mySignal=simGetStringSignal('measuredData')
          if not mySignal then
             mySignal=''
          end
        simSetStringSignal('measuredData',mySignal..packedData)
In which c represents the COM position that is continuously calculated.

In Matlab side:

Code: Select all

% Initialization phase:
[err,signal]=vrep.simxReadStringStream(clientID,'measuredData',vrep.simx_opmode_streaming);

while (vrep.simxGetConnectionId(clientID)~=-1) 
  [err,signal]=vrep.simxReadStringStream(clientID,'measuredData',vrep.simx_opmode_buffer);  
  if (err==vrep.simx_return_ok)
       % Unpack data           
      COM = vrep.simxUnpackFloats(signal);
  end
end
The output of COM is only the last value of "c" instead the data matrix I get from the console print in vrep. I was wondering what I did wrong with this. Thank you.

Regards,
Jay

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

Re: MATLAB-VREP Hokuyo Communication

Post by coppelia »

Hello,

I strongly suggest you use the simxCallScriptFunction function, which is much less confusing that using data streams. IF you don't do thing correctly with data streams, you can lose data in-between. Also, you don't show us how you print the COM value on the Matlab side.

Cheers

Jay
Posts: 11
Joined: 14 Oct 2015, 16:34

Re: MATLAB-VREP Hokuyo Communication

Post by Jay »

coppelia wrote:Hello,

I strongly suggest you use the simxCallScriptFunction function, which is much less confusing that using data streams. IF you don't do thing correctly with data streams, you can lose data in-between. Also, you don't show us how you print the COM value on the Matlab side.

Cheers
Hi Admin,

Thank you for your reply. I tried to use simxCallScriptFunction but the return code was always 8, I didn't read any data successfully. I still prefer to use the old method, because there isn't any 'lose data in-between' problem. I would greatly appreciate if you can help me to review my code and explain why the output of COM is only the latest value of "c" instead all the data matrix I get from the console print in vrep.

Regards

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

Re: MATLAB-VREP Hokuyo Communication

Post by coppelia »

A return value of 8 (i.e. simx_return_remote_error_flag) means there was an error on the V-REP side. Check the arguments of your function and make sure your callback function is correctly defined.

If you still want to go your way, try first to always set a same signal on the V-REP side. Instead of

Code: Select all

local packedData=simPackFloats(c)
local mySignal=simGetStringSignal('measuredData')
if not mySignal then
    mySignal=''
end
simSetStringSignal('measuredData',mySignal..packedData)
try with:

Code: Select all

simSetStringSignal('measuredData',packedData)
Does that make a difference?

Post Reply