leJOS Tutorial: Program 5: Regulating motor speed

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

Moderators: 99jonathan, roger, imaqine

leJOS Tutorial: Program 5: Regulating motor speed

Postby gwc » Fri Dec 16, 2011 2:48 pm

Dear community,

i'm currently doing the tutorials from this page but the code from "Program 5" doesn't works.

Solution from the tutorial: click here
Code: Select all
import lejos.nxt.*;
import lejos.util.*;

/**
 * Test the accuracy of motor speed regulation.
 * @author Roger
 */
public class RegulateTest
{
    Stopwatch sw = new Stopwatch();
    Motor[] m = {Motor.A, Motor.B, Motor.C}; //build an array of motors
   
    /**
     * Display program name, wait for button, then call go/
     * @param args
     */
    public static void main( String[] args)
    {
        LCD.drawString(" Reg Test", 0, 0);
        Button.waitForPress();
        LCD.clear();
        new RegulateTest().go();   
    }
    /**
     * helper method - does the detailed work; resets tacho count, runs motors,
     * displays data
     */
    public void go()
    {
        LCD.clear();
        sw.reset(); 
        for( int i = 0; i<3; i++)m[i].resetTachoCount();
        for( int i = 0; i<3; i++)m[i].setSpeed(720);
        for( int i = 0; i<3; i++)m[i].forward();
        for(int r = 0 ; r<8; r++)
        {
            while(sw.elapsed() < 200* r)Thread.yield();
            for( int i = 0; i<3; i++)
            LCD.drawInt(m[i].getTachoCount(),5*i,r);           
        }
        for( int i = 0; i<3; i++)m[i].stop();
        Button.waitForPress();       
    }
   
}


In eclipse i get at line 11

Code: Select all
Motor[] m = {Motor.A, Motor.B, Motor.C}; //build an array of motors


the following error:

Code: Select all
Multiple markers at this line
   - Type mismatch: cannot convert from NXTRegulatedMotor to Motor
   - Type mismatch: cannot convert from NXTRegulatedMotor to Motor
   - Type mismatch: cannot convert from NXTRegulatedMotor to Motor


and at lines 32, 33, 34, 39, 41

Code: Select all
32: for( int i = 0; i<3; i++)m[i].resetTachoCount();
33: for( int i = 0; i<3; i++)m[i].setSpeed(720);
34: for( int i = 0; i<3; i++)m[i].forward();
39: LCD.drawInt(m[i].getTachoCount(),5*i,r);
41: for( int i = 0; i<3; i++)m[i].stop();


the following errors:

Code: Select all
32: The method resetTachoCount() is undefined for the type Motor
33: The method setSpeed(int) is undefined for the type Motor
34: The method forward() is undefined for the type Motor
39: The method getTachoCount() is undefined for the type Motor
41: The method stop() is undefined for the type Motor


There seems to be an problem by defining the Motor[]-Array.

If I try to run it on my NXT, then i get this error code:

Code: Select all
Exception: 2
Unresolved compi
 at: 6(7)


If i rewrite the code to this, it works:

Code: Select all
import lejos.nxt.*;
import lejos.util.*;

public class RegTest
{   
   Stopwatch sw = new Stopwatch();
   //Motor[] m = {Motor.A, Motor.B, Motor.C};
   MotorPort[] m = {MotorPort.A, MotorPort.B, MotorPort.C};
   
   public static void main (String[] args)
   {
      LCD.drawString("Reg Test", 0, 0);
      Button.waitForPress();
      LCD.clear();
      new RegTest().go();      
   }
   
   public void go()
   {   
      LCD.clear();
      sw.reset();
      
      for(int i=0; i<3; i++) m[i].resetTachoCount();
      //for(int i=0; i<3; i++) m[i].setSpeed(720);
      //for(int i=0; i<3; i++) m[i].forward();
      for(int i=0; i<3; i++) m[i].controlMotor(100, 1);
      
      for(int r=0; r<8; r++)
      {
         while(sw.elapsed() < 200* r) Thread.yield();
         for(int i=0; i<3; i++)
            LCD.drawInt(m[i].getTachoCount(), 5*i, r);         
      }            
      
      //for(int i=0; i<3; i++) m[i].stop();
      for(int i=0; i<3; i++) m[i].controlMotor(0, 3);
      Button.waitForPress();      
   }
}


But i want to know, why the solution from the tutorial doens't work. Where is the problem? :?:

Thanks in advance!
gwc
New User
 
Posts: 2
Joined: Fri Dec 16, 2011 2:07 pm

Re: leJOS Tutorial: Program 5: Regulating motor speed

Postby gloomyandy » Fri Dec 16, 2011 3:06 pm

The tutorials are a little out of date. Try changing the Motor[] to RegulatedMotor[] (you may also need to change the imports to also import the RegulatedMotor interface)...
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3967
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: leJOS Tutorial: Program 5: Regulating motor speed

Postby gwc » Fri Dec 16, 2011 6:27 pm

Ah, ok. Thanks!
gwc
New User
 
Posts: 2
Joined: Fri Dec 16, 2011 2:07 pm


Return to NXJ Software

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

cron
more stuff