From d573cc5b2419188e9cdbde772c449e1482b12a61 Mon Sep 17 00:00:00 2001 From: jonny_ji7 Date: Thu, 14 Jul 2022 18:50:36 +0200 Subject: [PATCH] Revert "Add hysteresis to joystick position X-Axis" While testing this feature briefly that approach did not appear to be a good idea. - noticed that the driving with joystick was a little unintuitive/unexpected (maybe just not used to it?) - BUG that the state does not reset to center when previous state was X-AXIS? Resulting in the armchair to slightly shake randomly at joystick center -> this has to be fixed if this approach is tested again. This reverts commit b2b12fe5de1e16ea2f25dfa4b0c3ac6845024c71. --- main/http.cpp | 2 +- main/http.hpp | 2 -- main/joystick.cpp | 46 ++++++++++------------------------------------ main/joystick.hpp | 5 +---- 4 files changed, 12 insertions(+), 43 deletions(-) diff --git a/main/http.cpp b/main/http.cpp index 0231c56..0c76184 100644 --- a/main/http.cpp +++ b/main/http.cpp @@ -159,7 +159,7 @@ esp_err_t httpJoystick::receiveHttpData(httpd_req_t *req){ //--- calculate angle --- data.angle = (atan(data.y/data.x) * 180) / 3.141; //--- evaluate position --- - data.position = joystick_evaluatePosition(data.x, data.y, &stickPosPrevious); + data.position = joystick_evaluatePosition(data.x, data.y); //log processed values ESP_LOGI(TAG, "processed values: x=%.3f y=%.3f radius=%.3f angle=%.3f pos=%s", diff --git a/main/http.hpp b/main/http.hpp index 55e8a85..eab53ef 100644 --- a/main/http.hpp +++ b/main/http.hpp @@ -68,6 +68,4 @@ class httpJoystick{ .radius = 0, .angle = 0 }; - //store last joystick position for position hysteresis - joystickPos_t stickPosPrevious = joystickPos_t::CENTER; }; diff --git a/main/joystick.cpp b/main/joystick.cpp index c4e15cc..fa4bd12 100644 --- a/main/joystick.cpp +++ b/main/joystick.cpp @@ -93,7 +93,7 @@ joystickData_t evaluatedJoystick::getData() { data.angle = (atan(data.y/data.x) * 180) / 3.141; //define position - data.position = joystick_evaluatePosition(x, y, &stickPosPrevious); + data.position = joystick_evaluatePosition(x, y); return data; } @@ -234,67 +234,41 @@ void joystick_scaleCoordinatesLinear(joystickData_t * data, float pointX, float //========= joystick_evaluatePosition ========= //============================================= //function that defines and returns enum joystickPos from x and y coordinates -joystickPos_t joystick_evaluatePosition(float x, float y, joystickPos_t* prevPos){ - joystickPos_t newPos; +joystickPos_t joystick_evaluatePosition(float x, float y){ //define position //--- center --- if((fabs(x) == 0) && (fabs(y) == 0)){ - newPos = joystickPos_t::CENTER; + return joystickPos_t::CENTER; } //--- x axis --- else if(fabs(y) == 0){ - newPos = joystickPos_t::X_AXIS; + return joystickPos_t::X_AXIS; } //--- y axis --- else if(fabs(x) == 0){ - newPos = joystickPos_t::Y_AXIS; + return joystickPos_t::Y_AXIS; } //--- top right --- else if(x > 0 && y > 0){ - newPos = joystickPos_t::TOP_RIGHT; + return joystickPos_t::TOP_RIGHT; } //--- top left --- else if(x < 0 && y > 0){ - newPos = joystickPos_t::TOP_LEFT; + return joystickPos_t::TOP_LEFT; } //--- bottom left --- else if(x < 0 && y < 0){ - newPos = joystickPos_t::BOTTOM_LEFT; + return joystickPos_t::BOTTOM_LEFT; } //--- bottom right --- else if(x > 0 && y < 0){ - newPos = joystickPos_t::BOTTOM_RIGHT; + return joystickPos_t::BOTTOM_RIGHT; } //--- other --- else { - newPos = joystickPos_t::CENTER; + return joystickPos_t::CENTER; } - - - //--- apply hysteresis on change from X-AXIS --- - //=> prevent frequent switching between X-AXIS and other positions - //otherwise this results in a very jerky motor action when in driving mode - float xAxisHyst = 0.2; - - //change in position - if (newPos != *prevPos) { - - //switched FROM x-axis to other position - if(*prevPos == joystickPos_t::X_AXIS) { //switch FROM X_AXIS - - //verify coordinate changed more than hysteresis - if (fabs(y) < xAxisHyst) { //less offset than hysteresis - newPos = joystickPos_t::X_AXIS; //stay at X_AXIS position - } else { //switch is valid (enough change) - *prevPos = newPos; - } - - } else { //switched to any other position - *prevPos = newPos; - } - } - return newPos; } diff --git a/main/joystick.hpp b/main/joystick.hpp index 4dd4f8a..c631003 100644 --- a/main/joystick.hpp +++ b/main/joystick.hpp @@ -96,8 +96,6 @@ class evaluatedJoystick { joystickData_t data; float x; float y; - //store last joystick position for position hysteresis - joystickPos_t stickPosPrevious = joystickPos_t::CENTER; }; @@ -156,5 +154,4 @@ void joystick_scaleCoordinatesLinear(joystickData_t * data, float pointX, float //========= joystick_evaluatePosition ========= //============================================= //function that defines and returns enum joystickPos from x and y coordinates -//joystickPos_t joystick_evaluatePosition(float x, float y); -joystickPos_t joystick_evaluatePosition(float x, float y, joystickPos_t* prevPos); +joystickPos_t joystick_evaluatePosition(float x, float y);