Chapter 9. Feedback

Your LEGO Mindstorms NXT kit comes with a collection of motors and sensors. And the NXT Brick has three built-in timers plus the buttons on the Brick. Would it surprise you to learn that all of these items are able to provide some sort of feedback to the NXT Brick? The sensors are a little obvious; a sensor is designed to respond to external conditions such as light, touch, color, or sound and report this information to the Brick. But what about motors? And how can a timer be used as input or feedback to the Brick? All these questions have answers, and this chapter provides them. It's a little longer than most chapters, but you'll find this information very useful for giving your robot the ability to monitor external conditions (as well as time) and make decisions on its own.

What's Your Condition?

Let's start with the word condition. A traffic light has three conditions: red, yellow, and green. A light switch has two conditions: on and off. Just using these two simple examples, I can give you some pseudo-code for SPOT:

  • Me: SPOT, move forward until the traffic light turns red.

  • Me: SPOT, display the words "Light On" on your LCD screen until the light switch is off.

In both these examples, I'm assuming that SPOT has eyes and can see the traffic light or light switch. If SPOT doesn't have eyes, then I would need a way for the traffic light or light switch to provide their conditions to SPOT. The traffic light and light switch could then provide feedback, or input, to SPOT.

Asking a traffic light to provide input isn't realistic, but your NXT robots do have the ability to receive feedback from items such as the sensors. So I can change the pseudo-code for SPOT:

  • Me: SPOT, move forward until the Light sensor reports a value of 20.

  • Me: SPOT, display the word "Hello" until the Touch sensor reports that it has been pressed and released (also known as bumped).

  • Me: SPOT, play a C note when the Ultrasonic sensor detects an object six inches in front of you.

The motors, too, can provide feedback:

  • Me: SPOT, spin motor A until motor B reports that it has spun ten degrees.

  • Me: SPOT, display "5 rotations" when motor C has rotated five times.

I mentioned that the Brick has three built-in timers plus three buttons that can be used, so I could also write the following pseudo-code:

  • Me: SPOT, when 20 seconds have elapsed, turn 90 degrees.

  • Me: SPOT, play a B note if I press the left button and a C note if I press the right button.

Okay, so you can see that the sensors, motors, buttons, and timers can provide input to the Brick to control other actions (MOVE, SOUND, and other blocks). You program your robot to perform specific actions based on the conditions of these items. Just like you know a light switch has two conditions, on and off, you need to know the various conditions that the sensors, buttons, timers, and motors possess and can report to the Brick.

So, for the rest of this chapter, I'm going to explain the settings for these items, so you'll know how to configure them properly. Future blocks that we'll cover, including the WAIT, LOOP, and SWITCH blocks, will depend on your understanding of how to properly configure the conditions.

The method I'm going to use to do this is fairly simple: I'll describe each item (sensor, motor, button, and so on) and provide a description of the settings that can be modified within the item's configuration panel. Please note that I am also providing information on the so-called legacy items: the previous version of Mindstorms (Robotics Invention System, or RIS) included motors, sensors, and a lamp. These RIS items can be used with NXT robots but require special converter cables to connect to the NXT Brick. I call them "legacy" because they come from the older Mindstorms system, but they are still very useful and fully compatible with the NXT system.

Note

Right now, only the education version of the NXT kit comes with software support for the legacy/RCX items. If you are running the retail version, you may download NXT programming blocks for the RCX motors and sensors by visiting http://mindstorms.lego.com/Support/Updates/. Click on the Patches option along the left side of the screen and download the files from the Legacy Block section for your specific language (English, German, French, Japanese, and Dutch are the only options currently available).

Configuring the Sensors

Let's start with the sensors—NXT Touch, RIS Touch, NXT Sound, NXT Light, RIS Light, NXT Ultrasonic, NXT Rotation, RIS Rotation, NXT Color, and RIS Temperature—which have some interesting rules to abide by.

First, sensors detect a change in a condition. The condition could be a change in light level, a change from seeing a blue ball to a red ball, a change in volume (sound), or maybe a change in position (movement). The sensor is simply "watching" the changes in its condition. The Light sensor, for example, can detect changes in the level of lighting in the room. The Ultrasonic sensor is detecting changes in distance from an object or wall in front of it.

Second, sensor programming blocks can respond only to one condition at a time. When you drop a sensor block into an NXT-G program, it has to be configured to test one condition. A single Light sensor, for example, cannot be configured to respond to whether the light in a room is below 80 and greater than 50. In order to test both conditions, you would need to use two Light sensor blocks in your program.

