From 8e0441b27cbf6394157af98c707a45f3c8bb4903 Mon Sep 17 00:00:00 2001 From: jonny_jr9 Date: Sat, 9 Sep 2023 09:38:42 +0200 Subject: [PATCH] Fix stack-overflow, Adjust logging, test-ready enable all tasks in main for full function test with new driver --- board_single/main/main.cpp | 83 +++++++++++++++++++------------------- common/motorctl.cpp | 6 ++- common/motordrivers.cpp | 5 ++- 3 files changed, 49 insertions(+), 45 deletions(-) diff --git a/board_single/main/main.cpp b/board_single/main/main.cpp index f56f7cd..cc01644 100644 --- a/board_single/main/main.cpp +++ b/board_single/main/main.cpp @@ -139,8 +139,8 @@ void setLoglevels(void){ //--- set loglevel for individual tags --- esp_log_level_set("main", ESP_LOG_INFO); esp_log_level_set("buzzer", ESP_LOG_ERROR); - //esp_log_level_set("motordriver", ESP_LOG_INFO); - //esp_log_level_set("motor-control", ESP_LOG_DEBUG); + esp_log_level_set("motordriver", ESP_LOG_INFO); + esp_log_level_set("motor-control", ESP_LOG_INFO); //esp_log_level_set("evaluatedJoystick", ESP_LOG_DEBUG); //esp_log_level_set("joystickCommands", ESP_LOG_DEBUG); esp_log_level_set("button", ESP_LOG_INFO); @@ -176,40 +176,40 @@ extern "C" void app_main(void) { //--- create task for controlling the motors --- //---------------------------------------------- //task that receives commands, handles ramp and current limit and executes commands using the motordriver function - xTaskCreate(&task_motorctl, "task_motor-control", 2048, NULL, 6, NULL); + xTaskCreate(&task_motorctl, "task_motor-control", 2*4096, NULL, 6, NULL); -// //------------------------------ -// //--- create task for buzzer --- -// //------------------------------ -// xTaskCreate(&task_buzzer, "task_buzzer", 2048, NULL, 2, NULL); -// -// //------------------------------- -// //--- create task for control --- -// //------------------------------- -// //task that generates motor commands depending on the current mode and sends those to motorctl task -// xTaskCreate(&task_control, "task_control", 4096, NULL, 5, NULL); -// -// //------------------------------ -// //--- create task for button --- -// //------------------------------ -// //task that evaluates and processes the button input and runs the configured commands -// xTaskCreate(&task_button, "task_button", 4096, NULL, 4, NULL); -// -// //----------------------------------- -// //--- create task for fan control --- -// //----------------------------------- -// //task that evaluates and processes the button input and runs the configured commands -// xTaskCreate(&task_fans, "task_fans", 2048, NULL, 1, NULL); -// -// -// //beep at startup -// buzzer.beep(3, 70, 50); -// -// //--- initialize nvs-flash and netif (needed for wifi) --- -// wifi_initNvs_initNetif(); -// -// //--- initialize spiffs --- -// init_spiffs(); + //------------------------------ + //--- create task for buzzer --- + //------------------------------ + xTaskCreate(&task_buzzer, "task_buzzer", 2048, NULL, 2, NULL); + + //------------------------------- + //--- create task for control --- + //------------------------------- + //task that generates motor commands depending on the current mode and sends those to motorctl task + xTaskCreate(&task_control, "task_control", 4096, NULL, 5, NULL); + + //------------------------------ + //--- create task for button --- + //------------------------------ + //task that evaluates and processes the button input and runs the configured commands + xTaskCreate(&task_button, "task_button", 4096, NULL, 4, NULL); + + //----------------------------------- + //--- create task for fan control --- + //----------------------------------- + //task that evaluates and processes the button input and runs the configured commands + xTaskCreate(&task_fans, "task_fans", 2048, NULL, 1, NULL); + + + //beep at startup + buzzer.beep(3, 70, 50); + + //--- initialize nvs-flash and netif (needed for wifi) --- + wifi_initNvs_initNetif(); + + //--- initialize spiffs --- + init_spiffs(); //--- initialize and start wifi --- //FIXME: run wifi_init_client or wifi_init_ap as intended from control.cpp when switching state @@ -235,6 +235,7 @@ extern "C" void app_main(void) { //--- main loop --- //does nothing except for testing things while(1){ + vTaskDelay(5000 / portTICK_PERIOD_MS); //test sabertooth driver // motors.setLeft({motorstate_t::FWD, 70}); // vTaskDelay(1000 / portTICK_PERIOD_MS); @@ -255,12 +256,12 @@ extern "C" void app_main(void) { //motorLeft.setTarget(motorstate_t::BRAKE); //vTaskDelay(1000 / portTICK_PERIOD_MS); //command 90% - reverse - motorLeft.setTarget(motorstate_t::REV, 90); - vTaskDelay(5000 / portTICK_PERIOD_MS); - motorLeft.setTarget(motorstate_t::FWD, 80); - vTaskDelay(1000 / portTICK_PERIOD_MS); - motorLeft.setTarget(motorstate_t::IDLE, 90); - vTaskDelay(1000 / portTICK_PERIOD_MS); + //motorLeft.setTarget(motorstate_t::REV, 90); + //vTaskDelay(5000 / portTICK_PERIOD_MS); + //motorLeft.setTarget(motorstate_t::FWD, 80); + //vTaskDelay(1000 / portTICK_PERIOD_MS); + //motorLeft.setTarget(motorstate_t::IDLE, 90); + //vTaskDelay(1000 / portTICK_PERIOD_MS); //--------------------------------- diff --git a/common/motorctl.cpp b/common/motorctl.cpp index fdfdd7f..1ff8417 100644 --- a/common/motorctl.cpp +++ b/common/motorctl.cpp @@ -81,7 +81,7 @@ void controlledMotor::handle(){ //--- receive commands from queue --- if( xQueueReceive( commandQueue, &commandReceive, ( TickType_t ) 0 ) ) { - ESP_LOGI(TAG, "Read command from queue: state=%s, duty=%.2f", motorstateStr[(int)commandReceive.state], commandReceive.duty); + ESP_LOGD(TAG, "Read command from queue: state=%s, duty=%.2f", motorstateStr[(int)commandReceive.state], commandReceive.duty); state = commandReceive.state; dutyTarget = commandReceive.duty; receiveTimeout = false; @@ -141,6 +141,7 @@ void controlledMotor::handle(){ if (state == motorstate_t::BRAKE){ ESP_LOGD(TAG, "braking - skip fading"); motorSetCommand({motorstate_t::BRAKE, dutyTarget}); + ESP_LOGI(TAG, "Set Motordriver: state=%s, duty=%.2f - Measurements: current=%.2f, speed=N/A", motorstateStr[(int)state], dutyNow, currentNow); //dutyNow = 0; return; //no need to run the fade algorithm } @@ -174,8 +175,8 @@ void controlledMotor::handle(){ //----- CURRENT LIMIT ----- + currentNow = cSensor.read(); if ((config.currentLimitEnabled) && (dutyDelta != 0)){ - currentNow = cSensor.read(); if (fabs(currentNow) > config.currentMax){ float dutyOld = dutyNow; //adaptive decrement: @@ -231,6 +232,7 @@ void controlledMotor::handle(){ //--- apply new target to motor --- motorSetCommand({state, (float)fabs(dutyNow)}); + ESP_LOGI(TAG, "Set Motordriver: state=%s, duty=%.2f - Measurements: current=%.2f, speed=N/A", motorstateStr[(int)state], dutyNow, currentNow); //note: BRAKE state is handled earlier diff --git a/common/motordrivers.cpp b/common/motordrivers.cpp index 3392c76..4fa581f 100644 --- a/common/motordrivers.cpp +++ b/common/motordrivers.cpp @@ -236,12 +236,13 @@ float motorCommandToSignedDuty(motorCommand_t cmd){ //-------- send byte -------- //send byte via uart to sabertooth driver void sabertooth2x60a::sendByte(char data){ + //TODO mutex? if (!uart_isInitialized){ ESP_LOGE(TAG, "uart not initialized, not sending command %d...", data); return; } uart_write_bytes(config.uart_num, &data, 1); - ESP_LOGI(TAG, "sent data=%d to sabertooth driver via uart", data); + ESP_LOGD(TAG, "sent data=%d to sabertooth driver via uart", data); } @@ -299,7 +300,7 @@ void sabertooth2x60a::setRight(float dutyPerSigned) { data = static_cast(192 + (dutyPerSigned / 100.0) * 63); } - ESP_LOGW(TAG, "set right motor to duty=%.2f, (data=%d)", dutyPerSigned, data); + ESP_LOGI(TAG, "set right motor to duty=%.2f, (data=%d)", dutyPerSigned, data); sendByte(data); }