99
1010namespace INA260 {
1111 constexpr uint_fast16_t m_TO_BASE = 1000 ;
12+ constexpr uint_fast16_t u_TO_m = 1000 ;
13+ constexpr uint_fast32_t u_TO_BASE = m_TO_BASE * u_TO_m;
1214 static constexpr int_fast8_t MIN_VOLTAGE_V = -32 ;
1315 static constexpr int_fast16_t MIN_VOLTAGE_mV = MIN_VOLTAGE_V * m_TO_BASE;
1416 static constexpr int_fast8_t MAX_VOLTAGE_V = 32 ;
@@ -27,8 +29,18 @@ namespace INA260 {
2729 Adafruit_INA260 powerSensor;
2830
2931 std::atomic<int_fast16_t > voltage_mV = 0 ;
32+ int_fast16_t prevVoltage_mV = 0 ;
33+ std::atomic<int_fast16_t > dVoltage_mVPS = 0 ;
3034 std::atomic<int_fast16_t > current_mA = 0 ;
35+ int_fast16_t prevCurrent_mA = 0 ;
36+ std::atomic<int_fast16_t > dCurrent_mAPS = 0 ;
3137 std::atomic<int_fast32_t > power_mW = 0 ;
38+ int_fast32_t prevPower_mW = 0 ;
39+ std::atomic<int_fast32_t > dPower_mWPS = 0 ;
40+
41+ namespace detail {
42+ unsigned long lastUpdateTime_us = 0 ;
43+ }
3244
3345 bool begin (uint8_t i2c_addr, INA260_AveragingCount avgCount,
3446 INA260_ConversionTime convTime) {
@@ -50,35 +62,47 @@ namespace INA260 {
5062 bool updateReadings () {
5163 bool result = true ;
5264
65+ unsigned long currentTime_us = micros ();
66+ unsigned long dTime_us = currentTime_us - detail::lastUpdateTime_us;
67+
5368 auto vTemp_mV = (uint_fast32_t )powerSensor.readBusVoltage ();
5469 if ((vTemp_mV > MAX_VOLTAGE_mV) ||
5570 (vTemp_mV < PSENSOR ::MIN_VOLTAGE_mV)) {
5671 ESP_LOGE (" TAG" , " V reading out of bounds: %u mV" , vTemp_mV);
5772 result &= false ;
5873 } // Else: Valid reading
74+ prevVoltage_mV = voltage_mV;
5975 voltage_mV = vTemp_mV;
76+ int_fast16_t dVoltage_mV = voltage_mV - prevVoltage_mV;
77+ dVoltage_mVPS = dVoltage_mV * u_TO_BASE / dTime_us;
6078
6179 auto iTemp = (uint_fast32_t )powerSensor.readCurrent ();
6280 if ((iTemp > MAX_CURRENT_mA) || (iTemp < MIN_CURRENT_mA)) {
6381 ESP_LOGE (" TAG" , " C reading out of bounds: %d mA" , iTemp);
6482 result &= false ;
6583 } // Else: Valid reading
84+ prevCurrent_mA = current_mA;
6685 current_mA = iTemp;
86+ int_fast16_t dCurrent_mA = current_mA - prevCurrent_mA;
87+ dCurrent_mAPS = dCurrent_mA * u_TO_BASE / dTime_us;
6788
6889 auto pTemp = (uint_fast32_t )powerSensor.readPower ();
6990 if ((pTemp > (MAX_VOLTAGE_mV * MAX_CURRENT_A )) ||
7091 (pTemp < (PSENSOR ::MIN_VOLTAGE_mV * MIN_CURRENT_A ))) {
7192 ESP_LOGE (" TAG" , " P out of bounds: %d mW" , pTemp);
7293 result &= false ;
7394 } // Else: Valid reading
95+ prevPower_mW = power_mW;
7496 power_mW = pTemp;
97+ int_fast16_t dPower_mW = power_mW - prevPower_mW;
98+ dPower_mWPS = dPower_mW * u_TO_BASE / dTime_us;
7599
76100 return result;
77101 }
78102
79103 // TODO - improve this and null terminator may not be needed
80104 static constexpr uint_fast8_t LOG_STRING_SIZE =
81- 3 + 6 + 5 + 6 + 5 + 5 + 5 + 1 ;
105+ 3 + (( 6 + 5 ) * 3 ) + (( 8 + 5 ) * 3 ) + 1 ;
82106 /* *
83107 * @brief Get at string that describes the current state of the PID instance
84108 * @returns the current state of the PID instance as a string
@@ -95,14 +119,23 @@ namespace INA260 {
95119 * https://stackoverflow.com/questions/12346487/what-do-each-memory-order-mean
96120 * @see https://en.cppreference.com/cpp/atomic/memory_order
97121 */
98- etl::to_string (voltage_mV.load (std::memory_order::relaxed), logString,
99- decFormatA, true ); // 5 chars
100- logString.append (" , mA: " ); // 6 chars
101- etl::to_string (current_mA.load (std::memory_order::relaxed), logString,
102- decFormatA, true ); // 5 chars
103- logString.append (" , W: " ); // 5 chars
104- etl::to_string (power_mW.load (std::memory_order::relaxed) / m_TO_BASE,
105- logString, decFormatA, true ); // 5 chars
122+ etl::to_string (voltage_mV.load (), logString, decFormatA,
123+ true ); // 5 chars
124+ logString.append (" , mA: " ); // 6 chars
125+ etl::to_string (current_mA.load (), logString, decFormatA,
126+ true ); // 5 chars
127+ logString.append (" , mW: " ); // 6 chars
128+ etl::to_string (power_mW.load () / m_TO_BASE, logString, decFormatA,
129+ true ); // 5 chars
130+ logString.append (" , mV/s: " ); // 8 chars
131+ etl::to_string (dVoltage_mVPS.load (), logString, decFormatA,
132+ true ); // 5 chars
133+ logString.append (" , mA/s: " ); // 8 chars
134+ etl::to_string (dCurrent_mAPS.load (), logString, decFormatA,
135+ true ); // 5 chars
136+ logString.append (" , mW/s: " ); // 8 chars
137+ etl::to_string (dPower_mWPS.load (), logString, decFormatA,
138+ true ); // 5 chars
106139
107140 return logString;
108141 }
0 commit comments