nxtprograms.com

Fun Projects for your LEGO MINDSTORMS NXT!

Home     Projects     Help     Contacts

Multi-Bot Programs
Programs Index
 

(F) Using the Touch Sensors

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

Single Touch Sensor

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

Program Description and Observations Attachments
F1-Shoot1   Easy
This program shows the simplest way to use a touch sensor, which is with the Wait for Touch Sensor block.  After waiting for touch sensor 1 to be Pressed, motor A is turned one rotation, which will fire one ball from the Ball Shooter Arm attachment. Touch Sensors
(as Wired Remote)

Ball Shooter Arm

F2-Shoot2   Easy
This program makes a simple improvement to the F1-Shoot1 program by putting it in a Loop.  Now you can keep shooting balls each time you press the touch sensor. 

Note that if you press and hold the touch sensor down, the ball shooter will keep firing over and over like a machine gun in "automatic" mode.  This is because a Wait for Sensor block will complete immediately if the sensor test is already true.

Touch Sensors
(as Wired Remote)

Ball Shooter Arm

F3-Shoot3   Easy
This program changes the behavior of the F2-Shoot2 program by changing the Wait for Touch Sensor block to test for Bumped instead of Pressed.  Now the shooter will shoot only one ball each time you press and release the button, no matter how long you hold the button down.  This is sort of like a "semi-automatic" gun, except that the balls fire on the button release instead of the button press.

Bumped means "pressed and then released", but more specifically, a button is Bumped if it has been pressed and released since the last time you asked whether or not it was bumped.  The touch sensor maintains an internal press count, which is tested and then zeroed whenever you test for Bumped.

An advantage of testing Bumped instead of Pressed is that the button does not need to be pressed right at the time of the test, it only needs to have been pressed and released at some time before the test.  This is handy if the touch sensor test is inside of a loop which contains other lengthy operations, such as waiting for motors to complete.

The Bumped behavior in this program means that if you press and release the button quickly twice in a row, the shooter will fire two balls, because although the second "bump" (press and release) probably occurred during the motor rotation, the bump is remembered and thus Bumped will be true right away when tested the second time after the motor finishes.  However, if you are fast enough to press and release the button three times during the first loop iteration, only two balls will be fired.  If you understand why, then I think you finally understand how Bumped works!

Touch Sensors
(as Wired Remote)

Ball Shooter Arm

F4-Shoot4   Easy
This program improves on the F3-Shoot3 program by making the balls fire on the button press instead of the release, but still fire only one ball for each press, no matter how long the button is held down.  This makes it respond more quickly and more like a real "semi-automatic" gun. 

This behavior is done by using two separate Wait for Touch Sensor blocks, one that waits for Pressed, and one that waits for Released after the ball is fired.  Note that if you release the button during the firing of the ball, then Released will already be true when the second Wait for Touch Sensor block tests it, so it won't wait for anything and will continue right away.

Now try to predict what will happen before trying this:  What will happen if you press and release the button quickly twice in a row?

Touch Sensors
(as Wired Remote)

Ball Shooter Arm

F5-PathBug   Intermediate
F6-PathDebug   Intermediate
These two program show a way that you can use a touch sensor as a tool to help you "debug" a program (find a problem or mistake in it).  Because you can't see a program execute inside the NXT, you can't always tell which block is executing when.  This can lead to confusion that makes it hard to find a problem, even if the problem is just a simple mistake.

By inserting temporary Wait for Touch Sensor Bumped blocks into a program, you can make the robot step through the program one step (or one section) at a time and wait for you to bump (press and release) the touch sensor button before going on to the next step.  This can make it easier to determine which part of the program has the problem.

The F5-PathBug program is supposed to make Multi-Bot drive in an "N"-shaped path.  However, it has a simple bug in it, so the resulting path will not be shaped like an "N".  The F6-PathDebug program is a modified version of the program that inserts a Wait for Touch Sensor Bumped between each block.  By holding the wired touch sensor remote in your hand (or just a single touch sensor on a wire) while the robot is running the program, can you step through the program and find where the problem is?  Try it now before reading on if you want to try to find the problem yourself.

