WillemD
Posts: 29
Joined: Sat Jan 31, 2015 11:25 am

servo control with software pwm

Thu Feb 05, 2015 7:26 pm

I am trying to set the position of a small servo and am using the following piece of java, with different values.
Problem is the servo only turns left, anti-clockwise, so seems to get a value below 1.5 MS while I am trying different values from 0.5 MS to 2.5 MS (2 MS and 500000 NS). I have used frequency 100HZ and 50HZ, so cycle of 10MS or 20MS, this makes no difference.

When I try the same with a python test script the servo works without problems.
Any advice?

while (!stopPWM) {
try {
setValue(true);
Thread.sleep(pulsOnLengthMS,pulsOnLengthNS);
setValue(false);
Thread.sleep(pulsOffLengthMS,pulsOffLengthNS);
} catch (IOException | InterruptedException e) {
System.out.println(e.getMessage());
}
}

User avatar
mikronauts
Posts: 2804
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: servo control with software pwm

Thu Feb 05, 2015 7:52 pm

That won't work.

You cannot use Java to reliably generate pulses of the required 500us-2500us duration directly with code like that.

You can either hook up an external servo controller (I2C, serial) or see if anyone has ported joan's pigpio or servoblaster to java.

If not, pigpio has a servo daemon, and you could write a java interface for talking to it.
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

WillemD
Posts: 29
Joined: Sat Jan 31, 2015 11:25 am

Re: servo control with software pwm

Thu Feb 05, 2015 8:11 pm

Is it possible to do it with PI4J which uses the PWM implementation of WiringPi? Has anyone tried this with servo's?
Or will this also only work as PWM driver for DC motors and not for servo's ?

Also looking into piblaster. I probably will give that a try first.

Thanks

WillemD
Posts: 29
Joined: Sat Jan 31, 2015 11:25 am

Re: servo control with software pwm

Thu Feb 05, 2015 9:48 pm

I now discovered that the python test script that I was using from the motor board manufacturer was already using servoblaster. So that's apparently the way to go. servoblaster or piblaster is the next step.

User avatar
mikronauts
Posts: 2804
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: servo control with software pwm

Thu Feb 05, 2015 10:20 pm

Glad to hear servoblaster works for you.
WillemD wrote:I now discovered that the python test script that I was using from the motor board manufacturer was already using servoblaster. So that's apparently the way to go. servoblaster or piblaster is the next step.
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

WillemD
Posts: 29
Joined: Sat Jan 31, 2015 11:25 am

Re: servo control with software pwm

Sat Feb 07, 2015 1:09 pm

OK, next step.
I am using the following code in JAVA ME 8 to open /dev/servoblaster so I can then send values to it to drive the servos:

Path servoPath = FileSystems.getDefault().getPath("/dev", "servoblaster");
System.out.println("path defined "+servoPath.toString());
outStream = Files.newOutputStream(servoPath,WRITE);
System.out.println("outstream created");

It does the first system printout but never reaches the second printout, so the 3rd line with opening of the outputstream never returns.
I have started servoblaster (servod) as sudo and I am running this via netbeans to the PI with the write fileiopermission as sudo.

What am I doing wrong? Why does it not return?

Thanks,

Willem

update: I have also tried an alternative method in JAVA ME 8 with the following code:
connection = (FileConnection) Connector.open("file:///rootfs/dev/servoblaster", Connector.WRITE);
System.out.println("connection opened");
fileWriter = new PrintStream(connection.openOutputStream());
System.out.println("filewriter started");
this also hangs on the 3rd line, i.e. the opening of the outputstream.

WillemD
Posts: 29
Joined: Sat Jan 31, 2015 11:25 am

Re: servo control with software pwm

Sun Feb 08, 2015 2:28 pm

Some more info, with a proper exception handling I now get the following error reporting in the output window:
java.security.AccessControlException:
- com/oracle/meep/security/AccessControllerInternal.checkPermission(), bci=118
- java/security/AccessController.checkPermission(), bci=1
- java/lang/SecurityManager.checkPermission(), bci=5
- java/lang/SecurityManager.checkWrite(), bci=12
- com/oracle/cldc/file/FileSystemImpl.checkAccess(), bci=40
- com/oracle/cldc/file/FileChannelImpl.<init>(), bci=184
- java/nio/channels/FileChannel.open(), bci=7
- java/nio/channels/FileChannel.open(), bci=23
- java/nio/file/Files.newByteChannel(), bci=3
- java/nio/file/Files.newOutputStream(), bci=100
etc. etc

On the raspberry Pi I see:
[CRITICAL] [SECURITY] iso=2:Permission check failed: java.io.FilePermission "/dev/servoblaster" "write"
TRACE: <at java.security.AccessControlException: >, startApp threw an Exception