In Chapter 8, we learned how logic values can be sent and received between NXT-G blocks using data plugs. Well, sensors can provide a Logic type response, True or False, depending on the conditions that their respective configuration panels are programmed to detect. Think of our traffic light for a moment. Let's assume that every car has special traffic light sensors mounted on top. These light sensors are programmed to examine the conditions of the traffic light. Cars will move while the light is green and stop when the light is red. If the light is yellow, the car will slow down.

Note

There are many more types of information a sensor can provide. For example, a Light sensor can provide a numeric value related to the light level in a room. The Sound sensor can provide a decibel level. This type of information is all provided using the data plugs found on a sensor block's data hub. I'll talk about this later in the book, but this chapter is mainly concerned with the ability of sensors and motors to provide Yes or No (True or False) signals to other blocks.

These traffic light sensors, then, can detect one of three possible conditions: green, yellow, or red. An NXT sensor can check only one condition, so if this were an NXT traffic light sensor, each car would require three traffic light sensors:

  • If the first traffic light sensor detects green, the car should keep moving.

  • If the second traffic light sensor detects yellow, the car should start slowing down.

  • If the third traffic light sensor detects red, the car should stop.

Notice that only one condition can be true at any given time; the light cannot be green and red at the same time. So when the sensor detecting green is triggered, yellow and red cannot be triggered. Therefore, we can examine a sensor's Logic data type for a True/False response to the condition the sensor is configured to monitor.

Let's take a look now at the sensors.

NXT Touch Sensor

The NXT Touch sensor's configuration panel is shown in Figure 9-1.

The NXT Touch sensor's configuration panel

Figure 9-1. The NXT Touch sensor's configuration panel

In the Port section, select the port where the NXT Touch sensor has been connected. The default port for the Touch sensor is Port 1.

In the Action section, you can select Pressed, Released, or Bumped. Remember that Bumped means quickly pressing and releasing the button—less than .5 seconds for the entire press-and-release action.

The NXT Touch sensor's data hub is shown in Figure 9-2. Remember that you can hover your mouse pointer over a data plug to obtain the plug's name.

The NXT Touch sensor's data hub

Figure 9-2. The NXT Touch sensor's data hub

The port number can be configured using the Port data plug and a Number data type wire with an input value of 1, 2, 3, or 4. This value can also be used as output by dragging a data wire out of the output Port data plug.

The Action data plug can use a Number data type with an input value of 0 for Pressed, 1 for Released, or 2 for Bumped.

The Yes/No data plug can provide an output value of True or False. It can send and receive a Logic data type.

The Raw Value data plug can provide an output Number value with a range of 0–1024.

The Logical Number data plug provides a 1 for pressed and a 0 for released. Instead of a logic value, however, this data is a Number data type.

Note

The NXT-G 1.0 version of the software does not contain the Logical Number data plug. This comes only with the 2.0 version of the software. It's always good to know which version of NXT-G you are using, but if you find a data plug described in this book that you don't see on your own data hub, it's likely that you're running version 1.0 of the software.

For the remaining blocks in this chapter, I will not be describing the blocks' data hubs—only the blocks' configuration panels (I simply wanted to show you that the sensor blocks can use their data hubs for even more options than just sending and receiving logic data signals). Many data plugs are identical from block to block, so you'll likely find that learning just a few of them will allow you to recognize all of them. But if you're not familiar with any of the data hubs at this point, click the Help menu, and select the "Contents and Index" option to view the Help files. Along the left side of the screen, select the name of a programming block (see Figure 9-3). Details for the block's data hub can be found at the bottom of that block's Help documentation (also shown in Figure 9-3). You can always consult this Help documentation to determine the data types for the various data plugs (Number, Logic, or Text).

Data hub details for the Touch sensor are described.

Figure 9-3. Data hub details for the Touch sensor are described.

RIS Touch Sensor

The legacy RIS Touch sensor's configuration panel is shown in Figure 9-4.

The RIS Touch sensor's configuration panel

Figure 9-4. The RIS Touch sensor's configuration panel

In the Port section, select the port where the RIS Touch sensor has been connected. The default port for the Touch sensor is Port 1.

In the Action section, you can select Pressed, Released, or Bumped.

NXT Sound Sensor

The NXT Sound sensor's configuration panel is shown in Figure 9-5.

The NXT Sound sensor's configuration panel

Figure 9-5. The NXT Sound sensor's configuration panel

In the Port section, select the port where the NXT Sound sensor has been connected. The default port for the Sound sensor is Port 2.

