Fix variable scopes (static), Simplify encoder

- add static type to all local variables in every source file
  so the scope of each variable is limited to that file.
  This prevents conflicting variable names with other source file
  (e.g. 'state' variable in vfd.cpp)

- simplify encoder concept
  wrap all used functions from rotary_encoder.h library in
  custom encoder.hpp file
   -> only one file has to be included where encoder is used
   -> global variable 'encoder' is not necessary anymore
   -> reduces duplicate code for length calculation
- update all files where encoder functions where used accordingly
This commit is contained in:
jonny_ji7 2023-03-01 00:47:51 +01:00
parent 5dd392586d
commit ac4ca5450a
7 changed files with 89 additions and 47 deletions

View File

@ -9,29 +9,23 @@ static const char *TAG = "control"; //tag for logging
const char* systemStateStr[7] = {"COUNTING", "WINDING_START", "WINDING", "TARGET_REACHED", "AUTO_CUT_WAITING", "CUTTING", "MANUAL"};
systemState_t controlState = systemState_t::COUNTING;
uint32_t timestamp_changedState = 0;
static uint32_t timestamp_changedState = 0;
char buf_disp[20]; //both displays
char buf_disp1[10];// 8 digits + decimal point + \0
char buf_disp2[10];// 8 digits + decimal point + \0
char buf_tmp[15];
static char buf_disp1[10];// 8 digits + decimal point + \0
static char buf_disp2[10];// 8 digits + decimal point + \0
static char buf_tmp[15];
rotary_encoder_state_t encoderState;
int lengthNow = 0; //length measured in mm
int lengthTarget = 5000; //default target length in mm
int lengthRemaining = 0; //(target - now) length needed for reaching the target
int potiRead = 0; //voltage read from adc
uint32_t timestamp_motorStarted = 0; //timestamp winding started
static int lengthNow = 0; //length measured in mm
static int lengthTarget = 5000; //default target length in mm
static int lengthRemaining = 0; //(target - now) length needed for reaching the target
static int potiRead = 0; //voltage read from adc
static uint32_t timestamp_motorStarted = 0; //timestamp winding started
//encoder test / calibration
int lengthBeeped = 0; //only beep once per meter during encoder test
//automatic cut
int cut_msRemaining = 0;
uint32_t timestamp_cut_lastBeep = 0;
uint32_t autoCut_delayMs = 2500; //TODO add this to config
bool autoCutEnabled = false; //store state of toggle switch (no hotswitch)
static int cut_msRemaining = 0;
static uint32_t timestamp_cut_lastBeep = 0;
static uint32_t autoCut_delayMs = 2500; //TODO add this to config
static bool autoCutEnabled = false; //store state of toggle switch (no hotswitch)
//===== change State =====
@ -153,10 +147,8 @@ void task_control(void *pvParameter)
//----------------------------
//------ rotary encoder ------
//----------------------------
// Poll current position and direction
rotary_encoder_get_state(&encoder, &encoderState);
//--- calculate distance ---
lengthNow = (float)encoderState.position * 1000 / ENCODER_STEPS_PER_METER;
//--- get current length since last reset ---
lengthNow = encoder_getLenMm();
//---------------------------
@ -166,7 +158,7 @@ void task_control(void *pvParameter)
if (SW_RESET.risingEdge) {
//dont reset when press used for stopping pending auto-cut
if (controlState != systemState_t::AUTO_CUT_WAITING) {
rotary_encoder_reset(&encoder);
encoder_reset();
lengthNow = 0;
buzzer.beep(1, 700, 100);
displayTop.blink(2, 100, 100, "1ST ");
@ -371,7 +363,7 @@ void task_control(void *pvParameter)
//TODO stop if start buttons released?
changeState(systemState_t::COUNTING);
//TODO reset automatically or wait for manual reset?
rotary_encoder_reset(&encoder);
encoder_reset();
lengthNow = 0;
buzzer.beep(1, 700, 100);
}
@ -417,7 +409,7 @@ void task_control(void *pvParameter)
displayTop.handle();
displayBot.handle();
//-- show encoder steps on display1 ---
sprintf(buf_disp1, "EN %05d", encoderState.position); //count
sprintf(buf_disp1, "EN %05d", encoder_getSteps); //count
displayTop.showString(buf_disp1);
//--- show converted distance on display2 ---
sprintf(buf_disp2, "Met %5.3f", (float)lengthNow/1000); //m

View File

@ -10,7 +10,6 @@ extern "C"
#include "esp_log.h"
#include "driver/adc.h"
#include "rotary_encoder.h"
#include "max7219.h"
}

View File

@ -15,9 +15,9 @@ bool checkTimeout();
//---------------------------
//----- local variables -----
//---------------------------
cutter_state_t cutter_state = cutter_state_t::IDLE;
uint32_t timestamp_turnedOn;
uint32_t msTimeout = 3000;
static cutter_state_t cutter_state = cutter_state_t::IDLE;
static uint32_t timestamp_turnedOn;
static uint32_t msTimeout = 3000;
static const char *TAG = "cutter"; //tag for logging

View File

