errors in java program "black line"

Post your NXJ projects, project ideas, etc here!

Moderators: 99jonathan, roger, imaqine

errors in java program "black line"

Postby legott » Wed Sep 16, 2009 8:32 am

Hallo! I've just startet to program with Java in combination with leJOS. I use Eclipse Galileo. My first programs work, but I've still trouble with the "black line". Perhaps, you can help. Here is my Code. I've integrated two comments. These are the error reports of Eclipse.

Code: Select all
import lejos.nxt.*;
import lejos.util.*;
import lejos.robotics.navigation.*;
public class manuel
{
Stopwatch sw = new Stopwatch();
int sport1, sport2, wport1, wport2;
//Scharz- und Weiss-Werte für Lichtsensor 1,2
int midpoint1, midpoint2; //Mittelpunkte
int port1c, port2c; //Variablen fuer aktuellen Lichtwert für Port 1 und 2
boolean port1r, port2r;
LightSensor licht1 = new LightSensor(SensorPort.S1);
//Lichtsensor rechts an Port1
// Ambient Light on
LightSensor licht2 = new LightSensor(SensorPort.S3);
//Lichtsensor links an Port3
// Ambient Light on
Motor [] m = {Motor.A, Motor.B};
/**
* @param args
* @author Manuel und Timo
*/
public static void main(String[] args)
{
LCD.drawString("schwarze Linie", 0, 1);
new manuel().Configuration();
new manuel().go();
}
public void go()
{
boolean ButtonPressed = false;
while(ButtonPressed = false)
{
new manuel().aktuellerLichtwertMessen();
new manuel().RichtungsEntscheidung();
if(Button.isPressed(true)) ButtonPressed = true;
//The method isPressed() in the type Button is not applicable for the arguments (boolean)
}
}
public void Configuration()
{
while(sw.elapsed() < 5000)
{
sw.reset();
LCD.clear();
LCD.drawString("Bitte beide",0,0);
LCD.drawString("Sensoren auf",0,1);
LCD.drawString("schwarz setzen",0,2);
sport1 = licht1.getLightValue();
sport2 = licht2.getLightValue();
}
while(sw.elapsed() < 5000)
{
sw.reset();
LCD.clear();
LCD.drawString("Bitte beide",0,0);
LCD.drawString("Sensoren auf",0,1);
LCD.drawString("weiss setzen",0,2);
wport1 = licht1.getLightValue();
wport2 = licht2.getLightValue();
LCD.clear();
}
midpoint1 = ((wport1 + sport1)/2);
midpoint2 = ((wport2 + sport2)/2);
}
public void aktuellerLichtwertMessen()
{
port1c = licht1.getLightValue();
//Messergebnis von Port1 als integere Zahl
port2c = licht2.getLightValue();
//Messergebnis von Port3 als integere Zahl
if(port1c > midpoint1) port1r = true;
if(port1c < midpoint1) port1r = false;
//else port1r = false;
if(port2c > midpoint2) port2r = true;
if(port2c < midpoint2) port2r = false;
//else port2r = false
}
public void RichtungsEntscheidung()
{
aktuellerLichtwertMessen();
if(port1r = true) if(port2r = false)
for(int i = 0; i<2; i++)m[i].forward();
else
for(int i = 0; i<2; i++)m[i].rotateRight();
//The method rotateRight() is undefined for the type Motor
if(port1r = false) if(port2r = true)
for(int i = 0; i<2; i++)m[i].rotateLeft();
//The method rotateLeft() is undefined for the type Motor
else BBError();
}
}


BBError() is not programmed yet. You can ignore this fault.

Thanks for help! legott
legott
New User
 
Posts: 11
Joined: Wed Sep 16, 2009 8:05 am

Postby legott » Fri Sep 18, 2009 2:22 pm

Hallo, I've tried a lot. But my problems are still not completely solved.

Concerning my Button-Problem:
Eclipse cannot find an error, but this code causes an error with my NXT.

Code: Select all
  boolean ButtonPressed = false;
  while(ButtonPressed == false)
  {
        if(bt1.isPressed()) ButtonPressed = true;
  }

Other while codes (with time or the simple while(true)) do not repeat the code.

For my next problem, the full code at first:

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

public class manuel
{
   Stopwatch sw = new Stopwatch();
   int sport1, sport2, wport1, wport2;
   int midpoint1, midpoint2;
   int port1c, port2c;
   boolean port1r, port2r;
   LightSensor licht1 = new LightSensor(SensorPort.S1);
   LightSensor licht2 = new LightSensor(SensorPort.S3);
   Motor [] m = {Motor.A, Motor.B};
   Button bt1;
   
