Add exponential scaling of stick coordinates
- joystick.hpp/cpp - add function joystick_scaleCoordinatesExp(joystickData_t * data, float exponent) which updates joystick data with its scaled coordinates and re-calculated radius - control.cpp - scale coordinates exponential (pow 2) in JOYSTICK mode - scale coordinates exponential (pow 2) in HTTP mode - config.cpp - fixed swapped x/y zero tolerances for hardware joystick Note: tested armchair with scaling exponents 1.5; 2; 3 and noticed that 2 works best
This commit is contained in:
parent
9db5736f77
commit
1c6586c29e
@ -72,8 +72,8 @@ joystick_config_t configJoystick = {
|
||||
.adc_x = ADC1_CHANNEL_3, //GPIO39
|
||||
.adc_y = ADC1_CHANNEL_0, //GPIO36
|
||||
//percentage of joystick range the coordinate of the axis snaps to 0 (0-100)
|
||||
.tolerance_zeroX_per = 7,
|
||||
.tolerance_zeroY_per = 3,
|
||||
.tolerance_zeroX_per = 3,
|
||||
.tolerance_zeroY_per = 7,
|
||||
//percentage of joystick range the coordinate snaps to -1 or 1 before configured "_max" or "_min" threshold (mechanical end) is reached (0-100)
|
||||
.tolerance_end_per = 5,
|
||||
//threshold the radius jumps to 1 before the stick is at max radius (range 0-1)
|
||||
|
@ -68,9 +68,12 @@ void controlledArmchair::startHandleLoop() {
|
||||
break;
|
||||
|
||||
case controlMode_t::JOYSTICK:
|
||||
//get current joystick data with getData method of evaluatedJoystick
|
||||
stickData = joystick.getData();
|
||||
//additionaly scale coordinates exponentionally (more detail in slower area)
|
||||
joystick_scaleCoordinatesExp(&stickData, 2); //TODO: add scaling exponent to config
|
||||
//generate motor commands
|
||||
//pass joystick data from getData method of evaluatedJoystick to generateCommandsDriving function
|
||||
commands = joystick_generateCommandsDriving(joystick.getData());
|
||||
commands = joystick_generateCommandsDriving(stickData);
|
||||
//TODO: pass pointer to joystick object to control class instead of accessing it directly globally
|
||||
motorRight->setTarget(commands.right.state, commands.right.duty);
|
||||
motorLeft->setTarget(commands.left.state, commands.left.duty);
|
||||
@ -89,17 +92,16 @@ void controlledArmchair::startHandleLoop() {
|
||||
break;
|
||||
|
||||
case controlMode_t::HTTP:
|
||||
//create emptry struct for receiving data from http function
|
||||
joystickData_t dataRead = { };
|
||||
|
||||
//--- get joystick data from queue ---
|
||||
//Note this function waits several seconds (httpconfig.timeoutMs) for data to arrive, otherwise Center data or NULL is returned
|
||||
//TODO: as described above, when changing modes it might delay a few seconds for the change to apply
|
||||
dataRead = httpJoystickMain_l->getData();
|
||||
stickData = httpJoystickMain_l->getData();
|
||||
//scale coordinates additionally (more detail in slower area)
|
||||
joystick_scaleCoordinatesExp(&stickData, 2); //TODO: add scaling exponent to config
|
||||
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 ---
|
||||
ESP_LOGD(TAG, "generating commands from x=%.3f y=%.3f radius=%.3f angle=%.3f", dataRead.x, dataRead.y, dataRead.radius, dataRead.angle);
|
||||
//Note: timeout (no data received) is handled in getData method
|
||||
commands = joystick_generateCommandsDriving(dataRead);
|
||||
commands = joystick_generateCommandsDriving(stickData);
|
||||
|
||||
//--- apply commands to motors ---
|
||||
//TODO make motorctl.setTarget also accept motorcommand struct directly
|
||||
|
@ -75,6 +75,9 @@ class controlledArmchair {
|
||||
//struct with config parameters
|
||||
control_config_t config;
|
||||
|
||||
//store joystick data
|
||||
joystickData_t stickData;
|
||||
|
||||
//variables for http mode
|
||||
uint32_t http_timestamp_lastData = 0;
|
||||
|
||||
|
@ -163,6 +163,35 @@ float scaleCoordinate(float input, float min, float max, float center, float tol
|
||||
|
||||
|
||||
|
||||
//===========================================
|
||||
//====== joystick_scaleCoordinatesExp =======
|
||||
//===========================================
|
||||
//local function that scales the absolute value of a variable exponentionally
|
||||
float scaleExp(float value, float exponent){
|
||||
float result = powf(fabs(value), exponent);
|
||||
if (value >= 0) {
|
||||
return result;
|
||||
} else {
|
||||
return -result;
|
||||
}
|
||||
}
|
||||
//function that updates a joystickData object with exponentionally scaling applied to coordinates
|
||||
void joystick_scaleCoordinatesExp(joystickData_t * data, float exponent){
|
||||
//scale x and y coordinate
|
||||
data->x = scaleExp(data->x, exponent);
|
||||
data->y = scaleExp(data->y, exponent);
|
||||
//re-calculate radius
|
||||
data->radius = sqrt(pow(data->x,2) + pow(data->y,2));
|
||||
if (data->radius > 1-0.07) {//FIXME hardcoded radius tolerance
|
||||
data->radius = 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//=============================================
|
||||
//========= joystick_evaluatePosition =========
|
||||
//=============================================
|
||||
@ -227,7 +256,7 @@ motorCommands_t joystick_generateCommandsDriving(joystickData_t data){
|
||||
motorCommands_t commands;
|
||||
float dutyMax = 94; //TODO add this to config, make changeable during runtime
|
||||
|
||||
float dutyOffset = 5; //immedeately starts with this duty, TODO add this to config
|
||||
float dutyOffset = 10; //immedeately starts with this duty, TODO add this to config
|
||||
float dutyRange = dutyMax - dutyOffset;
|
||||
float ratio = fabs(data.angle) / 90; //90degree = x=0 || 0degree = y=0
|
||||
|
||||
|
@ -127,6 +127,15 @@ motorCommands_t joystick_generateCommandsShaking(joystickData_t data );
|
||||
float scaleCoordinate(float input, float min, float max, float center, float tolerance_zero_per, float tolerance_end_per);
|
||||
|
||||
|
||||
|
||||
//===========================================
|
||||
//====== joystick_scaleCoordinatesExp =======
|
||||
//===========================================
|
||||
//function that updates a joystickData object with exponentionally scaling applied to coordinates
|
||||
void joystick_scaleCoordinatesExp(joystickData_t * data, float exponent);
|
||||
|
||||
|
||||
|
||||
//=============================================
|
||||
//========= joystick_evaluatePosition =========
|
||||
//=============================================
|
||||
|
Loading…
x
Reference in New Issue
Block a user