Sensing if a light sensor is within a cone of light

Typically: "How do I... ", "How can I... " questions
RobAtLab
Posts: 42
Joined: 10 Jan 2018, 17:49

Sensing if a light sensor is within a cone of light

Post by RobAtLab » 03 Mar 2018, 22:11

In the real world I have an infrared LED in the base of a conical shield shinging out light into a conical "field of view" of a precise angle, and on another robot I have a number of IR light sensors each of which can detect whether it is illuminated (and hence somewhere within the geometric volume of the cone) or not illuminated (hence not within the cone). This provides information to the robot to help it with some fairly crude navigation. Can anyone provide any tips on simulating this in V-REP, preferably by the most computationally lightweight technique as I need to simulate a lot of other stuff at the same time. I've thought about setting up a 1x1 pixel light vision sensor and using that to detect some special coloured light that I'll ensure appears nowhere else in the scene, but I don't know whether light sources can be made which shine in cpnes of specifically defined angles (and if they can they might have computationally hungry ray tracing). I've also wondered about proximity sensors, but I'm NOT in a position where I want an object to which the proximity sensor is a child to know whether there are obstacles about, rather I want objects unrelated to the proximity sensor to just know if they are inside or outside it's cone.

Thank you

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

Re: Sensing if a light sensor is within a cone of light

Post by coppelia » 05 Mar 2018, 07:45

Hello,

yes, I would use a proximity sensor to emulate such a light source: this will be much faster than any other solution. The light sensors would be dummies.
Make sure that the proximity sensor is flagged as explicit handling and attach a non-threaded child script to it. Then, in each simulation step, in the sensing phase, go through all dummies in the scene and individually test them. I imagine something like:

Code: Select all

if (sim_call_type==sim.syscb_init) then
    h=sim.getObjectAssociatedWithScript(sim.handle_self)
    local objs=sim.getObjectsInTree(sim.handle_scene,sim.object_dummy_type)
    for i=1,#objs,1 do
        sim.writeCustomDataBlock(objs[i],'detected',sim.packInt32Table({0}))
    end
end


if (sim_call_type==sim.syscb_sensing) then
    local objs=sim.getObjectsInTree(sim.handle_scene,sim.object_dummy_type)
    for i=1,#objs,1 do
        local res=sim.checkProximitySensor(h,objs[i])
        sim.writeCustomDataBlock(objs[i],'detected',sim.packInt32Table({res}))
    end
end
In above situation, the sensor would write data to each dummy. You can then read that data to decide whether that dummy is seen or not. If you have several lights, then you'll have to read/write the dummy data and for instance bit-code the light channel.

Cheers

RobAtLab
Posts: 42
Joined: 10 Jan 2018, 17:49

Re: Sensing if a light sensor is within a cone of light

Post by RobAtLab » 05 Mar 2018, 15:50

Given those "getObjectsInTree" bits wouldn't this code only work if the dummy and the proximity cone are children of the same object, which in my case they definitely won't be?

Also, how would the child script of the robot which carried the light sensor go about determining whether each one was seeing light or not, that is to say how would the child script on an object which would be parent to the dummies go about reading that custom data written to the dummies?

Thanks

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

Re: Sensing if a light sensor is within a cone of light

Post by coppelia » 06 Mar 2018, 09:18

sim.getObjectsInTree(sim.handle_scene,sim.object_dummy_type) retrieves all dummies in the scene. So the script attached to the sensor would simply parse all dummies in the scene and mark them appropriately.

On your robot, you could then simply check which of the dummies contains the mark.

Cheers

RobAtLab
Posts: 42
Joined: 10 Jan 2018, 17:49

Re: Sensing if a light sensor is within a cone of light

Post by RobAtLab » 06 Apr 2018, 17:16

Thanks, with a tiny bit of code modification this is working great, but if I have multiple proximity sensor cones, each of which is doing this and each of which is deliberately writing a differently named (in your example the sensor names the custom data blocks as 'detected' ) custom data block to the dummy. Is there any way to search through multiple custom data blocks? Or am I missing something obvious?
Thanks

P.S. at the moment I have the child script on the robot which is the parent of the dummy being detected doing

Code: Select all

if (sim.readCustomDataBlock(DockingOne,"detected") == "1") then --DockingOne is the dummy to which custom datablocks are written
        print("Illuminated")
    else
        print("Dark")  
    end
and the proximity sensing cone has a child script on it doing the following

Code: Select all

if (sim_call_type==sim.syscb_init) then
    lightsource=sim.getObjectAssociatedWithScript(sim.handle_self)
    local objs=sim.getObjectsInTree(sim.handle_scene,sim.object_dummy_type)
    for i=1,#objs,1 do
        sim.writeCustomDataBlock(objs[i],'detected',"0")
    end
end


