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!

No comments:

Post a Comment