Major Rework all files - Pass pointers to tasks, Remove gloabl variables

- All files:
  Modify almost all files to adjust functions and classes to
  work with pointers to objects passed at task creation
  instead of global variables from config.hpp

- Remove/clear config.hpp to get rid of all global variables

- main.cpp
    - Create pointer to all shared (used in multiple tasks) objects in main

- remove evaluatedSwitch button object,
  since joystick library is used to get switch events

- changes HTTP-mode
    - always init http-server (do not enable/disable at mode change)
    - pass url-handle function to init-htpp function
    - add lambda function to pass method of instance for thatMajor Rework all files - Remove global variables, pass pointers to tasks

- All files:
  Modify almost all files to adjust functions and classes to
  work with pointers to objects passed at task creation
  instead of global variables from config.hpp

- Remove/clear config.hpp to get rid of all global variables

- main.cpp
    - Create pointer to all shared (used in multiple tasks) objects in main

- remove evaluatedSwitch button object,
  since joystick library is used to get switch events

- changes HTTP-mode
    - always init http-server (do not enable/disable at mode change)
    - pass url-handle function to init-htpp function
    - add lambda function to pass method of instance for that

NOTES:  - tested on breakoutboard only
        - known issue that slow encoder events are not recognized
        (especially in menu) - slowing down motorctl helps
This commit is contained in:
jonny_jr9
2024-02-18 10:00:34 +01:00
parent 40a0f56208
commit 2fcf17feda
20 changed files with 618 additions and 469 deletions

View File

@@ -201,27 +201,17 @@ joystickData_t httpJoystick::getData(){
}
//--------------------------------------------
//--- receiveHttpData for httpJoystickMain ---
//--------------------------------------------
//function that wraps pointer to member function of httpJoystickMain instance in a "normal" function which the webserver can run on joystick URL
//declare pointer to receiveHttpData method of httpJoystick class
esp_err_t (httpJoystick::*pointerToReceiveFunc)(httpd_req_t *req) = &httpJoystick::receiveHttpData;
esp_err_t on_joystick_url(httpd_req_t *req){
//run pointer to receiveHttpData function of httpJoystickMain instance
return (httpJoystickMain.*pointerToReceiveFunc)(req);
}
//============================
//===== init http server =====
//============================
//function that initializes http server and configures available urls
void http_init_server()
//function that initializes http server and configures available url's
//parameter: provide pointer to function that handle incomming joystick data (for configuring the url)
//TODO add handle functions to future additional endpoints/urls here too
void http_init_server(http_handler_t onJoystickUrl)
{
ESP_LOGI(TAG, "initializing HTTP-Server...");
//---- configure webserver ----
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
@@ -236,7 +226,7 @@ void http_init_server()
httpd_uri_t joystick_url = {
.uri = "/api/joystick",
.method = HTTP_POST,
.handler = on_joystick_url,
.handler = onJoystickUrl,
};
httpd_register_uri_handler(server, &joystick_url);
@@ -265,8 +255,8 @@ void http_init_server()
//function that destroys the http server
void http_stop_server()
{
printf("stopping http\n");
httpd_stop(server);
ESP_LOGW(TAG, "stopping HTTP-Server");
httpd_stop(server);
}

View File

@@ -13,7 +13,18 @@ extern "C"
//===== init http server =====
//============================
//function that initializes http server and configures available urls
void http_init_server();
//parameter: provide pointer to function that handles incomming joystick data (for configuring the url)
//TODO add handle functions to future additional endpoints/urls here too
typedef esp_err_t (*http_handler_t)(httpd_req_t *req);
void http_init_server(http_handler_t onJoystickUrl);
//example with lambda function to pass method of a class instance:
//esp_err_t (httpJoystick::*pointerToReceiveFunc)(httpd_req_t *req) = &httpJoystick::receiveHttpData;
//esp_err_t on_joystick_url(httpd_req_t *req){
// //run pointer to receiveHttpData function of httpJoystickMain instance
// return (httpJoystickMain->*pointerToReceiveFunc)(req);
//}
//http_init_server(on_joystick_url);
//==============================
@@ -27,7 +38,7 @@ void start_mdns_service();
//===== stop http server =====
//============================
//function that destroys the http server
void http_stop_server();
void http_stop_server(httpd_handle_t * httpServer);
//==============================
@@ -47,7 +58,7 @@ typedef struct httpJoystick_config_t {
class httpJoystick{
public:
//--- constructor ---
httpJoystick( httpJoystick_config_t config_f );
httpJoystick(httpJoystick_config_t config_f);
//--- functions ---
joystickData_t getData(); //wait for and return joystick data from queue, if timeout return CENTER
@@ -67,11 +78,4 @@ class httpJoystick{
.radius = 0,
.angle = 0
};
};
//===== global object =====
//create global instance of httpJoystick
//note: is constructed/configured in config.cpp
extern httpJoystick httpJoystickMain;
};

View File

@@ -7,6 +7,24 @@ static const char * TAG = "motor-control";
#define TIMEOUT_IDLE_WHEN_NO_COMMAND 8000
//====================================
//========== motorctl task ===========
//====================================
//task for handling the motors (ramp, current limit, driver)
void task_motorctl( void * task_motorctl_parameters ){
task_motorctl_parameters_t *objects = (task_motorctl_parameters_t *)task_motorctl_parameters;
ESP_LOGW(TAG, "Task-motorctl: starting handle loop...");
while(1){
objects->motorRight->handle();
objects->motorLeft->handle();
vTaskDelay(20 / portTICK_PERIOD_MS);
}
}
//=============================
//======== constructor ========
//=============================
@@ -33,6 +51,11 @@ controlledMotor::controlledMotor(motorSetCommandFunc_t setCommandFunc, motorctl
//============================
void controlledMotor::init(){
commandQueue = xQueueCreate( 1, sizeof( struct motorCommand_t ) );
if (commandQueue == NULL)
ESP_LOGE(TAG, "Failed to create command-queue");
else
ESP_LOGW(TAG, "Initialized command-queue");
//cSensor.calibrateZeroAmpere(); //currently done in currentsensor constructor TODO do this regularly e.g. in idle?
}
@@ -252,11 +275,11 @@ void controlledMotor::setTarget(motorstate_t state_f, float duty_f){
.state = state_f,
.duty = duty_f
};
ESP_LOGD(TAG, "Inserted command to queue: state=%s, duty=%.2f", motorstateStr[(int)commandSend.state], commandSend.duty);
ESP_LOGI(TAG, "setTarget: Inserting command to queue: state='%s'(%d), duty=%.2f", motorstateStr[(int)commandSend.state], (int)commandSend.state, commandSend.duty);
//send command to queue (overwrite if an old command is still in the queue and not processed)
xQueueOverwrite( commandQueue, ( void * )&commandSend);
//xQueueSend( commandQueue, ( void * )&commandSend, ( TickType_t ) 0 );
ESP_LOGD(TAG, "finished inserting new command");
}

View File

@@ -86,3 +86,21 @@ class controlledMotor {
uint32_t timestamp_commandReceived = 0;
bool receiveTimeout = false;
};
// struct with variables passed to task from main
typedef struct task_motorctl_parameters_t {
controlledMotor * motorLeft;
controlledMotor * motorRight;
} task_motorctl_parameters_t;
//====================================
//========== motorctl task ===========
//====================================
//task that inititialized the display, displays welcome message
//and releatedly updates the display with certain content
//note: pointer to required objects have to be provided as task-parameter
void task_motorctl( void * task_motorctl_parameters );