Once you find the problem and understand how the debug stepping works, can you explain why it is important that the Wait for Touch Sensor blocks test for Bumped instead of Pressed?

The Bug:  In the configuration panel of the LeftPivotAngle My Block, the programmer accidentally typed the desired angle of 135 in the Power parameter instead of in the Angle parameter and left the Angle parameter zero.  This will cause this turn to have no effect.  Because the steps before and after the turn are both Move blocks going forward, it may make it appear that the problem is that the Move before the left turn goes for too long.  But in fact, there is an invisible turn of zero degrees in the middle of the movement.  By stepping through with the touch sensor, you can see that it is actually two separate Move actions but the step where the LeftPivotAngle does nothing.

Touch Sensors
(as Wired Remote)
F7-SeqControl1   Easy
This program shows how you can use a touch sensor to make a simple form of one-button remote control for your robot.  In this program the robot does a sequence of events:
  1. Pivot turn left 
  2. Go straight
  3. Pivot turn right
  4. Stop and shoot a ball

In-between each step of the sequence is a Wait for Touch Sensor Bumped, and the movement blocks use Unlimited for the Duration.  This will cause each movement to continue until the touch sensor is bumped.  So, by holding the wired touch sensor remote in your hand, you can control the driving with touch sensor 1 (touch sensor 2 is not used).

With this technique, you can only control the duration of the movements, but not the sequence.  This can make it tricky to drive, but give it a try.  Put a target on the floor and try to drive to it and shoot it.

Touch Sensors
(as Wired Remote)

Ball Shooter Arm

F8-SeqControl2   Easy
This program shows an alternative to the F7-SeqControl1 program that uses a different strategy for one-button remote control that may feel more natural.  In this program, each step in the sequence is started by a press of the touch sensor and continues until the touch sensor is released, and the robot is stopped between each step.

This form of remote control is usually more natural and easier to control (although after practicing the
F7-SeqControl1
program, you may need to spend some time to unlearn that method).  Give it a try and see if it is easier (or at least less nerve-wracking) to drive.

Touch Sensors
(as Wired Remote)

Ball Shooter Arm

F9-CountPress   Advanced
This program uses a Variable and a Loop along with touch sensor tests to count how many times you can press a touch sensor in one second.  Instructions and your results are displayed on the screen.  Give it a try.  How fast can you press?

A careful combination of testing for Pressed and Released is used for this, since neither Pressed by itself nor Bumped will work easily.

Touch Sensors
(as Wired Remote)

 

Double Touch Sensor Remote Control

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

Program Description and Observations Attachments
F10-Remote1   Intermediate
This program shows a simple way to make a two-button remote control to control driving with Multi-Bot.  The left touch sensor controls the direction of the left motor (forward or backwards), and the right touch sensor controls the direction of the right motor.

Because two sensors are being tested, Switches and Motor Unlimited blocks are used, so that the Loop keeps repeating fast enough (more than 100 times per second) to see changes in either sensor when they occur.

This kind of motor control results in the following actions:

  • Press and hold the left button to turn left
  • Press and hold the right button to turn right
  • Press and hold both buttons to back up
  • Release both buttons to go forward

Note that there are four different actions, corresponding to the four different states that the two buttons can be in.  A noteable problem with this control is that there is no way to stop.

Touch Sensors
(as Wired Remote)
F11-Remote2   Intermediate
This program shows another way to make a two-button remote control for Multi-Bot.  The two touch sensors are tested in sequence by nesting Touch Sensor Switches, which separates the program into four different sequences corresponding to the four different button states (left, right, both, neither). 

With this method, different actions for the four different button states can be assigned however you want.  In this program, the actions assigned are:

  • Press and hold the left button to turn left
  • Press and hold the right button to turn right
  • Press and hold both buttons to go forward
  • Release both buttons to stop

Compared to the F10-Remote1 program, this approach adds a way to stop, but loses the ability to back up.

Touch Sensors
(as Wired Remote)
F12-PivotShoot   Intermediate
This program is similar to the F11-Remote2 program but uses the Ball Shooter Arm and assigns the four actions as follows:
  • Press and hold the left button to turn left
  • Press and hold the right button to turn right
  • Press/hold both buttons to shoot a ball
  • Release both buttons to stop

