Add menu item Set Max Duty
Add nvs to control.cpp
This commit is contained in:
		
							parent
							
								
									0672d08cb8
								
							
						
					
					
						commit
						cef2a841c8
					
				| @ -220,4 +220,15 @@ rotary_encoder_t encoder_config = { | ||||
| 	.index = 0, | ||||
| 	.btn_pressed_time_us = 20000, | ||||
| 	.btn_state = RE_BTN_RELEASED //default state
 | ||||
| }; | ||||
| 
 | ||||
| 
 | ||||
| //-----------------------------------
 | ||||
| //--- joystick command generation ---
 | ||||
| //-----------------------------------
 | ||||
| //configure parameters for motor command generation from joystick data
 | ||||
| joystickGenerateCommands_config_t joystickGenerateCommands_config{ | ||||
|     .maxDuty = 100, | ||||
|     .dutyOffset = 5, // duty at which motors start immediately
 | ||||
|     .altStickMapping = false, | ||||
| }; | ||||
| @ -32,14 +32,17 @@ controlledArmchair::controlledArmchair( | ||||
|     controlledMotor *motorLeft_f, | ||||
|     controlledMotor *motorRight_f, | ||||
|     evaluatedJoystick *joystick_f, | ||||
|     joystickGenerateCommands_config_t *joystickGenerateCommands_config_f, | ||||
|     httpJoystick *httpJoystick_f, | ||||
|     automatedArmchair_c *automatedArmchair_f, | ||||
|     cControlledRest *legRest_f, | ||||
|     cControlledRest *backRest_f) | ||||
|     cControlledRest *backRest_f, | ||||
|     nvs_handle_t * nvsHandle_f) | ||||
| { | ||||
| 
 | ||||
|     //copy configuration
 | ||||
|     config = config_f; | ||||
|     joystickGenerateCommands_config = *joystickGenerateCommands_config_f; | ||||
|     //copy object pointers
 | ||||
|     buzzer = buzzer_f; | ||||
|     motorLeft = motorLeft_f; | ||||
| @ -49,9 +52,13 @@ controlledArmchair::controlledArmchair( | ||||
|     automatedArmchair = automatedArmchair_f; | ||||
|     legRest = legRest_f; | ||||
|     backRest = backRest_f; | ||||
|     nvsHandle = nvsHandle_f; | ||||
|     //set default mode from config
 | ||||
|     modePrevious = config.defaultMode; | ||||
|      | ||||
|     // override default config value if maxDuty is found in nvs
 | ||||
|     loadMaxDuty(); | ||||
| 
 | ||||
|     //TODO declare / configure controlled motors here instead of config (unnecessary that button object is globally available - only used here)?
 | ||||
| } | ||||
| 
 | ||||
| @ -104,7 +111,7 @@ void controlledArmchair::startHandleLoop() { | ||||
|                 //additionaly scale coordinates (more detail in slower area)
 | ||||
|                 joystick_scaleCoordinatesLinear(&stickData, 0.6, 0.35); //TODO: add scaling parameters to config
 | ||||
|                 //generate motor commands
 | ||||
|                 commands = joystick_generateCommandsDriving(stickData, altStickMapping); | ||||
|                 commands = joystick_generateCommandsDriving(stickData, &joystickGenerateCommands_config); | ||||
|                 //apply motor commands
 | ||||
|                 motorRight->setTarget(commands.right.state, commands.right.duty);  | ||||
|                 motorLeft->setTarget(commands.left.state, commands.left.duty);  | ||||
| @ -138,7 +145,7 @@ void controlledArmchair::startHandleLoop() { | ||||
|                 ESP_LOGD(TAG, "generating commands from x=%.3f  y=%.3f  radius=%.3f  angle=%.3f", stickData.x, stickData.y, stickData.radius, stickData.angle); | ||||
|                 //--- generate motor commands ---
 | ||||
|                 //Note: timeout (no data received) is handled in getData method
 | ||||
|                 commands = joystick_generateCommandsDriving(stickData, altStickMapping); | ||||
|                 commands = joystick_generateCommandsDriving(stickData, &joystickGenerateCommands_config); | ||||
| 
 | ||||
|                 //--- apply commands to motors ---
 | ||||
|                 //TODO make motorctl.setTarget also accept motorcommand struct directly
 | ||||
| @ -268,8 +275,8 @@ bool controlledArmchair::toggleFreezeInputMassage() | ||||
| // toggle between normal and alternative stick mapping (joystick reverse position inverted)
 | ||||
| bool controlledArmchair::toggleAltStickMapping() | ||||
| { | ||||
|     altStickMapping = !altStickMapping; | ||||
|     if (altStickMapping) | ||||
|     joystickGenerateCommands_config.altStickMapping = !joystickGenerateCommands_config.altStickMapping; | ||||
|     if (joystickGenerateCommands_config.altStickMapping) | ||||
|     { | ||||
|         buzzer->beep(6, 70, 50); | ||||
|         ESP_LOGW(TAG, "changed to alternative stick mapping"); | ||||
| @ -279,7 +286,7 @@ bool controlledArmchair::toggleAltStickMapping() | ||||
|         buzzer->beep(1, 500, 100); | ||||
|         ESP_LOGW(TAG, "changed to default stick mapping"); | ||||
|     } | ||||
|     return altStickMapping; | ||||
|     return joystickGenerateCommands_config.altStickMapping; | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| @ -503,3 +510,56 @@ void controlledArmchair::toggleMode(controlMode_t modePrimary){ | ||||
|         changeMode(modePrimary); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| //-------------------------------
 | ||||
| //------ loadDecelDuration ------
 | ||||
| //-------------------------------
 | ||||
| // update local config value when maxDuty is stored in nvs
 | ||||
| void controlledArmchair::loadMaxDuty(void) | ||||
| { | ||||
|     // default value is already loaded (constructor)
 | ||||
|     // read from nvs
 | ||||
|     uint16_t valueRead; | ||||
|     esp_err_t err = nvs_get_u16(*nvsHandle, "c-maxDuty", &valueRead); | ||||
|     switch (err) | ||||
|     { | ||||
|     case ESP_OK: | ||||
|         ESP_LOGW(TAG, "Successfully read value '%s' from nvs. Overriding default value %.2f with %.2f", "c-maxDuty", joystickGenerateCommands_config.maxDuty, valueRead/100.0); | ||||
|         joystickGenerateCommands_config.maxDuty = (float)(valueRead/100.0); | ||||
|         break; | ||||
|     case ESP_ERR_NVS_NOT_FOUND: | ||||
|         ESP_LOGW(TAG, "nvs: the value '%s' is not initialized yet, keeping default value %.2f", "c-maxDuty", joystickGenerateCommands_config.maxDuty); | ||||
|         break; | ||||
|     default: | ||||
|         ESP_LOGE(TAG, "Error (%s) reading nvs!", esp_err_to_name(err)); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| //-----------------------------------
 | ||||
| //---------- writeMaxDuty -----------
 | ||||
| //-----------------------------------
 | ||||
| // write provided value to nvs to be persistent and update local variable in joystickGenerateCommmands_config struct
 | ||||
| // note: duty percentage gets stored as uint with factor 100 (to get more precision)
 | ||||
| void controlledArmchair::writeMaxDuty(float newValue){ | ||||
|     // check if unchanged
 | ||||
|     if(joystickGenerateCommands_config.maxDuty == newValue){ | ||||
|         ESP_LOGW(TAG, "value unchanged at %.2f, not writing to nvs", newValue); | ||||
|         return; | ||||
|     } | ||||
|     // update nvs value
 | ||||
|     ESP_LOGW(TAG, "updating nvs value '%s' from %.2f to %.2f", "c-maxDuty", joystickGenerateCommands_config.maxDuty, newValue) ; | ||||
|     esp_err_t err = nvs_set_u16(*nvsHandle, "c-maxDuty", (uint16_t)(newValue*100)); | ||||
|     if (err != ESP_OK) | ||||
|         ESP_LOGE(TAG, "nvs: failed writing"); | ||||
|     err = nvs_commit(*nvsHandle); | ||||
|     if (err != ESP_OK) | ||||
|         ESP_LOGE(TAG, "nvs: failed committing updates"); | ||||
|     else | ||||
|         ESP_LOGI(TAG, "nvs: successfully committed updates"); | ||||
|     // update variable
 | ||||
|     joystickGenerateCommands_config.maxDuty = newValue; | ||||
| } | ||||
| @ -1,5 +1,10 @@ | ||||
| #pragma once | ||||
| 
 | ||||
| extern "C" | ||||
| { | ||||
| #include "nvs_flash.h" | ||||
| #include "nvs.h" | ||||
| } | ||||
| #include "motordrivers.hpp" | ||||
| #include "motorctl.hpp" | ||||
| #include "buzzer.hpp" | ||||
| @ -50,10 +55,12 @@ class controlledArmchair { | ||||
|                 controlledMotor* motorLeft_f, | ||||
|                 controlledMotor* motorRight_f, | ||||
|                 evaluatedJoystick* joystick_f, | ||||
|                 joystickGenerateCommands_config_t* joystickGenerateCommands_config_f, | ||||
|                 httpJoystick* httpJoystick_f, | ||||
|                 automatedArmchair_c* automatedArmchair, | ||||
|                 cControlledRest * legRest, | ||||
|                 cControlledRest * backRest | ||||
|                 cControlledRest * backRest, | ||||
|                 nvs_handle_t * nvsHandle_f | ||||
|                 ); | ||||
| 
 | ||||
|         //--- functions ---
 | ||||
| @ -79,27 +86,37 @@ class controlledArmchair { | ||||
|         controlMode_t getCurrentMode() const {return mode;}; | ||||
|         const char *getCurrentModeStr() const { return controlModeStr[(int)mode]; }; | ||||
| 
 | ||||
|         //--- mode specific ---
 | ||||
|         // releases or locks joystick in place when in massage mode, returns true when input is frozen
 | ||||
|         bool toggleFreezeInputMassage(); | ||||
| 
 | ||||
|         // toggle between normal and alternative stick mapping (joystick reverse position inverted), returns true when alt mapping is active
 | ||||
|         bool toggleAltStickMapping(); | ||||
| 
 | ||||
|         // configure max dutycycle (in joystick or http mode)
 | ||||
|         void setMaxDuty(float maxDutyNew) { writeMaxDuty(maxDutyNew); }; | ||||
|         float getMaxDuty() const {return joystickGenerateCommands_config.maxDuty; }; | ||||
| 
 | ||||
|     private: | ||||
| 
 | ||||
|         //--- functions ---
 | ||||
|         //function that evaluates whether there is no activity/change on the motor duty for a certain time, if so a switch to IDLE is issued. - has to be run repeatedly in a slow interval
 | ||||
|         void handleTimeout(); | ||||
| 
 | ||||
|         void loadMaxDuty(); //load stored value for maxDuty from nvs
 | ||||
|         void writeMaxDuty(float newMaxDuty); //write new value for maxDuty to nvs
 | ||||
| 
 | ||||
|         //--- objects ---
 | ||||
|         buzzer_t* buzzer; | ||||
|         controlledMotor* motorLeft; | ||||
|         controlledMotor* motorRight; | ||||
|         httpJoystick* httpJoystickMain_l; | ||||
|         evaluatedJoystick* joystick_l; | ||||
|         joystickGenerateCommands_config_t joystickGenerateCommands_config; | ||||
|         automatedArmchair_c *automatedArmchair; | ||||
|         cControlledRest * legRest; | ||||
|         cControlledRest * backRest; | ||||
|         //handle for using the nvs flash (persistent config variables)
 | ||||
|         nvs_handle_t * nvsHandle; | ||||
| 
 | ||||
|         //---variables ---
 | ||||
|         //struct for motor commands returned by generate functions of each mode
 | ||||
| @ -109,7 +126,6 @@ class controlledArmchair { | ||||
| 
 | ||||
|         //store joystick data
 | ||||
|         joystickData_t stickData; | ||||
|         bool altStickMapping; //alternative joystick mapping (reverse mapped differently)
 | ||||
| 
 | ||||
|         //variables for http mode
 | ||||
|         uint32_t http_timestamp_lastData = 0; | ||||
|  | ||||
| @ -169,7 +169,7 @@ void createObjects() | ||||
| 
 | ||||
|     // create control object (control.hpp)
 | ||||
|     // with configuration from config.cpp
 | ||||
|     control = new controlledArmchair(configControl, buzzer, motorLeft, motorRight, joystick, httpJoystickMain, automatedArmchair, legRest, backRest); | ||||
|     control = new controlledArmchair(configControl, buzzer, motorLeft, motorRight, joystick, &joystickGenerateCommands_config, httpJoystickMain, automatedArmchair, legRest, backRest, &nvsHandle); | ||||
| 
 | ||||
|     // create automatedArmchair_c object (for auto-mode) (auto.hpp)
 | ||||
|     automatedArmchair = new automatedArmchair_c(motorLeft, motorRight); | ||||
|  | ||||
| @ -255,27 +255,25 @@ menuItem_t item_debugJoystick = { | ||||
| //########################
 | ||||
| void maxDuty_action(display_task_parameters_t * objects, SSD1306_t * display, int value) | ||||
| { | ||||
|     //TODO actually store the value
 | ||||
|     ESP_LOGW(TAG, "set max duty to %d", value); | ||||
|     objects->control->setMaxDuty(value); | ||||
| } | ||||
| int maxDuty_currentValue(display_task_parameters_t * objects) | ||||
| { | ||||
|     //TODO get real current value
 | ||||
|     return 84; | ||||
|     return (int)objects->control->getMaxDuty(); | ||||
| } | ||||
| menuItem_t item_maxDuty = { | ||||
|     maxDuty_action,       // function action
 | ||||
|     maxDuty_currentValue, // function get initial value or NULL(show in line 2)
 | ||||
|     NULL,                 // function get default value or NULL(dont set value, show msg)
 | ||||
|     1,                    // valueMin
 | ||||
|     99,                   // valueMax
 | ||||
|     100,                  // valueMax
 | ||||
|     1,                    // valueIncrement
 | ||||
|     "max duty        ",   // title
 | ||||
|     "Set max Duty    ",   // title
 | ||||
|     "",                   // line1 (above value)
 | ||||
|     "  set max-duty: ",   // line2 (above value)
 | ||||
|     "",                   // line4 * (below value)
 | ||||
|     "",                   // line5 *
 | ||||
|     "      1-99      ",   // line6
 | ||||
|     "      1-100     ",   // line6
 | ||||
|     "     percent    ",   // line7
 | ||||
| }; | ||||
| 
 | ||||
| @ -433,8 +431,8 @@ menuItem_t item_last = { | ||||
| //####################################################
 | ||||
| //### store all configured menu items in one array ###
 | ||||
| //####################################################
 | ||||
| const menuItem_t menuItems[] = {item_centerJoystick, item_calibrateJoystick, item_debugJoystick, item_accelLimit, item_decelLimit, item_reset, item_example, item_last}; | ||||
| const int itemCount = 8; | ||||
| const menuItem_t menuItems[] = {item_centerJoystick, item_calibrateJoystick, item_debugJoystick, item_maxDuty, item_accelLimit, item_decelLimit, item_reset, item_example, item_last}; | ||||
| const int itemCount = 9; | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|  | ||||
| @ -304,7 +304,7 @@ joystickPos_t joystick_evaluatePosition(float x, float y){ | ||||
| //========= joystick_CommandsDriving =========
 | ||||
| //============================================
 | ||||
| //function that generates commands for both motors from the joystick data
 | ||||
| motorCommands_t joystick_generateCommandsDriving(joystickData_t data, bool altStickMapping){ | ||||
| motorCommands_t joystick_generateCommandsDriving(joystickData_t data, joystickGenerateCommands_config_t * config){ | ||||
| 
 | ||||
|     //struct with current data of the joystick
 | ||||
|     //typedef struct joystickData_t {
 | ||||
| @ -317,10 +317,8 @@ motorCommands_t joystick_generateCommandsDriving(joystickData_t data, bool altSt | ||||
| 
 | ||||
| 	//--- variables ---
 | ||||
|     motorCommands_t commands; | ||||
|     float dutyMax = 100; //TODO add this to config, make changeable during runtime
 | ||||
| 
 | ||||
|     float dutyOffset = 5; //immediately starts with this duty, TODO add this to config
 | ||||
|     float dutyRange = dutyMax - dutyOffset; | ||||
|     float dutyRange = config->maxDuty - config->dutyOffset; | ||||
|     float ratio = fabs(data.angle) / 90; //90degree = x=0 || 0degree = y=0
 | ||||
| 	 | ||||
| 	//--- snap ratio to max at angle threshold --- 
 | ||||
| @ -334,7 +332,7 @@ motorCommands_t joystick_generateCommandsDriving(joystickData_t data, bool altSt | ||||
| 	*/ | ||||
| 
 | ||||
|     //--- experimental alternative control mode ---
 | ||||
|     if (altStickMapping == true){ | ||||
|     if (config->altStickMapping == true){ | ||||
|         //swap BOTTOM_LEFT and BOTTOM_RIGHT
 | ||||
|         if (data.position == joystickPos_t::BOTTOM_LEFT){ | ||||
|             data.position = joystickPos_t::BOTTOM_RIGHT; | ||||
|  | ||||
| @ -10,6 +10,7 @@ extern "C" | ||||
| #include "esp_err.h" | ||||
| #include "nvs_flash.h" | ||||
| #include "nvs.h" | ||||
| #include <stdbool.h> | ||||
| } | ||||
| 
 | ||||
| #include <cmath> | ||||
| @ -69,6 +70,13 @@ typedef struct joystickData_t { | ||||
| } joystickData_t; | ||||
| 
 | ||||
| 
 | ||||
| // struct with parameters provided to joystick_GenerateCommandsDriving()
 | ||||
| typedef struct joystickGenerateCommands_config_t { | ||||
|     float maxDuty; | ||||
|     float dutyOffset; | ||||
|     bool altStickMapping; | ||||
| } joystickGenerateCommands_config_t; | ||||
| 
 | ||||
| 
 | ||||
| //------------------------------------
 | ||||
| //----- evaluatedJoystick class  -----
 | ||||
| @ -120,7 +128,7 @@ private: | ||||
| //============================================
 | ||||
| //function that generates commands for both motors from the joystick data
 | ||||
| //motorCommands_t joystick_generateCommandsDriving(evaluatedJoystick joystick);
 | ||||
| motorCommands_t joystick_generateCommandsDriving(joystickData_t data, bool altStickMapping = false); | ||||
| motorCommands_t joystick_generateCommandsDriving(joystickData_t data, joystickGenerateCommands_config_t * config); | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user