LeJOS Programming

I decided to come up with few LeJOS programs to keep up with Java programming practice. As a part of my decision, this is the first program that I developed. With this program the robot detects objects using ultrasonic sensor. Whenever an object is located, the robot stops. It then takes a couple of ultrasonic sensor readings by rotating towards left and right. Based on the reading, the robot decides which way to go to. Please take a look into the LeJOS program below. Also, take a look at the couple of video clips to know the performance of the program.

In my program, the robot is a small car which is built using small booklet provided with NXT kit. The LeJOS firmware for NXT can be downloaded and installed on the NXT brick from LeJOS NXJ 0.9.1. My LeJOS program has four classes: DetectObject, DriveForward, NXTCar, NXTUtils. The NXTCar class has the main method. This main method starts two threads DriveForward and DetectObject. 

The DriveForward thread has its own run method which keeps the robot moving forward with the forwardInitialize method. The robot moves forward in a stretch of 10 seconds (10000 milliseconds) and gets interrupted by the thread DetectObject, which performs the task of detecting an object. On detecting an object, an interrupt occurs. Due to this interrupt, the interruptBlock method gets executed. The interruptBlock method invokes getForwardDirection method to decide the forward direction to move. This method makes the robot turn to left and right and gathers the ultrasonic sensor readings in both the directions. That means it measures the distance from the object in the left and right direction. If the distance to object in the right direction is greater than the distance to object in the left direction, the robot turns right and vice versa. That means, the robot moves in the direction where it gets more space to move (either on the left or on the right). If the forward direction is LEFT_TURN (-1), the robot takes a left turn, while if the forward direction is RIGHT_TURN (+1), the robot takes a right turn in the interruptBlock method.  

Next, let us go through the DetectObject thread. This thread has its own run method. In the class constructor the thread gets a handle over the other thread, which is DriveForward thread. This handle allows the DetectObject thread to interrupt the DriveForward thread on detection of an object. Let's see how these methods/classes/threads interact with each other. The run method of the DetectObject thread, gets the average ultrasonic sensor reading in a continuous manner. If the average distance is less than 60cm, the DetectObject thread interrupts DriveForward thread. In other case (where the average distance is greater than 60cm), the DetectObject thread sleeps for 20 sec (20000 milliseconds) and then starts gathering the ultrasonic sensor readings again.  

Next let us go through the NXTUtils class which has getAvgUltrasonicSensorReading method which averages ultrasonic sensor readings over 5 samples. 







Hope you liked this article!

No comments:

Post a Comment