Easy Eclipse Projekt for School

Post your NXJ projects, project ideas, etc here!

Moderators: 99jonathan, roger, imaqine

Easy Eclipse Projekt for School

Postby oensen » Mon Jun 29, 2009 12:34 pm

Hallo,

we have to do an easy eclipse projekt with the NXT for school.
So far I have made a Java-Projekt, but it doesn´t really work.
Normally the NXT schould drive with a speed of 720 for two seconds, but the wheels just turn for a very short moment
What could be my mistake?



import lejos.nxt.Motor;

public class Motoren {

public static void main(String [] args) {

Motor.A.setSpeed(720);
Motor.C.setSpeed(720);
Motor.A.forward();
Motor.C.forward();
Motor.A.stop();
Motor.C.stop();
try {
Thread.sleep(2000);
}catch (InterruptedException e) {
}
}

}




For the presentation it would be good to have a more difficult Projekt.
Could you please give me a prjekt where the NXT drives around obstacels with his ultrasonic sensor?

Sry for my bad englisch^^
oensen
New User
 
Posts: 8
Joined: Mon Jun 29, 2009 12:25 pm

Postby paulmc » Mon Jun 29, 2009 1:47 pm

Have another look at your code. It does 4 things:

1) Sets the motor speed
2) Tells the motors to go forward
3) Tells the motors to stop
4) Waits ("sleeps") for two seconds

Are these in exactly the right order? :)

Paul.
paulmc
New User
 
Posts: 23
Joined: Mon Jan 26, 2009 3:11 pm
Location: Sunderland, UK

Postby oensen » Mon Jun 29, 2009 1:57 pm

Ok , thx
so, this is right?

import lejos.nxt.Motor;

public class Motoren {

public static void main(String [] args) {

Motor.A.setSpeed(720);
Motor.C.setSpeed(720);
Motor.A.forward();
Motor.C.forward();
try {
Thread.sleep(2000);
}catch (InterruptedException e) {
}
Motor.A.stop();
Motor.C.stop();

}

}
oensen
New User
 
Posts: 8
Joined: Mon Jun 29, 2009 12:25 pm

Postby paulmc » Mon Jun 29, 2009 3:02 pm

Looks OK, but the only way to be sure is to test it - that is, see if it makes your NXT behave as expected.
paulmc
New User
 
Posts: 23
Joined: Mon Jan 26, 2009 3:11 pm
Location: Sunderland, UK

Postby oensen » Mon Jun 29, 2009 3:58 pm

yes that´s clear
I can do that in our next lesson.


Is it very difficult to do the projekt with the ultrasonic sensor?
Does anybody have a finished java projekt for that
oensen
New User
 
Posts: 8
Joined: Mon Jun 29, 2009 12:25 pm

Postby ChrisB01 » Mon Jun 29, 2009 7:39 pm

You could always try stoping at a set distance from an object, or rotating in a circle then moving towards the nearest object.

Chris
ChrisB01
Advanced Member
 
Posts: 189
Joined: Sat Mar 15, 2008 12:19 pm
Location: UK

Postby oensen » Tue Jun 30, 2009 4:58 pm

How can I make the robot turn left if a obstacle is a half meter away?
oensen
New User
 
Posts: 8
Joined: Mon Jun 29, 2009 12:25 pm

Postby lawrie » Wed Jul 01, 2009 5:11 am

You should look at the lejos.navigation.Pilot class and the PilotTester sample as pilots are a simpler way to drive two motors.

To turn left if an obstacle is half a meter away, do something like:

Code: Select all
UltrasonicSensor sonic = new UltrasonicSensor(SensorPort.S1);

Motor.A.forward();
Motor.C.forward();
while(true) { // or some other condition
  if (sonic.getDistance() < 50) {
    Motor.A.backward();

    Thread.sleep(1000); // or however long you want to turn for

    // Now go forwards again
    Motor.A.forward();
  }
}


Add the try block to Thread.sleep or add your own sleep method that calls Thread.sleep in a try block, or add "throws InterruptedException" to your main method.
lawrie
leJOS Team Member
 
Posts: 843
Joined: Mon Feb 05, 2007 1:27 pm

Postby oensen » Wed Jul 01, 2009 12:09 pm

Thanks, how can i make the robot turn, as long as there is no obstacle?
oensen
New User
 
Posts: 8
Joined: Mon Jun 29, 2009 12:25 pm

Postby lawrie » Thu Jul 02, 2009 10:03 am

Try something like:

Code: Select all
Motor.A.forward();
Motor.C.forward();
while(true) { // or some other condition
  if(sonic.getDistance() < 50)  {
    Motor.A.backward();
    while (sonic.getDistance() < 50) Thread.sleep(50);
  }

  // Now go forwards again
  Motor.A.forward();
}
lawrie
leJOS Team Member
 
Posts: 843
Joined: Mon Feb 05, 2007 1:27 pm

