Create class 'automatedArmchair', Add AUTO mode
- Create class automatedArmchair in new files auto.hpp and auto.cpp This class currently makes it possible to queue up and run commands (motorstate, duty for certaion duration), - add new file to cmakelists - create automatedArmchair instance 'armchair' in config.cpp - add loglevel configuration to main.cpp - add AUTO mode to control.cpp (no way to change to mode yet)
This commit is contained in:
		
							parent
							
								
									66756b9b75
								
							
						
					
					
						commit
						f8415655c2
					
				| @ -11,6 +11,7 @@ idf_component_register( | |||||||
|         "fan.cpp" |         "fan.cpp" | ||||||
|         "wifi.c" |         "wifi.c" | ||||||
|         "http.cpp" |         "http.cpp" | ||||||
|  |         "auto.cpp" | ||||||
|     INCLUDE_DIRS  |     INCLUDE_DIRS  | ||||||
|         "." |         "." | ||||||
|     ) |     ) | ||||||
|  | |||||||
							
								
								
									
										70
									
								
								main/auto.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										70
									
								
								main/auto.cpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,70 @@ | |||||||
|  | #include "auto.hpp" | ||||||
|  | 
 | ||||||
|  | //tag for logging
 | ||||||
|  | static const char * TAG = "automatedArmchair"; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | //=============================
 | ||||||
|  | //======== constructor ========
 | ||||||
|  | //=============================
 | ||||||
