예제 #1
0
class AnalogMFC(object):
    """ Driver for controling an analog MFC (or PC) with
    an AB Electronics ADCDAC """
    def __init__(self, channel, fullrange, voltagespan):
        self.channel = channel
        self.fullrange = fullrange
        self.voltagespan = voltagespan
        self.daq = ADCDACPi()  # create an instance of ADCDACPi
        self.daq.set_adc_refvoltage(3.3)

    def read_flow(self):
        """ Read the flow (or pressure) value """
        value = 0
        for _ in range(0, 10): # Average to minimiza noise
            value += self.daq.read_adc_voltage(1)
        value = value / 10
        #print('Value: ' + str(value))
        flow = value * self.fullrange / self.voltagespan
        return flow

    def set_flow(self, flow):
        """ Set the wanted flow (or pressure) """
        voltage = flow *  self.voltagespan / self.fullrange
        print('Voltage: ' + str(voltage))
        self.daq.set_dac_voltage(1, voltage)
        return voltage
예제 #2
0
def complextriangle(period):

	period = period * 2

	#complex triangle wave
	def f1(t,period,slope):
	    t=t%period
	    if (t<=period/5 or t>=4*period/5):
		t=t%(period/15)
		if (t<=period/30):
		    output = slope * t
		else:
		    output = 1.0/3 - slope * (t-period/30)
	    elif (t<=2*period/5 or t>=3*period/5):
		t=t%(period/15)
		if(t<=period/30):
		    output = 2 * slope * t
		else:
		    output = (2.0/3) - 2* slope * (t-period/30)
	    else:
		t=t%(period/15)
		if(t<=period/30):
		    output = 3 * slope * t
		else:
		    output = 1 - 3 * slope * (t-period/30)
	    
	    return output


	t0=time.time()
	t=0
	out=0
	n=0
	i=0
	period=.5*period
	slope1 = (1.0/3)/(period/30) #calculated from the period

	#initialize DAC
	dac=ADCDACPi()
	#infinite loop
	print "Press ENTER to quit.\n"
	while(True):
	    #exit when user presses enter
	 if sys.stdin in select.select([sys.stdin],[],[],0)[0]:
	     line = raw_input()
	     break
	 #get time
	 t=time.time()-t0
	 #make signal bipolar
	 if ((t//period)%2)==1:
	     out=f1(t,period,slope1)
	     dac.set_dac_voltage(1,.55*out)
	 else:
     	     out=f1(t,period,slope1)
     	     dac.set_dac_voltage(2,.55*out)
예제 #3
0
def simpletriangle(period):
	period=period * 2

	#simple triangle wave
	def f(t,period,slope):
	    t=t%(period/3)
	    if t<=period/6:
		output = slope * t
	    else:
		output = 1 - slope * (t-period/6)
	    return output

	t0=time.time()
	t=0
	out=0
	n=0
	i=0
	period=.5*period
	slope2 = 1.0/(period/6)

	#initialize DAC
	dac=ADCDACPi()
	#infinite loop
	print "Press ENTER to quit.\n"
	while(True):
	    #exit when user presses enter
	 if sys.stdin in select.select([sys.stdin],[],[],0)[0]:
	     line = raw_input()
	     break
	 #get time
	 t=time.time()-t0
	 #make signal bipolar
	 if ((t//period)%2)==1:
	     out=f(t,period,slope2)
	     dac.set_dac_voltage(1,.55*out)
	 else:
	     out=f(t,period,slope2)
	     dac.set_dac_voltage(2,.55*out)
예제 #4
0
#!/usr/bin/python

from ABE_ADCDACPi import ADCDACPi
import time

"""
================================================
ABElectronics ADCDAC Pi 2-Channel ADC, 2-Channel DAC | DAC Write Demo
Version 1.0 Created 17/05/2014
Version 1.1 16/11/2014 updated code and functions to PEP8 format
run with: python demo-dacwrite.py
================================================

this demo will generate a 1.5V p-p square wave at 1Hz
"""

adcdac = ADCDACPi()

while True:
    adcdac.set_dac_voltage(1, 1.5)  # set the voltage on channel 1 to 1.5V
    time.sleep(0.5)  # wait 0.5 seconds
    adcdac.set_dac_voltage(1, 0)  # set the voltage on channel 1 to 0V
    time.sleep(0.5)  # wait 0.5 seconds
예제 #5
0
# set voltage to X volts
start = float(raw_input("Enter a starting voltage below 0.4 volts, then press [Enter]"))
while startV_flag == 1:
	if start > 0.4:
		start = float(raw_input("Incorrect, please enter a starting voltage below 0.4 volts, then press [Enter]"))
	elif start < 0.0:
		start = float(raw_input("Incorrect, please enter a positive value, then press [Enter]")) 
	else:
		startV_flag = 0

high = float(raw_input("Enter a max voltage up to 0.4 volts, then press [Enter]"))
while maxV_flag == 1:
	if high > 0.4:
		high = float(raw_input("Incorrect, please enter a max voltage up to 0.4 volts, then press [Enter]"))
	elif high < 0.0:
		high = float(raw_input("Incorrect, please enter a positive value, then press [Enter]")) 
	else:
		maxV_flag = 0
# start test
raw_input("Press [Enter] to begin the test")
adcdac.set_dac_voltage(1, start)  # set the voltage on channel 1 to 1.5V
# set to max voltage
raw_input("press [Enter] to set DAC %0.3f volts" %high)
adcdac.set_dac_voltage(1, high)  # set the voltage on channel 1 to 1.5V
# back to start voltage
raw_input("press [Enter] to set DAC back to %0.3f volts" %start)
adcdac.set_dac_voltage(1, start)  # set the voltage on channel 1 to 1.5V
# set to Zero volts
raw_input("press [Enter] to set DAC to 0 volts")
adcdac.set_dac_voltage(1, 0)    
예제 #6
0
Version 1.1 16/11/2014 updated code and functions to PEP8 format
run with: python demo-dacwrite.py
================================================

this demo will generate a 1.5V p-p square wave at 1Hz
"""

# The ADCDAC Pi uses GPIO pin 22 to control the DAC.  
# This will need to be turned off for the DAC to operate correctly.

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(22, GPIO.OUT)
GPIO.output(22, False)

adcdac = ADCDACPi(1) # create an instance of the ADCDAC Pi with a DAC gain set to 1

# set voltage to X volts
adcdac.set_dac_voltage(1, 0.401)  # set the voltage on channel 1 to 1.5V

raw_input("press [Enter] to set DAC back to 0 volts")

adcdac.set_dac_voltage(1, 0)    

# toggle
#while True:
#    adcdac.set_dac_voltage(1, 1.5)  # set the voltage on channel 1 to 1.5V
#    time.sleep(0.5)  # wait 0.5 seconds
#    adcdac.set_dac_voltage(1, 0)  # set the voltage on channel 1 to 0V
#    time.sleep(0.5)  # wait 0.5 seconds
예제 #7
0
class DetectPi:
    def __init__(self):
        '''
		Initialise ADC-DAC Pi. The DAC gain factor is set to 1, which allows 
		the DAC output voltage to be set between 0 and 2.048 V. The DAC gain 
		factor may also be set to 2, which gives an output range of 0 to 3.3 V.
		'''

        # Create instance of ADCDACPi, with gain set to 1
        self.adcdac = ADCDACPi(1)

        # Set reference voltage to 3.3 V
        self.adcdac.set_adc_refvoltage(3.3)

        return

    def getDetails(self):
        '''
		Return details of ADC_DAC Pi. This function does not currently return 
		any details, but this may change later.
		'''

        return

    def writePort(self, Port, Volt):
        '''
		Write values to a DAC pin. Values may be written to either channel 1 
		or channel 2. The maximum voltage is specified by the gain factor.
		Note: Setting a voltage of 5 V will return an error for exceeding 
		the maximum voltage.
		'''

        # Ensure port is of type list
        if type(Port) == str:
            Port = [Port]

        # Convert DAQT7 DAC ports to DAC Pi channels
        if "DAC0" in Port:
            channel = 1
        elif "DAC1" in Port:
            channel = 2

        # Set DAC output voltage <Volt> on channel <channel>
        self.adcdac.set_dac_voltage(channel, Volt)

        return

    def readPort(self, Port):
        '''
		Read values from an ADC pin. Values may be read from either channel 1 
		or channel 2. 
		'''

        # Ensure port is of type list
        if type(Port) == str:
            Port = [Port]

        # Convert DAQT7 AIN ports to ADC Pi channels
        if "AIN0" in Port:
            channel = 1
        elif "AIN1" in Port:
            channel = 2

        # Read voltage from channel <channel> in single ended mode
        voltRead = self.adcdac.read_adc_voltage(channel, 0)

        return np.float(voltRead), time.time()

    def streamRead(self, scanRate, scansPerRead, Port):
        '''
		Read analogue input values from an ADC pin, in stream mode.
		'''

        # Ensure port is of type list
        if type(Port) == str:
            Port = [Port]

        # Initialise array and time values
        Read = [0, 1, 2]
        StartingMoment = 0
        FinishingMoment = 0
        scansPerRead = int(scansPerRead)

        # Determine timing characteristics
        duration = scansPerRead / float(scanRate)
        dt = 1 / float(scanRate)
        StartingMoment = time.time()

        # Allow for alternation between multiple ports
        portIndex = 0
        portLength = len(Port)

        # Loop for the duration
        while (time.time() - StartingMoment) < duration:
            # Read the ADC value and append to an array
            voltRead = self.readPort(Port[portIndex])[0]
            Read[0].append(voltRead)
            portIndex = (portIndex + 1) % portLength

            # Wait for the program to run at the correct frequency
            lastReadTime = readTime
            readTime = time.time()
            if readTime - lastReadTime < dt:
                time.sleep(dt - readTime + lastReadTime)

        # Calculate and print elapsed time
        FinishingMoment = time.time()
        print('Elapsed time %f seconds' % (FinishingMoment - StartingMoment))

        return Read, StartingMoment, FinishingMoment

    def close(self):
        '''
		Close ADC-DAC Pi.
		'''

        pass
예제 #8
0
def main(args):
	user_input = sys.argv
	startV = float(user_input[1])
	maxV = float(user_input[2])
	# lowV = float(user_input[3])
	# endV = float(user_input[4])

	print("User has typed in : %s" %user_input)
	print("Test starts at %.3f volts" %startV)
	print("Test peaks at %.3f volts" %maxV)

	# initialize
	adcdac = ADCDACPi(1) # create an instance of the ADCDAC Pi with a DAC gain set to 1
	adcdac.set_adc_refvoltage(3.3) # powered off 3.3V GPIO rail

	# TODO: add in sample number
	csv_filename, test_csv = open_csv(1)

	# Hardware settings/references
	shunt_resistor = 10 # 10 Ohms
		# High-Side Current Monitor IC
	gain = 500
		# DAC Vref = 2.048v internal
		# ADC Vref = Vdd = 3.3v

	# start test
	raw_input("Press [Enter] to view Conditions")
	adcdac.set_dac_voltage(1, startV)  # set the voltage on channel 1
	voltage = startV
	# 1mV increments
		# init = int(startV*1000)
		# peak = int(maxV*1000)
	# 10mV increments
	init = int(startV*100)
	peak = int(maxV*100)
	count = 0
	print("START is %0.3f VOLTS" %voltage)
	print("HIGH is %0.3f VOLTS" %maxV)
	print("INIT is %0.3f UNITS" %init)
	print("PEAK is %0.3f UNITS" %peak)
	raw_input("Press [Enter] to begin the test")

	# ramp up
	for i in range (init, peak, 1):
		adcdac.set_dac_voltage(1, voltage)
		# read channel 1 in single ended mode
		voltage_reading = adcdac.read_adc_voltage(1, 0)
		#print("%0.3f counts" %i)
		# Vdrop @ shunt = voltage_reading/gain
		# Current = Vdrop/Rshunt
		calc_current = (voltage_reading/gain)/shunt_resistor
		print("%.3f volts driven, %.3f volts seen" %(voltage, voltage_reading))
		print("Calculated current is %.5f mA \n" %(calc_current*1000))
		#save test results
		count = count + 1
		test_csv.write("%d, %.3f, %.3f, %s\n" %(count, voltage, voltage_reading, calc_current))
		voltage = voltage+0.01
		time.sleep(0.040)
	# ramp down
	for i in range (peak, init-1, -1):
		adcdac.set_dac_voltage(1, voltage)
		# read channel 1 in single ended mode
		voltage_reading = adcdac.read_adc_voltage(1, 0)
		#print("%0.3f counts" %i)
		calc_current = (voltage_reading/gain)/shunt_resistor
		print("%.3f volts driven, %.3f volts seen" %(voltage, voltage_reading))
		print("Calculated current is %.5f mA \n" %(calc_current*1000))
		#save test results
		count = count + 1
		test_csv.write("%d, %.3f, %.3f, %s\n" %(count, voltage, voltage_reading, calc_current))
		voltage = voltage-0.01
		time.sleep(0.040)  

	raw_input("press [Enter] to set DAC to 0 volts")
	adcdac.set_dac_voltage(1, 0)    
	test_csv.close()
	upload_file(csv_filename)