Rework/simplify chairAdjust - now using a class
- complete rework of chairAdjust.cpp/.hpp created a class cControlledRest which is more readable and saves a lot of duplicate code - joystick.cpp: move chair control via joystick to chairAdjust.cpp (prevents dependency loop) - button.cpp: fix bug instant idle when changing to ADJUST_CHAIR Note: briefly tested this state on a breakout board: Mode change and call of new class methods works.
This commit is contained in:
		
							parent
							
								
									384b732532
								
							
						
					
					
						commit
						a1bb808b62
					
				@ -77,8 +77,10 @@ void buttonCommands::action (uint8_t count, bool lastPressLong){
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            //toggle idle when 2x pressed
 | 
					            //toggle idle when 2x pressed
 | 
				
			||||||
 | 
					            else {
 | 
				
			||||||
            ESP_LOGW(TAG, "cmd %d: toggle IDLE", count);
 | 
					            ESP_LOGW(TAG, "cmd %d: toggle IDLE", count);
 | 
				
			||||||
            control->toggleIdle(); //toggle between idle and previous/default mode
 | 
					            control->toggleIdle(); //toggle between idle and previous/default mode
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
            break;
 | 
					            break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        case 3:
 | 
					        case 3:
 | 
				
			||||||
 | 
				
			|||||||
@ -195,5 +195,9 @@ controlledArmchair control(configControl, &buzzer, &motorLeft, &motorRight, &joy
 | 
				
			|||||||
//create global automatedArmchair object (for auto-mode) (auto.hpp)
 | 
					//create global automatedArmchair object (for auto-mode) (auto.hpp)
 | 
				
			||||||
automatedArmchair armchair;
 | 
					automatedArmchair armchair;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//create global objects for controlling the chair position
 | 
				
			||||||
 | 
					//                      gpio_up, gpio_down, name
 | 
				
			||||||
 | 
					cControlledRest legRest(GPIO_NUM_4, GPIO_NUM_16, "legRest");
 | 
				
			||||||
 | 
					cControlledRest backRest(GPIO_NUM_2, GPIO_NUM_15, "backRest");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -11,11 +11,12 @@
 | 
				
			|||||||
#include "http.hpp"
 | 
					#include "http.hpp"
 | 
				
			||||||
#include "auto.hpp"
 | 
					#include "auto.hpp"
 | 
				
			||||||
#include "speedsensor.hpp"
 | 
					#include "speedsensor.hpp"
 | 
				
			||||||
 | 
					#include "chairAdjust.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//in IDLE mode: set loglevel for evaluatedJoystick to DEBUG 
 | 
					//in IDLE mode: set loglevel for evaluatedJoystick to DEBUG 
 | 
				
			||||||
//and repeatedly read joystick e.g. for manually calibrating / testing joystick
 | 
					//and repeatedly read joystick e.g. for manually calibrating / testing joystick
 | 
				
			||||||
#define JOYSTICK_LOG_IN_IDLE
 | 
					//#define JOYSTICK_LOG_IN_IDLE
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//TODO outsource global variables to e.g. global.cpp and only config options here?
 | 
					//TODO outsource global variables to e.g. global.cpp and only config options here?
 | 
				
			||||||
@ -49,3 +50,7 @@ extern fan_config_t configCooling;
 | 
				
			|||||||
extern speedSensor speedLeft;
 | 
					extern speedSensor speedLeft;
 | 
				
			||||||
extern speedSensor speedRight;
 | 
					extern speedSensor speedRight;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//create global objects for controlling the chair position
 | 
				
			||||||
 | 
					extern cControlledRest legRest;
 | 
				
			||||||
 | 
					extern cControlledRest backRest;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -179,7 +179,7 @@ void controlledArmchair::startHandleLoop() {
 | 
				
			|||||||
                motorRight->setTarget(commands.right.state, commands.right.duty); 
 | 
					                motorRight->setTarget(commands.right.state, commands.right.duty); 
 | 
				
			||||||
                motorLeft->setTarget(commands.left.state, commands.left.duty); 
 | 
					                motorLeft->setTarget(commands.left.state, commands.left.duty); 
 | 
				
			||||||
                //--- control armchair position with joystick input ---
 | 
					                //--- control armchair position with joystick input ---
 | 
				
			||||||
                joystick_ControlChairAdjustment(stickData, 0);
 | 
					                controlChairAdjustment(joystick_l->getData(), &legRest, &backRest);
 | 
				
			||||||
                break;
 | 
					                break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -386,8 +386,8 @@ void controlledArmchair::changeMode(controlMode_t modeNew) {
 | 
				
			|||||||
        case controlMode_t::ADJUST_CHAIR:
 | 
					        case controlMode_t::ADJUST_CHAIR:
 | 
				
			||||||
            ESP_LOGW(TAG, "switching from ADJUST_CHAIR mode => turning off adjustment motors...");
 | 
					            ESP_LOGW(TAG, "switching from ADJUST_CHAIR mode => turning off adjustment motors...");
 | 
				
			||||||
            //prevent motors from being always on in case of mode switch while joystick is not in center thus motors currently moving
 | 
					            //prevent motors from being always on in case of mode switch while joystick is not in center thus motors currently moving
 | 
				
			||||||
            setLegrestOff();
 | 
					            legRest.setState(REST_OFF);
 | 
				
			||||||
            setBackrestOff();
 | 
					            backRest.setState(REST_OFF);
 | 
				
			||||||
            break;
 | 
					            break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
@ -154,6 +154,7 @@ void setLoglevels(void){
 | 
				
			|||||||
    esp_log_level_set("display", ESP_LOG_INFO);
 | 
					    esp_log_level_set("display", ESP_LOG_INFO);
 | 
				
			||||||
    //esp_log_level_set("current-sensors", ESP_LOG_INFO);
 | 
					    //esp_log_level_set("current-sensors", ESP_LOG_INFO);
 | 
				
			||||||
    //esp_log_level_set("speedSensor", ESP_LOG_INFO);
 | 
					    //esp_log_level_set("speedSensor", ESP_LOG_INFO);
 | 
				
			||||||
 | 
					    esp_log_level_set("chair-adjustment", ESP_LOG_INFO);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -4,32 +4,32 @@ extern "C"
 | 
				
			|||||||
#include "freertos/task.h"
 | 
					#include "freertos/task.h"
 | 
				
			||||||
#include "driver/gpio.h"
 | 
					#include "driver/gpio.h"
 | 
				
			||||||
#include "esp_log.h"
 | 
					#include "esp_log.h"
 | 
				
			||||||
 | 
					#include <string.h>
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "chairAdjust.hpp"
 | 
					#include "chairAdjust.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//--- gloabl variables ---
 | 
				
			||||||
 | 
					// strings for logging the rest state
 | 
				
			||||||
 | 
					const char* restStateStr[] = {"REST_OFF", "REST_DOWN", "REST_UP"};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//--- config ---
 | 
					//--- local variables ---
 | 
				
			||||||
//relays that control the motor for electric chair adjustment
 | 
					 | 
				
			||||||
//TODO: add this to config?
 | 
					 | 
				
			||||||
//relays connected to 4x stepper mosfets:
 | 
					 | 
				
			||||||
#define GPIO_LEGREST_UP     GPIO_NUM_4
 | 
					 | 
				
			||||||
#define GPIO_LEGREST_DOWN   GPIO_NUM_16
 | 
					 | 
				
			||||||
#define GPIO_BACKREST_UP    GPIO_NUM_2
 | 
					 | 
				
			||||||
#define GPIO_BACKREST_DOWN  GPIO_NUM_15
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
//--- variables ---
 | 
					 | 
				
			||||||
//tag for logging
 | 
					//tag for logging
 | 
				
			||||||
static const char * TAG = "chair-adjustment";
 | 
					static const char * TAG = "chair-adjustment";
 | 
				
			||||||
//current motor states
 | 
					 | 
				
			||||||
static restState_t stateLegRest = REST_OFF;
 | 
					 | 
				
			||||||
static restState_t stateBackRest = REST_OFF;
 | 
					 | 
				
			||||||
bool isInitialized = false;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//TODO Add timestamps or even a task to keep track of current position (estimate)
 | 
					
 | 
				
			||||||
 | 
					//=============================
 | 
				
			||||||
 | 
					//======== constructor ========
 | 
				
			||||||
 | 
					//=============================
 | 
				
			||||||
 | 
					cControlledRest::cControlledRest(gpio_num_t gpio_up_f, gpio_num_t gpio_down_f, const char * name_f){
 | 
				
			||||||
 | 
					    strcpy(name, name_f);
 | 
				
			||||||
 | 
					    gpio_up = gpio_up_f;
 | 
				
			||||||
 | 
					    gpio_down = gpio_down_f;
 | 
				
			||||||
 | 
					    init();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -37,136 +37,75 @@ bool isInitialized = false;
 | 
				
			|||||||
//======= init =======
 | 
					//======= init =======
 | 
				
			||||||
//====================
 | 
					//====================
 | 
				
			||||||
// init gpio pins for relays
 | 
					// init gpio pins for relays
 | 
				
			||||||
void chairAdjust_init(){
 | 
					void cControlledRest::init()
 | 
				
			||||||
    ESP_LOGW(TAG, "initializing gpio pins for relays...");
 | 
					{
 | 
				
			||||||
    gpio_pad_select_gpio(GPIO_LEGREST_UP);
 | 
					    ESP_LOGW(TAG, "[%s] initializing gpio pins %d, %d for relays...", name, gpio_up, gpio_down);
 | 
				
			||||||
    gpio_set_direction(GPIO_LEGREST_UP, GPIO_MODE_OUTPUT);
 | 
					    // configure 2 gpio pins
 | 
				
			||||||
    gpio_pad_select_gpio(GPIO_LEGREST_DOWN);
 | 
					    gpio_pad_select_gpio(gpio_up);
 | 
				
			||||||
    gpio_set_direction(GPIO_LEGREST_DOWN, GPIO_MODE_OUTPUT);
 | 
					    gpio_set_direction(gpio_up, GPIO_MODE_OUTPUT);
 | 
				
			||||||
    gpio_pad_select_gpio(GPIO_BACKREST_UP);
 | 
					    gpio_pad_select_gpio(gpio_down);
 | 
				
			||||||
    gpio_set_direction(GPIO_BACKREST_UP, GPIO_MODE_OUTPUT);
 | 
					    gpio_set_direction(gpio_down, GPIO_MODE_OUTPUT);
 | 
				
			||||||
    gpio_pad_select_gpio(GPIO_BACKREST_DOWN);
 | 
					    // both relays off initially
 | 
				
			||||||
    gpio_set_direction(GPIO_BACKREST_DOWN, GPIO_MODE_OUTPUT);
 | 
					    gpio_set_level(gpio_down, 0);
 | 
				
			||||||
    isInitialized = true;
 | 
					    gpio_set_level(gpio_up, 0);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//=============================
 | 
					//============================
 | 
				
			||||||
//======= set direction =======
 | 
					//========= setState =========
 | 
				
			||||||
//=============================
 | 
					//============================
 | 
				
			||||||
//functions for each rest that set the motors to desired direction / state
 | 
					void cControlledRest::setState(restState_t targetState)
 | 
				
			||||||
//TODO evaluate if separate functions needed, can be merged with run..Rest(state) function?
 | 
					{
 | 
				
			||||||
//--- leg-rest ---
 | 
					    //check if actually changed
 | 
				
			||||||
void setLegrestUp(){
 | 
					    if (targetState == state){
 | 
				
			||||||
    if (!isInitialized) chairAdjust_init();
 | 
					        ESP_LOGD(TAG, "[%s] state already at '%s', nothing to do", name, restStateStr[state]);
 | 
				
			||||||
    gpio_set_level(GPIO_LEGREST_DOWN, 0);
 | 
					        return;
 | 
				
			||||||
    gpio_set_level(GPIO_LEGREST_UP, 1);
 | 
					 | 
				
			||||||
    stateLegRest = REST_UP;
 | 
					 | 
				
			||||||
    ESP_LOGD(TAG, "switched relays to move leg-rest UP");
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
void setLegrestDown(){
 | 
					 | 
				
			||||||
    if (!isInitialized) chairAdjust_init();
 | 
					 | 
				
			||||||
    gpio_set_level(GPIO_LEGREST_DOWN, 1);
 | 
					 | 
				
			||||||
    gpio_set_level(GPIO_LEGREST_UP, 0);
 | 
					 | 
				
			||||||
    stateLegRest = REST_DOWN;
 | 
					 | 
				
			||||||
    ESP_LOGD(TAG, "switched relays to move leg-rest DOWN");
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
void setLegrestOff(){
 | 
					 | 
				
			||||||
    if (!isInitialized) chairAdjust_init();
 | 
					 | 
				
			||||||
    gpio_set_level(GPIO_LEGREST_DOWN, 0);
 | 
					 | 
				
			||||||
    gpio_set_level(GPIO_LEGREST_UP, 0);
 | 
					 | 
				
			||||||
    stateLegRest = REST_OFF;
 | 
					 | 
				
			||||||
    ESP_LOGD(TAG, "switched relays for leg-rest OFF");
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//--- back-rest ---
 | 
					    //apply new state
 | 
				
			||||||
void setBackrestUp(){
 | 
					    ESP_LOGI(TAG, "[%s] switching from state '%s' to '%s'", name, restStateStr[state], restStateStr[targetState]);
 | 
				
			||||||
    if (!isInitialized) chairAdjust_init();
 | 
					    state = targetState;
 | 
				
			||||||
    gpio_set_level(GPIO_BACKREST_DOWN, 0);
 | 
					    timestamp_lastChange = esp_log_timestamp(); //TODO use this to estimate position
 | 
				
			||||||
    gpio_set_level(GPIO_BACKREST_UP, 1);
 | 
					    switch (state)
 | 
				
			||||||
    stateBackRest = REST_UP;
 | 
					    {
 | 
				
			||||||
    ESP_LOGD(TAG, "switched relays to move back-rest UP");
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
void setBackrestDown(){
 | 
					 | 
				
			||||||
    if (!isInitialized) chairAdjust_init();
 | 
					 | 
				
			||||||
    gpio_set_level(GPIO_BACKREST_DOWN, 1);
 | 
					 | 
				
			||||||
    gpio_set_level(GPIO_BACKREST_UP, 0);
 | 
					 | 
				
			||||||
    stateBackRest = REST_DOWN;
 | 
					 | 
				
			||||||
    ESP_LOGD(TAG, "switched relays to move back-rest DOWN");
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
void setBackrestOff(){
 | 
					 | 
				
			||||||
    if (!isInitialized) chairAdjust_init();
 | 
					 | 
				
			||||||
    gpio_set_level(GPIO_BACKREST_DOWN, 0);
 | 
					 | 
				
			||||||
    gpio_set_level(GPIO_BACKREST_UP, 0);
 | 
					 | 
				
			||||||
    stateBackRest = REST_OFF;
 | 
					 | 
				
			||||||
    ESP_LOGD(TAG, "switched relays for back-rest OFF");
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
//==============================
 | 
					 | 
				
			||||||
//========= runLegrest =========
 | 
					 | 
				
			||||||
//==============================
 | 
					 | 
				
			||||||
//abstract functions that can be used to set the state of leg rest
 | 
					 | 
				
			||||||
// 0 = OFF;  <0 = DOWN;  >0 = UP
 | 
					 | 
				
			||||||
void runLegrest(float targetDirection){
 | 
					 | 
				
			||||||
    if (targetDirection > 0) {
 | 
					 | 
				
			||||||
        setLegrestUp();
 | 
					 | 
				
			||||||
        ESP_LOGD(TAG, "Leg-rest: coordinate = %.1f => run UP", targetDirection);
 | 
					 | 
				
			||||||
    } else if (targetDirection < 0) {
 | 
					 | 
				
			||||||
        setLegrestDown();
 | 
					 | 
				
			||||||
        ESP_LOGD(TAG, "Leg-rest: coordinate = %.1f => run DOWN", targetDirection);
 | 
					 | 
				
			||||||
    } else {
 | 
					 | 
				
			||||||
        setLegrestOff();
 | 
					 | 
				
			||||||
        ESP_LOGD(TAG, "Leg-rest: coordinate = %.1f => OFF", targetDirection);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
//set to certain state
 | 
					 | 
				
			||||||
void runLegrest(restState_t targetState){
 | 
					 | 
				
			||||||
    switch (targetState){
 | 
					 | 
				
			||||||
    case REST_UP:
 | 
					    case REST_UP:
 | 
				
			||||||
            setLegrestUp();
 | 
					        gpio_set_level(gpio_down, 0);
 | 
				
			||||||
 | 
					        gpio_set_level(gpio_up, 1);
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
    case REST_DOWN:
 | 
					    case REST_DOWN:
 | 
				
			||||||
            setLegrestDown();
 | 
					        gpio_set_level(gpio_down, 1);
 | 
				
			||||||
 | 
					        gpio_set_level(gpio_up, 0);
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
    case REST_OFF:
 | 
					    case REST_OFF:
 | 
				
			||||||
            setLegrestOff();
 | 
					        gpio_set_level(gpio_down, 1);
 | 
				
			||||||
 | 
					        gpio_set_level(gpio_up, 0);
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//==============================
 | 
					
 | 
				
			||||||
//========= runBackrest =========
 | 
					//====================================
 | 
				
			||||||
//==============================
 | 
					//====== controlChairAdjustment ======
 | 
				
			||||||
//abstract functions that can be used to set the state of back rest
 | 
					//====================================
 | 
				
			||||||
// 0 = OFF;  <0 = DOWN;  >0 = UP
 | 
					//function that controls the two rests according to joystick data (applies threshold, defines direction)
 | 
				
			||||||
void runBackrest(float targetDirection){
 | 
					//TODO:
 | 
				
			||||||
    if (targetDirection > 0) {
 | 
					// - add separate task that controls chair adjustment
 | 
				
			||||||
        setBackrestUp();
 | 
					//    - timeout
 | 
				
			||||||
        ESP_LOGD(TAG, "Back-rest: coordinate = %.1f => run UP", targetDirection);
 | 
					//    - track position
 | 
				
			||||||
    } else if (targetDirection < 0) {
 | 
					//    - auto-adjust: move to position while driving
 | 
				
			||||||
        setBackrestDown();
 | 
					//    - control via app
 | 
				
			||||||
        ESP_LOGD(TAG, "Back-rest: coordinate = %.1f => run DOWN", targetDirection);
 | 
					// - add delay betweem direction change
 | 
				
			||||||
    } else {
 | 
					void controlChairAdjustment(joystickData_t data, cControlledRest * legRest, cControlledRest * backRest){
 | 
				
			||||||
        setBackrestOff();
 | 
						//--- variables ---
 | 
				
			||||||
        ESP_LOGD(TAG, "back-rest: coordinate = %.1f => off", targetDirection);
 | 
					    float stickThreshold = 0.3; //min coordinate for motor to start
 | 
				
			||||||
    }
 | 
					
 | 
				
			||||||
}
 | 
					    //--- control rest motors ---
 | 
				
			||||||
//set to certain state
 | 
					    //leg rest (x-axis)
 | 
				
			||||||
void runBackrest(restState_t targetState){
 | 
					    if (data.x > stickThreshold) legRest->setState(REST_UP);
 | 
				
			||||||
    switch (targetState){
 | 
					    else if (data.x < -stickThreshold) legRest->setState(REST_DOWN);
 | 
				
			||||||
        case REST_UP:
 | 
					    //back rest (y-axis)
 | 
				
			||||||
            setBackrestUp();
 | 
					    if (data.y > stickThreshold) backRest->setState(REST_UP);
 | 
				
			||||||
            break;
 | 
					    else if (data.y < -stickThreshold) backRest->setState(REST_DOWN);
 | 
				
			||||||
        case REST_DOWN:
 | 
					 | 
				
			||||||
            setBackrestDown();
 | 
					 | 
				
			||||||
            break;
 | 
					 | 
				
			||||||
        case REST_OFF:
 | 
					 | 
				
			||||||
            setBackrestOff();
 | 
					 | 
				
			||||||
            break;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -1,28 +1,41 @@
 | 
				
			|||||||
#pragma once
 | 
					#pragma once
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include "joystick.hpp"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef enum {
 | 
					typedef enum {
 | 
				
			||||||
    REST_OFF = 0,
 | 
					    REST_OFF = 0,
 | 
				
			||||||
    REST_DOWN = -1,
 | 
					    REST_DOWN,
 | 
				
			||||||
    REST_UP = 1
 | 
					    REST_UP
 | 
				
			||||||
} restState_t;
 | 
					} restState_t;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//Set direction functions for leg-rest
 | 
					extern const char* restStateStr[];
 | 
				
			||||||
void setLegrestUp();
 | 
					 | 
				
			||||||
void setLegrestDown();
 | 
					 | 
				
			||||||
void setLegrestOff();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
//Set direction functions for back-rest
 | 
					 | 
				
			||||||
void setBackrestUp();
 | 
					 | 
				
			||||||
void setBackrestDown();
 | 
					 | 
				
			||||||
void setBackrestOff();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//Run leg-rest with target direction/state
 | 
					//=====================================
 | 
				
			||||||
// 0 = OFF;  <0 = DOWN;  >0 = UP
 | 
					//======= cControlledRest class =======
 | 
				
			||||||
void runLegrest(float targetDirection);
 | 
					//=====================================
 | 
				
			||||||
void runLegrest(restState_t targetState);
 | 
					//class that controls 2 relays powering a motor that moves a rest of the armchair up or down
 | 
				
			||||||
 | 
					//2 instances will be created one for back and one for leg rest
 | 
				
			||||||
 | 
					class cControlledRest {
 | 
				
			||||||
 | 
					    public:
 | 
				
			||||||
 | 
					    cControlledRest(gpio_num_t gpio_up, gpio_num_t gpio_down, const char * name);
 | 
				
			||||||
 | 
					    void setState(restState_t targetState);
 | 
				
			||||||
 | 
					    void stop();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//Run back-rest with target direction/state
 | 
					    private:
 | 
				
			||||||
// 0 = OFF;  <0 = DOWN;  >0 = UP
 | 
					    void init();
 | 
				
			||||||
void runBackrest(float targetDirection);
 | 
					
 | 
				
			||||||
void runBackrest(restState_t targetState);
 | 
					    char name[32];
 | 
				
			||||||
 | 
					    gpio_num_t gpio_up;
 | 
				
			||||||
 | 
					    gpio_num_t gpio_down;
 | 
				
			||||||
 | 
					    restState_t state;
 | 
				
			||||||
 | 
					    const uint32_t travelDuration = 5000;
 | 
				
			||||||
 | 
					    uint32_t timestamp_lastChange;
 | 
				
			||||||
 | 
					    float currentPosition = 0;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//====================================
 | 
				
			||||||
 | 
					//====== controlChairAdjustment ======
 | 
				
			||||||
 | 
					//====================================
 | 
				
			||||||
 | 
					//function that controls the two rests according to joystick data (applies threshold, defines direction)
 | 
				
			||||||
 | 
					void controlChairAdjustment(joystickData_t data, cControlledRest * legRest, cControlledRest * backRest);
 | 
				
			||||||
@ -570,35 +570,3 @@ motorCommands_t joystick_generateCommandsShaking(joystickData_t data){
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    return commands;
 | 
					    return commands;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
//=============================================
 | 
					 | 
				
			||||||
//====== joystick_ControlChairAdjustment ======
 | 
					 | 
				
			||||||
//=============================================
 | 
					 | 
				
			||||||
//function that controls the motors for adjusting the chair position using joystick input
 | 
					 | 
				
			||||||
//FIXME: turn off after timeout / function not called anymore? => separate task
 | 
					 | 
				
			||||||
//TODO: store current position
 | 
					 | 
				
			||||||
//FIXME: control with webinterface too -> conflict? => separate task
 | 
					 | 
				
			||||||
void joystick_ControlChairAdjustment(joystickData_t data, bool disable){
 | 
					 | 
				
			||||||
	//--- variables ---
 | 
					 | 
				
			||||||
    float radiusThreshold = 0.5; //min radius where movement starts
 | 
					 | 
				
			||||||
    //TODO: Add additional tolerance range where coordinates snaps to axis (move 1 motor only) -> run scaleCoordinate and evaluagePosition again:
 | 
					 | 
				
			||||||
    //float axisThreshold = 0.2; //axis tolerance (snap to 1 motor only)
 | 
					 | 
				
			||||||
    //float ratio = fabs(data.angle) / 90; //90degree = x=0 || 0degree = y=0
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    //--- off threshold ---
 | 
					 | 
				
			||||||
    //do not run when threshold not reached
 | 
					 | 
				
			||||||
    if (data.radius < radiusThreshold){
 | 
					 | 
				
			||||||
        data.x = 0;
 | 
					 | 
				
			||||||
        data.y = 0;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    //--- run rest motors ---
 | 
					 | 
				
			||||||
    //state/direction depends on coodrdinate sign
 | 
					 | 
				
			||||||
    runLegrest(data.x);
 | 
					 | 
				
			||||||
    runBackrest(data.y);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
				
			|||||||
@ -12,7 +12,6 @@ extern "C"
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#include <cmath>
 | 
					#include <cmath>
 | 
				
			||||||
#include "types.hpp"
 | 
					#include "types.hpp"
 | 
				
			||||||
#include "chairAdjust.hpp"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//======================================
 | 
					//======================================
 | 
				
			||||||
@ -117,14 +116,6 @@ motorCommands_t joystick_generateCommandsShaking(joystickData_t data );
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//=============================================
 | 
					 | 
				
			||||||
//====== joystick_ControlChairAdjustment ======
 | 
					 | 
				
			||||||
//=============================================
 | 
					 | 
				
			||||||
//function that controls the motors for adjusting the chair position using joystick input
 | 
					 | 
				
			||||||
void joystick_ControlChairAdjustment(joystickData_t data, bool disable);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
//==============================
 | 
					//==============================
 | 
				
			||||||
//====== scaleCoordinate =======
 | 
					//====== scaleCoordinate =======
 | 
				
			||||||
//==============================
 | 
					//==============================
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user