Electronic – Servo motor speed control with PIC

picservo

I am controlling a servo motor using the PIC12F675. The servo rotates in opposite directions.

I controlling the servo as follows:

if(GPIO & (1<<3))
        {
                GPIO |=(1<<2);
            __delay_ms(1);
            GPIO &=~(1<<2);

            }
            else
        {
                GPIO |=(1<<2);
            __delay_ms(2);
            GPIO &=~(1<<2);


            }

            __delay_ms(18); 

I would like to control the rate at which the servo reaches the opposite ends. I tried looking for articles related to speed control, I found it to be too complicated to control the rate of sweep. I am looking for suggestions for the same

Best Answer

Servos work by listening for 20ms, then adjusting the position based on the duration of the high pulse.

The high pulse (X below) can be between 1ms and 2ms long, with 1.5ms as the neutral or centre position.

 |------------20ms-----------|
 |-X-|                
  ___                         ___
_|   |_______________________|   |___ ...

Depending on your servo, -90° (left of centre) is usually ~1ms, 90° (right of centre) is ~2ms.

If it's at 0° and you send it a 1ms signal, it will try to turn to it's -90° position as fast as it can. The maximum speed varies from servo to servo (data sheets often specify a 0-60° time), and also depends on the voltage you give it (Hitec HS-422 at 4.8V takes 0.21 sec, at 6V takes 0.16 sec).

If you want to vary the speed (not just as fast as it can), you need to tell it to change it's position gradually.

You can do this by sending a series of position signals (each 20ms long with an X ms high pulse).

If we send 50 pulses, slowly from 1.5ms to 1ms stepping down 0.01ms each time, X will equal:

1.50ms, 1.49ms, 1.48ms, ... , 1.02ms, 1.01ms, 1.00ms

then, assuming that it can turn as rapidly as we want it to, it'll take 50 (number of 20ms signals) x 20 ms (length of pulse) = 1s to get from 0° to -90°.


Send a 20ms pulse with x ms high: (psudocode)

function send_signal (x):
    set output high
    wait x ms
    set output low
    wait (20 - x) ms

Make a servo slowly pan from left (-90°) to right (90°)

Psudocode:

loop x from 1 to 2, step 0.01
    send_signal(x)

Or if you prefer, in C (99):

for (float x = 1; x <= 2; x += 0.01) {
    send_signal(x);
}
Related Topic