Page 1 of 1

Threading in Vrep

Posted: 29 Dec 2014, 14:31
by AsadAli
Hey Every one . I have a function written in a threaded Script i want to use it in such a way that whenever a sensor reads a value then if that value falls in the desired range then that function of the threaded script must be called .How can i do that . Its like a thread in C++ which in continuously keeping track of a particular change.
Any help will be really appriciated.

Re: Threading in Vrep

Posted: 29 Dec 2014, 15:36
by Eric
Hi

if the function you want to use is in the same threaded script as the rest of your code (i.e. where you read the sensor)

Code: Select all

function myFunction(param)
	--do something
end

threadFunction=function() 
 while 1 do
  --reading the sensor
  sensorVal,forceVector=simReadForceSensor(objectHandle)

  --checking if the force sensor val is falling into the range
  if forecVector[1]> triggerVal then  
 	myFunction()
  end
  simSwitchThread()
 end
end

-- Put some initialization code here:
simSetThreadSwitchTiming(2) -- Default timing for automatic thread switching

-- Here we execute the regular thread code:
res,err=pcall(threadFunction)
if not res then
	simAddStatusbarMessage('Lua runtime error: '..err)
end
If the code that reads a proximity sensor (and the time it occurred) is in an other (non threaded) script and the threaded script with the function is in a separate script you can use for example signals to communicate within the scripts :

The non threaded script where the sensor is read:

Code: Select all

if (sim_call_type==sim_childscriptcall_initialization) then
	h=simGetObjectHandle('Proximity_sensor')
end

if (sim_call_type==sim_childscriptcall_sensing) then
	--reading the proximity sensor
	sensorData=simReadProximitySensor(h)
	if sensorData==1 then --the sensor read something
		--sending the time of the reading through a signal
		simSetStringSignal('proxySignal',string.format('%i',simGetSystemTimeInMilliseconds()) )
	end
end
The threaded script with the function:

Code: Select all

function myFunction(param)
	simAddStatusbarMessage(param)
end
threadFunction=function()
	while 1 do
		 --reading the signal
		 sensorData=simGetStringSignal("proxySignal")
		 --checking if the force sensor val is falling into the range
		 if sensorData~=nil then
			--we have some sata in the signal so we get it and clean it
			simClearStringSignal('proxiSignal')
			myFunction(sensorData)
		 end
		 simSwitchThread()
	end
end
-- Put some initialization code here:
simSetThreadSwitchTiming(2) -- Default timing for automatic thread switching

-- Here we execute the regular thread code:
res,err=pcall(threadFunction)
if not res then
	simAddStatusbarMessage('Lua runtime error: '..err)
end

Cheers

Eric

Re: Threading in Vrep

Posted: 29 Dec 2014, 15:59
by AsadAli
When you define a function with ThreadFunction , what does that mean , Is it different from simple function definition in Vrep

Re: Threading in Vrep

Posted: 30 Dec 2014, 14:32
by coppelia
it is just a function name. But in that case that function lives in a threaded child script, and you can stay inside the function until you stop the simulation (if you wish). You cannot do that in a non-threaded child script, otherwise the whole simulation blocks.

Cheers