Merge pull request #16653 from barbudor/ina226_sensor54_voltage_fs

INA226 - Sensor54 to specify VBUS Full Scale
This commit is contained in:
Theo Arends 2022-09-25 17:41:28 +02:00 committed by GitHub
commit 77a6fcc71a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 30 deletions

View File

@ -94,6 +94,7 @@ typedef struct Ina226Info_tag {
uint16_t config; uint16_t config;
uint8_t present : 1; uint8_t present : 1;
float i_lsb; float i_lsb;
float vbus_lsb;
} Ina226Info_t; } Ina226Info_t;
/* /*
@ -204,19 +205,9 @@ void Ina226Init()
Ina226Info_t *p = Ina226Info; Ina226Info_t *p = Ina226Info;
//AddLog( LOG_LEVEL_NONE, "Ina226Init"); //AddLog( LOG_LEVEL_NONE, "Ina226Init");
// AddLog( LOG_LEVEL_NONE, "Size of Settings: %d bytes", sizeof(TSettings));
// if (!TasmotaGlobal.i2c_enabled)
// AddLog(LOG_LEVEL_DEBUG, "INA226: Initialization failed: No I2C support");
// Clear Ina226 info data // Clear Ina226 info data
memset(Ina226Info, 0, sizeof(Ina226Info));
for (i = 0; i < 4; i++){
*p = {0};
}
//AddLog( LOG_LEVEL_NONE, PSTR("Sizeof Ina226Cfg: %d" ), sizeof(Ina226Cfg));
// Detect devices // Detect devices
@ -267,6 +258,7 @@ void Ina226Init()
//AddLog( LOG_LEVEL_NONE, "Full Scale I in tenths of an amp: %u", Settings->ina226_i_fs[i]); //AddLog( LOG_LEVEL_NONE, "Full Scale I in tenths of an amp: %u", Settings->ina226_i_fs[i]);
p->i_lsb = (((float) Settings->ina226_i_fs[i])/10.0f)/32768.0f; p->i_lsb = (((float) Settings->ina226_i_fs[i])/10.0f)/32768.0f;
//_debug_fval("i_lsb: %s", p->i_lsb, 7); //_debug_fval("i_lsb: %s", p->i_lsb, 7);
p->vbus_lsb = 40.96 / 32768.0; // default 40.96V full scale on 32768 points = 0.00125
// Get shunt resistor value in micro ohms // Get shunt resistor value in micro ohms
uint32_t r_shunt_uohms = _expand_r_shunt(Settings->ina226_r_shunt[i]); uint32_t r_shunt_uohms = _expand_r_shunt(Settings->ina226_r_shunt[i]);
@ -295,7 +287,7 @@ float Ina226ReadBus_v(uint8_t device)
uint8_t addr = Ina226Info[device].address; uint8_t addr = Ina226Info[device].address;
int16_t reg_bus_v = I2cReadS16( addr, INA226_REG_BUSVOLTAGE); int16_t reg_bus_v = I2cReadS16( addr, INA226_REG_BUSVOLTAGE);
float result = ((float) reg_bus_v) * 0.00125f; float result = ((float) reg_bus_v) * Ina226Info[device].vbus_lsb;
return result; return result;
@ -315,21 +307,6 @@ float Ina226ReadShunt_i(uint8_t device)
return result; return result;
} }
/*
* Read the calculated power
*/
float Ina226ReadPower_w(uint8_t device)
{
uint8_t addr = Ina226Info[device].address;
int16_t reg_shunt_i = I2cReadS16( addr, INA226_REG_POWER);
float result = ((float) reg_shunt_i) * (Ina226Info[device].i_lsb * 25.0);
return result;
}
/* /*
* Read voltage, shunt voltage, current, and power registerd for a given device * Read voltage, shunt voltage, current, and power registerd for a given device
*/ */
@ -339,7 +316,7 @@ void Ina226Read(uint8_t device)
//AddLog( LOG_LEVEL_NONE, "Ina226Read"); //AddLog( LOG_LEVEL_NONE, "Ina226Read");
voltages[device] = Ina226ReadBus_v(device); voltages[device] = Ina226ReadBus_v(device);
currents[device] = Ina226ReadShunt_i(device); currents[device] = Ina226ReadShunt_i(device);
powers[device] = Ina226ReadPower_w(device); powers[device] = voltages[device] * currents[device];
//AddLog( LOG_LEVEL_NONE, "INA226 Device %d", device ); //AddLog( LOG_LEVEL_NONE, "INA226 Device %d", device );
//_debug_fval("Voltage", voltages[device]); //_debug_fval("Voltage", voltages[device]);
//_debug_fval("Current", currents[device]); //_debug_fval("Current", currents[device]);
@ -384,6 +361,7 @@ bool Ina226CommandSensor()
uint8_t i, param_count, device, p1 = XdrvMailbox.payload; uint8_t i, param_count, device, p1 = XdrvMailbox.payload;
uint32_t r_shunt_uohms; uint32_t r_shunt_uohms;
uint16_t compact_r_shunt_uohms; uint16_t compact_r_shunt_uohms;
float vbus_fs;
//AddLog( LOG_LEVEL_NONE, "Command received: %d", XdrvMailbox.payload); //AddLog( LOG_LEVEL_NONE, "Command received: %d", XdrvMailbox.payload);
//AddLog( LOG_LEVEL_NONE, "Command data received: %s", XdrvMailbox.data); //AddLog( LOG_LEVEL_NONE, "Command data received: %s", XdrvMailbox.data);
@ -455,6 +433,11 @@ bool Ina226CommandSensor()
show_config = true; show_config = true;
break; break;
case 3: // Set full scale VBus
vbus_fs = CharToFloat(params[1]);
Ina226Info[device].vbus_lsb = vbus_fs / 32768.0;
show_config = true;
break;
default: default:
serviced = false; serviced = false;
@ -467,15 +450,18 @@ bool Ina226CommandSensor()
if (show_config) { if (show_config) {
char shunt_r_str[16]; char shunt_r_str[16];
char fs_i_str[16]; char fs_i_str[16];
char fs_vbus_str[16];
// Shunt resistance is stored in EEPROM in microohms. Convert to ohms // Shunt resistance is stored in EEPROM in microohms. Convert to ohms
r_shunt_uohms = _expand_r_shunt(Settings->ina226_r_shunt[device]); r_shunt_uohms = _expand_r_shunt(Settings->ina226_r_shunt[device]);
dtostrfd(((float)r_shunt_uohms)/1000000.0f, 6, shunt_r_str); dtostrfd(((float)r_shunt_uohms)/1000000.0f, 6, shunt_r_str);
// Full scale current is stored in EEPROM in tenths of an amp. Convert to amps. // Full scale current is stored in EEPROM in tenths of an amp. Convert to amps.
dtostrfd(((float)Settings->ina226_i_fs[device])/10.0f, 1, fs_i_str); dtostrfd(((float)Settings->ina226_i_fs[device])/10.0f, 1, fs_i_str);
// Full scale vbus is volatile (saved in RAM)
dtostrfd((Ina226Info[device].vbus_lsb*32768.0), 2, fs_vbus_str);
// Send json response // Send json response
Response_P(PSTR("{\"Sensor54-device-settings-%d\":{\"SHUNT_R\":%s,\"FS_I\":%s}}"), Response_P(PSTR("{\"Sensor54-device-settings-%d\":{\"SHUNT_R\":%s,\"FS_I\":%s\"FS_V\":%s}}"),
device + 1, shunt_r_str, fs_i_str); device + 1, shunt_r_str, fs_i_str, fs_vbus_str);
} }
return serviced; return serviced;