Sunday, 29 December 2019

EV3 Line Follower With Obstacle Avoidance

In this post, we have implemented a Line Follower, which has the ability to avoid obstacle placed on the line. The Line Follower is an inner edge follower implemented using PID control algorithm. We expect the Line Follower should keep following a threshold value of light. This threshold, is calculated by averaging the Black and White light value of the surface. We have also calculated the Proportional, Integral and Derivative gain of PID algorithm which keeps the robot on the black line. The program works with an IR Beacon which allows us to control the speed of the robot by applying 0 power (to stop), 20% power, 35% power and 45% power. Each of the power values, need different set of PID gains. While following the line, if the ultrasonic sensor detects an obstacle at a distance of 20cm, it takes a turn of 38 degrees with the help of Gyro sensor. The robot then goes forward for 3.5 seconds in 38 degrees direction. Then the robot take a turn of 90 degrees and moves forward, until it finds the black line. It starts moving on the line again. A small video clip showing the performance of the robot is shown below. Also, we have attached the LeJOS program which simulates this behavior of the robot. However, there are few limitations for this program. They are listed below.

  • Currently the program is not enough generic. That means, it does not take into account the readings that the gyro sensor gives, after having one complete round across the oval shaped black line. If the robot takes a complete turn and approaches the same object again, we do not guarantee that this program will function well. We are still working on it. 
  • We have not yet experimented with multiple objects placed on the oval shaped line. We are assuming that the program should work, in case if multiple objects are in a single straight line. 

NOTES: 
  • It would beneficial if you go through the following article, before going through the program. It will explain to you the PID Controller and Line Following concept in detail.
  • The gyro sensor returns positive values of the angle in degrees when it turns clockwise and it returns negative values when it turns anti-clockwise, based on gyro sensor mounting over the robot. We are moving the robot 38 degrees (we came up with this angle measure purely empirically) to move it away from the obstacle. We are making the robot move ahead for 3.5 seconds and then turning it 90 degrees in the other direction to make it meet the line again. We realized that by moving it in 90 degrees, the robot touches the obstacle at some point. Which is not right! In order for this not to happen, we should turn the robot less than 90 degrees and let it meet the line somewhat ahead of the obstacle.
  • The LimitAngle variable is set to 320 + 38 at the end of the loop. This is because with each of the completion around the oval shaped figure drawn with the black line, the angle returned by gyro sensor keeps on increasing by 360 degrees. So, in case we want the robot to go for another round and come across the same obstacle again, the program should work! However, as we mentioned above, we have not tested the program to face the obstacle again after completing the oval path.  



EV3 Limited Wanderer Robot

This post is about a robot, which wanders in a limited area. In this project, we have marked an oval shaped area with a black line. We calibrate the robot with white and black values by making a manual movements on the surface. We then take the threshold (average of black and white values) and expect the robot to turn, when it reaches the threshold. That's the reason, the robot turns 90 degrees, when it reaches half way through the black line. The robot continues the behavior of making turns after finding a black line and keeps itself within the oval. A small clip showing the behavior of robot is shared below. You can also find the LeJOS program, which simulates this behavior, shared below.

In this LeJOS program, we have a single class called LtdWanderer. In its main method, this class initializes the large regulated motors on port B and C. It also initializes the two sensors color sensor and gyro sensor attached to port S4 and S2. The color sensor is initialized in Red mode and gyro sensor in Angle mode. The program then stores the calibrated values of light intensity reflected from the black and white surface. The LeJOS API returns the light intensities in the range 0 to 1. We have multiplied them by 100 and calculated the average light intensity of black and white for further programming logic.

The default behavior of the robot is to keep moving forward with a speed of 180 degrees per second. While moving, it also fetches the samples from the color sensor continuously. The robot checks if the light intensity is greater than average light intensity. If yes, the robot is free to wander further because it is on a white surface. However, when the light intensity goes below the average light intensity, the robot concludes that it is on the black boundary, and it make a left turn to be within its limits. 