So this program adds the ability to shoot, but can no longer go forward or backwards (only turn).  This would be OK to control a gun on a turret, but is not great for a driving robot.

Touch Sensors
(as Wired Remote)

Ball Shooter Arm

F13-DriveShoot   Expert
The F10-Remote1, F11-Remote2, and F12-PivotShoot programs all assign exactly four different actions that correspond to the four states that two touch sensors can be in (left pushed, right pushed, both pushed, neither pushed).  Each program seemed like it was missing important features, because ultimately more than four actions is desired.

The F13-DriveShoot program shows a complex method to surpass the four-state limit of two touch sensors by using a Timer to distinguish the difference between a button being briefly pressed and released and the button being pushed and held down.  This allows different actions to be assigned to these two cases.  This same technique is often used by desktop computers to distinguish a mouse click, a drag, and a double-click, for example.

This program uses the two touch sensors to control the following actions:

  • Press and hold the left button to turn left
  • Press and hold the right button to turn right
  • Press and hold both button to go forward
  • Tap the left button to back up until the left button is tapped again
  • Tap the right button to shoot one ball
  • Release both buttons to stop

Note that there are four different actions, corresponding to the four different states that the two buttons can be in.  A problem with this control is that there is no way to stop.

Touch Sensors
(as Wired Remote)

Ball Shooter Arm

F14-Golfer   Expert
This program is a variation on the F13-DriveShoot program that allows you to drive Multi-Bot with the Touch Sensors remote control as above, but instead of shooting a ball when you tap the right button, it will swing the Golfing Arm.

You can use this program to make a remote controlled golfing robot.  Start with the Golfing Arm straight down and place a ball somewhere on the carpet.  See if you can drive up to the ball and hit it in the direction you want.  Make a course and see how many shots it takes to get the ball into the hole or other target that you make.

Touch Sensors
(as Wired Remote)

Golfing Arm

 

Double Touch Sensor Bumper

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

Program Description and Observations Attachments
F15-Bumper   Intermediate
This program uses the Touch Sensors as a bumper on the front of Multi-Bot to allow the robot to detect a collision with something on either side of the bumper.  The program will cause Multi-Bot to wander around the room and try to turn away from things that it bumps into.

Use Multi-Bot with Treads for best traction on carpet or uneven surfaces.  Wheels will also work on smooth floors.

Because two sensors are being tested, Switches are used, so that the Loop keeps repeating fast enough (more than 100 times per second) to see changes in either sensor when they occur.  The LeftPivotAngle and RightPivotAngle My Blocks are used to achieve strong pivot turns for turning on carpet (where turning with the Move block is not as good).

Touch Sensors
(as Bumper)
F16-Bumper2   Advanced
This program is a re-write of the F15-Bumper program that is reorganized to reduce some repeated blocks.  Looking at the F15-Bumper program, you will see that the Move block to go forward is repeated three times, and the Move block to back up is repeated twice.  The F16-Bumper2 program uses a more complex Switch structure to eliminate the duplicated blocks.

The F16-Bumper2 program also adds a single Sound block that will apply to both collision directions, and note that if added to F15-Bumper, the Sound block would need to be repeated twice.  Although F16-Bumper2 is more complex than F15-Bumper, as programs get more complex it often becomes more desirable to eliminate duplicated parts.

Touch Sensors
(as Bumper)
F17-WallTouch   Advanced
This program uses the double touch sensor bumper to allow Multi-Bot to align itself perpendicular to a wall and then back up straight away from the wall.  You can start Multi-Bot pointing at various different angles to the wall, and it should always straighten itself out perpendicular to the wall.

The robot will drive straight towards the wall until one side touches the wall, then it will pivot until the other side also touches, then it will back up when both sides are touching.

Compared to the "stalling" techniques for aligning with a wall that are shown in the Driving Straight - Advanced section (B7-SquareWall and B8-StallWall), using two touch sensors may be faster, less forceful, and better able to handle a variety of different angles.

Touch Sensors
(as Bumper)

 

[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.