Add functional STEPPER_TEST feature

- add macro variable STEPPER_TEST (config.hpp)
    if set: only `task_stepper-test` is started (main.cpp)
- add file for stepper/guide related code (guide-stepper.cpp/hpp)
    currently defines a task that simply moves the axis left to right
    repeatedly with a speed defined by a potentiometer connected to ADC
    GPIO34
This commit is contained in:
jonny_ji7
2023-02-21 21:27:33 +01:00
parent 2651a83ce7
commit 58c5a92d1e
6 changed files with 238 additions and 373 deletions

View File

@@ -8,6 +8,7 @@ idf_component_register(
"display.cpp"
"cutter.cpp"
"switchesAnalog.cpp"
"guide-stepper.cpp"
INCLUDE_DIRS
"."
)

View File

@@ -79,6 +79,17 @@ extern "C" {
#define ENABLE_HALF_STEPS false // Set to true to enable tracking of rotary encoder at half step resolution
#define FLIP_DIRECTION false // Set to true to reverse the clockwise/counterclockwise sense
//--------------------------
//----- stepper config -----
//--------------------------
//enable stepper test mode (dont start control and encoder task)
#define STEPPER_TEST
//more detailed options for testing are currently defined in guide-stepper.cpp
//--------------------------
//------ calibration -------
//--------------------------

105
main/guide-stepper.cpp Normal file
View File

@@ -0,0 +1,105 @@
#include "guide-stepper.hpp"
#define LEN_MOVE 90 //mm
#define SPEED_MIN 5.0 //mm/s
#define SPEED_MAX 110.0 //mm/s
#define ACCEL_MS 100.0 //ms from 0 to max
#define DECEL_MS 90.0 //ms from max to 0
#define STEPS_PER_ROT 1600
//----------------------
//----- variables ------
//----------------------
DendoStepper step;
DendoStepper step1;
static const char *TAG = "stepper"; //tag for logging
//----------------------
//----- functions ------
//----------------------
//calculate time needed for certain length (NOT NEEDED/ DELETE)
//float mmToS(int l){
//
// double accel = SPEED_MAX / (ACCEL_MS/1000);
// double t_accelMax = ACCEL_MS/1000;
// double l_accelMax = accel * t_accelMax * t_accelMax;
//
// printf("accel=%.2lf mm/s2 __ t_accelMAX=%.2lfs __ l_accelMax=%.2lfmm\n", accel, t_accelMax, l_accelMax);
// if (l < l_accelMax){
// return sqrt(l / accel);
// } else {
// return t_accelMax + (l - l_accelMax) / SPEED_MAX;
// }
//}
//define zero/start position
//currently crashes into hardware limitation for certain time
//TODO: limit switch
void home() {
ESP_LOGW(TAG, "auto-home...");
step.setSpeedMm(120, 120, 100);
step.runInf(0);
vTaskDelay(2000 / portTICK_PERIOD_MS);
step.stop();
step.resetAbsolute();
ESP_LOGW(TAG, "auto-home finished");
}
//initialize/configure stepper instance
void init_stepper() {
ESP_LOGW(TAG, "initializing stepper...");
DendoStepper_config_t step_cfg = {
.stepPin = 18,
.dirPin = 19,
.enPin = 21,
.timer_group = TIMER_GROUP_0,
.timer_idx = TIMER_0,
.miStep = MICROSTEP_32,
.stepAngle = 1.8};
step.config(&step_cfg);
step.init();
step.setSpeed(1000, 1000, 1000); //random default speed
step.setStepsPerMm(STEPS_PER_ROT /4); //guide: 4mm/rot
}
//function that updates speed value using ADC input and configured MIN/MAX
void updateSpeedFromAdc() {
int potiRead = gpio_readAdc(ADC1_CHANNEL_6); //0-4095 GPIO34
double poti = potiRead/4095.0;
int speed = poti*(SPEED_MAX-SPEED_MIN) + SPEED_MIN;
step.setSpeedMm(speed, ACCEL_MS, DECEL_MS);
ESP_LOGW(TAG, "poti: %d (%.2lf%%), set speed to: %d", potiRead, poti*100, speed);
}
//---------------------
//------- TASK --------
//---------------------
void task_stepper(void *pvParameter)
{
init_stepper();
home();
while (1) {
updateSpeedFromAdc();
step.runPosMm(-LEN_MOVE);
while(step.getState() != 1) vTaskDelay(2);
ESP_LOGI(TAG, "finished moving right => moving left");
updateSpeedFromAdc();
step.runPosMm(LEN_MOVE);
while(step.getState() != 1) vTaskDelay(2); //1=idle
ESP_LOGI(TAG, "finished moving left => moving right");
}
}

19
main/guide-stepper.hpp Normal file
View File

@@ -0,0 +1,19 @@
extern "C"
{
#include <stdio.h>
#include "DendoStepper.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "driver/adc.h"
}
#include "config.hpp"
//task that initializes and controls the stepper motor
//current functionality:
// - automatically auto-homes
// - moves left and right repeatedly
// - updates speed from potentiometer each cycle
void task_stepper(void *pvParameter);

View File

@@ -14,6 +14,7 @@ extern "C"
#include "control.hpp"
#include "buzzer.hpp"
#include "switchesAnalog.hpp"
#include "guide-stepper.hpp"
//=================================
@@ -81,11 +82,17 @@ extern "C" void app_main()
esp_log_level_set("buzzer", ESP_LOG_ERROR);
esp_log_level_set("switches-analog", ESP_LOG_WARN);
esp_log_level_set("control", ESP_LOG_INFO);
esp_log_level_set("stepper", ESP_LOG_INFO);
#ifdef STEPPER_TEST
//create task for stepper testing
xTaskCreate(task_stepper, "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
//beep at startup
buzzer.beep(3, 70, 50);