From 0b113eafb5dd84a10bd5056f319678d2d5bf1381 Mon Sep 17 00:00:00 2001 From: jonny_l480 Date: Sat, 23 Jul 2022 14:48:38 +0200 Subject: [PATCH] Add functions to modify/toggle fading (button x8) - add 3 functions to controlledMotor class in motorctl.cpp/hpp - setFade: set specific msFade duration for acceleration or deceleration - setFade: disable or set to default ramp for acceleration or deceleration - toggleFade: toggle between fading enabled or disables for acceleration or deceleration - button.cpp - add controlledMotor objects to constructor - add new command 8x press which now toggles fade-deceleration of both motors - config.cpp - slightly decrease fading durations --- main/button.cpp | 17 ++++++++- main/button.hpp | 7 +++- main/config.cpp | 4 +-- main/main.cpp | 2 +- main/motorctl.cpp | 88 ++++++++++++++++++++++++++++++++++++++++++++--- main/motorctl.hpp | 17 ++++++--- 6 files changed, 122 insertions(+), 13 deletions(-) diff --git a/main/button.cpp b/main/button.cpp index 0781953..927dac3 100644 --- a/main/button.cpp +++ b/main/button.cpp @@ -19,11 +19,13 @@ static const char * TAG = "button"; //----------------------------- //-------- constructor -------- //----------------------------- -buttonCommands::buttonCommands(gpio_evaluatedSwitch * button_f, controlledArmchair * control_f, buzzer_t * buzzer_f ){ +buttonCommands::buttonCommands(gpio_evaluatedSwitch * button_f, controlledArmchair * control_f, buzzer_t * buzzer_f, controlledMotor * motorLeft_f, controlledMotor * motorRight_f){ //copy object pointers button = button_f; control = control_f; buzzer = buzzer_f; + motorLeft = motorLeft_f; + motorRight = motorRight_f; //TODO declare / configure evaluatedSwitch here instead of config (unnecessary that button object is globally available - only used here)? } @@ -63,6 +65,19 @@ void buttonCommands::action (uint8_t count){ ESP_LOGW(TAG, "cmd %d: toggle between MASSAGE and JOYSTICK", count); control->toggleModes(controlMode_t::MASSAGE, controlMode_t::JOYSTICK); //toggle between MASSAGE and JOYSTICK mode break; + + case 8: + //toggle deceleration fading between on and off + bool decelEnabled = motorLeft->toggleFade(fadeType_t::DECEL); + motorRight->toggleFade(fadeType_t::DECEL); + ESP_LOGW(TAG, "cmd %d: toggle deceleration fading to: %d", count, (int)decelEnabled); + if (decelEnabled){ + buzzer->beep(3, 60, 50); + } else { + buzzer->beep(1, 1000, 1); + } + + } } diff --git a/main/button.hpp b/main/button.hpp index acaf520..b5d5900 100644 --- a/main/button.hpp +++ b/main/button.hpp @@ -3,6 +3,7 @@ #include "gpio_evaluateSwitch.hpp" #include "buzzer.hpp" #include "control.hpp" +#include "motorctl.hpp" @@ -16,7 +17,9 @@ class buttonCommands { buttonCommands ( gpio_evaluatedSwitch * button_f, controlledArmchair * control_f, - buzzer_t * buzzer_f + buzzer_t * buzzer_f, + controlledMotor * motorLeft_f, + controlledMotor * motorRight_f ); //--- functions --- @@ -32,6 +35,8 @@ class buttonCommands { gpio_evaluatedSwitch* button; controlledArmchair * control; buzzer_t* buzzer; + controlledMotor * motorLeft; + controlledMotor * motorRight; //--- variables --- uint8_t count = 0; diff --git a/main/config.cpp b/main/config.cpp index 1b515e1..784047e 100644 --- a/main/config.cpp +++ b/main/config.cpp @@ -29,8 +29,8 @@ single100a_config_t configDriverRight = { //--- configure motor contol --- motorctl_config_t configMotorControl = { - .msFadeAccel = 2000, //acceleration of the motor (ms it takes from 0% to 100%) - .msFadeDecel = 1000, //deceleration of the motor (ms it takes from 100% to 0%) + .msFadeAccel = 1300, //acceleration of the motor (ms it takes from 0% to 100%) + .msFadeDecel = 800, //deceleration of the motor (ms it takes from 100% to 0%) .currentMax = 10 }; diff --git a/main/main.cpp b/main/main.cpp index 8b701a6..f5bff17 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -75,7 +75,7 @@ void task_control( void * pvParameters ){ void task_button( void * pvParameters ){ ESP_LOGI(TAG, "Initializing command-button and starting handle loop"); //create button instance - buttonCommands commandButton(&buttonJoystick, &control, &buzzer); + buttonCommands commandButton(&buttonJoystick, &control, &buzzer, &motorLeft, &motorRight); //start handle loop commandButton.startHandleLoop(); } diff --git a/main/motorctl.cpp b/main/motorctl.cpp index c887c97..62ff8c6 100644 --- a/main/motorctl.cpp +++ b/main/motorctl.cpp @@ -11,6 +11,9 @@ static const char * TAG = "motor-control"; controlledMotor::controlledMotor(single100a_config_t config_driver, motorctl_config_t config_control): motor(config_driver) { //copy parameters for controlling the motor config = config_control; + //copy configured default fading durations to actually used variables + msFadeAccel = config.msFadeAccel; + msFadeDecel = config.msFadeDecel; init(); //TODO: add currentsensor object here @@ -89,11 +92,15 @@ void controlledMotor::handle(){ //--- calculate increment --- //calculate increment for fading UP with passed time since last run and configured fade time int64_t usPassed = esp_timer_get_time() - timestampLastRunUs; - dutyIncrementAccel = ( usPassed / ((float)config.msFadeAccel * 1000) ) * 100; //TODO define maximum increment - first run after startup (or long) pause can cause a very large increment + if (msFadeAccel > 0){ + dutyIncrementAccel = ( usPassed / ((float)msFadeAccel * 1000) ) * 100; //TODO define maximum increment - first run after startup (or long) pause can cause a very large increment + } else { + dutyIncrementAccel = 100; + } //calculate increment for fading DOWN with passed time since last run and configured fade time - if (config.msFadeDecel > 0){ - dutyIncrementDecel = ( usPassed / ((float)config.msFadeDecel * 1000) ) * 100; + if (msFadeDecel > 0){ + dutyIncrementDecel = ( usPassed / ((float)msFadeDecel * 1000) ) * 100; } else { dutyIncrementDecel = 100; } @@ -130,7 +137,7 @@ void controlledMotor::handle(){ if (dutyNow <= 0) { //reverse, accelerating fade(&dutyNow, dutyTarget, - dutyIncrementAccel); } - else if (&dutyNow > 0) { //forward, decelerating + else if (dutyNow > 0) { //forward, decelerating fade(&dutyNow, dutyTarget, - dutyIncrementDecel); } } @@ -222,3 +229,76 @@ motorCommand_t controlledMotor::getStatus(){ return status; }; + + +//=============================== +//=========== setFade =========== +//=============================== +//function for editing or enabling the fading/ramp of the motor control + +//set/update fading duration/amount +void controlledMotor::setFade(fadeType_t fadeType, uint32_t msFadeNew){ + //TODO: mutex for msFade variable also used in handle function + switch(fadeType){ + case fadeType_t::ACCEL: + msFadeAccel = msFadeNew; + break; + case fadeType_t::DECEL: + msFadeDecel = msFadeNew; + break; + } +} + + +//enable (set to default value) or disable fading +void controlledMotor::setFade(fadeType_t fadeType, bool enabled){ + uint32_t msFadeNew = 0; //define new fade time as default disabled + if(enabled){ //enable + //set to default/configured value + switch(fadeType){ + case fadeType_t::ACCEL: + msFadeNew = config.msFadeAccel; + break; + case fadeType_t::DECEL: + msFadeNew = config.msFadeDecel; + break; + } + } + //apply new Fade value + setFade(fadeType, msFadeNew); +} + + + +//================================== +//=========== toggleFade =========== +//================================== +//toggle fading between OFF and default value +bool controlledMotor::toggleFade(fadeType_t fadeType){ + uint32_t msFadeNew = 0; + bool enabled = false; + switch(fadeType){ + case fadeType_t::ACCEL: + if (msFadeAccel == 0){ + msFadeNew = config.msFadeAccel; + enabled = true; + } else { + msFadeNew = 0; + } + break; + case fadeType_t::DECEL: + if (msFadeDecel == 0){ + msFadeNew = config.msFadeAccel; + enabled = true; + } else { + msFadeNew = 0; + } + break; + } + //apply new Fade value + setFade(fadeType, msFadeNew); + + //return new state (fading enabled/disabled) + return enabled; +} + diff --git a/main/motorctl.hpp b/main/motorctl.hpp index 4528147..322b0f5 100644 --- a/main/motorctl.hpp +++ b/main/motorctl.hpp @@ -13,7 +13,7 @@ extern "C" //------------------------------------- -//-------- struct declarations ------- +//-------- struct/type declarations ------- //------------------------------------- //struct for sending command for one motor in the queue @@ -35,6 +35,10 @@ typedef struct motorctl_config_t { float currentMax; } motorctl_config_t; +//enum fade type (acceleration, deceleration) +//e.g. used for specifying which fading should be modified with setFade, togleFade functions +enum class fadeType_t {ACCEL, DECEL}; + class controlledMotor { @@ -45,6 +49,10 @@ class controlledMotor { void setTarget(motorstate_t state_f, float duty_f = 0); //adds target command to queue for handle function motorCommand_t getStatus(); //get current status of the motor (returns struct with state and duty) + void setFade(fadeType_t fadeType, bool enabled); //enable/disable acceleration or deceleration fading + void setFade(fadeType_t fadeType, uint32_t msFadeNew); //set acceleration or deceleration fade time + bool toggleFade(fadeType_t fadeType); //toggle acceleration or deceleration on/off + private: //--- functions --- @@ -60,7 +68,7 @@ class controlledMotor { //--- variables --- //struct for storing control specific parameters motorctl_config_t config; - + motorstate_t state = motorstate_t::IDLE; float currentMax; @@ -72,12 +80,13 @@ class controlledMotor { float dutyIncrementDecel; float dutyDelta; + uint32_t msFadeAccel; + uint32_t msFadeDecel; + uint32_t ramp; int64_t timestampLastRunUs; struct motorCommand_t commandReceive = {}; struct motorCommand_t commandSend = {}; - - };