Move stepper-cfg to config.h, Rework guide-stepper.cpp (new driver)

- move stepper config e.g. speed accel steps... from stepper.cpp to
config.h

- uncomment and rework code in guide-stepper.cpp (for traveling the
  stepper based on encoder) to work with new custom driver instead of
  previous library

Note currently in STEPPER_SIMULATE_ECNODER mode (use button as encoder),
see macro
This commit is contained in:
jonny_ji7 2023-04-26 17:46:06 +02:00
parent a22c60b817
commit 9cae5cf75d
3 changed files with 164 additions and 164 deletions

View File

@ -86,10 +86,16 @@ extern "C" {
//-------------------------- //--------------------------
//enable stepper test mode (dont start control and encoder task) //enable stepper test mode (dont start control and encoder task)
#define STEPPER_TEST #define STEPPER_TEST
#define STEPPER_STEP_PIN GPIO_NUM_18 //mos1 //pins
#define STEPPER_DIR_PIN GPIO_NUM_16 //ST3 #define STEPPER_STEP_PIN GPIO_NUM_18 //mos1
#define STEPPER_EN_PIN GPIO_NUM_0 //not connected (-> stepper always on) #define STEPPER_DIR_PIN GPIO_NUM_16 //ST3
//more detailed options for testing are currently defined in guide-stepper.cpp //driver settings
#define STEPPER_STEPS_PER_MM 200/2 //steps/mm (steps-per-rot / slope)
#define STEPPER_SPEED_DEFAULT 20 //mm/s
#define STEPPER_SPEED_MIN 4 //mm/s - speed threshold at which stepper immediately starts/stops
#define STEPPER_ACCEL_INC 3 //steps/s increment per cycle
#define STEPPER_DECEL_INC 8 //steps/s decrement per cycle
//options affecting movement are currently defined in guide-stepper.cpp

View File

@ -21,32 +21,28 @@ extern "C"
//for pin definition //for pin definition
#define STEPPER_TEST_TRAVEL 65 //mm #define STEPPER_TEST_TRAVEL 65 //mm
//
#define MIN_MM 0 #define MIN_MM 0
#define MAX_MM 60 #define MAX_MM 40
#define POS_MAX_STEPS MAX_MM * STEPPER_STEPS_PER_MM #define POS_MAX_STEPS MAX_MM * STEPPER_STEPS_PER_MM
#define POS_MIN_STEPS MIN_MM * STEPPER_STEPS_PER_MM #define POS_MIN_STEPS MIN_MM * STEPPER_STEPS_PER_MM
#define SPEED_MIN 2.0 //mm/s #define SPEED_MIN 2.0 //mm/s
#define SPEED_MAX 60.0 //mm/s #define SPEED_MAX 60.0 //mm/s
#define ACCEL_MS 100.0 //ms from 0 to max
#define DECEL_MS 90.0 //ms from max to 0
#define STEPPER_STEPS_PER_ROT 1600
#define STEPPER_STEPS_PER_MM STEPPER_STEPS_PER_ROT/4
#define D_CABLE 6 #define D_CABLE 6
#define D_REEL 160 //actual 170 #define D_REEL 160 //actual 170
#define PI 3.14159 #define PI 3.14159
//simulate encoder with reset button to test stepper ctl task
//note STEPPER_TEST has to be defined as well
#define STEPPER_SIMULATE_ENCODER
//---------------------- //----------------------
//----- variables ------ //----- variables ------
//---------------------- //----------------------
static const char *TAG = "stepper"; //tag for logging static const char *TAG = "stepper-ctrl"; //tag for logging
static bool stepp_direction = true; static bool stepp_direction = true;
static uint32_t posNow = 0; static uint32_t posNow = 0;
@ -56,97 +52,83 @@ static uint32_t posNow = 0;
//---------------------- //----------------------
//----- functions ------ //----- functions ------
//---------------------- //----------------------
////move axis certain Steps (relative) between left and right or reverse when negative //move axis certain Steps (relative) between left and right or reverse when negative
//void travelSteps(int stepsTarget){ void travelSteps(int stepsTarget){
// //posNow = step.getPositionMm(); //not otherwise controlled, so no update necessary //TODO simplify this function, one simple calculation of new position?
// int stepsToGo, remaining; //with new custom driver no need to detect direction change
//
// stepsToGo = abs(stepsTarget); int stepsToGo, remaining;
// if(stepsTarget < 0) stepp_direction = !stepp_direction; //invert direction in reverse mode
// stepsToGo = abs(stepsTarget);
// while (stepsToGo != 0){ if(stepsTarget < 0) stepp_direction = !stepp_direction; //invert direction in reverse mode
// //--- currently moving right ---
// if (stepp_direction == true){ //currently moving right while (stepsToGo != 0){
// remaining = POS_MAX_STEPS - posNow; //calc remaining distance fom current position to limit //--- currently moving right ---
// if (stepsToGo > remaining){ //new distance will exceed limit if (stepp_direction == true){ //currently moving right
// //....step.runAbs (POS_MAX_STEPS); //move to limit remaining = POS_MAX_STEPS - posNow; //calc remaining distance fom current position to limit
// //....while(step.getState() != 1) vTaskDelay(1); //wait for move to finish if (stepsToGo > remaining){ //new distance will exceed limit
// posNow = POS_MAX_STEPS; stepper_setTargetPosSteps(POS_MAX_STEPS); //move to limit
// stepp_direction = false; //change current direction for next iteration posNow = POS_MAX_STEPS;
// stepsToGo = stepsToGo - remaining; //decrease target length by already traveled distance stepp_direction = false; //change current direction for next iteration
// ESP_LOGI(TAG, " --- moved to max -> change direction (L) --- \n "); stepsToGo = stepsToGo - remaining; //decrease target length by already traveled distance
// } ESP_LOGI(TAG, " --- moved to max -> change direction (L) --- \n ");
// else { //target distance does not reach the limit }
// //....step.runAbs (posNow + stepsToGo); //move by (remaining) distance to reach target length else { //target distance does not reach the limit
// //....while(step.getState() != 1) vTaskDelay(1); //wait for move to finish stepper_setTargetPosSteps(posNow + stepsToGo); //move by (remaining) distance to reach target length
// ESP_LOGD(TAG, "moving to %d\n", posNow+stepsToGo); ESP_LOGD(TAG, "moving to %d\n", posNow+stepsToGo);
// posNow += stepsToGo; posNow += stepsToGo;
// stepsToGo = 0; //finished, reset target length (could as well exit loop/break) stepsToGo = 0; //finished, reset target length (could as well exit loop/break)
// } }
// } }
//
// //--- currently moving left --- //--- currently moving left ---
// else { else {
// remaining = posNow - POS_MIN_STEPS; remaining = posNow - POS_MIN_STEPS;
// if (stepsToGo > remaining){ if (stepsToGo > remaining){
// //....step.runAbs (POS_MIN_STEPS); stepper_setTargetPosSteps(POS_MIN_STEPS);
// //....while(step.getState() != 1) vTaskDelay(2); //wait for move to finish posNow = POS_MIN_STEPS;
// posNow = POS_MIN_STEPS; stepp_direction = true;
// stepp_direction = true; stepsToGo = stepsToGo - remaining;
// stepsToGo = stepsToGo - remaining; ESP_LOGI(TAG, " --- moved to min -> change direction (R) --- \n ");
// ESP_LOGI(TAG, " --- moved to min -> change direction (R) --- \n "); }
// } else {
// else { stepper_setTargetPosSteps(posNow - stepsToGo); //when moving left the coordinate has to be decreased
// //....step.runAbs (posNow - stepsToGo); //when moving left the coordinate has to be decreased ESP_LOGD(TAG, "moving to %d\n", posNow - stepsToGo);
// while(step.getState() != 1) vTaskDelay(2); //wait for move to finish posNow -= stepsToGo;
// ESP_LOGD(TAG, "moving to %d\n", posNow - stepsToGo); stepsToGo = 0;
// posNow -= stepsToGo; }
// stepsToGo = 0; }
// } }
// } if(stepsTarget < 0) stepp_direction = !stepp_direction; //undo inversion of stepp_direction after reverse mode is finished
// } return;
// if(stepsTarget < 0) stepp_direction = !stepp_direction; //undo inversion of stepp_direction after reverse mode is finished }
// return;
//}
// //move axis certain Mm (relative) between left and right or reverse when negative
// void travelMm(int length){
////move axis certain Mm (relative) between left and right or reverse when negative travelSteps(length * STEPPER_STEPS_PER_MM);
//void travelMm(int length){ }
// travelSteps(length * STEPPER_STEPS_PER_MM);
//}
// //define zero/start position
// //currently crashes into hardware limitation for certain time
////define zero/start position //TODO: limit switch
////currently crashes into hardware limitation for certain time void home() {
////TODO: limit switch //TODO add function for this to stepper driver
//void home() { ESP_LOGW(TAG, "auto-home...");
// ESP_LOGW(TAG, "auto-home..."); //....step.setSpeedMm(100, 500, 10);
// //....step.setSpeedMm(100, 500, 10); //....step.runInf(1);
// //....step.runInf(1); vTaskDelay(1500 / portTICK_PERIOD_MS);
// vTaskDelay(1500 / portTICK_PERIOD_MS); //....step.stop();
// //....step.stop(); //....step.resetAbsolute();
// //....step.resetAbsolute(); ESP_LOGW(TAG, "auto-home finished");
// ESP_LOGW(TAG, "auto-home finished"); }
//}
//initialize/configure stepper instance //initialize/configure stepper instance
void init_stepper() { void init_stepper() {
// ESP_LOGW(TAG, "initializing stepper..."); //TODO unnecessary wrapper?
// DendoStepper_config_t step_cfg = { ESP_LOGW(TAG, "initializing stepper...");
// .stepPin = STEPPER_STEP_PIN,
// .dirPin = STEPPER_DIR_PIN,
// .enPin = STEPPER_EN_PIN,
// .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(STEPPER_STEPS_PER_MM); //guide: 4mm/rot
stepper_init(); stepper_init();
} }
@ -156,7 +138,7 @@ void updateSpeedFromAdc() {
int potiRead = gpio_readAdc(ADC_CHANNEL_POTI); //0-4095 GPIO34 int potiRead = gpio_readAdc(ADC_CHANNEL_POTI); //0-4095 GPIO34
double poti = potiRead/4095.0; double poti = potiRead/4095.0;
int speed = poti*(SPEED_MAX-SPEED_MIN) + SPEED_MIN; int speed = poti*(SPEED_MAX-SPEED_MIN) + SPEED_MIN;
//....step.setSpeedMm(speed, ACCEL_MS, DECEL_MS); stepper_setSpeed(speed);
ESP_LOGW(TAG, "poti: %d (%.2lf%%), set speed to: %d", potiRead, poti*100, speed); ESP_LOGW(TAG, "poti: %d (%.2lf%%), set speed to: %d", potiRead, poti*100, speed);
} }
@ -165,6 +147,7 @@ void updateSpeedFromAdc() {
//---------------------------- //----------------------------
//---- TASK stepper-test ----- //---- TASK stepper-test -----
//---------------------------- //----------------------------
#ifndef STEPPER_SIMULATE_ENCODER
void task_stepper_test(void *pvParameter) void task_stepper_test(void *pvParameter)
{ {
stepper_init(); stepper_init();
@ -222,66 +205,74 @@ void task_stepper_test(void *pvParameter)
// stepperSw_setTargetSteps(30000); // stepperSw_setTargetSteps(30000);
// } // }
} }
#endif
//---------------------------- //----------------------------
//----- TASK stepper-ctl ----- //----- TASK stepper-ctl -----
//---------------------------- //----------------------------
#ifdef STEPPER_SIMULATE_ENCODER
void task_stepper_test(void *pvParameter)
#else
void task_stepper_ctl(void *pvParameter) void task_stepper_ctl(void *pvParameter)
#endif
{ {
// //variables //variables
// int encStepsNow = 0; //get curret steps of encoder int encStepsNow = 0; //get curret steps of encoder
// int encStepsPrev = 0; //steps at last check int encStepsPrev = 0; //steps at last check
// int encStepsDelta = 0; //steps changed since last iteration int encStepsDelta = 0; //steps changed since last iteration
//
// double cableLen = 0; double cableLen = 0;
// double travelStepsExact = 0; //steps axis has to travel double travelStepsExact = 0; //steps axis has to travel
// double travelStepsPartial = 0; double travelStepsPartial = 0;
// int travelStepsFull = 0; int travelStepsFull = 0;
// double travelMm = 0; double travelMm = 0;
// double turns = 0; double turns = 0;
//
// float potiModifier; float potiModifier;
//
// init_stepper(); init_stepper();
// home(); home();
//
// while(1){ while(1){
// //get current length #ifdef STEPPER_SIMULATE_ENCODER
// encStepsNow = encoder_getSteps(); //TESTING - simulate encoder using switch
// SW_RESET.handle();
// //calculate change //note
// encStepsDelta = encStepsNow - encStepsPrev; //FIXME MAJOR BUG: when resetting encoder/length in control task, diff will be huge! if (SW_RESET.risingEdge) encStepsNow += 500;
// #else
// //read potentiometer and normalize (0-1) to get a variable for testing //get current length
// potiModifier = (float) gpio_readAdc(ADC_CHANNEL_POTI) / 4095; //0-4095 -> 0-1 encStepsNow = encoder_getSteps();
// //ESP_LOGI(TAG, "current poti-modifier = %f", potiModifier); #endif
//
// //calculate steps to move //calculate change
// cableLen = (double)encStepsDelta * 1000 / ENCODER_STEPS_PER_METER; encStepsDelta = encStepsNow - encStepsPrev; //FIXME MAJOR BUG: when resetting encoder/length in control task, diff will be huge!
// turns = cableLen / (PI * D_REEL);
// travelMm = turns * D_CABLE; //read potentiometer and normalize (0-1) to get a variable for testing
// travelStepsExact = travelMm * STEPPER_STEPS_PER_MM + travelStepsPartial; //convert mm to steps and add not moved partial steps potiModifier = (float) gpio_readAdc(ADC_CHANNEL_POTI) / 4095; //0-4095 -> 0-1
// travelStepsPartial = 0; //ESP_LOGI(TAG, "current poti-modifier = %f", potiModifier);
// travelStepsFull = (int)travelStepsExact;
// //calculate steps to move
// //move axis when ready to move at least 1 step cableLen = (double)encStepsDelta * 1000 / ENCODER_STEPS_PER_METER;
// if (abs(travelStepsFull) > 1){ turns = cableLen / (PI * D_REEL);
// travelStepsPartial = fmod(travelStepsExact, 1); //save remaining partial steps to be added in the next iteration travelMm = turns * D_CABLE;
// ESP_LOGD(TAG, "cablelen=%.2lf, turns=%.2lf, travelMm=%.3lf, travelStepsExact: %.3lf, travelStepsFull=%d, partialStep=%.3lf", cableLen, turns, travelMm, travelStepsExact, travelStepsFull, travelStepsPartial); travelStepsExact = travelMm * STEPPER_STEPS_PER_MM + travelStepsPartial; //convert mm to steps and add not moved partial steps
// ESP_LOGI(TAG, "MOVING %d steps", travelStepsFull); travelStepsPartial = 0;
// //TODO: calculate variable speed for smoother movement? for example intentionally lag behind and calculate speed according to buffered data travelStepsFull = (int)travelStepsExact;
// //....step.setSpeedMm(35, 100, 50);
// //testing: get speed from poti //move axis when ready to move at least 1 step
// //step.setSpeedMm(35, 1000*potiModifier+1, 1000*potiModifier+1); if (abs(travelStepsFull) > 1){
// travelSteps(travelStepsExact); travelStepsPartial = fmod(travelStepsExact, 1); //save remaining partial steps to be added in the next iteration
// encStepsPrev = encStepsNow; //update previous length ESP_LOGD(TAG, "cablelen=%.2lf, turns=%.2lf, travelMm=%.3lf, travelStepsExact: %.3lf, travelStepsFull=%d, partialStep=%.3lf", cableLen, turns, travelMm, travelStepsExact, travelStepsFull, travelStepsPartial);
// } ESP_LOGI(TAG, "MOVING %d steps", travelStepsFull);
// else { travelSteps(travelStepsExact);
// //TODO use encoder queue to only run this check at encoder event? encStepsPrev = encStepsNow; //update previous length
// vTaskDelay(2); }
// } else {
// } //TODO use encoder queue to only run this check at encoder event?
vTaskDelay(2);
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
} }

View File

@ -10,16 +10,19 @@ extern "C" {
#include "esp_log.h" #include "esp_log.h"
} }
//config from config.hpp
//=====================
//=== configuration ===
//=====================
//used macros from config.hpp:
//#define STEPPER_STEP_PIN GPIO_NUM_18 //mos1 //#define STEPPER_STEP_PIN GPIO_NUM_18 //mos1
//#define STEPPER_DIR_PIN GPIO_NUM_16 //ST3 //#define STEPPER_DIR_PIN GPIO_NUM_16 //ST3
#define STEPPER_STEPS_PER_MM 200/2 //steps/mm //#define STEPPER_STEPS_PER_MM 200/2 //steps/mm (steps-per-rot / slope)
#define STEPPER_SPEED_DEFAULT 20 //mm/s //#define STEPPER_SPEED_DEFAULT 20 //mm/s
#define STEPPER_SPEED_MIN 4 //mm/s - speed at which stepper immediately starts/stops //#define STEPPER_SPEED_MIN 4 //mm/s - speed threshold at which stepper immediately starts/stops
#define STEPPER_ACCEL_INC 3 //steps/s per cycle //#define STEPPER_ACCEL_INC 3 //steps/s increment per cycle
#define STEPPER_DECEL_INC 8 //steps/s per cycle //#define STEPPER_DECEL_INC 8 //steps/s decrement per cycle
#define TIMER_F 1000000ULL #define TIMER_F 1000000ULL
#define TICK_PER_S TIMER_S #define TICK_PER_S TIMER_S
@ -30,7 +33,7 @@ extern "C" {
//======================== //========================
//=== global variables === //=== global variables ===
//======================== //========================
static const char *TAG = "stepper-ctl"; //tag for logging static const char *TAG = "stepper-driver"; //tag for logging
bool direction = 1; bool direction = 1;
bool directionTarget = 1; bool directionTarget = 1;