Wednesday 12 March 2014

SPIK3R operates with IR Beacon

Here is a small LeJOS program to demonstrate the use of IR beacon to operate SPIK3R. I would suggest you to go through my previous article SPIK3R With IR Sensor before going through the current article. I am operating the same robot (SPIK3R) with the Beacon here.  I would also suggest you to go through the operation of the EV3 IR Beacon with the link EV3 IR Beacon Tutorial. This tutorial will make you familiar with the concepts of Beacon channels, Beacon buttons etc. 

Let me explain my code below. My code consists of two classes EV3IRBeacon and InfraredSignalCheckerThread. The EV3IRBeacon class initializes the IR Sensor at port S1. It also spawns a thread InfraredSignalCheckerThread and passes the IR Sensor Object to it. The thread then initializes the large motor at port B and also works with the IR Sensor object as follows. The thread keeps listening to the IR sensor commands until the ESCAPE button is pressed. The IR commands are sent over Beacon channel 0. The remote command is received in the form of an integer (1, 2, 3) which represent the top-left, bottom-left and top-right buttons respectively. Pressing button 1, rotates the large motor (connected to port B), forward. Pressing button 2, rotates the motor backwards and pressing button 3, stops the motor. 

Below is a small clip to show the performance of the program (under construction ..).  

Sunday 9 March 2014

SPIK3R detects objects with IR sensor


Below is the LeJOS program for SPIK3R to detect objects with IR sensor provided with EV3 kit. This program makes SPIK3R crawl forward until it detects an object at a proximity of less than 35 cm. On detecting the object, SPIK3R crawls backwards until the object goes at a proximity of greater than 35 cm. Once the object goes out of its site, SPIK3R crawls forward again. There is a small video clip associated with this post to show the performance of the robot. 

NOTE: I have setup a Bluetooth connectivity from my PC to the robot to download the program wirelessly. However, it takes a long time (nearly 25 seconds) for the program to get downloaded onto EV3 brick. I did not understand the reason for this slowness yet. However, in essence, we will have to wait for 25 seconds to see how the program is doing!
 


I will explain my code to a certain extent below. My program consists of a single class EV3FirstProgram. This program starts with initializing the large regulated motor connected at port B. I have set the speed of the motor to be 90 degrees per second. It looks like the SPIK3R moves very slowly with this speed. However, you can always try increasing the speed to get a more thrilling experience! Let's move ahead with the program. I have initialized the port S1 where the infrared sensor is connected. Using LeJOS, the infrared sensor can be initialized in two modes Distance and Seek modes.  The Distance mode can be used to detect the presence of an object in the robot's proximity. The Seek mode is used along with the remote control (also called as beacon) to capture the infrared signals sent by the remote control. In this program I have used the Infrared Sensor's Distance mode.

The program runs in a continuous while loop which runs until the ESCAPE button is up. That means when we press the ESCAPE button, the robot will stop its execution. Next, I have provided a delay of 2 seconds. I have driven the motor forward in the continuous while loop. That means moving forward is the default behavior of the motor. What other things can be stated as default behavior of the robot? Let's see ... The robot collects 5 samples of distance from the infrared sensor and calculates the mean of them using the MeanFilter. This mean is stored in the variable dist. 

Now let's see how far this default behavior continues. It continues until an object is sighted in a proximity of 35 cm. When an object is sighted, the robot enters into the inner while loop and the robot's behavior changes. It starts going backwards. While going backwards, the robot keeps fetching the samples from the infrared sensor. It calculates the mean of 5 samples (similar to the case of moving forward). It checks whether the mean of 5 samples is greater than 35 cm. If the mean is greater than 35 cm (since the robot is moving backwards from the object), the robot will come out of the inner while loop and starts executing the outer while loop. Which essentially means the robot will starts moving forward again! We have considered the averaging of 5 samples in order to smoothen out any spikes from the Infrared sensor readings. 

That's how the robot will have an oscillating behavior of going forward and backward again! Please execute the code below and see the performance of the robot at your end! 
  
I hope you enjoyed this article!