Error on Utilizing RegulatedMotorListener

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

Moderators: 99jonathan, roger, imaqine

Error on Utilizing RegulatedMotorListener

Postby cwpark » Wed Nov 27, 2013 8:01 am

Hi

I'm developing an NXT test application for controlling NXT by PC.
I want to implement the application using RemoteMotor and MirrorMotor.
However, there is a few issues on utilizing RegulatedMotorListener.

For example, I don't know why RemoteMotor utilizes only a operation,rotationStopped(),in RegulatedMotorListener
I cannot also understand why MirrorMotor doesn't call rotationStopped().

The test codes and surveyed library codes are as follows:

In My Test Application
Code: Select all
MirrorMotor motor = (MirrorMotor) MirrorMotor
       .invertMotor(new RemoteMotor(new NXTCommand(connector
       .getNXTComm()), 0));

      motor.addListener(new RegulatedMotorListener() {
         Format format = new SimpleDateFormat("yyyy MM dd HH:mm:ss");
         
         @Override
         public void rotationStopped(RegulatedMotor motor, int tachoCount,
               boolean stalled, long timeStamp) {
            Date date = new Date(timeStamp);
            
            System.out.println("******rotationStopped******");
            System.out.println("tachoCount: " + tachoCount);
            System.out.println("isStopped");
            System.out.println(format.format(date).toString());
         }

         @Override
         public void rotationStarted(RegulatedMotor motor, int tachoCount,
               boolean stalled, long timeStamp) {
            // TODO Auto-generated method stub
            Date date = new Date(timeStamp);
            
            System.out.println("******rotationStarted******");
            System.out.println("tachoCount: " + tachoCount);
            System.out.println("isStarted");
            System.out.println(format.format(date).toString());
         }
      });
      
      System.out.println("=======An motor is being started=======");
      motor.rotate(360);      

      try {
         Thread.sleep(1000);
      } catch (InterruptedException e1) {
         // TODO Auto-generated catch block
         e1.printStackTrace();
      }
      
      System.out.println("=======An motor is being stopped=======");
      motor.flt(false);


In MirrorMotor.java
Code: Select all
public void rotationStarted(RegulatedMotor motor, int tachoCount,
         boolean stalled, long timeStamp) {
      if(regListener!=null)
         regListener.rotationStarted(this, -tachoCount, stalled, timeStamp);
   }

   public void rotationStopped(RegulatedMotor motor, int tachoCount,
         boolean stalled, long timeStamp) {
      if(regListener!=null)
            regListener.rotationStarted(this, -tachoCount, stalled, timeStamp);
   }


In RemoteMotor.java
Code: Select all
public void waitComplete()
    {
        while (isMoving())
            Thread.yield();
        if (listener != null) listener.rotationStopped(this, getTachoCount(), false, System.currentTimeMillis());
    }


If you know why the library is implemented like these, please tell me the reason.
Thank you in advance for your help.
cwpark
New User
 
Posts: 1
Joined: Wed Nov 27, 2013 7:43 am

Re: Error on Utilizing RegulatedMotorListener

Postby gloomyandy » Wed Nov 27, 2013 10:18 am

The MirrorMotor just looks like a copy and paste bug. The RemoteMotor case looks like it has just enough to make some other code work. Must admit I'm not a fan of the listener model as it has all sorts of implications for race conditions and the potential to create deadlocks. If you possibly can I would avoid using the mechanism. If you must use it then you will need to fix the code (which in the case of remote motor will be tricky as there is no single point to capture the start of motion. It is also worth noting that even if you do this then you will not be correctly tracking when a motor actually stops as LCP does not contain any mechanism to allow a call back to inform you of this event. You will only see the stop notification if something actually waits for the stop...

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


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 3 guests

more stuff