sensors: INA shunt set at runtime -- sensor set ina.shunt <mOhm>, kept in prefs

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-11-12 13:58:39 +01:00
1 parent ad4f53016c
commit 507842cea2
12 files changed
+106 -7

No files matched your search

+18
View File
@@ -1271,6 +1271,24 @@ Covers serial NMEA receivers (`MicroNMEALocationProvider`).
--- ---
#### Set the INA current sensor's shunt resistor
**Usage:**
- `sensor get ina.shunt`
- `sensor set ina.shunt <mOhm>`
**Parameters:**
- `mOhm`: the shunt on the module, in milliohms (`10` for a shunt marked `R010`,
`100` for `R100`); `0` = the build's value (100 mOhm unless the build sets
`TELEM_INA219_SHUNT_VALUE` / `TELEM_INA226_SHUNT_VALUE` / `TELEM_INA3221_SHUNT_VALUE`)
**Note:** Listed only when an INA219, INA226 or INA3221 was detected at boot.
Saved, applies at once and survives a reboot; it also works over the mesh as
admin, and on a companion as the custom variable `ina.shunt`. With the wrong value
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).
---
### Bridge (When bridge support is compiled in) ### Bridge (When bridge support is compiled in)
#### View the compiled bridge type #### View the compiled bridge type
+4
View File
@@ -1928,6 +1928,10 @@ void MyMesh::handleCmdFrame(size_t len) {
savePrefs(); savePrefs();
} }
#endif #endif
if (strcmp(sp, "ina.shunt") == 0) { // CUSTOM (TeTeHacko): survives a reboot
_prefs.ina_shunt_mohm = atof(np);
savePrefs();
}
writeOKFrame(); writeOKFrame();
} else { } else {
writeErrFrame(ERR_CODE_ILLEGAL_ARG); writeErrFrame(ERR_CODE_ILLEGAL_ARG);
+2
View File
@@ -53,6 +53,7 @@ public:
uint8_t default_scope_key[16]; uint8_t default_scope_key[16];
float tz_offset = 0; float tz_offset = 0;
float adc_multiplier = 0; // battery ADC multiplier, 0 = board default float adc_multiplier = 0; // battery ADC multiplier, 0 = board default
float ina_shunt_mohm = 0; // CUSTOM (TeTeHacko): INA shunt in mOhm, 0 = build default
#ifdef ENABLE_WIFI_INTERFACE #ifdef ENABLE_WIFI_INTERFACE
#ifndef WIFI_SSID #ifndef WIFI_SSID
#define WIFI_SSID "" #define WIFI_SSID ""
@@ -225,6 +226,7 @@ private:
def("tel_env", _parent->telemetry_mode_env); def("tel_env", _parent->telemetry_mode_env);
def("tz_offset", _parent->tz_offset); def("tz_offset", _parent->tz_offset);
def("adc_mult", _parent->adc_multiplier); def("adc_mult", _parent->adc_multiplier);
def("ina_shunt", _parent->ina_shunt_mohm);
} }
public: public:
CompanionPrefs(NodePrefs* parent) : _parent(parent) { } CompanionPrefs(NodePrefs* parent) : _parent(parent) { }
+1
View File
@@ -319,6 +319,7 @@ void setup() {
#if ENV_INCLUDE_GPS == 1 #if ENV_INCLUDE_GPS == 1
the_mesh.applyGpsPrefs(); the_mesh.applyGpsPrefs();
#endif #endif
sensors.applyInaShunt(the_mesh.getNodePrefs()->ina_shunt_mohm); // CUSTOM (TeTeHacko)
#ifdef DISPLAY_CLASS #ifdef DISPLAY_CLASS
ui_task.begin(disp, &sensors, the_mesh.getNodePrefs()); // still want to pass this in as dependency, as prefs might be moved ui_task.begin(disp, &sensors, the_mesh.getNodePrefs()); // still want to pass this in as dependency, as prefs might be moved
+1
View File
@@ -1529,6 +1529,7 @@ void MyMesh::begin(FILESYSTEM *fs) {
#if ENV_INCLUDE_GPS == 1 #if ENV_INCLUDE_GPS == 1
applyGpsPrefs(); applyGpsPrefs();
#endif #endif
sensors.applyInaShunt(_prefs.ina_shunt_mohm); // CUSTOM (TeTeHacko)
} }
void MyMesh::sendFloodScoped(const TransportKey& scope, mesh::Packet* pkt, uint32_t delay_millis, uint8_t path_hash_size) { void MyMesh::sendFloodScoped(const TransportKey& scope, mesh::Packet* pkt, uint32_t delay_millis, uint8_t path_hash_size) {
+1
View File
@@ -747,6 +747,7 @@ void MyMesh::begin(FILESYSTEM *fs) {
#if ENV_INCLUDE_GPS == 1 #if ENV_INCLUDE_GPS == 1
applyGpsPrefs(); applyGpsPrefs();
#endif #endif
sensors.applyInaShunt(_prefs.ina_shunt_mohm); // CUSTOM (TeTeHacko)
} }
void MyMesh::sendFloodScoped(const TransportKey& scope, mesh::Packet* pkt, uint32_t delay_millis, uint8_t path_hash_size) { void MyMesh::sendFloodScoped(const TransportKey& scope, mesh::Packet* pkt, uint32_t delay_millis, uint8_t path_hash_size) {
+1
View File
@@ -873,6 +873,7 @@ void SensorMesh::begin(FILESYSTEM* fs) {
#if ENV_INCLUDE_GPS == 1 #if ENV_INCLUDE_GPS == 1
applyGpsPrefs(); applyGpsPrefs();
#endif #endif
sensors.applyInaShunt(_prefs.ina_shunt_mohm); // CUSTOM (TeTeHacko)
} }
bool SensorMesh::formatFileSystem() { bool SensorMesh::formatFileSystem() {
+4
View File
@@ -413,6 +413,10 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
const char *key = (num > 0) ? parts[0] : ""; const char *key = (num > 0) ? parts[0] : "";
const char *value = (num > 1) ? parts[1] : "null"; const char *value = (num > 1) ? parts[1] : "null";
if (_sensors->setSettingValue(key, value)) { if (_sensors->setSettingValue(key, value)) {
if (strcmp(key, "ina.shunt") == 0) { // CUSTOM (TeTeHacko): survives a reboot
_prefs->ina_shunt_mohm = atof(value);
savePrefs();
}
strcpy(reply, "ok"); strcpy(reply, "ok");
} else { } else {
strcpy(reply, "can't find custom var"); strcpy(reply, "can't find custom var");
+2
View File
@@ -68,6 +68,7 @@ public:
uint8_t advert_loc_policy = 0; uint8_t advert_loc_policy = 0;
uint32_t discovery_mod_timestamp = 0; uint32_t discovery_mod_timestamp = 0;
float adc_multiplier = 0; float adc_multiplier = 0;
float ina_shunt_mohm = 0; // CUSTOM (TeTeHacko): INA shunt in mOhm, 0 = build default
char owner_info[120]; char owner_info[120];
uint8_t rx_boosted_gain = 0; // power settings uint8_t rx_boosted_gain = 0; // power settings
uint8_t radio_fem_rxgain = 0; // LoRa FEM RX gain setting uint8_t radio_fem_rxgain = 0; // LoRa FEM RX gain setting
@@ -225,6 +226,7 @@ private:
protected: protected:
void structure() override { void structure() override {
def("adc_mult", _parent->adc_multiplier); def("adc_mult", _parent->adc_multiplier);
def("ina_shunt", _parent->ina_shunt_mohm);
def("pwr_sav_en", _parent->powersaving_enabled); def("pwr_sav_en", _parent->powersaving_enabled);
def("bicon", _parent->batt_icon); def("bicon", _parent->batt_icon);
def("bicon_lo", _parent->batt_icon_lo); def("bicon_lo", _parent->batt_icon_lo);
+10
View File
@@ -1,6 +1,7 @@
#pragma once #pragma once
#include <CayenneLPP.h> #include <CayenneLPP.h>
#include <stdio.h>
#include "sensors/LocationProvider.h" #include "sensors/LocationProvider.h"
#define TELEM_PERM_BASE 0x01 // 'base' permission includes battery #define TELEM_PERM_BASE 0x01 // 'base' permission includes battery
@@ -60,6 +61,15 @@ public:
// With a periodic collector that costs one cycle and needs no scheduling. // With a periodic collector that costs one cycle and needs no scheduling.
virtual void requestLocationRefresh() { } virtual void requestLocationRefresh() { }
// CUSTOM (TeTeHacko): the INA shunt (mOhm) from prefs back into the driver
// after boot; 0 = keep the build's value. A no-op without an INA chip.
void applyInaShunt(float mohm) {
if (mohm <= 0) return;
char v[16];
snprintf(v, sizeof(v), "%.3f", mohm);
setSettingValue("ina.shunt", v);
}
// Live I2C scan, written into dest as text. Returns the number of devices // 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. // that answered, or -1 when the platform has no bus to scan.
// //
@@ -114,6 +114,9 @@ static Adafruit_INA3221 INA3221;
#ifndef TELEM_INA219_ADDRESS #ifndef TELEM_INA219_ADDRESS
#define TELEM_INA219_ADDRESS 0x40 // INA219 single channel current sensor I2C address #define TELEM_INA219_ADDRESS 0x40 // INA219 single channel current sensor I2C address
#endif #endif
#ifndef TELEM_INA219_SHUNT_VALUE
#define TELEM_INA219_SHUNT_VALUE 0.100 // CUSTOM (TeTeHacko): what Adafruit's calibration assumes
#endif
#include <Adafruit_INA219.h> #include <Adafruit_INA219.h>
static Adafruit_INA219 INA219(TELEM_INA219_ADDRESS); static Adafruit_INA219 INA219(TELEM_INA219_ADDRESS);
#endif #endif
@@ -146,6 +149,18 @@ static class INA226 INA226_ALT(TELEM_INA226_ALT_ADDR(TELEM_INA226_ADDRESS), TELE
static class INA226* active_ina226 = &INA226; // `class`: the object INA226 hides the type static class INA226* active_ina226 = &INA226; // `class`: the object INA226 hides the type
#endif #endif
#if ENV_INCLUDE_INA219 || ENV_INCLUDE_INA226 || ENV_INCLUDE_INA3221
// CUSTOM (TeTeHacko): the shunt is a property of the module, not of the
// firmware -- `sensor set ina.shunt <mOhm>`, kept in prefs (ina_shunt), 0 = the
// build's value for the chip. INA219/INA226 current is computed here from the
// shunt voltage (as in the Chytra Budka), so a new value needs no recalibration;
// INA3221 gets it through its driver.
static float ina_shunt_mohm = 0;
static float ina_shunt_ohm(float build_default) {
return ina_shunt_mohm > 0 ? ina_shunt_mohm / 1000.0f : build_default;
}
#endif
#if ENV_INCLUDE_MLX90614 #if ENV_INCLUDE_MLX90614
#ifndef TELEM_MLX90614_ADDRESS #ifndef TELEM_MLX90614_ADDRESS
#define TELEM_MLX90614_ADDRESS 0x5A // MLX90614 IR temperature sensor I2C address #define TELEM_MLX90614_ADDRESS 0x5A // MLX90614 IR temperature sensor I2C address
@@ -326,7 +341,7 @@ static void query_lps22hb(uint8_t ch, uint8_t, CayenneLPP& lpp) {
static uint8_t init_ina3221(TwoWire* wire, uint8_t addr) { static uint8_t init_ina3221(TwoWire* wire, uint8_t addr) {
if (!INA3221.begin(addr, wire)) return 0; if (!INA3221.begin(addr, wire)) return 0;
for (int i = 0; i < TELEM_INA3221_NUM_CHANNELS; i++) { for (int i = 0; i < TELEM_INA3221_NUM_CHANNELS; i++) {
INA3221.setShuntResistance(i, TELEM_INA3221_SHUNT_VALUE); INA3221.setShuntResistance(i, ina_shunt_ohm(TELEM_INA3221_SHUNT_VALUE));
} }
// Each enabled hardware channel becomes its own telemetry channel. // Each enabled hardware channel becomes its own telemetry channel.
uint8_t enabled = 0; uint8_t enabled = 0;
@@ -401,9 +416,11 @@ static void query_ina219(uint8_t ch, uint8_t, CayenneLPP& lpp) {
// begin() selects 12-bit single-sample bus and shunt conversions, 532 us each: // begin() selects 12-bit single-sample bus and shunt conversions, 532 us each:
// 3 ms covers the wake-up and one fresh pair with margin. // 3 ms covers the wake-up and one fresh pair with margin.
delay(3); delay(3);
lpp.addVoltage(ch, active_ina219->getBusVoltage_V()); const float volts = active_ina219->getBusVoltage_V();
lpp.addCurrent(ch, active_ina219->getCurrent_mA() / 1000.0f); const float amps = active_ina219->getShuntVoltage_mV() / 1000.0f / ina_shunt_ohm(TELEM_INA219_SHUNT_VALUE);
lpp.addPower(ch, active_ina219->getPower_mW() / 1000.0f); lpp.addVoltage(ch, volts);
lpp.addCurrent(ch, amps);
lpp.addPower(ch, volts * amps);
active_ina219->powerSave(true); active_ina219->powerSave(true);
} }
#endif #endif
@@ -451,9 +468,11 @@ static void query_ina226(uint8_t ch, uint8_t, CayenneLPP& lpp) {
ina->shutDown(); ina->shutDown();
return; return;
} }
lpp.addVoltage(ch, ina->getBusVoltage()); const float volts = ina->getBusVoltage();
lpp.addCurrent(ch, ina->getCurrent_mA() / 1000.0f); const float amps = ina->getShuntVoltage() / ina_shunt_ohm(TELEM_INA226_SHUNT_VALUE);
lpp.addPower(ch, ina->getPower_mW() / 1000.0f); lpp.addVoltage(ch, volts);
lpp.addCurrent(ch, amps);
lpp.addPower(ch, volts * amps);
ina->shutDown(); ina->shutDown();
} }
#endif #endif
@@ -820,11 +839,21 @@ bool EnvironmentSensorManager::querySensors(uint8_t requester_permissions, Cayen
} }
// CUSTOM (TeTeHacko): an INA chip whose shunt `ina.shunt` applies to is active.
bool EnvironmentSensorManager::hasShuntIna() const {
for (int j = 0; j < _active_sensor_count; j++) {
const char* n = _active_sensors[j].name;
if (n && (strcmp(n, "INA219") == 0 || strcmp(n, "INA226") == 0 || strcmp(n, "INA3221") == 0)) return true;
}
return false;
}
int EnvironmentSensorManager::getNumSettings() const { int EnvironmentSensorManager::getNumSettings() const {
int settings = 0; int settings = 0;
#if ENV_INCLUDE_GPS #if ENV_INCLUDE_GPS
if (gps_detected) settings += 1; // GPS enablement if (gps_detected) settings += 1; // GPS enablement
#endif #endif
if (hasShuntIna()) settings += 1; // ina.shunt
return settings; return settings;
} }
@@ -835,6 +864,7 @@ const char* EnvironmentSensorManager::getSettingName(int i) const {
return "gps"; return "gps";
} }
#endif #endif
if (hasShuntIna() && i == settings++) return "ina.shunt";
return NULL; return NULL;
} }
@@ -849,10 +879,34 @@ const char* EnvironmentSensorManager::getSettingValue(int i) const {
return _location->isGpsOn() ? "1" : "0"; return _location->isGpsOn() ? "1" : "0";
} }
#endif #endif
#if ENV_INCLUDE_INA219 || ENV_INCLUDE_INA226 || ENV_INCLUDE_INA3221
if (hasShuntIna() && i == settings++) {
static char buf[16]; // mOhm; 0 = the build's value
snprintf(buf, sizeof(buf), "%.3f", ina_shunt_mohm);
return buf;
}
#endif
return NULL; return NULL;
} }
bool EnvironmentSensorManager::setSettingValue(const char* name, const char* value) { bool EnvironmentSensorManager::setSettingValue(const char* name, const char* value) {
#if ENV_INCLUDE_INA219 || ENV_INCLUDE_INA226 || ENV_INCLUDE_INA3221
if (strcmp(name, "ina.shunt") == 0) {
if (!hasShuntIna()) return false;
// a number of mOhm, 0 = the build's value; atof() alone would take "nan"
const char* c = value;
if (!((*c >= '0' && *c <= '9') || *c == '.')) return false;
float mohm = atof(value);
if (!(mohm >= 0.0f && mohm <= 10000.0f)) return false;
ina_shunt_mohm = mohm;
#if ENV_INCLUDE_INA3221
for (int i = 0; i < TELEM_INA3221_NUM_CHANNELS; i++) {
INA3221.setShuntResistance(i, ina_shunt_ohm(TELEM_INA3221_SHUNT_VALUE));
}
#endif
return true;
}
#endif
#if ENV_INCLUDE_GPS #if ENV_INCLUDE_GPS
if (gps_detected && strcmp(name, "gps") == 0) { if (gps_detected && strcmp(name, "gps") == 0) {
if (strcmp(value, "0") == 0) { if (strcmp(value, "0") == 0) {
@@ -58,4 +58,5 @@ public:
const char* getSettingValue(int i) const override; const char* getSettingValue(int i) const override;
bool setSettingValue(const char* name, const char* value) override; bool setSettingValue(const char* name, const char* value) override;
int scanI2C(char* dest, size_t max_len) override; int scanI2C(char* dest, size_t max_len) override;
bool hasShuntIna() const; // CUSTOM (TeTeHacko): `ina.shunt` applies
}; };