I don't know which permissions are missing. I added the
java.io.FilePermission "/*" "write"
and the
javax.microedition.io.Connector.file.write
javax.microedition.io.Connector.file.read
permissions.

ls -l /dev/servoblaster shows:
prw-rw-rw- 1 root root 0 Feb 8 16:55 /dev/servoblaster

WillemD
Posts: 29
Joined: Sat Jan 31, 2015 11:25 am

Re: servo control with software pwm

Sun Feb 08, 2015 4:28 pm

more testing: it works on a regular file I create in the root directory, whether i create it beforehand or not,
so apparently the problem is related to the fact this is not a regular file but a pipe file ?
Any way around it in JAVA ME? Or only SE?

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

Re: servo control with software pwm

Sun Feb 08, 2015 4:33 pm

It might help if you post the file permissions, e.g. ls -l /dev/servoblaster

WillemD
Posts: 29
Joined: Sat Jan 31, 2015 11:25 am

Re: servo control with software pwm

Sun Feb 08, 2015 6:16 pm

joan wrote:It might help if you post the file permissions, e.g. ls -l /dev/servoblaster
see bottom of my previous post, here it is again

ls -l /dev/servoblaster shows:
prw-rw-rw- 1 root root 0 Feb 8 16:55 /dev/servoblaster

Did some further research and conclude that the pipedoutputstream is not supported in JAVA ME8.
Also the runtime exec command to run operating system commands is not supported.
Don't know of another way to send output to /dev/servoblaster, so I think that means the end of JAVA ME8 for me....

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

Re: servo control with software pwm

Sun Feb 08, 2015 8:36 pm

Sorry to jump in quite late and reply slightly off topic but I was doing some development with servo blaster and pi4j.

You can see it in 'develop' branch of pi4j (you would need to check that branch out and rebuild it on the pi).

pi4j servo blaster impl is here: https://github.com/Pi4J/pi4j/tree/devel ... servo/impl - so if you don't want to go with pi4j there you can see how I did it originally.

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

Re: servo control with software pwm

Sun Feb 08, 2015 8:43 pm

WillemD wrote:
joan wrote:It might help if you post the file permissions, e.g. ls -l /dev/servoblaster
see bottom of my previous post, here it is again

ls -l /dev/servoblaster shows:
prw-rw-rw- 1 root root 0 Feb 8 16:55 /dev/servoblaster

Did some further research and conclude that the pipedoutputstream is not supported in JAVA ME8.
Also the runtime exec command to run operating system commands is not supported.
Don't know of another way to send output to /dev/servoblaster, so I think that means the end of JAVA ME8 for me....
A pipe is just a file like any other file. Surely JAVA can open, read, write, close files?

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: servo control with software pwm

Sun Feb 08, 2015 11:27 pm

joan wrote:A pipe is just a file like any other file. Surely JAVA can open, read, write, close files?
JAVA ME might be more restricted.
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

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

Re: servo control with software pwm

Sun Feb 08, 2015 11:40 pm

-rst- wrote:
joan wrote:A pipe is just a file like any other file. Surely JAVA can open, read, write, close files?
JAVA ME might be more restricted.
Ah, could well be. My knowledge of Java variants is non-existent.


-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: servo control with software pwm

Fri Feb 13, 2015 1:59 pm

Then again I am not quite sure would the JAVA components (FileConnection and OutputStream) really see that it is a pipe in the background - as far as I understand it should look like a regular file and if opened using regular file opening mechanisms... i.e. would it work using just OutputStream and not PipedOutputStream?
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

WillemD
Posts: 29
Joined: Sat Jan 31, 2015 11:25 am

Re: servo control with software pwm

Fri Feb 13, 2015 5:27 pm

I guess there is some kind of difference, why would the different outputstreams mechanisms otherwise exist? I asked the same question on the java me embedded community forum of oracle and also there no explanation or solution is found.
Also note JAVA SE has no problem opening it and that has the full spectrum of outputstream mechanisms.
I cannot think of another reasons since all permission settings are fine, JAVA SE works, Python works, command line echo-ing works, just JAVA ME is failing.

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: servo control with software pwm

Fri Feb 13, 2015 10:19 pm

You use PipedOutputStream to connect to another process/thread connecting through a PipedInputStream. In your case there is no such thing available but a named pipe that looks like a regular file. If you open the file using regular OutputStream, there is nothing on the JAVA side making it - or trying to - into a PipedOutputStream ...which is in fact clear in the stack trace posted above: there is no PipedOutputStream listed.

I am fairly sure there has to be 'just a' permission issue - where the restrictions of ME may play a role.
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

Return to “Java”