AlfredoCJ
Posts: 4
Joined: Tue Apr 25, 2017 4:43 pm

Read a PWM with Java using Pi4j

Mon May 22, 2017 9:45 pm

Hello,

I try to use a sensor color (TCS230/TCS3200) in Java with Pi4j but I have a problem with the method getPwm()
This method returns a int between 0 to 1024 (hardware pwm) or 0 to 100 (software pwm) (http://pi4j.com/apidocs/com/pi4j/io/gpi ... tml#getPwm)
I try to execute this code (it's a summary) the return is -1

Code: Select all

public class TestSensorColor {
	public static void main(String[] args) throws Exception {
		Pin pinOut = RaspiPin.GPIO_24;
		long frequency = 0;
		
		GpioController gpio = GpioFactory.getInstance();
		// input mode for pin
		GpioPinImpl gpioOut = new GpioPinImpl(gpio, new RaspiGpioProvider(), pinOut);
		
		while (true) {
			// read PWM and print
			System.out.println(gpioOut.getPwm());			
			// wait a bit
			Thread.sleep(100);
		}
	}
}
What is bad?

Thanks and bye

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

Re: Read a PWM with Java using Pi4j

Mon May 22, 2017 10:23 pm

Java?

Use C or Python.

ghp
Posts: 1146
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Read a PWM with Java using Pi4j

Tue May 23, 2017 4:45 am

Hello,
these chips provide frequency encoded values. So you need to count pulses in a fixed time interval or measure time between pulses to get the measured value.
Frequencies are in kHz range. This is not a problem for a microcontroller, but difficult for a system running linux.
Perhaps you get faster to results using a chip with SPI or I2C interface as TCS34725.

The getPwm-methods are (as far as I understand this lib) just return the values you have set for a pin to operate as a pwm output. Nothing to measure frequencies.
Hope this helps,
Gerhard

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

Re: Read a PWM with Java using Pi4j

Tue May 23, 2017 6:24 am


mattlewis
Posts: 104
Joined: Sat Jan 12, 2013 3:05 pm
Location: UK
Contact: Website

Re: Read a PWM with Java using Pi4j

Tue May 23, 2017 9:33 pm

Thanks Joan. I don't have one of those devices - have just ordered one off eBay but will take forever to get here from China. I'll have a quick look at your code tomorrow and see if I can easily convert to Java. Always happy to add a new device to diozero.
Matt
http://www.diozero.com/

AlfredoCJ
Posts: 4
Joined: Tue Apr 25, 2017 4:43 pm

Re: Read a PWM with Java using Pi4j

Wed May 24, 2017 3:12 pm

Hi all,
joan wrote:Java?
Use C or Python.
I need to use Java because:
1. I am a rookie in Python
2. I usually programing Java (and I love it)
3. In my software I have another codes and devices in Java (I want to make a HTTP server to controlate a telescope mount).

I think... I can create a socket to comunicate my Java software with sensor color in python... or I can use an arduino (I tested the sensor in arduino and it ran very nice).

Many thanks all for help.

mattlewis
Posts: 104
Joined: Sat Jan 12, 2013 3:05 pm
Location: UK
Contact: Website

Re: Read a PWM with Java using Pi4j

Wed May 24, 2017 5:34 pm

I'll have a look at porting Joan's Python code to Java tonight.
http://www.diozero.com/

mattlewis
Posts: 104
Joined: Sat Jan 12, 2013 3:05 pm
Location: UK
Contact: Website

Re: Read a PWM with Java using Pi4j

Wed May 24, 2017 8:22 pm

Ive just committed a work in progress port of joan's Python code. Available here: https://github.com/mattjlewis/diozero/b ... S3200.java. This code won't work right now, I need to review which time periods are fractions of a second and what are milliseconds. Also my digital input/ output switching class doesn't currently support callbacks, something I'll need to address.
Matt
http://www.diozero.com/

megamarky
Posts: 10
Joined: Sun Jun 11, 2017 6:43 am

Re: Read a PWM with Java using Pi4j

Mon Jun 12, 2017 1:01 pm

AlfredoCJ wrote:Hi all,
joan wrote:Java?
Use C or Python.
I need to use Java because:
1. I am a rookie in Python
2. I usually programing Java (and I love it)
3. In my software I have another codes and devices in Java (I want to make a HTTP server to controlate a telescope mount).

I think... I can create a socket to comunicate my Java software with sensor color in python... or I can use an arduino (I tested the sensor in arduino and it ran very nice).

Many thanks all for help.
Yes you should use Java, and there's a nice way to create a HTTP server with Spring Boot that has an embedded Tomcat server inside the application. It's much more convenient running and testing applications this way.

https://thehecklers.com/spring-boot-for ... g-started/
(broken link fixed)

Hope you have the time to get familiar with Spring Boot there's lots of examples http://javabeat.net/spring-boot/

If anyone's interested I could show more examples on how to connect Pi4j with Spring Boot, just send a message.
Last edited by megamarky on Fri Sep 29, 2017 5:47 am, edited 1 time in total.

clicky
Posts: 202
Joined: Thu Oct 25, 2012 7:34 am

Re: Read a PWM with Java using Pi4j

Tue Jun 13, 2017 8:33 am

Python is definitively slower unless you're using function written in C and Python only as a wrapper to it.

Return to “Java”

Who is online

Users browsing this forum: No registered users and 1 guest