首页 > 解决方案 > 如何使用树莓派获取 ina219 传感器数据

问题描述

我想请教大家一个问题。

我正在尝试使用树莓派从当前传感器“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;

}

标签: c++craspberry-pi3sensors

解决方案


我看到几个可能的差异:

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

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

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

推荐阅读