|  | automatedArmchair::automatedArmchair(void) { | ||||||
|  |     //create command queue
 | ||||||
|  |     commandQueue = xQueueCreate( 32, sizeof( commandSimple_t ) ); //TODO add max size to config?
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | //==============================
 | ||||||
|  | //====== generateCommands ======
 | ||||||
|  | //==============================
 | ||||||
|  | motorCommands_t automatedArmchair::generateCommands() { | ||||||
|  |     //check if previous command is finished
 | ||||||
|  |     if ( esp_log_timestamp() > timestampCmdFinished ) { | ||||||
|  |         //get next command from queue
 | ||||||
|  |         if( xQueueReceive( commandQueue, &cmdCurrent, pdMS_TO_TICKS(0) ) ) { | ||||||
|  |             ESP_LOGI(TAG, "running next command from queue..."); | ||||||
|  |             //calculate timestamp the command is finished
 | ||||||
|  |             timestampCmdFinished = esp_log_timestamp() + cmdCurrent.msDuration; | ||||||
|  |             //copy the new commands
 | ||||||
|  |             motorCommands = cmdCurrent.commands; | ||||||
|  |         } else { //queue empty
 | ||||||
|  |             ESP_LOGD(TAG, "no new command in queue -> set motors to IDLE"); | ||||||
|  |             motorCommands = cmds_bothMotorsIdle; | ||||||
|  |         } | ||||||
|  |     } else { //previous command still running
 | ||||||
|  |         ESP_LOGD(TAG, "command still running -> no change"); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     return motorCommands; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | //============================
 | ||||||
|  | //======== addCommand ========
 | ||||||
|  | //============================
 | ||||||
|  | //function that adds a basic command to the queue
 | ||||||
|  | void automatedArmchair::addCommand(commandSimple_t command) { | ||||||
|  |     //add command to queue
 | ||||||
|  |      if ( xQueueSend( commandQueue, ( void * )&command, ( TickType_t ) 0 ) ){ | ||||||
|  |          ESP_LOGI(TAG, "Successfully inserted command to queue"); | ||||||
|  |      } else { | ||||||
|  |          ESP_LOGE(TAG, "Failed to insert new command to queue"); | ||||||
|  |      } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | //===============================
 | ||||||
|  | //======== clearCommands ========
 | ||||||
|  | //===============================
 | ||||||
|  | //function that deletes all pending/queued commands
 | ||||||
|  | //e.g. when switching modes
 | ||||||
|  | motorCommands_t automatedArmchair::clearCommands() { | ||||||
|  |     //clear command queue
 | ||||||
|  |     xQueueReset( commandQueue ); | ||||||
|  |     ESP_LOGW(TAG, "command queue was successfully emptied"); | ||||||
|  |     //return commands for idling both motors
 | ||||||
|  |     return cmds_bothMotorsIdle; | ||||||
|  | } | ||||||
|  | 
 | ||||||
							
								
								
									
										74
									
								
								main/auto.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										74
									
								
								main/auto.hpp
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,74 @@ | |||||||
|  | #pragma once | ||||||
|  | 
 | ||||||
|  | extern "C" | ||||||
|  | { | ||||||
|  | #include "freertos/FreeRTOS.h" | ||||||
|  | #include "freertos/task.h" | ||||||
|  | #include "esp_log.h" | ||||||
|  | #include "esp_err.h" | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | #include "freertos/queue.h" | ||||||
|  | #include <cmath> | ||||||
|  | #include "motorctl.hpp" | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | //--------------------------------------------
 | ||||||
|  | //---- struct, enum, variable declarations ---
 | ||||||
|  | //--------------------------------------------
 | ||||||
|  | //struct for a simple command
 | ||||||
|  | //e.g. put motors in a certain state for certain time
 | ||||||
|  | typedef struct { | ||||||
|  |     motorCommands_t commands; | ||||||
|  |     uint32_t msDuration; | ||||||
|  | } commandSimple_t; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | //------------------------------------
 | ||||||
|  | //----- automatedArmchair class  -----
 | ||||||
|  | //------------------------------------
 | ||||||
|  | class automatedArmchair { | ||||||
|  |     public: | ||||||
|  |         //--- methods ---
 | ||||||
|  |         //constructor
 | ||||||
|  |         automatedArmchair(void); | ||||||
|  |         //function to generate motor commands
 | ||||||
|  |         //can be also seen as handle function 
 | ||||||
|  |         //TODO: maybe create separate task for handling at mode switch and communicate with queue?
 | ||||||
|  |         motorCommands_t generateCommands(); //repeatedly called by control task
 | ||||||
|  | 
 | ||||||
|  |         //function that adds a basic command to the queue
 | ||||||
|  |         void addCommand(commandSimple_t command); | ||||||
|  | 
 | ||||||
|  |         //function that deletes all pending/queued commands
 | ||||||
|  |         motorCommands_t clearCommands(); | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  |     private: | ||||||
|  |         //--- methods ---
 | ||||||
|  |         //--- objects ---
 | ||||||
|  |         //TODO: add buzzer here
 | ||||||
|  |         //--- variables ---
 | ||||||
|  |         //queue for storing pending commands
 | ||||||
|  |         QueueHandle_t commandQueue = NULL; | ||||||
|  |         //current running command
 | ||||||
|  |         commandSimple_t cmdCurrent; | ||||||
|  |         //timestamp current command is finished
 | ||||||
|  |         uint32_t timestampCmdFinished = 0; | ||||||
|  | 
 | ||||||
|  |         motorCommands_t motorCommands; | ||||||
|  | 
 | ||||||
|  |         //command preset for idling motors
 | ||||||
|  |         const motorCommand_t cmd_motorIdle = { | ||||||
|  |             .state = motorstate_t::IDLE, | ||||||
|  |             .duty = 0 | ||||||
|  |         }; | ||||||
|  |         const motorCommands_t cmds_bothMotorsIdle = { | ||||||
|  |             .left = cmd_motorIdle, | ||||||
|  |             .right = cmd_motorIdle | ||||||
|  |         }; | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| @ -126,5 +126,7 @@ httpJoystick httpJoystickMain(configHttpJoystickMain); | |||||||
| //create global control object
 | //create global control object
 | ||||||
| controlledArmchair control(configControl, &buzzer, &motorLeft, &motorRight, &joystick, &httpJoystickMain); | controlledArmchair control(configControl, &buzzer, &motorLeft, &motorRight, &joystick, &httpJoystickMain); | ||||||
| 
 | 
 | ||||||
|  | //create global automatedArmchair object (for auto-mode)
 | ||||||
|  | automatedArmchair armchair; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -9,6 +9,7 @@ | |||||||
| #include "control.hpp" | #include "control.hpp" | ||||||
| #include "fan.hpp" | #include "fan.hpp" | ||||||
| #include "http.hpp" | #include "http.hpp" | ||||||
|  | #include "auto.hpp" | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| //create global controlledMotor instances for both motors
 | //create global controlledMotor instances for both motors
 | ||||||
| @ -27,6 +28,9 @@ extern buzzer_t buzzer; | |||||||
| //create global control object
 | //create global control object
 | ||||||
| extern controlledArmchair control; | extern controlledArmchair control; | ||||||
| 
 | 
 | ||||||
|  | //create global automatedArmchair object (for auto-mode)
 | ||||||
|  | extern automatedArmchair armchair; | ||||||
|  | 
 | ||||||
| //create global httpJoystick object
 | //create global httpJoystick object
 | ||||||
| extern httpJoystick httpJoystickMain; | extern httpJoystick httpJoystickMain; | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -121,6 +121,14 @@ void controlledArmchair::startHandleLoop() { | |||||||
|                 motorLeft->setTarget(commands.left.state, commands.left.duty);  |                 motorLeft->setTarget(commands.left.state, commands.left.duty);  | ||||||
|                break; |                break; | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
|  |             case controlMode_t::AUTO: | ||||||
|  |                 vTaskDelay(20 / portTICK_PERIOD_MS); | ||||||
|  |                //generate commands
 | ||||||
|  |                commands = armchair.generateCommands(); | ||||||
|  |                break; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|               //  //TODO: add other modes here
 |               //  //TODO: add other modes here
 | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -4,6 +4,7 @@ | |||||||
| #include "motorctl.hpp" | #include "motorctl.hpp" | ||||||
| #include "buzzer.hpp" | #include "buzzer.hpp" | ||||||
| #include "http.hpp" | #include "http.hpp" | ||||||
|  | #include "auto.hpp" | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| //--------------------------------------------
 | //--------------------------------------------
 | ||||||
|  | |||||||
| @ -152,6 +152,7 @@ extern "C" void app_main(void) { | |||||||
|     esp_log_level_set("fan-control", ESP_LOG_INFO); |     esp_log_level_set("fan-control", ESP_LOG_INFO); | ||||||
|     esp_log_level_set("wifi", ESP_LOG_INFO); |     esp_log_level_set("wifi", ESP_LOG_INFO); | ||||||
|     esp_log_level_set("http", ESP_LOG_INFO); |     esp_log_level_set("http", ESP_LOG_INFO); | ||||||
|  |     esp_log_level_set("automatedArmchair", ESP_LOG_DEBUG); | ||||||
| 
 | 
 | ||||||
|      |      | ||||||
|     //----------------------------------------------
 |     //----------------------------------------------
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user