Chapter 5. Record and Playback

One nice feature of the NXT-G programming language is the RECORD/PLAY block. With this block, you can record the movements of your bot's motors to a file that is stored on the Brick. This file can be used to later play back the bot's movement.

For this chapter, we'll use SPOT again. But this time, I'm going to add one additional motor (motor A) to make SPOT do something silly, like spin an arm or a sensor around. Feel free to do what you like. My SPOT has motor B (in Port B) spinning the left wheel of my robot and motor C (in Port C) spinning the right wheel. I have motor A (in Port A) spinning a small propeller (like an airplane) on the front of the robot (see Figure 5-1).

SPOT with his new propeller

Figure 5-1. SPOT with his new propeller

The RECORD/PLAY Block

I'm ready to record some basic movement. If you've built your own version of SPOT, follow along.

The RECORD/PLAY block is located on the Common Palette directly below the MOVE block. Drag and drop a RECORD/PLAY block on the beam (see Figure 5-2).

Start recording a bot's movements with the RECORD/PLAY block.

Figure 5-2. Start recording a bot's movements with the RECORD/PLAY block.

I know it seems like common sense, but I still need to say it: we cannot play back SPOT's recorded movements until we've actually recorded some. So the first thing we need to do is configure the RECORD/PLAY block to record SPOT's movements. To do this, in the block's Action section, select the Record option as shown in Figure 5-3. This is the default setting when you drop a RECORD/PLAY block onto the workspace.

First, choose the Record option in the Action section.

Figure 5-3. First, choose the Record option in the Action section.

Next, we need to specify a name for the recorded movement. As an example, I want SPOT to move forward two feet (motors B and C will be spinning forward) and turn left. I then want motor A to spin the propeller a few times. I'm going to type the words Takeoff in the Name text box, shown in Figure 5-4, but you can type whatever description you like that will help you remember the purpose of the recorded movement.

Give your recorded movement a unique name.

Figure 5-4. Give your recorded movement a unique name.

The name you type in the Name text box is the name of a file that will be stored on the Brick. This file must be stored on the Brick in order for you to later play back the movement, so try to make the name memorable and easy to understand.

Now, look at Figure 5-5. The Recording section of the configuration panel is where you will specify which ports should be monitored. In my example, motors B and C will move my robot around, and motor A will spin the propeller. So I want to select all the ports. If you are not using one of the motor ports, you don't need to select it. You won't get an error if you select a motor port and don't use it, however.

Configure the motor ports to monitor and record.

Figure 5-5. Configure the motor ports to monitor and record.

The last item you will need to configure is the amount of time (in seconds) you wish to record your bot's movements (see Figure 5-6). You can type in the number of seconds you want to record or click the up and down arrows with your mouse to select the number in the Time section.

Enter the number of seconds to record in the Time section.

Figure 5-6. Enter the number of seconds to record in the Time section.

You can record anywhere from one second up into the hundreds of minutes. Is this realistic? Not really. Your NXT Brick has a limited amount of memory, and you'll find that you are limited to a few minutes at most. And even recording a few minutes of movement will probably not leave much memory for your actual program. You'll have to play around with the Time section to test its limits.

Recording Movement

Once you've got your RECORD/PLAY block configured, save the program, and upload it to your NXT bot. Place the robot at its starting position, and press the Run button for your new program. Using your hands, guide the robot through the movements you wish your robot to perform.

For my example, I simply push the robot forward two feet and stop. I then turn the robot to the left and stop. Next, I spin the little propeller on the front of the robot five or six times, and I'm finished.

I suggest that you time your movements as you're doing them. If you come close to the number of seconds you configured, you can simply leave the recording time alone. If you didn't have enough time, go back and add the right number of seconds to your program, save it, and run it again to record the complete movements. Most importantly, if you originally configured too much time, reduce the number of seconds you entered in the Time section; because the recording process will continue to run until the time is over, the file stored on the Brick will be larger than it needs to be.

Replaying Movement

Okay, so you've successfully recorded your bot's movements, and there is a file stored on the Brick with the name you gave it in the Name section (you can verify this by connecting your Brick to your computer and checking its memory contents). Now, let me show you how to play back the file.

