Wednesday, September 06, 2006

Wait For Ultrasonic And Touch Sensor

Everyone in my family just loves to play with LEGO MINDSTORMS NXT (yep, I have taken it to family meetings). But I noticed that my nephew Mathijs, who is 14 years old, struggles with using two sensors at the same time.

In particular, using both the ultrasonic sensor to detect obstacles, and the touch sensor to act like a bumper, proves to be pretty challenging. Frankly, this is a piece of code I have debugged for myself also multiple times, so I decided to create a My Brick that captures this functionality, and make it available as a download.


(download link near bottom)

What the “Wait For Ultrasonic And Touch Sensor” brick does

 The sensor will wait until either the Ultrasonic sensor has detected the selected range, or until the touch sensor is pushed. It will return the value 1 if the Ultrasonic sensor has detected the selected range, it will return the value 2 if the touch sensor was pushed, and it will return 3 if both events happened together.

Configuring the brick

  • You need to provide the ports to which the touch sensor and the ultrasonic sensor are connected.
  • For the ultrasonic connector, you must also specify the trigger point. This is the distance that the ultrasonic sensor is responding to.
  • If you want the ultrasonic to trigger when it comes too close to an obstacle, check the "Trigger If Closer Than Trigger Point" box. When this box is unchecked, the ultrasonic sensor will fire when there is no obstacle between the robot and the trigger point distance.

Sample Application

In this sample application, the robot has an ultrasonic sensor at the front, and a bumper at the back.

This application will drive forward until the ultrasonic sensor detects an obstacle closer than 30 cm. When that happens, it will drive backward and turn until the obstacle is further than 50 cm, or until it bumps into something with the rear bumper. Then it will start to drive forward again.

With this simple program, the robot can navigate the room and avoid obstacles and walls.

Sample Application 2

In this second sample application, we'll make things a little bit more complicated. This time, the bumper and the ultrasonic sensor are both at the front of the robot. When the bumper is pushed, then some obstacle did not get properly detected by the ultrasonic sensor.

We want the robot to drive forward. When the ultrasonic sensor sees an object, then it should simply turn a number of degrees to avoid the obstacle. But if something hits the bumper, it may be blocking the robot. The robot may not be able to turn. Therefore, the robot should first drive backward, and then turn. Here's the program that does this:

sample program 2

Here's the setting of the switch (if-then) statement:

switch settings

Notice that I have set the second option as the default option. That means that if both the ultrasonic and touch sensor fire at the same time, then we will respond to the bumper and ignore the ultrasonic sensor.


There are two versions for download: one expresses the trigger point in centimeters, and the other in inches.
  • Download the "Wait For Ultrasonic and Touch Sensor" brick where the trigger point is expressed in centimeters;
  • Download the version where the trigger point is expressed in inches.

Installation Instructions

  • Download the brick using the link above.
  • From within NXT-G, go to the Edit menu, and select the Manage Custom Palette submenu. A directory opens.
  • In this directory, create a directory bNXT if it does not already exist.
  • Next, copy the downloaded brick from the downloaded zip file to the bNXT directory you have just created.
  • Now close NXT-G completely.
  • When you restart NXT-G, the brick will be available in the custom bricks menu (third palette down).

How it works

The program of this brick can be seen by double clicking the brick after installation. I encourage you to do so, and perhaps make your own variations.


At October 15, 2006 6:27 PM , Anonymous Anonymous said...

I think this is neat, but the download is a PC zip archive. Can you create one that will work on Macs as well?

At December 18, 2006 11:58 PM , Anonymous Anonymous said...

Mac OS X has the ability to open .zip files as part of the operating system.

At December 20, 2006 4:55 PM , Anonymous Anonymous said...

Can you tell me with i can use visualbasic to program the lego Mindstorms NXT?


At December 27, 2006 12:29 AM , Anonymous Anonymous said...

I tried the brick that you built-and met a problem. There is no movement in the program. What to do?

At December 29, 2006 6:15 AM , Anonymous Mad_Robot said...

The 'Block' doesnt seem to run properly for me.
Looking at your code.. my IDE shows NO variables as you have defined in your 'Custom Block'.
Do I need to identify them also?
Example: Your block contains a variable for 'touch sensor port'.
The variable name is longer than the 'icon' and therefore, I cant read the name on the screen.
Also, the name does NOT appear in the 'variable icon' 'vars list'.
This leads me to believe that I need to create the variables locally on my machine/profile in order to make the Block work.
If I choose another variable name from the list (say a default one like 'logic1' then the Icon's name changes to logic1.
I know your vars begin with 'bNxt_U' but that is all that I can see.
Could you please update your page if needed to identify how to handle variables.
BTW: Also, HOW did you make that 'input parameter' for your block? Could you discuss it on the page?

John.. the Mad Robot.

At January 04, 2007 1:21 PM , Anonymous Anonymous said...

i am eleven so i am not brilliant at this stuff but the new block really helped.

At January 08, 2007 10:48 AM , Anonymous Dédé said...

Congratulation, I have experienced it; only followed installation instructions and it works perfectly.
Dédé from Belgium.

At February 10, 2007 2:29 AM , Anonymous Anonymous said...

How to program a rover with US scanning left and right and go the direction of the far away object.


At August 31, 2007 7:31 PM , Anonymous Anonymous said...

hi When I open the MANAGE COSTUME PALLET it reads that there is no file by that name. What do I do?!


Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home