Skip to content

Commit b0c1f99

Browse files
committed
ADC working
ADC funktioniert, aber ist gibt kein Pin an dem die aktuelle Batteriespannung ausgelesen werden kann. VMAIN ist eine konstant geregelte Spannung. Bei der TBS TANGO 2 sind es 6V.
1 parent 37c8920 commit b0c1f99

File tree

5 files changed

+104
-8
lines changed

5 files changed

+104
-8
lines changed

components/battery/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ set(srcs "battery.c")
22

33
idf_component_register(SRCS "${srcs}"
44
INCLUDE_DIRS "include"
5-
REQUIRES bt ble)
5+
REQUIRES bt ble esp_adc)

components/battery/battery.c

Lines changed: 92 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,72 @@ static const char *tag_BAT = "SimLinkModule_BAT";
44

55
uint8_t batteryPercentage = 0;
66

7+
static adc_oneshot_unit_handle_t adc_handle;
8+
static adc_cali_handle_t adc_cal_handle = NULL;
9+
static bool adc_calibrated = false;
10+
static int measurementCount = 0;
11+
static int sumVoltage = 0; //Voltage in mV
12+
13+
14+
//determine the esp ADC reference voltage which is around 1100 mV
15+
bool adc_calibration(adc_unit_t unit, adc_atten_t atten, adc_cali_handle_t *return_handle){
16+
adc_cali_handle_t handle = NULL;
17+
esp_err_t ret = ESP_FAIL;
18+
bool returnValue = false;
19+
20+
adc_cali_line_fitting_config_t cal_config = {
21+
.unit_id = unit,
22+
.atten = atten,
23+
.bitwidth = ADC_BITWIDTH_12,
24+
};
25+
26+
//eFuse Vref represents the true ADC reference voltage. This value is measured and burned into eFuse BLOCK0 during factory calibration.
27+
ret = adc_cali_create_scheme_line_fitting(&cal_config, &handle);
28+
if (ret == ESP_OK) {
29+
returnValue = true;
30+
}
31+
32+
*return_handle = handle;
33+
return returnValue;
34+
}
35+
736
void initBatteryRead(){
37+
//adc init
38+
adc_oneshot_unit_init_cfg_t init_config = {
39+
.unit_id = ADC_UNIT_1,
40+
.ulp_mode = ADC_ULP_MODE_DISABLE,
41+
};
842

43+
ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config, &adc_handle));
44+
45+
//adc configuration
46+
//wenn .atten auf 0db Dämpfung gesetzt ist, ist der mögliche Wertebereich von 0 bis 1.1V
47+
//bei .atten 12db wird der Wertebereich von 0 bis theoretisch 3.9V (3.55*1.1V) verwendet aber durch VDD auf 3.3V begrenzt
48+
//recommended range between 150 to 2450 mV at 12db
49+
adc_oneshot_chan_cfg_t config = {
50+
.bitwidth = ADC_BITWIDTH_12,
51+
.atten = ADC_ATTEN_DB_11,
52+
};
53+
//esp32_technical_reference_manual_en.pdf page 629 for the pin/channel
54+
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc_handle, ADC_CHANNEL_4, &config));
55+
56+
//ADC calibration
57+
adc_calibrated = adc_calibration(ADC_UNIT_1, ADC_ATTEN_DB_11, &adc_cal_handle);
58+
}
59+
60+
//Voltage in mV
61+
int32_t getVoltage(){
62+
int raw_val, voltage;
63+
adc_oneshot_read(adc_handle, ADC_CHANNEL_4, &raw_val);
64+
if(adc_calibrated){
65+
//voltage in mV
66+
adc_cali_raw_to_voltage(adc_cal_handle, raw_val, &voltage);
67+
} else {
68+
//rough value
69+
voltage = (raw_val*3.3/4096)*1000;
70+
}
71+
72+
return voltage;
973
}
1074

