Chapter 8. True or False?

What is the difference between the following two questions?

  • What color is the sky?

  • Is the sky blue?

Well, there are a lot of differences: the first question has five words and the second one has four words, for example. My real point in asking these two questions is to point out that the first question is open-ended; the sky could be blue, or gray, or any number of answers. The second question, however, has only two possible answers: Yes or No.

When it comes to programming your robots, you need to understand that many times your robots can only provide you with those two answers: Yes or No. At other times, your robots can understand only a Yes or No answer. Understanding how to take a Yes/No answer and use it to properly program your bots is the focus of this short chapter.

One or the Other

Let's have a question/answer session with SPOT:

  • Me: SPOT, what color is the box in front of you?

  • [SPOT sits there and gives no response.]

  • Me: SPOT, what is the position of your Touch sensor button?

  • [SPOT still sits there and gives no response.]

Hmmm ... SPOT doesn't seem to be too responsive today. I seem to remember, however, that SPOT prefers Yes/No questions, so let me try this again:

  • Me: SPOT, is the color of the box in front of you blue?

  • SPOT: Yes [appears on the LCD screen].

  • Me: SPOT, is your Touch sensor button pressed?

  • SPOT: No [appears on the LCD screen].

OK, now we're getting somewhere. SPOT does prefer to communicate with me using Yes or No answers. Another way of saying this is that SPOT prefers to communicate using logical responses; a logical response is simply Yes or No.

Note

Some computers and robots use True or False, but it's all the same: Yes = True and No = False. Some computers and robots even use 1 or 0 as a logical response, where 1= True and 0 = False. There's even another method: On or Off! In that case, On = True and Off = False. But for the purposes of this chapter and programming, let's stick with either Yes/No or True/False.

Sensors: Yes or No

Let's have another conversation with SPOT:

  • Me: SPOT, is your Ultrasonic sensor detecting an object six inches in front of you?

  • SPOT: True.

  • Me: SPOT, is your Right button being pressed?

  • SPOT: False.

Apparently, SPOT's sensors have the ability to return a logical response to SPOT that he can pass along to me. SPOT listens to his sensors' conditions and responds with True or False.

What does all this have to do with programming, though? Here's your answer: your NXT robots can send and receive logical responses to and from the sensors, motors, buttons, and other items. As an example, take a look at Figure 8-1.

What you are looking at in Figure 8-1 is the TOUCH SENSOR block and its configuration panel. Notice also that the TOUCH SENSOR block has its data hub visible (see Chapter 7 for a discussion of data hubs and Chapter 9 for a more detailed discussion of the sensors). If you are seeing only one data output plug on the data hub, click the data hub again to expand it to its full size.

On the data hub, you'll see some small input and output data plugs. If you hover the mouse pointer over the third plug from the top, the words "Yes/No" will appear. What this tells you is that this data plug can provide output (using a data wire) in the form of a Logic data type.

But how do you know if the output will be Yes or No? How do you know what it means when a sensor returns one answer or the other? Simple—the answer is based on what you are monitoring with the sensor.

In Figure 8-1, notice that the Touch sensor's Action section has the Pressed option selected. This means that if the Touch sensor's button is pressed down (and not released) the Yes/No Logic plug will provide a Yes answer. If the button is not pressed, the Yes/No Logic plug will provide a No answer.

The Touch sensor's configuration panel

Figure 8-1. The Touch sensor's configuration panel

Variables and the Logic Type

Think back to Chapter 7; I told you that when you connect two blocks with a wire, the input and output data plugs must be carrying the same data type. In this case, if you wish to connect a wire from the Yes/No data plug, it must be going into a block that has a Logic data type input plug. As an example, take a look at the new block in Figure 8-2.

I've dropped a VARIABLE block on the beam.

Figure 8-2. I've dropped a VARIABLE block on the beam.

This new block is a VARIABLE block. I cover this block in more detail in Chapter 18, but for now all you need to know about the VARIABLE block is that it can hold one of the three data types: Logic, Number, or Text. In Figure 8-2, I've configured the VARIABLE block to hold a Logic value. I've also opened the data hub, so you can see that it has a Yes/No input data plug. All that I need to do is connect it to the TOUCH SENSOR block with a wire (see Figure 8-3).

Connecting two blocks with a wire

Figure 8-3. Connecting two blocks with a wire

When connecting blocks in a program using data wires, always keep in mind that a data wire will work only if it is connected to input and output plugs that expect the same data type (Logic, Number, or Text). I also need to point out that many blocks hold either a True or False value as a default setting. For example, the VARIABLE block in Figure 8-2 is configured to hold a default value of False. But you could easily change this to True (I've got more examples, later in the book, where you'll configure True/False values).

The Logic data type can be found in many blocks, especially the sensor blocks (see Figure 8-4). The sensor blocks all have a data plug that provides a Yes/No response. These plugs are designated by a check mark and an "X," which symbolize the Yes/No response.

These four sensors (Touch, Sound, Light, and Ultrasonic) all have Logic data plugs.

Figure 8-4. These four sensors (Touch, Sound, Light, and Ultrasonic) all have Logic data plugs.

Note

The Sound sensor is not included with the NXT 2.0 robotics kit. It has been replaced with an extra Touch sensor. Likewise, the NXT 2.0 kit does not come with the Light sensor but it does come with the Color sensor, which can be also be configured to work as a Light sensor.

