Remotely call threaded script functions

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

Remotely call threaded script functions

Post by Jay »

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: 10337
Joined: 14 Dec 2012, 00:25

Re: Remotely call threaded script functions

Post by coppelia »

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

Post by Jay »

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: 10337
Joined: 14 Dec 2012, 00:25

Re: Remotely call threaded script functions

Post by coppelia »

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

Lil_macha
Posts: 6
Joined: 03 Mar 2020, 13:21

Re: Remotely call threaded script functions

Post by Lil_macha »

Hi admin,

I tried out the solution you gave out in response to @jay. It printed out the following in MATLAB :
Connected to remote API server
Remote function call failed
Program ended

and the following in VREP :
Error: [string -unknown location]:?: Call failed. (simCallScriptFunctionEx on displayText_function@object)

Any suggestions why? I'm working on something similar and have been presented with the same problem.

Regards

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

Re: Remotely call threaded script functions

Post by coppelia »

Hello,

I didn't read through all of the previous messages. But my guess is that you threaded child script is not yet (or not anymore) initialized. This happens if you try to call a function in that script if your simulation is not yet running, the threaded script has not yet started, or the threaded script has already ended.

Cheers

Lil_macha
Posts: 6
Joined: 03 Mar 2020, 13:21

Re: Remotely call threaded script functions

Post by Lil_macha »

Thanks, i'm starting to better understand the problem. If you have any suggestions on how to overcome this, that would be helpful too.

Lil_macha
Posts: 6
Joined: 03 Mar 2020, 13:21

Re: Remotely call threaded script functions

Post by Lil_macha »

Hi, can i just know :

1. how to measure the angle between 2 links connected to a joint in vrep
2. display that angle in a graph

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

Re: Remotely call threaded script functions

Post by coppelia »

You can measure the angle in a joint with sim.getJointPosition.
Here about displaying custom data in a graph.

Cheers

Post Reply