NOTE: With each turn the LimitAngle variable increases its value by 90 degrees. We ideally should have reset the sensor and measure the angle each time the robot makes a turn, to check if it is equal to 90 degrees. However, we found that the reset function did not work as we were expecting. The angle measured by the Gyro sensor kept on increasing as and when the robot takes a turn. Hence the purpose of the LimitAngle variable. With the LimitAngle variable, we are checking whether the difference between previous turn angle and the current turn angle is equal to 90 degrees. We then stop the robot from making a turn and let it go straight and wander. 

The robot wanders infinitely until ESCAPE button is pressed.         




Monday, 28 October 2019

Lego EV3 Guided Car Parking

Here we have implemented Guided EV3 Car Parking. This car is a line follower which is implemented using a light sensor. The car follows the line and reaches the "statues" which indicate that there is an existence of car parking slot in front of them. The slots may be empty or occupied. Car intercepts these statues using an Infra-red sensor. Once the car intercepts a statue, it tries to find if there is a car already parked in front of the statue. Car recognizes this with the help of Ultrasonic sensor. If the parking is already occupied, the car moves on, in search of an empty parking. Once it finds an empty parking, the car parks itself in that parking place.

As you must have noticed, the three statues are erected in the left of the road (the thick black line) and the car parking is on the right of the road. The three statues, correspond to three parking slots. To intercept the statues, the infra-red sensor is mounted on the back-left of the car. While the ultrasonic sensor is mounted on the back-right of the car, to detect whether the parking slot is empty or a car is already parked on the slot. Once an empty car parking slot is detected, the car takes a right turn to park itself. The gyro sensor is mounted on the front-right of the car and helps the car to turn in 90 degrees.  

NOTE: In order to understand the line follower mechanism, it would be good if you read the following article: EV3 Line Follower

Let me explain the program for guided car parking in more detail. The program is implemented in LeJOS programming language. We have a single class called CarkParking. The main method of the program begins with initializing the motors and the sensors. The four sensors, ultrasonic sensor, infrared sensor, color sensor and the gyro sensor are connected to ports S3, S1, S4, S2 respectively. They are initialized in Distance mode, Distance mode, Red mode and Angle mode respectively. The modes of all the sensors are almost self explanatory, except the Red mode of the color sensor. The Red mode returns the intensity of the reflected Red light from the black/white surface. On initializing all the sensors, the line follower program begins and we follow the following algorithm. 

  • Follow the line
    • Check whether the infrared sensor sample is less than 30cm. 
      • If yes, we know that there is a statue present near the car. Then we go ahead and check if ultrasonic sensor sample is greater than 50 cm. 
        • If yes, we know that the car parking is empty. Then we go ahead and check if gyro sensor sample is greater than -90 degrees
          • If yes, we keep taking a right turn until the gyro sensor sample reaches -90 degrees. 
          • If no, continue to follow the line       

Assumption: This program assumes that the automated car is necessarily searching for car parking slot and does not want to continue driving on the road without any need for parking. 




Thursday, 17 October 2019

EV3 Stair Climber (Climbs Stairs of Size 15 cm)

I have assembled the Stair Climber robot as per the instruction manual given here. In this post, I am explaining the details about the robot which I understood. The front part of the robot is a complete "car". This car is attached with two Lego pins, to the rotating belt. A motor is driving the rotating belt to elevate the back wheels and middle wheels of the car. If the belt rotates anti-clockwise, middle wheels come up. If it rotates clockwise, the back wheels come up. The back wheels of the robot, are for keeping the robot stable when all 4 wheels of the car are up in the air. Front wheels are moving with high speed and they are climbing up due to thrust. There is a "gyro sensor" attached to the robot which made me know how much the robot is oriented in "space". It gives me the angle with which the robot has elevated from flat surface. When this angle goes above a certain threshold, I elevate the middle wheels. That's how the entire car goes up. Once the car reaches in a horizontal position on a stair, I bring up the last pair of wheels. To make things clear, there are two phenomena happening in this assembly: Driving and Lifting of the wheels. The following points are worth noting about the robot movements. 
  • The medium motor and one large motor are responsible for driving the back and front wheels respectively to move the robot forward
  • Another large motor is responsible for lifting the middle and back wheels 
  • The middle wheels are not driven, they are only lifted. They are present for proper balance of the car.
