Esempio n. 1
0
class Device_ABE_DeltaSigmaPiADC:
    """
    https://github.com/abelectronicsuk/ABElectronics_Python_Libraries
    """
    def __init__(self,
                 bus=None,
                 address=None,
                 address2=None,
                 samplerate=None,
                 gain=None):
        from ABE_helpers import ABEHelpers
        from ABE_ADCDifferentialPi import ADCDifferentialPi

        if bus is None:
            i2c_helper = ABEHelpers()
            bus = i2c_helper.get_smbus()

        # Initialize the ADC device using the default addresses and sample rate 18.
        # Sample rate can be 12, 14, 16 or 18.
        self.adc = ADCDifferentialPi(bus,
                                     address=address,
                                     address2=address2,
                                     rate=samplerate)

        # Set amplifier gain to 8.
        if gain is not None:
            self.adc.set_pga(gain)

    def read(self, channel):
        # Returns the voltage from the selected ADC channel - channels 1 to 8.
        return self.adc.read_voltage(channel)
Esempio n. 2
0
def hallSen():
    # GPIO Setup
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(7, GPIO.OUT)

    text_file = open("hallSensor_Data.txt", "w")
    initTime = time.time()

    t1 = 0
    t2 = 0
    flag = 0
    diameter = 14  # need to find the actual diameter !!!

    i2c_helper = ABEHelpers()
    bus = i2c_helper.get_smbus()
    adc = ADCDifferentialPi(bus, i2cAddr1, i2cAddr2, bitRate)

    SevenSeg.initDisplay()

    while (True):
        voltage = adc.read_voltage(
            channel
        )  # voltage output from hall sensor (either a pos. or neg. value)
        curTime = time.time(
        ) - initTime  # calculates the current time from time acc. minus time started with

        if voltage < 0.0 and flag == 0:  # if voltage is neg. and we have not passed by the magnet yet
            flag = 1
            t1 = t2  # stores the previous curent time from curTime
            t2 = curTime  # stores the current time in curTime
            rps = 1 / (t2 - t1)  # revolutions per second
            mph = rps * (math.pi) * diameter * (
                3600 / 63360)  # conversion from rps to miles per second
            #mph = rps*60

            #print( str(curTime) + "," + str(voltage) + "," + str(mph) )
            #text_file.write( str(curTime) + "," + str(voltage) + "," + str(mph) + "\n" )

        elif voltage < 0.0 and flag == 1:  # else if voltage is neg. but we are currently still passing by the magnet
            #print( str(curTime) + "," + str(voltage) )
            #text_file.write( str(curTime) + "," + str(voltage) + "\n" )
            temp = 1

        else:  # else the voltage is positive (meaning the magnet is not next the the hall sensor)
            flag = 0
            #print( str(curTime) + "," + str(voltage) )
            #text_file.write( str(curTime) + "," + str(voltage) + "\n" )

        SevenSeg.displayNum(int(mph))

        #time.sleep( 0.0100 )

    text_file.close()
Esempio n. 3
0
	def run(self):
		global mph
		global flag
		t1 = 0
		t2 = 0
		flagMine = 0
		diameter = 14     # need to find the actual diameter !!!

		i2c_helper = ABEHelpers()
		bus = i2c_helper.get_smbus()
		adc = ADCDifferentialPi(bus, i2cAddr1, i2cAddr2, bitRate)
		while True:
			c.acquire()
			if flag == 1:
				flag = 0
				voltage = adc.read_voltage( channel )               # voltage output from hall sensor (either a pos. or neg. value)
				curTime = time.time() - initTime                    # calculates the current time from time acc. minus time started with

				if voltage < 0.0 and flagMine == 0:                     # if voltage is neg. and we have not passed by the magnet yet
					flagMine = 1
					t1 = t2                                             # stores the previous curent time from curTime
					t2 = curTime                                        # stores the current time in curTime
					rps = 1/(t2 - t1)                                   # revolutions per second
					mph = rps * (math.pi) * diameter * (3600/63360)     # conversion from rps to miles per second
					#mph = rps*60

					#print( str(curTime) + "," + str(voltage) + "," + str(mph) )
					#text_file.write( str(curTime) + "," + str(voltage) + "," + str(mph) + "\n" )

				elif voltage < 0.0 and flagMine == 1:                   # else if voltage is neg. but we are currently still passing by the magnet
					#print( str(curTime) + "," + str(voltage) )
					#text_file.write( str(curTime) + "," + str(voltage) + "\n" )
					temp = 1

				else:                                               # else the voltage is positive (meaning the magnet is not next the the hall sensor)
					flagMine = 0
					#print( str(curTime) + "," + str(voltage) )
					#text_file.write( str(curTime) + "," + str(voltage) + "\n" )

				#mph = mph + 1
				c.notify_all()
			else:
				c.wait()
			c.release()
		text_file.close()	
