Tuesday 31 July 2012

Move Forward Until Occurrence Of Black Boundary

Now, let's give the robot a restricted space to move. The robot here is the small car which we can build by following the small booklet provided with the NXT kit. Let us draw a black line around the robot's space, to identify its boundary. When the robot touches the black line, it is supposed to take reverse and try another time to escape the boundary. The question is how the robot notices the black boundary? Yes, we have to use a Light Sensor. Light Sensor measures the light intensity in a room and converts it into meaningful values. It passes the values to the NXT. NXT can then use a calibration program to convert the light sensor values into minimum and maximum. Calibration process allows the sensor to get the readings within an acceptable range which then makes programming easier. Please take a look at below video to see the different ways provided by NXT to calibrate the light sensor. You can go ahead with any one way and calibrate your sensor. 


Let's start to write a program which then uses the calibrated values to understand robot's boundary. It is going to be a two-step program. 
  • Keep moving forward until the robot hits a black boundary line (Light Sensor helps to identify the black boundary line)
  • When robot hits the black boundary line, drive backwards for 2 seconds
Keep repeating the above steps and observe robot’s behavior. Now let's program these steps with the help of NXT-G programming language. Drag and Drop the Switch Block from the left Palette to the Work Area. This Switch has two parts, the upper part and the lower part. The upper part controls the unusual behavior of the robot. The lower part controls the default behavior of the robot. Now, click on the Switch and see its configuration panel at the bottom of the screen. Please select the below configuration fields and their values.

Control - Sensor
Sensor - Light Sensor
Port - The port number where light sensor is connected (in my case port 3)
Light - Greater than 20 (which means when the light intensity exceeds 20% of the maximum value)

This configuration means that the light sensor will consider darkness when the illumination is about 20% of maximum and it will consider brightness when the illumination exceeds 20% of maximum. Please take a look at the below figure to see my configuration of Switch Block. 


As a next step, let us drag and drop a Motor Block in the upper part of the Switch. This Motor Block will get activated when the robot finds brightness. Click on the Motor Block and see its configuration panel at the bottom of the screen. I would like the robot to keep moving forward until it is in brightness. I selected the motors B and C in order for robot to move forward for unlimited duration. Keep rest all the configuration the same. Please see the below configuration of my Motor Block on detection of light. 


Next, let us drag and drop another Motor Block in the lower part of the Switch. This motor block will get activated when the robot encounters the black line. Click on this Motor Block and see its configuration panel at the bottom of the screen. Since I would like the motor to go reverse for 2 seconds on occurrence of the black boundary, I have selected Motor B and C of the robot, with Direction field to be reverse and Duration to be 2 seconds. I kept rest all the fields to be default. Please take a look at the figure below to check my configuration of the Motor Block. 



Further, configure the robot to keep moving between light and dark indefinitely. In order to achieve this please put the entire configuration above in a Loop. Click on this loop and see its configuration at the bottom of the screen below. Please select the Control field to be Forever. Please see my final NXT-G program below. 
 


Download this program on the NXT brick and see the movement of the robot. The below video clip captures the movement of my robot. 



As seen in the video, the light sensor is mounted on the front part of the robot, about 2 mm above from the surface and has a red light emitted out of it. The robot touches the black boundary line twice and then reverses back for 2 seconds. 

For learning about Lego NXT blocks please visit the site below. 

Hope you enjoyed the article!

 

Monday 23 July 2012

Roaming With Ultrasonic Sensor

In the last post, we used the touch sensor to detect an object. However, touch sensor responds on colliding with an object. Colliding could harm our robot (The robot here is the small car which we can build by following the small booklet provided with the NXT kit). So, let's use an ultrasonic sensor. Ultrasonic sensor detects an object when it is at a certain distance from the object. Thus ultrasonic sensor gives a much cleaner object detection. The robot follows the following rules while moving around in the world. 
  • If there is no object in 50 Inches distance in front of the robot, the robot keeps moving on 
  • Once an object is detected the robot takes following two steps
    • Get the robot reversed for 2 seconds
    • Turn the robot away from the obstacle by turning the motor 180 degrees
Now, let's take a look into the NXT program to achieve the above behavior.

