Jump to content
  • 0

Controlling AD2 imp meter relays using the Python SDK


Torbjørn

Question

I want to control the relays of the impedance meter board using python. I can toggle the relay back and forth by running my code more than once, but can't figure out how to toggle it back and forth without starting from the top. I tried using triggerPC but that won't work either.

The interesting bits:

dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf))

if hdwf.value == hdwfNone.value:
    print ("failed to open device")
    quit()

def send5mspulse(hdwf, ch):
    dwf.FDwfDigitalOutRunSet(hdwf, c_double(0.005)) # 5ms run
    dwf.FDwfDigitalOutEnableSet(hdwf, c_int(ch), c_int(1))
    dwf.FDwfDigitalOutIdleSet(hdwf, c_int(ch), c_int(1)) # 1=DwfDigitalOutIdleLow, low when not running 
    dwf.FDwfDigitalOutCounterInitSet(hdwf, c_int(ch), c_int(1), c_int(0)) # initialize with high when running
    dwf.FDwfDigitalOutCounterSet(hdwf, c_int(ch), c_int(0), c_int(0))

dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(0), c_int(0), c_double(True)) # enable positive supply
dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(0), c_int(1), c_double(5.0)) # set voltage to 5 V
dwf.FDwfAnalogIOEnableSet(hdwf, c_int(True)) # master enable

#dwf.FDwfDigitalOutTriggerSourceSet(hdwf, trigsrcPC) 
dwf.FDwfDigitalOutRepeatSet(hdwf, c_int(0)) # infinite repeat
dwf.FDwfDigitalOutRepeatTriggerSet(hdwf, c_int(1))  # wait for trigger in each cycle

# <-- This toggles the relay. If I change to pins 14 and 15 and re-run the code it toggles back -->
send5mspulse(hdwf, 6)
send5mspulse(hdwf, 7)

dwf.FDwfDigitalOutConfigure(hdwf, c_int(1))
#dwf.FDwfDeviceTriggerPC(hdwf)
time.sleep(1)

# <--- This will not toggle back! -->
send5mspulse(hdwf, 14)
send5mspulse(hdwf, 15)
#dwf.FDwfDeviceTriggerPC(hdwf)

time.sleep(1)
dwf.FDwfDeviceCloseAll()

 

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

Hi @Torbjørn

I would suggest using the digital-io functions for such relay control purpose, simple bit-bang, software sleep...

Regarding you earlier code: Trigger are only needed to synchronize instrument or devices, staring one instrument is sufficient with Config. Use reset or disable earlier configured channels if you don't want these to pulse again after you configure the following and start the instrument. Calling any Set will stop the instrument, Configure is needed again to start or arm it.
The control should look like this (I have not tested it) :

dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf))

if hdwf.value == hdwfNone.value:
    print ("failed to open device")
    quit()

dwf.FDwfDeviceAutoConfigureSet(hdwf, c_int(0)) # 0 = the instruments will only be configured when FDwf###Configure is called

def send5mspulse(hdwf, ch):
    dwf.FDwfDigitalOutReset(hdwf) # to clear previously instrument configuration, channels will be in by default in high impendance 
    dwf.FDwfDigitalOutRunSet(hdwf, c_double(0.005)) # 5ms run
    dwf.FDwfDigitalOutEnableSet(hdwf, c_int(ch), c_int(1))
    dwf.FDwfDigitalOutIdleSet(hdwf, c_int(ch), c_int(1)) # 1=DwfDigitalOutIdleLow, low when not running 
    dwf.FDwfDigitalOutCounterInitSet(hdwf, c_int(ch), c_int(1), c_int(0)) # initialize with high when running
    dwf.FDwfDigitalOutCounterSet(hdwf, c_int(ch), c_int(0), c_int(0))
    dwf.FDwfDigitalOutRepeatSet(hdwf, c_int(1)) # once
    dwf.FDwfDigitalOutConfigure(hdwf, c_int(1))

dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(0), c_int(0), c_double(True)) # enable positive supply
dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(0), c_int(1), c_double(5.0)) # set voltage to 5 V
dwf.FDwfAnalogIOEnableSet(hdwf, c_int(True)) # master enable
time.sleep(0.1) # wait for V+ ramp

send5mspulse(hdwf, 6)
time.sleep(0.006) # or verify dwf.FDwfDigitalOutStatus
send5mspulse(hdwf, 7)
time.sleep(0.006)
send5mspulse(hdwf, 14)
time.sleep(0.006)
send5mspulse(hdwf, 15)
time.sleep(0.006)

dwf.FDwfDeviceCloseAll()


 

Link to comment
Share on other sites

  • 0

Thanks again @attila!

Adding dwf.FDwfDigitalOutReset(hdwf) made all the difference. After some more debugging I can now toggle one of the relays back and forth, next step is now to add measurements with the scope channels.

Here is the working relay-toggler if you or anyone else are interested:

dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf))

def send5mspulse(hdwf, ch):
    dwf.FDwfDigitalOutRunSet(hdwf, c_double(0.005)) # 5ms run
    dwf.FDwfDigitalOutEnableSet(hdwf, c_int(ch), c_int(1))
    dwf.FDwfDigitalOutIdleSet(hdwf, c_int(ch), c_int(1)) # 1=DwfDigitalOutIdleLow, low when not running 
    dwf.FDwfDigitalOutCounterInitSet(hdwf, c_int(ch), c_int(1), c_int(0)) # initialize with high when running
    dwf.FDwfDigitalOutCounterSet(hdwf, c_int(ch), c_int(0), c_int(0))
    dwf.FDwfDigitalOutRepeatSet(hdwf, c_int(1)) # once

def enable5V(hdwf):
    dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(0), c_int(0), c_double(True)) # enable positive supply
    dwf.FDwfAnalogIOChannelNodeSet(hdwf, c_int(0), c_int(1), c_double(5.0)) # set voltage to 5 V
    dwf.FDwfAnalogIOEnableSet(hdwf, c_int(True)) # master enable
    time.sleep(0.1)

enable5V(hdwf)
send5mspulse(hdwf, 6)
send5mspulse(hdwf, 7)
#"run/trigger"
dwf.FDwfDigitalOutConfigure(hdwf, c_int(1))

#do clever measurements with relays in first position here
time.sleep(1)
dwf.FDwfDigitalOutReset(hdwf) # to clear previous instrument configuration, channels are by default in high impendance 

enable5V(hdwf)
send5mspulse(hdwf, 14)
send5mspulse(hdwf, 15)
dwf.FDwfDigitalOutConfigure(hdwf, c_int(1))

#do clever measurements with relays in second position here
time.sleep(1)
dwf.FDwfDigitalOutReset(hdwf)

dwf.FDwfDeviceCloseAll()

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...