Python 使用采样计数或time.time()在RasPi3和ADS1115中采样
我使用的是Raspberry Pi 3和ADS1115,我的项目要求我获得均匀分布的样本,以便绘图和分析。其他帖子是关于实现10k和50k SP,但我只需要500SP,这也不起作用。有没有办法用500个SP运行我的代码120秒,最终同时从A0和A1通道获得60000个样本?我已附上代码以供参考。提前谢谢Python 使用采样计数或time.time()在RasPi3和ADS1115中采样,python,raspberry-pi3,adc,Python,Raspberry Pi3,Adc,我使用的是Raspberry Pi 3和ADS1115,我的项目要求我获得均匀分布的样本,以便绘图和分析。其他帖子是关于实现10k和50k SP,但我只需要500SP,这也不起作用。有没有办法用500个SP运行我的代码120秒,最终同时从A0和A1通道获得60000个样本?我已附上代码以供参考。提前谢谢 from Adafruit_ADS1x15 import ADS1x15 import time import numpy as np pga = 2/3 #
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()
从Adafruit_ADS1x15导入ADS1x15
导入时间
将numpy作为np导入
pga=2/3#设置可编程增益的满标度范围
#放大器(数据表第13页),更改
#取决于输入电压范围
ADS1115=0x01#指定正在使用的设备是
#ADS1115,对于使用0x00的ADS1015
adc=Adafruit_ADS1x15.ADS1015()#创建类ADS1x15的实例
#函数将采样值打印到终端
def logdata():
打印“sps值应为以下值之一:8、16、32、64、128、250、475、860、,
否则,该值将默认为250“
频率=输入(“输入采样频率(Hz):”)#Get
#来自用户的采样频率
sps=输入(“输入sps(Hz):”)#获取
#来自用户的ads1115 sps值
time1=输入(“输入采样时间(秒):”)#如何获取
#长时间从用户处取样
周期=1.0/频率#计算采样周期
datapoints=int(time1*frequency)#datapoints是总数
#要采集的样本数,必须为整数
startTime=time.time()#第一个样本的时间
t1=开始时间#t1是最后一次采样时间
t2=t1#t2为当前时间
对于范围内的x(0,数据点):#数据采样的循环
而(t2-t1<周期):#检查t2-t1是否小于
#采样周期,如果是,则更新t2
t2=time.time()#并再次检查
t1+=周期#通过
#采样周期
打印adc.read_adc(0,pga,数据速率=sps),“mV”,(“%.2f”%
(t2 startTime)),“s”#将采样值和时间打印到终端
#调用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必须配置多个参数,如通道、速率、增益等。经过一段时间后,需要进行模拟/数字转换,在连续模式下,可以一次或多次读取值。
在原始版本中,此时间是根据数据速率计算的(不安全),在最近的端口中,此时间通常约为0.01秒,因为转换很可能不会在配置后直接完成(请检查)
4) 以500SPS的速度读取大约是ADS1115能够读取的最快速度。状态为860SPS时的转换需要1.2毫秒。添加配置和读取时间后,您将无法每0.002s连续读取两个或多个值,即使您收到转换通知(如我在上所述),而不是等待一段固定时间
5) 我认为使用Python最接近的方法是在连续模式下使用GPIO通知运行2个菊花链ADS1115,但我没有这方面的经验