View Full Version : how to check if specified number of targets are in proximity sensor?

12-05-2016, 08:47 PM
Hey there! Can anyone tell me what's the best way to check, if a specified number of targets are inside a proximity sensor?

I have an experiment in which a person is required to move a specified number of targets into a proximity sensor in order to continue. On top of that, this should be done within a specified time, e.g. 5 seconds.

I tried something like:

def task1(numActiveTargets):
import random
numTrials = 100

for trial in xrange(numTrials):
targetSensor = random.choice(sensors) #a list of sensors
activeTargets = manager.getActiveTargets(sensor=targetSensor)
if len(activeTargets) < numActiveTargets:
yield vizproximity.waitEnter(sensor = targetSensor, target = targets) #seems to be activated by any target in list of targets

yield viztask.waitTime(5)

def experiment():
yield task1(2)
yield task1(3)


...but it wouldn't work. Like, I'd have to check for 5 seconds if my chosen sensor contains at least 3 targets, for example.

12-06-2016, 03:14 AM
Hi Vaquero ,
You might be looking for this
Call these methods as per logic:


Thank You!!!

12-06-2016, 06:35 AM
Thank you for your reply. I already use manager.getActiveTargets(sensor=targetSensor) in my code sample above. So unfortunately your reply doesn't provide any logic to the problem of how to check for 5 seconds long if at least 3 targets occupy a sensor at the same time, and if not, start the next trial.

