Add button cmd x12: alternative joystick mapping
New command and feature also made some general changes to control and button necessary joystick.cpp: - add optional parameter to joystick_CommandsDriving function bool altStickMapping (default false) if true this currently swaps BOTTOM_LEFT with BOTTOM_RIGHT for experimental different joystick mapping button.cpp: - fix/move variable declaration outside of switch - add 12x case: send count to control task control.cpp: - create new section in handle loop for button events - move x1 commands from JOYSTICK and MASSAGE case to new button section - remove unnecessary variable buttonEvent (only using buttonCount now) - add functionality to 12x button event -> toggle alternative stick mapping readme.md: - add new x12 button command
This commit is contained in:
parent
1c3576206a
commit
bbe26a3823
@ -110,6 +110,8 @@ A diagram which shows what components are connected to which terminals of the pc
|
||||
| 6x | toggle mode | **MASSAGE** <=> JOYSTICK | switch to MASSAGE mode or back to JOYSTICK mode |
|
||||
| 7x | | | |
|
||||
| 8x | toggle option | **deceleration limit** | disable/enable deceleration limit (default on) => more responsive |
|
||||
| | | | |
|
||||
| 12x | toggle option | **alt stick mapping** | toggle between default and alternative stick mapping (reverse swapped) |
|
||||
| >1s | system | **restart** | Restart the controller when pressing the button longer than 1 second |
|
||||
|
||||
|
||||
|
@ -36,6 +36,10 @@ buttonCommands::buttonCommands(gpio_evaluatedSwitch * button_f, controlledArmcha
|
||||
//----------------------------
|
||||
//function that runs commands depending on a count value
|
||||
void buttonCommands::action (uint8_t count){
|
||||
//--- variable declarations ---
|
||||
bool decelEnabled; //for different beeping when toggling
|
||||
|
||||
//--- actions based on count ---
|
||||
switch (count){
|
||||
//no such command
|
||||
default:
|
||||
@ -51,6 +55,7 @@ void buttonCommands::action (uint8_t count){
|
||||
|
||||
case 1:
|
||||
ESP_LOGW(TAG, "cmd %d: sending button event to control task", count);
|
||||
//-> define joystick center or toggle freeze input (executed in control task)
|
||||
control->sendButtonEvent(count); //TODO: always send button event to control task (not just at count=1) -> control.cpp has to be changed
|
||||
break;
|
||||
|
||||
@ -76,7 +81,7 @@ void buttonCommands::action (uint8_t count){
|
||||
|
||||
case 8:
|
||||
//toggle deceleration fading between on and off
|
||||
bool decelEnabled = motorLeft->toggleFade(fadeType_t::DECEL);
|
||||
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){
|
||||
@ -84,7 +89,13 @@ void buttonCommands::action (uint8_t count){
|
||||
} else {
|
||||
buzzer->beep(1, 1000, 1);
|
||||
}
|
||||
break;
|
||||
|
||||
case 12:
|
||||
ESP_LOGW(TAG, "cmd %d: sending button event to control task", count);
|
||||
//-> toggle altStickMapping (executed in control task)
|
||||
control->sendButtonEvent(count); //TODO: always send button event to control task (not just at count=1)?
|
||||
break;
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -78,17 +78,11 @@ 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);
|
||||
commands = joystick_generateCommandsDriving(stickData, altStickMapping);
|
||||
//apply motor commands
|
||||
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
|
||||
|
||||
//--- button event ---
|
||||
if (buttonEvent){
|
||||
joystick_l->defineCenter();
|
||||
buzzer->beep(2, 200, 100);
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
@ -99,7 +93,6 @@ void controlledArmchair::startHandleLoop() {
|
||||
if (!freezeInput){
|
||||
stickData = joystick_l->getData();
|
||||
}
|
||||
|
||||
//--- generate motor commands ---
|
||||
//pass joystick data from getData method of evaluatedJoystick to generateCommandsShaking function
|
||||
commands = joystick_generateCommandsShaking(stickData);
|
||||
@ -107,12 +100,6 @@ void controlledArmchair::startHandleLoop() {
|
||||
motorRight->setTarget(commands.right.state, commands.right.duty);
|
||||
motorLeft->setTarget(commands.left.state, commands.left.duty);
|
||||
|
||||
//--- button event ---
|
||||
if (buttonEvent){
|
||||
//toggle freeze of input (lock joystick at current values)
|
||||
freezeInput = !freezeInput;
|
||||
buzzer->beep(2, 200, 100);
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
@ -126,7 +113,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);
|
||||
commands = joystick_generateCommandsDriving(stickData, altStickMapping);
|
||||
|
||||
//--- apply commands to motors ---
|
||||
//TODO make motorctl.setTarget also accept motorcommand struct directly
|
||||
@ -134,16 +121,43 @@ void controlledArmchair::startHandleLoop() {
|
||||
motorLeft->setTarget(commands.left.state, commands.left.duty);
|
||||
break;
|
||||
|
||||
|
||||
// //TODO: add other modes here
|
||||
}
|
||||
|
||||
//--- reset button event --- (only one run of handle loop)
|
||||
//TODO: what if variable gets set durin above code? -> mutex around entire handle loop
|
||||
if (buttonEvent == true){
|
||||
ESP_LOGI(TAG, "resetting button event");
|
||||
buttonEvent = false;
|
||||
|
||||
//--- run actions based on received button button event ---
|
||||
//TODO: what if variable gets set from other task during this code? -> mutex around this code
|
||||
switch (buttonCount) {
|
||||
case 1: //define joystick center or freeze input
|
||||
if (mode == controlMode_t::JOYSTICK){
|
||||
//joystick mode: calibrate joystick
|
||||
joystick_l->defineCenter();
|
||||
} else if (mode == controlMode_t::MASSAGE){
|
||||
//massage mode: toggle freeze of input (lock joystick at current values)
|
||||
freezeInput = !freezeInput;
|
||||
if (freezeInput){
|
||||
buzzer->beep(5, 40, 25);
|
||||
} else {
|
||||
buzzer->beep(1, 300, 100);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 12: //toggle alternative joystick mapping (reverse swapped)
|
||||
altStickMapping = !altStickMapping;
|
||||
if (altStickMapping){
|
||||
buzzer->beep(6, 70, 50);
|
||||
} else {
|
||||
buzzer->beep(1, 500, 100);
|
||||
}
|
||||
break;
|
||||
}
|
||||
//--- reset button event --- (only one action per run)
|
||||
if (buttonCount > 0){
|
||||
ESP_LOGI(TAG, "resetting button event/count");
|
||||
buttonCount = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//-----------------------
|
||||
@ -179,7 +193,6 @@ void controlledArmchair::resetTimeout(){
|
||||
void controlledArmchair::sendButtonEvent(uint8_t count){
|
||||
//TODO mutex - if not replaced with queue
|
||||
ESP_LOGI(TAG, "setting button event");
|
||||
buttonEvent = true;
|
||||
buttonCount = count;
|
||||
}
|
||||
|
||||
|
@ -83,6 +83,7 @@ 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;
|
||||
@ -91,7 +92,6 @@ class controlledArmchair {
|
||||
bool freezeInput = false;
|
||||
|
||||
//variable to store button event
|
||||
bool buttonEvent = false;
|
||||
uint8_t buttonCount = 0;
|
||||
|
||||
//definition of mode enum
|
||||
|
@ -278,7 +278,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){
|
||||
motorCommands_t joystick_generateCommandsDriving(joystickData_t data, bool altStickMapping){
|
||||
|
||||
|
||||
//struct with current data of the joystick
|
||||
@ -292,12 +292,23 @@ motorCommands_t joystick_generateCommandsDriving(joystickData_t data){
|
||||
|
||||
|
||||
motorCommands_t commands;
|
||||
float dutyMax = 94; //TODO add this to config, make changeable during runtime
|
||||
float dutyMax = 95; //TODO add this to config, make changeable during runtime
|
||||
|
||||
float dutyOffset = 10; //immedeately starts with this duty, TODO add this to config
|
||||
float dutyOffset = 10; //immediately 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
|
||||
|
||||
//experimental alternative control mode
|
||||
if (altStickMapping == true){
|
||||
//swap BOTTOM_LEFT and BOTTOM_RIGHT
|
||||
if (data.position == joystickPos_t::BOTTOM_LEFT){
|
||||
data.position = joystickPos_t::BOTTOM_RIGHT;
|
||||
}
|
||||
else if (data.position == joystickPos_t::BOTTOM_RIGHT){
|
||||
data.position = joystickPos_t::BOTTOM_LEFT;
|
||||
}
|
||||
}
|
||||
|
||||
switch (data.position){
|
||||
|
||||
case joystickPos_t::CENTER:
|
||||
|
@ -107,7 +107,7 @@ class evaluatedJoystick {
|
||||
//============================================
|
||||
//function that generates commands for both motors from the joystick data
|
||||
//motorCommands_t joystick_generateCommandsDriving(evaluatedJoystick joystick);
|
||||
motorCommands_t joystick_generateCommandsDriving(joystickData_t data );
|
||||
motorCommands_t joystick_generateCommandsDriving(joystickData_t data, bool altStickMapping = false);
|
||||
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user