nxtprograms.com

Fun Projects for your LEGO® MINDSTORMS® NXT!

Home     Projects     Help     Contacts

Multi-Bot Programs
Programs Index
 

(E) Using the Ultrasonic Sensor

The programs in this section show different ways to use Multi-Bot's Ultrasonic Sensor attachment.

Ultrasonic Distance Measurement

(Downloadable programs are available only on the CD "LEGO MINDSTORMS NXT 2.0 by Example").

Program Description and Observations Attachments
E1-FindWall   Easy
This program shows a simple use of the ultrasonic sensor that makes the robot drive forward until it gets close to something then stop.

Using the Wait for Sensor block is the typically easiest way to use a sensor.  Note that Move Unlimited is used to make the robot drive an unknown distance until the sensor test triggers.

Ultrasonic Sensor
(Front Mounted)
E2-ExploreWalls   Intermediate
This program extends the E1-FindWall program to make the robot back up and turn when it sees a wall, then repeat the process with a Loop to keep exploring for walls until the program is aborted.

The main challenge when using a Loop like this is to make sure the sequence of actions is correct at the beginning and end of the loop, and also for the first and last time through the loop.  Note that an additional Move Unlimited block is used to cover all the cases here.

The LeftPivotAngle My Block is used for convenience, but another type of turn would be fine too.

Ultrasonic Sensor
(Front Mounted)
E3-MagicPush1   Intermediate
With this program, you should attach the Ultrasonic Sensor attachment to the rear of Multi-Bot, pointing backwards.  The program will cause the robot to drive forward whenever your hand is close to it and stop whenever your hand is far away, as if your hand was pushing the robot with an invisible force.

Note that the Wait for Sensor (with Ultrasonic Sensor) block can be configured to test for something closer (less than) a certain distance, or farther (greater than), and both are used in this strategy.

Ultrasonic Sensor
(Rear Mounted)
E4-MagicPush2   Intermediate
This program achieves the same result as the E3-MagicPush1 program in a different way, this time using a Switch configured with Ultrasonic Sensor to test the sensor and choose which of the two actions to do each time through the loop.

Although somewhat harder to understand than the E3-MagicPush1 program, it is important to understand how this technique works, as it builds the foundation for more complex programs (such as the revisions below).  In particular, note that the loop in this version of the program is always repeating over 100 times per second, whereas the loop in E3-MagicPush1 only repeats once for each time the robot restarts from a stop. 

In most of the repetitions of the loop in this program, the robot will be "retold" to do the same thing it was told last time (go forward or stop), which may seem redundant, but this repetition has advantages in more complex cases.  In particular, the rapid repeating of the loop allows the program to add additional processing inside the loop that will get executed no matter what state the robot is in (moving or stopped) and won't get postponed because the program is stuck at a Wait block.

Ultrasonic Sensor
(Rear Mounted)
E5-MagicPush3   Intermediate
This program is exactly equivalent to the E4-MagicPush2 program, except that it uses a Logic Switch and a separate Ultrasonic Sensor block instead of a Sensor Switch, which requires the use of a data wire to connect the Yes/No result of the Ultrasonic Sensor block to the logic input on the Logic Switch.

Understanding how to use the individual Sensor blocks (yellow colored blocks) and how they differ from the Wait for Sensor blocks (orange) is very important for more complex tasks.  Although they look similar and have similar configuration panels, note that the (yellow) Sensor blocks do not wait for the requested condition to become true, but rather just perform an instantaneous test of the condition and produce the Yes/No result on their data hub.  This allows a rapidly-repeating loop to keep repeating as fast as possible without waiting or anything.

Ultrasonic Sensor
(Rear Mounted)
E6-MagicPush4   Advanced
This program makes a significant improvement to the behavior of the E5-MagicPush3 program by using the numeric Distance in inches as reported from the Ultrasonic Sensor block to adjust the power of the driving motors, based on the distance your hand is away from the robot.  The closer your hand is to the robot, the faster the robot will drive.  The result is smoother and more realistic, as if the invisible force was a magnetic repulsion.

Note that the Ultrasonic Sensor block produces both a Distance result (a Number in inches) and a Yes/No (Logic) result based on the threshold test built into the block, and both are used in this program.

The Distance result is only available from the individual Ultrasonic Sensor block, and it is also important that the block does not wait for a result, so that the loop can rapidly repeat and continuously update the motor power many times per second.

Ultrasonic Sensor
(Rear Mounted)

 

Advanced Object Detection

(Downloadable programs are available only on the CD "LEGO MINDSTORMS NXT 2.0 by Example").

Program Description and Observations Attachments
E7-FindObject   Expert
This program contains a My Block named FindObject, which is easy to use but has a complex implementation inside it.  The FindObject My Block will use the ultrasonic sensor to attempt to locate a nearby object and point the robot at it, by turning the robot until the robot is pointing at the object.

To use the program, put the robot in an open area on the floor away from furniture and other objects, and place an object that is large and tall enough to be seen by the ultrasonic sensor (a soda can for example) near the robot (no more than about a foot away), but not right in front of it.

Locating an object in an unknown direction and at an unknown distance is tricky with the ultrasonic sensor, because you don't know what specific distance to test for, and the ultrasonic sensor has a fairly wide field of view, so it will "see" an object before it is really pointing at it.  To try to solve these problems, the FindObject My Block attempts to scan for both the left and right "edges" of the object and then determine a center direction from that.

Ultrasonic Sensor
(Front Mounted)
E8-FindPush   Expert
This program makes an addition to the E7-FindObject program so that after finding the object, the robot tries to drive into it to knock it over.  Set it up the same way.

Note that once the difficult task of finding an object has been solved with the FindObject My Block, the result is easy to re-use in different programs.

Ultrasonic Sensor
(Front Mounted)
E9-FindShoot   Expert
This program makes another more complex addition to the
E7-FindObject and E8-FindPush programs.  Now after finding the object using the FindObject My Block, the robot tries to move to 30 cm away from the object (forward or backward as required), and then shoot the object with the Ball Shooter Arm attachment.

If you place an empty soda can somewhere near Multi-Bot (about 5-15 inches away) at the beginning of the program,  Multi-Bot will usually be able to find it and shoot it down!

Ultrasonic Sensor
(Front Mounted)

Ball Shooter Arm

 

 

[Back to Programs Index]

 

Home     Projects     Help     Contacts

 

Copyright © 2007-2011 by Dave Parker.  All rights reserved. 
All project designs, images, and programs are protected by copyright.  Please see the usage policy.