1175
void battery_Timer_Event(TimerHandle_t ev){
@@ -14,11 +78,34 @@ void battery_Timer_Event(TimerHandle_t ev){
1478
struct os_mbuf *om;
1579
int rc;
1680

17-
batteryPercentage = (batteryPercentage < 100) ? batteryPercentage+10 : 0;
81+
getVoltage();
82+
if(measurementCount < 10){
83+
sumVoltage += getVoltage();
84+
measurementCount++;
85+
} else {
86+
int avgVoltage = sumVoltage/10;
87+
sumVoltage = 0;
88+
measurementCount = 0;
89+
90+
//battery range between 3.4 and 4.2V
91+
//r2/(r1+r2) = 0.755 akutell
92+
//vmax_in = 4.2*0.755 = 3.171
93+
//vmin_in = 3.8*0.755 = 2.869
94+
//steps = (3171-2869)/100 = 302/100
95+
int8_t newPercentage = 0;
96+
if(avgVoltage > 2869) {
97+
newPercentage = avgVoltage*302/100;
98+
}
99+
100+
ESP_LOGI(tag_BAT, "%d", newPercentage);
18101

19-
if(notify_state_battery_status){
20-
om = ble_hs_mbuf_from_flat(&batteryPercentage, sizeof(batteryPercentage));
21-
//Deprecated. Should not be used. Use ble_gatts_notify_custom instead.
22-
rc = ble_gattc_notify_custom(conn_handle, battery_status_handle, om);
102+
if(newPercentage != batteryPercentage){
103+
if(notify_state_battery_status){
104+
om = ble_hs_mbuf_from_flat(&batteryPercentage, sizeof(batteryPercentage));
105+
//Deprecated. Should not be used. Use ble_gatts_notify_custom instead.
106+
rc = ble_gattc_notify_custom(conn_handle, battery_status_handle, om);
107+
}
108+
newPercentage = batteryPercentage;
109+
}
23110
}
24111
}

components/battery/include/battery.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
#include <stdint.h>
77
#include "freertos/FreeRTOS.h"
88
#include "freertos/timers.h"
9+
10+
#include "esp_adc/adc_oneshot.h"
11+
#include "esp_adc/adc_cali.h"
12+
#include "esp_adc/adc_cali_scheme.h"
13+
914
#include "host/ble_gatt.h"
1015

1116
#include "gatt.h"
@@ -15,5 +20,6 @@ extern uint8_t batteryPercentage;
1520

1621
void initBatteryRead();
1722
void battery_Timer_Event(TimerHandle_t ev);
23+
bool adc_calibration(adc_unit_t unit, adc_atten_t atten, adc_cali_handle_t *out_handle);
1824

1925
#endif

docs/todo

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@
99
- buttons hinzufügen
1010
- LEDs hinzufügen
1111
- Display output hinzufügen
12+
- freertos timer stürzt zu oft ab fehler beheben wie im forum steht
13+
- Verbindung zu ADC sollte eigentlich mit Transistor geschalten werden damit nicht dauerhat strom fließt zu maße über den Spannungsteiler, auch sollte ein kondensator noch hinzugefügt werden, damit rauschen vermindert wird.

main/main.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ void app_main(void)
4141
xTaskCreate(crsf_get_ChannelData_task, "crsf_task", 4096, NULL, 10, NULL);
4242

4343
//timer to read battery percentage via ADC
44-
//wert alle 10 sekunden auslesen
45-
batteryTimerHandle = xTimerCreate("battery_timer", pdMS_TO_TICKS(10000), pdTRUE, (void *)0, battery_Timer_Event);
44+
initBatteryRead();
45+
//wert jede sekunde auslesen --> aus 10 messungen wird der mittelwert gebildet --> vermindern von rauschen
46+
batteryTimerHandle = xTimerCreate("battery_timer", pdMS_TO_TICKS(1000), pdTRUE, (void *)0, battery_Timer_Event);
4647

4748
//start the timer
4849
if (xTimerStart(batteryTimerHandle, 1000 / portTICK_PERIOD_MS ) != pdPASS) {

0 commit comments

Comments
 (0)