How to get handles of object subcomponents when multiple object copies exist

Typically: "How do I... ", "How can I... " questions
Post Reply
lpitonakova
Posts: 17
Joined: 22 Nov 2017, 12:32

How to get handles of object subcomponents when multiple object copies exist

Post by lpitonakova » 09 Jan 2018, 17:34

I have a scene with multiple robots of the same type. In the robot model, there are two joints: leftMotor and rightMotor. When I paste the robot into the scene, a myRobot instance is created that has leftMotor and rightMotor. However, when I paste another instance of the model in, myRobot#0 is created with components leftMotor#0 and rightMotor#0.

I am writing RemoteAPI code that could get handles of a robot's joints. All the program knows is the robot scene object name (myRobot or myRobot#0) and that the original robot model has components leftMotor and rightMotor. What I am trying to do is get handle to leftMotor when the code tries to access component of myRobot and get a handle for leftMotor#0 when the program accesses component of myRobot#0. But, I cannot rely on the number part (#0) from the robot name, because the robot name may be arbitrary.

If I knew the exact component names, the matter would be simple, I would call simXGetObjectHandle() with the exact joint name. However, what I need the program to do is to get me a component that:
1. starts with "leftMotor"
2. is a child of the correct robot model, say myRobot#0

Is there a way of doing this easily via the RemoteAPI? Or do I need to write a custom function to search through the robot's children, look at their names, etc.?

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

Re: How to get handles of object subcomponents when multiple object copies exist

Post by coppelia » 15 Jan 2018, 18:09

Hello,

the best would be to have a child script attached to the base of the robot model (probably you already have such a child script attached). Then add a function to that child script that will return the desired handles to the caller. e.g. something like:

Code: Select all

function getMotorHandles(inInts,inFloats,inStrings,inBuffer)
	return {simGetObjectHandle('leftMotor'),simGetObjectHandle('rightMotor')},{},{},''
end
Then call the function from your remote API client with simxCallScriptFunction.

Cheers

lpitonakova
Posts: 17
Joined: 22 Nov 2017, 12:32

Re: How to get handles of object subcomponents when multiple object copies exist

Post by lpitonakova » 15 Jan 2018, 18:32

Thanks.

I think I found a slighly more general solution, where only a single script has to exist in the whole scene, with a function that can be called via simxCallScriptFunction:

Code: Select all

--
-- Returns handle of an object that is named like inStrings and is a child of inInts[1]
-- where inInts[1] is a handle of the parent object.
-- E.g. it is possible to search for "leftMotor#0" by settngs inStrings = "leftMotor"
--
rneGetChildHandle=function(inInts,inFloats,inStrings,inBuffer)
    if #inStrings == 1 and #inInts == 1 then
        local targetChildName = inStrings[1];
        local parentHandle = inInts[1];
        simAddStatusbarMessage("Looing for " .. targetChildName .. " in " .. parentHandle);
        
        -- iterate through children of the parent object and look for a child with the correct name
        children=simGetObjectsInTree(parentHandle,sim_handle_all,1);
        simAddStatusbarMessage('RES' .. children[1]);
        
        local childName;
        for k, v in pairs(children) do
            childName = simGetObjectName(children[k]);
            childNameWithoutSuffix = childName:gsub("(.*)#.*$","%1");
            simAddStatusbarMessage("checking " .. childName .. "(" .. childNameWithoutSuffix .. ") against " .. targetChildName);
            if (childNameWithoutSuffix == targetChildName) then
                return {children[k]},{},{},''
            end
        end
    else
        simAddStatusbarMessage("[rneGetChildHandle] [!!! ERROR !!!] Incorrect arguments. Expects 1 int (parent object handle) and 1 string (child object name)");
        simAddStatusbarMessage(" Got " .. #inInts .. " ints and " .. #inStrings .. " strings");
    end
    
    -- if the function got to this point, the child was not found
    return {-1},{},{},''

end

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

Re: How to get handles of object subcomponents when multiple object copies exist

Post by coppelia » 15 Jan 2018, 18:43

Oh yes, that can be even better!

Cheers

Post Reply