Postby oensen » Thu Jul 02, 2009 2:26 pm

Code: Select all
import lejos.nxt.UltrasonicSensor;
import lejos.nxt.Motor;
import lejos.nxt.SensorPort;


public class Ultraschall {

   static UltrasonicSensor sonic = new UltrasonicSensor(SensorPort.S1);

   public static void main(String [] args) {
   
   Motor.A.setSpeed(720);
   Motor.C.setSpeed(720);
   while(true) {
     if(sonic.getDistance() < 50)  {
       Motor.A.backward();
       while (sonic.getDistance() < 50)       try {
         Thread.sleep(1500);
      }catch (InterruptedException e) {
      }
     }

      Motor.A.setSpeed(720);
      Motor.C.setSpeed(720);
   }

      }
}


I Have know made this class

But the Ultrasonic Sensor still don´t work, the nxt just drives backwards with motor A.
Do I have to make a Pilot class?
How can I Stop the Projekt while it is running on the nxt? Would it be Ok to add a block thread.sleep to the whole class with a sleep time of 2 minutes, so that the hole Programm stops after two minutes?
oensen
New User
 
Posts: 8
Joined: Mon Jun 29, 2009 12:25 pm

Postby paulmc » Thu Jul 02, 2009 3:02 pm

To interrupt a running program hold the orange and dark grey buttons down at the same time. The NXT will shut down - just press the orange button again to boot back to the menu.
paulmc
New User
 
Posts: 23
Joined: Mon Jan 26, 2009 3:11 pm
Location: Sunderland, UK

Postby oensen » Thu Jul 02, 2009 3:17 pm

Ok thx, thats much easier than taking out the batteries^^

Code: Select all

public class Ultraschall {

   static UltrasonicSensor sonic = new UltrasonicSensor(SensorPort.S2);

   public static void main(String [] args) {
   
   Motor.A.setSpeed(720);
   Motor.C.setSpeed(720);
   Motor.A.forward();
   Motor.C.forward();
   while(true) {
     if(sonic.getDistance() < 50)  {
       Motor.A.backward();
       while (sonic.getDistance() < 50)       try {
         Thread.sleep(1500);
      }catch (InterruptedException e) {
      }
     }

   }

      }
}


I have now tried out this class and it does work better. But if there is an obstacle the nxt just turns around endless, what do i have to change?
oensen
New User
 
Posts: 8
Joined: Mon Jun 29, 2009 12:25 pm

Postby gloomyandy » Thu Jul 02, 2009 3:32 pm

Try...
Code: Select all
public class Ultraschall {

   static UltrasonicSensor sonic = new UltrasonicSensor(SensorPort.S2);

   public static void main(String [] args) {
   
   Motor.A.setSpeed(720);
   Motor.C.setSpeed(720);
   while(true) {
     Motor.A.forward();
     Motor.C.forward();     
     if(sonic.getDistance() < 50)  {
       Motor.A.backward();
       while (sonic.getDistance() < 50)       try {
         Thread.sleep(1500);
      }catch (InterruptedException e) {
      }
     }

   }

      }
}



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

Postby paulmc » Thu Jul 02, 2009 3:38 pm

Since this is a school project I don't think we should just be writing your program for you! :)

First, it will help you work out what is going on if you keep your code neatly laid out with consistent indentation, like this:

Code: Select all
import lejos.nxt.Motor;
import lejos.nxt.SensorPort;
import lejos.nxt.UltrasonicSensor;

public class Ultraschall {

   static UltrasonicSensor sonic = new UltrasonicSensor(SensorPort.S2);

   public static void main(String[] args) {

      Motor.A.setSpeed(720);
      Motor.C.setSpeed(720);
      Motor.A.forward();
      Motor.C.forward();
      while (true) {
         if (sonic.getDistance() < 50) {
            Motor.A.backward();
            while (sonic.getDistance() < 50){
               try {
                  Thread.sleep(1500);
               } catch (InterruptedException e) {
               }
            }
         }

      }

   }
}


Now think about what's going on here. You set the motors going forward, then you keep going round a big "while" loop, each time checking if the distance to the nearest object is less than 50cm. If it is you then set one of the motors going backward (so the NXT turns) and go into a little "while" loop which continues while you're still less than 50cm from an object - when there is more space ahead you come out of the smaller loop.

What you need to do is work out at which point in the code to set the motor going forwards again once it has clearance ahead.

Incidentally, your "sleep(1500)" will wait 1.5 seconds (1500 milliseconds) between checks on the distance ahead. The NXT will spin a long way in that time, so you might want to reduce that interval so it doesn't miss a direction it could go ahead.

Hope this helps you get going!
paulmc
New User
 
Posts: 23
Joined: Mon Jan 26, 2009 3:11 pm
Location: Sunderland, UK

Next

Return to NXJ Projects

Who is online

Users browsing this forum: No registered users and 3 guests

more stuff