Stepper: add mutex, logging, delay, less steps

When testing last commit the stepper task crashed almost instantly and
moved randomly. Trying to fix/debug that with those changes
This commit is contained in:
jonny_ji7 2023-03-13 11:12:42 +01:00
parent de42b6252e
commit 45409676a0
3 changed files with 20 additions and 10 deletions

View File

@ -128,10 +128,13 @@ esp_err_t DendoStepper::runPos(int32_t relative)
if (ctrl.dir == newDir){ //current direction is the same
ctrl.statusPrev = ctrl.status; //update previous status
ctrl.status = ctrl.status==COAST ? COAST : ACC; //stay at coast otherwise switch to ACC
xSemaphoreTake(semaphore_isrVariables, portMAX_DELAY);
calc(abs(relative + ctrl.stepsRemaining)); //calculate new velolcity profile for new+remaining steps
ESP_LOGW("DendoStepper", "EXTEND running movement (stepsRemaining: %d + stepsNew: %d)", ctrl.stepsRemaining, relative);
xSemaphoreGive(semaphore_isrVariables);
} else { //direction has changed
//direction change not supported TODO wait for decel finish / queue?
STEP_LOGW("DendoStepper", "DIRECTION HOT-CHANGE NOT SUPPORTED - Finising previous move, this command will be ignored");
STEP_LOGE("DendoStepper", "DIRECTION HOT-CHANGE NOT SUPPORTED - Finising previous move, this command will be ignored");
return ESP_ERR_NOT_SUPPORTED;
}
}
@ -333,14 +336,17 @@ bool DendoStepper::xISR()
ctrl.stepCnt++;
//CUSTOM: track actual precice current position
if (ctrl.dir) {
ctrl.posActual ++;
} else {
ctrl.posActual --;
}
////CUSTOM: track actual precice current position
//if (ctrl.dir) {
// ctrl.posActual ++;
//} else {
// ctrl.posActual --;
//}
//CUSTOM: track remaining steps for eventually resuming
xSemaphoreTake(semaphore_isrVariables, portMAX_DELAY);
ctrl.stepsRemaining = ctrl.stepCnt - ctrl.stepCnt;
xSemaphoreGive(semaphore_isrVariables);
// we are done
if (ctrl.stepsToGo == ctrl.stepCnt && !ctrl.runInfinite)

View File

@ -29,6 +29,7 @@
#include "freertos/task.h"
#include "esp_timer.h"
#include "math.h"
#include "freertos/semphr.h"
//#define STEP_DEBUG
@ -95,7 +96,7 @@ typedef struct
uint32_t accSteps = 0;
uint32_t decSteps = 0;
int32_t stepsRemaining = 0;
uint64_t posActual = 0; //actual current pos incremented at every step
//uint64_t posActual = 0; //actual current pos incremented at every step
uint8_t statusPrev = DISABLED; //FIXME currently unused
uint8_t status = DISABLED;
bool dir = CW;
@ -111,6 +112,7 @@ private:
ctrl_var_t ctrl;
esp_timer_handle_t dyingTimer;
TaskHandle_t enTask;
SemaphoreHandle_t semaphore_isrVariables = NULL;
uint64_t currentPos = 0; // absolute position
bool timerStarted = 0;

View File

@ -34,7 +34,7 @@ extern "C"
#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_ROT 800
#define STEPPER_STEPS_PER_MM STEPPER_STEPS_PER_ROT/4
#define D_CABLE 6
@ -71,7 +71,7 @@ void travelSteps(int stepsTarget){
//--- wait if direction changed ---
if (dirPrev != dir){
ESP_LOGI(TAG, " dir-change detected - waiting for move to finish \n ");
ESP_LOGW(TAG, " dir-change detected - waiting for move to finish \n ");
while(step.getState() != 1) vTaskDelay(1); //wait for move to finish
}
@ -235,6 +235,8 @@ void task_stepper_ctl(void *pvParameter)
//read potentiometer and normalize (0-1) to get a variable for testing
potiModifier = (float) gpio_readAdc(ADC_CHANNEL_POTI) / 4095; //0-4095 -> 0-1
//ESP_LOGI(TAG, "current poti-modifier = %f", potiModifier);
ESP_LOGI(TAG, "delaying stepper-ctl task by %.1f ms (poti value)", 2000 * potiModifier);
vTaskDelay(2000 * potiModifier / portTICK_PERIOD_MS);
//calculate steps to move
cableLen = (double)encStepsDelta * 1000 / ENCODER_STEPS_PER_METER;