In the Compare section, you must configure the sound value (0–100) as well as whether the sensor will monitor for sounds less than or greater than the configured value. You can use the drag bar to select the value or enter a numeric value in the range of 0–100 in the text box. Select the less-than option (<) or the greater-than option (>) from the drop-down menu or click the left or right radio button to configure this option.

NXT Light Sensor

The NXT Light sensor's configuration panel is shown in Figure 9-6.

The NXT Light sensor's configuration panel

Figure 9-6. The NXT Light sensor's configuration panel

In the Port section, select the port where the NXT Light sensor has been connected. The default port for the Light sensor is Port 3.

In the Compare section, you must configure the light value (0–100) as well as whether the sensor will monitor for a light value less than or greater than the configured value. You can use the drag bar to select the value or enter a numeric value in the range of 0–100 in the text box. Select the less-than option (<) or the greater-than option (>) from the drop-down menu, or click on the left or right radio button to configure this option.

If you leave the "Generate light" box checked, the Light sensor will turn on its built-in LED to provide an artificial light source for assisting with determining light levels. If you uncheck the box, the Light sensor will detect only ambient light levels—that is, normal light conditions.

RIS Light Sensor

The RIS Light sensor's configuration panel is shown in Figure 9-7.

The RIS Light sensor's configuration panel

Figure 9-7. The RIS Light sensor's configuration panel

In the Port section, select the port where the RIS Light sensor has been connected. The default port for the Light sensor is Port 3.

In the Compare section, you must configure the light value (0–100) as well as whether the sensor will monitor for a light value less than or greater than the configured value. You can use the drag bar to select the value or enter a numeric value in the range of 0–100 in the text box. Select the less-than option (<) or the greater-than option (>) from the drop-down menu, or click the left or right radio button to configure this option.

NXT Ultrasonic Sensor

The NXT Ultrasonic sensor's configuration panel is shown in Figure 9-8.

The Ultrasonic sensor's configuration panel

Figure 9-8. The Ultrasonic sensor's configuration panel

In the Port section, select the port where the NXT Ultrasonic sensor has been connected. The default port for the Ultrasonic sensor is Port 4.

In the Compare section, you must configure the Distance value as well as whether the sensor will monitor for a distance value less than or greater than the configured value. You can use the drag bar to select the value in the range of 0–100, or you may type a value in the text box with a lower limit of 0 but an upper value greater than 100. Using the drag bar limits you to a range of 0–100, but you are able to type in a value greater than 100. Select the less-than option (<) or the greater-than option (>) from the drop-down menu, or click on the left or right radio button to configure this option.

In the Show section, select either Centimeters or Inches from the drop-down menu.

NXT Rotation Sensor

The NXT Rotation sensor's configuration panel is shown in Figure 9-9. Remember that the Rotation sensor is built into the NXT motors, so don't go looking for an actual NXT Rotation sensor by itself.

The NXT Rotation sensor's configuration panel

Figure 9-9. The NXT Rotation sensor's configuration panel

In the Port section, select the motor port where the NXT motor (Rotation sensor) has been connected. The default port for the Rotation sensor is Port A.

In the Action section, you should select the Read option if you want the built-in rotation sensor to monitor the value (count) returned by the sensor. Select the Reset option to set the sensor count back to zero.

In the Compare section, select the motor's spin direction (Forward or Reverse) to monitor. Select Degrees or Rotations from the bottom drop-down menu, and enter a numeric value in the text box. You must also decide whether the sensor will monitor for a value less than or greater than the configured value by selecting the option in the other drop-down menu (using the Yes/No data plug, you can test for a True or False logic response based on the settings you have configured in the panel).

RIS Rotation Sensor

The RIS Rotation sensor's configuration panel is shown in Figure 9-10.

The RIS Rotation sensor's configuration panel

Figure 9-10. The RIS Rotation sensor's configuration panel

In the Port section, select the port where the RIS Rotation sensor has been connected. The default port for the Rotation sensor is Port A.

In the Action section, you should select the Read option if you want the built-in rotation sensor to monitor the value (count) returned by the sensor. Select the Reset option to set the sensor count back to zero.

In the Compare section, select the motor's spin direction (Forward or Reverse) to monitor. Enter a numeric value in the text box for the number of ticks you wish to monitor; there are 16 ticks in one rotation. You must also decide whether the sensor will monitor for a value less than or greater than the configured value by selecting the option in the other drop-down menu.

Note

