main/task_ina219.c

changeset 5
b1f38105ca7e
parent 3
e5d91caa6ab4
child 6
bad3414f7bc4
equal deleted inserted replaced
4:d0155c16e992 5:b1f38105ca7e
13 EventGroupHandle_t xEventGroupINA219; ///< Events INA219 task 13 EventGroupHandle_t xEventGroupINA219; ///< Events INA219 task
14 INA219_State *ina219_state; ///< Public state for other tasks 14 INA219_State *ina219_state; ///< Public state for other tasks
15 15
16 extern ina219_t ina219_b_dev; 16 extern ina219_t ina219_b_dev;
17 extern ina219_t ina219_s_dev; 17 extern ina219_t ina219_s_dev;
18 extern float s_Volts[I_MAX_LOOPS + 1];
19 extern float s_Current[I_MAX_LOOPS + 1];
20 extern float b_Volts[I_MAX_LOOPS + 1];
21 extern float b_Current[I_MAX_LOOPS + 1];
22 extern uint8_t loopno;
18 23
19 const int TASK_INA219_REQUEST_DONE = BIT0; ///< All requests are done. 24 const int TASK_INA219_REQUEST_DONE = BIT0; ///< All requests are done.
20 const int TASK_INA219_REQUEST_POWER = BIT1; ///< Request power readings 25 const int TASK_INA219_REQUEST_POWER = BIT1; ///< Request power readings
21 26
22 27
41 /* 46 /*
42 * Task to read INA219 sensors on request. 47 * Task to read INA219 sensors on request.
43 */ 48 */
44 void task_ina219(void *pvParameter) 49 void task_ina219(void *pvParameter)
45 { 50 {
46 int error = 0;
47 float bus_voltage, shunt_voltage, current, power; 51 float bus_voltage, shunt_voltage, current, power;
48 52
49 ESP_LOGI(TAG, "Starting task INA219 sda=%d scl=%d", CONFIG_I2C_MASTER_SDA, CONFIG_I2C_MASTER_SCL); 53 ESP_LOGI(TAG, "Starting task INA219 sda=%d scl=%d", CONFIG_I2C_MASTER_SDA, CONFIG_I2C_MASTER_SCL);
50 ina219_state = malloc(sizeof(INA219_State)); 54 ina219_state = malloc(sizeof(INA219_State));
51 55
102 ESP_ERROR_CHECK(ina219_get_current(&ina219_b_dev, &current)); 106 ESP_ERROR_CHECK(ina219_get_current(&ina219_b_dev, &current));
103 ESP_ERROR_CHECK(ina219_get_power(&ina219_b_dev, &power)); 107 ESP_ERROR_CHECK(ina219_get_power(&ina219_b_dev, &power));
104 ESP_LOGI(TAG, "Battery VBUS: %.04f V, VSHUNT: %.04f mV, IBUS: %.04f mA, PBUS: %.04f mW", 108 ESP_LOGI(TAG, "Battery VBUS: %.04f V, VSHUNT: %.04f mV, IBUS: %.04f mA, PBUS: %.04f mW",
105 bus_voltage, shunt_voltage * 1000, current * 1000, power * 1000); 109 bus_voltage, shunt_voltage * 1000, current * 1000, power * 1000);
106 } 110 }
111 if (xSemaphoreTake(xSemaphoreINA219, 25) == pdTRUE) {
112 if (ina219_state->Battery.fake) {
113 ina219_state->Battery.volts = 13.21;
114 if (ready_WiFi()) {
115 ina219_state->Battery.shunt = 0.00785;
116 ina219_state->Battery.current = 78.5;
117 } else {
118 ina219_state->Battery.shunt = 0.00182;
119 ina219_state->Battery.current = 18.2;
120 }
121 } else {
122 ina219_state->Battery.volts = bus_voltage;
123 ina219_state->Battery.shunt = shunt_voltage;
124 ina219_state->Battery.current = current;
125 }
126 ina219_state->Battery.valid = true;
127 xSemaphoreGive(xSemaphoreINA219);
128 }
129
107 if (! ina219_state->Solar.fake) { 130 if (! ina219_state->Solar.fake) {
108 ESP_ERROR_CHECK(ina219_get_bus_voltage(&ina219_s_dev, &bus_voltage)); 131 ESP_ERROR_CHECK(ina219_get_bus_voltage(&ina219_s_dev, &bus_voltage));
109 ESP_ERROR_CHECK(ina219_get_shunt_voltage(&ina219_s_dev, &shunt_voltage)); 132 ESP_ERROR_CHECK(ina219_get_shunt_voltage(&ina219_s_dev, &shunt_voltage));
110 ESP_ERROR_CHECK(ina219_get_current(&ina219_s_dev, &current)); 133 ESP_ERROR_CHECK(ina219_get_current(&ina219_s_dev, &current));
111 ESP_ERROR_CHECK(ina219_get_power(&ina219_s_dev, &power)); 134 ESP_ERROR_CHECK(ina219_get_power(&ina219_s_dev, &power));
112 ESP_LOGI(TAG, " Solar VBUS: %.04f V, VSHUNT: %.04f mV, IBUS: %.04f mA, PBUS: %.04f mW", 135 ESP_LOGI(TAG, " Solar VBUS: %.04f V, VSHUNT: %.04f mV, IBUS: %.04f mA, PBUS: %.04f mW",
113 bus_voltage, shunt_voltage * 1000, current * 1000, power * 1000); 136 bus_voltage, shunt_voltage * 1000, current * 1000, power * 1000);
114 } 137 }
115 138 if (xSemaphoreTake(xSemaphoreINA219, 25) == pdTRUE) {
116 /* 139 if (! ina219_state->Solar.fake && ! ina219_state->Battery.fake) {
117 error = ina219_read_float(&ina219_dev, &temperature, &pressure, &humidity); 140 ina219_state->Solar.volts = bus_voltage;
118 if (xSemaphoreTake(xSemaphoreINA219, 25) == pdTRUE) { 141 ina219_state->Solar.shunt = shunt_voltage;
119 if (error == ESP_OK) { 142 ina219_state->Solar.current = current;
120 ina219_state->error = INA219_ERR_NONE; 143 } else if (ina219_state->Solar.fake && ! ina219_state->Battery.fake) {
121 ina219_state->valid = true; 144 ina219_state->Solar.volts = ina219_state->Battery.volts + 0.78;
122 ina219_state->temperature = temperature; 145 ina219_state->Solar.shunt = 0.02341;
123 ina219_state->pressure = pressure; 146 ina219_state->Solar.current = 234.1;
124 ina219_state->humidity = humidity; 147 } else {
125 } else { 148 ina219_state->Solar.volts = 13.98;
126 ina219_state->error = INA219_ERR_READ; 149 ina219_state->Solar.shunt = 0.02341;
127 ina219_state->valid = false; 150 ina219_state->Solar.current = 234.1;
128 ina219_state->temperature = 0;
129 ina219_state->pressure = 0;
130 ina219_state->humidity = 0;
131 }
132 xSemaphoreGive(xSemaphoreINA219);
133 } 151 }
134 } else { 152 ina219_state->Solar.valid = true;
135 if (xSemaphoreTake(xSemaphoreINA219, 25) == pdTRUE) { 153 xSemaphoreGive(xSemaphoreINA219);
136 ina219_state->error = INA219_ERR_NONE;
137 ina219_state->valid = true;
138 ina219_state->temperature = 21.23;
139 ina219_state->pressure = 101360;
140 ina219_state->humidity = 0;
141 xSemaphoreGive(xSemaphoreINA219);
142 }
143 } 154 }
144 155
145 */
146 xEventGroupClearBits(xEventGroupINA219, TASK_INA219_REQUEST_POWER); 156 xEventGroupClearBits(xEventGroupINA219, TASK_INA219_REQUEST_POWER);
147 xEventGroupSetBits(xEventGroupINA219, TASK_INA219_REQUEST_DONE); 157 xEventGroupSetBits(xEventGroupINA219, TASK_INA219_REQUEST_DONE);
148 #if 0
149 ESP_LOGI(TAG, " TB: %.3f C, %.1f hPa, error: %d", ina219_state->temperature, ina219_state->pressure / 100, ina219_state->error);
150 #endif
151 } 158 }
152 } 159 }
153 } 160 }

mercurial