Chapter 25. Messages

There may come a time when you'd like your NXT robot to be able to communicate and share information with other NXT robots. Or, if you have a second NXT kit, you could build a remote control for your robot and give it commands using the second NXT brick.

To make all this happen requires a handful of blocks that will be covered in this chapter. Many people find these blocks difficult or confusing, but they're really very simple if you understand the basic concept behind sending and receiving messages.

Of course, sending and receiving messages requires that the NXT bricks be connected via a Bluetooth connection. Connecting two or more NXT bricks via Bluetooth is fairly straightforward and I'll cover the basics here, but if you need more help with this task, check the built-in Help file with your NXT software. You can find instructions for connecting two or more NXT devices using the BLUETOOTH CONNECTION block in addition to the SEND MESSAGE or RECEIVE MESSAGE block's documentation.

The BLUETOOTH CONNECTION Block

If you have more than one NXT brick, you can use a variety of NXT-G blocks to allow communication between them. NXT robots can use Bluetooth communication to share information such as distances or recorded sensor values (the brightness of a room, for example). Using Bluetooth is fun, but it does have a small disadvantage in that it can drain the batteries when it's being used.

Note

Without getting too technical, Bluetooth is a way for devices to communicate wirelessly over short distances. Bluetooth requires that both devices identify themselves to one another and that a password (also called a passcode) be swapped to give the devices permission to communicate. For more information (and a slightly more technical discussion), visit http://en.wikipedia.org/wiki/Bluetooth.

Some Preliminaries

You can turn Bluetooth on using the brick's buttons and menu system; you'll use this same menu system to find other NXT bricks and perform what's called Discovery and Handshake. Discovery and Handshake is simply allowing each NXT brick to identify itself to other bricks and then exchanging passwords that will allow bricks to securely communicate. The Discovery part is where the two bricks send out signals for the other device to listen for and receive. Once the devices "see" one another, they must exchange a password (also called a passcode) in order to obtain permission to start communicating and sharing information.

Note

If you wish to have your NXT brick communicate with one or more NXT bricks, you will need to make certain that you have gone through the Discovery and Handshake process. Ideally, you'll want to do this while your brick is connected to your computer or laptop so that a list of other NXT devices can be gathered and listed in the NXT-G software. Consult the Help documentation for more information on how to perform this task.

Once the Discovery and Handshake process is completed by both bricks, you can use NXT-G programs to send and receive data between bricks. Sending and receiving information is covered a bit later in this chapter, but with NXT-G 2.0, you can now turn on and off the Bluetooth communication ability. This is a useful feature that will help save battery power.

Configuring a Connection

Figure 25-1 shows the BLUETOOTH CONNECTION block and its single setting, which you can configure.

The BLUETOOTH CONNECTION block and configuration panel

Figure 25-1. The BLUETOOTH CONNECTION block and configuration panel

The Action section's drop-down menu offers four options: Turn On, Turn Off, Initiate Connection, and Close Connection.

The Turn On and Turn Off options do exactly as you'd imagine. Turn On will turn on the Bluetooth function on your brick, allowing it to communicate with any other bricks that have been identified and exchanged passwords with. Turn Off will disable the Bluetooth function on your brick. If you intend to use an NXT-G program to communicate and send and receive data with another brick, I highly encourage you to use the Turn On and Turn Off ability. You simply drop a BLUETOOTH CONNECTION block at the beginning of the program and configure its Action setting to Turn On, and drop another BLUETOOTH CONNECTION block at the end of the program and configure its Action setting to Turn Off. By doing so, you won't have to remember to turn off the Bluetooth feature on the brick.

During the Discovery and Handshake task, you will need to make note of the ID of the other NXT bricks. The ID is the same thing as the brick's Name. The Name is stored on your NXT brick and appears at the top of the LCD screen when your brick is turned on. You can also view the Name in the NXT Data window shown in Figure 25-2. Click the button indicated in Figure 25-2 to open up the NXT Data window.

The NXT data window will show you the name of your connected brick.

Figure 25-2. The NXT data window will show you the name of your connected brick.

You may notice that there are two devices in the list, both with the name NXT. Ideally, all bricks should have unique names. You can change the name of the currently connected brick by typing a new one in the Name field in the upper-right corner of the window, shown in Figure 25-3. I'm changing this brick's name to Jim. Now I can give that name to other NXT brick owners and they will be able to use it to send and receive data—but only after one more task is performed.

Rename your brick to something unique and easy to remember.

Figure 25-3. Rename your brick to something unique and easy to remember.

