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:
1 parent
ad4f53016c
commit
507842cea2
12 files changed
+106
-7
No files matched your search
@@ -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)
|
||||
|
||||
#### View the compiled bridge type
|
||||
|
||||
@@ -1928,6 +1928,10 @@ void MyMesh::handleCmdFrame(size_t len) {
|
||||
savePrefs();
|
||||
}
|
||||
#endif
|
||||
if (strcmp(sp, "ina.shunt") == 0) { // CUSTOM (TeTeHacko): survives a reboot
|
||||
_prefs.ina_shunt_mohm = atof(np);
|
||||
savePrefs();
|
||||
}
|
||||
writeOKFrame();
|
||||
} else {
|
||||
writeErrFrame(ERR_CODE_ILLEGAL_ARG);
|
||||
|
||||
@@ -53,6 +53,7 @@ public:
|
||||
uint8_t default_scope_key[16];
|
||||
float tz_offset = 0;
|
||||
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
|
||||
#ifndef WIFI_SSID
|
||||
#define WIFI_SSID ""
|
||||
@@ -225,6 +226,7 @@ private:
|
||||
def("tel_env", _parent->telemetry_mode_env);
|
||||
def("tz_offset", _parent->tz_offset);
|
||||
def("adc_mult", _parent->adc_multiplier);
|
||||
def("ina_shunt", _parent->ina_shunt_mohm);
|
||||
}
|
||||
public:
|
||||
CompanionPrefs(NodePrefs* parent) : _parent(parent) { }
|
||||
|
||||
@@ -319,6 +319,7 @@ void setup() {
|
||||
#if ENV_INCLUDE_GPS == 1
|
||||
the_mesh.applyGpsPrefs();
|
||||
#endif
|
||||
sensors.applyInaShunt(the_mesh.getNodePrefs()->ina_shunt_mohm); // CUSTOM (TeTeHacko)
|
||||
|
||||
#ifdef DISPLAY_CLASS
|
||||
ui_task.begin(disp, &sensors, the_mesh.getNodePrefs()); // still want to pass this in as dependency, as prefs might be moved
|
||||
|
||||
@@ -1529,6 +1529,7 @@ void MyMesh::begin(FILESYSTEM *fs) {
|
||||
#if ENV_INCLUDE_GPS == 1
|
||||
applyGpsPrefs();
|
||||
#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) {
|
||||
|
||||
@@ -747,6 +747,7 @@ void MyMesh::begin(FILESYSTEM *fs) {
|
||||
#if ENV_INCLUDE_GPS == 1
|
||||
applyGpsPrefs();
|
||||
#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) {
|
||||
|
||||
@@ -873,6 +873,7 @@ void SensorMesh::begin(FILESYSTEM* fs) {
|
||||
#if ENV_INCLUDE_GPS == 1
|
||||
applyGpsPrefs();
|
||||
#endif
|
||||
sensors.applyInaShunt(_prefs.ina_shunt_mohm); // CUSTOM (TeTeHacko)
|
||||
}
|
||||
|
||||
bool SensorMesh::formatFileSystem() {
|
||||
|
||||
@@ -413,6 +413,10 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, char* command, char* re
|
||||
const char *key = (num > 0) ? parts[0] : "";
|
||||
const char *value = (num > 1) ? parts[1] : "null";
|
||||
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");
|
||||
} else {
|
||||
strcpy(reply, "can't find custom var");
|
||||
|
||||
@@ -68,6 +68,7 @@ public:
|
||||
uint8_t advert_loc_policy = 0;
|
||||
uint32_t discovery_mod_timestamp = 0;
|
||||
float adc_multiplier = 0;
|
||||
float ina_shunt_mohm = 0; // CUSTOM (TeTeHacko): INA shunt in mOhm, 0 = build default
|
||||
char owner_info[120];
|
||||
uint8_t rx_boosted_gain = 0; // power settings
|
||||
uint8_t radio_fem_rxgain = 0; // LoRa FEM RX gain setting
|
||||
@@ -225,6 +226,7 @@ private:
|
||||
protected:
|
||||
void structure() override {
|
||||
def("adc_mult", _parent->adc_multiplier);
|
||||
def("ina_shunt", _parent->ina_shunt_mohm);
|
||||
def("pwr_sav_en", _parent->powersaving_enabled);
|
||||
def("bicon", _parent->batt_icon);
|
||||
def("bicon_lo", _parent->batt_icon_lo);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <CayenneLPP.h>
|
||||
#include <stdio.h>
|
||||
#include "sensors/LocationProvider.h"
|
||||
|
||||
#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.
|
||||
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
|
||||
// that answered, or -1 when the platform has no bus to scan.
|
||||
//
|
||||
|
||||
@@ -114,6 +114,9 @@ static Adafruit_INA3221 INA3221;
|
||||
#ifndef TELEM_INA219_ADDRESS
|
||||
#define TELEM_INA219_ADDRESS 0x40 // INA219 single channel current sensor I2C address
|
||||
#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>
|
||||
static Adafruit_INA219 INA219(TELEM_INA219_ADDRESS);
|
||||
#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
|
||||
#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
|
||||
#ifndef TELEM_MLX90614_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) {
|
||||
if (!INA3221.begin(addr, wire)) return 0;
|
||||
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.
|
||||
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:
|
||||
// 3 ms covers the wake-up and one fresh pair with margin.
|
||||
delay(3);
|
||||
lpp.addVoltage(ch, active_ina219->getBusVoltage_V());
|
||||
lpp.addCurrent(ch, active_ina219->getCurrent_mA() / 1000.0f);
|
||||
lpp.addPower(ch, active_ina219->getPower_mW() / 1000.0f);
|
||||
const float volts = active_ina219->getBusVoltage_V();
|
||||
const float amps = active_ina219->getShuntVoltage_mV() / 1000.0f / ina_shunt_ohm(TELEM_INA219_SHUNT_VALUE);
|
||||
lpp.addVoltage(ch, volts);
|
||||
lpp.addCurrent(ch, amps);
|
||||
lpp.addPower(ch, volts * amps);
|
||||
active_ina219->powerSave(true);
|
||||
}
|
||||
#endif
|
||||
@@ -451,9 +468,11 @@ static void query_ina226(uint8_t ch, uint8_t, CayenneLPP& lpp) {
|
||||
ina->shutDown();
|
||||
return;
|
||||
}
|
||||
lpp.addVoltage(ch, ina->getBusVoltage());
|
||||
lpp.addCurrent(ch, ina->getCurrent_mA() / 1000.0f);
|
||||
lpp.addPower(ch, ina->getPower_mW() / 1000.0f);
|
||||
const float volts = ina->getBusVoltage();
|
||||
const float amps = ina->getShuntVoltage() / ina_shunt_ohm(TELEM_INA226_SHUNT_VALUE);
|
||||
lpp.addVoltage(ch, volts);
|
||||
lpp.addCurrent(ch, amps);
|
||||
lpp.addPower(ch, volts * amps);
|
||||
ina->shutDown();
|
||||
}
|
||||
#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 settings = 0;
|
||||
#if ENV_INCLUDE_GPS
|
||||
if (gps_detected) settings += 1; // GPS enablement
|
||||
#endif
|
||||
if (hasShuntIna()) settings += 1; // ina.shunt
|
||||
return settings;
|
||||
}
|
||||
|
||||
@@ -835,6 +864,7 @@ const char* EnvironmentSensorManager::getSettingName(int i) const {
|
||||
return "gps";
|
||||
}
|
||||
#endif
|
||||
if (hasShuntIna() && i == settings++) return "ina.shunt";
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -849,10 +879,34 @@ const char* EnvironmentSensorManager::getSettingValue(int i) const {
|
||||
return _location->isGpsOn() ? "1" : "0";
|
||||
}
|
||||
#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;
|
||||
}
|
||||
|
||||
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 (gps_detected && strcmp(name, "gps") == 0) {
|
||||
if (strcmp(value, "0") == 0) {
|
||||
|
||||
@@ -58,4 +58,5 @@ public:
|
||||
const char* getSettingValue(int i) const override;
|
||||
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
|
||||
};
|
||||
Reference in new issue
Block a user