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
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)
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 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)
#!/usr/bin/python from ABE_ADCDACPi import ADCDACPi import time adcdac = ADCDACPi() adcdac.set_adc_refvoltage(3.3) while True: v = adcdac.read_adc_voltage(1) if (v < 2.2) : print v time.sleep(0.001)
#!/usr/bin/python from ABE_ADCDACPi import ADCDACPi import time """ ================================================ ABElectronics ADCDAC Pi 2-Channel ADC, 2-Channel DAC | ADC Read 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-adcread.py ================================================ this demo reads the voltage from channel 1 on the ADC inputs """ adcdac = ADCDACPi( 1) # create an instance of the ADCDAC Pi with a DAC gain set to 1 # set the reference voltage. this should be set to the exact voltage # measured on the raspberry pi 3.3V rail. adcdac.set_adc_refvoltage(3.3) while True: # read the voltage from channel 1 and display on the screen print adcdac.read_adc_voltage(1) time.sleep(0.5)
from ABE_ADCDACPi import ADCDACPi import time import datetime import numpy as N """ ================================================ ABElectronics ADCDAC Pi 2-Channel ADC, 2-Channel DAC | ADC Read Speed Demo Version 1.0 Created 17/05/2014 Version 1.1 16/11/2014 updated code and functions to PEP8 format Version 1.2 21/04/2017 added code to save value into a numpy array and calculate the speed run with: python demo-speed.py ================================================ this demo reads the voltage from channel 1 on the ADC inputs """ adcdac = ADCDACPi(1) # create an instance of the ADCDAC Pi with a DAC gain set to 1 # set the reference voltage. this should be set to the exact voltage # measured on the raspberry pi 3.3V rail. adcdac.set_adc_refvoltage(3.3) counter = 1 totalsamples = 100000 a = N.zeros(totalsamples) starttime = datetime.datetime.now() print ("Start: " + str(starttime)) while (counter < totalsamples): # read the voltage from channel 1 and display on the screen
#!/usr/bin/python3 from ABE_ADCDACPi import ADCDACPi import time """ ================================================ ABElectronics ADCDAC Pi 2-Channel ADC, 2-Channel DAC | ADC Read Demo Version 1.0 Created 29/02/2015 run with: python3 demo-adcread.py ================================================ this demo reads the voltage from channel 1 on the ADC inputs """ adcdac = ADCDACPi() # create an instance of ADCDACPi # set the reference voltage. this should be set to the exact voltage # measured on the raspberry pi 3.3V rail. adcdac.set_adc_refvoltage(3.3) while True: # read the voltage from channel 1 in single ended mode and display on the screen print(adcdac.read_adc_voltage(1, 0)) time.sleep(0.5)
run with: python3 demo-dacsinewave.py ================================================ # this demo uses the set_dac_raw method to generate a sine wave from a # predefined set of values """ # 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 DACLookup_FullSine_12Bit = \ [2048, 2073, 2098, 2123, 2148, 2174, 2199, 2224, 2249, 2274, 2299, 2324, 2349, 2373, 2398, 2423, 2448, 2472, 2497, 2521, 2546, 2570, 2594, 2618, 2643, 2667, 2690, 2714, 2738, 2762, 2785, 2808, 2832, 2855, 2878, 2901, 2924, 2946, 2969, 2991, 3013, 3036, 3057, 3079, 3101, 3122, 3144, 3165, 3186, 3207, 3227, 3248, 3268, 3288, 3308, 3328, 3347, 3367, 3386, 3405, 3423, 3442, 3460, 3478, 3496, 3514, 3531, 3548, 3565, 3582, 3599, 3615, 3631, 3647, 3663, 3678, 3693, 3708, 3722, 3737, 3751, 3765, 3778, 3792, 3805, 3817, 3830, 3842, 3854, 3866, 3877, 3888, 3899, 3910, 3920, 3930, 3940, 3950, 3959, 3968, 3976, 3985, 3993, 4000,
#!/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
#!/usr/bin/python3 from ABE_ADCDACPi import ADCDACPi import time """ ================================================ ABElectronics ADCDAC Pi 2-Channel ADC, 2-Channel DAC | ADC Read Demo Version 1.0 Created 29/02/2015 run with: python3 demo-adcread.py ================================================ this demo reads the voltage from channel 1 on the ADC inputs """ adcdac = ADCDACPi() # create an instance of ADCDACPi # set the reference voltage. this should be set to the exact voltage # measured on the raspberry pi 3.3V rail. adcdac.set_adc_refvoltage(3.3) while True: # read the voltage from channel 1 in single ended mode and display on the screen print (adcdac.read_adc_voltage(1, 0)) time.sleep(0.5)
#!/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( 1) # create an instance of the ADCDAC Pi with a DAC gain set to 1 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
time.sleep(0.1) qsize = self.pushsocket.queue.qsize() print qsize while qsize > 0: element = self.pushsocket.queue.get() mfc = element.keys()[0] self.mfcs[mfc].set_flow(element[mfc]) qsize = self.pushsocket.queue.qsize() for mfc in self.mfcs: flow = self.mfcs[mfc].read_flow() print(mfc + ': ' + str(flow)) self.pullsocket.set_point_now(mfc, flow) self.livesocket.set_point_now(mfc, flow) adcdac = ADCDACPi() # create an instance of ADCDACPi adcdac.set_adc_refvoltage(3.3) devices = ['1'] MFC = AnalogMFC(1, 2, 2, adcdac) MFCs = {} MFCs['1'] = MFC Datasocket = DateDataPullSocket('microreactor_mfc_control', devices, timeouts=[3.0], port=9000) Datasocket.start() Pushsocket = DataPushSocket(unichr(0x03BC) + '-reactor_analog_mfc_control', action='enqueue')
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
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)
#!/usr/bin/python from ABE_ADCDACPi import ADCDACPi import time """ ================================================ ABElectronics ADCDAC Pi 2-Channel ADC, 2-Channel DAC | ADC Read 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-adcread.py ================================================ this demo reads the voltage from channel 1 on the ADC inputs """ adcdac = ADCDACPi(1) # create an instance of the ADCDAC Pi with a DAC gain set to 1 # set the reference voltage. this should be set to the exact voltage # measured on the raspberry pi 3.3V rail. adcdac.set_adc_refvoltage(3.3) while True: # read the voltage from channel 1 and display on the screen print adcdac.read_adc_voltage(1) time.sleep(0.5)
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)
run with: python3 demo-dacsinewave.py ================================================ # this demo uses the set_dac_raw method to generate a sine wave from a # predefined set of values """ # 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 DACLookup_FullSine_12Bit = \ [2048, 2073, 2098, 2123, 2148, 2174, 2199, 2224, 2249, 2274, 2299, 2324, 2349, 2373, 2398, 2423, 2448, 2472, 2497, 2521, 2546, 2570, 2594, 2618, 2643, 2667, 2690, 2714, 2738, 2762, 2785, 2808, 2832, 2855, 2878, 2901, 2924, 2946, 2969, 2991, 3013, 3036, 3057, 3079, 3101, 3122, 3144, 3165, 3186, 3207, 3227, 3248, 3268, 3288, 3308, 3328, 3347, 3367, 3386, 3405, 3423, 3442, 3460, 3478, 3496, 3514, 3531, 3548, 3565, 3582, 3599, 3615, 3631, 3647, 3663, 3678, 3693, 3708, 3722, 3737, 3751, 3765, 3778, 3792, 3805, 3817, 3830, 3842, 3854, 3866, 3877, 3888, 3899, 3910, 3920, 3930, 3940, 3950, 3959, 3968, 3976, 3985, 3993, 4000,
import time import math """ ================================================ ABElectronics ADCDAC Pi 2-Channel ADC, 2-Channel DAC | DAC sine wave generator 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-dacsinewave.py ================================================ # this demo uses the set_dac_raw method to generate a sine wave from a # predefined set of values """ adcdac = ADCDACPi() DACLookup_FullSine_12Bit = \ [2048, 2073, 2098, 2123, 2148, 2174, 2199, 2224, 2249, 2274, 2299, 2324, 2349, 2373, 2398, 2423, 2448, 2472, 2497, 2521, 2546, 2570, 2594, 2618, 2643, 2667, 2690, 2714, 2738, 2762, 2785, 2808, 2832, 2855, 2878, 2901, 2924, 2946, 2969, 2991, 3013, 3036, 3057, 3079, 3101, 3122, 3144, 3165, 3186, 3207, 3227, 3248, 3268, 3288, 3308, 3328, 3347, 3367, 3386, 3405, 3423, 3442, 3460, 3478, 3496, 3514, 3531, 3548, 3565, 3582, 3599, 3615, 3631, 3647, 3663, 3678, 3693, 3708, 3722, 3737, 3751, 3765, 3778, 3792, 3805, 3817, 3830, 3842, 3854, 3866, 3877, 3888, 3899, 3910, 3920, 3930, 3940, 3950, 3959, 3968, 3976, 3985, 3993, 4000,