0
\$\begingroup\$

I am trying to control this servo from a RaspberryPi.
https://skippy.org.uk/wp-content/uploads/MG996R_Tower-Pro.pdf

I sort of figured out that a servo needs a specific pulse for a specific degree. www.youtube.com/watch?v=ddlDgUymbxc

I am using this https://github.com/hybridgroup/pi-blaster for PWM.
I already confirmed this PWM works as expected by lighting an LED, but no matter what pulse I give to my servo, it just goes wild.
It goes like either keep moving from left to right or shakes at one point continuously.

The youtube's servo moves +/- 90 degree, but my servo does +/- 60 degree. so maybe the pulse I need to give is different from 0.5ms/1.5ms/2.5ms?

Has anybody used this servo with RaspberryPi? What are the pulses to give?

updates

I changed the source code to have 50Hz frequency.

diff --git a/pi-blaster.c b/pi-blaster.c
index 2e15ee1..d7d8af2 100644
--- a/pi-blaster.c
+++ b/pi-blaster.c
@@ -70,8 +70,8 @@ static uint8_t pin2gpio[8];
 // will use too much memory bandwidth.  10us is a good value, though you
 // might be ok setting it as low as 2us.

-#define CYCLE_TIME_US          10000
-#define SAMPLE_US              10
+#define CYCLE_TIME_US          20000
+#define SAMPLE_US              20
 #define NUM_SAMPLES            (CYCLE_TIME_US/SAMPLE_US)
 #define NUM_CBS                        (NUM_SAMPLES*2)

and now pi-blaster shows this.

$ sudo /usr/sbin/pi-blaster
Using hardware:                   PWM
Number of channels:                 9
PWM frequency:                  50 Hz
PWM steps:                       1000
Maximum period (100  %):      20000us
Minimum period (0.100%):         20us

but my servo still goes wild. Since Minimum period (0.100%): 20us, to have 0.5ms, I did this.

0.5ms(500us) / min period(20us) = 25
0.1% * 25 = 2.5% = 0.025

$ echo "17=0.025" > /dev/pi-blaster

It seems it is always going back to 0 degree point whichever the % I give it to.

It goes like this.
For 180 degree, it is 2.5ms. When I give

$ echo "17=0.125" > /dev/pi-blaster

servo goes to the 180 degree point, and it soon goes back to 0 degree point. It goes like this back and forth, back and forth, as long as I give the power to it. same goes to any degree.

why?

\$\endgroup\$
2
\$\begingroup\$

Hobby servos are controlled by a pulse width, but this is not the same kind of pulse width modulation that is done by pi-blaster.

Hobby servos use a periodic pulse at a relatively low rate (50 Hz is typical, but the rate is not critical). Each frame contains a nominal 1.5ms pulse to set the server at its nominal 0° position. Varying that pulse width from 0.5ms to 2.5ms moves the servo over its full mechanical range. If the servo is able to move 90° each way by varying the pulse width by up to 1ms, that is the same as 11.1 µs per degree.

So precise control of that servo requires high resolution control of that pulse, and a relatively long dead time to the next pulse.

PWM as done by pi-blaster is designed to control power by adjusting a pulse from 0% to 100% duty cycle of a 100 Hz clock. To use this to control a hobby servo you need to start by turning the base clock frequency down to something in the 50 Hz ballpark. Some servos may accept control as fast as 100 Hz, but that isn't the usual practice.

Next, you need to limit yourself to the PWM range of 0.025 to 0.125, assuming a 50 Hz PWM clock.

The later limit means that of the default precision of 1000 steps from 0% to 100% duty, you can only use 100. For a 180° motion, that would give you a precision of 1.8° per step.

The better answer is to use software designed to control hobby servos. pi-blaster notes that it was derived from ServoBlaster. Use that. It will be a little trickier to interface with because it is just a kernel driver and doesn't provide the slick ASCII FIFO.

One other caution: don't wire a hobby servo directly to your RPi. The RPi has 3.3V I/O, but the hobby servo will want to be driven at 5V or more. You should use a suitable level shifter to get the right drive voltage, and to introduce something less expensive than your entire RPi to burn out when something goes wrong.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.