Page 1 of 1

sim.checkProximitySensor always returns result = 1

Posted: 21 Jun 2018, 15:14
by SRL_Max
Hello,

I'm trying to work with proximity sensors and I encountered a problem with the sim.checkProximitySensor function : it seems that it always detects the object I'm passing as argument.

My scene is very simple : a ray type proximity sensor is attached to a robot leg. A static, respondable shape is approached to the sensor until contact (I do this manually).

Here is my code:

Code: Select all

-- attached to each leg tip

function sysCall_init()

	prox_FL = sim.getObjectHandle("prox_FL") -- proximity sensor on leg tip
						 -- sensor range is 0.002m in front of leg tip
	dummy_tip_FL = sim.getObjectHandle("dummy_tip_FL")
	tar = sim.getObjectHandle("tar") -- target
	cub = sim.getObjectHandle("Cuboid") -- static cube shape

end

function sysCall_sensing()

	res_FL, dist_FL, det_pt_FL = sim.checkProximitySensor(prox_FL, cub)
	
	if res_FL then -- we detect the cube in front of the foot
		print(dist_FL)
	end

end
No matter where I put the cube, res_FL is always 1.
The strange thing is that dist_FL is "nil" when the cube is not in the sensor range, which contradicts the fact that res_FL is 1 (ref. to the function documentation).

Explicit handling or not doesn't change the output.

I think I don't understand how the sim.checkProximitySensor is supposed to work. Can you help me with that?

Best,
Maxens

Re: sim.checkProximitySensor always returns result = 1

Posted: 23 Jun 2018, 18:08
by coppelia
Hello Maxens,

in Lua, 0 or 1 (or for that matter any number, string, function, etc.) evaluates to true. That is the problem in your code. Only nil or false evaluates to false. So you should write instead:

Code: Select all

if res_FL>0 then
    -- we detected the cube
end
Cheers

Re: sim.checkProximitySensor always returns result = 1

Posted: 25 Jun 2018, 11:54
by SRL_Max
Hello admin,

I understand now, thank you very much!

Best,
Maxens