dependency swap - INA3221Sensor (#10379)
* dependency swap - INA3221Sensor update INA3221 initialization and measurement methods for compatibility with Rob Tillaart's library Co-authored-by: Copilot <copilot@github.com> * Addresses copilot review Co-authored-by: Copilot <copilot@github.com> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Refine comments on USB detection and INA3221 Updated comments regarding USB detection and INA3221 usage. * Fix static_assert conditions for INA3221 channel definitions Co-authored-by: Copilot <copilot@github.com> * moved macro defines earlier to allow better use. Co-authored-by: Copilot <copilot@github.com> * Add raw register read methods for bus voltage and shunt current in INA3221Sensor Co-authored-by: Copilot <copilot@github.com> --------- Co-authored-by: Copilot <copilot@github.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
co-authored by
GitHub
Copilot
Copilot Autofix powered by AI
parent
bb86cf4e81
commit
fcef46f4b0
@@ -16,10 +16,28 @@ int32_t INA3221Sensor::runOnce()
|
||||
return DEFAULT_SENSOR_MINIMUM_WAIT_TIME_BETWEEN_READS;
|
||||
}
|
||||
if (!status) {
|
||||
ina3221.begin(nodeTelemetrySensorsMap[sensorType].second);
|
||||
ina3221.setShuntRes(100, 100, 100); // 0.1 Ohm shunt resistors
|
||||
status = true;
|
||||
// Re-initialise with the address and Wire bus from the telemetry sensors map.
|
||||
// (Rob Tillaart INA3221_RT takes address + TwoWire*, unlike sgtwilko which took Wire in begin().)
|
||||
ina3221 = INA3221(nodeTelemetrySensorsMap[sensorType].first, nodeTelemetrySensorsMap[sensorType].second);
|
||||
status = ina3221.begin();
|
||||
if (status) {
|
||||
// Default all three channels to a 0.1 Ω shunt resistor.
|
||||
// Override per-variant by defining INA3221_SHUNT_R_CH1/CH2/CH3 (in Ohms) in variant.h.
|
||||
#ifndef INA3221_SHUNT_R_CH1
|
||||
#define INA3221_SHUNT_R_CH1 0.1f
|
||||
#endif
|
||||
#ifndef INA3221_SHUNT_R_CH2
|
||||
#define INA3221_SHUNT_R_CH2 0.1f
|
||||
#endif
|
||||
#ifndef INA3221_SHUNT_R_CH3
|
||||
#define INA3221_SHUNT_R_CH3 0.1f
|
||||
#endif
|
||||
ina3221.setShuntR(0, INA3221_SHUNT_R_CH1);
|
||||
ina3221.setShuntR(1, INA3221_SHUNT_R_CH2);
|
||||
ina3221.setShuntR(2, INA3221_SHUNT_R_CH3);
|
||||
}
|
||||
} else {
|
||||
// Already initialised; status stays true and initI2CSensor() returns next poll interval.
|
||||
status = true;
|
||||
}
|
||||
return initI2CSensor();
|
||||
@@ -27,12 +45,14 @@ int32_t INA3221Sensor::runOnce()
|
||||
|
||||
void INA3221Sensor::setup() {}
|
||||
|
||||
struct _INA3221Measurement INA3221Sensor::getMeasurement(ina3221_ch_t ch)
|
||||
struct _INA3221Measurement INA3221Sensor::getMeasurement(uint8_t ch)
|
||||
{
|
||||
struct _INA3221Measurement measurement;
|
||||
|
||||
measurement.voltage = ina3221.getVoltage(ch);
|
||||
measurement.current = ina3221.getCurrent(ch);
|
||||
measurement.voltage = ina3221.getBusVoltage(ch); // Volts
|
||||
// getCurrent_mA() is used instead of getCurrent() because Rob Tillaart's getCurrent()
|
||||
// returns Amperes; the telemetry proto and VoltageSensor/CurrentSensor interfaces expect mA.
|
||||
measurement.current = ina3221.getCurrent_mA(ch); // milliAmps
|
||||
|
||||
return measurement;
|
||||
}
|
||||
@@ -43,7 +63,7 @@ struct _INA3221Measurements INA3221Sensor::getMeasurements()
|
||||
|
||||
// INA3221 has 3 channels starting from 0
|
||||
for (int i = 0; i < 3; i++) {
|
||||
measurements.measurements[i] = getMeasurement((ina3221_ch_t)i);
|
||||
measurements.measurements[i] = getMeasurement((uint8_t)i);
|
||||
}
|
||||
|
||||
return measurements;
|
||||
@@ -87,24 +107,41 @@ bool INA3221Sensor::getPowerMetrics(meshtastic_Telemetry *measurement)
|
||||
measurement->variant.power_metrics.has_ch3_voltage = true;
|
||||
measurement->variant.power_metrics.has_ch3_current = true;
|
||||
|
||||
measurement->variant.power_metrics.ch1_voltage = m.measurements[INA3221_CH1].voltage;
|
||||
measurement->variant.power_metrics.ch1_current = m.measurements[INA3221_CH1].current;
|
||||
measurement->variant.power_metrics.ch2_voltage = m.measurements[INA3221_CH2].voltage;
|
||||
measurement->variant.power_metrics.ch2_current = m.measurements[INA3221_CH2].current;
|
||||
measurement->variant.power_metrics.ch3_voltage = m.measurements[INA3221_CH3].voltage;
|
||||
measurement->variant.power_metrics.ch3_current = m.measurements[INA3221_CH3].current;
|
||||
// INA3221 channel indices are zero-based (0=CH1, 1=CH2, 2=CH3).
|
||||
measurement->variant.power_metrics.ch1_voltage = m.measurements[0].voltage;
|
||||
measurement->variant.power_metrics.ch1_current = m.measurements[0].current;
|
||||
measurement->variant.power_metrics.ch2_voltage = m.measurements[1].voltage;
|
||||
measurement->variant.power_metrics.ch2_current = m.measurements[1].current;
|
||||
measurement->variant.power_metrics.ch3_voltage = m.measurements[2].voltage;
|
||||
measurement->variant.power_metrics.ch3_current = m.measurements[2].current;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
uint16_t INA3221Sensor::getBusVoltageMv()
|
||||
{
|
||||
return lround(ina3221.getVoltage(BAT_CH) * 1000);
|
||||
return lround(ina3221.getBusVoltage_mV(BAT_CH));
|
||||
}
|
||||
|
||||
int16_t INA3221Sensor::getCurrentMa()
|
||||
{
|
||||
return lround(ina3221.getCurrent(BAT_CH));
|
||||
return lround(ina3221.getCurrent_mA(BAT_CH));
|
||||
}
|
||||
|
||||
// Bus voltage register (0x02 + ch*2): bits [15:3] unsigned, 1 LSB = 8 mV (datasheet p.6).
|
||||
// Voltage raw units: 1 count = 8 mV, so V_mV = raw * 8.
|
||||
int16_t INA3221Sensor::getRawBusVoltage(uint8_t ch)
|
||||
{
|
||||
return (int16_t)(ina3221.getRegister(0x02 + ch * 2) >> 3);
|
||||
}
|
||||
|
||||
// Shunt voltage register (0x01 + ch*2): bits [15:3] signed two's complement, 1 LSB = 40 µV (datasheet p.6).
|
||||
// Current raw units are shunt-voltage counts: 1 count = 40 uV, signed.
|
||||
// I_mA = (raw * 40 uV) / R_mOhm, because uV / mOhm = mA.
|
||||
// Example for 100 mOhm shunt: I_mA = raw * 40 / 100 = raw * 0.4.
|
||||
int16_t INA3221Sensor::getRawShuntCurrent(uint8_t ch)
|
||||
{
|
||||
return (int16_t)(ina3221.getRegister(0x01 + ch * 2) >> 3);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,3 +1,9 @@
|
||||
// INA3221 channel aliases (zero-based: 0 = CH1, 1 = CH2, 2 = CH3).
|
||||
// Defined before configuration.h so variant.h can use them in INA3221_ENV_CH / INA3221_BAT_CH.
|
||||
#define INA3221_CH1 0
|
||||
#define INA3221_CH2 1
|
||||
#define INA3221_CH3 2
|
||||
|
||||
#include "configuration.h"
|
||||
|
||||
#if HAS_TELEMETRY && !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && __has_include(<INA3221.h>)
|
||||
@@ -9,26 +15,29 @@
|
||||
#include <INA3221.h>
|
||||
|
||||
#ifndef INA3221_ENV_CH
|
||||
#define INA3221_ENV_CH INA3221_CH1
|
||||
#define INA3221_ENV_CH INA3221_CH1 // channel to report in environment metrics (default: CH1)
|
||||
#endif
|
||||
|
||||
#ifndef INA3221_BAT_CH
|
||||
#define INA3221_BAT_CH INA3221_CH1
|
||||
#define INA3221_BAT_CH INA3221_CH1 // channel for device_battery_ina_address (default: CH1)
|
||||
#endif
|
||||
|
||||
class INA3221Sensor : public TelemetrySensor, VoltageSensor, CurrentSensor
|
||||
{
|
||||
private:
|
||||
INA3221 ina3221 = INA3221(INA3221_ADDR42_SDA);
|
||||
// Placeholder constructor; re-initialised with correct address and Wire in runOnce().
|
||||
INA3221 ina3221 = INA3221(INA3221_ADDR);
|
||||
|
||||
// channel to report voltage/current for environment metrics
|
||||
static const ina3221_ch_t ENV_CH = INA3221_ENV_CH;
|
||||
static const uint8_t ENV_CH = INA3221_ENV_CH;
|
||||
static_assert(INA3221_ENV_CH >= 0 && INA3221_ENV_CH <= 2, "INA3221_ENV_CH must be 0, 1, or 2");
|
||||
|
||||
// channel to report battery voltage for device_battery_ina_address
|
||||
static const ina3221_ch_t BAT_CH = INA3221_BAT_CH;
|
||||
static const uint8_t BAT_CH = INA3221_BAT_CH;
|
||||
static_assert(INA3221_BAT_CH >= 0 && INA3221_BAT_CH <= 2, "INA3221_BAT_CH must be 0, 1, or 2");
|
||||
|
||||
// get a single measurement for a channel
|
||||
struct _INA3221Measurement getMeasurement(ina3221_ch_t ch);
|
||||
struct _INA3221Measurement getMeasurement(uint8_t ch);
|
||||
|
||||
// get all measurements for all channels
|
||||
struct _INA3221Measurements getMeasurements();
|
||||
@@ -45,6 +54,10 @@ class INA3221Sensor : public TelemetrySensor, VoltageSensor, CurrentSensor
|
||||
bool getMetrics(meshtastic_Telemetry *measurement) override;
|
||||
virtual uint16_t getBusVoltageMv() override;
|
||||
virtual int16_t getCurrentMa() override;
|
||||
|
||||
// Raw register reads (bits [15:3] right-shifted), no conversion applied.
|
||||
int16_t getRawBusVoltage(uint8_t ch);
|
||||
int16_t getRawShuntCurrent(uint8_t ch);
|
||||
};
|
||||
|
||||
struct _INA3221Measurement {
|
||||
|
||||
Reference in New Issue
Block a user