diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index e8f4fa1..b944db2 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,3 +1,3 @@ idf_component_register(SRCS "functions.c" "app_main.c" INCLUDE_DIRS "include""${CMAKE_SOURCE_DIR}""." - REQUIRES ssd1306 driver esp_adc hal) + REQUIRES ssd1306 driver esp_adc hal esp_timer) diff --git a/main/app_main.c b/main/app_main.c index 26cd524..0884872 100644 --- a/main/app_main.c +++ b/main/app_main.c @@ -13,6 +13,12 @@ void app_main(void) int32_t Current_V = 0; int32_t Current_W = 0; int32_t Current_bridge =0; + bool Hall_A_On = false; + bool Hall_B_On = false; + bool Hall_C_On = false; + int direction = 0; + float Speed_indx = 0.0; + float Speed_AB = 0.0; float duty = 0.0; char display_message[50]; // Puffer für die Nachricht ESP_LOGI(TAG, "Test"); @@ -31,6 +37,15 @@ void app_main(void) Current_U = get_current_ASC712(adc1_handle,CONFIG_I_SENSE_U_ADC); Current_V = get_current_ASC712(adc1_handle,CONFIG_I_SENSE_U_ADC); Current_W = get_current_ASC712(adc1_handle,CONFIG_I_SENSE_U_ADC); + + /* Hall_A_On = get_Hall(CONFIG_HALL_A_GPIO); + Hall_B_On = get_Hall(CONFIG_HALL_B_GPIO); + Hall_C_On = get_Hall(CONFIG_HALL_C_GPIO); + */ + Speed_indx = get_speed_index(); + Speed_AB = get_speed_AB(); + direction = get_direction(); + Current_bridge = get_current_bridge(adc1_handle, CONFIG_I_SENSE_ADC); if (Voltage_IN >= 20000){ ssd1306_display_text(dev_pt, 1, "Bridge=ON", 10, false); diff --git a/main/functions.c b/main/functions.c index 3f8c12d..e0597a0 100644 --- a/main/functions.c +++ b/main/functions.c @@ -1,10 +1,32 @@ #include "functions.h" +uint64_t delta_index_time = 0; +uint64_t last_index_time = 0; +uint64_t delta_AB_time = 0; +uint64_t last_AB_time = 0; // Definition der Variablen + adc_cali_handle_t cali_handle= NULL; /*############################################*/ /*############### GPIO-Setup #################*/ /*############################################*/ +void IRAM_ATTR index_isr_handler(void *arg){ + uint64_t current_time = esp_timer_get_time(); + + if (last_index_time != 0){ + delta_index_time = current_time - last_index_time; + } + last_index_time = current_time; +} +void IRAM_ATTR enc_ab_isr_handler(void *arg){ + uint64_t current_time = esp_timer_get_time(); + + if (last_AB_time != 0){ + delta_AB_time = current_time - last_AB_time; + } + last_AB_time = current_time; +} + void configure_GPIO_dir(const char *TAG) { /* reset every used GPIO-pin * @@ -52,7 +74,19 @@ void configure_GPIO_dir(const char *TAG) gpio_set_direction(CONFIG_EXT_ENC_LEFT_GPIO, GPIO_MODE_INPUT); gpio_set_direction(CONFIG_EXT_ENC_RIGHT_GPIO, GPIO_MODE_INPUT); gpio_set_direction(CONFIG_RFE_GPIO, GPIO_MODE_INPUT); + ESP_LOGI(TAG, "GPIO dirs configured for DIY power PCB"); + + gpio_config_t io_conf = {}; + io_conf.pin_bit_mask = (1ULL << CONFIG_EXT_ENC_INDX_GPIO)| (1ULL << CONFIG_HALL_A_GPIO); + io_conf.mode = GPIO_MODE_INPUT; + io_conf.pull_up_en = GPIO_PULLUP_ENABLE; + io_conf.intr_type = GPIO_INTR_POSEDGE; // Interrupt auf steigende Flanke + gpio_config(&io_conf); + + gpio_install_isr_service(0); + gpio_isr_handler_add(CONFIG_EXT_ENC_INDX_GPIO, index_isr_handler, NULL); + gpio_isr_handler_add(CONFIG_HALL_A_GPIO, enc_ab_isr_handler, NULL); } /*############################################*/ /*################ ADC-Setup #################*/ @@ -504,6 +538,65 @@ void conf_mcpwm_timers(){ } +bool get_Hall(int HallSensorGPIO){ + char* TAG=""; + + if(HallSensorGPIO == CONFIG_HALL_A_GPIO){ + TAG = "HALL_A"; + }else if(HallSensorGPIO == CONFIG_HALL_B_GPIO){ + TAG = "HALL_B"; + } + else if(HallSensorGPIO == CONFIG_HALL_C_GPIO){ + TAG = "HALL_C"; + }else{ + TAG = "Undefinded"; + } + + bool level = gpio_get_level(HallSensorGPIO); + + if(level){ + ESP_LOGI(TAG, "HIGH"); + }else{ + ESP_LOGI(TAG,"LOW"); + } + return level; +} +int get_direction(){//-1=Error,0=right,1=left + bool right = gpio_get_level(CONFIG_EXT_ENC_RIGHT_GPIO); + bool left = gpio_get_level(CONFIG_EXT_ENC_LEFT_GPIO); + int direction; + if (left && right){ + direction= -1; + ESP_LOGI("Encoder","Direction: Error"); + }else if(right){ + direction = 0; + ESP_LOGI("Encoder","Direction: Right"); + }else{ + direction = 1; + ESP_LOGI("Encoder","Direction: Left"); + } + return direction; +} + +float get_speed_index(){ + uint64_t local_delta_time = delta_index_time; + float speed_rpm = 0; + if (local_delta_time>0){ + speed_rpm = (60.0*1000000.0/local_delta_time); + ESP_LOGI("Encoder", "Geschwindigkeit_Indx: %.2f RPM", speed_rpm); + } +return speed_rpm; +} +float get_speed_AB(){ + uint64_t local_delta_time = delta_AB_time; + float speed_rpm = 0; + if (local_delta_time>0){ + speed_rpm = (60.0*1000000.0/local_delta_time)/1000; + ESP_LOGI("Encoder", "Geschwindigkeit_AB: %.2f RPM", speed_rpm); + } +return speed_rpm; +} + /*############################################*/ /*################## MISC ####################*/ /*############################################*/ diff --git a/main/include/functions.h b/main/include/functions.h index 0138e05..25b71dc 100644 --- a/main/include/functions.h +++ b/main/include/functions.h @@ -15,6 +15,8 @@ #include "esp_adc/adc_oneshot.h" #include "hal/mcpwm_types.h" #include "driver/mcpwm_prelude.h" +#include "esp_timer.h" + #define HIN_U_CH 0 #define HIN_V_CH 1 @@ -23,6 +25,10 @@ #ifndef MY_COMPONENT_H #define MY_COMPONENT_H extern adc_cali_handle_t cali_handle; +extern uint64_t delta_index_time; +extern uint64_t last_index_time; +extern uint64_t delta_AB_time; +extern uint64_t last_AB_time; // Deklaration der Funktion, die in my_component.c implementiert ist void configure_GPIO_dir(const char *TAG); @@ -43,6 +49,10 @@ void U_W_start(int duty); void W_U_start(int duty); void V_W_start(int duty); void W_V_start(int duty); +bool get_Hall(int HallSensorGPIO); +int get_direction(); +float get_speed_index(); +float get_speed_AB(); void conf_mcpwm_timers(); void parse_3pins(const char *TAG, const char *pin_string, int *pins); SSD1306_t *configure_OLED(const char *TAG); diff --git a/sdkconfig.old b/sdkconfig.old index 89e3734..2608ea4 100644 --- a/sdkconfig.old +++ b/sdkconfig.old @@ -239,7 +239,7 @@ CONFIG_IDF_TOOLCHAIN="gcc" CONFIG_IDF_TARGET_ARCH_XTENSA=y CONFIG_IDF_TARGET_ARCH="xtensa" CONFIG_IDF_TARGET="esp32" -CONFIG_IDF_INIT_VERSION="5.3.0" +CONFIG_IDF_INIT_VERSION="5.3.1" CONFIG_IDF_TARGET_ESP32=y CONFIG_IDF_FIRMWARE_CHIP_ID=0x0000