Move joystick pos-definition to separate function

Move section that defines joystick position enum to a separate function
(outside of joystick class), this makes it usable for other inputs as
well
- create new function joystick_evaluatePosition
- call the new function in joystickgetData (where code was initially)
- change function joystick_generateCommandsDriving to accept joystick
  data struct instead of joystick object as parameter -> makes the
  function usable with other input than hardware joystick too
This commit is contained in:
jonny_ji7 2022-06-17 10:43:00 +02:00
parent 57de7e3e59
commit cc5226647b
3 changed files with 58 additions and 34 deletions

View File

@ -53,7 +53,10 @@ void controlledArmchair::startHandleLoop() {
break;
case controlMode_t::JOYSTICK:
commands = joystick_generateCommandsDriving(joystick);
//generate motor commands
//pass joystick data from getData method of evaluatedJoystick to generateCommandsDriving function
commands = joystick_generateCommandsDriving(joystick.getData());
//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);
//TODO make motorctl.setTarget also accept motorcommand struct directly

View File

@ -135,37 +135,8 @@ joystickData_t evaluatedJoystick::getData() {
//calculate angle
data.angle = (atan(data.y/data.x) * 180) / 3.141;
//define position
//--- center ---
if((fabs(x) == 0) && (fabs(y) == 0)){
data.position = joystickPos_t::CENTER;
}
//--- x axis ---
else if(fabs(y) == 0){
data.position = joystickPos_t::X_AXIS;
}
//--- y axis ---
else if(fabs(x) == 0){
data.position = joystickPos_t::Y_AXIS;
}
//--- top right ---
else if(x > 0 && y > 0){
data.position = joystickPos_t::TOP_RIGHT;
}
//--- top left ---
else if(x < 0 && y > 0){
data.position = joystickPos_t::TOP_LEFT;
}
//--- bottom left ---
else if(x < 0 && y < 0){
data.position = joystickPos_t::BOTTOM_LEFT;
}
//--- bottom right ---
else if(x > 0 && y < 0){
data.position = joystickPos_t::BOTTOM_RIGHT;
}
data.position = joystick_evaluatePosition(x, y);
return data;
}
@ -188,13 +159,55 @@ void evaluatedJoystick::defineCenter(){
//=============================================
//========= joystick_evaluatePosition =========
//=============================================
//function that defines and returns enum joystickPos from x and y coordinates
joystickPos_t joystick_evaluatePosition(float x, float y){
//define position
//--- center ---
if((fabs(x) == 0) && (fabs(y) == 0)){
return joystickPos_t::CENTER;
}
//--- x axis ---
else if(fabs(y) == 0){
return joystickPos_t::X_AXIS;
}
//--- y axis ---
else if(fabs(x) == 0){
return joystickPos_t::Y_AXIS;
}
//--- top right ---
else if(x > 0 && y > 0){
return joystickPos_t::TOP_RIGHT;
}
//--- top left ---
else if(x < 0 && y > 0){
return joystickPos_t::TOP_LEFT;
}
//--- bottom left ---
else if(x < 0 && y < 0){
return joystickPos_t::BOTTOM_LEFT;
}
//--- bottom right ---
else if(x > 0 && y < 0){
return joystickPos_t::BOTTOM_RIGHT;
}
//--- other ---
else {
return joystickPos_t::CENTER;
}
}
//============================================
//========= joystick_CommandsDriving =========
//============================================
//function that generates commands for both motors from the joystick data
motorCommands_t joystick_generateCommandsDriving(evaluatedJoystick joystick){
motorCommands_t joystick_generateCommandsDriving(joystickData_t data){
//struct with current data of the joystick
@ -207,7 +220,6 @@ motorCommands_t joystick_generateCommandsDriving(evaluatedJoystick joystick){
//} joystickData_t;
joystickData_t data = joystick.getData();
motorCommands_t commands;
float dutyMax = 60; //TODO add this to config, make changeable during runtime

View File

@ -107,4 +107,13 @@ class evaluatedJoystick {
//========= joystick_CommandsDriving =========
//============================================
//function that generates commands for both motors from the joystick data
motorCommands_t joystick_generateCommandsDriving(evaluatedJoystick joystick);
//motorCommands_t joystick_generateCommandsDriving(evaluatedJoystick joystick);
motorCommands_t joystick_generateCommandsDriving(joystickData_t data );
//============================================
//========= joystick_CommandsDriving =========
//============================================
//function that defines and returns enum joystickPos from x and y coordinates
joystickPos_t joystick_evaluatePosition(float x, float y);