Jump to content

[Java] Update auf 2.0 Probleme


Recommended Posts

Ich bin kein Experte in Sachen Java, sondern habe mir ein Tinkerforge ServoBrick zum lernen gekauft und ich muss sagen damit macht das richtig Spaß. Ich habe auch schon erfolgreich ein Programm geschrieben um mein Modellauto fernzusteuern.

Mein Problem ist jetzt, dass es nach dem Update nicht mehr funktioniert.

Die hier genannten Änderungen habe ich gemacht:

http://www.tinkerforge.com/doc/Transitioning_Guide_1To2.html#java

 

Allerdings bekomme ich jetzt immer wenn ich die externe Stromversorgung mit servo.getExternalInputVoltage() abfrage

eine NullPointerException und beim beenden des Programmes mit folgendem Code auch:

WindowAdapter exitListener = new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
            	
    	        try {
    	        	
				Init.servo.setPosition((short)0, (short)0);
    	        Init.servo.setPosition((short)6, (short)0);
    	        
			} 	catch (TimeoutException e1) {	e1.printStackTrace();} 
    	        	catch (NotConnectedException e1) {e1.printStackTrace();	}	  
    	        
    	        System.out.println("Programm beendet!");
            	System.exit(0);
            }
        };

 

Zur Fehlersuche noch meine Klasse, die die verbindung aufbaut:

 

public class Init {
static String host;
    private static final int port = 4223;
    private static final String UID = "****";

    static IPConnection ipcon;
    static BrickServo servo;
    
    static void connection() {
    	Scanner s = new Scanner(System.in);
    	System.out.print("Welche Ip soll verwendet werden?\n1. 192.168.2.***\n2. 127.0.0.1\n3. eine Andere\nWarte auf Eingabe...\n");
    	int x = s.nextInt();
	switch(x) {
	case 1:
		host = "192.168.2.***";
		break;
	case 2:
		host = "127.0.0.1";
		break;
	case 3:
		System.out.println("Bitte Ip eingeben...");
		host = s.next();
		break;
	default :
		System.out.println("Falsche Eingabe!");
		Init.connection();
		break;
	}

    	try{
    		System.out.println("Stelle Verbindung zum Brick her ...");

    		IPConnection ipcon = new IPConnection();
    		BrickServo servo = new BrickServo(UID, ipcon);
    		ipcon.connect(host, port);
    		
            System.out.println("Verbindung erfolgreich hergestellt");
            
            servo.setOutputVoltage(5500);
            
            servo.setDegree((short)0, (short)-4500, (short)4500);
            servo.setPulseWidth((short)0, 1000, 2000);
            servo.setPeriod((short)0, 19500);
            servo.setAcceleration((short)0, 0xFFFF);
            servo.setVelocity((short)0, 0xFFFF); //Notiz: 0xFFFF = 65535 (größter 2 Bytewert)
            servo.setPosition((short)0, (short)0);
            servo.enable((short)0);
            System.out.println("Servo 0(Lenkung) konfiguriert.");
            
            servo.setDegree((short)6, (short)-4500, (short)4500);
            servo.setPulseWidth((short)6, 1000, 2000);
            servo.setPeriod((short)6, 20000);
            servo.setAcceleration((short)6, 0xFFFF);
            servo.setVelocity((short)6, 0xFFFF);
            servo.setPosition((short)6, (short)0);
            servo.enable((short)6);
            System.out.println("Servo 6(Motor) konfiguriert.");    		
    	} catch( Exception ex) {
    		System.out.println("Programm beendet, die Verbindung konnte nicht hergestellt werden!");
    		System.exit(-1);
    	}
    }
}

 

Link to comment
Share on other sites

Kannst Du den Stacktrace der Exception noch dazu packen?

 

Ich erzeuge die Devices immer erst nach dem Connect (allerdings generisch über den enumerate()). Das wäre dann diese Reihenfolge:

ipcon.connect(host, port);
BrickServo servo = new BrickServo(UID, ipcon);

 

Ob das aber was ändert habe ich nicht ausprobiert. Die übergebene UID ist korrekt?

Link to comment
Share on other sites

Die erste Exception durch das Abfragen der Akkuspannung:

Exception in thread "main" java.lang.NullPointerException
at servo.SecondThread.run(SecondThread.java:19)
at java.lang.Thread.run(Thread.java:679)
at servo.SecondThread.MyThread(SecondThread.java:9)
at servo.MeinFrame.launchFrame(MeinFrame.java:68)
at servo.Main.main(Main.java:11)

Und die vom ExitListener

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at servo.MeinFrame$1.windowClosing(MeinFrame.java:50)
at java.awt.AWTEventMulticaster.windowClosing(AWTEventMulticaster.java:349)
at java.awt.AWTEventMulticaster.windowClosing(AWTEventMulticaster.java:349)
at java.awt.Window.processWindowEvent(Window.java:1905)
at javax.swing.JFrame.processWindowEvent(JFrame.java:290)
at java.awt.Window.processEvent(Window.java:1863)
at java.awt.Component.dispatchEventImpl(Component.java:4750)
at java.awt.Container.dispatchEventImpl(Container.java:2103)
at java.awt.Window.dispatchEventImpl(Window.java:2518)
at java.awt.Component.dispatchEvent(Component.java:4576)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:672)
at java.awt.EventQueue.access$400(EventQueue.java:96)
at java.awt.EventQueue$2.run(EventQueue.java:631)
at java.awt.EventQueue$2.run(EventQueue.java:629)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:116)
at java.awt.EventQueue$3.run(EventQueue.java:645)
at java.awt.EventQueue$3.run(EventQueue.java:643)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:642)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)

 

Die UID müsste laut BrickViewer korrekt sein. Ist es normal das sie sich nach einem Update der Firmware ändert?

 

 

Link to comment
Share on other sites

You are using this

 

public class Init {
...	
    static IPConnection ipcon;
    static BrickServo servo;
...
static void connection() {
...
IPConnection ipcon = new IPConnection();
BrickServo servo = new BrickServo(UID, ipcon);
..

 

Change it to

public class Init {
...	
    static IPConnection ipcon;
    static BrickServo servo;
...
static void connection() {
...
ipcon = new IPConnection();
servo = new BrickServo(UID, ipcon);
..

 

Otherwise you assign the BrickServo object to a local variable which visible in the method only. And the static member servo is still null.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...