freeRTOS microsecond delay

mehbstnc
Posts: 8
Joined: Mon Aug 22, 2022 12:15 pm

freeRTOS microsecond delay

Postby mehbstnc » 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

username
Posts: 534
Joined: Thu May 03, 2018 1:18 pm

Re: freeRTOS microsecond delay

Postby username » Fri Sep 23, 2022 5:20 pm

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.

mehbstnc
Posts: 8
Joined: Mon Aug 22, 2022 12:15 pm

Re: freeRTOS microsecond delay

Postby mehbstnc » Sat Sep 24, 2022 5:44 am

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?

filo_gr
Posts: 110
Joined: Wed Jul 28, 2021 12:25 pm
Location: Italy

Re: freeRTOS microsecond delay

Postby filo_gr » Mon Sep 26, 2022 12:20 pm

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.
Filippo

chegewara
Posts: 2364
Joined: Wed Jun 14, 2017 9:00 pm

Re: freeRTOS microsecond delay

Postby chegewara » Tue Sep 27, 2022 1:55 am

Using ets_delay_us and printf (or any other logs) makes no sense, because those are time consuming.

ESP_Sprite
Posts: 9739
Joined: Thu Nov 26, 2015 4:08 am

Re: freeRTOS microsecond delay

Postby ESP_Sprite » Tue Sep 27, 2022 1:56 am

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.

filo_gr
Posts: 110
Joined: Wed Jul 28, 2021 12:25 pm
Location: Italy

Re: freeRTOS microsecond delay

Postby filo_gr » Tue Sep 27, 2022 8:34 am

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 :)
Filippo

username
Posts: 534
Joined: Thu May 03, 2018 1:18 pm

Re: freeRTOS microsecond delay

Postby username » Sun Oct 02, 2022 2:30 pm

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

Who is online

Users browsing this forum: Majestic-12 [Bot] and 105 guests