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:
jonny_l480 2022-08-11 11:21:29 +02:00
parent 24d89b96cc
commit 6dfae934d5
5 changed files with 50 additions and 26 deletions

View File

@ -1,4 +1,5 @@
#include "auto.hpp"
#include "config.hpp"
//tag for logging
static const char * TAG = "automatedArmchair";
@ -23,6 +24,11 @@ motorCommands_t automatedArmchair::generateCommands() {
//get next command from queue
if( xQueueReceive( commandQueue, &cmdCurrent, pdMS_TO_TICKS(500) ) ) {
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
timestampCmdFinished = esp_log_timestamp() + cmdCurrent.msDuration;
//copy the new commands
@ -35,6 +41,7 @@ motorCommands_t automatedArmchair::generateCommands() {
ESP_LOGD(TAG, "command still running -> no change");
}
//TODO also return instructions via call by reference
return motorCommands;
}

View File

@ -18,7 +18,7 @@ extern "C"
//---- struct, enum, variable declarations ---
//--------------------------------------------
//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
//e.g. put motors in a certain state for certain time
@ -26,8 +26,8 @@ typedef struct commandSimple_t{
motorCommands_t motorCmds;
uint32_t msDuration;
uint32_t fadeDecel;
uint32_t fadeAcel;
instructions_t instructions;
uint32_t fadeAccel;
auto_instruction_t instruction;
} commandSimple_t;

View File

@ -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
//====== TESTING: send cmd to auto mode =====
//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] =
{
.motorCmds = {
.left = {motorstate_t::FWD, 40},
.right = {motorstate_t::FWD, 40}
.left = {motorstate_t::FWD, 50},
.right = {motorstate_t::FWD, 50}
},
.msDuration = 2000,
.fadeDecel = 800,
.fadeAcel = 1300,
.instructions = instructions_t::NONE
.msDuration = 100,
.fadeDecel = 0,
.fadeAccel = 400,
.instruction = auto_instruction_t::NONE
};
cmds[2] =
{
@ -77,21 +88,10 @@ void buttonCommands::action (uint8_t count){
.left = {motorstate_t::IDLE, 0},
.right = {motorstate_t::IDLE, 0}
},
.msDuration = 1000,
.msDuration = 10,
.fadeDecel = 800,
.fadeAcel = 1300,
.instructions = instructions_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
.fadeAccel = 1300,
.instruction = auto_instruction_t::NONE
};
//send commands to automatedArmchair command queue

View File

@ -126,6 +126,10 @@ void controlledArmchair::startHandleLoop() {
vTaskDelay(20 / portTICK_PERIOD_MS);
//generate commands
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;
@ -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]);
//--- run functions when changing FROM certain mode ---
//========== commands change FROM mode ==========
//run functions when changing FROM certain mode
switch(modePrevious){
default:
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
freezeInput = false;
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){
default:
ESP_LOGI(TAG, "noting to execute when changing TO this mode");

View File

@ -176,7 +176,7 @@ extern "C" void app_main(void) {
//--- create task for button ---
//------------------------------
//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 ---