Python数组元素从ADS1115附加

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

Python array elements append from ADS1115

问题

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

  1. 我想使用命令 `chan =` 测量电压并将电压值写入一个具有尺寸为 128x128 并包含 16384 个电压值的数组中我将ADS1115的采样率设置为每秒 128 次样本数据采集的时间应为 128 x 128 个样本 = 总共 16384 个样本
  2. 问题是程序在 128 秒内不起作用而提前停止我认为问题出在其他地方
  3. 代码如下
  4. import numpy as np
  5. import time
  6. from matplotlib import pyplot as plt
  7. import board
  8. import busio
  9. import adafruit_ads1x15.ads1115 as ADS
  10. from adafruit_ads1x15.analog_in import AnalogIn
  11. from timeit import default_timer as timer
  12. i2c = busio.I2C(board.SCL, board.SDA)
  13. ads = ADS.ADS1115(i2c)
  14. ads.gain = 8
  15. ads.mode = ADS.Mode.CONTINUOUS
  16. ads.data_rate = 128
  17. chan = AnalogIn(ads, ADS.P0, ADS.P1)
  18. trig = AnalogIn(ads, ADS.P2, ADS.P3)
  19. level = 192 # 触发电平
  20. totalElapsed = 0
  21. buffer = []
  22. while True:
  23. if (trig.value > level):
  24. start = timer()
  25. thisTime = timer() - start
  26. while(len(buffer) < 16384):
  27. buffer.append(chan.value)
  28. totalElapsed += thisTime
  29. print(">>>>>>>>>>> 总经过时间: " + str(totalElapsed) + "秒")
  30. # 转换为 numpy 数组并重塑为 (128,128)
  31. a = np.array(buffer).reshape((128,128))
  32. # 反转交替行
  33. for r in range(1,128,2):
  34. a[r] = a[r][::-1]
  35. # 生成图像
  36. plt.imsave('/home/rpi/Downloads/mystm_pic.tiff', a, cmap='afmhot') # 文件位置
  37. plt.imshow(a, cmap='afmhot')
  38. plt.show()
  39. 总之我想测量电压并将其值写入数组直到数组完全填满即在 128 秒内写入 16384 个测量电压值假设采样速度为 128 /
  40. 非常感谢您的帮助和建议

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

英文:

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:

  1. import numpy as np
  2. import time
  3. from matplotlib import pyplot as plt
  4. import board
  5. import busio
  6. import adafruit_ads1x15.ads1115 as ADS
  7. from adafruit_ads1x15.analog_in import AnalogIn
  8. from timeit import default_timer as timer
  9. i2c = busio.I2C(board.SCL, board.SDA)
  10. ads = ADS.ADS1115(i2c)
  11. ads.gain = 8
  12. ads.mode = ADS.Mode.CONTINUOUS
  13. ads.data_rate = 128
  14. chan = AnalogIn(ads, ADS.P0, ADS.P1)
  15. trig = AnalogIn(ads, ADS.P2, ADS.P3)
  16. level = 192 # trigger level
  17. totalElapsed = 0
  18. buffer = []
  19. while True:
  20. if (trig.value &gt; level):
  21. start = timer()
  22. thisTime = timer() - start
  23. while(len(buffer) &lt; 16384):
  24. buffer.append(chan.value)
  25. totalElapsed += thisTime
  26. print(&quot;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; Total Elapsed: &quot; + str(totalElapsed) + &quot;s&quot;)
  27. # Make into numpy array and reshape to (128,128)
  28. a = np.array(buffer).reshape((128,128))
  29. # Reverse alternate rows
  30. for r in range(1,128,2):
  31. a[r] = a[r][::-1]
  32. #Generate image
  33. plt.imsave(&#39;/home/rpi/Downloads/mystm_pic.tiff&#39;, a, cmap=&#39;afmhot&#39;) # file location
  34. plt.imshow(a, cmap=&#39;afmhot&#39;)
  35. 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?

  1. import numpy as np
  2. import time
  3. from matplotlib import pyplot as plt
  4. import board
  5. import busio
  6. import adafruit_ads1x15.ads1115 as ADS
  7. from adafruit_ads1x15.analog_in import AnalogIn
  8. from timeit import default_timer as timer
  9. RATE = 128
  10. SAMPLES = 16384
  11. i2c = busio.I2C(board.SCL, board.SDA)
  12. ads = ADS.ADS1115(i2c)
  13. ads.gain = 8
  14. ads.mode = ADS.Mode.CONTINUOUS
  15. ads.data_rate = RATE
  16. chan = AnalogIn(ads, ADS.P0, ADS.P1)
  17. trig = AnalogIn(ads, ADS.P2, ADS.P3)
  18. level = 192 # trigger level
  19. sample_interval = 1.0 / ads.data_rate
  20. repeats = 0
  21. skips = 0
  22. data = [None] * SAMPLES
  23. start = time.monotonic()
  24. time_next_sample = start + sample_interval
  25. if (trig.value &gt; level):
  26. for i in range(SAMPLES):
  27. while time.monotonic() &lt; (time_next_sample):
  28. pass
  29. data[i] = chan.value
  30. # Loop timing
  31. time_last_sample = time.monotonic()
  32. time_next_sample = time_next_sample + sample_interval
  33. if time_last_sample &gt; (time_next_sample + sample_interval):
  34. skips += 1
  35. time_next_sample = time.monotonic() + sample_interval
  36. # Detect repeated values due to over polling
  37. if data[i] == data[i - 1]:
  38. repeats += 1
  39. end = time.monotonic()
  40. total_time = end - start
  41. # Make into numpy array and reshape to (128,128)
  42. a = np.array(data).reshape((128,128))
  43. # Reverse alternate rows
  44. for r in range(1,128,2):
  45. a[r] = a[r][::-1]
  46. print(&quot;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; Total Elapsed: &quot; + str(total_time) + &quot;s&quot;)
  47. #Generate image
  48. plt.imsave(&#39;/home/rpi/Downloads/mystm_pic.tiff&#39;, a, cmap=&#39;afmhot&#39;) # file location
  49. plt.imshow(a, cmap=&#39;afmhot&#39;)
  50. 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:

确定