Where the Logic data type really comes in handy, however, is with the LOOP and SWITCH blocks (these are covered in Chapters 11 and 12, respectively). Logic data types are very useful when programming a bot to make decisions on its own, and the LOOP and SWITCH blocks can both use a Yes/No response (as input) to give your robot more complex behaviors and control of itself. The bot can examine a sensor, motor, or other type of input and, based on the Yes/No response, make further decisions about what it does next.

Using Logic to Configure Settings

Before we leave this chapter, let's take a look at a few more examples of NXT-G blocks that use the Yes/No Logic data type. Keep in mind that many blocks use a Yes or No answer to control actions. In these cases, a Yes or No answer isn't so much an affirmative or negative response as it is simply a way for a block to distinguish between two actions. Here's a perfect example of this situation. Figure 8-5 shows a MOVE block's data hub.

The MOVE block has two data ports that use the Logic data type.

Figure 8-5. The MOVE block has two data ports that use the Logic data type.

For the MOVE block, you can configure the direction (forward or backward) using the configuration panel; you simply click the Up or Down arrow in the Direction section to assign a spin direction. But the direction of spin can also be controlled by sending a Yes or No signal to the Direction Data Plug (see Figure 8-5). A Yes signal is interpreted as Forward and a No signal is interpreted as Backward. The same concept works for the Next Action section—you can use a Yes signal to indicate that the robot should brake when the spin action is completed or a No signal to tell the robot to coast!

How might you use this? I've created a small program in Figure 8-6. It has the Touch sensor mentioned earlier in this chapter. You'll learn all about this sensor in Chapter 9, but for now focus on the Touch sensor's data hub. I've dragged a wire from its Yes/No output plug into the MOVE block's Direction input plug.

This program uses the Touch sensor to control the motors' spin direction.

Figure 8-6. This program uses the Touch sensor to control the motors' spin direction.

This program doesn't do much other than spin the motors forward or backward for ten seconds, depending on whether the Touch sensor button is pressed. When I run the program, if I hold down the Touch sensor button the motors will spin in the forward direction for ten seconds before the program ends. You can see in the Touch sensor configuration panel in Figure 8-6 that I could have changed the Action to Released by selecting that option. If I had done this, what do you think would happen if I pressed the button when I started the program?

Think about it for second—if the Action the Touch sensor is testing for is set to "Released," then when the button is pressed, it is not released, right? That means the Touch sensor will send a No signal (or False) to the MOVE block. A No signal means the motors will spin backwards!

You'll learn all about the various sensors and how they work in the next chapter. Once you've got a good grasp of their functions and how their data hubs work, you'll be able to use wires to send Yes and No signals to other blocks and give your robots some amazing decision-making abilities. Your robots will be able to interact with objects, avoid walls, detect light or dark rooms, and much more. And many of these abilities hinge on your robot being able to use Yes and No logic responses to make decisions.

Exercise 11

Before we move on to Chapter 9, I've got an exercise for you that should help you better understand how to interpret a Yes/No signal and determine what it will do. If you get stumped, I've provided the answer at the end of this chapter.

Figure 8-7 shows a small program I've created and the four configuration panels for the four NXT-G blocks. Examine it and determine what actions the robot will take given the following situation:

  • Touch sensor button is NOT pressed

  • Light sensor detects the room's lights are turned on

Look carefully, as the settings are a little tricky!

The complete program and the Touch sensor's configuration panel

Figure 8-7. The complete program and the Touch sensor's configuration panel

The Light sensor's configuration panel

Figure 8-8. The Light sensor's configuration panel

The MOVE block's configuration panel

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

The WAIT TIME block's configuration panel

Figure 8-10. The WAIT TIME block's configuration panel

What's Next?

And that's it for this short chapter on logic. As I said, you'll get a more detailed description of how to use the Logic data type with the LOOP and SWITCH blocks in Chapters 11 and 12. Now let's change direction in Chapter 9 and take a look at some more items that can be used to communicate with the NXT Brick: sensors, buttons, and timers.

Exercise Solution

Let's go over each block before deciding what will happen when the program is executed. First, the Touch sensor block is configured to send a Yes signal to the MOVE block's Next Action input port (in Figure 8-7) if the button is Pressed. Next, the Light sensor is configured to send a Yes signal to the MOVE block's Direction input port (also seen in Figure 8-7) if the room's lights are turned on. (Without going into too much detail, I've set the sensor to detect a light reading of 50 or greater (see Figure 8-8); the Light sensor detects the light level in the room and assigns a value between 0 and 100. If the lights are on and bright enough, the reading will usually be around 70–80, meaning the Light sensor will detect a light value greater than 50 and send a Yes (or True) signal to the MOVE block.)

Now, examine the MOVE block's configuration panel in Figure 8-9. I've configured the MOVE block to spin motors B and C forward and to Coast when done. (The WAIT TIME block will allow the motors to complete their action before waiting for five seconds and stopping the program).

So, if the Touch sensor button is pressed (a YES signal), the MOVE block's Next Action input data plug will receive it. A Yes signal tells the motors to Brake. But didn't I use the configuration panel to set the motors to Coast? Yes, but the configuration panel's setting will always be ignored if a wire is used to receive a signal. So, the Yes signal will override the Coast configuration panel setting and have the robot brake instead.

What about the Light sensor? Well, it's sending a Yes signal to the Direction input data plug, which corresponds to a Forward setting. I had already configured the MOVE block for a Forward direction (see Figure 8-9) so this setting will not be changed.

So, when the program runs, the motors will spin for three rotations (at a Power setting of 50) and then the motors will brake. The robot will sit for five seconds and then the program will end.

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

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