measure.py 3.22 KB
Newer Older
Anton Sarukhanov's avatar
Anton Sarukhanov committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import time
import os
import RPi.GPIO as GPIO
from ADCPi.ABE_ADCPi import ADCPi
from ADCPi.ABE_helpers import ABEHelpers

class Meter():
    # Config
    def __init__(self, channels=None):
        self.adc_channels = {
            1: {'internal_resistance': 2.13, 'gpio_pin': 5},
            2: {'internal_resistance': 2.48, 'gpio_pin': 6},
            3: {'internal_resistance': 2.13, 'gpio_pin': 13},
            4: {'internal_resistance': 2.25, 'gpio_pin': 19},
            5: {'internal_resistance': 2.13, 'gpio_pin': 26},
            6: {'internal_resistance': 2.13, 'gpio_pin': 12},
            8: {'internal_resistance': 2.13, 'gpio_pin': 16},
        }
        if channels:
            for ch in self.adc_channels.keys():
                if ch not in channels:
                    del self.adc_channels[ch]
        self.calibration_channel = 8
        self.repeat_measurements = 3
        self.r1 = 67.1
        self.margin_of_error_volts = 0.001
        self.max_ohms = 3000
        self.system_voltage = 5
        
        # ADCPi Setup
        self.i2c_helper = ABEHelpers()
        self.bus = self.i2c_helper.get_smbus()
        self.adc = ADCPi(self.bus, 0x68, 0x69, 14)
        
        # GPIO Setup
        GPIO.setmode(GPIO.BCM)
        for channel in self.adc_channels:
            GPIO.setup(self.adc_channels[channel]['gpio_pin'], GPIO.OUT)
        

    def measure(self):

        # Measure each channel
        measurements = {}
        for channel in self.adc_channels:
            
            # Get system voltage.
            # We do this with each channel, because it fluctuates.
            GPIO.output(self.adc_channels[self.calibration_channel]['gpio_pin'], True)
            time.sleep(.01)
            self.system_voltage = self.adc.read_voltage(self.calibration_channel)
            GPIO.output(self.adc_channels[self.calibration_channel]['gpio_pin'], False)
        
            GPIO.output(self.adc_channels[channel]['gpio_pin'], True)
    
            time.sleep(.01)
            
            voltage = 0
            for n in range(self.repeat_measurements):
                voltage += self.adc.read_voltage(channel)
            voltage = voltage / self.repeat_measurements
    
            GPIO.output(self.adc_channels[channel]['gpio_pin'], False)
    
            if voltage < self.margin_of_error_volts:
                measurements[channel] = None
                continue

            if voltage == self.system_voltage:
                measurements[channel] = (0, voltage)
                continue
    
            r2 = self.r1 * (1 / ((self.system_voltage / voltage) - 1))
            r2 -= self.adc_channels[channel]['internal_resistance']
            if r2 > self.max_ohms or r2 < 0:
                measurements[channel] = None
                continue
    
            measurements[channel] = (r2, voltage)
        
        os.system('clear')
        print "Vcc: {0:.2f} V".format(self.system_voltage)
        for channel in measurements:
            if measurements[channel] == None:
                print "Channel {0}: Not Connected".format(channel)
            else:
                (ohms,volts) = measurements[channel]
                print "Channel {0}: {1:.2f} ohms (raw: {2:.6f} V)".format(channel, ohms, volts)
        return measurements