Newer
Older
Martin Drechsler
committed
Created on Thu Oct 25 15:35:49 2018
Martin Drechsler
committed
@author: Usuario
For now, this will work with one measruement computing daq. It is expected to be the USB-3105 and
the board number will be 0.
If the daq is not found, whenever trying to send signals, the command will be printed in the console.
Martin Drechsler
committed
Martin Drechsler
committed
import warnings
Martin Drechsler
committed
try:
from mcculw import ul
from mcculw.enums import InterfaceType, ULRange, InfoType, BoardInfo, DigitalIODirection
from mcculw.ul import ULError
from props.digital import DigitalProps
from props.ao import AnalogOutputProps
Martin Drechsler
committed
warnings.warn('Problem importing ul from mcculw, Thus might happen in Linux')
import uldaq as ul
from uldaq import InterfaceType
from uldaq import Range as ULRange
Martin Drechsler
committed
Martin Drechsler
committed
try:
ul.ignore_instacal() #instacal will be ignored
except NameError:
print('Instacall not ignored because mcculw not imported')
except AttributeError:
print('Instacall not ignored because you imported uldaq and there is no instacal in that case')
Martin Drechsler
committed
# First of all, I check which daqs are available and print it.
devices = ul.get_daq_device_inventory(interface_type=InterfaceType.USB)
Martin Drechsler
committed
print('daq %s is connected' % device.product_name)
else:
Martin Drechsler
committed
Martin Drechsler
committed
def discover_device(board_num, board_name):
"""
Checks if daq is found.
"""
Martin Drechsler
committed
Martin Drechsler
committed
devices = ul.get_daq_device_inventory(InterfaceType.USB) #get device inventory
device = next((device for device in devices
if board_name in device.product_name), None)
Martin Drechsler
committed
Martin Drechsler
committed
if device != None: #if it is found,
try:
ul.create_daq_device(board_num, device) #create an instance
except ULError:
ul.release_daq_device(board_num)
ul.create_daq_device(board_num, device)
warnings.warn('The board number %i was already in use, and it was released and redefined ' % board_num);
finally:
return True
return False
daqfound = discover_device(0, 'USB-3105')
class daq_AO(object):
def __init__(self, out_num, board_num = 0, output_range = ULRange.BIP10VOLTS):
global daqfound
self.out_num = out_num
self.board_num = board_num
self.output_range = output_range
Martin Drechsler
committed
self.current_value = None
Martin Drechsler
committed
if daqfound:
ul.set_config(InfoType.BOARDINFO, self.board_num, self.out_num, BoardInfo.DACRANGE, self.output_range)
self.ao_props = AnalogOutputProps(self.board_num)
Martin Drechsler
committed
Martin Drechsler
committed
#methods
if daqfound:
def set_out(self, value):
try:
ul.v_out(self.board_num, self.out_num, self.output_range, value)
Martin Drechsler
committed
self.current_value = value
Martin Drechsler
committed
except ULError as e:
self.show_ul_error(e)
else:
def set_out(self, value):
print('Analog out %i set to %f' % (self.out_num, value) )
Martin Drechsler
committed
self.current_value = value
Martin Drechsler
committed
return value, self.out_num
Martin Drechsler
committed
global daqfound
Martin Drechsler
committed
Martin Drechsler
committed
self.out_num = out_num
if daqfound:
self.digital_props = DigitalProps(self.board_num)
# Find the first port that supports output, defaulting to None
# if one is not found.
self.port = next(
(port for port in self.digital_props.port_info
if port.supports_output), None)
# If the port is configurable, configure it for output
if self.port != None and self.port.is_port_configurable:
try:
ul.d_config_port(
self.board_num, self.port.type, DigitalIODirection.OUT)
except ULError as e:
self.show_ul_error(e)
Martin Drechsler
committed
Martin Drechsler
committed
if daqfound:
def set_out(self, bit_value):
Martin Drechsler
committed
# Output the value to the board
ul.d_bit_out(self.board_num, self.port.type, self.out_num, bit_value)
Martin Drechsler
committed
else:
def set_out(self, bit_value):
print('Digital out %i set to %s' % (self.out_num, bit_value) )
Martin Drechsler
committed
return bit_value, self.out_num