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
This commit is contained in:
parent
74adcbc78a
commit
5dd392586d
@ -9,6 +9,7 @@ idf_component_register(
|
||||
"cutter.cpp"
|
||||
"switchesAnalog.cpp"
|
||||
"guide-stepper.cpp"
|
||||
"encoder.cpp"
|
||||
INCLUDE_DIRS
|
||||
"."
|
||||
)
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
||||
|
||||
//---------------------------
|
||||
|
39
main/encoder.cpp
Normal file
39
main/encoder.cpp
Normal file
@ -0,0 +1,39 @@
|
||||
extern "C" {
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#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;
|
||||
}
|
20
main/encoder.hpp
Normal file
20
main/encoder.hpp
Normal file
@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
extern "C" {
|
||||
#include "rotary_encoder.h"
|
||||
#include <freertos/task.h>
|
||||
}
|
||||
|
||||
#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);
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user