From 5dd392586dc9bebb02d82e239b49c990a22d798f Mon Sep 17 00:00:00 2001 From: jonny_ji7 Date: Tue, 28 Feb 2023 23:57:55 +0100 Subject: [PATCH] Outsource encoder code to encoder.cpp To be able to use the encoder in future stepper task as well - make certain variables global - initialize encoder in main - outsorce encoder functions in encoder.cpp / h - adjust config --- main/CMakeLists.txt | 1 + main/config.hpp | 7 +++++-- main/control.cpp | 30 ++---------------------------- main/encoder.cpp | 39 +++++++++++++++++++++++++++++++++++++++ main/encoder.hpp | 20 ++++++++++++++++++++ main/main.cpp | 7 ++++++- 6 files changed, 73 insertions(+), 31 deletions(-) create mode 100644 main/encoder.cpp create mode 100644 main/encoder.hpp diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index f627fd9..d2b02cb 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -9,6 +9,7 @@ idf_component_register( "cutter.cpp" "switchesAnalog.cpp" "guide-stepper.cpp" + "encoder.cpp" INCLUDE_DIRS "." ) diff --git a/main/config.hpp b/main/config.hpp index 55838f6..92f7cd3 100644 --- a/main/config.hpp +++ b/main/config.hpp @@ -85,7 +85,10 @@ extern "C" { //----- stepper config ----- //-------------------------- //enable stepper test mode (dont start control and encoder task) -#define STEPPER_TEST +//#define STEPPER_TEST +#define STEPPER_STEP_PIN GPIO_NUM_18 //(mos1) +#define STEPPER_DIR_PIN GPIO_NUM_5 //(mos2) +#define STEPPER_EN_PIN GPIO_NUM_0 //not connected (stepper always on) //more detailed options for testing are currently defined in guide-stepper.cpp @@ -98,7 +101,7 @@ extern "C" { //#define ENCODER_TEST //steps per meter -#define STEPS_PER_METER 2127 //roll-v3-gummi-86.6mm - d=89.8mm +#define ENCODER_STEPS_PER_METER 2127 //roll-v3-gummi-86.6mm - d=89.8mm //millimetres added to target length //to ensure that length does not fall short when spool slightly rotates back after stop diff --git a/main/control.cpp b/main/control.cpp index 24b1638..c73fe52 100644 --- a/main/control.cpp +++ b/main/control.cpp @@ -1,27 +1,5 @@ #include "control.hpp" - - -//======================== -//===== init encoder ===== -//======================== -QueueHandle_t init_encoder(rotary_encoder_info_t * info){ - // 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)); -#ifdef FLIP_DIRECTION - ESP_ERROR_CHECK(rotary_encoder_flip_direction(info)); -#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)); - return event_queue; -} - +#include "encoder.hpp" //==================== @@ -38,8 +16,6 @@ char buf_disp1[10];// 8 digits + decimal point + \0 char buf_disp2[10];// 8 digits + decimal point + \0 char buf_tmp[15]; -rotary_encoder_info_t encoder; //encoder device/info -QueueHandle_t encoder_queue = NULL; //encoder event queue rotary_encoder_state_t encoderState; int lengthNow = 0; //length measured in mm @@ -135,8 +111,6 @@ void setDynSpeedLvl(uint8_t lvlMax = 3){ //======================== void task_control(void *pvParameter) { - //initialize encoder - encoder_queue = init_encoder(&encoder); //initialize display max7219_t two7SegDisplays = display_init(); @@ -182,7 +156,7 @@ void task_control(void *pvParameter) // Poll current position and direction rotary_encoder_get_state(&encoder, &encoderState); //--- calculate distance --- - lengthNow = (float)encoderState.position * 1000 / STEPS_PER_METER; + lengthNow = (float)encoderState.position * 1000 / ENCODER_STEPS_PER_METER; //--------------------------- diff --git a/main/encoder.cpp b/main/encoder.cpp new file mode 100644 index 0000000..63e5096 --- /dev/null +++ b/main/encoder.cpp @@ -0,0 +1,39 @@ +extern "C" { +#include +#include +#include "esp_system.h" +#include "esp_log.h" +} + +#include "encoder.hpp" + + +//---------------------------- +//----- global variables ----- +//---------------------------- +rotary_encoder_info_t encoder; //encoder device/info +QueueHandle_t encoder_queue = NULL; //encoder event queue + + +//------------------------- +//------- functions ------- +//------------------------- +//--- init_encoder --- +//initialize encoder and return event queue +QueueHandle_t init_encoder(rotary_encoder_info_t * info){ + // 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)); +#ifdef FLIP_DIRECTION + ESP_ERROR_CHECK(rotary_encoder_flip_direction(info)); +#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)); + return event_queue; +} diff --git a/main/encoder.hpp b/main/encoder.hpp new file mode 100644 index 0000000..6f6a652 --- /dev/null +++ b/main/encoder.hpp @@ -0,0 +1,20 @@ +#pragma once +extern "C" { +#include "rotary_encoder.h" +#include +} + +#include "config.hpp" + + +//---------------------------- +//----- global variables ----- +//---------------------------- +extern rotary_encoder_info_t encoder; //encoder device/info +extern QueueHandle_t encoder_queue; //encoder event queue + + +//------------------------ +//----- init encoder ----- +//------------------------ +QueueHandle_t init_encoder(rotary_encoder_info_t * info); diff --git a/main/main.cpp b/main/main.cpp index b97bfc5..4e3f22a 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -15,6 +15,7 @@ extern "C" #include "buzzer.hpp" #include "switchesAnalog.hpp" #include "guide-stepper.hpp" +#include "encoder.hpp" //================================= @@ -77,6 +78,9 @@ extern "C" void app_main() //enable 5V volate regulator gpio_set_level(GPIO_NUM_17, 1); + //init encoder (global) + encoder_queue = init_encoder(&encoder); + //define loglevel esp_log_level_set("*", ESP_LOG_INFO); esp_log_level_set("buzzer", ESP_LOG_ERROR); @@ -86,10 +90,11 @@ extern "C" void app_main() #ifdef STEPPER_TEST //create task for stepper testing - xTaskCreate(task_stepper, "task_stepper-test", configMINIMAL_STACK_SIZE * 3, NULL, 5, NULL); + xTaskCreate(task_stepper_test, "task_stepper-test", configMINIMAL_STACK_SIZE * 3, NULL, 5, NULL); #else //create task for controlling the machine xTaskCreate(task_control, "task_control", configMINIMAL_STACK_SIZE * 3, NULL, 5, NULL); + //create task for handling the buzzer xTaskCreate(&task_buzzer, "task_buzzer", 2048, NULL, 2, NULL); #endif