At first, drag and drop the Switch Block from the left palette to the work area. The Switch Block has two parts. Upper and Lower part. The upper part gets enabled when the object is detected within 50 Inches limit. The lower part gets enabled as a default function of the robot. Click on the Switch and you will get to see its Configuration Panel at the bottom. In the Configuration Panel, please select Control field to be Sensor. Further, choose Sensor field as Ultrasonic Sensor. Select the port where Ultrasonic Sensor is attached to the NXT brick. In my case it was Port-4. Select the Compare field to be of Distance less than 50 (it is a little mathematical expression) and its unit to be Inches. That means the ultrasonic sensor will detect an object if it is in the range less than 50 Inches. Please take a look at my configuration in the below figure. 





Now let us program the first step on object detection. The car gets reversed for two seconds. In order to achieve that please drag and drop the Motor Box on to the Work Area within the upper part of the Switch. Click on this Motor Box. The Configuration Panel of the Motor Box will appear at the bottom of the screen as shown in the figure below. I am using Motor B and Motor C to drive the car. So I have selected the motor options accordingly. Since I want to drive the motors reverse, I have chosen the Direction option to be reverse arrow. Choose the Duration to be 2 seconds. Keep rest all the options as default. 



Let's program the second step of object detection, which is to take a turn to move away from the object. To do this, please drag and drop the Motor Box from the left Configuration Palette to the Work Area, just next to the previous Motor Box. This will make the previous and the current actions execute sequentially. Click on this Motor Box to get its corresponding configuration at the bottom of the screen. As shown in the below figure, this box is configured to take a turn to move away from the obstacle. To take a turn (in place), enable a single motor, in my case Motor B. Please select the Duration option to be 180 degrees. That means the servo motors of the robot will be programmed to take a 180 degrees turn. Please remember, the servo motor turn is very different from the 180 degrees turn in the physical space. To know more about Lego NXT Servo Motor design, please visit 




Now configure the Motor Box to take a default action of moving forward, when there is no object in the limit of 50 Inches in front of the car.  Place this Motor Box in the lower part of the Switch box as shown in the below figure. Click on the Motor Box to get its configuration at the bottom of the screen. Since we want to keep the motor freely moving forward in the default condition, we will select the Motor B and Motor C with the Direction to be Forward Arrow. The Duration is selected to be Unlimited and rest all the configurations are kept as unchanged. 



As the last step, we need to put the Switch Box in a Loop, which will allow us to run all the configurations inside the loop forever. Please drag and drop the Loop from the left palette and place it around the Switch Box. Click on the Loop to see its configuration at the bottom of the screen. Please select Control field to be Forever. See the figure below for my configurations. 



Further, we will download our program on the Lego NXT brick and see the results as shown in the below video. 





You can see that every time when the obstacle is encountered, the robot goes reverse for 2 seconds and then take a turn of 180 degrees in Motor Space. In my case, at first the car encounters my hand in front of the ultrasonic sensor, so the car goes back when I switch the Lego NXT Program ON. Then the car encounters the wall. The robot can not turn away from the wall in one go. So you will see that there are multiple "reverse and turn" actions to move away from the wall. At the end, the robot encounters a soft toy. For some reason the Ultrasonic Sensor is unable to detect the fabric of the toy. May be, because the robot approaches the toy from a different angle or because the ultrasonic sensor is unable to detect the fabric itself. 

I hope you enjoyed this article!

Thursday 19 July 2012

Roaming With Touch Sensor

Now, let's take it a little further and set the car robot free to roam around. The car in this article is built using the small booklet which is delivered with the Lego kit. Now think, what if the robot collided with an obstacle! There comes the role of a touch sensor. Touch sensor gives the robot a sense of obstacle on its path. The sensor communicates this information to the NXT. NXT then decides what action to take, if an obstacle is present. So, lets take a look into the NXT program to observe the robot's behavior in the free world! In this project, the robot handles two distinct situations. 
  • If there is an obstacle present on the path, (as realized when Touch Sensor is pressed), do the following
    1. Make the robot go reverse
    2. Take a turn to move away from the obstacle
  • There is no obstacle, (as realized when Touch Sensor is in released or bumped state)
    1. Keep the robot moving forward
Let's program for the above two situations now. Drag and drop the Switch block from the left palette. This block performs certain actions when specific conditions are met. You will observe that there are two parts of the Switch box, the upper and lower one. 



