如何使用Raspbery Pi获取INA219传感器数据

how can i get ina219 sensor data with raspbery pi

本文关键字:INA219 传感器 数据 获取 Pi 何使用 Raspbery      更新时间:2023-10-16

我想问你们一个问题。

我正在尝试使用树莓派从当前传感器"ina219"获取数据。 我尝试使用 c++ 获取数据,但当我尝试使用 python 模块时数据不同

这个数据是尝试过的,以下是来自 Python 脚本的数据

value:6912
value:10496
value:6912
value:6912
value:6912
value:10496
value:6912
value:3584
value:3584
value:-3329
value:0
value:14080
value:6912
value:6912
value:6912
value:3584
value:14080
value:14080
value:0
value:0
value:0
value:3584
value:3584
value:3584
value:6912
value:10496
value:10496
value:10496
value:10496
bus current:-0.695 mA
bus current:-0.598 mA
bus current:-0.598 mA
bus current:-0.598 mA
bus current:-0.695 mA
bus current:-0.695 mA
bus current:-0.695 mA
bus current:-0.695 mA
bus current:-0.695 mA
bus current:-0.695 mA
bus current:-0.598 mA
bus current:-0.695 mA
bus current:-0.695 mA
bus current:-0.695 mA
bus current:-0.695 mA
bus current:-0.598 mA
bus current:-0.598 mA
bus current:-0.695 mA
bus current:-0.695 mA
bus current:-0.695 mA
bus current:-0.695 mA
bus current:-0.598 mA
bus current:-0.598 mA
bus current:-0.695 mA
bus current:-0.695 mA
bus current:-0.695 mA
bus current:-0.695 mA
bus current:-0.598 mA

我无法想象问题是什么。有没有人成功使用树莓派作为C++获取传感器数据?如果有,请帮助我... 以下代码是电流传感器的 C++ 代码

uint16_t cal, config;
float r_shunt,current_lsb,power_lsb;

void configure(int fd);
void calibrate(int fd,float shunt_val, float v_shunt_max, float v_bux_max, float i_max_expected);
int main(){
int fd;
int16_t result;
unsigned char buffer[100];
fd = wiringPiI2CSetup(0x40);
configure(fd);
calibrate(fd, 0.1 , 0.2 , 32, 2);

while(1){
result = wiringPiI2CReadReg16(fd, 0x04);//0x02);
if(result == -1)
cout<<"Error. Error is:"<<errno<<endl;
else
cout<<"value:"<<result<<endl;//*current_lsb<<endl;
}
return 0;
}
void configure(int fd){
config = 0;
//config |= (RANGE_32V<<BRNG | GAIN_8_320MV<<PG0 | ADC_12BIT << BADC1 | ADC_12BIT << SADC1 | CONT_SH_BUS);
config |= (1<<13 | 3<<11 | 3<<7 | 3 << 3 | 7);
int result = wiringPiI2CWriteReg16(fd, 0x00,config);
if(result == -1)
cout<<"Error, is :"<<errno<<endl;
}
void calibrate(int fd,float shunt_val, float v_shunt_max, float v_bux_max, float i_max_expected){
uint16_t digits;
float min_lsb,swap;
r_shunt = shunt_val;
min_lsb = i_max_expected / 32767;
current_lsb = min_lsb;
digits = 0;
while(current_lsb > 0.0){
if((uint16_t)current_lsb /1){
current_lsb = (uint16_t) current_lsb +1;
current_lsb /= pow(10,digits);
break;
}else{
digits++;
current_lsb *= 10.0;
}
};
swap = (0.04096)/(current_lsb*r_shunt);
cal = (uint16_t)swap;
power_lsb = current_lsb *20;
int result = wiringPiI2CWriteReg16(fd, 0x05,cal);
if(result == -1)
cout<<"Error, is:"<<errno<<endl;
}

我看到了几个可能的差异:

  1. 配置与您的python脚本不同:

    • 运行你的蟒蛇脚本
    • 调用configure(fd);之前和之后转储带有wiringPiI2CReadReg16(fd,0x00)的配置寄存器(地址 0x00),以识别任何差异
  2. 校准的方式不完全相同:

    • 也许不需要每次启动程序时都校准INA。如果您的目标是确定 python 脚本结果不同的原因,请执行一次校准并测试两个脚本。(例如,通过从CPP程序中删除校准并按此顺序运行python脚本,然后运行CPP程序)。
  3. 采样率不同(两个数据请求之间的时间):
    • 您在一段时间内(1)连续调用result = wiringPiI2CReadReg16(fd, 0x04);,没有任何延迟。在 python 脚本中以相同的方式完成吗?
    • 识别连续请求数据时的传感器行为(是否重复相同的测量?您可能应该考虑以较低的速率请求数据。
    • 如果采样率与 python 脚本不同,则进行不同的测量是正常的,因为信号可能是嘈杂的。