mpatronska
Posts: 14
Joined: Sun Jun 07, 2015 7:05 am

Move servo motor to a certain angle

Mon Jun 29, 2015 9:45 am

Hi,

I'm trying to move a servo motor to a certain angle (say 0, 90 and 180 gr).

I'm using SoftPwm.

With this code, the servo moves to the one direction, and then stops - it does not move to the opposite direction, but it makes noise.

Code: Select all

public class Servo {

	public static void main(String[] args) {
		// initialize wiringPi library
        com.pi4j.wiringpi.Gpio.wiringPiSetup();

        // create soft-pwm pins (min=0 ; max=100)
        SoftPwm.softPwmCreate(1, 0, 100);

        while (true) {            
            for (int i = 0; i <= 100; i+=2) {
                SoftPwm.softPwmWrite(1, i);
                try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }

            for (int i = 100; i >= 0; i--) {
                SoftPwm.softPwmWrite(1, i);
                try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        }
	}
}
I want to be able to fix the servo at some position, not to move from one end to the other. When using this code, the servo does not move at all.

Code: Select all

public class Servo {

	public static void main(String[] args) {
		// initialize wiringPi library
        com.pi4j.wiringpi.Gpio.wiringPiSetup();

        // create soft-pwm pins (min=0 ; max=100)
        SoftPwm.softPwmCreate(1, 0, 100);
        SoftPwm.softPwmWrite(1, 20);
        SoftPwm.softPwmWrite(1, 30);
        SoftPwm.softPwmWrite(1, 50);

	}
}
Does someone already get the servo moving to a certain angle?

Thanks.

User avatar
xranby
Posts: 539
Joined: Sat Mar 03, 2012 10:02 pm
Contact: Website

Re: Move servo motor to a certain angle

Mon Jun 29, 2015 10:02 am

mpatronska wrote:Hi,

I'm trying to move a servo motor to a certain angle (say 0, 90 and 180 gr).

I'm using SoftPwm.

With this code, the servo moves to the one direction, and then stops - it does not move to the opposite direction, but it makes noise.
The problem using SoftPwm is that it do not generate exact enough timings for use by servos.
you servo will at best shake a lot. because the SoftPwm cant generate a stable servo signal also makes the servo wear quickly because it will never stop moving.

The raspberry pi only have one pin with a hardware PWM that generate exact enough timings but unfortunally the hardware PWM is used to produce audio.

Richard Hirst came up with a very clever idea to reprogram the DMA controller of the Pi to update several GPIO pins at the same time by routing the Pi's single PWM circuit to update the DMA controller. The idea by richard had since then evolved into the pi-blaster project.
The technique used is extremely efficient: does not use the CPU and gives very stable pulses.
http://www.tbideas.com/blog/2013/02/con ... pberry-pi/
By using servo-blaster or pi-blaster you end up with a new device driver where you can set exact pulse on many GPIO's by simply passing a string to the device file. Its easy to construct the strings from Java and write them to the device file to update the GPIO pulses.

https://github.com/sarfata/pi-blaster/ - use this version of pi-blaster, its awesome! for driving leds and servos
https://github.com/richardghirst/PiBits ... rvoBlaster - Richard Hirst's original servoblaster

Its easy to update up to 8 servos using the pi-blaster or servoblaster kernel module. You simply write to the /dev/pi-blaster or /dev/pi-blaster "text" file from java.
Xerxes Rånby @xranby I once had two, then I gave one away. Now both are in use every day!
twitter.com/xranby

User avatar
joan
Posts: 14993
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Move servo motor to a certain angle

Mon Jun 29, 2015 10:04 am

Servos generally require pulses 50 times per second. Each pulse will typically lie in the range 1000µs to 2000µs. 1000µs being one extreme of rotation, 1500µs being the centre, and 2000µs being the other extreme of rotation.

I'm not sure how your usage of software PWM is generating these pulses.

Have a look at https://github.com/nkolban/jpigpio if you want to use reliable pulses.

User avatar
clicky
Posts: 499
Joined: Thu Oct 25, 2012 7:34 am

Re: Move servo motor to a certain angle

Mon Jun 29, 2015 1:01 pm

As always Xerxes nailed it.

For such usage there is pi4j's adapter for servo blaster interface. You can see example here:

https://github.com/Pi4J/pi4j/blob/maste ... mple.java

You need to run pi-blaster (as a daemon for instance) and then pi4j would utilise it at Java layer.

Return to “Java”