Initiating a Connection

Once all the bricks have identified themselves to one another and exchanged passwords entered by their owners, you will still need to tell your program the name of the brick with which you wish to communicate. This is especially important if you have multiple NXT bricks.

Fortunately, it's extremely simple. Drop in another BLUETOOTH CONNECTION block early in your program and select the "Initiate Connection" setting in the Action section, as shown in Figure 25-4.

Initiating connection with another brick to share data

Figure 25-4. Initiating connection with another brick to share data

Type in the name of another brick (in this case, John) in the Connect To section and select a Connection port—1, 2, or 3. As you can see, this will allow your NXT brick to communicate with up to three additional bricks, each having its own communication port. You'll see in a moment how this is useful when sending and receiving data between bricks.

Closing a Connection

Finally, if you are done sending and receiving information from another NXT brick, it's useful to close a connection with another brick using the "Close Connection" option in the Action section. This can be useful to the other NXT brick's owner as it will free up a communication port that might be needed for that owner to communicate with another brick (not yours). Figure 25-5 shows that I've closed the connection with the brick named John. The Connections section on the configuration panel will also show you any other NXT bricks that have been identified and are in communication with your brick.

Closing a connection will free up a communication port on another brick.

Figure 25-5. Closing a connection will free up a communication port on another brick.

You may have noticed the Contacts section in Figure 25-4. This list will show all the names of any NXT bricks that have been connected to your laptop or computer. It can be useful to you to ask the other NXT brick owners to connect their bricks to your laptop or computer so the name of their bricks will be stored in this list. Otherwise, you'll have to type in the name manually when initiating a connection.

So, now that you know how to turn Bluetooth on and off and how to select the particular brick you wish to communicate with, it's time to learn how to actually send and receive data between bricks. This is done with two easy-to-remember blocks, the SEND MESSAGE and RECEIVE MESSAGE blocks.

The SEND MESSAGE Block

The SEND MESSAGE block is shown in Figure 25-6 along with its configuration panel.

The SEND MESSAGE block and its configuration panel

Figure 25-6. The SEND MESSAGE block and its configuration panel

There are a couple of items you really need to understand in the configuration panel. The first one is found in the Message section. The drop-down menu has three options: Text, Number, or Logic. By selecting one of these, you supply the correct type of message in the text box immediately below the drop-down menu. If you choose Text in the drop-down, the text box must contain some form of text content. Likewise, if you choose Number in the drop-down menu, the text box must contain a positive or negative integer value. And last, selecting Logic from the drop-down menu requires that you must select the True or False value.

Note

As with most of the advanced NXT blocks, the SEND MESSAGE block can be configured to receive Text, Number, or Logic values using data wires, as shown in Figure 25-6. Note that the Connection data plug is a value between 0 and 3 and is used to identify the master or slave NXT device. The master device is always configured to use the value 0, and up to three additional NXT bricks can be linked via Bluetooth, with each being identified by 1, 2, or 3. Earlier in the chapter you saw how the BLUETOOTH CONNECTION block is used to select a communication port—1, 2, or 3—to assign to a brick. When sending and receiving data between bricks, you'll want to pay attention to the port configured in the SEND MESSAGE and RECEIVE MESSAGE blocks so you get the proper data from the proper brick.

Now, this is a great way for one NXT brick to send a single item to another brick. One NXT brick could, for example, monitor the number of times the Touch sensor is triggered and send this value to another robot. But what if you want to send even more information? What about sending the Light sensor's value as well as the number of rotations motor B has made to the second robot? Is this possible?

Well, if you think of each potential message as a variable (covered in Chapter 18), then the answer is "Yes." The SEND MESSAGE and RECEIVE MESSAGE blocks allow for up to ten unique messages to be sent back and forth between bricks using a concept called mailboxes.

Each NXT brick has ten mailboxes, and each mailbox is numbered. The first mailbox is called Mailbox 1; the second mailbox is called Mailbox 2, and so on. If you look at the configuration panel for the SEND MESSAGE block, you'll see that the last section is called Mailbox, and it contains a drop-down menu. Select this drop-down menu, and you'll see that you can select from numbers 1 to 10. It's simple! When you place a Text, Number, or Logic value in Mailbox 3, for example, that value is held in that mailbox (remember, a Text value can be a single character or multiple characters but is still considered a single value to a mailbox). Up to five values can be held per mailbox, but the values held must be of the same type (Text, Number, or Logic). If, for example, a SEND MESSAGE block attempts to put a sixth value in Mailbox 5, the first value inserted into Mailbox 5 drops out and is lost. So be careful about putting too many values into a Mailbox before they are retrieved!