The A small video clip showing the performance of the robot is given below. Also, I am sharing the program for the robot movements. Let me explain the program in detail: The program begins with initializing all the three motors. The motor which drives the back wheels is the medium motor connected to port A. It is initialized with speed of 450 degrees per second. The motor which lifts the middle wheels and back wheels is a large regulated motor connected at port D. It is initialized with speed of 180 degrees per second. And lastly, the motor which drives the front wheels is the large regulated motor connected at port B. It is initialized with speed of 630 degrees per second. The front motor is intentionally set at a high speed to achieve a large thrust from the stair. The large thrust, help the front wheels move up on the stairs. The speed of the rest of the two motors is initialized based on observations about how fast/slow the motors should move. 

The touch sensor is connected at port S2 and initialized in Touch mode. This initialization helps in identifying whether the front button on the touch sensor is pressed. However, I am not making use of the touch sensor in this program. Ideally it could be used to stop the robot when it climbs all the stairs. I am programmatically stopping the robot after 2 stairs. The gyro sensor is connected at port S3. It is reset and then initialized in Angle mode. The angle mode returns the rotation angle in degrees since last reset. 

The main program begins with moving the front and the back motors forward with the function moveTheRobotForwardWithFrontAndBackWheels(). (NOTE: if you dig deep into the function, you will realize that if I rotate the motors backwards, the robot moves forward. It looks like there is some issue with my connections or the robot wheel alignment. However, apart from this issue, there is no cause of concern with the functioning of the robot further.) In the process of moving forward, the robot also starts collecting the Gyro sensor and the Touch sensor samples. If the Gyro sensor sample is less than -15 degrees (please remember, our robot is rotating counter-clockwise while climbing up the stairs, which will result into negative Gyro sensor angle.), two things happen: First, the speed of the lifting mechanism and the speed of the back wheels changes to 630 and -121 degrees per second respectively. (NOTE: the present name of the method is changeTheSpeedOfBackAndMiddleWheels(). Instead the name should be changeTheSpeedOfLiftingAndDrivingOfWheels(). The present name is causing a lot of confusion. However, if we rename the method and use it, the confusion will be gone!) I have kept the speed of the back wheels in negative, which will drive the back wheels backwards. This method results in front wheels moving forward, back wheels moving backwards and middle wheels getting lifted. This movement does not allow the robot to topple. The following little figure will make these movements clear. 



Please also note that the forward speed of the front wheels (630) is much more than the reverse speed  of the back wheels (-121). So in essence, the robot will move forward. However, because of the negative/backward speed, it will be protected from toppling from the stairs. Second, the middle wheels get pulled up until the gyro sample is negative. 

Once the gyro sample turns positive, the pull up of the middle wheels will stop and the flag singleStepDone will be set to true. There are certain steps to be performed once single step is done. They are: 
  • Make the robot move forward on the stair until the front and the middle wheels set properly on the stair
  • Back wheels which were on a negative speed and moving backwards as shown in the above figure, are now set to move forward with a positive speed of 540
  • The back wheels are lifted by moving the large motor clockwise. Please take a look at the two methods for pulling up the back wheels (method: startPullUpTheBackWheels()) and for pulling up the middle wheels (method: pullUpTheMiddleWheels()). You will find that they rotate the motors in opposite direction as explained in the first paragraph of the article. 
  When all the steps are done, the robot will stop by stopping all the three motors. 