NXT-G 1.0 software users will find that the Rotation sensor block will only allow for comparisons in units of Rotation. With NXT-G 2.0, the software has been updated to allow the Rotation sensor block to also compare using units in Degrees in addition to number of rotations.

NXT Color Sensor

The NXT Color sensor is new to the NXT 2.0 kit. Its configuration panel is shown in Figure 9-11.

The NXT Color sensor's configuration panel

Figure 9-11. The NXT Color sensor's configuration panel

In the Port section, select the port where the Color sensor has been connected. The default port for the Color sensor is Port 3.

In the Action section, you can select the Light Sensor option, which will allow you to use the Color sensor as a Light sensor. When used as a Light sensor, the configuration panel will change to mimic the Light sensor's configuration panel, with the exception of allowing you to choose the color of the reflected light—Red, Blue, or Green.

If the Color Sensor selection is left in the Action section, click the drop-down menu for the Compare section and select either Inside Range or Outside Range. You then use the two small drag bars to define a single color or a range of colors to check. Figure 9-12 shows how I've configured the Color sensor to look for a color outside of green and red; this would mean that yellow is also ignored. (I select the Outside Range for the comparison so the Color sensor can report a True signal if a detected color falls outside that range.)

The NXT Color sensor can detect ranges of colors.

Figure 9-12. The NXT Color sensor can detect ranges of colors.

RIS Temperature Sensor

The RIS Temperature sensor's configuration panel is shown in Figure 9-13.

The RIS Temperature sensor's configuration panel

Figure 9-13. The RIS Temperature sensor's configuration panel

In the Port section, select the port where the RIS Temperature sensor has been connected. The default port for the Temperature sensor is Port 4.

In the Compare section, you must enter a numeric Temperature value in the text box or use the drag bar to set a value. The value for Celsius must be in the range of −20 (negative 20) to 70. The value for Fahrenheit must be in the range of −4 (negative 4) to 158. Select the less-than option (<) or the greater-than option (>) from the drop-down menu, or click on the left or right radio button to configure this option.

In the Show section, select either Fahrenheit or Celsius from the drop-down menu. The value ranges the sensor can monitor are determined by this selection.

Other Input Types

There are three other methods that can be used to provide feedback to your NXT robots. These include the NXT buttons, the NXT timers, and a message received using Bluetooth technology by the NXT Brick using a RECEIVE MESSAGE programming block. I'll cover the RECEIVE MESSAGE block (and its partner, the SEND MESSAGE block) in Chapter 25, so let me close this chapter with details on the NXT buttons and timers.

Your NXT Brick has four buttons on its front: Left, Right, Enter (orange), and Cancel. The Cancel button cannot be used as input; its function is simply to cancel a running program, or if you are navigating around the on-screen LCD tools, it can be used to move back to a previous screen. That leaves the Left, Right, and Enter buttons—any program you create can use these three buttons to provide input.

Figure 9-14 shows the configuration panel for the NXT BUTTONS block.

The NXT buttons are configured here.

Figure 9-14. The NXT buttons are configured here.

In the Button section, select the button (Enter, Left, or Right) from the drop-down menu. The default selection is the Enter button.

In the Action section, select Pressed, Released, or Bumped. These work just like the Touch sensor, by the way. Now your robot can detect the condition of any of its buttons and send a True/False signal to other blocks that can, in turn, perform more actions.

The NXT Brick also has three built-in timers. These timers begin counting the moment you press the Enter button to begin running a program. Figure 9-15 shows the TIMER block configuration panel.

The NXT Timer configuration panel

Figure 9-15. The NXT Timer configuration panel

In the Timer section, select 1, 2, or 3 from the drop-down menu to select the timer you wish to use. The default selection is Timer 1.

In the Action section, select the Read option to obtain the current value of the timer. If you select Reset, the timer will be reset to zero when this block is executed.

The Compare section allows you to specify a value (in seconds) as a trigger that can be tested. Select either the greater-than option or the less-than option from the drop-down menu, and the TIMER block can now be used to provide a True/False Logic data type response. For example, if you configure the TIMER block for greater than ten seconds for Timer 1, the Logic data plug will provide a False response until Timer 1 exceeds ten seconds. After that, the data plug will provide a True response.

Data Hub Power

So now you know how to configure the various sensor blocks to detect conditions and provide a logic signal (True or False) using a data wire. If you've programmed your Light sensor's configuration block to detect when the light level is greater than 50, then a wire from its output Yes/No data plug that runs into a MOVE block's Yes/No data plug will have the motor spin Forward if the light level is truly greater than 50 (a Yes signal is sent over the wire) or have the motor spin Backward (if the light is less than 50, a No signal is being sent over the wire).

