Call C++ run function from Lua

Typically: "How do I... ", "How can I... " questions
Post Reply
yale.lee
Posts: 4
Joined: 24 Oct 2022, 18:20

Call C++ run function from Lua

Post by yale.lee »

Hi there,

I am trying to call a C++ function from my Lua script so it only contains

Code: Select all

simMyPlugin.Run()
and all the algorithm is placed in that

Code: Select all

void LUA_RUN_CALLBACK
function.
For now, I have the following C++ code:

Code: Select all

void LUA_RUN_CALLBACK(SScriptCallBack* cb)
{
	launchThreadIfNeeded();
	
	printf("[simMyPlugin]" Initializing myPlugin);
	
	bool bEndSim = false;
	while(!bEndSim)
	{
		// My routine
		printf(bEndSim);
		if(stopCondition)
		{
			bEndSim = true;
		}
	}
}
And this Lua script:

Code: Select all

function sysCall_actuation()
	simMyPlugin.Run()
end
And I only get the plugin initialization, as if the while loop was jammed.

Bottomline, I'd like to have a single version of the binary .ttt file and do the development in my C++ project. Also, the additional difficulty is that I need to read the data of my sensor through that C++ algo.

I may be designing the whole thing but given the constraints I have, this configuration seems like the only solution. Any idea would be greatly appreciated.

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

Re: Call C++ run function from Lua

Post by coppelia »

Hello,

I do not entirely understand what s not working correctly. Basically, you have a c++ CoppeliaSim plugin, and want to call one function inside of that plugin, from within a CoppeliaSim script.

There are 2 different ways of doing this: the old way and the new way. The old way is easier to understand and set up, for small projects. The new way is a bit more difficult to initially set up, butmore flexible and easier to maintain.

From your code, you are using the old way. There are several things in your plugin that are missing, not sure if this is intended to simplify your post. Have a look at the skeleton plugin from the old way, but basically you will need a callback function. e.g.:

Code: Select all

// --------------------------------------------------------------------------------------
// simExtSkeleton_getData: an example of custom Lua command
// --------------------------------------------------------------------------------------
#define LUA_GETDATA_COMMAND "simSkeleton.getData" // the name of the new Lua command

void LUA_GETDATA_CALLBACK(SScriptCallBack* p)
{ // the callback function of the new Lua command ("simExtSkeleton_getData")
    int stack=p->stackID;

    CStackArray inArguments;
    inArguments.buildFromStack(stack);

    if ( (inArguments.getSize()>=2)&&inArguments.isString(0)&&inArguments.isMap(1) )
    { // we expect at least 2 arguments: a string and a map

        CStackMap* map=inArguments.getMap(1);
        std::string tmp("we received a string (");
        tmp+=inArguments.getString(0).c_str();
        tmp+=") and following message in the map: ";
        tmp+=map->getString("message").c_str();
        simAddLog("PluginSkeleton",sim_verbosity_msgs,tmp.c_str());
    }
    else
        simSetLastError(LUA_GETDATA_COMMAND,"Not enough arguments or wrong arguments.");

    // Now return a string and a map:
    CStackArray outArguments;
    outArguments.pushString("Hello World");
    CStackMap* map=new CStackMap();
    map->setBool("operational",true);
    CStackArray* pos=new CStackArray();
    double _pos[3]={0.0,1.0,2.0};
    pos->setDoubleArray(_pos,3);
    map->setArray("position",pos);
    outArguments.pushMap(map);
    outArguments.buildOntoStack(stack);
}
// --------------------------------------------------------------------------------------
Then you need to make sure that you have the 3 required entry function to your plugin:

Code: Select all

SIM_DLLEXPORT unsigned char simStart(void* reservedPointer,int reservedInt)
SIM_DLLEXPORT void simEnd()
SIM_DLLEXPORT void* simMessage(int message,int* auxiliaryData,void* customData,int* replyData)
Inside of the simStart function, you will have to register your callback function, e.g.:

Code: Select all

simRegisterScriptCallbackFunction(strConCat(LUA_GETDATA_COMMAND,"@","PluginSkeleton"),strConCat("...=",LUA_GETDATA_COMMAND,"(string data1,map data2)"),LUA_GETDATA_CALLBACK);
Cheers

yale.lee
Posts: 4
Joined: 24 Oct 2022, 18:20

Re: Call C++ run function from Lua

Post by yale.lee »

Hello,

Thanks for the reply. The problem was that I was trying to run a while loop within the actuation function, which is really dumb. So sure enough, the thread was kind of stuck I guess, making the whole thing crash.

Yet, I have another issue. I am trying to compute the distance between my mobile and a wall, and it appears that

Code: Select all

float d[7];
simCheckDistance(quad, wall, 0.4, d);
std::cout << d[6] << std::endl;
does not return the same value as

Code: Select all

simCheckDistance(wall, quad, 0.4, d);
std::cout << d[6] << std::endl;
0.4 being the threshold under which I don't even bother calculate that distance.

IfI understand it right, d is a placeholder in which the return values of simCheckDistance are written, d[0..5] are distance segments and d[7] the actual value. What am I understand wrong?

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

Re: Call C++ run function from Lua

Post by coppelia »

Both should result in a same distance. At least, given a very small calculation threshold. It this is not the case, please post the scene for us to test. And yes, d should be a float array of size 7. The last item is the actual length of the segment.

Cheers

yale.lee
Posts: 4
Joined: 24 Oct 2022, 18:20

Re: Call C++ run function from Lua

Post by yale.lee »

It actually works, I was trying to reach for d[7] which should not exist in C++... Sorry for the incovenience and thank you again for all the answers!

Post Reply