very simple lejos project

Post your NXJ projects, project ideas, etc here!

Moderators: 99jonathan, roger, imaqine

very simple lejos project

Postby zoli0726 » Thu Nov 25, 2010 11:50 am

hello, i need some help with my first lejos project.
I need to drive a robot in a quare which is bordered with white belt. i have the idea, but i dont know how to write it in java. i read some articles and a full presentation but its too hard to me to understand in my own way. i need to drive the robot straight to the belt and when its close it needs to slow down, and turn to left. so i use the ultrasonic sensor and when distance <like 50 cm, its slows down, and when distance <25 cm it turns to left.
anybody could help me to write it with lejos with some comments please?
zoli0726
New User
 
Posts: 2
Joined: Thu Nov 25, 2010 11:35 am

Postby s.frings » Sun Nov 28, 2010 4:02 pm

Hello zoli,

The Lejos tutorial describes how to control the motors and the in projects directory you can find an example program for the Ultrasonic sensor.

I'm pretty sure that nobody will write a complete program for you, so think about it and ask more detailed questions.

You could call this in a loop while you drive towards the boundary:

Code: Select all
int distance=myUltrasonicSensor.getDistance();
if (distance<50) {
    int remaining=distance-10;
    if (remaining<0) {
        Motor.A.stop();
    }
    else if (Motor.A.getSpeed() > remaining*10) {
        Motor.A.setSpeed(remaining*10);
    }
}


This would slow down the robot from in the range 50-10cm and then finally stop it.
s.frings
Active User
 
Posts: 131
Joined: Tue Jul 20, 2010 2:01 pm

Postby zoli0726 » Mon Nov 29, 2010 2:57 pm

hello . i studied a little java, so i can start writing my project.
Code: Select all

package robot;

import lejos.nxt.*;
public class Robot
{
 
   public static void main(String[] args ) throws Exception
   {
   UltrasonicSensor sonic = new UltrasonicSensor(SensorPort.S3);
        int tavolsag=sonic.getDistance();
while (sonic.getDistance() >10) {
   Motor.A.setSpeed(400);
        Motor.B.setSpeed(400);
        Motor.A.forward();
        Motor.B.forward();
if(sonic.getDistance()<30 {

Motor.A.Stop();
Motor.B.Stop();
Thread.sleep(1000);
Motor.A.rotate(-90,true);
}
   }
 


}
}


i doesnt have a robot, so i cant test it anytime, just once in a lesson.
i feel that its not good totaly. i want to ask you , where are problems?
zoli0726
New User
 
Posts: 2
Joined: Thu Nov 25, 2010 11:35 am

Postby s.frings » Tue Nov 30, 2010 7:24 pm

The conditions overrule each other.

You set the speed to 400 if the distance is >10cm. The you stop if the distance is smaller than 30.

Be aware that you might get different values when you read the sonic sesor multiple times withing a loop. Be also aware that reading sensors takes a lot of time. So better read it only once.

I would recommend:

Code: Select all
int distance=sonic.getDistance();
while (distance >30) {
        Motor.A.setSpeed(400);
        Motor.B.setSpeed(400);
        Motor.A.forward();
        Motor.B.forward();
}
else if (distance<10) {
       Motor.A.Stop();
       Motor.B.Stop();
       Thread.sleep(1000);
       Motor.A.rotate(-90,true);
}



You may use Sound.pause(...) instead of Thread.sleep(...) because sound.pause does not throw exceptions.

Motor.A.rotate(-90,true) does not rotate the whole robot by 90 degrees. It rotates only a single wheel by 90 degrees, which is possibly not what you want because the robot will still face the wall (except when your wheel are very large).

To rotate the robot by 90 degrees you would need some calculation or use the Pilot (or DifferentialPilot) class which does that work for you. But be aware that the Pilot classes do not work properly if you set the speed value repeatedly. If you use them, you should set the speed only once outside your loop or use System.currentTimeMillis() to check the timing and ensure that you set the speed not more often that 3 times per seconds. If you set the speed too often using a Pilot class, the robot will drive with incorrect speed and bucking.

}
}
s.frings
Active User
 
Posts: 131
Joined: Tue Jul 20, 2010 2:01 pm


Return to NXJ Projects

Who is online

Users browsing this forum: No registered users and 0 guests

more stuff