Thursday, 28 March 2019

EV3 - Light Follower

This is a post for EV3 Track3r light follower. Track3r has an attached color sensor which helps it to follow the light. In the video, the kids are projecting a focused light beam with a torch, over the color sensor attached to the robot. The rest of the room is dark. When the robot follows the light, the resulting effect is like, the kids are pulling the robot with the light (without any physical connection to the robot). In my opinion, it is an interesting effect! 

NOTE: It would be good if you read the article for EV3 - Line Follower, before continuing to read the below article. 

For the light follower to function, calibration of "Bright Light" and "Dark" intensities has to be done. The actual algorithm to follow the light is influenced by Jacek Fedorynski's Line Follower NXC program. The LeJOS (Java) program has one class "LightFollower". This class initializes the color sensor and calibrates the sensor for "Bright Light" and "Dark" values. Further, this class has the algorithm to follow the light - given the calibrated values and the light intensity at any given point in time. The rmotors are powered based on the difference between the calibrated values and the light intensity at a given point of time. Below is a small video clip for Light Follower function.






Friday, 25 May 2018

EV3 Banner Printer

We have developed an EV3 Banner Printer based on the instructions given here. We developed a Java (LeJOS) program to make it operate according to our requirements. Below is a small clip which will demonstrate the printing of various characters. We have also posted the gist of Java program which we developed. This Java program has an array called "myString" which stores the entire set of characters that we want to print. 

Let me give you a brief introduction of this array. The main objective of the program was to operate with the help of a Infrared beacon (For Example: Print "P" when beacon is on mode-3 and button-4). However, testing frequently with the help of beacon was quite time consuming. Hence we have put the entire list of beacon characters in "myString" array. 

NOTE: Below is the gist of how "myString" array looks like
myString[0][0] = 2;  // This is the mode number of the first character
myString[0][1] = 4;  // This is the button number of the first character
myString[1][0] = 2;  // This is the mode number of the second character
myString[1][1] = 1;  // This is the button number of the second character
and so on ... 

Once the string is stored inside the array, we can iterate it over the while loop and start printing the characters one by one. The job of printing any characters is very simple. Rotate the three motors as per requirements. In our case, Large Motor C is doing the job of moving paper as the characters get printed. Large Motor A is doing the job of moving the pen up and down. Medium motor is doing the job of moving the pen from bottom to top of the paper and vice-versa. 

Some key points to remember:
  • Each character is printed 180 degrees apart from each other horizontally, where 180 degrees is the rotation of large motor C. The motor C is responsible for the horizontal movement along the paper's length
  • Each character is printed within 90 degrees to 270 degrees rotation of medium motor D. The medium motor D is responsible for the vertical movement of the pen along the paper's breadth. Here, motor's rotation to 90 degrees correspond to bottom most position along the paper's breadth and 270 degrees correspond to topmost position along the paper's breadth 
  • When the large motor A is rotated through 160 degrees, the pen touches the paper and the character starts printing. And when the motor is rotated through 360 degrees, the pen is lifted up and the printing gets disabled  
