Merge feat/ina-coulomb into main
This commit is contained in:
commit
e987fc5566
18 files changed
+356
-6
No files matched your search
@@ -219,7 +219,10 @@ Kolik to dá pro váš uzel (deska, firmware, nastavení, provoz, baterie, panel
|
||||
a BMP280 0,42 mA navíc (nápad a první patch pro INA219: Alkel Brostl;
|
||||
upstream PR meshcore-dev/MeshCore#4017). Čipy INA se rozlišují podle ID, takže
|
||||
INA226 už se nedetekuje jako INA219. Odpor bočníku (INA219, INA226, INA3221; INA260 ho má uvnitř) se
|
||||
nastavuje za běhu a ukládá se: `sensor set ina.shunt <mOhm>`.
|
||||
nastavuje za běhu a ukládá se: `sensor set ina.shunt <mOhm>`. INA226 zapojený
|
||||
do přívodu baterie umí počítat náboj: `sensor set ina.coulomb 1` sčítá mAh
|
||||
z baterie a do ní od startu (ve stats bundlu, v exportéru
|
||||
`meshcore_battery_charge_{out,in}_mah_total`); stojí vlastní odběr čipu ~0,33 mA.
|
||||
* Nové čidlo **BMP388 / BMP390** (tlak, teplota, výška; adresa 0x77, se SDO na GND
|
||||
0x76; `ENV_INCLUDE_BMP3XX`, výchozí adresa `TELEM_BMP3XX_ADDRESS`). Měří jen na
|
||||
vyžádání a mezi čteními spí; první měření po startu, které je u tohoto čipu
|
||||
@@ -599,7 +602,10 @@ panel): [power calculator](docs/power-calculator.md).
|
||||
INA219 patch: Alkel Brostl; upstream PR meshcore-dev/MeshCore#4017). INA chips are told apart by their ID,
|
||||
so an INA226 is no longer taken for an INA219. The shunt (INA219, INA226,
|
||||
INA3221; the INA260 has it inside) is set at runtime and saved:
|
||||
`sensor set ina.shunt <mOhm>`.
|
||||
`sensor set ina.shunt <mOhm>`. An INA226 in the battery line counts charge:
|
||||
`sensor set ina.coulomb 1` sums mAh drawn from and charged into the battery
|
||||
since boot (stats bundle; exporter `meshcore_battery_charge_{out,in}_mah_total`);
|
||||
it costs the chip's own ~0.33 mA.
|
||||
* New sensor **BMP388 / BMP390** (pressure, temperature, altitude; address 0x77,
|
||||
0x76 with SDO to GND; `ENV_INCLUDE_BMP3XX`, default address
|
||||
`TELEM_BMP3XX_ADDRESS`). It measures on demand and sleeps between reads; the
|
||||
|
||||
@@ -1428,6 +1428,29 @@ admin, and on a companion as the custom variable `ina.shunt`. With the wrong val
|
||||
the current is off by the ratio -- LaskaKit's INA226 modules carry 10 mOhm, the
|
||||
default assumes 100. Current is reported in steps of 1 mA (CayenneLPP).
|
||||
|
||||
#### Battery charge counter (INA226)
|
||||
*meshcore-tth addition.*
|
||||
|
||||
**Usage:**
|
||||
- `sensor get ina.coulomb`
|
||||
- `sensor set ina.coulomb 1|0`
|
||||
|
||||
**Wiring:** the INA226 in series with the battery, IN+ on the cells and IN- on
|
||||
the board, so current drawn from the battery reads positive and charging negative.
|
||||
Set `ina.shunt` first (10 for LaskaKit modules).
|
||||
|
||||
**Note:** On, the INA226 converts continuously and averages in hardware without
|
||||
gaps ((8.244 ms shunt + 0.14 ms bus) x 1024 = one result every ~8.6 s); the node
|
||||
picks each result up and counts the charge drawn from and charged into the battery
|
||||
since boot, separately. Telemetry then reports the latest ~8.6 s average instead of
|
||||
a one-shot reading. The stats bundle carries the two counters (ids 22/23, uAh), the
|
||||
exporter turns them into `meshcore_battery_charge_out_mah_total` and
|
||||
`..._in_mah_total`; `rate()` of each is the consumption and the harvest. Off by
|
||||
default: then the chip sleeps between telemetry reads. The only cost is the
|
||||
INA226's own supply current (~0.33 mA, datasheet); the node reads it once per
|
||||
result in a loop that wakes every 50 ms anyway. Listed only when an INA226 was
|
||||
detected at boot; saved.
|
||||
|
||||
---
|
||||
|
||||
### Bridge (When bridge support is compiled in)
|
||||
|
||||
@@ -86,7 +86,8 @@ reads past the data and fails the page.
|
||||
| 17 / 18 | `direct_dups` / `flood_dups` | |
|
||||
| 19 | `recv_errors` | |
|
||||
| 20 / 21 | `tx_start_fail` / `tx_timeout` | |
|
||||
| | *the fields of `RepeaterStats` plus telemetry; ids 1..21 are all a guest sees* | |
|
||||
| 22 / 23 | `charge_out` / `charge_in` | uAh drawn from / charged into the battery since boot; only with `sensor set ina.coulomb 1` (an INA226 in the battery line) |
|
||||
| | *the fields of `RepeaterStats` plus telemetry; ids 1..23 are all a guest sees* | |
|
||||
| 24 | `fair_group` | FairnessLimiter drops, channel (group) bucket |
|
||||
| 25 | `fair_sender` | sender bucket |
|
||||
| 26 | `fair_advert` | advert bucket |
|
||||
|
||||
@@ -324,6 +324,7 @@ lost 16 % of its reception -- see cli_commands.md and [power.md](power.md).
|
||||
| `sensor read` | the live telemetry decoded into one line (`sensor list` shows only settings) |
|
||||
| `sensor get ina.shunt` | the shunt the current is computed with |
|
||||
| `sensor set ina.shunt <mOhm>` | shunt resistance of an INA219, INA226 or INA3221 module (`10` for `R010`), saved; `0` = build default (100). The INA260 has its shunt inside and no setting |
|
||||
| `sensor set ina.coulomb 1\|0` | an INA226 in the battery line as a charge counter: converts continuously (~8.6 s averages) and counts mAh drawn / charged since boot (stats bundle ids 22/23, exporter `meshcore_battery_charge_{out,in}_mah_total`). Saved; off by default. Costs the INA226's own ~0.33 mA |
|
||||
|
||||
## Build flags
|
||||
|
||||
|
||||
@@ -606,6 +606,10 @@ int MyMesh::handleRequest(ClientInfo *sender, uint32_t sender_timestamp, uint8_t
|
||||
in.v[SB_DIRECT_DUPS] = ((SimpleMeshTables *)getTables())->getNumDirectDups();
|
||||
in.v[SB_FLOOD_DUPS] = ((SimpleMeshTables *)getTables())->getNumFloodDups();
|
||||
in.v[SB_RECV_ERRORS] = radio_driver.getPacketsRecvErrors();
|
||||
{ // CUSTOM (TeTeHacko): INA226 charge counter in the battery line, when on
|
||||
uint32_t co = 0, ci = 0; int32_t ua = 0;
|
||||
if (sensors.getCharge(&co, &ci, &ua)) { in.v[SB_CHARGE_OUT] = co; in.v[SB_CHARGE_IN] = ci; }
|
||||
}
|
||||
in.v[SB_TX_START_FAIL] = getNumTxStartFail();
|
||||
in.v[SB_TX_TIMEOUT] = getNumTxTimeout();
|
||||
// The node's sensors as a telemetry request would add them, without channel 1
|
||||
@@ -1776,6 +1780,7 @@ void MyMesh::begin(FILESYSTEM *fs) {
|
||||
applyGpsPrefs();
|
||||
#endif
|
||||
sensors.applyInaShunt(_prefs.ina_shunt_mohm); // CUSTOM (TeTeHacko)
|
||||
sensors.applyInaCoulomb(_prefs.ina_coulomb); // CUSTOM (TeTeHacko)
|
||||
}
|
||||
|
||||
void MyMesh::sendFloodScoped(const TransportKey& scope, mesh::Packet* pkt, uint32_t delay_millis, uint8_t path_hash_size) {
|
||||
|
||||
@@ -783,6 +783,7 @@ void MyMesh::begin(FILESYSTEM *fs) {
|
||||
applyGpsPrefs();
|
||||
#endif
|
||||
sensors.applyInaShunt(_prefs.ina_shunt_mohm); // CUSTOM (TeTeHacko)
|
||||
sensors.applyInaCoulomb(_prefs.ina_coulomb); // CUSTOM (TeTeHacko)
|
||||
}
|
||||
|
||||
void MyMesh::sendFloodScoped(const TransportKey& scope, mesh::Packet* pkt, uint32_t delay_millis, uint8_t path_hash_size) {
|
||||
|
||||
@@ -983,6 +983,7 @@ void SensorMesh::begin(FILESYSTEM* fs) {
|
||||
applyGpsPrefs();
|
||||
#endif
|
||||
sensors.applyInaShunt(_prefs.ina_shunt_mohm); // CUSTOM (TeTeHacko)
|
||||
sensors.applyInaCoulomb(_prefs.ina_coulomb); // CUSTOM (TeTeHacko)
|
||||
}
|
||||
|
||||
bool SensorMesh::formatFileSystem() {
|
||||
|
||||
@@ -426,6 +426,10 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
|
||||
_prefs->ina_shunt_mohm = atof(value);
|
||||
savePrefs();
|
||||
}
|
||||
if (strcmp(key, "ina.coulomb") == 0) { // CUSTOM (TeTeHacko): survives a reboot
|
||||
_prefs->ina_coulomb = (value[0] == '1') ? 1 : 0;
|
||||
savePrefs();
|
||||
}
|
||||
strcpy(reply, "ok");
|
||||
} else {
|
||||
strcpy(reply, "can't find custom var");
|
||||
|
||||
@@ -71,6 +71,7 @@ public:
|
||||
uint32_t discovery_mod_timestamp = 0;
|
||||
float adc_multiplier = 0;
|
||||
float ina_shunt_mohm = 0; // CUSTOM (TeTeHacko): INA shunt in mOhm, 0 = build default
|
||||
uint8_t ina_coulomb = 0; // CUSTOM (TeTeHacko): INA226 as the battery charge counter
|
||||
char owner_info[120];
|
||||
uint8_t rx_boosted_gain = 0; // power settings
|
||||
#ifdef WITH_RX_POWERSAVING
|
||||
@@ -259,6 +260,7 @@ private:
|
||||
void structure() override {
|
||||
def("adc_mult", _parent->adc_multiplier);
|
||||
def("ina_shunt", _parent->ina_shunt_mohm);
|
||||
def("ina_coul", _parent->ina_coulomb);
|
||||
def("pwr_sav_en", _parent->powersaving_enabled);
|
||||
def("bicon", _parent->batt_icon);
|
||||
def("bicon_lo", _parent->batt_icon_lo);
|
||||
|
||||
@@ -70,6 +70,18 @@ public:
|
||||
setSettingValue("ina.shunt", v);
|
||||
}
|
||||
|
||||
// CUSTOM (TeTeHacko): `sensor set ina.coulomb 1` from prefs after boot.
|
||||
void applyInaCoulomb(uint8_t on) {
|
||||
if (on) setSettingValue("ina.coulomb", "1");
|
||||
}
|
||||
// CUSTOM (TeTeHacko): the battery-line charge counter (CoulombCounter.h), since
|
||||
// boot: uAh out of / into the battery and the latest averaged current (uA,
|
||||
// positive = drawn). false = no INA226 counting.
|
||||
virtual bool getCharge(uint32_t* out_uah, uint32_t* in_uah, int32_t* ua) const {
|
||||
(void)out_uah; (void)in_uah; (void)ua;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Live I2C scan, written into dest as text. Returns the number of devices
|
||||
// that answered, or -1 when the platform has no bus to scan.
|
||||
//
|
||||
|
||||
@@ -25,13 +25,15 @@
|
||||
#define SB_PAGE_MAX 150 // reply budget after the 4 B tag (see MyMesh::handleRequest)
|
||||
#define SB_MAX_PAGES 15
|
||||
|
||||
// Scalar ids (wt 0). 1..21 = status + telemetry (a guest may read these).
|
||||
// Scalar ids (wt 0). 1..23 = status + telemetry (a guest may read these).
|
||||
enum {
|
||||
SB_BATT_MV = 1, SB_TEMP_DC = 2 /* zigzag, 0.1 C */, SB_UPTIME = 3, SB_AIR_TX = 4, SB_AIR_RX = 5,
|
||||
SB_NOISE = 6 /* zigzag dBm */, SB_RSSI = 7 /* zigzag */, SB_SNR4 = 8 /* zigzag, x4 */, SB_TX_QUEUE = 9,
|
||||
SB_RECV = 10, SB_SENT = 11, SB_SENT_FLOOD = 12, SB_SENT_DIRECT = 13, SB_RECV_FLOOD = 14,
|
||||
SB_RECV_DIRECT = 15, SB_ERR_EVENTS = 16, SB_DIRECT_DUPS = 17, SB_FLOOD_DUPS = 18,
|
||||
SB_RECV_ERRORS = 19, SB_TX_START_FAIL = 20, SB_TX_TIMEOUT = 21,
|
||||
SB_CHARGE_OUT = 22 /* uAh drawn from the battery since boot (`sensor set ina.coulomb 1`) */,
|
||||
SB_CHARGE_IN = 23 /* uAh charged into it since boot */,
|
||||
// admin only
|
||||
SB_FAIR_GROUP = 24, SB_FAIR_SENDER = 25, SB_FAIR_ADVERT = 26, SB_FAIR_WINDOW = 27,
|
||||
SB_RULES_AUTO = 32 /* count << 16 | crc16 */, SB_RULES_STATIC = 33, SB_PREFS_CRC = 34,
|
||||
@@ -40,7 +42,7 @@ enum {
|
||||
SB_COUNTERS = 40 /* run of (slot, dropped, passed) varints */, SB_RULE = 41,
|
||||
SB_ENV_LPP = 42 /* CayenneLPP bytes of the node's sensors, channel 1 (the node itself) left out */,
|
||||
};
|
||||
#define SB_LAST_STATUS_ID 21
|
||||
#define SB_LAST_STATUS_ID 23
|
||||
|
||||
#define SB_PWR_RXGAIN_ACTIVE 0x01
|
||||
#define SB_PWR_RXPS_ACTIVE 0x02
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
// CUSTOM (TeTeHacko): charge counter for an INA in the battery line
|
||||
// (`sensor set ina.coulomb 1`). Header-only and free of Arduino so the native
|
||||
// tests cover it (test/test_coulomb_counter).
|
||||
//
|
||||
// The INA226 averages the shunt voltage in hardware without gaps (continuous
|
||||
// mode, (8.244 ms shunt + 0.14 ms bus) x 1024 = one result every ~8.6 s); the firmware picks
|
||||
// the latest result up once per period and integrates it over the time since
|
||||
// the previous pick. Sign convention follows the wiring: IN+ on the cells,
|
||||
// IN- on the board, so current OUT of the battery (the node draws) is positive
|
||||
// and current INTO it (charging) negative. The two directions are summed
|
||||
// separately: a solar node's daily balance is out - in, and each alone is
|
||||
// what the calculator needs (consumption at night, harvest by day).
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
struct CoulombCounter {
|
||||
// micro-amp-milliseconds; 2^64 uA*ms = 5e9 Ah, no overflow in practice
|
||||
uint64_t out_uams = 0, in_uams = 0;
|
||||
int32_t last_ua = 0; // latest averaged current, signed
|
||||
uint32_t samples = 0;
|
||||
|
||||
// A gap this long means the reads stopped (bus error, busy loop): the old
|
||||
// current is not extrapolated over it, the time is simply not counted.
|
||||
static const uint32_t MAX_DT_MS = 60000;
|
||||
|
||||
void add(int32_t current_ua, uint32_t dt_ms) {
|
||||
last_ua = current_ua;
|
||||
samples++;
|
||||
if (dt_ms == 0 || dt_ms > MAX_DT_MS) return;
|
||||
if (current_ua >= 0) out_uams += (uint64_t)current_ua * dt_ms;
|
||||
else in_uams += (uint64_t)(-(int64_t)current_ua) * dt_ms;
|
||||
}
|
||||
// charge in micro-amp-hours, saturating (4295 Ah: ~50 years at 10 mA)
|
||||
static uint32_t uah(uint64_t uams) {
|
||||
uint64_t v = uams / 3600000ULL;
|
||||
return v > 0xFFFFFFFFULL ? 0xFFFFFFFFu : (uint32_t)v;
|
||||
}
|
||||
uint32_t outUah() const { return uah(out_uams); }
|
||||
uint32_t inUah() const { return uah(in_uams); }
|
||||
void reset() { out_uams = in_uams = 0; last_ua = 0; samples = 0; }
|
||||
};
|
||||
@@ -174,6 +174,20 @@ static float ina_shunt_ohm(float build_default) {
|
||||
}
|
||||
#endif
|
||||
|
||||
#if ENV_INCLUDE_INA226
|
||||
// CUSTOM (TeTeHacko): `sensor set ina.coulomb 1` -- the INA226 in the battery
|
||||
// line as a charge counter (CoulombCounter.h). Off by default: then the chip
|
||||
// sleeps between telemetry reads as before. On, it converts continuously
|
||||
// (~0.33 mA of its own, the only cost: the MCU reads it once per result,
|
||||
// ~8.6 s, inside a loop that wakes every <= 50 ms anyway).
|
||||
#include "CoulombCounter.h"
|
||||
static bool ina_coulomb_on = false;
|
||||
static uint32_t ina_coulomb_last_ms = 0;
|
||||
static CoulombCounter ina_cc;
|
||||
static int32_t ina_coulomb_bus_mv = 0;
|
||||
#define INA_COULOMB_PERIOD_MS 8600 // (8.244 ms shunt + 0.14 ms bus) x 1024 averages
|
||||
#endif
|
||||
|
||||
#if ENV_INCLUDE_MLX90614
|
||||
#ifndef TELEM_MLX90614_ADDRESS
|
||||
#define TELEM_MLX90614_ADDRESS 0x5A // MLX90614 IR temperature sensor I2C address
|
||||
@@ -502,8 +516,44 @@ static uint8_t init_ina226(TwoWire*, uint8_t addr) {
|
||||
ina->setMaxCurrentShunt(TELEM_INA226_MAX_AMP, TELEM_INA226_SHUNT_VALUE);
|
||||
return ina->shutDown() ? 1 : 0; // sleeps between reads, see above
|
||||
}
|
||||
// Continuous, gapless averaging for the charge counter; false = chip gone.
|
||||
static bool ina226_coulomb_start() {
|
||||
class INA226* ina = active_ina226;
|
||||
if (!ina->setAverage(INA226_1024_SAMPLES) || !ina->setShuntVoltageConversionTime(INA226_8300_us) ||
|
||||
!ina->setBusVoltageConversionTime(INA226_140_us) || !ina->setModeShuntBusContinuous()) {
|
||||
return false;
|
||||
}
|
||||
ina_coulomb_last_ms = millis();
|
||||
return true;
|
||||
}
|
||||
// Back to the telemetry setup: defaults (1 average, 1.1 ms), asleep.
|
||||
static void ina226_coulomb_stop() {
|
||||
class INA226* ina = active_ina226;
|
||||
ina->setAverage(INA226_1_SAMPLE);
|
||||
ina->setShuntVoltageConversionTime(INA226_1100_us);
|
||||
ina->setBusVoltageConversionTime(INA226_1100_us);
|
||||
ina->shutDown();
|
||||
}
|
||||
// The latest averaged result into the counter, over the time since the last one.
|
||||
static void ina226_coulomb_tick() {
|
||||
uint32_t now = millis();
|
||||
if ((uint32_t)(now - ina_coulomb_last_ms) < INA_COULOMB_PERIOD_MS) return;
|
||||
class INA226* ina = active_ina226;
|
||||
const float amps = ina->getShuntVoltage() / ina_shunt_ohm(TELEM_INA226_SHUNT_VALUE);
|
||||
ina_coulomb_bus_mv = (int32_t)(ina->getBusVoltage() * 1000.0f);
|
||||
ina_cc.add((int32_t)(amps * 1e6f), now - ina_coulomb_last_ms);
|
||||
ina_coulomb_last_ms = now;
|
||||
}
|
||||
static void query_ina226(uint8_t ch, uint8_t, CayenneLPP& lpp) {
|
||||
class INA226* ina = active_ina226;
|
||||
if (ina_coulomb_on) { // converting continuously: the latest ~8.6 s average, chip left running
|
||||
const float amps = ina_cc.last_ua / 1e6f;
|
||||
const float volts = ina_coulomb_bus_mv / 1000.0f;
|
||||
lpp.addVoltage(ch, volts);
|
||||
lpp.addCurrent(ch, amps);
|
||||
lpp.addPower(ch, volts * amps);
|
||||
return;
|
||||
}
|
||||
// one shunt+bus conversion: 2 x 1.1 ms at the defaults
|
||||
if (!ina->setModeShuntBusTrigger() || !ina->waitConversionReady(20)) {
|
||||
ina->shutDown();
|
||||
@@ -913,6 +963,27 @@ bool EnvironmentSensorManager::querySensors(uint8_t requester_permissions, Cayen
|
||||
|
||||
|
||||
// CUSTOM (TeTeHacko): an INA chip whose shunt `ina.shunt` applies to is active.
|
||||
bool EnvironmentSensorManager::hasIna226() const {
|
||||
for (int j = 0; j < _active_sensor_count; j++) {
|
||||
const char* n = _active_sensors[j].name;
|
||||
if (n && strcmp(n, "INA226") == 0) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool EnvironmentSensorManager::getCharge(uint32_t* out_uah, uint32_t* in_uah, int32_t* ua) const {
|
||||
#if ENV_INCLUDE_INA226
|
||||
if (!ina_coulomb_on) return false;
|
||||
*out_uah = ina_cc.outUah();
|
||||
*in_uah = ina_cc.inUah();
|
||||
*ua = ina_cc.last_ua;
|
||||
return true;
|
||||
#else
|
||||
(void)out_uah; (void)in_uah; (void)ua;
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool EnvironmentSensorManager::hasShuntIna() const {
|
||||
for (int j = 0; j < _active_sensor_count; j++) {
|
||||
const char* n = _active_sensors[j].name;
|
||||
@@ -927,6 +998,7 @@ int EnvironmentSensorManager::getNumSettings() const {
|
||||
if (gps_detected) settings += 1; // GPS enablement
|
||||
#endif
|
||||
if (hasShuntIna()) settings += 1; // ina.shunt
|
||||
if (hasIna226()) settings += 1; // ina.coulomb
|
||||
return settings;
|
||||
}
|
||||
|
||||
@@ -938,6 +1010,7 @@ const char* EnvironmentSensorManager::getSettingName(int i) const {
|
||||
}
|
||||
#endif
|
||||
if (hasShuntIna() && i == settings++) return "ina.shunt";
|
||||
if (hasIna226() && i == settings++) return "ina.coulomb";
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -959,10 +1032,30 @@ const char* EnvironmentSensorManager::getSettingValue(int i) const {
|
||||
return buf;
|
||||
}
|
||||
#endif
|
||||
#if ENV_INCLUDE_INA226
|
||||
if (hasIna226() && i == settings++) return ina_coulomb_on ? "1" : "0";
|
||||
#endif
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool EnvironmentSensorManager::setSettingValue(const char* name, const char* value) {
|
||||
#if ENV_INCLUDE_INA226
|
||||
if (strcmp(name, "ina.coulomb") == 0) {
|
||||
if (!hasIna226()) return false;
|
||||
bool on;
|
||||
if (strcmp(value, "1") == 0) on = true;
|
||||
else if (strcmp(value, "0") == 0) on = false;
|
||||
else return false;
|
||||
if (on && !ina_coulomb_on) {
|
||||
if (!ina226_coulomb_start()) return false;
|
||||
ina_cc.reset();
|
||||
} else if (!on && ina_coulomb_on) {
|
||||
ina226_coulomb_stop();
|
||||
}
|
||||
ina_coulomb_on = on;
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
#if ENV_INCLUDE_INA219 || ENV_INCLUDE_INA226 || ENV_INCLUDE_INA3221
|
||||
if (strcmp(name, "ina.shunt") == 0) {
|
||||
if (!hasShuntIna()) return false;
|
||||
@@ -1082,6 +1175,9 @@ void EnvironmentSensorManager::stop_gps() {
|
||||
|
||||
#if ENV_INCLUDE_GPS || defined(ENV_INCLUDE_BME680_BSEC)
|
||||
void EnvironmentSensorManager::loop() {
|
||||
#if ENV_INCLUDE_INA226
|
||||
if (ina_coulomb_on) ina226_coulomb_tick();
|
||||
#endif
|
||||
|
||||
#if ENV_INCLUDE_GPS
|
||||
static unsigned long next_gps_update = 0;
|
||||
|
||||
@@ -59,4 +59,6 @@ public:
|
||||
bool setSettingValue(const char* name, const char* value) override;
|
||||
int scanI2C(char* dest, size_t max_len) override;
|
||||
bool hasShuntIna() const; // CUSTOM (TeTeHacko): `ina.shunt` applies
|
||||
bool hasIna226() const; // CUSTOM (TeTeHacko): `ina.coulomb` applies
|
||||
bool getCharge(uint32_t* out_uah, uint32_t* in_uah, int32_t* ua) const override;
|
||||
};
|
||||
@@ -0,0 +1,45 @@
|
||||
// CUSTOM (TeTeHacko): CoulombCounter (INA226 charge counter, `sensor set ina.coulomb 1`)
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "helpers/sensors/CoulombCounter.h"
|
||||
|
||||
TEST(CoulombCounter, SplitsDrawnAndCharged) {
|
||||
CoulombCounter c;
|
||||
// 9 mA drawn for one hour in ~8.6 s steps, then 200 mA charging for 30 min
|
||||
for (int i = 0; i < 3600000 / 8600; i++) c.add(9000, 8600);
|
||||
for (int i = 0; i < 1800000 / 8600; i++) c.add(-200000, 8600);
|
||||
EXPECT_NEAR(9000, (int)c.outUah(), 100); // ~9 mAh, the last partial step not reached
|
||||
EXPECT_NEAR(100000, (int)c.inUah(), 1000); // ~100 mAh
|
||||
EXPECT_EQ(-200000, c.last_ua);
|
||||
}
|
||||
|
||||
TEST(CoulombCounter, GapIsNotExtrapolated) {
|
||||
CoulombCounter c;
|
||||
c.add(10000, 3600000); // an hour without reads: not counted
|
||||
EXPECT_EQ(0u, c.outUah());
|
||||
c.add(10000, 0); // first sample after start
|
||||
EXPECT_EQ(0u, c.outUah());
|
||||
c.add(3600000, 1000); // 3.6 A for 1 s = 1000 uAh
|
||||
EXPECT_EQ(1000u, c.outUah());
|
||||
}
|
||||
|
||||
TEST(CoulombCounter, AYearAtTenMilliampsAndSaturation) {
|
||||
CoulombCounter c;
|
||||
for (long i = 0; i < 365L * 24 * 3600 / 9; i++) c.add(10000, 9000); // a year, 10 mA
|
||||
EXPECT_NEAR(87600.0e3, (double)c.outUah(), 100.0e3); // 87.6 Ah
|
||||
c.out_uams = 0xFFFFFFFFFFFFULL * 3600000ULL; // far past 4295 Ah
|
||||
EXPECT_EQ(0xFFFFFFFFu, c.outUah());
|
||||
}
|
||||
|
||||
TEST(CoulombCounter, Reset) {
|
||||
CoulombCounter c;
|
||||
c.add(5000, 8600);
|
||||
c.reset();
|
||||
EXPECT_EQ(0u, c.outUah());
|
||||
EXPECT_EQ(0u, c.samples);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
@@ -76,6 +76,21 @@ TEST(StatsBundle, AdminEmptyBytesMatchVector) {
|
||||
EXPECT_EQ(-1, enc(in, 1, o));
|
||||
}
|
||||
|
||||
// CUSTOM (TeTeHacko): vector guest_charge -- an INA226 charge counter (ids 22/23)
|
||||
TEST(StatsBundle, GuestGetsChargeCounters) {
|
||||
StatsBundleIn in;
|
||||
fillStatus(in);
|
||||
in.v[SB_CHARGE_OUT] = 123456; in.v[SB_CHARGE_IN] = 7890;
|
||||
FlowTable t; twoRules(t);
|
||||
in.admin = false; in.flow = &t; in.prefs_crc = 1;
|
||||
uint8_t o[200];
|
||||
int n = enc(in, 0, o);
|
||||
EXPECT_EQ("010104931f08ae030c87ad4b10ca0614a2900318e1011cc101202b288986092ca04730cb3e34d50838dbf7073cae8e01449c0348ffa3014cff17540258c0c4075cd23d",
|
||||
hex(o, n));
|
||||
StatsBundleOut d; ASSERT_TRUE(sbDecode(o, n, &d));
|
||||
EXPECT_EQ(123456u, d.get(SB_CHARGE_OUT)); EXPECT_EQ(7890u, d.get(SB_CHARGE_IN));
|
||||
}
|
||||
|
||||
TEST(StatsBundle, GuestGetsStatusOnly) {
|
||||
StatsBundleIn in;
|
||||
fillStatus(in);
|
||||
|
||||
@@ -21,7 +21,7 @@ STATUS_IDS = {
|
||||
"batt_mv": 1, "temp_dc": 2, "uptime": 3, "air_tx": 4, "air_rx": 5, "noise": 6, "rssi": 7,
|
||||
"snr4": 8, "tx_queue": 9, "recv": 10, "sent": 11, "sent_flood": 12, "sent_direct": 13,
|
||||
"recv_flood": 14, "recv_direct": 15, "err_events": 16, "direct_dups": 17, "flood_dups": 18,
|
||||
"recv_errors": 19, "tx_start_fail": 20, "tx_timeout": 21,
|
||||
"recv_errors": 19, "tx_start_fail": 20, "tx_timeout": 21, "charge_out": 22, "charge_in": 23,
|
||||
}
|
||||
ZIGZAG = {"temp_dc", "noise", "rssi", "snr4"}
|
||||
FAIR_IDS = {"group": 24, "sender": 25, "advert": 26, "window": 27}
|
||||
@@ -407,6 +407,8 @@ def boundary_cases():
|
||||
def cases():
|
||||
out = [
|
||||
("guest", {"admin": False, "status": STATUS, "rules": TWO_RULES, "prefs_crc": 1}),
|
||||
("guest_charge", {"admin": False, "status": {**STATUS, "charge_out": 123456, "charge_in": 7890},
|
||||
"rules": TWO_RULES, "prefs_crc": 1}),
|
||||
("admin_empty", {"admin": True, "status": {"batt_mv": 4012, "uptime": 90}, "prefs_crc": 0x1234,
|
||||
"rules": []}),
|
||||
("admin_two_rules", {"admin": True, "status": STATUS, "fair": {"group": 31, "sender": 4, "window": 120},
|
||||
|
||||
@@ -84,6 +84,94 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"decoded": [
|
||||
{
|
||||
"counters": [],
|
||||
"page": 0,
|
||||
"pages": 1,
|
||||
"rules": [],
|
||||
"scalars": {
|
||||
"1": 3987,
|
||||
"10": 148233,
|
||||
"11": 9120,
|
||||
"12": 8011,
|
||||
"13": 1109,
|
||||
"14": 130011,
|
||||
"15": 18222,
|
||||
"17": 412,
|
||||
"18": 20991,
|
||||
"19": 3071,
|
||||
"2": 430,
|
||||
"21": 2,
|
||||
"22": 123456,
|
||||
"23": 7890,
|
||||
"3": 1234567,
|
||||
"4": 842,
|
||||
"5": 51234,
|
||||
"6": 225,
|
||||
"7": 193,
|
||||
"8": 43
|
||||
}
|
||||
}
|
||||
],
|
||||
"input": {
|
||||
"admin": false,
|
||||
"prefs_crc": 1,
|
||||
"rules": [
|
||||
{
|
||||
"dropped": 416,
|
||||
"key": "2e",
|
||||
"kind": "c",
|
||||
"mode": "throttle",
|
||||
"passed": 14,
|
||||
"remain": 604800,
|
||||
"secs": 1800
|
||||
},
|
||||
{
|
||||
"auto": true,
|
||||
"dropped": 37,
|
||||
"expires": 1800000000,
|
||||
"key": "8b2c",
|
||||
"kind": "r",
|
||||
"mode": "block"
|
||||
}
|
||||
],
|
||||
"status": {
|
||||
"air_rx": 51234,
|
||||
"air_tx": 842,
|
||||
"batt_mv": 3987,
|
||||
"charge_in": 7890,
|
||||
"charge_out": 123456,
|
||||
"direct_dups": 412,
|
||||
"err_events": 0,
|
||||
"flood_dups": 20991,
|
||||
"noise": -113,
|
||||
"recv": 148233,
|
||||
"recv_direct": 18222,
|
||||
"recv_errors": 3071,
|
||||
"recv_flood": 130011,
|
||||
"rssi": -97,
|
||||
"sent": 9120,
|
||||
"sent_direct": 1109,
|
||||
"sent_flood": 8011,
|
||||
"snr4": -22,
|
||||
"temp_dc": 215,
|
||||
"tx_queue": 0,
|
||||
"tx_start_fail": 0,
|
||||
"tx_timeout": 2,
|
||||
"uptime": 1234567
|
||||
}
|
||||
},
|
||||
"name": "guest_charge",
|
||||
"pages": [
|
||||
{
|
||||
"crc16": "B571",
|
||||
"hex": "010104931f08ae030c87ad4b10ca0614a2900318e1011cc101202b288986092ca04730cb3e34d50838dbf7073cae8e01449c0348ffa3014cff17540258c0c4075cd23d",
|
||||
"len": 67
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"decoded": [
|
||||
{
|
||||
@@ -2016,6 +2104,8 @@
|
||||
"air_rx": 5,
|
||||
"air_tx": 4,
|
||||
"batt_mv": 1,
|
||||
"charge_in": 23,
|
||||
"charge_out": 22,
|
||||
"direct_dups": 17,
|
||||
"err_events": 16,
|
||||
"flood_dups": 18,
|
||||
|
||||
Reference in new issue
Block a user