Hello. I am playing with FreeRTOS and classes and found an issue. All I am trying to do is to start task1, count to 10 and call my custom method
State_change_handle which is going to delete the current task and start a new one. However, the issue is that the program hangs after the vTaskDelete is called. My cpp file:
my .h file:
In my main.c I just create class object and call begin method. Then the task1 will start. The serial monitor:
As you can see from the serial monitor, nothing else happens after vTaskDelete. Could someone help me understand what is the issue?
State_change_handle which is going to delete the current task and start a new one. However, the issue is that the program hangs after the vTaskDelete is called. My cpp file:
Code:
#include "Controller.h"
Controller::Controller(){
printf("Thermostat object created \n");
}
void Controller::begin(){
this->test1 = 1;
this->old_state = INITIAL;
this->new_state = INITIAL;
this->main_task_handle = NULL;
this->secondary_task_handle = NULL;
printf("Start the task1 \n");
this->State_change_handle(TASK1);
//xTaskCreate(task3,"TASK3",10000,(void*)this,1,&this->secondary_task_handle); // receiving commands from main uart
}
void Controller::State_change_handle(e_thermostat_state state)
{
this-> new_state = state;
if(this-> old_state != this-> new_state ){ // if the new state is different than current state, delete the task
if (this ->main_task_handle != NULL){
printf("different state, delete the previous task \n");
printf(" deleting task with name = %s \n",pcTaskGetTaskName(this->main_task_handle));
vTaskDelete(this->main_task_handle);
printf("task deleted sucesfully \n"); // this never prints for some reason
}
else{
printf("no tasks currently running \n");
}
}
else
{
printf("same task \n"); // the same task is set, dont do anything
return;
}
switch(this->new_state)
{
case TASK1:
printf("NEW STATE = TASK1 \n");
this-> old_state = TASK1;
xTaskCreate(task1,"TASK1",10000,(void*)this,1,&this->main_task_handle); // receiving commands from main uart
break;
case TASK2:
printf("NEW STATE = MODE2\n");
this-> old_state = TASK2;
xTaskCreate(task2,"TASK2",10000,(void*)this,1,&this->main_task_handle); // receiving commands from main uart
break;
default:
printf("state not recognised \n");
}
}
Code:
#ifndef CONTROLLER_H
#define CONTROLLER_H
#include "stdint.h"
#include "stdio.h"
//#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
//#include "esp_system.h"
//#include "esp_log.h"
//const char* TAG1 ="RTOS";
enum e_thermostat_state
{
INITIAL,
TASK1,
TASK2,
TASK3,
MAX_STATES
};
class Controller
{
private://only accesible for class
int test1;
e_thermostat_state new_state;
e_thermostat_state old_state;
TaskHandle_t main_task_handle;
TaskHandle_t secondary_task_handle;
public:
//accesible outside class
Controller(); // INIT OBJECT
void begin();
void State_change_handle(e_thermostat_state state);
static void task1(void* parameters)
{
Controller* controller = (Controller*)parameters;
for(;;)
{
controller->test1++;
printf("hello from task 1\n");
if(controller->test1 % 10 == 0){
printf("changing to TASK2\n");
controller->test1 = 1;
controller->State_change_handle(TASK2);
}
vTaskDelay(1000/portTICK_RATE_MS);
}
}
static void task2(void* parameters)
{
Controller* controller = (Controller*)parameters;
for(;;)
{
printf("hello from task 2\n");
controller->test1 ++;
if(controller->test1 % 5==0){
printf("changing to TASK1\n");
controller->test1 = 1;
controller->State_change_handle(TASK1);
}
vTaskDelay(2000/portTICK_RATE_MS);
}
}
static void task3(void* parameters)
{
Controller* controller = (Controller*)parameters;
for(;;)
{
printf("hello from task 3\n");
vTaskDelay(3000/portTICK_RATE_MS);
}
}
//static void normal_operation_task(void *argument); // DECLARE DEFAULT THERMOSTAT STATES AND VARS
};
#endif
Code:
Start the task1
no tasks currently running
NEW STATE = TASK1
hello from task 1
hello from task 1
hello from task 1
hello from task 1
hello from task 1
hello from task 1
hello from task 1
hello from task 1
hello from task 1
changing to TASK2
different state, delete the previous task
deleting task with name = TASK1