@ -3,6 +3,8 @@ extern "C" {
#include <freertos/task.h>
#include "esp_system.h"
#include "esp_log.h"
#include "rotary_encoder.h"
}
#include "encoder.hpp"
@ -11,29 +13,55 @@ extern "C" {
//----------------------------
//----- global variables -----
//----------------------------
rotary_encoder_info_t encoder; //encoder device/info
static rotary_encoder_info_t encoder; //encoder device/info
QueueHandle_t encoder_queue = NULL; //encoder event queue
//-------------------------
//------- functions -------
//-------------------------
//--- init_encoder ---
//--- encoder_init ---
//initialize encoder and return event queue
QueueHandle_t init_encoder(rotary_encoder_info_t * info){
QueueHandle_t encoder_init(){
// esp32-rotary-encoder requires that the GPIO ISR service is installed before calling rotary_encoder_register()
ESP_ERROR_CHECK(gpio_install_isr_service(0));
// Initialise the rotary encoder device with the GPIOs for A and B signals
ESP_ERROR_CHECK(rotary_encoder_init(info, ROT_ENC_A_GPIO, ROT_ENC_B_GPIO));
ESP_ERROR_CHECK(rotary_encoder_enable_half_steps(info, ENABLE_HALF_STEPS));
ESP_ERROR_CHECK(rotary_encoder_init(&encoder, ROT_ENC_A_GPIO, ROT_ENC_B_GPIO));
ESP_ERROR_CHECK(rotary_encoder_enable_half_steps(&encoder, ENABLE_HALF_STEPS));
#ifdef FLIP_DIRECTION
ESP_ERROR_CHECK(rotary_encoder_flip_direction(info));
ESP_ERROR_CHECK(rotary_encoder_flip_direction(&encoder));
#endif
// Create a queue for events from the rotary encoder driver.
// Tasks can read from this queue to receive up to date position information.
QueueHandle_t event_queue = rotary_encoder_create_queue();
ESP_ERROR_CHECK(rotary_encoder_set_queue(info, event_queue));
ESP_ERROR_CHECK(rotary_encoder_set_queue(&encoder, event_queue));
return event_queue;
}
//--- encoder_getSteps ---
//get steps counted since last reset
int encoder_getSteps(){
// Poll current position and direction
rotary_encoder_state_t encoderState;
rotary_encoder_get_state(&encoder, &encoderState);
//calculate total distance since last reset
return encoderState.position;
}
//--- encoder_getLenMm ---
//get current length in Mm since last reset
int encoder_getLenMm(){
return (float)encoder_getSteps() * 1000 / ENCODER_STEPS_PER_METER;
}
//--- encoder_reset ---
//reset counted steps / length to 0
void encoder_reset(){
rotary_encoder_reset(&encoder);
return;
}

View File

@ -1,6 +1,8 @@
/* in this file all used functions from original rotary_encoder.h library are wrapped with custom functions to reduce global variables and duplicate code
*/
//TODO create a cpp class for an encoder?
#pragma once
extern "C" {
#include "rotary_encoder.h"
#include <freertos/task.h>
}
@ -10,11 +12,29 @@ extern "C" {
//----------------------------
//----- global variables -----
//----------------------------
extern rotary_encoder_info_t encoder; //encoder device/info
//TODO ignore global encoder queue, since it is not used?
extern QueueHandle_t encoder_queue; //encoder event queue
//------------------------
//----- init encoder -----
//------------------------
QueueHandle_t init_encoder(rotary_encoder_info_t * info);
//-------------------------
//------- functions -------
//-------------------------
//--- encoder_init ---
//init encoder
QueueHandle_t encoder_init();
//--- encoder_getSteps ---
//get steps counted since last reset
int encoder_getSteps();
//--- encoder_getLenMm ---
//get current length in Mm since last reset
int encoder_getLenMm();
//--- encoder_reset ---
//reset counted steps / length to 0
void encoder_reset();

View File

@ -79,7 +79,7 @@ extern "C" void app_main()
gpio_set_level(GPIO_NUM_17, 1);
//init encoder (global)
encoder_queue = init_encoder(&encoder);
encoder_queue = encoder_init();
//define loglevel
esp_log_level_set("*", ESP_LOG_INFO);
@ -95,6 +95,9 @@ extern "C" void app_main()
//create task for controlling the machine
xTaskCreate(task_control, "task_control", configMINIMAL_STACK_SIZE * 3, NULL, 5, NULL);
//create task for controlling the stepper
xTaskCreate(task_stepper_ctl, "task_stepper-test", configMINIMAL_STACK_SIZE * 3, NULL, 5, NULL);
//create task for handling the buzzer
xTaskCreate(&task_buzzer, "task_buzzer", 2048, NULL, 2, NULL);
#endif

View File

@ -4,9 +4,9 @@
const char* vfd_directionStr[2] = {"FWD", "REV"};
static const char *TAG = "vfd";
uint8_t level = 0; //current speed level
bool state = false; //current state
vfd_direction_t direction = FWD; //current direction
static uint8_t level = 0; //current speed level
static bool state = false; //current state
static vfd_direction_t direction = FWD; //current direction
//=============================