main/task_ina219.c

changeset 14
2a9f67ecbc72
parent 13
c3b29a1dcf1e
child 24
74609f70411e
--- a/main/task_ina219.c	Mon Apr 03 20:11:29 2023 +0200
+++ b/main/task_ina219.c	Tue Apr 04 14:33:26 2023 +0200
@@ -43,7 +43,7 @@
  */
 void task_ina219(void *pvParameter)
 {
-    float	bus_voltage, shunt_voltage, current, power;
+    float	bus_voltage, shunt_voltage, current;
 
     ESP_LOGI(TAG, "Starting task INA219 sda=%d scl=%d", CONFIG_I2C_MASTER_SDA, CONFIG_I2C_MASTER_SCL);
     ina219_state = malloc(sizeof(INA219_State));
@@ -99,17 +99,18 @@
 		/*
 		 * 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().
 		 */
-//		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(20 / portTICK_PERIOD_MS);
 		ESP_ERROR_CHECK(ina219_get_bus_voltage(&ina219_b_dev, &bus_voltage));
 		ESP_ERROR_CHECK(ina219_get_shunt_voltage(&ina219_b_dev, &shunt_voltage));
+		vTaskDelay(20 / portTICK_PERIOD_MS);
 		ESP_ERROR_CHECK(ina219_get_current(&ina219_b_dev, &current));
-		ESP_ERROR_CHECK(ina219_get_power(&ina219_b_dev, &power));
-//		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: %.04f V, VSHUNT: %.04f mV, IBUS: %.04f mA, PBUS: %.04f mW",
-                		bus_voltage, shunt_voltage * 1000, current * 1000, power * 1000);
+		ESP_LOGI(TAG, "Battery VBUS: %.04f V, VSHUNT: %.04f mV, IBUS: %.04f mA", bus_voltage, shunt_voltage * 1000, current * 1000);
 	    }
 	    if (xSemaphoreTake(xSemaphoreINA219, 25) == pdTRUE) {
 		if (ina219_state->Battery.fake) {
@@ -124,29 +125,25 @@
 		} else {
 		    ina219_state->Battery.volts = bus_voltage;
 		    ina219_state->Battery.shunt = shunt_voltage;
-		    ina219_state->Battery.current = current;
+		    ina219_state->Battery.current = current * 1000;
 		}
 		ina219_state->Battery.valid = true;
 		xSemaphoreGive(xSemaphoreINA219);
 	    }
 
 	    if (! ina219_state->Solar.fake) {
-//		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(20 / portTICK_PERIOD_MS);
                 ESP_ERROR_CHECK(ina219_get_bus_voltage(&ina219_s_dev, &bus_voltage));
                 ESP_ERROR_CHECK(ina219_get_shunt_voltage(&ina219_s_dev, &shunt_voltage));
+		vTaskDelay(20 / portTICK_PERIOD_MS);
                 ESP_ERROR_CHECK(ina219_get_current(&ina219_s_dev, &current));
-                ESP_ERROR_CHECK(ina219_get_power(&ina219_s_dev, &power));
-//		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: %.04f V, VSHUNT: %.04f mV, IBUS: %.04f mA, PBUS: %.04f mW",
-                                bus_voltage, shunt_voltage * 1000, current * 1000, power * 1000);
+                ESP_LOGI(TAG, "  Solar VBUS: %.04f V, VSHUNT: %.04f mV, IBUS: %.04f mA", bus_voltage, shunt_voltage * 1000, current * 1000);
 	    }
 	    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;
+                    ina219_state->Solar.current = current * 1000;
 		} 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