How can I call via MATLAB the function sysCall_threadmain?

Typically: "How do I... ", "How can I... " questions
Post Reply
elizakh
Posts: 9
Joined: 20 Feb 2021, 13:27

How can I call via MATLAB the function sysCall_threadmain?

Post by elizakh »

Hi everyone,

I want to call via MATLAB the main function of the threaded script, but I don't know if it's possible to do that, or I have to create another function for do that.

I want to send two values (velocity and angle) via MATLAB to a function that an Asti robot throws a ball. So I want that Asti wait to receive that values and then throws the ball with the angle and the velocity that receives from MATLAB.

I tried with a non-threaded script but I think it doesn't work.

Thanks in advice

Cheers!

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

Re: How can I call via MATLAB the function sysCall_threadmain?

Post by coppelia »

Hello,

how are you trying to call a script function?

You cannot call system callback functions like sysCall_threadMain and similar. You'll have to create a new script function that you then call from your external application via simxCallScriptFunction.

Maybe something like:

Code: Select all

function myThread()
    while true do
        if receivedPosition then
            -- do something with the received position data:
            
            ...
            
            receivedPosition=nil
        else
            sim.switchThread() -- don't waste time looping several times in the same sim step
        end
    end
end

function setData(inInts,inFloats,inStrings,inBuffer)
    -- e.g. a position data:
    receivedPosition=inFloats
    return {},{},{},''
end
Cheers

elizakh
Posts: 9
Joined: 20 Feb 2021, 13:27

Re: How can I call via MATLAB the function sysCall_threadmain?

Post by elizakh »

Hi,
Thanks for your response.


I've these script on MATLAB:

Code: Select all

vel = optimizableVariable('x',[0 1]);
ang = optimizableVariable('y',[0 90]);
vars = [X1,X2];

results = bayesopt(@myrastrig,vars)


function fval = myrastrig(in)
x(1) = in.x;
x(2) = in.y;
fval = simxCallScriptFunction....
end
So in fval I've to call the function that Asti robot throws a ball in CoppeliaSim and optimizate the velocity and the angle.

In Coppelia I've these script:

Code: Select all

function sysCall_init()
    -- do some initialization here
    simRemoteApi.start(19999)
    path=sim.getObjectHandle('Path')
    ball=sim.getObjectHandle('ball')
  pathdummy=sim.getObjectHandle('Pathdummy')
    
end

function sysCall_actuation()
    sim.setPathTargetNominalVelocity(path,0.3)
    sim.setObjectOrientation(path,pathdummy,{0,0,90*3.14/180})
    simHandlePath(sim.handle_all_except_explicit,sim.getSimulationTimeStep())
    pos=sim.getPathPosition(path)/sim.getPathLength(path)
    if (pos==1) then
        sim.setObjectParent(ball,-1,true)
    end

end

function sysCall_sensing()
    -- put your sensing code here
end

function sysCall_cleanup()
    -- do some clean-up here
end
But I don't know how can I do the action of throwing a ball in a function different of sysCall_actuation().


Thanks in advance

Best regards!

elizakh
Posts: 9
Joined: 20 Feb 2021, 13:27

Re: How can I call via MATLAB the function sysCall_threadmain?

Post by elizakh »

And I've another question.
When I throw the ball, the ball never stops and I tried to add friction to the ball but It stills running until the floor finishes and It falls.

How can I fix that ?


Thanks in advance!

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

Re: How can I call via MATLAB the function sysCall_threadmain?

Post by coppelia »

You have several ways to execute some code in CoppeliaSim. The code can be executed in non-threaded mode, or in threaded mode. But if an action is triggered from an external application you can do something like (e.g. in a non-threaded script):

Code: Select all

function sysCall_init()
    ....
end

function sysCall_actuation()
    if dataFromExternalApp then
        -- do something with the data received from your external application
        ...
        
        dataFromExternalApp=nil
    end
end

function callMeFromAndExternalApplication(inInts,inFloats,inStrings,inBuffer)
    dataFromExternalApp={ints=inInts,floats=inFloats,strings=inStrings,buffer=inBuffer}
    return {},{},{},''
end
here the same but in a threaded script:

Code: Select all

function sysCall_init()
    corout=coroutine.create(coroutineMain)
end

function sysCall_actuation()
    if coroutine.status(corout)~='dead' then
        local ok,errorMsg=coroutine.resume(corout)
        if errorMsg then
            error(debug.traceback(corout,errorMsg),2)
        end
    end
end

function coroutineMain()
    while true do
        if dataFromExternalApp then
            -- do something with the data received from your external application
            ...
            
            dataFromExternalApp=nil
        end
        sim.switchThread() -- resume in next simulation step
    end
end

function callMeFromAndExternalApplication(inInts,inFloats,inStrings,inBuffer)
    dataFromExternalApp={ints=inInts,floats=inFloats,strings=inStrings,buffer=inBuffer}
    return {},{},{},''
end
and from your external application (e.g. legacy remote API client), simply call simxCallScriptFunction.

About the rolling ball... you can add drag to your ball. In its material dialog, look at linear/angular damping, or similar settings.

Note: above notation for threaded scripts is available starting with CoppeliaSim V4.2.0, available now for download

Cheers

elizakh
Posts: 9
Joined: 20 Feb 2021, 13:27

Re: How can I call via MATLAB the function sysCall_threadmain?

Post by elizakh »

Hi,

First of all, thank you for the response!

So, if I understood well,
· it's impossible to execute an action in a secondary function (It has to be executed always in the main?)
·Also, it's impossible to call directly a function where is the action, (so I've to do it like in your examples)?

Thank you in advance

Cheers!

Post Reply