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
freeRTOS microsecond delay
Re: freeRTOS microsecond delay
In your loop you have no delay after turning the pin high.gpio_set_level(STEP_PIN , 0);
ets_delay_us(5);
gpio_set_level(STEP_PIN , 1);
Re: freeRTOS microsecond delay
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?
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
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:mehbstnc wrote: ↑Fri Sep 23, 2022 1:39 pmHello,
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
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
}
}
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
Re: freeRTOS microsecond delay
Using ets_delay_us and printf (or any other logs) makes no sense, because those are time consuming.
-
- Posts: 9730
- Joined: Thu Nov 26, 2015 4:08 am
Re: freeRTOS microsecond delay
filo_gr wrote: ↑Mon Sep 26, 2022 12:20 pmIt 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:where number_of_microseconds is an uint64_t and it is your delay, in microseconds.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 } }
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
Nice, I didn't know it works by using the same structure of code. Thank you for this informationESP_Sprite wrote: ↑Tue Sep 27, 2022 1:56 amThat is actually what the delay_us code does under the hood, so there's no real use in rolling your own.
Filippo
Re: freeRTOS microsecond delay
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
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: No registered users and 115 guests