Python:如何提高树莓派上TCS34725颜色传感器的采样率
Python: How to increase sampling rate of TCS34725 Color Sensor on RaspberryPi
我正在尝试通过 Adafruit 传感器测量 LED 照明闪烁率:Raspberry Pi4 上的 TCS34725。我正在读取内部计算的勒克斯值或原始数据 (RGB)。
根据 TCS34725 库文档,传感器的最小采样率为 2.4ms(~400Hz)。传感器本身的时钟频率为 400kHz。
Python TCS34725 library
但是,当我 运行 测试脚本时,对于原始值或计算的勒克斯值,每次采样的时间周期都在 ~0.0155769 秒(~60 Hz)。
sensor.lux # takes abt 0.0167 s for script below
sensor.color_raw # takes abt 0.0155 s for script below
为什么我无法获得接近 400 Hz 的更高采样率?我的脚本是否延迟了时间?
测试脚本:
import board
import busio
import adafruit_tcs34725
import time
import numpy as np
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_tcs34725.TCS34725(i2c)
"""
integration_time - The integration time of the sensor in milliseconds. Must be a value between 2.4 and 614.4.
gain - The gain of the sensor, must be a value of 1, 4, 16, 60.
"""
sensor.integration_time = 2.4 #lowest possible of 2.4ms
sensor.gain = 1
lx = []
tm = []
loop_delay = time.time() + 60
while time.time() < loop_delay:
t = time.time()
k = sensor.lux
lx.append(k)
tm.append(t)
t_0 = tm[0]
t_norm = [i-t_0 for i in tm]
Z = np.column_stack((t_norm,lx))
np.savetxt('/home/pi/share/py/flicker/test.csv', Z, delimiter=",", fmt='%1.7f')
print('end')
类似地,使用另一个库驱动程序,通过将后续测量之间的延迟(第 139 行)设置为零(假),也会产生相同的 ~60Hz。
Python drivers for the TCS34725 light color sensor by Dexter Industries
关于如何获得接近 400 Hz 的采样率的任何 insights/suggestions?
如果您查看库的代码,了解 access .lux
时发生的情况,
你会发现 all of this code 被执行了。
它涉及(至少)
- 一次 I2C 写入+读取
ATIME
- 一次 I2C 写入+读取
.gain
- 原始颜色的四个 I2C 写入+读取
- 所有这些计算
如果您需要提高性能,我建议您手动访问这些,然后在捕获数据后稍后“离线”进行计算。
我正在尝试通过 Adafruit 传感器测量 LED 照明闪烁率:Raspberry Pi4 上的 TCS34725。我正在读取内部计算的勒克斯值或原始数据 (RGB)。
根据 TCS34725 库文档,传感器的最小采样率为 2.4ms(~400Hz)。传感器本身的时钟频率为 400kHz。 Python TCS34725 library
但是,当我 运行 测试脚本时,对于原始值或计算的勒克斯值,每次采样的时间周期都在 ~0.0155769 秒(~60 Hz)。
sensor.lux # takes abt 0.0167 s for script below
sensor.color_raw # takes abt 0.0155 s for script below
为什么我无法获得接近 400 Hz 的更高采样率?我的脚本是否延迟了时间?
测试脚本:
import board
import busio
import adafruit_tcs34725
import time
import numpy as np
i2c = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_tcs34725.TCS34725(i2c)
"""
integration_time - The integration time of the sensor in milliseconds. Must be a value between 2.4 and 614.4.
gain - The gain of the sensor, must be a value of 1, 4, 16, 60.
"""
sensor.integration_time = 2.4 #lowest possible of 2.4ms
sensor.gain = 1
lx = []
tm = []
loop_delay = time.time() + 60
while time.time() < loop_delay:
t = time.time()
k = sensor.lux
lx.append(k)
tm.append(t)
t_0 = tm[0]
t_norm = [i-t_0 for i in tm]
Z = np.column_stack((t_norm,lx))
np.savetxt('/home/pi/share/py/flicker/test.csv', Z, delimiter=",", fmt='%1.7f')
print('end')
类似地,使用另一个库驱动程序,通过将后续测量之间的延迟(第 139 行)设置为零(假),也会产生相同的 ~60Hz。 Python drivers for the TCS34725 light color sensor by Dexter Industries
关于如何获得接近 400 Hz 的采样率的任何 insights/suggestions?
如果您查看库的代码,了解 access .lux
时发生的情况,
你会发现 all of this code 被执行了。
它涉及(至少)
- 一次 I2C 写入+读取
ATIME
- 一次 I2C 写入+读取
.gain
- 原始颜色的四个 I2C 写入+读取
- 所有这些计算
如果您需要提高性能,我建议您手动访问这些,然后在捕获数据后稍后“离线”进行计算。