Python数组元素从ADS1115附加

huangapple go评论74阅读模式
英文:

Python array elements append from ADS1115

问题

以下是您提供的代码的中文翻译部分:

我想使用命令 `chan =` 测量电压并将电压值写入一个具有尺寸为 128x128 并包含 16384 个电压值的数组中我将ADS1115的采样率设置为每秒 128 次样本数据采集的时间应为 128 秒 x 128 个样本 = 总共 16384 个样本

问题是程序在 128 秒内不起作用而提前停止我认为问题出在其他地方

代码如下

import numpy as np
import time
from matplotlib import pyplot as plt

import board
import busio
import adafruit_ads1x15.ads1115 as ADS
from adafruit_ads1x15.analog_in import AnalogIn
from timeit import default_timer as timer


i2c = busio.I2C(board.SCL, board.SDA)
ads = ADS.ADS1115(i2c)
ads.gain = 8
ads.mode = ADS.Mode.CONTINUOUS
ads.data_rate = 128
chan = AnalogIn(ads, ADS.P0, ADS.P1)

trig = AnalogIn(ads, ADS.P2, ADS.P3)

level = 192 # 触发电平

totalElapsed = 0

buffer = []

while True:

     if (trig.value > level):
     
         start = timer()
         thisTime =  timer() - start

         while(len(buffer) < 16384):

              buffer.append(chan.value)
              
              totalElapsed += thisTime
              
              print(">>>>>>>>>>> 总经过时间: " + str(totalElapsed) + "秒")

         # 转换为 numpy 数组并重塑为 (128,128)
         a = np.array(buffer).reshape((128,128))

         # 反转交替行
         for r in range(1,128,2):
            a[r] = a[r][::-1]

         # 生成图像
         plt.imsave('/home/rpi/Downloads/mystm_pic.tiff', a, cmap='afmhot') # 文件位置

         plt.imshow(a, cmap='afmhot')

         plt.show()

总之我想测量电压并将其值写入数组直到数组完全填满即在 128 秒内写入 16384 个测量电压值假设采样速度为 128/

非常感谢您的帮助和建议

另外,您提供的代码修正部分也已经被翻译了。如果您需要进一步的帮助或有其他问题,请随时告诉我。

英文:

I wanted to measure voltage with command chan = and write the voltage value to array which has dimensions 128x128 and contains 16384 voltage values. I set ADS1115 sample rate to 128 samples per second. The time of data acquisition should be 128 seconds x 128 samples = 16384 total samples.

The problem is that the program is not working for 128 seconds and it stops much earlier. I suppose, there are some problems elsewhere.

The code look like this:

