rotate() doesn't work (!)

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: roger, imaqine, 99jonathan

hyderabat
New User
Posts: 14
Joined: Sun Oct 16, 2011 12:48 pm

rotate() doesn't work (!)

Postby hyderabat » Sun Nov 06, 2011 7:08 pm

Hi

I am really frustrated by now. I spent hours searching for an error, but I just can't find it.

I have separated the problem code from the rest and made an own program with it:

Code: Select all

     public static RemoteMotor [] mR;
   public static NXTRegulatedMotor [] m = {Motor.A,Motor.B,Motor.C};
   
   /**
    * @param args
    */
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      
      try{
         RemoteNXT remo = new RemoteNXT("REMO",Bluetooth.getConnector());
         mR[0] =  remo.A; mR[1] = remo.B; mR[2] = remo.C;
   
      } catch (Exception e) {LCD.drawString("Damn",0,2);}
      
      workThing();
   }
   
   public static void workThing() {
      try {
      LCD.drawString("Starting",0,0);
      Button.waitForPress();
      mR[0].rotate(180,true);
      mR[2].rotate(180);
      Button.waitForPress(10000);
      
      do {
         mR[1].rotate(90,true);
         for (int i = 0;i<3;i++) m[i].rotate(360,true);
         m[2].waitComplete();
         //Delay.msDelay(270/50 * 1000); -> waitComplete() seems to work
         mR[1].rotate(-90);
         
      } while (Button.RIGHT.isPressed());
      } catch (Exception e) {LCD.drawString("Fucked up",0,4);}
      
      Button.waitForPress();


After "Starting", the system collapses and a NullPointerException (Exception 16) is thrown (before I did the try-catch myself). It is because of the rotate()-commands just following the "Starting". And if I comment them out, the same happens with the last rotate() in the loop.

Any clue why this happens with rotate? I am using rotate hundreds of times before in the same program, and it works perfectly. Only here and at another place does it collapse:

Code: Select all

while (true) {
      for (int i = 2;i>=0;i= i-1) {m[i].setSpeed(velocity); m[i].rotate(180);}
      for (int i = 2;i>=0;i= i-1) {mR[i].setSpeed(velocity); mR[i].rotate(180);}
      ErrorEscape();
      for (int i = 0;i<3;i++) {m[i].rotate(180,true); mR[i].rotate(180,true);}   
      ErrorEscape(); //have to get an error response as fast as possible, therefore twice in here
      }


Here, it is even stranger: the error is thrown with mR[1], after m[1-3] and mR[2] already worked perfectly!

User avatar
gloomyandy
leJOS Team Member
Posts: 4769
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: rotate() doesn't work (!)

Postby gloomyandy » Sun Nov 06, 2011 7:46 pm

Almost certainly a problem with the RemoteMotor class which in 0.9.0 does not correctly handle the case of not having a listener call back. See the following threads for details...
viewtopic.php?f=7&t=2837&p=13881&hilit=+NullPointerException#p13881
viewtopic.php?f=7&t=2837&p=13854&hilit=remotemotor+null#p13854
viewtopic.php?f=7&t=2867&p=14097&hilit=motor+listener#p14097

Andy

hyderabat
New User
Posts: 14
Joined: Sun Oct 16, 2011 12:48 pm

Re: rotate() doesn't work (!)

Postby hyderabat » Sun Nov 06, 2011 8:08 pm

I tried the following thing, just like Dominik said:

Code: Select all

import lejos.nxt.*;
import lejos.nxt.remote.*;
import lejos.nxt.comm.Bluetooth;
import lejos.nxt.remote.RemoteNXT;
import lejos.robotics.RegulatedMotor;
import lejos.robotics.RegulatedMotorListener;

public class TestIt {
   
   public static RemoteMotor [] mR;
   public static NXTRegulatedMotor [] m = {Motor.A,Motor.B,Motor.C};
   
   /**
    * @param args
    */
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      
      try {
      RegulatedMotorListener [] Listener = new RegulatedMotorListener[6];
      for (int i = 0;i<3;i++) {m[i].addListener(Listener[i]);}
      for (int i = 3;i<6;i++) {mR[i-3].addListener(Listener[i]);}
      } catch (Exception e) {LCD.drawString("ListenerException",0,0);}
...
      


Threw a NullPointerException :-( Do you need the upDownInitialized = true?! don't think so... and I don't have a clue what he is doing with this implement stuff^^

User avatar
gloomyandy
leJOS Team Member
Posts: 4769
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: rotate() doesn't work (!)

Postby gloomyandy » Sun Nov 06, 2011 9:42 pm

You need to create a class that implements that motor listener interface (as shown in the first thread) and then create a single instance of that class, you then set this instance to be the listener for all three remote motors...

Andy

hyderabat
New User
Posts: 14
Joined: Sun Oct 16, 2011 12:48 pm

Re: rotate() doesn't work (!)

Postby hyderabat » Mon Nov 07, 2011 7:01 pm

So, first, I have to use the class, looking like that:

Code: Select all

package TestingArea;

import lejos.robotics.RegulatedMotorListener;
import lejos.robotics.RegulatedMotor;

public class MotorListener implements RegulatedMotorListener{

   @Override
      public void rotationStarted(RegulatedMotor arg0, int arg1, boolean arg2,
            long arg3) {
         // TODO Auto-generated method stub
        
      }

      @Override
      public void rotationStopped(RegulatedMotor arg0, int arg1, boolean arg2,
            long arg3) {
    // TODO Auto-generated method stub
        
     }
}


I didn't change anything, as I suppose this is correct (only two methods in RegulatedMotorListener). Do I have to ajust anything here?

Then, I initialize the motors in the main class/where the rest happens:

Code: Select all

try {
      RegulatedMotorListener Listener = new MotorListener();
      for (int i = 0;i<3;i++) {mR[i-3].addListener(Listener);}
      } catch (Exception e) {LCD.drawString("ListenerException",0,0);}


Isn't working for me, as I can't even put the MotorListener() class on the brick :-( I would really, really appreciate some detailed help here... Thanks!

skoehler
leJOS Team Member
Posts: 1545
Joined: Thu Oct 30, 2008 4:54 pm

Re: rotate() doesn't work (!)

Postby skoehler » Mon Nov 07, 2011 8:50 pm

hyderabat wrote:

Code: Select all

try {
      RegulatedMotorListener Listener = new MotorListener();
      for (int i = 0;i<3;i++) {mR[i-3].addListener(Listener);}
      } catch (Exception e) {LCD.drawString("ListenerException",0,0);}



i-3 is clearly negative! Since Java arrays cannot have negative indexes, your code most certainly crashes with an ArrayIndexOutOfBoundsException and not with some sort of "ListenerException". If you want to know what Exception your actually catching, you might also write e.getClass().toString() to the Screen. It will not print the name, but it print the classes number.

hyderabat
New User
Posts: 14
Joined: Sun Oct 16, 2011 12:48 pm

Re: rotate() doesn't work (!)

Postby hyderabat » Mon Nov 07, 2011 9:23 pm

You're right, the mistake came from previous code... But it doesn't change anything :-(

Exception is class 16 -> still NullPointerException

BTW: In order to get the class MotorListener to the NXT, I just added an empty main()-loop below... can I do that?

hyderabat
New User
Posts: 14
Joined: Sun Oct 16, 2011 12:48 pm

Re: rotate() doesn't work (!)

Postby hyderabat » Thu Nov 10, 2011 9:06 pm

Please, has anyone an idea? I would be really, really greatful. It is for a school project, and I can't get any further on my own as I am just getting to know LeJOS...

Here again the code in the main-file:

Code: Select all

try {
      RegulatedMotorListener Listener = new MotorListener();
      for (int i = 0;i<3;i++) {mR[i].addListener(Listener);}
      } catch (Exception e) {LCD.drawString(e.getClass().toString(),0,0);}


Then, I created an own file for the following class. I can't include it into the other file (as Eclipse told me).

Code: Select all

public class MotorListener implements RegulatedMotorListener{

   @Override
      public void rotationStarted(RegulatedMotor arg0, int arg1, boolean arg2,
            long arg3) {
         // TODO Auto-generated method stub
        
      }

      @Override
      public void rotationStopped(RegulatedMotor arg0, int arg1, boolean arg2,
            long arg3) {
    // TODO Auto-generated method stub
         }
      
      public static void main(String[] args) {
         // TODO Auto-generated method stub
         
      }
}

User avatar
gloomyandy
leJOS Team Member
Posts: 4769
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: rotate() doesn't work (!)

Postby gloomyandy » Thu Nov 10, 2011 10:38 pm

Try the following code (you will have to change the name of the brick to match your second nxt)...

Code: Select all

import lejos.nxt.comm.Bluetooth;
import lejos.nxt.remote.RemoteNXT;
import lejos.robotics.RegulatedMotor;
import lejos.robotics.RegulatedMotorListener;

class Listener implements RegulatedMotorListener
{

    @Override
    public void rotationStarted(RegulatedMotor arg0, int arg1, boolean arg2,
            long arg3)
    {
        // TODO Auto-generated method stub       
    }

    @Override
    public void rotationStopped(RegulatedMotor arg0, int arg1, boolean arg2,
            long arg3)
    {
        // TODO Auto-generated method stub       
    }
   
}


public class RemoteTest
{

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception
    {
        RegulatedMotorListener l = new Listener();
        RemoteNXT nxt = new RemoteNXT("nxt4", Bluetooth.getConnector());
        RegulatedMotor m = nxt.B;
        m.addListener(l);
        m.rotateTo(360);
    }
}

If you still get an exception. Post exactly what is displayed on the screen here (do not add any try/catch statements to the above code) and also include the verbose output from the linker (you may have to change your eclipse project settings to get leJOS to generate this output)...

Andy

hyderabat
New User
Posts: 14
Joined: Sun Oct 16, 2011 12:48 pm

Re: rotate() doesn't work (!)

Postby hyderabat » Sun Nov 13, 2011 8:51 pm

After I changed a few things, I run the program on the NXT, and it worked perfectly. Thanks a ton for it, I could alter my own program following the general syntax of your program, and it works perfectly now :-) Finally, my bloody school project is running... Thanks everyone for your help!!!


Return to “NXJ Software”

Who is online

Users browsing this forum: No registered users and 1 guest