Society of Robots  Robot Forum
Software => Software => Topic started by: fangwei on May 10, 2007, 08:38:10 AM

Hi!
I want to control the speed of a motor with a microcontroller (ATMEL 128) and need to see the number of counts according to rpm, the thing is that it should not be difficult but I cannot think straight how to fix the number of counts.
DC motor vendor details:
2 signal outputs in quadrature
3 pulses / revolution
360 counts / revolution
default speed: 170 rpm
I checked also that the duty cycle at this speed was 50 %
now, I should calculate the number of counts for different speeds (from 1,5 rpm till 200 rpm) ....
I will generate a PWM signal from a f cpuclock = 8 MHz and scaled ...
Has anybody any idea? :( It shouldn´t be difficult !!
Greetings
fangwei

to start off, i recommend ignoring the fact that it is a quadrature and just use one encoder until your code works . . .
now to calculate speed . . . read the first half of this page to understand the basic concepts:
http://www.societyofrobots.com/mechanics_dynamics.shtml
you need to include the diameter of the wheel in your math . . .
wheel circumference / counts per revolution = distance traveled per encoder count
now velocity is distance divided by time . . .
velocity = (wheel circumference / counts per revolution) / time
as for generating PWM, you will have to run a PID control system (http://www.societyofrobots.com/programming_PID.shtml) and do lots of tweaking until it works . . .

Thanks!!
I already got the encoder counts per revolution: 360 x 4 = 1440 counts / rev if velocity is 170 rpm
it results then a 170 rev/min x 1 / 60 min/s x 1440 counts / rev = 4,079.5 counts /s (so, frequency of 4.08 KHz )
I am not sure if I am right, but I proceed in the same way to calculate the counts / rev for different speeds (assuming the same frequency of 4.08 KHz)
The thing is that, looking at the datasheet of the microcontroller, the PWM frequency that I can generate is 60 Hz
(the formula is f pwm = fclock / 2*N*TOP where fclock = 8 MHz, TOP = 65535 and N = 1 )
I think there is something wrong with this, makes any senses to use 60 Hz frequency???
I just don´t get it :'(
Can anybody help me??
Thanks!

The frequency that your encoder outputs has nothing to do with the PWM frequency you run your motor at, which (in addition) has nothing to do with how fast your motor turns.
PWM frequency is one of those weird things  you need to look at the motor, the hbridge, and figure out what works. If you're running a really high quality motor (like a Maxxon or MicroMo), you can probably run at 20 KHz. For a cheapo plastic gearmotor, you might try 5 KHz.
The thing that sets the motor speed is the duty cycle, which is the percentage of each cycle that the PWM pin stays high.
There is no direct formula between PWM duty cycle and output speed  if there was, we wouldn't need encoders. Write a feedback loop. For simple fixed speed, you can just use a simple feedback loop that compares the computed speed (from the encoder) with the desired speed, and increases or decreases the PWM duty cycle according to how far it is off.
If you want more complex speed control, with less oscillations, look up PID on Google.
The only kind of motor where you can be guaranteed a correspondance between the driving and the actual output speed (without some kind of feedback loop) is a stepper motor.
 Jon

I already got the encoder counts per revolution: 360 x 4 = 1440 counts / rev if velocity is 170 rpm
it results then a 170 rev/min x 1 / 60 min/s x 1440 counts / rev = 4,079.5 counts /s (so, frequency of 4.08 KHz )
first, '170 rpm' is not velocity of your robot, it is rotational velocity of your wheel :P
you MUST factor in the wheel circumference to actually calculate robot velocity
The thing is that, looking at the datasheet of the microcontroller, the PWM frequency that I can generate is 60 Hz
(the formula is f pwm = fclock / 2*N*TOP where fclock = 8 MHz, TOP = 65535 and N = 1 )
I think there is something wrong with this, makes any senses to use 60 Hz frequency???
As for PWM, frequency is not that important. What is important is the wave length:
(http://www.societyofrobots.com/images/PWM.gif)
Agreeing with JonHylands, there is no easy equation for PWM. The best way to implement PWM is just to do it, then tweak it until it works the way you like. You MUST have PID control  that is why you have an encoder, no? :P

I don't know where you got the number 60 Hz, btw, but the formula you posted is not correct.
On page 98 of the ATmega128 data sheet, it gives the formula (for that PWM pair) as Freq / (2 * prescalar * (1 + OCRn))
Since OCRn can be set to zero, and the prescalar can be one, that gives you a maximum frequency of 4 MHz if you're running an 8 MHz clock.
 Jon

Thank u guys!!
Well , the formula comes from the page 127 of ATMEGA 128L because I was thinking about which mode of Timer1 I should use and I read that for motor controlling the one of "Phase and Frequency Correct PWM mode" was recommended.
That´s why I disccard the one you have written down... ???