Calling custom lua functions from CPP

Typically: "How do I... ", "How can I... " questions
amitsaini
Posts: 13
Joined: 09 Jun 2019, 08:12

Calling custom lua functions from CPP

Post by amitsaini »

Hi all, I want to know. Is it possible to call custom made lua functions from CPP program.
I mean i have made a program to control robot using API mode. Now i want to make a function in lua thread inside simulator and want to call this function from Cpp program i made. Is it possible?

Thank you
Stay Safe!!

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

Re: Calling custom lua functions from CPP

Post by coppelia »

Hello,

yes, you can do that. If your cpp application is running in its own thread/process, then you can do this via the remoteAPI for instance, e.g. simxCallScriptFunction (b0-based remote API) or simxCallScriptFunction (legacy remote API). There are similar ways when running a ROS/ROS2 client application.
If your cpp code is located inside of a CoppeliaSim plugin (and runs in the CoppeliaSim simulation thread), then you can call simCallScriptFunctionEx.

Cheers

amitsaini
Posts: 13
Joined: 09 Jun 2019, 08:12

Re: Calling custom lua functions from CPP

Post by amitsaini »

Thank you Coppelia for your answer. Let me give it a try.

Let me clarify why i need this. I made a quadruped robot and giving commands to motors from cpp program.
But when i am updating values of 12 motors. It took some time to reflect. The motor i update first reflect changes first and the last one changes in the last. It causes my robot fall down.

So what i did is i saved the values of motor angles generated from the cpp program in an array in lua(inside simulator)
and changed the values of 12 motors simultaneously from that arrays from lua program instead of cpp program. It behaved better.

So now i am thinking to make some functions in lua side(specially controlling motions) and call them from cpp program.

Is there any other solution for this?
Thank you!!

amitsaini
Posts: 13
Joined: 09 Jun 2019, 08:12

Re: Calling custom lua functions from CPP

Post by amitsaini »

I am trying to call the function named testing()
which is defined inside a threaded script attached with a cuboid named my_object

Code: Select all

simxCallScriptFunction(clientID,"my_object",sim_scripttype_childscript,"testing",0,NULL,0,NULL,0,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,simx_opmode_blocking);

on simulation side i am getting this message
Error: [string -unknown location]:?: Call failed. (simCallScriptFunctionEx on testing@my_object)

I don't know what is happening

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

Re: Calling custom lua functions from CPP

Post by coppelia »

So it seems that you are using the legacy remote API for that task.
Does you testing function look similar to this?

Code: Select all

function myFunctionName(inInts,inFloats,inStrings,inBuffer)
	-- inInts, inFloats and inStrings are tables
	-- inBuffer is a string
    
    -- Perform any type of operation here.

	-- Always return 3 tables and a string, e.g.:
	return {},{},{},''
end
If you simply want to set some values on CoppeliaSim side, without the need of a return value, call the function via the simx_opmode_oneshot.

Via the legacy remote API you have another option to make sure that all 12 motors are set at the same time, e.g.:

Code: Select all

simxPauseCommunication(clientID,1);
simxSetJointPosition(clientID,joint1Handle,joint1Value,simx_opmode_oneshot);
simxSetJointPosition(clientID,joint2Handle,joint2Value,simx_opmode_oneshot);
...
simxSetJointPosition(clientID,joint12Handle,joint12Value,simx_opmode_oneshot);
simxPauseCommunication(clientID,0);
Cheers

amitsaini
Posts: 13
Joined: 09 Jun 2019, 08:12

Re: Calling custom lua functions from CPP

Post by amitsaini »

Yes i made the error of syntax. Now i changed function arguments as per the syntax given by you. But still getting same error.

simRemoteApi.start(19999) is being called from one threaded script attached to a cuboid

and function is defined in another threaded script.

amitsaini
Posts: 13
Joined: 09 Jun 2019, 08:12

Re: Calling custom lua functions from CPP

Post by amitsaini »

Update
When i called a function in non-threaded script. It is working.

but in threaded script it is not.

amitsaini
Posts: 13
Joined: 09 Jun 2019, 08:12

Re: Calling custom lua functions from CPP

Post by amitsaini »

Can it work in threaded script as well?

amitsaini
Posts: 13
Joined: 09 Jun 2019, 08:12

Re: Calling custom lua functions from CPP

Post by amitsaini »

Sir/mam kindly help!

fferri
Posts: 646
Joined: 09 Sep 2013, 19:28

Re: Calling custom lua functions from CPP

Post by fferri »

Are you sure when you call the function in the threaded script, the threaded script is initialized (i.e. it has started, but it has not terminated yet)?

Post Reply