Just keep in mind that a True (or Yes) logic signal will be sent using the Yes/No data plug when the condition described on a configuration panel is met. When the condition is not met, a False (or No) logic signal will be sent.

But what about all those other data plugs in a sensor block? Glad you asked. Take a look at Figures 9-16 through 9-18. This shows a small program I've created for SPOT.

A program using data plugs for control and the configuration panel for the Touch sensor

Figure 9-16. A program using data plugs for control and the configuration panel for the Touch sensor

The Light sensor's configuration panel

Figure 9-17. The Light sensor's configuration panel

The MOVE block's configuration panel

Figure 9-18. The MOVE block's configuration panel

Let's walk through this program carefully. Notice first that when the program is executed the Touch sensor will send a Yes signal to the MOVE block via a wire if the button is Pressed. A Yes signal will tell the MOVE block to Brake vs. Coast. If the button isn't being pressed the moment the program is executed, the wire will carry a No signal, indicating the motors should coast when they complete their programmed action (in this case, spin for five rotations).

Next, the Light sensor is going to take a light-level reading (Intensity) on a scale of 1 to 100 and send this value to the MOVE block's Power data plug. At the same time, if it detects the light level is greater than 50, it will send a Yes signal (via a logic data wire) to the Direction data plug on the MOVE block and have the motors spin in a Forward direction (a No signal will spin the motors backward).

Here you can see how I'll control the speed (Power) of the motors by linking them to the Intensity of the light being detected by the Light sensor. In a slightly darker room (with an Intensity value greater than 0 but less than 50), the robot will not only roll slower but it will also roll backwards!

Finally, after the motors complete their movement, the program will pause for ten seconds before ending.

Are you beginning to see how each data plug in a block can be used to control other blocks? Sensors are the best for doing this because they have so many different data plugs that can supply different signals. With some experimenting, you can easily program your robots so they make left turns when the Sound sensor detects a loud CLAP!

Or maybe you'd like to have your robot run from a flashlight? Easy—just program its Light or Color sensor block to detect a bright light hitting its surface and tell the robot to roll backwards, away from the light.

With the various sensors available to you, you have the ability to give your robot some real decision-making skills. Run from a light or loud sound? Move closer and inspect? Shut down and hibernate? Detect a nearby wall? Move closer or farther away?

Your creativity and skill with using data wires is your only limit. And I haven't even covered the majority of the NXT-G blocks available to you to use in your programs! Once you know how all the blocks work, you'll have moved into the realm of real power programming and your robots will amaze.

Now, before leaving this chapter, try your hand at creating a small program that satisfies the requirement specified in Exercise 9-1.

Exercise 9-1

Create a small program that takes the Sound level (range of 0 to 100) detected when the program first executes and uses it to control the Power of the motors. The robot should also use its Ultrasonic sensor to see if anything is directly in front of it and no closer than five inches. If something is five inches or closer, it needs to roll away from the obstacle instead of closer to it. Have the robot's motors spin for a total of ten rotations.

What's Next?

This chapter showed you only how to configure a handful of blocks using the configuration panel and perform some small tricks with data wires. The real power comes with using these tricks in conjunction with other blocks, such as the LOOP, WAIT, and SWITCH blocks.

Chapter 11 will show you how to use the LOOP block to repeat certain tasks. When using a LOOP block, you can have the block loop forever, that is, perform the tasks over and over again until you cancel the program. Your other option is to configure the LOOP block to stop looping when a certain condition is met—for example, if the Light sensor detects a light level below 20, the Sound sensor detects a noise over level 80, or maybe a motor spins more than 20 degrees (Rotation sensor).

It's the same with the WAIT and SWITCH blocks. Both of these blocks will allow you to use what you've learned in this chapter to give your robots decision-making power.

So, read on—the WAIT block is up next in Chapter 10.

Exercise Solution

Figures 9-19 to 9-21 display the blocks and the three configuration panels used in this program. Notice that the Sound sensor is sending a Number data type value (yellow wire) to the MOVE block's Power setting and the Ultrasonic sensor is sending a Logic data type value (green wire) to the MOVE block's Direction setting.

The complete program and the Sound sensor's configuration panel

Figure 9-19. The complete program and the Sound sensor's configuration panel

The Ultrasonic sensor block's configuration panel

Figure 9-20. The Ultrasonic sensor block's configuration panel

The MOVE block's configuration panel

Figure 9-21. The MOVE block's configuration panel

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset