Page 1 of 1

Best practices for loading model to a specific coordinate in a running simulation

Posted: 16 Apr 2018, 10:13
by Rex
Hi, may I know what are the best practices for loading a model to a specific coordinate in a scene while a simulation is actively running?

I'm currently using the simxLoadModel C++ remote api to load multiple quadricopter models in a 25x25m V-REP scene.

As they are all loaded at the center of the floor, I would then invoke the following lua function using simxCallScriptFunction twice to move it to the desired starting point. The first API call positions the quadricopter's target orb and the second API call positions the quadricopter itself.

Code: Select all

function SetRobotPose_function(inInts,inFloats,inStrings,inBuffer)

    -- inInts[2]: set object position if = 1
    -- inFloats[1]: xcoordinate
    -- inFloats[2]: ycoordinate
    -- inFloats[3]: zcoordinate

    -- inInts[3]: set object orientation when non-zero
    -- inFloats[4]: xorientation
    -- inFloats[5]: yorientation
    -- inFloats[6]: zorientation

    local functionCode

    --Get Model Base Handle
    local ModelBaseHandle = inInts[1]

    --Get initial configuration of the model
    local initConf=simGetConfigurationTree(ModelBaseHandle)

    --Dynamic reset the model
    local objects=simGetObjectsInTree(ModelBaseHandle,sim_handle_all,0)
    for i=1,#objects,1 do
        functionCode = simResetDynamicObject(objects[i])
        if functionCode==-1 then
            simDisplayDialog('From remoteAPICommandServer', 'Failed to ResetDynamicObject...',sim_dlgstyle_ok,false)
            return {functionCode},{},{},''
        end
    end

    --Restore configuration of model
    simSetConfigurationTree(initConf)

    --if position values are available
    if #inInts>=2 then
        if inInts[2] == 1 then
        local SetPosition = {inFloats[1], inFloats[2], inFloats[3]}
            simSetObjectPosition(ModelBaseHandle,-1, SetPosition)
        end
    end

    --if orientation values are available
    if #inInts>=3 then
        if inInts[3] == 1 then
        local SetOrientation = {inFloats[4], inFloats[5], inFloats[6]}
            simSetObjectOrientation(ModelBaseHandle,-1, SetOrientation)         
        end
    end

    --return function code
    return {functionCode},{},{},''

end
Occasionally, the quadricopter can be seen going out of control and then explode. The target orb will behave correctly and stay at the position.

Here's a sample video:
https://drive.google.com/file/d/1T_t9OH ... ziYyWzYEaN

Thanks!

Re: Best practices for loading model to a specific coordinate in a running simulation

Posted: 17 Apr 2018, 17:14
by coppelia
Hello,

Did you check the forum posts? The reason is that you normally can't simply move a dynamically enabled object instantaneously, without generating infinite forces with objects it links. This is the cause of an explosion. Read more here for instance.

In your case, it would be enough to move the model to its new position just after loading it, in exactly the same simulation step. You can do this directly from a remote API client, unless you run in synchronous mode. Or, you simply use simxCallScriptFunction: in the called script function, you can execute the two commands at once (i.e. in the same simulation step)

Cheers

Re: Best practices for loading model to a specific coordinate in a running simulation

Posted: 19 Apr 2018, 05:20
by Rex
Thanks, I will try the regular api call route through simxcallscriptfunction.