Society of Robots - Robot Forum
Software => Software => Topic started by: not_this_punk on June 23, 2009, 06:52:48 PM
-
Hello.
OK, so I need to control all 10 servos simultaneously with an ATmega16. Here's how I do it:
- Clock frequency is 16 MHz.
- I set Timer 1 to the system clock (16 MHz), in Fast PWM TOP=ICR1.
- I want the timer to count each passing of 10 microseconds: 1/16 = 0.0625 us (timer1's period). I want 10 us so 10/0.0625 = 160 periods equal 10 us
- In Timer1's interrupt I increment a variable (every 10 microseconds). When it reaches 2000 I set it back to 0.
// Timer 1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
t++; //increment t every 10 us
if(t==2000) //20 ms have passed
{
t=0; //set t back to 0
per++; //increment per (I need this in my function)
}
}
This is the interrupt of Timer 1: per keeps the number of 20 ms signals.
So basically, I create my own PWM using Timer 1.
In main() I do something like this (but for independent control and speed of 10 servos):
while(1)
{ if(t<150) //150 means 1.5 ms like I said
PORTC.0=1;
else PORTC.0=0;
}
Controlling all 10 servos is just an expansion of that code.
What do you think? Is it OK? I can't use the built-in PWM to control all 10 servos and I don't want to waste precious resources with delays. Is it correct? Are there any better solutions (remember that I have 10 servos to control)?
-
You may find interrupts cause you problems with the UART, if you are using it that is . . . make sure you turn interrupts off when you use it . . .
-
Hello admin, thank you very much for your reply!
Yeah, I never thought about that, but it seems logical now :)
But what do you think about my method? Is there any way to control 10 servos from a single ATmega16 using the built-in PWM? Or...what is the usual way of doing it? I feel like I'm missing something here...I don't want to stress out the MCU for no reason.
So how is it made in servo controllers and MCU's like the Axon? ???
-
There are three main ways to control servos:
- interrupt method
- delay method
- PWM method
Each method has benefits and drawbacks, and even variations in ways to implement them. The problem with the PWM method is your ATmega16 only has 4 PWM, meaning it can only do 4 servos.
My Axon II will have 16 PWM, but will be several months before it comes out (I'm aiming for early October).