Esempio n. 4
0
def StrainGauge():
    text_file = open("strainGuage_Data.txt", "w")

    i2c_helper = ABEHelpers()
    bus = i2c_helper.get_smbus()
    adc = ADCDifferentialPi(bus, i2cAddr1, i2cAddr2, bitRate)

    adc.set_pga(
        1)  # Set the gain of the PDA on the chip (Parameters: gain - 1,2,4,8)

    prev_voltage = 0

    for i in range(10000):
        voltage = adc.read_voltage(channel)  # voltage output from strain Gauge

        changeInVoltage = voltage - prev_voltage
        prev_voltage = voltage

        #print( str(changeInVoltage * 1000) )

        text_file.write(str(voltage) + "\n")
# Constants
t0 = 273.15;
t25 = t0 + 25;

def calcResistance(voltage):
    return (resistor2*resistor3 + resistor3* (resistor1+resistor2)*voltage / voltin )/ (resistor1- (resistor1+resistor2)*voltage / voltin)

def calcTemp(resistance):
    return 1 / ( (math.log(resistance / t25Resistance) / bResistance) + (1 / t25) ) - t0;

# loop forever reading the values and printing them to screen
while (True):
    # read from adc channels and print to screen
   
    bridgeVoltage =  adc.read_voltage(1)
    thermresistance = calcResistance(bridgeVoltage)
    temperature = calcTemp(thermresistance)
    
    # clear the console
    os.system('clear')
    # print values to screen
    
    print ("Bridge Voltage: %02f volts" % bridgeVoltage)
    print ("Resistance: %d ohms" % thermresistance)
    print ("Temperature: %.2fC" % temperature)
    
	
   

    # wait 0.5 seconds before reading the pins again

def calcResistance(voltage):
    return (resistor2 * resistor3 + resistor3 *
            (resistor1 + resistor2) * voltage / voltin) / (
                resistor1 - (resistor1 + resistor2) * voltage / voltin)


def calcTemp(resistance):
    return 1 / ((math.log(resistance / t25Resistance) / bResistance) +
                (1 / t25)) - t0


# loop forever reading the values and printing them to screen
while (True):
    # read from adc channels and print to screen

    bridgeVoltage = adc.read_voltage(1)
    thermresistance = calcResistance(bridgeVoltage)
    temperature = calcTemp(thermresistance)

    # clear the console
    os.system('clear')
    # print values to screen

    print("Bridge Voltage: %02f volts" % bridgeVoltage)
    print("Resistance: %d ohms" % thermresistance)
    print("Temperature: %.2fC" % temperature)

    # wait 0.5 seconds before reading the pins again
    time.sleep(0.5)

def formatHouseCurrent(val):
    return val * 19.5


# Main program loop

while (True):

    # clear the console
    os.system('clear')

    # read from adc channels and save to variables

    varPVAmps = formatInverterCurrent(adc.read_voltage(1))
    varDCAmps = formatHouseCurrent(adc.read_voltage(2))
    varDCVolts = formatBatteryVoltage(adc.read_voltage(3))

    # print ("Inverter: %.2f" % varPVAmps)
    # print ("Home: %.2f" % varDCAmps)
    # print ("Battery Voltage: %.2f V" % varDCVolts)
    # print ("System Current: %.2f A" % adc.read_voltage(4))

    # print("varTempHome1: %.2f" % varTempHome1)
    # print("varTempHome2: %.2f" % varTempHome2)
    # print("varTempHome3: %.2f" % varTempHome3)

    # print("varTempBase: %.2f" % varTempBase)
    # print("varTempTop: %.2f" % varTempTop)
    # print("varTempCollector: %.2f" % varTempCollector)
