simxCallScriptFunction received corrupted object handle

Typically: "How do I... ", "How can I... " questions
Post Reply
Rex
Posts: 15
Joined: 22 Jan 2018, 07:45

simxCallScriptFunction received corrupted object handle

Post by Rex » 16 Apr 2018, 05:07

While calling the remote API simxCallScriptFunction from a C++ program to create pure shapes in VREP, the object handle getting returned to the C++ program is occasionally different from the actual shape's handle.

The shape handle created by the lua script in VREP was 65, but 1987208531 was returned to the C++ program.

For shapes with handles 64 and 66, the handle returned to the C++ program was correct. They were called through the same C++ code in a for loop.

Can someone kindly shed some light on why this is happening? Thanks.

Here's the API call code I used:

Code: Select all

int Sim::CallScriptFunction(const char* functionName, int inIntCnt, const int* inInt, int inFloatCnt, const float* inFloat, int inStringCnt, const char* inString, int inBufferSize, const unsigned char* inBuffer, int* outIntCnt, int** outInt, int* outFloatCnt, float** outFloat, int* outStringCnt, char** outString, int* outBufferSize, unsigned char** outBuffer){

	std::mutex mtx;
	mtx.lock();

	const char* scriptDescription = "remoteAPICommandServer";
	int scriptHandleOrType = sim_scripttype_childscript;
	int operationMode = simx_opmode_blocking;
	
	int functionCode = 0;
	int timeoutcounter = 0;
	do{
		functionCode=simxCallScriptFunction(clientID, scriptDescription, scriptHandleOrType, functionName, inIntCnt, inInt, inFloatCnt, inFloat, inStringCnt, inString, inBufferSize, inBuffer, outIntCnt, outInt, outFloatCnt, outFloat, outStringCnt, outString, outBufferSize, outBuffer,operationMode);
		cout << "simxCallScriptFunction return code: " << functionCode << endl;
		if(functionCode&1<<0){timeoutcounter++; usleep(200);}
	}while(functionCode&1<<0 && timeoutcounter<=timeout);

	usleep(5000);

	mtx.unlock();

	return functionCode;
}
Here's the lua function used:

Code: Select all

function CreatePureShape_function(inInts,inFloats,inStrings,inBuffer)
    -- -- take note that lua array indexes start at 1!

    -- -- inInts[1]: primitiveType
    -- -- inInts[2]: shapeoptions

    -- -- inFloats[1]: shape x dimension
    -- -- inFloats[2]: shape y dimension
    -- -- inFloats[3]: shape z dimension

    -- -- inFloats[4]: shape x position
    -- -- inFloats[5]: shape y position
    -- -- inFloats[6]: shape z position

    -- -- inFloats[7]: shape's alpha orientation
    -- -- inFloats[8]: shape's beta orientation
    -- -- inFloats[9]: shape's gamma orientation

    local primitiveType = inInts[1]
    local shapeoptions = inInts[2]

    local sizes = {inFloats[1], inFloats[2], inFloats[3]}
    local position = {inFloats[4], inFloats[5], inFloats[6]}
    local orientation = {inFloats[7], inFloats[8], inFloats[9]}
    --calculate mass based on density of 1000kg/m^3
    local mass = sizes[1]*sizes[2]*sizes[3]*1000

    local shapehandle

    local functionCode

    sim.addStatusbarMessage("Creating cuboid")

    --create the shape
    shapehandle = simCreatePureShape(primitiveType, shapeoptions, sizes, mass, nil)

    shapename = simGetObjectName(shapehandle)
    sim.addStatusbarMessage(shapename)

    --print orientation of shape to be set
    sim.addStatusbarMessage("Dimensions:")
    sim.addStatusbarMessage(sizes[1])
    sim.addStatusbarMessage(sizes[2])
    sim.addStatusbarMessage(sizes[3]) 


    --return error code if shape was not successfully created
    if shapehandle == -1 then
        sim.addStatusbarMessage("Could not create shape...")
        return {-1, shapehandle},{},{},''
    end
    
    sim.addStatusbarMessage("Shape Handle:")
    sim.addStatusbarMessage(shapehandle)

    -- sleep(3)

    sim.addStatusbarMessage("setting shape position")

    --print position of shape to be set
    sim.addStatusbarMessage(position[1])
    sim.addStatusbarMessage(position[2])
    sim.addStatusbarMessage(position[3])

    --set position the shape
    functionCode = simSetObjectPosition(shapehandle, -1, position)

    if functionCode >1 then
        sim.addStatusbarMessage("Failed to position shape...")
    end

    sim.addStatusbarMessage("setting shape orientation")

    --print orientation of shape to be set
    sim.addStatusbarMessage(orientation[1])
    sim.addStatusbarMessage(orientation[2])
    sim.addStatusbarMessage(orientation[3])

    --orientate the shape
    functionCode = simSetObjectOrientation(shapehandle, -1, orientation)
    
    if functionCode >1 then
        sim.addStatusbarMessage("Failed to orientate shape...")
        sim.addStatusbarMessage(functionCode)
    end

    return {0, shapehandle},{},{shapename},''
end
Image
VREP console
Image
C++ program printout

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

Re: simxCallScriptFunction received corrupted object handle

Post by coppelia » 16 Apr 2018, 07:36

Hello,

please try following and tell me if it works.

Non-threaded child script attached to object dummy:

Code: Select all

function test_function(inInts,inFloats,inStrings,inBuffer)
    cnt=cnt+1
    return {0,cnt},{},{'hello'..cnt},''
end

function sysCall_init()
    cnt=-1
end 
C/C++ client side:

Code: Select all

#include <stdio.h>
#include <stdlib.h>

extern "C" {
    #include "extApi.h"
}

int main(int argc,char* argv[])
{
    int clientID=simxStart((simxChar*)"127.0.0.1",19997,true,true,2000,5);
    if (clientID!=-1)
    {
        printf("Connected to remote API server\n");
        simxStartSimulation(clientID,simx_opmode_blocking);
        int retIntCnt;
        int* retInts;
        int retStrCnt;
        char* retStrs;
        for (size_t i=0;i<100;i++)
        {
            int result=simxCallScriptFunction(clientID,"dummy",sim_scripttype_childscript,"test_function",0,NULL,0,NULL,0,NULL,0,NULL,&retIntCnt,&retInts,NULL,NULL,&retStrCnt,&retStrs,NULL,NULL,simx_opmode_blocking);
            if (result==simx_return_ok)
                printf("RetVal: %i, retInt: %i, retString: %s\n",result,retInts[1],retStrs);
            else
                printf("Remote function call failed\n");
        }
        simxFinish(clientID);
    }
    return(0);
}
Cheers

Rex
Posts: 15
Joined: 22 Jan 2018, 07:45

Re: simxCallScriptFunction received corrupted object handle

Post by Rex » 16 Apr 2018, 09:11

Hi, thanks for your help. I've tried your code and the returned data was all correct.

Post Reply