Results 1 to 11 of 11
  1. #1
    scarface78987 is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Can't get JInput work properly

    Hello everyone,
    I'm scarface78987 and I'm new to programming. I knew something of php and C, then I moved to Java for a school project.
    I'm trying to remotely control an Arduino-based robot. I already know how to make Java and Arduino communicate.
    I've downloaded JInput from here Jinput — Java.net and I tried to figure out how to read the Joystick values and take them apart, to process them and send them to my Arduino.
    I took some pieces of code from the tutorials I found on the internet: I'm able to display the Controller type, the number of Components that it has, but the values are stuck at 0.0 (for buttons).
    I also ran jinput-test.jar which comes together with the distributed pack and it works pretty well, it displays some windows and one of them is the controller which I'm intended to use.. just fine. But I can't replicate this in my Java Project.
    I'm using Eclipse.
    Here is my java class code:

    Java Code:
    package como.diprov.arduino;
    
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    import javax.swing.JFrame;
    
    import net.java.games.input.Component;
    import net.java.games.input.Controller;
    import net.java.games.input.ControllerEnvironment;
    import net.java.games.input.test.ControllerReadTest;
    
    public class Prova extends JFrame {
    	static final long HEARTBEATMS = 100; // 10th of a second
    	List controllers = new ArrayList();
    
    	/** Creates a new instance of ConrtollerReadTest */
    	public Prova() {
    
    		super("Controller Read Test");
    		ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment();
    		Controller[] ca = ce.getControllers();
    		/*for(int i =0;i<ca.length;i++){
    			System.out.println(ca[i].getName());
    		}*/
    		final Controller myController = ca[3];
    		Component[] mc = myController.getComponents();
    
    
    		new Thread(new Runnable() {
    			public void run() {
    				try {
    					while (true) {
    
    							try {
    								Component[] myComponent = myController.getComponents();
    								for(int i =0;i<myComponent.length;i++){
    								
    															
    								System.out.println(i + "..." + myComponent[i].getName());
    								StringBuffer buffer = new StringBuffer();
    								if(myComponent[i].isAnalog()) {
    									buffer.append(myComponent[i].getPollData());
    								} else {
    									float valore = myComponent[i].getPollData();
    									if(valore==1.0f) {
    										buffer.append("On [" + valore + "]");
    									} else {
    										buffer.append("Off [" + valore + "]");
    									}
    									
    								}
    								System.out.println(buffer.toString());
    								}
    							} catch (Exception e) {
    								e.printStackTrace();
    							}
    						Thread.sleep(HEARTBEATMS);
    					}
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		}).start();
    	}
    
    }
    This is what I can see in the output
    5...Pulsante 5
    Off [0.0]
    This kind of thing for every button or axis, no matter which button I press!

    Thank you very much for the help,
    I'm sorry for my English and for my ignorance, I'm from Italy and I'm new to programming
    If you need further information I'll post them asap!

    scarface78987

    p.s. If you'd like to check out the project I've been talking about you can find us on twitter! https://twitter.com/dip_ROV

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Can't get JInput work properly

    I suggest you get your components before the loop and keep them - maybe they get initialized each time you obtain them?
    I like likes!

  3. #3
    scarface78987 is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Re: Can't get JInput work properly

    I tried to initialize the Components before the loop but nothing changes. I think there is something missing in my code. Do you know where can I find a tutorial with all the information I need to build a class from zero and make it work for my joystick? I found some guides but they were written for people who already know exactly where to put everything to make it work.

  4. #4
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Can't get JInput work properly

    Post your code with the changes please, only with them I can make sure you got what I meant. :)
    Sadly, I did not use that class before so I do not know any tutorials.
    I like likes!

  5. #5
    scarface78987 is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Re: Can't get JInput work properly

    Oh sorry. The strange fact is: with the jinput-test.jar everything works. So how do I take the code I want to use from there?
    Here is the source of the class that is working
    Jinput: Subversion: branchesversion2coreAPIsrcjavanetjavagamesinputtes tControllerReadTest.java &mdash; Java.net

    Java Code:
    package como.diprov.arduino;
    
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    import javax.swing.JFrame;
    
    import net.java.games.input.Component;
    import net.java.games.input.Controller;
    import net.java.games.input.ControllerEnvironment;
    import net.java.games.input.test.ControllerReadTest;
    
    public class Prova extends JFrame {
    	static final long HEARTBEATMS = 1000; // 10th of a second
    	List controllers = new ArrayList();
    
    	/** Creates a new instance of ConrtollerReadTest */
    	public Prova() {
    
    	super("Controller Read Test");
    	ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment();
    	Controller[] ca = ce.getControllers();
    	/*for(int i =0;i<ca.length;i++){
    		System.out.println(ca[i].getName());
    	}*/
    	final Controller myController = ca[6];
    	Component[] mc = myController.getComponents();
    	final Component[] myComponent = myController.getComponents();
    		new Thread(new Runnable() {
    		public void run() {
    			try {
    				while (true) {
    					try {
    					
    				    	//for(int i =0;i<myComponent.length;i++){
    						int i = 5;
    													
    						System.out.println(i + "..." + myComponent[i].getName());
    						StringBuffer buffer = new StringBuffer();
    						if(myComponent[i].isAnalog()) {
    							buffer.append(myComponent[i].getPollData());
    						} else {
    							float valore = myComponent[i].getPollData();
    								if(valore==1.0f) {
    									buffer.append("On [" + valore + "]");
    								} else {
    									buffer.append("Off [" + valore + "]");
    								}
    								
    							}
    							System.out.println(buffer.toString());
    								//}
    						} catch (Exception e) {
    							e.printStackTrace();
    						}
    					Thread.sleep(HEARTBEATMS);
    					}
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		}).start();
    	}
    
    }

  6. #6
    scarface78987 is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Re: Can't get JInput work properly

    No answer?

  7. #7
    jeraux is offline Member
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default Re: Can't get JInput work properly

    Quote Originally Posted by scarface78987 View Post
    No answer?
    I'm trying a similiar project and very much enjoyed your pics on twitter. Only difference is I'm 7 years out of programming and when I left I had yet to get into Classes...so you can imagine the learning curve I'm facing here! ha

    I'm not going through Arduino, I planning on getting some motor controllers I ordered working through usb with Android...in theory it works...pounding it out will be interesting!

    Anyway, I'm fighing the same battle but with a usb xbox controller and this link below finally gave me some results - good instructions for eclipse.

    Yod: HowTo: Set up JInput API and Library for Java Development on Mac OS X or Linux Platform (there's no source here, just class files)

    Joystick in Java with JInput TheUzo007 (this link has some source that can provide some examples, I've yet to start coding, just got the controller/computer connection/read done...baby steps...baby steps.

    best of luck, your rov looks awesome - please post more pics!!! I need ideas...lol

    take care

  8. #8
    jeraux is offline Member
    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Default Re: Can't get JInput work properly

    btw, search with the term 'gamepad' or 'gamepadtest' ect, and you should be able to dig up more coding examples...search through libgdx, there's a file in there for gamepad testing. There's an IDE called Greenfoot that also has a gamepad test program.
    take care

  9. #9
    scarface78987 is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Re: Can't get JInput work properly

    Jeraux,
    Thank you for your kind answer :) I finally got it working and we've already tried the ROV in the tub. The ps2 controller rocks ;)
    I'd really like to see your project :) if you need more ideas you can keep reading us on twitter: we're now planning to build a servo-controlled cilinder to be filled with water when we want to dive deeper, like ordinary submarines! Test tubes to catch water for further analyzes are also being planned.
    We're almost done, while my mates do the mechanics I'm trying to figure out how to build a rotating 3D model basing on gyroscope and accelerometer values. This is what I'm about to do: MPU6050 DMP 6-axis Fusion running on Arduino!!! - YouTube
    Best of luck to you too ;) we'll post HD videos of Lake Como's lakebed as soon as we get there!
    Bye!

  10. #10
    TheUzo007 is offline Member
    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    Default Re: Can't get JInput work properly

    You forgot to poll your controler for data before you use controller caomponents. You need to poll controller every time before using its components. Put myController.poll(); into your while loop.
    Also maybe would be better to search for controler by the type of controller rather then index, it may not be always on index 6.

  11. #11
    scarface78987 is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Re: Can't get JInput work properly

    I found out that was the problem, but thank you anyway :) I'd like to search for controller by type but I have two controllers with the same name so Java gets confused on which he has to choose, so I use the index, it won't change.
    See you!

Similar Threads

  1. The <sup> tag doesn't work properly
    By JosAH in forum AWT / Swing
    Replies: 26
    Last Post: 02-18-2012, 08:55 AM
  2. Help, I can't make the JButton to work properly
    By furieux in forum New To Java
    Replies: 11
    Last Post: 01-07-2012, 06:13 PM
  3. class that wont work out properly.
    By vampire-elf in forum New To Java
    Replies: 7
    Last Post: 09-07-2010, 02:39 AM
  4. Replies: 3
    Last Post: 05-06-2009, 01:11 AM
  5. Eclipse Documentation does not work properly.
    By krawetko in forum Eclipse
    Replies: 0
    Last Post: 10-05-2008, 11:06 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •