How to pass a vector of path nodes?

Typically: "How do I... ", "How can I... " questions
Post Reply
engharat
Posts: 7
Joined: 18 Feb 2013, 12:22

How to pass a vector of path nodes?

Post by engharat » 08 May 2013, 07:15

Hi,
I've two main questions: my main goal is to pass a vector of path nodes(so a 2xN or Nx2 matrix) from Matlab to Vrep trough RemoteAPI calls. Then I'll build a path in Vrep that will be followed by a robot.
The main problem is that I don't find a good method to pass this 2xN matrix, with N variable. Shoud I loop over all the numbers and pass them as floatSignal,one by one?

And then, when I retrieve those points in Vrep, what is the "default code" to build a path that uses this nodes as control points, without caring of speed?(for example a default constant speed for all the path)

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

Re: How to pass a vector of path nodes?

Post by coppelia » 08 May 2013, 20:49

Hello,

Try using the remote API function simxAppendStringSignal on the client side. Here a simple C/C++ code example:

Code: Select all

std::vector<float> matrixData;
// suppose you have a matrix in there
simxAppendStringSignal(clientID,'stringSignalName',(char*)&matrixData[0],matrixData.size()*4,simx_opmode_oneshot);
Then in a non-threaded child script on V-REP:

Code: Select all

signal=simGetStringSignal('stringSignalName')
simClearStringSignal('stringSignalName')
if (signal) then
    data=simUnpackFloats(signal)
    -- you have all the values in data now
end
To build a path from a collection of points:

Code: Select all

handle=simCreatePath(-1)
simInsertPathCtrlPoints(handle,0,0,(#thePoints)/11,thePoints)
For each point, there are 11 vales required. Refer to the simInsertPathCtrlPoints function for details.

Cheers

engharat
Posts: 7
Joined: 18 Feb 2013, 12:22

Re: How to pass a vector of path nodes?

Post by engharat » 13 May 2013, 10:12

Very thanks for the help. I'm now just stuck at an incredible easy problem. A simple code like that:

Code: Select all

	A={1.0,2.3}
    data=simPackFloats(A)
    simAuxiliaryConsolePrint(consoleHandle,data)
don't print anything in the console, while

Code: Select all

	A={2,5}
    data=simPackInts(A)
    simAuxiliaryConsolePrint(consoleHandle,data)
prints "STX" (without quotes) in the console window.
I'm testing this code into quadrirotor script( so a non-threaded child script)

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

Re: How to pass a vector of path nodes?

Post by coppelia » 13 May 2013, 11:31

Hello,

Your data is actually a string that may contain any values (e.g. embedded zeros, etc.). A packed table should be seen as a succession of byte values, and you cannot print that string (well, you can, but the output doesn't make sense). A packed table is a convenient way to send data to other entities (e.g. remote API clients, etc.).

If you want to print that data, unpack the string, then use something like:

Code: Select all

unpackedFloats=simUnpackFloats(packedFloats)
for i=1,#unpackedFloats,1 do
    simAuxiliaryConsolePrint(consoleHandle,unpackedFloats..',')
end
simAuxiliaryConsolePrint(consoleHandle,'\n')
Cheers

engharat
Posts: 7
Joined: 18 Feb 2013, 12:22

Re: How to pass a vector of path nodes?

Post by engharat » 13 May 2013, 12:57

coppelia wrote:Hello,

Your data is actually a string that may contain any values (e.g. embedded zeros, etc.). A packed table should be seen as a succession of byte values, and you cannot print that string (well, you can, but the output doesn't make sense). A packed table is a convenient way to send data to other entities (e.g. remote API clients, etc.).
...
Cheers
Well, but I need to know the packaging format that vrep expect from me,otherwise I cannot make a matlab function to pack matrix. Matlab num2str function(both %f and %e option) doesn't seems to work, because when I unpack the data into vrep I get wrong numbers.
Thanks for any help :)

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

Re: How to pass a vector of path nodes?

Post by coppelia » 14 May 2013, 10:27

You are right,

The packaging format is little endian. Basically every 4 chars in the string can represent either a 32-bit signed integer value, or a 32-bit floating point value. I am sure there is a conversion in Matlab. Let me know, otherwise we might add a convenience function in the remote API.

However, you are also free to pack your data in a different way on one side and the other.

Cheers

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

Re: How to pass a vector of path nodes?

Post by coppelia » 21 May 2013, 17:08

As a heads-up, next release (V3.0.4) will have 4 convenience functions for that. Here they are:

Code: Select all

function [string]= simxPackInts(obj,intArray)
	string=char(typecast(int32(intArray),'uint8'));
end

function [intArray]= simxUnpackInts(obj,string)
	intArray=typecast(uint8(int32(string)),'int32');
end

function [string]= simxPackFloats(obj,floatArray)
	string=char(typecast(single(floatArray),'uint8'));
end

function [floatArray]= simxUnpackFloats(obj,string)
	floatArray=typecast(uint8(single(string)),'single');
end
Cheers

Post Reply