# setup these static values when the sensor is not moving
xStatic = 0
yStatic = 0
zStatic = 0

xMax = 0
xMin = 0
yMax = 0
yMin = 0
zMax = 0
zMin = 0

# get 50 samples from each adc channel and use that to get an average value for 0G.  
# Keep the accelerometer still while this part of the code is running
for x in range(0, 50):
    xStatic = xStatic + adc.read_voltage(1)
    yStatic = yStatic + adc.read_voltage(2)
    zStatic = zStatic + adc.read_voltage(3)		
xStatic = (xStatic / 50) * conversionfactor
yStatic = (yStatic / 50) * conversionfactor
zStatic = (zStatic / 50) * conversionfactor
	
# loop forever reading the values and printing them to screen
while (True):
    # read from adc channels and print to screen

    xVoltage =  (adc.read_voltage(1) * conversionfactor) - xStatic
    yVoltage = (adc.read_voltage(2) * conversionfactor) - yStatic
    zVoltage = (adc.read_voltage(3) * conversionfactor) - zStatic
    
    xForce = xVoltage / 0.3
this value if you have changed the address selection jumpers

Sample rate can be 12,14, 16 or 18
"""


i2c_helper = ABEHelpers()
bus = i2c_helper.get_smbus()
adc = ADCDifferentialPi(bus, 0x68, 0x69, 18)


def writetofile(texttowrtite):
    f = open('adclog.txt', 'a')
    f.write(str(datetime.datetime.now()) + " " + texttowrtite)
    f.closed

while (True):

    # read from adc channels and write to the log file
    writetofile("Channel 1: %02f\n" % adc.read_voltage(1))
    writetofile("Channel 2: %02f\n" % adc.read_voltage(2))
    writetofile("Channel 3: %02f\n" % adc.read_voltage(3))
    writetofile("Channel 4: %02f\n" % adc.read_voltage(4))
    writetofile("Channel 5: %02f\n" % adc.read_voltage(5))
    writetofile("Channel 6: %02f\n" % adc.read_voltage(6))
    writetofile("Channel 7: %02f\n" % adc.read_voltage(7))
    writetofile("Channel 8: %02f\n" % adc.read_voltage(8))

    # wait 1 second before reading the pins again
    time.sleep(1)
Esempio n. 10
0
Requires python smbus to be installed
run with: python demo-read_voltage.py
================================================


Initialise the ADC device using the default addresses and sample rate, change this value if you have changed the address selection jumpers
Sample rate can be 12,14, 16 or 18
"""

i2c_helper = ABEHelpers()
bus = i2c_helper.get_smbus()
adc = ADCDifferentialPi(bus, 0x68, 0x69, 18)

while (True):

    # clear the console
    os.system('clear')

    # read from adc channels and print to screen
    print("Channel 1: %02f" % adc.read_voltage(1))
    print("Channel 2: %02f" % adc.read_voltage(2))
    print("Channel 3: %02f" % adc.read_voltage(3))
    print("Channel 4: %02f" % adc.read_voltage(4))
    print("Channel 5: %02f" % adc.read_voltage(5))
    print("Channel 6: %02f" % adc.read_voltage(6))
    print("Channel 7: %02f" % adc.read_voltage(7))
    print("Channel 8: %02f" % adc.read_voltage(8))

    # wait 0.5 seconds before reading the pins again
    time.sleep(0.5)
