Move config parameters from control to config
- create new struct control_config_t with several variables previously
  hardcoded in control.cpp
- modified constructor: add config parameter
- add definition of config struct in config.cpp
typedef struct control_config_t {
    controlMode_t defaultMode;  //default mode after startup and toggling IDLE
    //--- timeout ---
    uint32_t timeoutMs;         //time of inactivity after which the mode gets switched to IDLE
    float timeoutTolerancePer;  //percentage the duty can vary between timeout checks considered still inactive
    //--- http mode ---
    float http_toleranceZeroPer;//percentage around joystick axis the coordinate snaps to 0
    float http_toleranceEndPer; //percentage before joystick end the coordinate snaps to 1/-1
    uint32_t http_timeoutMs;    //time no new data was received before the motors get turned off
			
			
This commit is contained in:
		
							parent
							
								
									d32b0c5671
								
							
						
					
					
						commit
						f38940b7bf
					
				@ -39,6 +39,21 @@ controlledMotor motorRight(configDriverRight, configMotorControl);
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//------------------------------
 | 
				
			||||||
 | 
					//------- control config -------
 | 
				
			||||||
 | 
					//------------------------------
 | 
				
			||||||
 | 
					control_config_t configControl = {
 | 
				
			||||||
 | 
					    .defaultMode = controlMode_t::JOYSTICK, //default mode after startup and toggling IDLE
 | 
				
			||||||
 | 
					    //--- timeout ---    
 | 
				
			||||||
 | 
					    .timeoutMs = 30*1000,        //time of inactivity after which the mode gets switched to IDLE
 | 
				
			||||||
 | 
					    .timeoutTolerancePer = 5,    //percentage the duty can vary between timeout checks considered still inactive
 | 
				
			||||||
 | 
					    //--- http mode ---
 | 
				
			||||||
 | 
					    .http_toleranceZeroPer = 5,  //percentage around joystick axis the coordinate snaps to 0
 | 
				
			||||||
 | 
					    .http_toleranceEndPer = 2,   //percentage before joystick end the coordinate snaps to 1/-1
 | 
				
			||||||
 | 
					    .http_timeoutMs = 3000       //time no new data was received before the motors get turned off
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//--------------------------------------
 | 
					//--------------------------------------
 | 
				
			||||||
@ -95,7 +110,7 @@ gpio_evaluatedSwitch buttonJoystick(GPIO_NUM_33, true, false); //pullup true, no
 | 
				
			|||||||
buzzer_t buzzer(GPIO_NUM_12, 100);
 | 
					buzzer_t buzzer(GPIO_NUM_12, 100);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//create global control object
 | 
					//create global control object
 | 
				
			||||||
controlledArmchair control(&buzzer, &motorLeft, &motorRight);
 | 
					controlledArmchair control(configControl, &buzzer, &motorLeft, &motorRight);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -24,15 +24,21 @@ const char* controlModeStr[7] = {"IDLE", "JOYSTICK", "MASSAGE", "HTTP", "MQTT",
 | 
				
			|||||||
//-------- constructor --------
 | 
					//-------- constructor --------
 | 
				
			||||||
//-----------------------------
 | 
					//-----------------------------
 | 
				
			||||||
controlledArmchair::controlledArmchair (
 | 
					controlledArmchair::controlledArmchair (
 | 
				
			||||||
 | 
					        control_config_t config_f,
 | 
				
			||||||
        buzzer_t * buzzer_f,
 | 
					        buzzer_t * buzzer_f,
 | 
				
			||||||
        controlledMotor* motorLeft_f,
 | 
					        controlledMotor* motorLeft_f,
 | 
				
			||||||
        controlledMotor* motorRight_f
 | 
					        controlledMotor* motorRight_f
 | 
				
			||||||
        ){
 | 
					        ){
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    //copy configuration
 | 
				
			||||||
 | 
					    config = config_f;
 | 
				
			||||||
    //copy object pointers
 | 
					    //copy object pointers
 | 
				
			||||||
    buzzer = buzzer_f;
 | 
					    buzzer = buzzer_f;
 | 
				
			||||||
    motorLeft = motorLeft_f;
 | 
					    motorLeft = motorLeft_f;
 | 
				
			||||||
    motorRight = motorRight_f;
 | 
					    motorRight = motorRight_f;
 | 
				
			||||||
 | 
					    //set default mode from config
 | 
				
			||||||
 | 
					    modePrevious = config.defaultMode;
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
    //TODO declare / configure controlled motors here instead of config (unnecessary that button object is globally available - only used here)?
 | 
					    //TODO declare / configure controlled motors here instead of config (unnecessary that button object is globally available - only used here)?
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -79,6 +85,7 @@ void controlledArmchair::startHandleLoop() {
 | 
				
			|||||||
                break;
 | 
					                break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            case controlMode_t::HTTP:
 | 
					            case controlMode_t::HTTP:
 | 
				
			||||||
 | 
					                //TODO: outsource this code to http.cpp?
 | 
				
			||||||
                //create emptry struct for receiving data from http function
 | 
					                //create emptry struct for receiving data from http function
 | 
				
			||||||
                joystickData_t dataRead = { };
 | 
					                joystickData_t dataRead = { };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -91,10 +98,10 @@ void controlledArmchair::startHandleLoop() {
 | 
				
			|||||||
                            dataRead.x, dataRead.y, dataRead.radius, dataRead.angle);
 | 
					                            dataRead.x, dataRead.y, dataRead.radius, dataRead.angle);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    //--- scale coordinates ---
 | 
					                    //--- scale coordinates ---
 | 
				
			||||||
                    //note: scaleCoordinate function currently can not handle negative input -> add offset to input
 | 
					                    //note: scaleCoordinate function currently can not handle negative input -> added offset to input
 | 
				
			||||||
                    // scaleCoordinate(input, min, max, center, tolerance_zero_per, tolerance_end_per)
 | 
					                    // scaleCoordinate(input, min, max, center, tolerance_zero_per, tolerance_end_per)
 | 
				
			||||||
                    dataRead.x = scaleCoordinate(dataRead.x+1, 0, 2, 1, 5, 2); //TODO: move tolerance to config (virtualJoystick or control_Config_t?)
 | 
					                    dataRead.x = scaleCoordinate(dataRead.x+1, 0, 2, 1, config.http_toleranceZeroPer, config.http_toleranceEndPer); 
 | 
				
			||||||
                    dataRead.y = scaleCoordinate(dataRead.y+1, 0, 2, 1, 5, 2);
 | 
					                    dataRead.y = scaleCoordinate(dataRead.y+1, 0, 2, 1, config.http_toleranceZeroPer, config.http_toleranceEndPer);
 | 
				
			||||||
                    //--- calculate radius with new coordinates ---
 | 
					                    //--- calculate radius with new coordinates ---
 | 
				
			||||||
                    dataRead.radius = sqrt(pow(dataRead.x,2) + pow(dataRead.y,2));
 | 
					                    dataRead.radius = sqrt(pow(dataRead.x,2) + pow(dataRead.y,2));
 | 
				
			||||||
                    ESP_LOGD(TAG, "processed/scaled data: x=%.3f  y=%.3f  radius=%.3f", dataRead.x, dataRead.y, dataRead.radius);
 | 
					                    ESP_LOGD(TAG, "processed/scaled data: x=%.3f  y=%.3f  radius=%.3f", dataRead.x, dataRead.y, dataRead.radius);
 | 
				
			||||||
@ -113,7 +120,7 @@ void controlledArmchair::startHandleLoop() {
 | 
				
			|||||||
                //--- timeout ---
 | 
					                //--- timeout ---
 | 
				
			||||||
                //turn off motors when motor still on and no new data received for some time
 | 
					                //turn off motors when motor still on and no new data received for some time
 | 
				
			||||||
                if (
 | 
					                if (
 | 
				
			||||||
                        (esp_log_timestamp() - http_timestamp_lastData > 3000) //no data received for x seconds //TODO: move timeout to config
 | 
					                        (esp_log_timestamp() - http_timestamp_lastData > config.http_timeoutMs) //no data received for x seconds 
 | 
				
			||||||
                        && (commands.left.state != motorstate_t::IDLE || commands.right.state != motorstate_t::IDLE) //at least one motor is still running
 | 
					                        && (commands.left.state != motorstate_t::IDLE || commands.right.state != motorstate_t::IDLE) //at least one motor is still running
 | 
				
			||||||
                   ){
 | 
					                   ){
 | 
				
			||||||
                    ESP_LOGE(TAG, "TIMEOUT - no data received for 3s -> stopping motors");
 | 
					                    ESP_LOGE(TAG, "TIMEOUT - no data received for 3s -> stopping motors");
 | 
				
			||||||
@ -124,6 +131,7 @@ void controlledArmchair::startHandleLoop() {
 | 
				
			|||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                //TODO: add other modes here
 | 
					                //TODO: add other modes here
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -158,7 +166,6 @@ void controlledArmchair::resetTimeout(){
 | 
				
			|||||||
//------------------------------------
 | 
					//------------------------------------
 | 
				
			||||||
//---------- handleTimeout -----------
 | 
					//---------- handleTimeout -----------
 | 
				
			||||||
//------------------------------------
 | 
					//------------------------------------
 | 
				
			||||||
uint32_t msTimeout = 30000; //TODO move this to config #####################
 | 
					 | 
				
			||||||
float inactivityTolerance = 10; //percentage the duty can vary since last timeout check and still counts as incative
 | 
					float inactivityTolerance = 10; //percentage the duty can vary since last timeout check and still counts as incative
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//local function that checks whether two values differ more than a given tolerance
 | 
					//local function that checks whether two values differ more than a given tolerance
 | 
				
			||||||
@ -190,8 +197,8 @@ void controlledArmchair::handleTimeout(){
 | 
				
			|||||||
            dutyRight_lastActivity = dutyRightNow;
 | 
					            dutyRight_lastActivity = dutyRightNow;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        //no activity on any motor and msTimeout exceeded
 | 
					        //no activity on any motor and msTimeout exceeded
 | 
				
			||||||
        else if (esp_log_timestamp() - timestamp_lastActivity > msTimeout){
 | 
					        else if (esp_log_timestamp() - timestamp_lastActivity > config.timeoutMs){
 | 
				
			||||||
            ESP_LOGI(TAG, "timeout check: [TIMEOUT], no activity for more than %.ds  -> switch to idle", msTimeout/1000);
 | 
					            ESP_LOGI(TAG, "timeout check: [TIMEOUT], no activity for more than %.ds  -> switch to idle", config.timeoutMs/1000);
 | 
				
			||||||
            //toggle to idle mode
 | 
					            //toggle to idle mode
 | 
				
			||||||
            toggleIdle();
 | 
					            toggleIdle();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
				
			|||||||
@ -5,11 +5,27 @@
 | 
				
			|||||||
#include "buzzer.hpp"
 | 
					#include "buzzer.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//--------------------------------------------
 | 
				
			||||||
 | 
					//---- struct, enum, variable declarations ---
 | 
				
			||||||
 | 
					//--------------------------------------------
 | 
				
			||||||
//enum that decides how the motors get controlled
 | 
					//enum that decides how the motors get controlled
 | 
				
			||||||
enum class controlMode_t {IDLE, JOYSTICK, MASSAGE, HTTP, MQTT, BLUETOOTH, AUTO};
 | 
					enum class controlMode_t {IDLE, JOYSTICK, MASSAGE, HTTP, MQTT, BLUETOOTH, AUTO};
 | 
				
			||||||
//extern controlMode_t mode;
 | 
					//string array representing the mode enum (for printing the state as string)
 | 
				
			||||||
extern const char* controlModeStr[7];
 | 
					extern const char* controlModeStr[7];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//struct with config parameters
 | 
				
			||||||
 | 
					typedef struct control_config_t {
 | 
				
			||||||
 | 
					    controlMode_t defaultMode;  //default mode after startup and toggling IDLE
 | 
				
			||||||
 | 
					    //--- timeout ---
 | 
				
			||||||
 | 
					    uint32_t timeoutMs;         //time of inactivity after which the mode gets switched to IDLE
 | 
				
			||||||
 | 
					    float timeoutTolerancePer;  //percentage the duty can vary between timeout checks considered still inactive
 | 
				
			||||||
 | 
					    //--- http mode ---
 | 
				
			||||||
 | 
					    float http_toleranceZeroPer;//percentage around joystick axis the coordinate snaps to 0
 | 
				
			||||||
 | 
					    float http_toleranceEndPer; //percentage before joystick end the coordinate snaps to 1/-1
 | 
				
			||||||
 | 
					    uint32_t http_timeoutMs;    //time no new data was received before the motors get turned off
 | 
				
			||||||
 | 
					} control_config_t;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//==================================
 | 
					//==================================
 | 
				
			||||||
@ -21,6 +37,7 @@ class controlledArmchair {
 | 
				
			|||||||
    public:
 | 
					    public:
 | 
				
			||||||
        //--- constructor ---
 | 
					        //--- constructor ---
 | 
				
			||||||
        controlledArmchair (
 | 
					        controlledArmchair (
 | 
				
			||||||
 | 
					                control_config_t config_f,
 | 
				
			||||||
                buzzer_t* buzzer_f,
 | 
					                buzzer_t* buzzer_f,
 | 
				
			||||||
                controlledMotor* motorLeft_f,
 | 
					                controlledMotor* motorLeft_f,
 | 
				
			||||||
                controlledMotor* motorRight_f
 | 
					                controlledMotor* motorRight_f
 | 
				
			||||||
@ -56,6 +73,8 @@ class controlledArmchair {
 | 
				
			|||||||
        //---variables ---
 | 
					        //---variables ---
 | 
				
			||||||
        //struct for motor commands returned by generate functions of each mode
 | 
					        //struct for motor commands returned by generate functions of each mode
 | 
				
			||||||
        motorCommands_t commands;
 | 
					        motorCommands_t commands;
 | 
				
			||||||
 | 
					        //struct with config parameters
 | 
				
			||||||
 | 
					        control_config_t config;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        //variables for http mode
 | 
					        //variables for http mode
 | 
				
			||||||
        uint32_t http_timestamp_lastData = 0;
 | 
					        uint32_t http_timestamp_lastData = 0;
 | 
				
			||||||
@ -64,7 +83,7 @@ class controlledArmchair {
 | 
				
			|||||||
        controlMode_t mode = controlMode_t::IDLE;
 | 
					        controlMode_t mode = controlMode_t::IDLE;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        //variable to store mode when toggling IDLE mode 
 | 
					        //variable to store mode when toggling IDLE mode 
 | 
				
			||||||
        controlMode_t modePrevious = controlMode_t::JOYSTICK; //default mode
 | 
					        controlMode_t modePrevious; //default mode
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        //command preset for idling motors
 | 
					        //command preset for idling motors
 | 
				
			||||||
        const motorCommand_t cmd_motorIdle = {
 | 
					        const motorCommand_t cmd_motorIdle = {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user