如何使用Raspbery Pi获取INA219传感器数据
how can i get ina219 sensor data with raspbery pi
我想问你们一个问题。
我正在尝试使用树莓派从当前传感器"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;
}
我看到了几个可能的差异:
-
配置与您的python脚本不同:
- 运行你的蟒蛇脚本 在
- 调用
configure(fd);
之前和之后转储带有wiringPiI2CReadReg16(fd,0x00)
的配置寄存器(地址 0x00),以识别任何差异
-
校准的方式不完全相同:
- 也许不需要每次启动程序时都校准INA。如果您的目标是确定 python 脚本结果不同的原因,请执行一次校准并测试两个脚本。(例如,通过从CPP程序中删除校准并按此顺序运行python脚本,然后运行CPP程序)。
- 采样率不同(两个数据请求之间的时间):
- 您在一段时间内(1)连续调用
result = wiringPiI2CReadReg16(fd, 0x04);
,没有任何延迟。在 python 脚本中以相同的方式完成吗? - 识别连续请求数据时的传感器行为(是否重复相同的测量?您可能应该考虑以较低的速率请求数据。
- 如果采样率与 python 脚本不同,则进行不同的测量是正常的,因为信号可能是嘈杂的。
- 您在一段时间内(1)连续调用
相关文章:
- Android NDK传感器向事件队列报告奇怪的间隔
- 扩展光电二极管探测器以支持多个传感器
- 传感器值的阈值
- 无法获取webot::PositionSensor对象中位置传感器的值
- 同步两个具有不同帧速率的传感器
- POST Arduino 传感器值通过 Flask API 发送到本地 Sqlite db
- Arduino Si7021 温度和湿度传感器代码中的错误
- 通过串行端口从GPS传感器读取
- 如何固定从传感器获得的变量值
- 当机器人车用超声波传感器(C++)检测到物体时,停止机器人车
- 为电容式触摸传感器设置延迟以防止"double press"
- 什么是英特尔实感的等效传感器
- 校准和归一化来自多个温度传感器的读数的算法
- 使用带有 PIR 传感器的米利斯检测连续运动
- 使用 Arduino 发送两种不同类型的传感器数据
- 如果继电器在 arduino 上是常闭的,超声波传感器不起作用
- 为什么我的Arduino代码用于运行蓝牙控制的机器人,障碍物传感器无法正常工作
- Arduino Makefile:如何将传感器数据保存到计算机中的文件中
- 如何使用变速操作员<<分配BH1750光传感器中的值?
- 如何使用Raspbery Pi获取INA219传感器数据