using servo motor with rtos

Go To Last Post
16 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

can i use timer 1 in atmega32 while i'm using freertos  ? becuase the freertos kernal depend on the 16-bit timer and i need the 16-bit timer to set the frequency for the servo motor  ?

 

if i can't , is there any other solution to set the right frequency for the servomotor maby by timer0 or by delay  ?

 

Thanks in advance :)

This topic has a solution.
Last Edited: Mon. Mar 12, 2018 - 01:30 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

One could question the use of freertos on a small micro. Anyway, why can’t you use an 8 bit timer for the servo? All you need is to output a variable pulse 50 times a second. Or you could change the timer for freertos to an 8 bit timer - its probably only set up for 10ms ticks or just don’t use freertos.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The problem with 8 bit (0 .. 256) is that in 20ms the active 1 .. 2ms is just 13 discrete steps.

 

I wonder what the timer granularity in FreeRTOS is?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You really only need the timer to generate the pulse. The repetition can be done by counting overflows, or putting the task to sleep for the required time.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

how to get 20 msec pulse from the timer 0 , i tried but it didn't work.

 

and i will try to search more on changing the timer for freertos , but really thanks 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 i tried to start the tcnt0 from 100 to overflow at 20ms but it didn't work 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

can you explain more please ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

karim_92 wrote:

how to get 20 msec pulse from the timer 0 , i tried but it didn't work.

 

You can't if your AVR is clocked at 16MHz, but does it matter? It's only going to be used for timing tasks asleep. If it does matter then use a 10ms timer and just fire the RTOS routines on every other ISR.

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

karim_92 wrote:
i tried but it didn't work

So what, exactly,  did you try?

 

And what, exactly, was the result?

 

You didn't answer:

 

clawson wrote:
I wonder what the timer granularity in FreeRTOS is?

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:

karim_92 wrote:

how to get 20 msec pulse from the timer 0 , i tried but it didn't work.

 

You can't if your AVR is clocked at 16MHz, but does it matter? It's only going to be used for timing tasks asleep. If it does matter then use a 10ms timer and just fire the RTOS routines on every other ISR.

 

 

my AVR is clocked at 8MHZ , 

 

"If it does matter then use a 10ms timer and just fire the RTOS routines on every other ISR." i can't understand this . 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

int main()

{

DDRB|=(1<<3);

TCCR0=(1<<WGM01)|(1<<WGM00)|(1<<COM01)|(1<<CS00)|(1<<CS02); //fast mode - 1024 perscaler - non inverting mode

unsigned char i=0;

 

while(1)

{

for(i=5;i>0;i--)

{

TCCR0=(1<<WGM01)|(1<<WGM00)|(1<<COM01)|(1<<CS00)|(1<<CS02);

TCNT0=100;

OCR0=111;

while(!(TIFR&(1<<TOV0)));

TCCR0=0;

}

 

_delay_ms(1000);

for(i=5;i>0;i--)

{

TCCR0=(1<<WGM01)|(1<<WGM00)|(1<<COM01)|(1<<CS00)|(1<<CS02);

TCNT0=100;

OCR0=104;

while(!(TIFR&(1<<TOV0)));

TCCR0=0;

}

 

_delay_ms(1000);

 

 

}

return 0;

}

 

the result : the servo motor didn't make the angle i want but moved in steps and didn't return back . 

 

"I wonder what the timer granularity in FreeRTOS is?" i don't know 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

karim_92 wrote:
"I wonder what the timer granularity in FreeRTOS is?" i don't know 

Don't you think that's something you should find out, then?

 

You've chosen to use it - so you should get to know facilities it gives you!

No point re-inventing the wheel if FreeRTOS already does this for you.

 

Please note Tip #1 - how to properly post source code.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

karim_92 wrote:

my AVR is clocked at 8MHZ , 

 

In that case you can do it...

 

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For the servo, the critical time is the 1 to 2ms pulse - this sets the angle, so use the 8 bit timer. The 20ms time is not critical at all - so you can use freertos to create that delay for you.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What CPU are you using?

Have you considered swapping your CPU for one with some more timers?

I've heard rumours that the m328PB has 3 usarts instead of the single usart of it's predesessors.

Similar things probably happened with the timers.

 

I voted 5 stars for Kartman's #2 there hardly ever is a need for a RTOS in small uC systems.

On the contrary even. An RTOS uses interrupts to do the task switching and with single interrupt level uC's such as AVRs this increases interrupt latency by a very significant amount.

 

You are probably a student or beginner with uC's. My suggestion is to first write some "simple" programs. Then extend them with interrupts, an if you are getting comfortable with that, then look into the usability and performance an RTOS can give you.

Paul van der Hoeven.
Bunch of old projects with AVR's:
http://www.hoevendesign.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Paulvdh wrote:
You are probably a student or beginner with uC's. My suggestion is to first write some "simple" programs. Then extend them with interrupts, and if you are getting comfortable with that, then look into the usability and performance an RTOS can give you.

+100

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...