使用Sample count或time.time()在RasPi3和ADS1115中进行采样

时间:2017-08-31 04:22:23

标签: python raspberry-pi3 adc

我正在使用Raspberry Pi 3和ADS1115,我的项目要求我获得均匀间隔的样本,以便进行绘图和分析。其他帖子是关于实现10k和50k sps,但我只需要500SPS,这也不起作用。有没有办法用500 sps运行我的代码120秒,同时从A0和A1通道获得60,000个样本?我附上了代码供参考。提前致谢

from Adafruit_ADS1x15 import ADS1x15
import time
import numpy as np

pga = 2/3                  # Set full-scale range of programmable gain  
                            # amplifier (page 13 of data sheet), change 
                            #depending on the input voltage range
ADS1115 = 0x01              # Specify that the device being used is the 
                            #  ADS1115, for the ADS1015 used 0x00
adc = Adafruit_ADS1x15.ADS1015()   # Create instance of the class ADS1x15

# Function to print sampled values to the terminal
def logdata():

    print "sps value should be one of: 8, 16, 32, 64, 128, 250, 475, 860, 
    otherwise the value will default to 250"

    frequency = input("Input sampling frequency (Hz):     ")    # Get 
                                               #sampling frequency from user
    sps = input("Input sps (Hz) :     ")                        # Get 
                                           # ads1115 sps value from the user
    time1 = input("Input sample time (seconds):     ")          # Get how 
                                           #long to sample for from the user

    period = 1.0 / frequency        # Calculate sampling period

    datapoints = int(time1*frequency)       # Datapoints is the total number 
                               #of samples to take, which must be an integer

    startTime=time.time()                   # Time of first sample
    t1=startTime                            # T1 is last sample time
    t2=t1                                   # T2 is current time

    for x in range (0,datapoints) :     # Loop in which data is sampled

            while (t2-t1 < period) :        # Check if t2-t1 is less then 
                                     #sample period, if it is then update t2
                t2=time.time()              # and check again       
            t1+=period                      # Update last sample time by the 
                                            #  sampling period

            print adc.read_adc(0, pga, data_rate=sps), "mV      ", ("%.2f" % 
(t2-startTime)) , "s"      # Print sampled value and time to the terminal

# Call to logdata function
logdata()

1 个答案:

答案 0 :(得分:0)

1)您是否正在使用ADS1115?

adc = Adafruit_ADS1x15.ADS1015()   # should be adc = Adafruit_ADS1x15.ADS1115() 

2)您不能同时读取两个或多个单端通道。在差分模式下,可以比较两个通道以产生一个值。 要从通道1读取值,除了通道0外,您还必须在循环中添加另一个调用:

print adc.read_adc(0, pga, data_rate=sps) ..... # original call for channel 0
print adc.read_adc(1, pga, data_rate=sps) ..... # new call for channel 1

3)在读取值之前,必须为ADS配置多个参数,例如通道,速率,增益等。经过一段时间后(进行模/数转换),可以读取一次值,或在连续模式下一遍又一遍。 在原始的ADAFruit library中,这次是根据数据速率(不安全)计算的,而在最近的端口中,CircuitPython这次通常是在0.01 s左右,因为转换很可能不会直接完成配置后(检查_read method)。

4)以500SPS的读取速度是ADS1115可以读取的最快速度。 reference指出,以860SPS进行转换需要1.2毫秒。花费时间进行配置和读取,即使您收到转换通知(如我在homepage上所述),也不必等待固定的时间,您将无法每0.002s连续读取两个或多个值。时间。

5)我认为,使用Python可以得到的最接近的结果是,以连续模式通过GPIO通知运行2个菊花链式ADS1115,但我对此没有经验。