Now let me explain the way we print the letter A as an example:
  • The character begins at the position begin = (character - 1) x 180 degrees. The motor C is rotated to this position with the statement ev3lmotorC.rotateTo(begin)
  • Medium motor D is placed at its lowest position on the paper by rotating the medium motor through 90 degrees. This is achieved by the statement ev3mMotorD.rotateTo(90)
  • Large motor A is rotated through 160 degrees by the statement ev3lmotorA.rotateTo(160). This brings us to position A of the character A (based on the below figure)
  • Then comes the drawing of slanting edges of the letter A. The drawing of slanting edge is achieved by incremental movement of motors C and D which move the pen horizontally and vertically respectively. As a result, if you closely observe, the letter A looks like in the below image

 
  • There are total 10 increments through which the motors move and draw the edges. The motor D is moved through increments of 18 degrees vertically, that helps it reach the maximum of 270 degrees of character height in 10 increments. While motor C is moved through increments of 5 degrees that will help it reach 45 degrees horizontally, when it is at the character height. A similar principle is applied when the downward slanting edge is drawn. This exercise brings us from point A to B to C   
  • Now it is the time to draw the horizontal middle line of the letter A. But before jumping into drawing the line, we have to know where we are, with respect to the entire drawing of letter A. We are at point C. At this point we have to lift the pen, by rotating the motor through 360 degrees. We lift the pen so that nothing gets printed unless we want it
  • We then bring the pen at the position M, by rotating the motor C horizontally through an angle begin + 90 - 68. Also we need to rotate the motor D vertically by 180 to reach the point M
  • Now rotate the motor A through 160 degrees, to get the pen in contact with the paper. Then rotate the motor C horizontally through an angle begin + 90 - 23. This will bring the pen at position N. 
  • The next steps are to wrap up the printing of the character, by brining the pen from position N to position C.  
    • Lift the pen up by rotating the motor A through 360 degrees
    • Rotate the motor C through begin + 90 degrees and motor D through 90 degrees to reach the point C. With respect to point C, the next character will be drawn. 
For us, it was a lot of joy to work on this printer. Hope you will also enjoy reading the article and also building it. 


Monday, 7 May 2018

Our Pet Spik3r

Below is a story of our pet Spik3r, which we have captured in the form of a small video clip. Our Spik3r was moving on as usual and we were about to give it some food (the bug). However, our Spik3r sensed some danger at that point of time and shot the bug. It started running backwards and stopped after some time. Enjoy the visual. (The Java program is also shared along with the visual).

Based on the mechanical assembly of the Spik3r, the large motor D is used to drive the robot forward. Another large motor A, is used to implement the sting. While the medium motor is used to implement the claw to catch the food (the bugs). The IR sensor is mounted on the front part of the robot in order to sense the existence of any danger. 

The program to drive the robot Spik3r consists of a single class, Spik3r2. It has a main method, which initializes all the three motors and the IR sensor in Distance mode. When the ESCAPE button is un-pressed and the IR Sensor does not find any object (danger) in front of it in a range of 20 cm, the robot keeps moving forward with the large motor D (which rotates with a speed of 400 degrees per seconds) and it also keeps moving its claw to catch the bug. The claw movement is done by rotating the medium motor through 220 to -220 degrees. The IR Sensor distance samples are fetched continuously while moving forward.  

If the robot locates a danger (our hand which is placing the bug in front of the robot) in less than 20 cm distance, it starts running backwards with a speed of 990 degrees per seconds. While running backwards, it stings the dangerous object (in the case of Spiker, it shoots a ball towards the dangerous object). The sting is caused by setting the large motor A at a speed of 640 degrees per second and rotating it through 600 degrees. The IR Sensor keeps fetching the Distance samples and as soon as the dangerous object goes out of sight, the robot starts moving forward again (unfortunately this forward movement at a later stage has not gotten captured in the video).





Tuesday, 17 April 2018

EV3 Track3r Line Follower

This is a post for EV3 Track3r line follower. Track3r has an attached color sensor which helps the track3r to follow the black line on the white surface. For the track3r to function, calibration of white and black color has to be done (which is not captured in this video.) The actual algorithm to follow the line is influenced by Jacek Fedorynski's Line Follower NXC program. The LeJOS (Java) program has one class EV3LF. This class initializes the color sensor and calibrates the sensor for Black and White values. This class has the algorithm to follow the line - given the calibrated black and white values and the light intensity at any given point in time.

The detail process of calibrating Black and White values is as follows. We can hear two beeps and the robot waits for press and release of ESCAPE button. While the robot is waiting, we have to manually adjust the color sensor such that the red light emitted by the color sensor will be focused over the white surface. Then press and release the ESCAPE button. This is the instant when the robot will fetch a sample from the color sensor. This sample will act as the threshold value for white color, which will be printed on the screen. 

