Add chairAdjust driver, Add joystick control
- Add functions that control the relays for adjusting the armchair position - Add function that controls the chair using joystick input
This commit is contained in:
		
							parent
							
								
									96d627c217
								
							
						
					
					
						commit
						9dfe1bddb2
					
				@ -10,6 +10,7 @@ idf_component_register(
 | 
			
		||||
		"joystick.cpp"
 | 
			
		||||
		"http.cpp"
 | 
			
		||||
		"speedsensor.cpp"
 | 
			
		||||
        "chairAdjust.cpp"
 | 
			
		||||
    INCLUDE_DIRS 
 | 
			
		||||
        "."
 | 
			
		||||
		PRIV_REQUIRES nvs_flash mdns json spiffs esp_http_server
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										147
									
								
								common/chairAdjust.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										147
									
								
								common/chairAdjust.cpp
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,147 @@
 | 
			
		||||
extern "C"
 | 
			
		||||
{
 | 
			
		||||
#include "freertos/FreeRTOS.h"
 | 
			
		||||
#include "freertos/task.h"
 | 
			
		||||
#include "driver/gpio.h"
 | 
			
		||||
#include "esp_log.h"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#include "chairAdjust.hpp"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
//--- config ---
 | 
			
		||||
//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
 | 
			
		||||
static const char * TAG = "chair-adjustment";
 | 
			
		||||
//current motor states
 | 
			
		||||
static restState_t stateLegRest = REST_OFF;
 | 
			
		||||
static restState_t stateBackRest = REST_OFF;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
//TODO Add timestamps or even a task to keep track of current position (estimate)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
//=============================
 | 
			
		||||
//======= set direction =======
 | 
			
		||||
//=============================
 | 
			
		||||
//functions for each rest that set the motors to desired direction / state
 | 
			
		||||
//TODO evaluate if separate functions needed, can be merged with run..Rest(state) function?
 | 
			
		||||
//--- leg-rest ---
 | 
			
		||||
void setLegrestUp(){
 | 
			
		||||
        gpio_set_level(GPIO_LEGREST_DOWN, 0);
 | 
			
		||||
        gpio_set_level(GPIO_LEGREST_UP, 1);
 | 
			
		||||
        stateLegRest = REST_UP;
 | 
			
		||||
        ESP_LOGD(TAG, "switched relays to move leg-rest UP");
 | 
			
		||||
}
 | 
			
		||||
void setLegrestDown(){
 | 
			
		||||
        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(){
 | 
			
		||||
        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 ---
 | 
			
		||||
void setBackrestUp(){
 | 
			
		||||
        gpio_set_level(GPIO_BACKREST_DOWN, 0);
 | 
			
		||||
        gpio_set_level(GPIO_BACKREST_UP, 1);
 | 
			
		||||
        stateBackRest = REST_UP;
 | 
			
		||||
        ESP_LOGD(TAG, "switched relays to move back-rest UP");
 | 
			
		||||
}
 | 
			
		||||
void setBackrestDown(){
 | 
			
		||||
        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(){
 | 
			
		||||
        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:
 | 
			
		||||
            setLegrestUp();
 | 
			
		||||
            break;
 | 
			
		||||
        case REST_DOWN:
 | 
			
		||||
            setLegrestDown();
 | 
			
		||||
            break;
 | 
			
		||||
        case REST_OFF:
 | 
			
		||||
            setLegrestOff();
 | 
			
		||||
            break;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
//==============================
 | 
			
		||||
//========= runBackrest =========
 | 
			
		||||
//==============================
 | 
			
		||||
//abstract functions that can be used to set the state of back rest
 | 
			
		||||
// 0 = OFF;  <0 = DOWN;  >0 = UP
 | 
			
		||||
void runBackrest(float targetDirection){
 | 
			
		||||
    if (targetDirection > 0) {
 | 
			
		||||
        setBackrestUp();
 | 
			
		||||
        ESP_LOGD(TAG, "Back-rest: coordinate = %.1f => run UP", targetDirection);
 | 
			
		||||
    } else if (targetDirection < 0) {
 | 
			
		||||
        setBackrestDown();
 | 
			
		||||
        ESP_LOGD(TAG, "Back-rest: coordinate = %.1f => run DOWN", targetDirection);
 | 
			
		||||
    } else {
 | 
			
		||||
        setBackrestOff();
 | 
			
		||||
        ESP_LOGD(TAG, "back-rest: coordinate = %.1f => off", targetDirection);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
//set to certain state
 | 
			
		||||
void runBackrest(restState_t targetState){
 | 
			
		||||
    switch (targetState){
 | 
			
		||||
        case REST_UP:
 | 
			
		||||
            setBackrestUp();
 | 
			
		||||
            break;
 | 
			
		||||
        case REST_DOWN:
 | 
			
		||||
            setBackrestDown();
 | 
			
		||||
            break;
 | 
			
		||||
        case REST_OFF:
 | 
			
		||||
            setBackrestOff();
 | 
			
		||||
            break;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										28
									
								
								common/chairAdjust.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								common/chairAdjust.hpp
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,28 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
typedef enum {
 | 
			
		||||
    REST_OFF = 0,
 | 
			
		||||
    REST_DOWN = -1,
 | 
			
		||||
    REST_UP = 1
 | 
			
		||||
} restState_t;
 | 
			
		||||
 | 
			
		||||
//Set direction functions for leg-rest
 | 
			
		||||
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
 | 
			
		||||
void runLegrest(float targetDirection);
 | 
			
		||||
void runLegrest(restState_t targetState);
 | 
			
		||||
 | 
			
		||||
//Run back-rest with target direction/state
 | 
			
		||||
// 0 = OFF;  <0 = DOWN;  >0 = UP
 | 
			
		||||
void runBackrest(float targetDirection);
 | 
			
		||||
void runBackrest(restState_t targetState);
 | 
			
		||||
@ -570,3 +570,35 @@ motorCommands_t joystick_generateCommandsShaking(joystickData_t data){
 | 
			
		||||
 | 
			
		||||
    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,6 +12,7 @@ extern "C"
 | 
			
		||||
 | 
			
		||||
#include <cmath>
 | 
			
		||||
#include "types.hpp"
 | 
			
		||||
#include "chairAdjust.hpp"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
//======================================
 | 
			
		||||
@ -116,6 +117,14 @@ 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 =======
 | 
			
		||||
//==============================
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user