Add task for each rest, sleep/wakeup task only when running
This commit is contained in:
parent
71460bbf87
commit
946fdd6c15
@ -286,9 +286,9 @@ extern "C" void app_main(void) {
|
||||
//-------------------------------------
|
||||
//-- create task for chairAdjustment --
|
||||
//-------------------------------------
|
||||
//task that stops chair-rest motors when they reach target
|
||||
chairAdjust_task_parameters_t chairAdjust_param = {legRest, backRest};
|
||||
xTaskCreate(&chairAdjust_task, "chairAdjust_task", 2048, &chairAdjust_param, 1, NULL);
|
||||
//tasks that stop chair-rest motors when they reach target (note: they sleep when motors not running)
|
||||
xTaskCreate(&chairAdjust_task, "chairAdjustLeg_task", 2048, legRest, 1, NULL);
|
||||
xTaskCreate(&chairAdjust_task, "chairAdjustBack_task", 2048, backRest, 1, NULL);
|
||||
|
||||
vTaskDelay(200 / portTICK_PERIOD_MS); //wait for all tasks to finish initializing
|
||||
printf("\n");
|
||||
|
@ -1,8 +1,5 @@
|
||||
extern "C"
|
||||
{
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_log.h"
|
||||
#include <string.h>
|
||||
}
|
||||
@ -108,6 +105,10 @@ void cControlledRest::setState(restState_t targetState)
|
||||
if (state != REST_OFF)
|
||||
updatePosition();
|
||||
|
||||
// activate handle task when turning on (previous state is off)
|
||||
if (state == REST_OFF)
|
||||
xTaskNotifyGive(taskHandle); //activate handle task that stops the rest-motor again
|
||||
|
||||
//apply new state
|
||||
ESP_LOGI(TAG, "[%s] switching from state '%s' to '%s'", name, restStateStr[state], restStateStr[targetState]);
|
||||
switch (targetState)
|
||||
@ -194,21 +195,22 @@ void cControlledRest::handle(){
|
||||
//============================
|
||||
//===== chairAdjust_task =====
|
||||
//============================
|
||||
#define CHAIR_ADJUST_HANDLE_TASK_DELAY 300
|
||||
void chairAdjust_task( void * pvParameters )
|
||||
#define CHAIR_ADJUST_HANDLE_TASK_DELAY 100
|
||||
void chairAdjust_task( void * pvParameter )
|
||||
{
|
||||
ESP_LOGW(TAG, "Starting chairAdjust task...");
|
||||
//get struct with pointers to all needed global objects from task parameter
|
||||
chairAdjust_task_parameters_t *objects = (chairAdjust_task_parameters_t *)pvParameters;
|
||||
cControlledRest * rest = (cControlledRest *)pvParameter;
|
||||
ESP_LOGW(TAG, "Starting task for controlling %s...", rest->getName());
|
||||
rest->taskHandle = xTaskGetCurrentTaskHandle();
|
||||
|
||||
// repeatedly update display with content depending on current mode
|
||||
while (1)
|
||||
{
|
||||
//TODO mutex
|
||||
//TODO add queue so task only runs when at least one rest is actually moving
|
||||
objects->legRest->handle();
|
||||
objects->backRest->handle();
|
||||
vTaskDelay(CHAIR_ADJUST_HANDLE_TASK_DELAY / portTICK_PERIOD_MS);
|
||||
ulTaskNotifyTake(pdTRUE, portMAX_DELAY); // wait for wakeup by setState() (rest-motor turned on)
|
||||
ESP_LOGW(TAG, "task %s: received notification -> activating task!", rest->getName());
|
||||
while (rest->getState() != REST_OFF){
|
||||
rest->handle();
|
||||
vTaskDelay(CHAIR_ADJUST_HANDLE_TASK_DELAY / portTICK_PERIOD_MS);
|
||||
}
|
||||
ESP_LOGW(TAG, "task %s: rest turned off -> sleeping task", rest->getName());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,10 @@
|
||||
#pragma once
|
||||
extern "C"
|
||||
{
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "driver/gpio.h"
|
||||
}
|
||||
|
||||
#include "joystick.hpp"
|
||||
|
||||
@ -20,10 +26,13 @@ class cControlledRest {
|
||||
public:
|
||||
cControlledRest(gpio_num_t gpio_up, gpio_num_t gpio_down, uint32_t travelDurationMs, const char *name, float defaultPosition = 0);
|
||||
void setState(restState_t targetState);
|
||||
restState_t getState() const {return state;};
|
||||
float getPercent(); //TODO update position first
|
||||
void setTargetPercent(float targetPercent);
|
||||
float getTargetPercent() const {return positionTarget;};
|
||||
void handle();
|
||||
const char * getName() const {return name;};
|
||||
TaskHandle_t taskHandle = NULL; //task that repeatedly runs the handle() method
|
||||
|
||||
private:
|
||||
void init();
|
||||
@ -40,19 +49,13 @@ private:
|
||||
|
||||
};
|
||||
|
||||
// struct with variables passed to task from main()
|
||||
typedef struct chairAdjust_task_parameters_t {
|
||||
cControlledRest * legRest;
|
||||
cControlledRest * backRest;
|
||||
//buzzer_t *buzzer;
|
||||
} chairAdjust_task_parameters_t;
|
||||
|
||||
|
||||
|
||||
//===========================
|
||||
//==== chairAdjust_task =====
|
||||
//===========================
|
||||
void chairAdjust_task( void * chairAdjust_task_parameters_t );
|
||||
// repeatedly runs handle method of specified ControlledRest object to turn of the rest, when activated by setState()
|
||||
void chairAdjust_task( void * cControlledRest );
|
||||
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user