if (sim_call_type==sim.syscb_sensing) then
    local objs=sim.getObjectsInTree(sim.handle_scene,sim.object_dummy_type)
    for i=1,#objs,1 do
        resolved=sim.checkProximitySensor(lightsource,objs[i])
        if (resolved == 1) then
            sim.writeCustomDataBlock(objs[i],'detected',"1")
        else
            sim.writeCustomDataBlock(objs[i],'detected',"0")
        end
        --print(res)
        --sim.writeCustomDataBlock(objs[i],'detected',sim.packInt32Table({1}))
    end
end

if (sim_call_type==sim.syscb_cleanup) then
    local objs=sim.getObjectsInTree(sim.handle_scene,sim.object_dummy_type)
    for i=1,#objs,1 do
        sim.writeCustomDataBlock(objs[i],'detected',"0")
    end
    -- Put some restoration code here
    --simUI.destroy(ui)
end

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

Re: Sensing if a light sensor is within a cone of light

Post by coppelia » 07 Apr 2018, 09:26

I would always write to the same data block, i.e. the data block named detected (for instance). But instead of writing a simple string in there, do something like:

Code: Select all

local objectHandleOfThisScript=sim.getObjectAssociatedWithScript(sim.handle_self)
local data=sim.readCustomDataBlock(objs[i],'detected')
if data then
    data=sim.unpackTable(data)
else
    data={}
end
if (resolved == 1) then
    data[objectHandleOfThisScript]=true
else
    data[objectHandleOfThisScript]=nil
end
sim.writeCustomDataBlock(objs[i],'detected',sim.packTable(data))
Then you can check which sensor detected the dummy by simply parsing the map-like table stored in detected:

Code: Select all

local data=sim.readCustomDataBlock(objs[i],'detected')
if data then
    data=sim.unpackTable(data)
    for key,value in pairs(data) do
        print('sensor '..sim.getObjectName(key)..' detected me')
    end
end
Cheers

RobAtLab
Posts: 42
Joined: 10 Jan 2018, 17:49

Re: Sensing if a light sensor is within a cone of light

Post by RobAtLab » 11 Apr 2018, 16:35

The solution you most recently proposed, if I had multiple sensor cones operating and the dummy found itself within their overlap what would happen? In the real world with the infrared system actually in use each emitter sends pulsed IR signals in a really brief time period, so a sensor sitting in their intersection might recognise the emitted codes and think "I'm in light one's cone" or "I'm in light two's cone" if the brief pulsed signals didn't interfere, or if they do interfere and arrive at the same time (not too likely for really brief pulses with a long period inbetween) the sensor would regard this nonsense of noise as being meaningless. So in the real world a sensor caught in the crossing of multiple sensors might read itself as being in one of their cones, or might read itself as being in no cone at all. Using the simulation code you have most recently suggested would something similar happen? I don't really need to simulate things like the small chance of interference causing the sensor to think "I'm in no-one's cone", but I would prefer if possible something where there was a relatively equal chance of the sensor thinking itself in each of the cones that overlap it. This doesn't need to be highly random, I would be ok with a dummy sensor in the field of view of two proximity cones saying "I'm in light one's cone" during one timestep, "I'm in light two's cone" on the next and "I'm in light one's cone" the next and so on... but I wouldn't want this sensor in the intersection of multiple cones to report that it was "I'm in light one's cone" all the time, nor to always say "I'm in light two's cone", but to flip between the two. Do you have suggestions, does the method you have recommended do this? I would think that at a guess each proximity cone writing to the datablock wipes over what was in it previously, does this mean that in every timestep of the simulation whichever proximity cone was last to have it's script (would this be whichever has the letters of it's object name nearest to the end of the alphabet? A sensor called Cone_z would always wipe over Cone_a?) called would always wipe over other stuff written to the datablock?
Thanks
P.S. sorry if this post is a little hard to follow, I'm trying to explain things in a generalised way such that key features of actions and behaviours are common between simulations and the real robots without bogging down in platform specific details

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

Re: Sensing if a light sensor is within a cone of light

Post by coppelia » 12 Apr 2018, 07:37

How you interpret the information stored in the dummies is completely up to you. E.g. you could have a algorithm randomly select one of the cones if several cones have detected it. Or ignore detection if the dummy is within more than one cone.

The information stored in your dummy is a map between cone object handles (key) and detection state (value). That map needs to be updated in each simulation step. Make sure that you read that map not while it is being updated. e.g. you could update the map in the sensing section of a non-threaded child script, and read it in the actuation section.

Cheers

RobAtLab
Posts: 42
Joined: 10 Jan 2018, 17:49

Re: Sensing if a light sensor is within a cone of light

Post by RobAtLab » 12 Apr 2018, 15:42

So if I have multiple proximity cones writing to the same datablock on a dummy object then they won't overwrite? If I wrote a child script for an algorithm to pick which cone or ignore detection when the dummy is detected by mul;tiple cones, what object would the child script need to be associated with?
Thanks

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

Re: Sensing if a light sensor is within a cone of light

Post by coppelia » 16 Apr 2018, 06:34

No they don't overwrite if the process of writing is like following:
  • read the info
  • modify the info
  • write the info
It is like reading a database, adding a name to it, and writing it back: by doing so you won't erase the names of other participants that have their name in the database.

Cheers

Post Reply