Click on the Switch Box to see its configuration panel at the bottom of the screen (Please check the above figure). As per the configuration, please make sure that the sensor is selected to be Touch Sensor and the sensor is connected to port number 1 of NXT brick. The action is selected to be pressed. That means we have configured the upper part of Switch box if touch sensor is in pressed condition and below part of the Switch box is configured for sensor to be in other conditions, which are released or bumped. Drag and Drop the Motor Box from the left palette, over to the upper part of the Switch Box. Click on the Motor Box to see its configuration panel at the bottom of the screen. Configure this panel to make the car go reverse, when the obstacle is found and Touch Sensor is pressed. Please power on the motors B and C to go into reverse direction for 2 seconds. Keep rest of the configuration as it is. The below figure shows the configuration panel for the motor box for my car.     

 


Drag and drop another Motor Box in the upper part of the Switch Box, and place it next to the previous Motor Box (which we used for reversing the car). Click on this box and see its configuration panel at the bottom. Configure this Motor Box to take a turn. In order to take a turn, powered on only one motor (in my case Motor B) in reverse direction for 1 second. This will make the car move away from the direction of the obstacle. Please check the below figure for the configuration of this second Motor Box.   



Now drag and drop the third Motor Box in the lower part of the Switch Box, which will function in all other states of the touch sensor except pressed state, for example Bumped and Release state. This box will keep the car moving forward in the default conditions. Please check the figure below. 



Click on this third Motor Box and check its configuration panel below. I have configured it for my car to power on the Motors B and C to move in forward direction for unlimited duration. Now is the time to put the entire switch in a loop which will help the car to execute all previously defined steps forever. Please check the figure below. Click on the loop and we get the Configuration Panel of the loop at the bottom. Please select forever, in order to keep the car moving for its entire lifecycle. 




Download the program on to NXT brick and observe the movement of the robot. I have recorded the movement of my robot in a small clip below. 



As you can view from the clip, the robot encounters three obstacles on its way. On every occurrence of obstacle, the robot reverses, then it makes a turn and then start moving forward. Hope you enjoyed this article!



Sunday 15 July 2012

Return To Origin

Let's move on and make the robot move little more distant. The robot here is the small car which is built using small booklet provided with NXT kit. Let's make it a point that the robot will come back from where it has started. So, I have made the robot move for 5 seconds, make it take a U-turn and return to its origin. Let's take a look at the steps that we are going to follow. 


I will program the three steps mentioned above using NXT-G programming. Let's program for the first step Go Forward.


As shown in the figure above, drag and drop the Motor Block from the left palette to the Work Area. Click on the Motor Block and check its Configuration Panel in the bottom. Since we want to program it to move forward, we need to move both the rear wheels with the same speed for the same time. In my case, the rear wheels were powered on by Motor B and Motor C. So I have checked the ports B and C in the Configuration Panel. I want to drive the motor for 5 seconds, so I have specified the Duration to be 5 seconds. Next step for the robot is to take a U-turn (either from the left or from the right side). In order to do this, please drag and drop the Motor Block to the Work Area in succession with the first block shown in the above figure. The NXT-G program now looks like below. 


Please click on the second Motor Block and check its configuration at the bottom. In order to take a U turn, stop one of the rear motors and power-on the other motor. In my case I am powering on the C Motor and stopping the B motor. That will make the robot take a turn. To take a complete U-Turn the motor has to be rorated through 810 degrees (We came up to this number through multiple trial-and-error). Keep rest of the configurations as they are. And let's move to configuration of the third step, which is Come Back to The Origin. The NXT-G program now looks like below. 


  
As you can see the program now has three Motor Blocks in succession which correspond to the three steps which are required to return back to the origin. In order to configure the third Motor Block, click on to the block and check its Configuration Panel at the bottom. I have powered on both the rear motors for 5 seconds of duration as shown in the above figure. Now it's the time to run the NXT-G program and see the output in the below video. I have used the Line-Follower paper just as a baseline in order to get the correct start and end point of the robot. 


Friday 13 July 2012

Spinning Motion


Recently I have started understanding Lego Mindstorms NXT programming. As and when I update my learnings, I thought of sharing it with you. Please find a small program below in NXT-G, to spin the Robot in a clockwise direction. 



The robot here is the small car which is built using small booklet provided with NXT kit. In order to begin programming, drag and drop the Motor Block from the left palette to the Work Area. Click on the Motor Block on the Work Area and check its Configuration Panel in the bottom. Pick the appropriate port from the bottom Configuration Panel which is used to stop the right motor and power on the left motor. In my case the Motor Port to power on the left motor was Port C. Specify the Duration for which the motor needs to spin. I am spinning it for 5 seconds. Keep rest all the configurations as default. Please view the below video to check the output of the program.