Now, can you guess how the other robots will retrieve a value held in a mailbox? If you said by using a RECEIVE MESSAGE block, you're right. Take a look at Figure 25-7, and you'll see the RECEIVE MESSAGE block and its configuration panel.

The RECEIVE MESSAGE block and its configuration panel

Figure 25-7. The RECEIVE MESSAGE block and its configuration panel

The RECEIVE MESSAGE block works similarly to the SEND MESSAGE block. Select the mailbox number from the drop-down menu in the Mailbox section. Again, the mailboxes are numbered 1 to 10. You must also select the type of data you are expecting to receive from the SEND MESSAGE block (Text, Number, or Logic) in the drop-down menu in the Message section.

You'll notice that in the Message section there is a "Compare to" text box. If you enter a value in this box, a True or False logic value will be determined, and using an output data wire, you can obtain this logic value to give it to another block. For example, if Mailbox 3 is holding a Number data type value of 250, your RECEIVE MESSAGE block is configured to read a Number value from Mailbox 3, and you've entered 250 in the "Compare to" text box, a True value will result from the comparison.

Now, for a quick example of a simple program, let's take the following pseudo-code and convert it to an NXT-G program:

  • Me: SPOT, move forward until your Ultrasonic sensor detects an object at least 10 inches in front of you.

  • Me: Read the value on your Light sensor.

  • Me: Send the Light sensor Intensity value to SPOT2 using Mailbox 3.

As you can see from Figure 25-8, I've created a simple program that causes SPOT to start moving forward (unlimited duration) while monitoring his Ultrasonic sensor. When the Ultrasonic sensor is triggered, the second MOVE block stops motors B and C. The Light sensor takes a reading, and this value is sent to the SEND MESSAGE block using a data wire configured to hold a Number value.

A program using the SEND MESSAGE block

Figure 25-8. A program using the SEND MESSAGE block

Now, if I have a second robot (called SPOT2), I can convert the following pseudo-code into another NXT-G program that will obtain the Light sensor value:

  • Me: SPOT2, check Mailbox 3 for a Number value.

  • Me: Compare that value to your Light sensor reading.

  • Me: If your Light sensor reading matches the value in Mailbox 3, move forward eight rotations.

Now, take a look at Figure 25-9 to see how I've converted the pseudo-code to an NXT-G program.

A program using the RECEIVE MESSAGE block

Figure 25-9. A program using the RECEIVE MESSAGE block

In this program, SPOT2's Light sensor takes a reading, and this value is passed to the RECEIVE MESSAGE block using a data wire. Now, when SPOT2 checks Mailbox 3 and reads the value, a comparison is made between the stored value and the value obtained by SPOT2's Light sensor. This comparison is done automatically, because a number value has been provided to the RECEIVE MESSAGE block's Number data port, and a data wire has been dragged out of the RECEIVE MESSAGE block's Logic data port and into the SWITCH block. This setup forces the block to do a comparison of the two values (the one obtained by the Light sensor and the Number configured in the configuration panel). If these values are equal, a True value is passed to the SWITCH block. The True tab is shown in Figure 25-9, and it includes a MOVE block that will move SPOT2 forward eight rotations if Mailbox 3's value and SPOT2's Light sensor values are identical.

The SEND MESSAGE block and the RECEIVE MESSAGE block are absolutely essential for two or more NXT robots to communicate and share information. This chapter has barely scratched the surface of what can be done using these two blocks. I mentioned earlier that with a second NXT kit, you can build a remote control for your robot. In order to build a useful remote control, you need to use the SEND MESSAGE blocks in your remote control to send numeric values to the robot. These values could represent the number of rotations or degrees for motors B and C to spin, for example. Or they could represent the speed/power of the motors to spin. There are numerous ways to do this, and there really is no incorrect method—just pick the one that works best for you. And since you have three communication ports (1, 2, and 3), you could theoretically remote control up to three robots! Just remember to use BLUETOOTH CONNECTION blocks to specify the communication port number for each brick that you wish to send data. You can use the built-in Bluetooth controls on the brick's menu system, but using blocks will make it easier and faster to test as you create your program—no more fiddling with the buttons on the brick to find and link up to other bricks!

What's Next?

Well, we've got one more item to cover—it's called the My Block. And you're going to find it very useful for creating small programs that can be used over and over again in future robots.

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

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