我使用的是覆盆子PI3和ADS1115,我的项目要求我获得均匀分布的样本,以便绘图和分析。其他职位是关于实现10公里和5万个sps,但我只需要500个sps,这也不起作用。有没有办法用500个sps运行我的代码120秒,同时从A0和A1通道获得60000个样本?我已附上代码以供参考。提前谢谢
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)您正在使用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中,此时间是根据数据速率(不安全)计算的,而在最近的端口中,此时间通常在0.01秒左右,因为在配置之后转换很可能没有直接完成(请检查CircuitPython)。
4)以500SPS的速度读取大约是ADS1115所能读取的最快速度。_read method表示在860SPS时转换需要1.2ms。添加配置和读取时间后,您将无法每隔0.002s连续读取两个或多个值,即使您正在接收转换通知(如我在reference中所述),而不是等待固定的时间段。
5)我认为最接近Python的方法是在GPIO通知的连续模式下运行2个菊花链ADS1115,但我没有这方面的经验。