Chapter 2. Program Structure

I don't really like using technical terms like "program structure," but it is a very useful concept that will benefit you as you begin to program your robots. The word structure implies some sort of organization or plan, doesn't it? By the time you finish this chapter, you should understand the importance of program structure to you and your robots.

What Do I Mean by Structure?

In Chapter 1, I gave you some examples of real-world programs. Would the following example have made any sense?

  • Teacher: Class, open your books to page 55.

  • [The class looks confused.]

  • Teacher: Class, I want you to get out your math books.

  • [Giving the teacher confused looks, the students get out their books.]

How can you read page 55 if you haven't yet been told which book to open? You might answer, "Yes, but I'm in math class, and the teacher said turn to page 55. So I'm sure the teacher means my math book!"

That's true. As a human, you are able to figure out certain instructions on your own. But remember—robots aren't that smart! They need to be given very strict and specific instructions. And those instructions need to be given in a specific order. That order is another way of saying "program structure."

Think of a house. You can't have a second floor without a first floor to build on top of, right? And you can't install a roof before you pour the concrete floor and build some walls. There's an order to how things are done when you build a house. The same goes for programming a robot.

Planning Your Program's Structure

Let's get out SPOT for another example. He's still doing his one and only trick—sit. You're not quite ready to upload an NXT-G program yet, but let's do some preplanning at this stage. I want you to use something that computer programmers call pseudo-code. What is pseudo-code? Well, the definition of "pseudo" is fake (as in pretend, simulated, virtual—get the idea?); it's not real. And "code" is simply another word for program. Put it all together and one way of looking at pseudo-code is this: fake program.

Our fake program isn't going to be written using NXT-G. The best way I can tell you to start creating a fake program is to pretend that SPOT has ears and tell SPOT what you want him to do. Let's try writing some pseudo-code using a numbered list:

  1. SPOT, move forward until your Touch sensor is pressed and released. Then, stop.

  2. OK, SPOT, I want you to turn left 90 degrees.

  3. Now, I want you to give a loud Beep!

  4. Good job, SPOT. Now move backward until your Light sensor detects something black. Then, stop.

  5. Now, SPOT, do a little dance.

That's pseudo-code? Well, it's a form of pseudo-code. Remember how I told you there are different programming languages? People write pseudo-code differently, too. The point I want you to understand is that before you can really program your robot using NXT-G, you need to have an idea of exactly what your robot will be doing. The easiest way to do this is to simply write down, in simple language, instructions for your robot. That's the beginning of a good structure for the future NXT-G program.

When you write pseudo-code, you are accomplishing three things:

  • You are gaining a better understanding of the tasks your robot will perform.

  • You are looking at the world from the robot's point of view which is important when programming.

  • You are creating an ordered set of instructions (structure) for your robot to follow.

You will use this pseudo-code to assist you when you begin to create your program with NXT-G. One final thing I want to mention about pseudo-code is that each instruction you give the robot should be as simple as possible. Take a look at the next two examples and tell me which one has the simpler instructions:

  • Example 1: SPOT, move forward about 10 inches; turn left 90 degrees, and start moving forward; then start looking for a black object with your Ultrasonic sensor, because I want you to stop when you find a black object; then turn right 90 degrees, and move backward 2 feet, OK?

  • Example 2:

    1. SPOT, move forward 10 inches and stop.

    2. Now turn left 90 degrees.

    3. Starting moving forward, and turn on your Ultrasonic sensor.

    4. Stop when you find a black object.

    5. Turn right 90 degrees and stop.

    6. Now move backwards 2 feet and stop.

Which example is less complicated to read? If you said Example 2, you are right. Let's be honest—some humans would be confused if you gave them the instructions in Example 1! When writing pseudo-code, break down your instructions into short and simple statements for your robot. This will make it easier for you to convert your pseudo-code to an actual NXT-G program.

Moving Into Real Code

Are you wondering how you convert pseudo-code to a real NXT-G program? Let me give you a small preview of what's to come in the chapters ahead.

Take a look back at my original pseudo-code for SPOT and read step 4, "Now move backward until your Light sensor detects something black. Then, stop."

If I am programming in NXT-G and am familiar with all the tools it contains, I would realize that there are tools (called blocks) that match up to my pseudo-code. Why are they called blocks? Take a look at Figure 2-1.

A collection of NXT-G blocks that make up a program

Figure 2-1. A collection of NXT-G blocks that make up a program

