I would like to control a brushless motor with a Java program running on a Raspberry Pi model b+.

This is the code that I am using:

package de.ye.boat_eng;

import com.pi4j.wiringpi.Gpio;
import com.pi4j.wiringpi.SoftPwm;

public class ON implements Runnable{

public static boolean stop = false;

public void run() {
    SoftPwm.softPwmCreate(24, 0, 60);
    SoftPwm.softPwmWrite(24, 12);
        SoftPwm.softPwmWrite(24, 12);
        SoftPwm.softPwmWrite(24, 20);

            SoftPwm.softPwmWrite(24, 0);

But the motor isn't running "stable", it turns on and off sometimes slower sometimes faster...

Thanks in advance.


You are not using SoftPwm.softPwmWrite properly. You almost certainly want a delay between one call and the next.

SoftPwm.softPwmWrite(24, 5) will start to send pulses of I guess length 833 µs (1000000/60 * 5/100). It will send those pulses 60 times a second until told to change. You don't need to call the function 60 times a second.

At the moment you keep changing the pulse width so the motor never gets a chance to arrive and stay at the set throttle.

I would stop doing that. You will wear your motor out much faster than intended. Put a sleep of a tenth of a second or so between each call to give the motor time to reach the target speed.

  • Like that? I changed it in the main comment.... – MrGrimod May 15 '16 at 8:40
  • @YellowDev Possibly. Remember I do not know Pi4J so have no idea if sleep(100) sleeps for a 100 days, hours, minutes, seconds, miiliseconds, or microseconds. Also, I repeat, you will not properly control an ESC with Pi4J software PWM. It is the wrong tool for the job. – joan May 15 '16 at 8:46
  • Ok, what ist the right tool? – MrGrimod May 15 '16 at 8:48
  • @YellowDev I have no idea for Java, and anyhow there may be other undeclared factors to take into consideration. Does Java allow access to the local file system? You can control servoblaster and my pigpio via a pipe (the same as writing to a local file). You can also control my pigpio via its socket interface. – joan May 15 '16 at 8:53
  • Yes java allows the acces to the local file system. But i tried it with servoblaster and it don t work the motors turn shortly on and than they go off..... – MrGrimod May 15 '16 at 8:57

Raspberry Pi should be relatively bad for this task, because raspbian is not a real-time operating system. See this thread, hope it is helpful: Is it possible to run real time software?

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.