Fix stack overflow, apply and restore fading [functional]
auto.cpp: apply fadeAccel and fadeDecel from commands to motors main.cpp: double stack size of button task (crashed controller every time) button.cpp: Modify test command to actually eject armchair leg support control.cpp: apply generated motor commands to motors rename some variables: typo: fadeAccel more specific: auto_instruction_t
This commit is contained in:
		
							parent
							
								
									24d89b96cc
								
							
						
					
					
						commit
						6dfae934d5
					
				@ -1,4 +1,5 @@
 | 
				
			|||||||
#include "auto.hpp"
 | 
					#include "auto.hpp"
 | 
				
			||||||
 | 
					#include "config.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//tag for logging
 | 
					//tag for logging
 | 
				
			||||||
static const char * TAG = "automatedArmchair";
 | 
					static const char * TAG = "automatedArmchair";
 | 
				
			||||||
@ -23,6 +24,11 @@ motorCommands_t automatedArmchair::generateCommands() {
 | 
				
			|||||||
        //get next command from queue
 | 
					        //get next command from queue
 | 
				
			||||||
        if( xQueueReceive( commandQueue, &cmdCurrent, pdMS_TO_TICKS(500) ) ) {
 | 
					        if( xQueueReceive( commandQueue, &cmdCurrent, pdMS_TO_TICKS(500) ) ) {
 | 
				
			||||||
            ESP_LOGI(TAG, "running next command from queue...");
 | 
					            ESP_LOGI(TAG, "running next command from queue...");
 | 
				
			||||||
 | 
					            //set acceleration / fading parameters according to command
 | 
				
			||||||
 | 
					            motorLeft.setFade(fadeType_t::DECEL, cmdCurrent.fadeDecel);
 | 
				
			||||||
 | 
					            motorRight.setFade(fadeType_t::DECEL, cmdCurrent.fadeDecel);
 | 
				
			||||||
 | 
					            motorLeft.setFade(fadeType_t::ACCEL, cmdCurrent.fadeAccel);
 | 
				
			||||||
 | 
					            motorRight.setFade(fadeType_t::ACCEL, cmdCurrent.fadeAccel);
 | 
				
			||||||
            //calculate timestamp the command is finished
 | 
					            //calculate timestamp the command is finished
 | 
				
			||||||
            timestampCmdFinished = esp_log_timestamp() + cmdCurrent.msDuration;
 | 
					            timestampCmdFinished = esp_log_timestamp() + cmdCurrent.msDuration;
 | 
				
			||||||
            //copy the new commands
 | 
					            //copy the new commands
 | 
				
			||||||
@ -35,6 +41,7 @@ motorCommands_t automatedArmchair::generateCommands() {
 | 
				
			|||||||
        ESP_LOGD(TAG, "command still running -> no change");
 | 
					        ESP_LOGD(TAG, "command still running -> no change");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    //TODO also return instructions via call by reference
 | 
				
			||||||
    return motorCommands;
 | 
					    return motorCommands;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -18,7 +18,7 @@ extern "C"
 | 
				
			|||||||
//---- struct, enum, variable declarations ---
 | 
					//---- struct, enum, variable declarations ---
 | 
				
			||||||
//--------------------------------------------
 | 
					//--------------------------------------------
 | 
				
			||||||
//enum for special instructions / commands to be run in control task
 | 
					//enum for special instructions / commands to be run in control task
 | 
				
			||||||
enum class instructions_t { NONE, SWITCH_PREV_MODE, RESET_ACCEL, RESET_DECEL };
 | 
					enum class auto_instruction_t { NONE, SWITCH_PREV_MODE, RESET_ACCEL, RESET_DECEL };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//struct for a simple command
 | 
					//struct for a simple command
 | 
				
			||||||
//e.g. put motors in a certain state for certain time
 | 
					//e.g. put motors in a certain state for certain time
 | 
				
			||||||
@ -26,8 +26,8 @@ typedef struct commandSimple_t{
 | 
				
			|||||||
    motorCommands_t motorCmds;
 | 
					    motorCommands_t motorCmds;
 | 
				
			||||||
    uint32_t msDuration;
 | 
					    uint32_t msDuration;
 | 
				
			||||||
    uint32_t fadeDecel;
 | 
					    uint32_t fadeDecel;
 | 
				
			||||||
    uint32_t fadeAcel;
 | 
					    uint32_t fadeAccel;
 | 
				
			||||||
    instructions_t instructions;
 | 
					    auto_instruction_t instruction;
 | 
				
			||||||
} commandSimple_t;
 | 
					} commandSimple_t;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -60,16 +60,27 @@ void buttonCommands::action (uint8_t count){
 | 
				
			|||||||
            ////////control->sendButtonEvent(count); //TODO: always send button event to control task (not just at count=1) -> control.cpp has to be changed
 | 
					            ////////control->sendButtonEvent(count); //TODO: always send button event to control task (not just at count=1) -> control.cpp has to be changed
 | 
				
			||||||
            //====== TESTING: send cmd to auto mode =====
 | 
					            //====== TESTING: send cmd to auto mode =====
 | 
				
			||||||
            //define commands
 | 
					            //define commands
 | 
				
			||||||
 | 
					            cmds[0] =
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                .motorCmds = {
 | 
				
			||||||
 | 
					                    .left = {motorstate_t::REV, 90},
 | 
				
			||||||
 | 
					                    .right = {motorstate_t::REV, 90}
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					                .msDuration = 700,
 | 
				
			||||||
 | 
					                .fadeDecel = 800,
 | 
				
			||||||
 | 
					                .fadeAccel = 1200,
 | 
				
			||||||
 | 
					                .instruction = auto_instruction_t::NONE
 | 
				
			||||||
 | 
					            };
 | 
				
			||||||
            cmds[1] =
 | 
					            cmds[1] =
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                .motorCmds = {
 | 
					                .motorCmds = {
 | 
				
			||||||
                    .left = {motorstate_t::FWD, 40},
 | 
					                    .left = {motorstate_t::FWD, 50},
 | 
				
			||||||
                    .right = {motorstate_t::FWD, 40}
 | 
					                    .right = {motorstate_t::FWD, 50}
 | 
				
			||||||
                },
 | 
					                },
 | 
				
			||||||
                .msDuration = 2000,
 | 
					                .msDuration = 100,
 | 
				
			||||||
                .fadeDecel = 800,
 | 
					                .fadeDecel = 0,
 | 
				
			||||||
                .fadeAcel = 1300,
 | 
					                .fadeAccel = 400,
 | 
				
			||||||
                .instructions = instructions_t::NONE
 | 
					                .instruction = auto_instruction_t::NONE
 | 
				
			||||||
            };
 | 
					            };
 | 
				
			||||||
            cmds[2] =
 | 
					            cmds[2] =
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
@ -77,21 +88,10 @@ void buttonCommands::action (uint8_t count){
 | 
				
			|||||||
                    .left = {motorstate_t::IDLE, 0},
 | 
					                    .left = {motorstate_t::IDLE, 0},
 | 
				
			||||||
                    .right = {motorstate_t::IDLE, 0}
 | 
					                    .right = {motorstate_t::IDLE, 0}
 | 
				
			||||||
                },
 | 
					                },
 | 
				
			||||||
                .msDuration = 1000,
 | 
					                .msDuration = 10,
 | 
				
			||||||
                .fadeDecel = 800,
 | 
					                .fadeDecel = 800,
 | 
				
			||||||
                .fadeAcel = 1300,
 | 
					                .fadeAccel = 1300,
 | 
				
			||||||
                .instructions = instructions_t::NONE
 | 
					                .instruction = auto_instruction_t::NONE
 | 
				
			||||||
            };
 | 
					 | 
				
			||||||
            cmds[3] =
 | 
					 | 
				
			||||||
            {
 | 
					 | 
				
			||||||
                .motorCmds = {
 | 
					 | 
				
			||||||
                    .left = {motorstate_t::REV, 40},
 | 
					 | 
				
			||||||
                    .right = {motorstate_t::REV, 40}
 | 
					 | 
				
			||||||
                },
 | 
					 | 
				
			||||||
                .msDuration = 1000,
 | 
					 | 
				
			||||||
                .fadeDecel = 800,
 | 
					 | 
				
			||||||
                .fadeAcel = 1300,
 | 
					 | 
				
			||||||
                .instructions = instructions_t::NONE
 | 
					 | 
				
			||||||
            };
 | 
					            };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            //send commands to automatedArmchair command queue
 | 
					            //send commands to automatedArmchair command queue
 | 
				
			||||||
 | 
				
			|||||||