Esempio n. 11
0
def hallSen():
    # ADC Setup
    channel = 1  # analog channel number (1-8 inclusive)
    bitRate = 12  # programmable data rate
    # 18 (3.75 SPS) less per sec. but more accurate
    # 16 (  15 SPS) --
    # 14 (  60 SPS) --
    # 12 ( 240 SPS) more per sec. but less accurate

    i2cAddr1 = 0x68  # these addresses are associated with the Address Config. 1
    i2cAddr2 = 0x69

    text_file = open("hallSensor_Data_1.txt", "w")
    initTime = time.time()

    text_file.write(str(globalValue))
    text_file.flush()

    global t1
    global t2
    flag = 0
    diameter = 14  # need to find the actual diameter !!!

    i2c_helper = ABEHelpers()
    bus = i2c_helper.get_smbus()
    adc = ADCDifferentialPi(bus, i2cAddr1, i2cAddr2, bitRate)

    global mph
    global flagThread
    global killH

    while True:
        if killH == True:
            text_file.close()
            time.sleep(10)
        else:
            voltage = adc.read_voltage(
                channel
            )  # voltage output from hall sensor (either a pos. or neg. value)
            curTime = time.time(
            ) - initTime  # calculates the current time from time acc. minus time started with

            if voltage < 0.0 and flag == 0:  # if voltage is neg. and we have not passed by the magnet yet
                flag = 1
                t1 = t2  # stores the previous curent time from curTime
                t2 = curTime  # stores the current time in curTime
                rps = 1 / (t2 - t1)  # revolutions per second
                flagThread = 1
                mph = rps * (math.pi) * diameter * (
                    3600 / 63360)  # conversion from rps to miles per second
                rpm = rps * 60
                flagThread = 0
                #mph = rps*60

                #print( str(curTime) + "," + str(voltage) + "," + str(mph) )
                text_file.write(str(curTime) + "," + str(rpm) + "\n")
                text_file.flush()

            elif voltage < 0.0 and flag == 1:  # else if voltage is neg. but we are currently still passing by the magnet
                #print( str(curTime) + "," + str(voltage) )
                #text_file.write( str(curTime) + "," + str(voltage) + "\n" )
                filler = 0

            else:  # else the voltage is positive (meaning the magnet is not next the the hall sensor)
                flag = 0
                #print( str(curTime) + "," + str(voltage) )
                #text_file.write( str(curTime) + "," + str(voltage) + "\n" )

                #time.sleep( 0.0100 )

    text_file.close()
Esempio n. 12
0
def hallSen(hallSensor_Num, adcChannel_Num, adcBitRate_Num, i2cAddr1_Num,
            i2cAddr2_Num, outputFile_Name, diameter_Num, hallSensorActive):
    # ADC Setup
    channel = 1  # analog channel number (1-8 inclusive)
    bitRate = 12  # programmable data rate
    # 18 (3.75 SPS) less per sec. but more
    # 16 (  15 SPS) --
    # 14 (	60 SPS) --
    # 12 ( 240 SPS) more per sec. but less accurate

    i2cAddr1 = 0x68  # these address are associated with the Address Config. 1
    i2cAddr2 = 0x69

    text_file = open("hallSensor_Data1.txt", "w")
    initTime = time.time()

    t1 = 0
    t2 = 0

    flag = 0
    diameter = 14  # need to find the actual diameter !!!

    i2c_helper = ABEHelpers()
    bus = i2c_helper.get_smbus()
    adc = ADCDifferentialPi(bus, i2cAddr1, i2cAddr2, bitRate)

    global activeMphValues
    global flagThread

    global activeHallSensors
    activeHallSensors.append(hallSensorActive)

    mph = 0
    activeMphValues.append(mph)
    #activeMphValues[hallSensor_Num-1] = mph

    while activeHallSensors[hallSensor_Num - 1]:
        voltage = adc.read_voltage(
            channel
        )  # voltage output from hall sensor (either a pos. or neg. value)
        curTime = time.time(
        ) - initTime  # calculates the current time from time acc. minus time started with

        if voltage < 0.0 and flag == 0:  # if voltage is neg. and we have not passed by the magnet yet
            flag = 1
            t1 = t2  # stores the previous current time from curTime
            t2 = curTime  # stores the current time in curTime
            rps = 1 / (t2 - t1)  # revolutions per second
            activeMphValues[
                hallSensor_Num -
                1] = (rps * (math.pi) * diameter * (3600 / 63360)
                      ) / 11.5  # conversion from rps to miles per second
            #temp = rps * (math.pi) * diameter * (3600/63360)
            rpm = rps * 60
            #activeMphValues[hallsensor_Num-1] = rpm
            #mph = rps*60

            #print( str(curTime + "," + str(voltage) + "," + str(mph) )
            text_file.write(str(curTime) + "," + str(rpm) + "\n")
            text_file.flush()

        elif voltage < 0.0 and flag == 1:  # else if voltage is neg. but we are currently still passing by the magnet
            #print( str(curTime) + "," + str(voltage) )
            #text_file.write( str(curTime) + "," str(voltage) + "\n" )
            filler = 0

        else:  # else the voltage is positive (meaning the magnet is not next to the hall sensor)
            flag = 0
            #print( str(curTime) + "," + str(voltage) )
            #text_file.write( str(curTime) + "," + str(voltage) + "\n" )
            flag = 0  # added hopefully no problems, if problems comment this out

        #time.sleep( 0.0100 )

    text_file.close()
