Fix isr-function (fully functional now), Add task for debug output

new driver works well while testing with one button with debug output
- small changes to isr function that made it fully functional!
- add stepper debug task
- minor optimizations necessary
This commit is contained in:
jonny_ji7 2023-04-25 14:35:07 +02:00
parent c99e71846c
commit 63f0da25f1
4 changed files with 56 additions and 15 deletions

View File

@ -183,16 +183,24 @@ void task_stepper_test(void *pvParameter)
SW_CUT.handle(); SW_CUT.handle();
SW_AUTO_CUT.handle(); SW_AUTO_CUT.handle();
//cycle through test commands with one button
if (SW_RESET.risingEdge) { if (SW_RESET.risingEdge) {
//buzzer.beep(1, 1000, 100); switch (state){
if (state) { case 0:
stepper_setTargetSteps(1000); stepper_setTargetSteps(1000);
state = 0; state++;
} else { break;
stepper_setTargetSteps(600); case 1:
state = 1; stepper_setTargetSteps(100);
state++;
break;
case 2:
stepper_setTargetSteps(2000);
state = 0;
break;
} }
} }
}
// if (SW_PRESET1.risingEdge) { // if (SW_PRESET1.risingEdge) {
// buzzer.beep(2, 300, 100); // buzzer.beep(2, 300, 100);
// stepperSw_setTargetSteps(1000); // stepperSw_setTargetSteps(1000);
@ -205,7 +213,6 @@ void task_stepper_test(void *pvParameter)
// buzzer.beep(1, 100, 100); // buzzer.beep(1, 100, 100);
// stepperSw_setTargetSteps(30000); // stepperSw_setTargetSteps(30000);
// } // }
}
} }

View File

@ -96,6 +96,7 @@ extern "C" void app_main()
//create task for stepper testing //create task for stepper testing
//xTaskCreate(task_stepperCtrlSw, "task stepper control software", configMINIMAL_STACK_SIZE * 3, NULL, configMAX_PRIORITIES - 1, NULL); //xTaskCreate(task_stepperCtrlSw, "task stepper control software", configMINIMAL_STACK_SIZE * 3, NULL, configMAX_PRIORITIES - 1, NULL);
xTaskCreate(task_stepper_test, "task_stepper_test", configMINIMAL_STACK_SIZE * 3, NULL, 2, NULL); xTaskCreate(task_stepper_test, "task_stepper_test", configMINIMAL_STACK_SIZE * 3, NULL, 2, NULL);
xTaskCreate(task_stepper_debug, "task_stepper_test", configMINIMAL_STACK_SIZE * 3, NULL, 2, NULL);
#else #else
//create task for controlling the machine //create task for controlling the machine
xTaskCreate(task_control, "task_control", configMINIMAL_STACK_SIZE * 3, NULL, 4, NULL); xTaskCreate(task_control, "task_control", configMINIMAL_STACK_SIZE * 3, NULL, 4, NULL);

View File

@ -26,13 +26,20 @@ extern "C" {
static const char *TAG = "stepper-ctl"; //tag for logging static const char *TAG = "stepper-ctl"; //tag for logging
bool direction = 1; bool direction = 1;
bool directionTarget = 1;
bool timerIsRunning = false; bool timerIsRunning = false;
bool timer_isr(void *arg); bool timer_isr(void *arg);
static timer_group_t timerGroup = TIMER_GROUP_0; static timer_group_t timerGroup = TIMER_GROUP_0;
static timer_idx_t timerIdx = TIMER_0; static timer_idx_t timerIdx = TIMER_0;
//move to isr
static uint64_t posTarget = 0; static uint64_t posTarget = 0;
static uint64_t posNow = 0; static uint64_t posNow = 0;
static uint64_t stepsToGo = 0;
static uint32_t speedMin = 20000;
static uint32_t speedNow = speedMin;
static int debug = 0;
@ -88,6 +95,32 @@ void stepperSw_setTargetSteps(uint64_t target){
void task_stepper_debug(void *pvParameter){
while (1){
ESP_LOGI("stepper-DEBUG",
"timer=%d "
"dir=%d "
"dirTarget=%d "
"posTarget=%llu "
"posNow=%llu "
"stepsToGo=%llu "
"speedNow=%u "
"debug=%d ",
timerIsRunning,
direction,
directionTarget,
posTarget,
posNow,
stepsToGo,
speedNow,
debug
);
vTaskDelay(300 / portTICK_PERIOD_MS);
}
}
@ -158,20 +191,16 @@ bool timer_isr(void *arg) {
//--- variables --- //--- variables ---
static uint32_t speedTarget = 100000; static uint32_t speedTarget = 100000;
static uint32_t speedMin = 20000;
//FIXME increment actually has to be re-calculated every run to have linear accel (because also gets called faster/slower) //FIXME increment actually has to be re-calculated every run to have linear accel (because also gets called faster/slower)
static uint32_t decel_increment = 200; static uint32_t decel_increment = 200;
static uint32_t accel_increment = 150; static uint32_t accel_increment = 150;
static uint64_t stepsToGo = 0;
static uint32_t speedNow = speedMin;
//--- define direction, stepsToGo --- //--- define direction, stepsToGo ---
//int64_t delta = (int)posTarget - (int)posNow; //int64_t delta = (int)posTarget - (int)posNow;
//bool directionTarget = delta >= 0 ? 1 : 0; //bool directionTarget = delta >= 0 ? 1 : 0;
bool directionTarget; if (posTarget >= posNow) {
if (posTarget > posNow) {
directionTarget = 1; directionTarget = 1;
} else { } else {
directionTarget = 0; directionTarget = 0;
@ -179,7 +208,7 @@ bool timer_isr(void *arg) {
//directionTarget = 1; //directionTarget = 1;
//direction = 1; //direction = 1;
//gpio_set_level(STEPPER_DIR_PIN, direction); //gpio_set_level(STEPPER_DIR_PIN, direction);
if (direction != directionTarget) { if ( (direction != directionTarget) && (posTarget != posNow)) {
//ESP_LOGW(TAG, "direction differs! new: %d", direction); //ESP_LOGW(TAG, "direction differs! new: %d", direction);
if (stepsToGo == 0){ if (stepsToGo == 0){
direction = directionTarget; //switch direction if almost idle direction = directionTarget; //switch direction if almost idle
@ -188,7 +217,8 @@ bool timer_isr(void *arg) {
stepsToGo = 2; stepsToGo = 2;
} else { } else {
//stepsToGo = speedNow / decel_increment; //set steps to decel to min speed //stepsToGo = speedNow / decel_increment; //set steps to decel to min speed
stepsToGo = speedNow / decel_increment; //set to minimun decel steps
stepsToGo = (speedNow - speedMin) / decel_increment;
} }
} else if (direction == 1) { } else if (direction == 1) {
stepsToGo = posTarget - posNow; stepsToGo = posTarget - posNow;

View File

@ -4,6 +4,9 @@
void stepper_init(); void stepper_init();
void stepper_setTargetSteps(int steps); void stepper_setTargetSteps(int steps);
//task that periodically logs variables for debugging stepper driver
void task_stepper_debug(void *pvParameter);
//control stepper without timer (software) //control stepper without timer (software)
void task_stepperCtrlSw(void *pvParameter); void task_stepperCtrlSw(void *pvParameter);
void stepperSw_setTargetSteps(uint64_t target); void stepperSw_setTargetSteps(uint64_t target);