PDA

View Full Version : External Trigger via LabJack


ursi
01-15-2010, 11:53 AM
Hi,
I wanna trigger a Vizard-function via an external trigger (via LabJack). How do I do that?

I wrote the following code using 'vizact.waitsignal':

##############################
import viz
viz.go()

import u12
d = u12.U12()
##############################
Trigg = d.eAnalogIn(0)

#Create the signal object
sig = vizact.signal(Trigg > 0)

#Create an action to wait on the signal
waitsig = vizact.waitsignal(sig)

def ExtTrigg(waitsig)
### add SOUND
song = viz.add('Beethoven.wma')
song.loop(viz.OFF)

#viz.callback(viz.Event,ExtTrigg) ##?
viz.callback(viz.ACTION_BEGIN_EVENT,ExtTrigg) ##?
##############################

Thanks!

Jeff
01-15-2010, 04:11 PM
You could use vizact.onupdate to check every frame if the trigger has occurred and then either do something there or call another function

def checkTrigger():

trigg = d.eAnalogIn(0)
if trigg > 0:
ExtTrigg()

vizact.onupdate(0, checkTrigger)

ursi
01-18-2010, 12:04 PM
Thanks!

And the callback function is ok or do I have to callback also the 'checkTrigger' function?

##############################
#Create the signal object
sig = vizact.signal(Trigg > 0)

#Create an action to wait on the signal
waitsig = vizact.waitsignal(sig)

def ExtTrigg(waitsig)
### add SOUND
song = viz.add('Beethoven.wma')
song.loop(viz.OFF)

###+++++++++++++++++++++++++++
def checkTrigger():
trigg = d.eAnalogIn(0)
if trigg > 0:
ExtTrigg()

vizact.onupdate(0, checkTrigger)

viz.callback(viz.ACTION_BEGIN_EVENT,ExtTrigg)
##############################

Jeff
01-18-2010, 06:06 PM
The line of code
vizact.onupdate(0, checkTrigger)

already registers the checkTrigger function and that will be called every frame. Have you tried running that on it's own without the code you added?

ursi
01-19-2010, 05:17 PM
Hi

I tried it like that to keep it simple:

##############################
import viz
viz.go()

import u12
d = u12.U12()

trigg = d.eAnalogIn(0) # AI0
##############################
def ExtTrigg():
print trigg

###+++++++++++++++++++++++++++
def checkTrigger():
if trigg > 2.0:
ExtTrigg()

vizact.onupdate(0, checkTrigger)
##############################

But it gives me this output (ever and ever again; regardless if I change my if-condition to 2.0, what should call the ExtTrigg-function):

{'overVoltage': 0, 'idnum': 0, 'voltage': 1.4208984375}

I can't read in a external voltage into AI0 (a ramp 0V-5V-0V produced by a battery). My output in Vizard is always unchanged by 1.42...V.

Do I also have to address my output like ' if trigg{3} > 2.0' or similar? I tried that, but it didn't seem to work with either [] nor () or {}.

I just have some background in Matlab, so I don't have a clue, what I could try next in Python...

Jeff
01-20-2010, 10:42 AM
Please use the code tags when you post code to preserve the indentation. This line
trigg = d.eAnalogIn(0) # AI0
should be inside your checkTrigger function if you want to be continuously checking it.

ursi
01-20-2010, 01:06 PM
The problem still remains...

def ExtTrigg():
print 'trigger > 2.0'


###+++++++++++++++++++++++++++
def checkTrigger():
trigg = d.eAnalogIn(0) # AI0
if trigg > 2.0:
ExtTrigg()

vizact.onupdate(0, checkTrigger)

How can I address the 'right' output to be checked?
Why does it not work to feed in a voltage?

Jeff
01-20-2010, 05:40 PM
What do you get if you try the following? This should print out the voltage once a second. Do you notice any errors in Vizard's input/output window?
def checkTrigger():
trigg = d.eAnalogIn(0) # AI0
print trigg['voltage']

vizact.ontimer(1, checkTrigger)

ursi
01-21-2010, 10:39 AM
Hi,

I can now feed in 'my' voltage from the external source and print just the voltage output. So, that works fine, thanks.

But it prints the result still every second, so the if-condition does not seem to work properly...


def ExtTrigg():
print 'trigger > 2.0'


###+++++++++++++++++++++++++++
def checkTrigger():
trigg = d.eAnalogIn(0) # AI0
if trigg > 2.0:
ExtTrigg()
print trigg['voltage']

vizact.ontimer(1, checkTrigger)

Another question: can I chance the code, so that the frequence for checking the trigger would be higher than once a second?

ursi
01-21-2010, 11:55 AM
Ok, I see, that the if-condition work fine, if I address the voltage output...

if trigg['voltage'] > 0.2:

So, the last question that remains is the higher frequency for checking the trigger?

ursi
01-21-2010, 11:57 AM
Sorry, I figured it out...

vizact.ontimer(.01, checkTrigger)