Skip to content
ADoutputs.py 4.32 KiB
Newer Older
Martin Drechsler's avatar
Martin Drechsler committed
# -*- coding: utf-8 -*-
"""
Martin Drechsler's avatar
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's avatar
Martin Drechsler committed
"""

Martin Drechsler's avatar
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's avatar
Martin Drechsler committed

except:
    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
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')

# First of all, I check which daqs are available and print it. 
devices = ul.get_daq_device_inventory(interface_type=InterfaceType.USB)
Martin Drechsler's avatar
Martin Drechsler committed
if len(devices) == 1 :
    device = devices[0]
    print('daq %s is connected' % device.product_name)
else:
Martin Drechsler's avatar
Martin Drechsler committed
       warnings.warn('No daq connected')


def discover_device(board_num, board_name):
    """
    Checks if daq is found. 
    """
    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)
    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
       
        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)
    #methods
    if daqfound:  
        def set_out(self, value):
            try:
                ul.v_out(self.board_num, self.out_num, self.output_range, value)
            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's avatar
Martin Drechsler committed
class daq_DO(object):
    def __init__(self, out_num):
Martin Drechsler's avatar
Martin Drechsler committed
        self.board_num = 0
        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's avatar
Martin Drechsler committed
            try:
                # Output the value to the board
                ul.d_bit_out(self.board_num, self.port.type, self.out_num, bit_value)
Martin Drechsler's avatar
Martin Drechsler committed
            except ULError as e:
                self.show_ul_error(e)
            print('Digital out %i set to %s' % (self.out_num, bit_value) )