Best strategy to get laser range finder data via remote API

Typically: "How do I... ", "How can I... " questions
Post Reply
rico
Posts: 6
Joined: 07 Jun 2013, 19:50

Best strategy to get laser range finder data via remote API

Post by rico »

Hello,

I would like to know what is the best strategy to get the URG laser range finder data of my simulation, from an application in C++ via remote API.

Thanks in advance

Rico

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

Re: Best strategy to get laser range finder data via remote

Post by coppelia »

Hi Rico,

The easiest is following:
  • In the child script that reads the various points, pack the points into a string signal (needs to be done at each simulation pass):

Code: Select all

data=simPackFloats(points) -- points is a table that contains all scanned points (3 values per point)
simSetStringSignal("ScannerData",data)

Code: Select all

// Streaming initialization:
simxChar* laserScannerData;
simxInt dataSize;
simxGetStringSignal(clientID,"ScannerData",&laserScannerData,&dataSize,simx_opmode_streaming);

// Then everytime you want to read fresh values:
if (simxGetStringSignal(clientID,"ScannerData",&laserScannerData,&dataSize,simx_opmode_buffer)==simx_error_noerror)
{ // until you can read the first set of values, you might have to wait a few ms (until streaming started)
	// laserScannerData points onto a buffer of chars (dataSize chars). Read the data with:
	for (int i=0;i<dataSize/(4*3);i++) // if each point has 3 coordinates
	{
		float x=((simxFloat*)(laserScannerData+4*3*i))[0];
		float y=((simxFloat*)(laserScannerData+4*3*i))[1];
		float z=((simxFloat*)(laserScannerData+4*3*i))[2];
	}
}
Above code was not tested, but something along those lines!

Cheers

rico
Posts: 6
Joined: 07 Jun 2013, 19:50

Re: Best strategy to get laser range finder data via remote

Post by rico »

Wow! thank you for your very fast answer!

One more question about the code C++ application side: when I read the laser data, do I get all the laser data since last reading or only the latest ones?

Thank you

Rico

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

Re: Best strategy to get laser range finder data via remote

Post by coppelia »

In the example above, you always read the last data (i.e. data is not accumulated on the client side). If you wish to be able to read all data, without losing any of it, do like following:

Code: Select all

data=simPackFloats(points) -- points is a table that contains all scanned points (3 values per point)
oldData=simGetStringSignal("ScannerData")
if not oldData then
    oldData=""
end
simSetStringSignal("ScannerData",oldData..data) -- concatenate the old and new string
  • on the remote API client side:

Code: Select all

// Streaming initialization:
simxChar* laserScannerData;
simxInt dataSize;
simxGetAndClearStringSignal(clientID,"ScannerData",&laserScannerData,&dataSize,simx_opmode_streaming);

// Then everytime you want to read fresh values:
if (simxGetAndClearStringSignal(clientID,"ScannerData",&laserScannerData,&dataSize,simx_opmode_buffer)==simx_error_noerror)
{ 
    // Read the data here. You need to know how large the data of one scan is to correctly interpret the data
	
	// Now enable streaming again (was automatically disabled with the positive event):
    simxGetAndClearStringSignal(clientID,"ScannerData",&laserScannerData,&dataSize,simx_opmode_streaming);	
}
Make sure you understand how simxGetAndClearStringSignal operates.

Cheers

Post Reply