From d08903bcbe3d9b7f6f451064fdba773841583bce Mon Sep 17 00:00:00 2001 From: der_zaske Date: Mon, 10 Feb 2025 22:41:59 +0100 Subject: [PATCH] added BLDC Mode --- main/ADC.c | 2 +- main/app_main.c | 1 + main/functions.c | 28 ++++++++++++++++++++++++++++ main/include/functions.h | 15 +++++++++++++-- main/menu.c | 37 ++++++++++++++++++++++--------------- 5 files changed, 65 insertions(+), 18 deletions(-) diff --git a/main/ADC.c b/main/ADC.c index 540a572..e862af5 100755 --- a/main/ADC.c +++ b/main/ADC.c @@ -80,7 +80,7 @@ uint32_t get_voltage_in() int32_t get_current_ASC712(int ADC_pin) { int32_t adc_voltage = read_voltage(ADC_pin); - int32_t current = (adc_voltage -2500)*5.405; + int32_t current = (adc_voltage +184)/(10.0/12)-2500; ESP_LOGI("ADC", "ADC%d:voltage:%ldcurrent%ld", ADC_pin, adc_voltage, current); return current; } diff --git a/main/app_main.c b/main/app_main.c index 1b2e9d6..5690995 100755 --- a/main/app_main.c +++ b/main/app_main.c @@ -56,6 +56,7 @@ void app_main(void) //gpio_set_level(CONFIG_HIN_V_GPIO, 1); while (1) { menu_loop(); + //ssd1306_clear_screen(dev_pt, false); /* Die Anzeige der OLED mit der richtigen Nachricht Torque = get_torque(); diff --git a/main/functions.c b/main/functions.c index 1e58dd3..e30f2bd 100755 --- a/main/functions.c +++ b/main/functions.c @@ -54,3 +54,31 @@ bool get_Hall(int HallSensorGPIO){ } return level; } +HallState get_Hall_Combi(){ + + int hall_A = gpio_get_level(CONFIG_HALL_A_GPIO); + int hall_B = gpio_get_level(CONFIG_HALL_B_GPIO); + int hall_C = gpio_get_level(CONFIG_HALL_C_GPIO); + + // Wandelt die GPIO-Levels in einen binären Wert um + return (HallState)((hall_A << 2) | (hall_B << 1) | hall_C); + +} +OutCombis get_output_combination(HallState hall_state) { + switch (hall_state) { + case HALL_001: + return OUT_U_W; + case HALL_010: + return OUT_W_V; + case HALL_011: + return OUT_U_V; + case HALL_100: + return OUT_V_U; + case HALL_101: + return OUT_V_W; + case HALL_110: + return OUT_W_U; + default: + return COMBI_COUNT; // Ungültiger Zustand + } +} \ No newline at end of file diff --git a/main/include/functions.h b/main/include/functions.h index f373fe3..46cf758 100755 --- a/main/include/functions.h +++ b/main/include/functions.h @@ -5,9 +5,20 @@ #include "ssd1306.h" #include - +#include "mcpwm.h" +typedef enum { + HALL_000 = 0b000, // Ungültiger Zustand + HALL_001 = 0b001, + HALL_010 = 0b010, + HALL_011 = 0b011, + HALL_100 = 0b100, + HALL_101 = 0b101, + HALL_110 = 0b110, + HALL_111 = 0b111 // Ungültiger Zustand +} HallState; bool get_Hall(int HallSensorGPIO); SSD1306_t *configure_OLED_old(); - +HallState get_Hall_Combi(); +OutCombis get_output_combination(HallState hall_state); #endif \ No newline at end of file diff --git a/main/menu.c b/main/menu.c index 6c1be71..216b8c0 100644 --- a/main/menu.c +++ b/main/menu.c @@ -120,7 +120,8 @@ const char *mode_names[] = { "+W -V", " +U ", " +V ", - " +W " + " +W ", + "ERROR" }; typedef enum { @@ -233,8 +234,9 @@ static void check_button_pressed(){ static void getset_bridge_state(){ bool RFE_Pulled = !(gpio_get_level(CONFIG_RFE_GPIO)); - - if (RFE_Pulled){ + if(get_voltage_in()<18000){ + current_bridge_state=STATE_UV; + }else if (RFE_Pulled){ current_bridge_state=STATE_OC; }else if(!ShouldState){ current_bridge_state=STATE_DEAKTIVE; @@ -244,7 +246,8 @@ bool RFE_Pulled = !(gpio_get_level(CONFIG_RFE_GPIO)); } static void render_main_menu(){ - max_cursor_pos = 4; + if(current_mode == BLDC_MODE)max_cursor_pos = 3;else max_cursor_pos=4; + //Mode snprintf(display_message, sizeof(display_message), "Mode: %s", mode_names[current_mode]); ssd1306_display_text(&dev, 1, display_message, strlen(display_message), cursor_position == 0); @@ -259,11 +262,11 @@ static void render_main_menu(){ //More_Info_Menu snprintf(display_message, sizeof(display_message), "Sensor Info ->"); - ssd1306_display_text(&dev, 4, display_message, strlen(display_message), cursor_position == 3); + ssd1306_display_text(&dev, 4, display_message, strlen(display_message),cursor_position == 3); //Output Selection snprintf(display_message, sizeof(display_message), "Out: %s",OutCombi_names[current_out_combi]); - ssd1306_display_text(&dev, 5, display_message, strlen(display_message), cursor_position == 4); + ssd1306_display_text(&dev, 5, display_message, strlen(display_message), (current_mode == BLDC_MODE)|(cursor_position == 4)); //State @@ -280,6 +283,7 @@ max_cursor_pos = 3; switch(current_mode){ case MCPWM_MODE: + case BLDC_MODE: //Titel snprintf(display_message, sizeof(display_message), "Conf. MCPWM"); ssd1306_display_text(&dev, 1, display_message, strlen(display_message), false); @@ -324,9 +328,7 @@ switch(current_mode){ static void render_info_menu(){ max_cursor_pos = 1; -switch(current_mode){ - case MCPWM_MODE: - + //cur_U & Hall_A snprintf(display_message, sizeof(display_message), "U:%ldmA H_A:%c", get_current_ASC712(CONFIG_I_SENSE_U_ADC),get_Hall(CONFIG_HALL_A_GPIO)?'1':'0'); ssd1306_display_text(&dev, 1, display_message, strlen(display_message), false); @@ -351,17 +353,22 @@ switch(current_mode){ snprintf(display_message, sizeof(display_message), "Back"); ssd1306_display_text(&dev, 7, display_message, strlen(display_message), true); - break; - default: - break; - - } + } MenuState last_menu = MAIN_MENU; // Initialisiere mit dem Startmenü +OutCombis last_out_combi = OUT_U_V; void menu_loop(){ - + if(current_mode== BLDC_MODE){ + current_out_combi = get_output_combination(get_Hall_Combi()); + if (current_out_combi != last_out_combi) { + configure_mcpwm_output(current_out_combi); + if(ShouldState){ + start_mcpwm_output(); + } + last_out_combi = current_out_combi; // Update to the new out_combi + }} if(!in_editing){ if (enc_in_counter<0){