Then, we can hear two beeps again and the robot waits for press and release of ESCAPE button. While the robot is waiting, we have to manually adjust the color sensor such that the red light emitted by the color sensor will be focused over the black line. Then press and release the ESCAPE button. This is the instant when the robot will fetch a sample from the color sensor. This sample will act as the threshold value for black color, which will be printed on the screen. 

We then initialize the motors B and C which will drive the robot forward. We power the large motors such that the power of each motor is a function of average light ((white + black)/2), instantaneous value of light (color[0]), white threshold and black threshold. We use this power to drive the motors forward. Let's analyze the formulae to calculate the power for the two large motors in detail. 






  • The polarity of the second term is opposite for both the motors to calculate the speed requirements (as indicated by the RED circles). Due to the opposite polarity, the cSpeed and bSpeed function in opposite manner. That means, when cSpeed is positive, bSpeed is negative and vice-versa. Hence when the robot is on the black surface, it gets pushed on to white surface and when the robot is on white surface, it gets pushed on to black surface. Thus the robot moves in a zig-zag manner along the line. However, since the zig-zag movement is very subtle, it appears to be smooth. 
  • The defaultPower is set to a constant value, which can be tuned based on the speed requirements.
  • The multiplyingFactor is set to a constant value, which can be tuned such that cSpeed and bSpeed will function in opposite manner. 












Thursday, 12 April 2018

Dinor3x

Dinor3x is a creation from Lego Mindstorms EV3 Fan robots which we attempted to program. This robot is a four legged robot. It starts walking as soon as it is powered on. When an object comes along its way (as detected by infrared sensor), it opens its mouth and acknowledges its presence. Please find the below code as well as a small video clip to see the performance of the robot.


This program consists of two classes. Dinor3x and OpenMouth. Let me explain the functionality of both the classes. Dinor3x is the class which has the main method. This method starts a thread OpenMouth. It also initializes both the large motors which control the legs of the Dinor3x. Based on the assembly of the Dinor3x, it looks like when I move the motors backwards, the Dinor3x moves forward. The Dinor3x moves forward, until ESCAPE button is un-pressed and the turnFlag is OFF (turnFlag represents the condition when there is an obstacle in front of the Dinor3x. I will explain it in detail while explaining the OpenMouth class). When the turnFlag is ON, that means when the Dinor3x mouth is open, both the large motors stop. As a result, the Dinor3x stops. 

Let's talk about the OpenMouth class now. Based on the assembly of the robot, we know that the medium motor controls the opening and closing action of the mouth. Also, the IR sensor in the front part of the Dinor3x activates the opening and closing action of the mouth. When we keep an obstacle (our hand) in the path of the robot, the IR sensor captures the presence of the obstacle. To capture this, the IR sensor has to be in Distance mode. The IR sensor then fetches a single sample of the distance (the distance of the obstacle in front of the robot). If this sample is of distance less than 20 cm, we make the Dinor3x open and close the mouth by moving the motor through 45 and -45 degrees respectively. If you carefully observe the movement of the mouth, the mouth quivers a little after closing. This phenomenon is achieved by rotating the medium motor through 2 and -2 degrees for 4 times. Once the object goes out of site, the medium motor is reinitialized to an angle of 45 degrees and the thread ends. 
 
Meanwhile, the turnFlag is turned on as soon as the object is sensed and it is turned off as soon as the object goes out of site (with more than 20cm distance). This is the same turnFlag which we use in the main method of Dinor3x class to stop the Dinor3x from walking when the object gets sensed.
 
      



We have also implemented the Dinor3x revolving around itself. The video of the revolving process is posted below. However, we have not posted the LeJOS code for the revolving Dinor3x. We suggest you to take this as an assignment. Believe us, it is going to be an interesting program! 





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!