PDA

View Full Version : Proximity Manager - How to implement delay


jelly
11-16-2016, 11:57 AM
Hello,

I have a timing issue in my script, as I am not sure how to implement a delay, in that when the proximity sensor is entered, a variable (handstate) changes only after 2 seconds have elapsed.

That variable change triggers a certain action in a different function, but to keep it simple, I have copied below the proximity function I have:

def EnterProximity_IndexFinger(e): # decide what happens on sensor entry
global handstate
e.sensor == SensorIndexFinger
handstate = 1
print "sensor A entered"
print condition
manager.onEnter(SensorIndexFinger,EnterProximity_I ndexFinger)

So far, I tried to not change the handstate inside the proximity function but to use it to call another function that has a delay implemented. Something like this:


def Change():
global handstate
yield viztask.waitTime(2)
handstate = 1


def EnterProximity_IndexFinger(e): # decide what happens on sensor entry
global handstate
e.sensor == SensorIndexFinger
print "sensor A entered"
yield Change()
manager.onEnter(SensorIndexFinger,EnterProximity_I ndexFinger)

Another thing I tried is to not use the "manager.onEnter" it but the "yield vizproximity.waitEnter". However, I must be doing something wrong as neither method works.

I reckon it must not be too tricky to achieve this - in essence I just want a 2 second delay after proximity sensor was entered before my action is triggered. But I am not sure how to achieve this. I would be very happy to receive some ideas or help!

Best,
J

Jeff
11-16-2016, 05:36 PM
Use the vizproximity.waitEnter command within a task function:

import viztask

def proximityTask():

yield vizproximity.waitEnter(SensorIndexFinger)
print 'entered sensor'
yield viztask.waitTime(2)
print 'two seconds elapsed'

viztask.schedule(proximityTask())

jelly
11-17-2016, 02:45 AM
Dear Jeff,

thank you for the help, this seems like the way to go! But I have some problems with it:

I noticed that adding the line

viztask.schedule(proximityTask())

makes the event happen only once, upon entering the sensor for the first time.

My question would be how do I make it happen everytime, like it was the case when I used

manager.onEnter(SensorIndexFinger,EnterProximity_I ndexFinger)

I tried the following (somehow adding the function that specifies the delay to the manager):

SensorIndexFinger = vizproximity.Sensor(vizproximity.Box([0.03,0.03,0.03],center=[0,0,0]),IndexFinger)
manager.addSensor(SensorIndexFinger)

def proximityTask():
global handstate
yield vizproximity.waitEnter(SensorIndexFinger)
print 'entered sensor'
yield viztask.waitTime(2)
print 'two seconds elapsed'
handstate = 1
print 'change hand'

def EnterProximity_IndexFinger(e): # decide what happens on sensor entry
global handstate
e.sensor == SensorIndexFinger
print "activate proximityTask"
manager.onEnter(SensorIndexFinger,proximityTask())

But I get an error "TypeError: 'generator' object is not callable."

I would be very happy to hear if you had any suggestions on how to call the action (with delay) every time the sensor was entered, as I can't seem to find a way to implement it :o

Jeff
11-17-2016, 03:29 PM
You can add a loop inside the task function:

def proximityTask():

while True:
yield vizproximity.waitEnter(SensorIndexFinger)
print 'entered sensor'
yield viztask.waitTime(2)
print 'two seconds elapsed'

viztask.schedule(proximityTask())

jelly
11-18-2016, 08:48 AM
Thank you so much, Jeff, this works perfectly!