c++ - 我如何使用树莓派获取ina219传感器数据

标签 c++ c raspberry-pi3 sensors

我想问你们一些问题。

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

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

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

关于c++ - 我如何使用树莓派获取ina219传感器数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55193607/

相关文章:

c++ - Qt5 duplicate(拷贝)一个可执行文件

c - 为什么threadID是唯一的?

c - 用于大小分析的 GCC 工具?

c++ - NodeJS Nan C++ 将嵌套对象绑定(bind)到插件实例

c++ - RVO应该什么时候启动?

c - 如何通过libevent创建tcp客户端

python - Raspberry pi 3.0 i2c SMBus 问题

python-2.7 - 树莓派(Python): Send SMS using SIM800L

ip - 如何检查我的树莓派的IP是否是静态的?

c++ - 快速 json 解析器在尝试解析 '\n' 字符时返回 null