Search code examples
javaproximitysensor

Get distance v-rep proximity sensor Java


I need to get distance in meters on a v-rep proximity sensor coded in java.

I can only know if it detects an obstacle or not, but not the distance

Any idea?


Solution

  • The way I have found to do this is to create a child script on the proximity sensor. In V-Rep, with the proximity sensor selected, go to Add --> associated child script --> Non-Threaded then make the sysCall_actuation() look something like:

    function sysCall_actuation()
        local name = sim.getObjectName(sim.getObjectAssociatedWithScript(sim.handle_self))
        result,distance,detectedPoint,detectedObjectHandle = sim.handleProximitySensor(sim.handle_self)
        if distance then
            sim.setFloatSignal("mydistance", distance);
        end
    end
    

    this reads the distance from the proximity sensor and pushes it to a signal (like a cross-platform variable type thing). Then on the java side have something like this:

        FloatW val = new FloatW(-1);
        vrep.simxGetFloatSignal(clientID, "mydistance", val, remoteApi.simx_opmode_buffer);//call this with simx_opmode_streaming once before this to start vrep streaming the distances
    

    this gets the value sent from vrep and gives you the distance in the form of a FloatW.