   /**
    * @param args
    */
   
   public static void main(String[] args)
   {
      new manuel().Configuration();
      new manuel().go();
   }
   
   public void go()
   {
      boolean ButtonPressed = false;
      while(ButtonPressed == false)
                {
               new manuel().aktuellerLichtwertMessen();
              new manuel().RichtungsEntscheidung();
              if(bt1.isPressed()) ButtonPressed = true;
                }
   }
   
   public void Configuration()
   {
      Button.waitForPress();
      sport1 = licht1.getLightValue();
      sport2 = licht2.getLightValue();
            
      Button.waitForPress();
      wport1 = licht1.getLightValue();
      wport2 = licht2.getLightValue();
      midpoint1 = ((wport1 + sport1)/2);
      midpoint2 = ((wport2 + sport2)/2);
      
      Button.waitForPress();
   }
   
   public void aktuellerLichtwertMessen()
   {
      port1c = licht1.getLightValue();
      port2c = licht2.getLightValue();
      
      if(port1c < midpoint1) port1r = false;
      if(port1c > midpoint1) port1r = true;

      if(port2c < midpoint2) port2r = false;
      if(port2c > midpoint2) port2r = true;
   }
   
   public void RichtungsEntscheidung()
   {
      aktuellerLichtwertMessen();
      
      if(port1r = true) if(port2r == true)
         {
         for(int i = 0; i<2; i++)m[i].forward();
         }
      
      if(port1r = true) if(port2r == false)
         {
         Motor.A.forward();
         }
      
      if(port1r = false) if(port2r == true)
      {
         Motor.B.forward();
      }
      
      if(port1r = false) if(port2r == false)
      {
         Motor.A.forward();
      }
   }
   
}


My problem: I get always the case port1r and port 2r = true. My Lightsensors are okay. They're getting correct results. Here are three of them for each color and each sensor:

sport1: 37,29,29
sport2: 26, 27, 26

wport1: 56,60,58
wport2: 51,56,53

midpoint1: 46,44,43 (midpoint1 = (sport1 + wport1)/2)
midpoint2: 38,41,39 (midpoint2 = (sport2 + wport2)/2)

I think, the fault has to be in the method "aktuellerLichtwertMessen()" ( or in "RichtungsEntscheidung()" ).

Thanks for your help! Sorry for my grammar faults. I'm not a native speaker.

G.legott
Last edited by legott on Fri Sep 18, 2009 4:42 pm, edited 2 times in total.
legott
New User
 
Posts: 11
Joined: Wed Sep 16, 2009 8:05 am

Postby gloomyandy » Fri Sep 18, 2009 3:05 pm

Hi,
A few hints...
1. You seem to be trying to use bt1 without ever assigning a value to it. You probably want something like
Button bt1 = Button.ENTER; or Button bt1 = Button.ESCAPE;

2. You need to learn the difference between "=" and "==", one performs assignment the other compares two values to see if they are equal. In many of your if statements you are using if (val1 = val2), you almost certainly should be using if (val1 == val2).

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

Postby legott » Fri Sep 18, 2009 4:46 pm

Hi gloomayandy, I've corrected the fault concerning "=" and "==". But there's still the problem, that both boolean port1r and port2r are "true".

It seems, that "<" and ">" does not work correct.

Code: Select all
      if(port1c < midpoint1) port1r = false;
      if(port1c > midpoint1) port1r = true;

      if(port2c < midpoint2) port2r = false;
      if(port2c > midpoint2) port2r = true;


G.legott
legott
New User
 
Posts: 11
Joined: Wed Sep 16, 2009 8:05 am

Postby gloomyandy » Fri Sep 18, 2009 5:02 pm

Hi,
Have you checked the actual light levels? Modify your program so that if both values are true you display the values of port1c and port2c along with midpoint1 and midpoint2 and sleep for a long time then you can check the light levels you have read. Remember that the light sensor will pick up all sources of light. Perhaps the sensor was in the shadow when you calibrated it...

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

Postby legott » Fri Sep 18, 2009 8:46 pm

All but Problem 1 with the Button is solved. I had to create a static midpoint, not an unstatic. Now it works fine. At the moment, I've to stop the robot by putting off the batteries.

Thanks for your help. Timo
edit: Your Tip with Button.ESKAPE() war perfect. My first line follower who works. ^^
legott
New User
 
Posts: 11
Joined: Wed Sep 16, 2009 8:05 am


Return to NXJ Projects

Who is online

Users browsing this forum: No registered users and 1 guest

more stuff