0

I have a raspberry pi 4 B and I want to control servo motor. I am using NetBeans IDE and Java programming language with pi4j library for raspberry. Also I have a servo motor that can rotate for 300 degree with Pulse Width Range 500μS ~ 2500μS. I have a class so I can control it by setting values in range 5-25 into SetAngle method. It wokrs fine, but I want to set floating point values to pwm, because pwm value and servo angle ratio is 1:15 so for every one on pwm I get 15 degress on servo motor. It is too big ration, that because I to be able to set floating point values to PWM. I read somewhere about pigpio library but didn't see any exmaples for it on Java. So the question itself - Could I somehow set floating vlaues to PWM using pi4j library or set PWM not in ms but in μs?

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinPwmOutput;
import com.pi4j.io.gpio.Pin;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
/**
 *
 * @author abdra
 */
public class HCStandartServo 
{
    private GpioPinPwmOutput pwm;
    
    public HCStandartServo(Pin p)
    {
        GpioController gpio = GpioFactory.getInstance();
        pwm = gpio.provisionSoftPwmOutputPin(p);
        pwm.setPwmRange(100);
    }
    
    public void SetAngle(float angle)
    {
        pwm.setPwm((int)angle);
    }
}
4
  • Why don't you just read the documentation for setPwm() ? I don't understand what you want to know.
    – joan
    Jun 7, 2021 at 9:25
  • I have checked it and there is only setPwm(int value). And I don't know what should I do then Jun 7, 2021 at 9:36
  • I would look at the implementation of SetPwm() and create a copy called mySetPwm(). I would change that to take a float value and make the needed changes within mySetPwm(). This has little to do with the Pi.
    – joan
    Jun 7, 2021 at 10:35
  • I still do not understand how to do this, when this method is inside GpioPinPwmOutput class and I can't see the code of the method Jun 7, 2021 at 12:53

1 Answer 1

0

After several days of unsuccessful attempts, I still found a great solution to my problem. This solution is the repository: https://github.com/sarfata/pi-blaster. I just installed this on my raspberry pi and wrote a simple program to run my servo motors. At the very beginning of my program, the code is called that launches the pi-blaster via command line:

String tmp;
String s = new String();
Process p;

try {
    p = Runtime.getRuntime().exec("sudo /home/pi/pi-blaster/pi-blaster");
    BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
    while ((tmp = br.readLine()) != null)
        System.out.println("[LINE]: " + tmp);
        s += tmp + "\n";
    p.waitFor();
    System.out.println ("[EXIT]: " + p.exitValue());
    p.destroy();
} catch (Exception e) {}

After this code, I can safely use pinouts of the board to control the servo motors. Like this:

try (PrintWriter out = new PrintWriter(new FileOutputStream("/dev/pi-blaster"), true))
{
    out.println("4=0.2");
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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