mlx90640用python程序控制和读取数据的步骤和代码
时间: 2024-05-15 18:17:42 浏览: 220
感谢您使用ChitGPT进行交流!针对您的问题,以下是针对 mlx90640的Python控制和读取数据的一般步骤和代码示例:
1. 导入所需要的模块:numpy, cv2 (可选), smbus等。
``` python
import numpy as np
import smbus2 as smbus
```
2. 初始化传感器
``` python
# 获得I2C总线
bus = smbus.SMBus(1)
# I2C地址,或通过寻址找到设备地址
MLX90640_address = 0x33
# 初始化传感器
bus.write_byte_data(MLX90640_address, 0x02, 0x00)
```
3. 从传感器读取数据
``` python
# 读取一帧数据
frame = np.zeros((24,32), dtype=np.float16)
# 要求16x读取和写入单元块(I2C协议要求
for i in range(0, 0x7FFF, 16):
d = bus.read_i2c_block_data(MLX90640_address, 0x4000 | i, 16)
d = [x if x < 32768 else x - 65536 for x in d]
frame[((i // 16) % 4) * 8 + (i // 512), (i // 16) % 32:(i // 16) % 32 + 8] = d
```
4. 对读取的数据进行进一步处理
``` python
# 温度测量的相关参数
MLX90640_MaxRefreshRate = 2
MLX90640_DumpRefreshRate = 0.5
MLX90640_RefreshRate = MLX90640_MaxRefreshRate
MLX90640_Kta = [0.12125, 0.06375, 0.075, 0.080]
MLX90640_Kv = [0.00375, 0.00375, 0.003125, 0.00375]
MLX90640_KsTa = [-0.32, -0.25, -0.16, -0.25, -0.33, -0.3, -0.21, -0.17,
-0.05, 0.0, -0.14, -0.2, -0.1, -0.05, -0.18, -0.15,
-0.03, -0.08, -0.06, -0.21, -0.1, -0.23, -0.19, -0.13]
# 温度缩放因子
MLX90640_TemperatureScale = 0.02
# 偏移因子
MLX90640_TemperatureOffset = -273.15
# 温度数据处理
eTa = [np.exp(k * MLX90640_RefreshRate) - 1.0 for k in MLX90640_KsTa]
frame = frame - (MLX90640_Kta[0] * MLX90640_TemperatureOffset) / eTa[0]
# Compute the Ambient Temperature
Ta = MLX90640_TemperatureScale * (frame[0][0] * eTa[0] + MLX90640_Kta[0] * MLX90640_TemperatureOffset)
# The input voltage on a PD pixel is roughly 130mV. This produces a reading of about 512 counts.
Pr = frame[:, 2:].reshape(-1)
Vcc = np.mean(Pr) * 1.1875
PTAT = frame[0][1]
KT1 = 0.004
KT2 = 0.0002
V_ptat_25 = 1.5
alpha_ptat = 0.005
PTAT25 = 25
a_common = 1.75E-3/4.7E-6/np.math.pow(2,18) # equation 11 in AN201906
a_i_scale = 1.0/(0.0005*(1+65/128))
a_cp_kv = 0.000534
# Compute Temperature
a_cp_kv = 0.000534
a_cp_ta = (-10.0 - Ta)/MLX90640_DumpRefreshRate
KsTa = [k / eTa[idx] for idx, k in enumerate(MLX90640_KsTa)]
V_PX_BIASE = 0
for i in range(768):
V_PD_BG = (Pr[i] - (KT1*(PTAT - PTAT25))) / (KT2 * (PTAT - PTAT25))
V_PD = V_PD_BG - (alpha_ptat*(PTAT - PTAT25))
Ta_corr_PTAT = (PTAT*(1 + KT1*(Ta - 25))) - (PTAT25*(1+KT1*(Ta - 25)))
Ta_corr = Ta - ((Ta_corr_PTAT - 25)*a_common*a_i_scale) - a_cp_ta[i//32] - (KsTa[i//32]*Ta)
alpha_compensated = MLX90640_Kv[int((i%32)/8)]*(1 + MLX90640_Kta[int((i%32)/8)]*(Ta - 25))
Sx = np.math.pow((alpha_compensated * (V_PD - V_PD_BG)) + alpha_ptat*(PTAT - V_ptat_25), 3) * (a_cp_kv * 1.0E-14)
Sw = np.math.pow(MLX90640_TemperatureScale, 4) * Sx
V_PX = np.power((Pr[i]/Vcc) + ((Sx - Sw) / alpha_compensated), (1.0/alpha_compensated))
V_PX_BIASE += V_PX
V_PX_BIASE /= 768
a = 0.00004
b = 0.00029
Vcp = (V_PX_BIASE - b) / a
Vcp = (Vcp / Vcc) + 0.5 # normalise to 0..1 scale
VTOT = 3960 # Or whatever voltage you supply to Vdd/Vin
Ta = Ta - Vcp*VTOT/MLX90640_TemperatureScale
# 需要注意的是,这段代码仅供参考,实际的处理过程可能略有不同
```
希望这可以帮助您。如有其他问题,请继续与我交流。
阅读全文