import numpy as np
import time
from matplotlib import pyplot as plt
import board
import busio
import adafruit_ads1x15.ads1115 as ADS
from adafruit_ads1x15.analog_in import AnalogIn
from timeit import default_timer as timer
i2c = busio.I2C(board.SCL, board.SDA)
ads = ADS.ADS1115(i2c)
ads.gain = 8
ads.mode = ADS.Mode.CONTINUOUS
ads.data_rate = 128
chan = AnalogIn(ads, ADS.P0, ADS.P1)
trig = AnalogIn(ads, ADS.P2, ADS.P3)
level = 192 # trigger level
totalElapsed = 0
buffer = []
while True:
if (trig.value &gt; level):
start = timer()
thisTime =  timer() - start
while(len(buffer) &lt; 16384):
buffer.append(chan.value)
totalElapsed += thisTime
print(&quot;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; Total Elapsed: &quot; + str(totalElapsed) + &quot;s&quot;)
# Make into numpy array and reshape to (128,128)
a = np.array(buffer).reshape((128,128))
# Reverse alternate rows
for r in range(1,128,2):
a[r] = a[r][::-1]
#Generate image
plt.imsave(&#39;/home/rpi/Downloads/mystm_pic.tiff&#39;, a, cmap=&#39;afmhot&#39;) # file location
plt.imshow(a, cmap=&#39;afmhot&#39;)
plt.show()

To sum up, I want to measure voltage and write its value to array until the array is fully filled, i. e. 16384 written measured voltages in time 128 seconds, assuming speed of 128 samples/second.

Thank you very much for help and your suggestions.

Edit:

The correct code should like this?

import numpy as np
import time
from matplotlib import pyplot as plt
import board
import busio
import adafruit_ads1x15.ads1115 as ADS
from adafruit_ads1x15.analog_in import AnalogIn
from timeit import default_timer as timer
RATE = 128
SAMPLES = 16384    
i2c = busio.I2C(board.SCL, board.SDA)
ads = ADS.ADS1115(i2c)
ads.gain = 8
ads.mode = ADS.Mode.CONTINUOUS
ads.data_rate = RATE
chan = AnalogIn(ads, ADS.P0, ADS.P1)
trig = AnalogIn(ads, ADS.P2, ADS.P3)
level = 192 # trigger level
sample_interval = 1.0 / ads.data_rate
repeats = 0
skips = 0
data = [None] * SAMPLES
start = time.monotonic()
time_next_sample = start + sample_interval
if (trig.value &gt; level):
for i in range(SAMPLES):
while time.monotonic() &lt; (time_next_sample): 
pass
data[i] = chan.value
# Loop timing
time_last_sample = time.monotonic()
time_next_sample = time_next_sample + sample_interval
if time_last_sample &gt; (time_next_sample + sample_interval):
skips += 1
time_next_sample = time.monotonic() + sample_interval
# Detect repeated values due to over polling
if data[i] == data[i - 1]:
repeats += 1
end = time.monotonic()
total_time = end - start
# Make into numpy array and reshape to (128,128)
a = np.array(data).reshape((128,128))
# Reverse alternate rows
for r in range(1,128,2):
a[r] = a[r][::-1]
print(&quot;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; Total Elapsed: &quot; + str(total_time) + &quot;s&quot;)    
#Generate image
plt.imsave(&#39;/home/rpi/Downloads/mystm_pic.tiff&#39;, a, cmap=&#39;afmhot&#39;) # file location
plt.imshow(a, cmap=&#39;afmhot&#39;)
plt.show()

答案1

得分: 1

当您请求AnalogIn.value时,您将获得最新的测量值。如果您连续快速调用它,您将只是重复获得相同的测量值 - 它不会自动等待ADC完成读取。因此,通过在循环中调用它,您将以树莓派可以执行的速度填充缓冲区,执行16384次对ADC的I2C读取。

据我所知,AdaFruit的驱动程序没有任何与ADC进行适当同步的机制。因此,您将需要实现自己的时间设置:在每个循环中,您希望在下一次读取之前延迟1/128秒。您可以通过在循环中检查time.monotonic()来实现这一点;这将比使用time.sleep()更可靠。

请参考 https://github.com/adafruit/Adafruit_CircuitPython_ADS1x15/blob/main/examples/ads1x15_fast_read.py,了解定时循环的完整示例。

英文:

When you request AnalogIn.value, you’ll get the latest measured value. If you call this in quick succession, you’ll simply repeatedly get the same measurement - it does not automatically wait for the ADC to finish reading. Thus, by calling it in a loop, you’re just going to fill the buffer as quickly as the Pi can perform 16384 I2C reads to your ADC.

As far as I can tell, the AdaFruit drivers don’t have any mechanism to properly synchronize with the ADC. Thus, you’ll have to implement your own timing setup: in each loop, you will want to delay for 1/128 seconds before the next read. You can do this by checking time.monotonic() in a loop; that will be more reliable than using time.sleep().

See https://github.com/adafruit/Adafruit_CircuitPython_ADS1x15/blob/main/examples/ads1x15_fast_read.py for a complete example of the timing loop in action.

huangapple
  • 本文由 发表于 2023年6月12日 05:25:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452569.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定