Esempio n. 13
0
this value if you have changed the address selection jumpers

Sample rate can be 12,14, 16 or 18
"""

i2c_helper = ABEHelpers()
bus = i2c_helper.get_smbus()
adc = ADCDifferentialPi(bus, 0x68, 0x69, 18)


def writetofile(texttowrtite):
    f = open('adclog.txt', 'a')
    f.write(str(datetime.datetime.now()) + " " + texttowrtite)
    f.closed


while (True):

    # read from adc channels and write to the log file
    writetofile("Channel 1: %02f\n" % adc.read_voltage(1))
    writetofile("Channel 2: %02f\n" % adc.read_voltage(2))
    writetofile("Channel 3: %02f\n" % adc.read_voltage(3))
    writetofile("Channel 4: %02f\n" % adc.read_voltage(4))
    writetofile("Channel 5: %02f\n" % adc.read_voltage(5))
    writetofile("Channel 6: %02f\n" % adc.read_voltage(6))
    writetofile("Channel 7: %02f\n" % adc.read_voltage(7))
    writetofile("Channel 8: %02f\n" % adc.read_voltage(8))

    # wait 1 second before reading the pins again
    time.sleep(1)
Requires python smbus to be installed
run with: python demo-read_voltage.py
================================================


Initialise the ADC device using the default addresses and sample rate, change this value if you have changed the address selection jumpers
Sample rate can be 12,14, 16 or 18
"""

i2c_helper = ABEHelpers()
bus = i2c_helper.get_smbus()
adc = ADCDifferentialPi(bus, 0x68, 0x69, 18)

while (True):

    # clear the console
    os.system('clear')

    # read from adc channels and print to screen
    print ("Channel 1: %02f" % adc.read_voltage(1))
    print ("Channel 2: %02f" % adc.read_voltage(2))
    print ("Channel 3: %02f" % adc.read_voltage(3))
    print ("Channel 4: %02f" % adc.read_voltage(4))
    print ("Channel 5: %02f" % adc.read_voltage(5))
    print ("Channel 6: %02f" % adc.read_voltage(6))
    print ("Channel 7: %02f" % adc.read_voltage(7))
    print ("Channel 8: %02f" % adc.read_voltage(8))

    # wait 0.5 seconds before reading the pins again
    time.sleep(0.5)
Esempio n. 15
0
    
def formatHouseCurrent(val):
    return val * 19.5
  
			
# Main program loop

while (True):

    # clear the console
    os.system('clear')


    # read from adc channels and save to variables
    
    varPVAmps = formatInverterCurrent(adc.read_voltage(1))
    varDCAmps =  formatHouseCurrent(adc.read_voltage(2))
    varDCVolts = formatBatteryVoltage(adc.read_voltage(3))
    
    # print ("Inverter: %.2f" % varPVAmps)
    # print ("Home: %.2f" % varDCAmps)
    # print ("Battery Voltage: %.2f V" % varDCVolts)
    # print ("System Current: %.2f A" % adc.read_voltage(4))
    
    # print("varTempHome1: %.2f" % varTempHome1)
    # print("varTempHome2: %.2f" % varTempHome2)
    # print("varTempHome3: %.2f" % varTempHome3)
    
    # print("varTempBase: %.2f" % varTempBase)
    # print("varTempTop: %.2f" % varTempTop)
    # print("varTempCollector: %.2f" % varTempCollector)