See those three squares located in the center of the NXT-G software screen in Figure 2-1? Those are blocks! The block on the left (with gears) is the MOVE block. The block in the middle (with the little speaker icon) is the SOUND block. And the block on the right is the SENSOR block—its icon tells me that it's the Light sensor block. Don't worry, you'll learn about all these blocks and more in future chapters (the SOUND block is covered in Chapter 6 and the sensors are covered in Chapter 9), but for now I just wanted you to understand what a block is and what it looks like.

When I want SPOT to move backward, he's going to use his motors, right? Well, I'll be using something called a MOVE block. The MOVE block will allow me to program my robot to spin the motors (and wheels) in reverse, so SPOT moves backward.

I only want SPOT to back up until his Light sensor detects the color black. To do this, I'll use something called a SENSOR block to monitor the Light sensor. The SENSOR block will be programmed to look for the color black.

Finally, I want SPOT to stop when the SENSOR block detects the color black. For this, I can use another MOVE block that tells the motors to stop spinning.

You will use these blocks and many more to properly program your robot to follow your instructions. This book will teach you about all the different NXT-G blocks, so you'll know which ones to use when converting your pseudo-code to an NXT-G program.

If you can remember one thing from this chapter, it should be this: Programming your robot will be much easier if you take the time to write down the pseudo-code. If it helps, pretend your robot has ears, and tell it what you want it to do. Write down these instructions, and keep them short and simple.

Exercises

Now, I want to give you some practice in writing pseudo code. It won't take long, but it will require a partner. Ask a friend or teacher or parent to help you with the following two exercises. (Possible solutions to the exercises in the book can be found at the end of each chapter.) If possible, switch roles with your partner for both exercises. This will help you write pseudo code as well as see how easy (or hard) it is to follow instructions.

Exercise 2-1. Move One Object Nearer to Another

Place two objects on a table (such as a pencil and a book). Space the objects with approximately two feet between them and label the object on the left Object 1 and label the object on the right Object 2. Next, have your partner sit in front of the table, with one object to his left and the other object to his right. Write down a bit of pseudo-code and give it to your partner that will instruct him or her (while seated) to pick up Object 1 and move it to a location near Object 2. Then have your partner pick up Object 2 and move it to the approximate original location of Object 1.

Exercise 2-2. Place Objects at Corners

Ask your partner to stand in one corner of a room (any corner is fine and call it Starting Point). Give your partner two objects labeled Object 1 and Object 2. Write pseudo-code that instructs your partner to move along the wall to his right and place Object 2 on the floor at the first corner he encounters. Have your partner continue moving to the next corner (not back to the Starting Point) where he will place Object 1 on the floor. Have your partner reverse direction and return to the Starting Point.

Were there any parts of your pseudo-code that confused your partner (or vice versa)? If so, don't worry. The goal here is to understand that as you program your robots, you'll always want to strive to provide as much detail as possible. The more details, the more likely it will be that your robot succeeds in its endeavors.

What's Next?

In the next chapter, you're going to write some pseudo-code and actually convert it to an NXT-G program. The key to writing excellent NXT-G programs is understanding how the NXT-G programming blocks work. When you know how the blocks work, you'll know which blocks to use when converting your pseudo-code.

Chapter 3 is going to demonstrate the DISPLAY block, a very useful block that gives your robot the ability to write things to its LCD screen for others to read.

Exercise Solutions

There is no single hard-and-fast solution to each of the exercises in this chapter. Many solutions are possible. I present two such possible solutions here. Your own solutions may be different in some details, but overall they should be similar to the ones presented here.

Exercise 2-1

Following is one possible solution to the task of instructing your partner to move one object next to another:

  1. Partner: Pick up Object 1 to your left.

  2. Partner: Place Object 1 to the right of Object 2.

  3. Partner: Pick up Object 2 on your right.

  4. Partner: Place Object 2 in the approximate original location of Object 1.

Exercise 2-2

Following is a series of steps that should result in your partner dropping one object at each of two corners. Your own solution may vary, but it should be similar to that presented here.

  1. Partner: Walk along the wall to your right and stop at the first corner you encounter.

  2. Partner: Place Object 2 on the floor.

  3. Partner: Without returning to the Starting Point, walk along the wall to the next corner and stop.

  4. Partner: Place Object 1 on the floor.

  5. Partner: Reverse your direction and, following the walls, return to the Starting Point.

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

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