Page 1 of 1

freeRTOS microsecond delay

Posted: Fri Sep 23, 2022 1:39 pm
by mehbstnc
Hello,
I am trying the run stepper motor with the tmc2208 driver and ı want to delay in microseconds like 5uS or 10 uS.
When ı create a task using xTaskCreate() function and adding some delay in the task function. I dont get any delay even if I add some different delays. I also used portTICK_RATE_MS but the speed didnt change :| .

What should ı do to adding microsecond delay?

Here is the sample code :

for(int32_t i = stepper1.curr_pos; i<=stepper1.pos;i++)
{
gpio_set_level(STEP_PIN , 0);
ets_delay_us(5);
gpio_set_level(STEP_PIN , 1);
}

It is the just basic movement for stepper motor.

Thanks for helps

Re: freeRTOS microsecond delay

Posted: Fri Sep 23, 2022 5:20 pm
by username
gpio_set_level(STEP_PIN , 0);
ets_delay_us(5);
gpio_set_level(STEP_PIN , 1);
In your loop you have no delay after turning the pin high.

Re: freeRTOS microsecond delay

Posted: Sat Sep 24, 2022 5:44 am
by mehbstnc
The complete code :
void motor_move()
{
while(1)
{
gpio_set_level(EN_PIN, 0);
gpio_set_level(DIR_PIN , 0);
for(int32_t i =stepper1.curr_pos ; i>= stepper1.pos; i--)
{

gpio_set_level(STEP_PIN , 0);
ets_delay_us(5);
gpio_set_level(STEP_PIN , 1);
ets_delay_us(5);
stepper1.curr_pos = i;
printf("current position : %d \n",stepper1.curr_pos);
if(stepper1.curr_pos==stepper1.pos)
{
printf("Reached position : %d \n ",stepper1.curr_pos);
gpio_set_level(EN_PIN, 1)
}
}
}
}

and ı also crate a task in the main using upper function

void app_main()
{
gpio_pad_select_gpio(STEP_PIN);
gpio_pad_select_gpio(DIR_PIN);
gpio_pad_select_gpio(EN_PIN);

gpio_set_direction(STEP_PIN, GPIO_MODE_OUTPUT);
gpio_set_direction(EN_PIN, GPIO_MODE_OUTPUT);
gpio_set_direction(DIR_PIN, GPIO_MODE_OUTPUT);
motor_init();//Inıt function it is ust prints motor initialized
xTaskCreate(motor_move_position, "motor_move_position", 4096, NULL,configMAX_PRIORITIES,&motor_move_handle);
stepper1.pos=1500;
vTaskDelay(35000/portTICK_RATE_MS);
stepper1.pos=0;
}

But when ı change the delay between hıgh and low, step's speed doesnt change and its slow for me.
What sould I do?

Re: freeRTOS microsecond delay

Posted: Mon Sep 26, 2022 12:20 pm
by filo_gr
mehbstnc wrote:
Fri Sep 23, 2022 1:39 pm
Hello,
I am trying the run stepper motor with the tmc2208 driver and ı want to delay in microseconds like 5uS or 10 uS.
When ı create a task using xTaskCreate() function and adding some delay in the task function. I dont get any delay even if I add some different delays. I also used portTICK_RATE_MS but the speed didnt change :| .

What should ı do to adding microsecond delay?

Here is the sample code :

for(int32_t i = stepper1.curr_pos; i<=stepper1.pos;i++)
{
gpio_set_level(STEP_PIN , 0);
ets_delay_us(5);
gpio_set_level(STEP_PIN , 1);
}

It is the just basic movement for stepper motor.

Thanks for helps
It is better to avoid ets_delay_us() and esp_rom_delay_us() (read this https://docs.espressif.com/projects/esp ... table.html). You can use something like this:

Code: Select all

    uint64_t microseconds = esp_timer_get_time();

    if (0 != number_of_microseconds)
    {
        while (((uint64_t) esp_timer_get_time() - microseconds) <=
               number_of_microseconds)
        {
            // Wait
        }
    }
where number_of_microseconds is an uint64_t and it is your delay, in microseconds.
Interrupts could produce wrong timings, it could be useful to disable them until you finish to process the movement.
An alternative could be to use a PWM.

Re: freeRTOS microsecond delay

Posted: Tue Sep 27, 2022 1:55 am
by chegewara
Using ets_delay_us and printf (or any other logs) makes no sense, because those are time consuming.

Re: freeRTOS microsecond delay

Posted: Tue Sep 27, 2022 1:56 am
by ESP_Sprite
filo_gr wrote:
Mon Sep 26, 2022 12:20 pm
It is better to avoid ets_delay_us() and esp_rom_delay_us() (read this https://docs.espressif.com/projects/esp ... table.html). You can use something like this:

Code: Select all

    uint64_t microseconds = esp_timer_get_time();

    if (0 != number_of_microseconds)
    {
        while (((uint64_t) esp_timer_get_time() - microseconds) <=
               number_of_microseconds)
        {
            // Wait
        }
    }
where number_of_microseconds is an uint64_t and it is your delay, in microseconds.
Interrupts could produce wrong timings, it could be useful to disable them until you finish to process the movement.
An alternative could be to use a PWM.

That is actually what the delay_us code does under the hood, so there's no real use in rolling your own.

Re: freeRTOS microsecond delay

Posted: Tue Sep 27, 2022 8:34 am
by filo_gr
ESP_Sprite wrote:
Tue Sep 27, 2022 1:56 am
That is actually what the delay_us code does under the hood, so there's no real use in rolling your own.
Nice, I didn't know it works by using the same structure of code. Thank you for this information :)

Re: freeRTOS microsecond delay

Posted: Sun Oct 02, 2022 2:30 pm
by username
Instead of messing with loops and delays for controlling a servo. You should use ledc or the MCPWM feature.
Are you aware that Espressif has an example for controlling a servo?

https://github.com/espressif/esp-idf/tr ... ol-example