It's so easy, you're going to laugh. Create a new program, and drop in a RECORD/PLAY block. This time, however, select the Play option in the Action section (see Figure 5-7).

Note

If you have your NXT Brick connected to your computer via the USB cable, any files stored on the NXT that you created using the RECORD/PLAY block will appear in the File section seen in Figure 5-7. Rather than type the name of the file, you can select it from the list. If you do not have your NXT Brick connected, you will need to remember the name of the file and type it in the Name section.

Configure your robot to play back the recorded movement.

Figure 5-7. Configure your robot to play back the recorded movement.

The only other section that can be configured now is the Name section. Type the name of the file that contains the recorded movements in the Name section (see Figure 5-8). For my example, I've typed Takeoff, the name I gave the file that moves the robot forward two feet, turns it left, and then spins the propeller a few times.

Enter the name of the file you created during the Record process.

Figure 5-8. Enter the name of the file you created during the Record process.

Next, you need to save the new program and upload it to your Brick. Before you run the program, place your robot in the original starting position (or wherever you like), and press the Run button to run the program. The robot will begin to move and will match the movements you recorded earlier. That's it for the RECORD/PLAY block.

Having Fun with Record/Play

Here are some ideas for using the RECORD/PLAY block:

  • A fun use for it is to record your robot doing some sort of dance (for ten to twenty seconds) and save it to a file called Dance. If you keep the Dance file on your Brick, you can drop in a RECORD/PLAY block anywhere in your program and have your robot do the little dance (you can drop it in multiple times, too).

  • Teams could use this block when giving a presentation. The robot might have interesting parts and mechanisms that you wish to focus attention on, and the RECORD/PLAY block could be used to let the audience view these more easily. Configure the times properly, and you can synchronize it to a speech given on the robot and its different components.

Exercises

Now, let's try some exercises using what we know. If you need solutions to the following exercises, I've placed them at the end of this chapter.

Exercise 5-1: Record a Cha-cha-cha Movement

Record your robot performing a short (3–4 seconds) cha-cha-cha movement—just wiggling back and forth in short movements. Have the file saved as "ChaChaCha" on the Brick.

Exercise 5-2: Roll Forwards and Backwards First

Program your robot to roll forwards a few inches and then backwards a few inches using MOVE blocks... and then do a cha-cha-cha movement. Have your robot perform this set of actions one more time.

What's Next?

Chapter 6 will show you how to give your robot the ability to talk and make some noise! While this isn't a requirement for your robot, adding sound makes a robot much more interesting. There's even a way to add your own sounds and music to the NXT (for NXT 2.0 owners)! I'll show you how all this is done next.

Possible Solutions to Exercises

Below are the solutions for the exercises I gave you earlier. Keep in mind that you can modify these exercises and try new things—it's a great way to learn.

Exercise 7

Figure 5-9 contains the program and the single configuration panel used in this program. Be sure to write down the name you used for the file as you'll need it for Exercise 8. Save this program and upload it to your robot. Run the program and perform the back and forth movements for no more than five seconds.

A single RECORD/PLAY block is required to record a movement.

Figure 5-9. A single RECORD/PLAY block is required to record a movement.

Exercise 8

Figures 5-10 through 5-15 contain the program and the configuration panels for the blocks used in this program. Note that although the MOVE blocks look similar, the only change between the first and second MOVE blocks is the Direction setting (and the same goes for the third and fourth MOVE blocks). This instructs the robot to roll forward one rotation and then roll backwards one rotation... and then do the cha-cha-cha movement. The last three blocks are simply repeats of the first three.

The complete program and the first MOVE block's configuration panel

Figure 5-10. The complete program and the first MOVE block's configuration panel

The second MOVE block's configuration panel

Figure 5-11. The second MOVE block's configuration panel

The first RECORD/PLAY block's configuration panel

Figure 5-12. The first RECORD/PLAY block's configuration panel

The third MOVE block's configuration panel

Figure 5-13. The third MOVE block's configuration panel

The fourth MOVE block's configuration panel

Figure 5-14. The fourth MOVE block's configuration panel

The final RECORD/PLAY block's configuration panel

Figure 5-15. The final RECORD/PLAY 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