Remotely call threaded script functions

Requests or suggestions for new features
Jay
Posts: 11
Joined: 14 Oct 2015, 16:34

Remotely call threaded script functions

Postby Jay » 04 Aug 2016, 18:57

Hi,

I coded a climbing gait of a quadruped robot using IK chain and simRMLMoveToPosition in a threaded script. I am trying to remotely call the created climbing gait as a function from MATLAB.

I understand simxCallScriptFunction can only be used to Call non-threaded scripts, and I found simSetThreadIsFree can be used to declare a non-blocking section in a threaded script. I assume this means we can change functions in threaded scripts to performing as non-threaded ones by using simSetThreadIsFree.

My code structure on V-rep side is:

Code: Select all

simExtRemoteApiStart(19999,1300,false,true)

simSetThreadIsFree(1)

rearBodyClimbing=function(inInts,inFloats,inStrings,inBuffer)

  if #inInts>=1 then
--Climbing gait--
    return {},{},{'Rear body climbing finished'},' '
  end
end
simSetThreadIsFree(0)


On MATLAB side is:

Code: Select all

function rearBodyClibming()

   disp('Program started');
   % vrep=remApi('remoteApi','extApi.h'); % using the header (requires a compiler)
   vrep=remApi('remoteApi'); % using the prototype file (remoteApiProto.m)
   vrep.simxFinish(-1); % just in case, close all opened connections
   clientID=vrep.simxStart('127.0.0.1',19999,true,true,5000,5);

   if (clientID>-1)
      disp('Connected to remote API server');
      
      % 1. First send a command to display a specific message in a dialog box:
      [res retInts retFloats retStrings retBuffer]=vrep.simxCallScriptFunction(clientID,'cricketBodyDynamics',vrep.sim_scripttype_childscript,'rearBodyClimbing',[1 2 3],[],'',[],vrep.simx_opmode_blocking);
      if (res==vrep.simx_return_ok)
         fprintf('Returned message: %s\n',retStrings);
      else
         fprintf('Remote function call failed\n');
      end

        % Now close the connection to V-REP:   
%       vrep.simxFinish(clientID);
   else
      disp('Failed connecting to remote API server');
   end
%    vrep.delete(); % call the destructor!
   
%    disp('Program ended');
end


The command window shows: "Error: [string -unknown location]:?: Call failed. (simCallScriptFunctionEx on rearBodyClimbing@bodyDynamics)".

I was wondering is it possible I can remotely call functions in threaded scripts by this method? If so, could you help me to fix my problem? Thank you very much.

Regards,
Jay

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

Re: Remotely call threaded script functions

Postby coppelia » 05 Aug 2016, 11:06

Hello Jay,

actually you can call functions in a threaded child script, from a remote API client. The documentation wasn't updated for last release.
However:

When you call a script function from outside (e.g. from a remote API client), then the function call will always be blocking, i.e. V-REP will run the function, but simulation will halt until the function has finished.

So keeping in mind that the function call will block, you should use rather following scenario:

  • from the remote API client call a script function. In that script function, execute additional function calls, memorize variables, etc. But don't spend too much time in here. since during the whole time, V-REP will block. Once you are done, you can set a variable or signal.
  • Once the threaded child script resumes normally, you can check if that variable or signal is set. If yes, then you can execute a longer blocking operation, without having the simulation itself stop.

This is actually illustrated in the scene that you can find here (in next release you will find that scene in scenes/motionPlanningServerDemo.ttt), and its remote API counterpart that you can find in programming/remoteApiBindings/python/python/pathPlanningTest.py

Cheers

Jay
Posts: 11
Joined: 14 Oct 2015, 16:34

Re: Remotely call threaded script functions

Postby Jay » 08 Aug 2016, 18:58

Hi Admin,

Thank you very much for your reply.

I tried what you said "to execute additional function calls in the remote API client", but got the same "Error: [string -unknown location]:?: Call failed. (simCallScriptFunctionEx on moveBody@BodyDynamics)". I did get any data to conduct tests. And when you say "Once the threaded child script resumes normally", I wonder how can you resume it since the simulation is always blocked.

In the example you gave, 'motionPlanningServerDemo.ttt', I found the remote API function calls only call functions in the non-threaded child script, no functions in the threaded child script is called. And I didn't really run the simulation successfully.

I wonder can you provide more information about this problem. I will appreciate it a lot.

Regards,
Jay

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

Re: Remotely call threaded script functions

Postby coppelia » 09 Aug 2016, 10:51

Do following:

a) Open a new scene, and attach a threaded child script to an object (e.g. one of the lights). Rename that object to 'object'
b) Replace the script content with following:

Code: Select all

displayText_function=function(inInts,inFloats,inStrings,inBuffer)
    -- Simply display a dialog box that prints the text stored in inStrings[1]:
    if #inStrings>=1 then
        simDisplayDialog('Message from the remote API client',inStrings[1],sim_dlgstyle_ok,false)
        return {},{},{'message was displayed'},'' -- return a string
    end
end

simExtRemoteApiStart(19999)
while simGetSimulationState()~=sim_simulation_advancing_abouttostop do
    -- do something in here
    simSwitchThread()
end


c) Prepare a Matlab file with following content:

Code: Select all

function test()
   disp('Program started');
   vrep=remApi('remoteApi');
   vrep.simxFinish(-1);
   clientID=vrep.simxStart('127.0.0.1',19999,true,true,5000,5);

   if (clientID>-1)
      disp('Connected to remote API server');
      
      % Send a command to display a specific message in a dialog box:
      [res retInts retFloats retStrings retBuffer]=vrep.simxCallScriptFunction(clientID,'object',vrep.sim_scripttype_childscript,'displayText_function',[],[],'Hello world!',[],vrep.simx_opmode_blocking);
      if (res==vrep.simx_return_ok)
         fprintf('Returned message: %s\n',retStrings);
      else
         fprintf('Remote function call failed\n');
      end

      vrep.simxFinish(clientID);
   else
      disp('Failed connecting to remote API server');
   end
   vrep.delete();
   disp('Program ended');
end


d) start the V-REP simulation
e) in Matlab, run your program test. --> a message will display in V-REP.

Cheers


Return to “Feature requests”

Who is online

Users browsing this forum: No registered users and 2 guests