@ -126,6 +126,10 @@ void controlledArmchair::startHandleLoop() {
 | 
				
			|||||||
                vTaskDelay(20 / portTICK_PERIOD_MS);
 | 
					                vTaskDelay(20 / portTICK_PERIOD_MS);
 | 
				
			||||||
               //generate commands
 | 
					               //generate commands
 | 
				
			||||||
               commands = armchair.generateCommands();
 | 
					               commands = armchair.generateCommands();
 | 
				
			||||||
 | 
					                //--- apply commands to motors ---
 | 
				
			||||||
 | 
					                //TODO make motorctl.setTarget also accept motorcommand struct directly
 | 
				
			||||||
 | 
					                motorRight->setTarget(commands.right.state, commands.right.duty); 
 | 
				
			||||||
 | 
					                motorLeft->setTarget(commands.left.state, commands.left.duty); 
 | 
				
			||||||
               break;
 | 
					               break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -274,7 +278,8 @@ void controlledArmchair::changeMode(controlMode_t modeNew) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    ESP_LOGW(TAG, "=== changing mode from %s to %s ===", controlModeStr[(int)mode], controlModeStr[(int)modeNew]);
 | 
					    ESP_LOGW(TAG, "=== changing mode from %s to %s ===", controlModeStr[(int)mode], controlModeStr[(int)modeNew]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    //--- run functions when changing FROM certain mode ---
 | 
					    //========== commands change FROM mode ==========
 | 
				
			||||||
 | 
					    //run functions when changing FROM certain mode
 | 
				
			||||||
    switch(modePrevious){
 | 
					    switch(modePrevious){
 | 
				
			||||||
        default:
 | 
					        default:
 | 
				
			||||||
            ESP_LOGI(TAG, "noting to execute when changing FROM this mode");
 | 
					            ESP_LOGI(TAG, "noting to execute when changing FROM this mode");
 | 
				
			||||||
@ -307,10 +312,22 @@ void controlledArmchair::changeMode(controlMode_t modeNew) {
 | 
				
			|||||||
            //reset frozen input state
 | 
					            //reset frozen input state
 | 
				
			||||||
            freezeInput = false;
 | 
					            freezeInput = false;
 | 
				
			||||||
            break;
 | 
					            break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        case controlMode_t::AUTO:
 | 
				
			||||||
 | 
					            ESP_LOGW(TAG, "switching from AUTO mode -> restoring fading to default");
 | 
				
			||||||
 | 
					            //TODO: fix issue when downfading was disabled before switching to auto mode - currently it gets enabled again here...
 | 
				
			||||||
 | 
					            //enable downfading (set to default value)
 | 
				
			||||||
 | 
					            motorLeft->setFade(fadeType_t::DECEL, true);
 | 
				
			||||||
 | 
					            motorRight->setFade(fadeType_t::DECEL, true);
 | 
				
			||||||
 | 
					            //set upfading to default value
 | 
				
			||||||
 | 
					            motorLeft->setFade(fadeType_t::ACCEL, true);
 | 
				
			||||||
 | 
					            motorRight->setFade(fadeType_t::ACCEL, true);
 | 
				
			||||||
 | 
					            break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    //--- run functions when changing TO certain mode ---
 | 
					    //========== commands change TO mode ==========
 | 
				
			||||||
 | 
					    //run functions when changing TO certain mode
 | 
				
			||||||
    switch(modeNew){
 | 
					    switch(modeNew){
 | 
				
			||||||
        default:
 | 
					        default:
 | 
				
			||||||
            ESP_LOGI(TAG, "noting to execute when changing TO this mode");
 | 
					            ESP_LOGI(TAG, "noting to execute when changing TO this mode");
 | 
				
			||||||
 | 
				
			|||||||
@ -176,7 +176,7 @@ extern "C" void app_main(void) {
 | 
				
			|||||||
    //--- create task for button ---
 | 
					    //--- create task for button ---
 | 
				
			||||||
    //------------------------------
 | 
					    //------------------------------
 | 
				
			||||||
    //task that evaluates and processes the button input and runs the configured commands
 | 
					    //task that evaluates and processes the button input and runs the configured commands
 | 
				
			||||||
    xTaskCreate(&task_button, "task_button", 2048, NULL, 4, NULL);
 | 
					    xTaskCreate(&task_button, "task_button", 4096, NULL, 4, NULL);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    //-----------------------------------
 | 
					    //-----------------------------------
 | 
				
			||||||
    //--- create task for fan control ---
 | 
					    //--- create task for fan control ---
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user