12-06-2016, 10:52 AM
One way to do this is create a custom event (http://docs.worldviz.com/vizard/#Custom_Events.htm) that's triggered when 3 targets are inside a sensor. Write a function that's called every frame, checks to see how many targets are in the sensor, and sends the event. In the viztask function, use the viztask.waitAny (http://docs.worldviz.com/vizard/#commands/viztask/waitAny.htm) command to wait for either the custom event or 5 seconds:

waitTargets = viztask.waitEvent(THREE_TARGETS_EVENT)
waitTime = viztask.waitTime(5)

for trial in xrange(numTrials):
d = yield viztask.waitAny( [ waitTargets, waitTime ] )

if d.condition is waitTargets:
print 'targets entered'
print '5 seconds passed'

12-19-2016, 01:14 AM
Thanks Jeff, that brought me closer to a solution, but I can't get it to work.
In each trial there's another sensor that needs to trigger the event, but whatever I do, I can't seem to get the functions/events recognize the newly chosen sensor.

First I thought, well, I would only need to check for the number of targets in the sensor, if a target enters the sensor. Here's some pseudo-code:

def checkTrigger(manager,requiredSensor):
activeTargets = manager.getActiveTargets(sensor=requiredSensor)
if len(activeTargets) == len(targets):
time = viz.tick()
viz.sendEvent(TRIGGER_ACTIVATED_EVENT,requiredSens or.name,time)

def myTask():
targets = [target1,target2,target3]
sensors = [sensor1,sensor2,sensor3]
triggerSensor = someSensor

def EnterSensor(e):

for sensor in sensors:
manager.onEnter(sensor, EnterSensor)

results = yield mySubTask()

def mySubTask():
waitTargets = viztask.waitEvent(TRIGGER_ACTIVATED_EVENT)
waitTime = viztask.waitTime(5)

for trial in xrange(100):
triggerSensor = random.choice(sensors)
d = yield viztask.waitAny( [ waitTargets, waitTime ] )

if d.condition is waitTargets:
print 'targets entered'
print '5 seconds passed'


But the EnterSensor/checkTrigger function is always checking for 'someSensor' and not updating to the new triggerSensor in each trial (IDs were the same for triggerSensor object in both functions, I checked)

Now I tried using
checkingTrigger = vizact.onupdate(0,checkTrigger,manager,triggerSens or)
and using checkingTrigger.setEnabled(viz.<OFF/ON>) when I need it, but it's the same problem in that it only recognizes the very first triggerSensor.

Could you please give a more complete, working example?

12-19-2016, 05:59 AM
It works by doing the checkingTrigger = vizact.onupdate(0,checkTrigger,manager,triggerSens or) part inside the loop after triggerSensor = random.choice(sensors) was called, and removing the EventFunction at the end of the loop.

12-27-2016, 03:15 PM
There's another requirement for the task I need to implement, but can't figure out how to accomplish it. All the targets shall reach the given sensor concurrently (within tolerance) to count as a success.
So there's, for example, a 5 seconds time limit in which the sensor must be reached, but within that time limit the targets must reach the sensor within a shorter amount of time, e.g. 1 second. For now, the user should be able to try this within the time limit until she succeeds.

I guess I should utilize starttimer, so I'd have to start it, when the first target enters the sensor and before it elapses, all targets should have followed. If one target exits the sensor before the timer elapsed, it should be stopped (killtimer). But if I put the starttimer in the on_enter function of the sensor, then each target starts a new timer. I suppose there should be an event that marks a success only when the TRIGGER_ACTIVATED_EVENT occured while the timer event did not yet occur.
I tried setting a global variable to true and false, but since each onEnter-call sets it to true, it's useless.

Here's a condensed debug version of my script so far:

import viz
import vizcam
import vizshape
import vizproximity
import viztask
import random

def use_pivot_cam():
cam = vizcam.PivotNavigate(distance=1.5,center=(0,0,0))

def create_proximity_manager():
manager = vizproximity.Manager()
debugEventHandle = vizact.onkeydown('1',manager.setDebug,viz.TOGGLE)
return manager

def add_debug_tracker(targetName,controlScheme):
axis = vizshape.addAxes(length=0.1)
if controlScheme == 0:
tracker = vizcam.addKeyboardPos()
elif controlScheme == 1:
tracker = vizcam.addKeyboardPos(forward=viz.KEY_UP,backward= viz.KEY_DOWN,left=viz.KEY_LEFT,right=viz.KEY_RIGHT ,up=viz.KEY_PAGE_UP,down=viz.KEY_PAGE_DOWN)
tracker = vizcam.addKeyboardPos(forward='i',backward='k',lef t='j',right='l',up='p',down='o')
target = vizproximity.Target(axis)
target.name = targetName
return target

def add_sensor(position,shape,color,name):
if shape == 'torus':
sensorShape = vizshape.addTorus(0.15,0.025,24,32,vizshape.AXIS_Z )
sensorShape.name = 'TorusShape'
sensorRadius = 0.175
elif shape == 'sphere':
sensorShape = vizshape.addSphere(0.1)
sensorShape.name = 'SphereShape'
sensorRadius = 0.1
sensorShape = vizshape.addCube(0.3)
sensorRadius = 0.15
sensor = vizproximity.Sensor(vizproximity.Sphere(sensorRadi us), source=sensorShape)
sensor.name = name
return sensor

bTimerActive = False
def check_trigger(manager,requiredSensor,numTargets):
activeTargets = manager.getActiveTargets(sensor=requiredSensor)
#print('Timer active: ', bTimerActive) #Debug
if len(activeTargets) == numTargets and bTimerActive:
viz.sendEvent(TRIGGER_ACTIVATED_EVENT,requiredSens or)

def on_trigger_activated(trigger):
triggerSound = trigger.getSourceObject().playsound('quack.wav', viz.PLAY)
viz.callback(TRIGGER_ACTIVATED_EVENT,on_trigger_ac tivated)

def experiment():
manager = create_proximity_manager()
sensors = []
for sensor in sensors:
targets = []
for target in targets:
numTargets = len(manager.getTargets())

waitTargets = viztask.waitEvent(TRIGGER_ACTIVATED_EVENT)
timeLimit = 5
waitTime = viztask.waitTime(timeLimit)

global bTimerActive
bTimerActive = False

def on_timer_elapsed(timerID):
print 'Elapsed time: {:.2f} seconds'.format(viz.elapsed())
global bTimerActive
bTimerActive = False


concurrencyTolerance = 1.0 # Time frame for accepting targets as entering simulataneously.
def enter_sensor(e):
print('{0} entered {1} .'.format(e.target.name,e.sensor.name))
print('Starting timer.')
global bTimerActive
bTimerActive = True

def exit_sensor(e):
print('{0} exited {1}.'.format(e.target.name,e.sensor.name))
print('Killing timer.')
global bTimerActive
bTimerActive = False

manager.onEnter(None, enter_sensor)
manager.onExit(None, exit_sensor)

i = 0
while True:
i += 1
triggerSensor = random.choice(sensors)

print('Press spacebar to start next trial:\nMove {0} targets concurrently (tolerance: {2}s) into {3} sensor within {1} seconds.'.format(numTargets,timeLimit,concurrencyT olerance,triggerSensor.name))
yield viztask.waitKeyDown(' ')
print("Trial {} started.".format(i))
#ToDo: Trigger shall be reached by all targets within concurrencyTolerance.
checkingTrigger = vizact.onupdate(0,check_trigger,manager,triggerSen sor,numTargets)

d = yield viztask.waitAny([waitTargets, waitTime])
if d.condition is waitTargets:
print('Failure! {} seconds passed'.format(timeLimit))



Does anybody have an idea how to solve this?

12-27-2016, 04:40 PM
I think I got it. I just check for the boolean status of the timer before I start a new one AND make sure, there are not already other targets in the sensor.

def enter_sensor(e):
print('{0} entered {1}.'.format(e.target.name,e.sensor.name))
numTargets = len(e.manager.getActiveTargets(sensor=e.sensor))
global bTimerActive
if not bTimerActive and numTargets == 1:
print('Starting timer.')
bTimerActive = True

But maybe there's a more elegant solution?

12-27-2016, 05:23 PM
Unfortunately, when 2 targets arrive at the exact same time at the sensor, the timer isn't started because numtargets > 1. I didn't think it was possible or probable, but it is. So my solution is still not good enough.