main/task_ina219.c

changeset 28
5872b972e553
parent 24
74609f70411e
child 30
7448b8dd4288
--- a/main/task_ina219.c	Thu Apr 13 14:29:41 2023 +0200
+++ b/main/task_ina219.c	Thu Apr 13 15:28:11 2023 +0200
@@ -43,7 +43,7 @@
  */
 void task_ina219(void *pvParameter)
 {
-    float	bus_voltage, shunt_voltage, current;
+    float	bus_voltage, shunt_voltage;
 
     ESP_LOGI(TAG, "Starting task INA219 sda=%d scl=%d", CONFIG_I2C_MASTER_SDA, CONFIG_I2C_MASTER_SCL);
     ina219_state = malloc(sizeof(INA219_State));
@@ -97,21 +97,25 @@
 	     */
 	    if (! ina219_state->Battery.fake) {
 		/*
-		 * Note, on Arduino power down and resume works, but not on esp-idf.
-		 * This could save only 0.5 mA per device.
-		 *
 		 * We run the ESP32-C3 in Power Save mode, Dynamic Frequency Scaling.
 		 * This means a wrong current measurement (too high) unless we let the CPU
 		 * rest for a while. The INA219 runs in continuous mode so we get the
 		 * results during the vTaskDelay().
 		 */
-		vTaskDelay(20 / portTICK_PERIOD_MS);
+		ESP_ERROR_CHECK(ina219_configure(&ina219_b_dev, INA219_BUS_RANGE_32V, INA219_GAIN_0_125,
+		    INA219_RES_12BIT_1S, INA219_RES_12BIT_1S, INA219_MODE_CONT_SHUNT_BUS));
+		vTaskDelay(10 / portTICK_PERIOD_MS);
 		ESP_ERROR_CHECK(ina219_get_bus_voltage(&ina219_b_dev, &bus_voltage));
 		vTaskDelay(10 / portTICK_PERIOD_MS);
 		ESP_ERROR_CHECK(ina219_get_shunt_voltage(&ina219_b_dev, &shunt_voltage));
-		vTaskDelay(10 / portTICK_PERIOD_MS);
-		ESP_ERROR_CHECK(ina219_get_current(&ina219_b_dev, &current));
-		ESP_LOGI(TAG, "Battery VBUS: %.04f V, VSHUNT: %.04f mV, IBUS: %.04f mA", bus_voltage, shunt_voltage * 1000, current * 1000);
+		/*
+		 * We don't call ina219_get_current(&ina219_b_dev, &current) because it takes
+		 * a new measurement which usual gives the same results as the shunt voltage.
+		 * So just calculate the current.
+		 */
+		ESP_ERROR_CHECK(ina219_configure(&ina219_b_dev, INA219_BUS_RANGE_32V, INA219_GAIN_0_125,
+            	    INA219_RES_12BIT_1S, INA219_RES_12BIT_1S, INA219_MODE_POWER_DOWN));
+		ESP_LOGI(TAG, "Battery VBUS: %.03f V, VSHUNT: %.02f mV, IBUS: %.01f mA", bus_voltage, shunt_voltage * 1000, shunt_voltage * 10000);
 	    }
 	    if (xSemaphoreTake(xSemaphoreINA219, 25) == pdTRUE) {
 		if (ina219_state->Battery.fake) {
@@ -126,26 +130,28 @@
 		} else {
 		    ina219_state->Battery.volts = bus_voltage;
 		    ina219_state->Battery.shunt = shunt_voltage;
-		    ina219_state->Battery.current = current * 1000;
+		    ina219_state->Battery.current = shunt_voltage * 10000;
 		}
 		ina219_state->Battery.valid = true;
 		xSemaphoreGive(xSemaphoreINA219);
 	    }
 
 	    if (! ina219_state->Solar.fake) {
-		vTaskDelay(20 / portTICK_PERIOD_MS);
+		ESP_ERROR_CHECK(ina219_configure(&ina219_s_dev, INA219_BUS_RANGE_32V, INA219_GAIN_0_125,
+                    INA219_RES_12BIT_1S, INA219_RES_12BIT_1S, INA219_MODE_CONT_SHUNT_BUS));
+		vTaskDelay(10 / portTICK_PERIOD_MS);
                 ESP_ERROR_CHECK(ina219_get_bus_voltage(&ina219_s_dev, &bus_voltage));
 		vTaskDelay(10 / portTICK_PERIOD_MS);
                 ESP_ERROR_CHECK(ina219_get_shunt_voltage(&ina219_s_dev, &shunt_voltage));
-		vTaskDelay(10 / portTICK_PERIOD_MS);
-                ESP_ERROR_CHECK(ina219_get_current(&ina219_s_dev, &current));
-                ESP_LOGI(TAG, "  Solar VBUS: %.04f V, VSHUNT: %.04f mV, IBUS: %.04f mA", bus_voltage, shunt_voltage * 1000, current * 1000);
+		ESP_ERROR_CHECK(ina219_configure(&ina219_s_dev, INA219_BUS_RANGE_32V, INA219_GAIN_0_125,
+                    INA219_RES_12BIT_1S, INA219_RES_12BIT_1S, INA219_MODE_POWER_DOWN));
+                ESP_LOGI(TAG, "  Solar VBUS: %.03f V, VSHUNT: %.02f mV, IBUS: %.01f mA", bus_voltage, shunt_voltage * 1000, shunt_voltage * 10000);
 	    }
 	    if (xSemaphoreTake(xSemaphoreINA219, 25) == pdTRUE) {
 		if (! ina219_state->Solar.fake && ! ina219_state->Battery.fake) {
 		    ina219_state->Solar.volts = bus_voltage;
                     ina219_state->Solar.shunt = shunt_voltage;
-                    ina219_state->Solar.current = current * 1000;
+                    ina219_state->Solar.current = shunt_voltage * 10000;
 		} else if (ina219_state->Solar.fake && ! ina219_state->Battery.fake) {
 		    ina219_state->Solar.volts = ina219_state->Battery.volts + 0.78;
 		    ina219_state->Solar.shunt = 0.02341;

mercurial