Programming Lego Mindstorms NXT

Programming Lego Mindstorms NXT

by Owen Bishop B.Sc (Bristol.), B.Sc (Oxon.)
Programming Lego Mindstorms NXT

Programming Lego Mindstorms NXT

by Owen Bishop B.Sc (Bristol.), B.Sc (Oxon.)

eBook

$32.49  $42.95 Save 24% Current price is $32.49, Original price is $42.95. You Save 24%.

Available on Compatible NOOK Devices and the free NOOK Apps.
WANT A NOOK?  Explore Now

Related collections and offers


Overview

Teach your robot new tricks! With this projects-based approach you can program your Mindstorms NXT robot to solve a maze, build a house, run an obstacle course, and many other activities. Along the way you will learn the basics of programming structures and techniques using NXT-G and Microsoft VPL.

For hobbyists, and students working on robot projects, Bishop provides the background and tools to program your robot for tasks that go beyond the simple routines provided with the robot kit. The programs range in complexity from simple contact avoidance and path following, to programs generating some degree of artificial intelligence
  • a how-to guide for programming your robot, using NXT-G and Microsoft VPL
  • ten robot-specific projects show how to extend your robot's capabilities beyond the manufacturer's provided software. Examples of projects include: Maze solver, Robot House Builder, Search (obstacle avoidance), Song and Dance Act
  • flowcharts and data flow diagrams are used to illustrate how to develop programs
  • introduces basic programming structures

Product Details

ISBN-13: 9780080569963
Publisher: Elsevier Science
Publication date: 04/18/2011
Sold by: Barnes & Noble
Format: eBook
Pages: 200
File size: 5 MB

About the Author

Author of over 70 books, mostly electronic and many in the field of science education. Contributor to numerous electronic magazines such as Everyday Practical Electronics, Elektor Electronics, Electronics Australia and Electronics Today International. Former Science Education Advisor in developing countries as staff member of the British Council and as a part of the UN Educational and Scientific Organisation.

Read an Excerpt

Programming Lego Mindstorms NXT


By Owen Bishop

Syngress Publishing, Inc.

Copyright © 2008 Elsevier, Inc.
All right reserved.

ISBN: 978-0-08-056996-3


Chapter One

Roborts and Programs

Introduction

Without a program, a robot is just an assembly of electronic and mechanical components. This book shows you how to give it a program.

The LEGO® Mindstorms® kit contains the parts and directions for building the following four different types of robots:

* A vehicle called a Tribot

* A machine called a RoboArm

* An animal called Spike the scorpion

* A humanoid Alpha Rex

Of these, Alpha Rex is closest to most people's idea of what a robot is. But Tribot and RoboArm are typical of robots commonly seen in industrial situations. It is a lot of fun building them, testing them with the few simple programs that come with the kit, and then getting them to do new and interesting things by writing your own programs.

As well as these four, there are dozens of other robots waiting to be built and programmed. For example, the monkey and the mouse in the book by Dave Astolfo, Mario Ferrari, and Guilio Ferrari, "Building Robots with LEGO Mindstorms NXT" (Syngress, 2007). There are also dozens of robots that you can invent and program using the same kit of parts!

In this book, the programs and program snippets are written using the Mindstorms NXT-G language that comes with the kit. NXT-G is described as a visual language. Instead of being written in text, it is created as a diagram (see Figure 1.1). NXT-G is described in more detail in Chapter 2.

Most of the NXT-G programs in this book are based on the Tribot (Figure 1.2) and the RoboArm, because these are the most versatile robots. Also, there are several programs specially written for Alpha Rex.

What is a Program?

A program is essentially a list of instructions that tells the robot what to do and when and how to do it.

A NXT-G program does not look anything like a list (see Figure 1.3).The program is built up from icons, each icon representing one or more instructions. The icons are arranged in order on a framework. The design of each icon tells us what the icon does (see Figure 1.4). When the program is run, the icons are taken in the order in which they are on the framework, or sequence beam, and executed in that order. This is the same as having a written list of instructions and following them in the order in which they are listed.

Now comes the problem. Humans can understand the icons and sequences of icons, but robots can't. Robots only understand an arrangement of electrical charges in the electronic circuits in their memories. The program in icon form has be turned into electronic form. This is where a compiler program comes in. The compiler program is run by clicking on one of the buttons at the bottom right-hand corner of the screen. When the program has been compiled, it is read and executed by the microcontrollers in the NXT brick.

Designing a Robot

When you have finished building and programming the robots that come with the kit, you will almost certainly want to design and build robots of your own. This is when things get really exciting. When you are designing your own robots, the first question that must be asked is "What do I want it to do?". Do I want it to pick up objects? If so, it will need one or more arms with grippers (see Figure 1.5). Do I want it to move around? If so, it needs to have wheels, or legs, if its environment includes steps or rough surfaces. Does it need an ultrasonic sensor for navigation? Does it need an audio sensor for receiving spoken commands?

By considering these types of questions you build up a specification for the robot that is related to what it has to do and where it has to operate.

Specify the Robot's Environment

Animals (including humans) adapt to the environment in which they live. It is different with robots. Sometimes it makes things simpler if we adapt the environment to the robot instead of adapting the robot to the environment. One example is if the robot is designed to find its way to a source of light, it's a good idea to make sure there is only one source of light in the room. Later you can try to get it to distinguish between one bright source of light and other sources of light. Give it a simple environment to start with, and when the program is working properly you can try improving it to work in a more complex environment. Start with something easy and progress to something more difficult.

In the case of a robot that handles objects, it may be a good idea for the objects to be all the same size and shape. Later you can try programming the robot to handle objects of different sizes and shapes. If the robot has to move from one place to another, paint a line on the floor for it to follow. It is much easier for a robot to follow a line, rather than to navigate between the walls and furniture. There are many ways of adapting the environment to simplify the task of the robot.

Intelligent or Smart?

Most of the programs in this book give the robot a pattern of smart behavior. A robot that heads toward a bright source of light while avoiding obstacles may be smart, but can hardly be called intelligent. It is interesting to compare the NXT brick with the brain of a very simple animal such as the flatworm. This animal can solve a simple T-maze (see Figure 1.6) by trial and error. The experimenter put the worm at the base of the T. In the early trials the worm found its way to the food by chance, sometimes going left and sometimes going right. After the trial was repeated several times, it gradually learned that going left would take it to the food. From then on it always went to the left. This is one type of intelligent behavior. The animal learned to find where its food was placed. Robots can do the same thing. Later in this book we describe how to program a Mindstorms robot to solve a maze with three or more junctions. At first it operates by trial and error, but when it finds a successful pattern of behavior it remembers it and repeats it as long as it continues to be successful.

Summary

If it is possible to program a robot to solve a maze better than a flatworm, it should be easy to get it to mimic the behavior of other lowly animals. Why not program the Tribot to behave like a bat, using an ultrasonic sensor to steer its way across an obstacle-littered floor in total darkness. One of the programs later in this book shows how a Tribot can be programmed to behave like a housefly, ignoring slow changes in the light intensity, but accelerating rapidly away when the shadow of a swatting hand suddenly A bit further up the intelligence scale, we see how RoboArm can be programmed to perform a magical illusion in the same way that a human magician does. But is this intelligent behavior or just smart? This is for the reader to decide.

Chapter Two

About NXT-G Programs

Introduction

NXT-G is a visual language where instead of writing the program in text, you work with icons that represent self-contained units of program instructions. These units are called programming blocks, or just blocks. You drag the required blocks on to the work area of the screen and arrange them on the sequence beam in the order in which they are to be executed (see Figure 2.1).

When you first open your LEGO Mindstorms kit you are invited to begin by building the Quickstart buggy. This is a mobile vehicle with two drive wheels at the front and a small caster wheel at the rear (see Figure 2.2). The Quickstart has three motors, one for each drive wheel. The third motor is not used until later, when it is used to drive a gripper.

After you have built the Quickstart (or Tribot) it needs to be told what to do and how to behave. In other words, it needs to be programmed.

Creating a Program

As explained above, a NXT-G program appears on the computer screen as a sequence of programming blocks placed in order of execution on a sequence beam. Each block is dragged from the programming palette on the left-hand side of the screen. Clicking on a block (see Figure 2.3) shows its settings in a configuration pane (see Figure 2.4) at the bottom left-hand side of the screen.

The settings are edited to suit the program. When the configuration panel is first displayed the settings are set to their default values. For example, the motor block always sets the motor to run forward. If you want it to run in reverse, you need to click the radio button beside the backward-pointing arrow. For some of the settings you have to enter a value.

Many, but not all, of the settings are indicated by symbols on the block icons, so you can get a good idea of the program by looking at the displayed symbols. In Figure 2.3, for example, we can see that this is a move block because of the double gear wheels and that the motors operate on high power (but not the exact power) and runs for a given time (but not exactly how long). The icon does not show whether the next action is to brake or coast. You have to display the configuration pane (Figure 2.4) to discover the settings in full detail.

If you want a complete record of the program, you can type the comments as well as the blocks, which are saved along with the program. They will help you to remember how the program works if you come back to it a few months later. However, the comments are not incorporated into the compiled program, which is downloaded into the brick.

Comments are added to the screen shots to make the program clearer to you and to show you how to edit the settings.

First Steps

Figure 2.6 shows an example of a NXT program. It is the NXT-G equivalent of the traditional "Hello World" program that is featured in many books on programming. The Quickstart moves forward a short distance, says, "Have a nice day," and then reverses back to its starting point.

The robot performs three different actions, one after the other. This means that the program has three action stages, and therefore consists of three action blocks:

* Move To move the robot forward

* Sound To say "Have a nice day"

* Move To reverse back to the starting position

Create and run the program by following these seven steps:

1. Build the sequence. Use the mouse to drag blocks from the programming palette on the left-hand side of the screen and drop them in order onto the sequence beam. 2. Settings. All settings have their default values except where otherwise indicated. Check or uncheck radio buttons, set sliders, and type values or text into text boxes. 3. Save the file under the file name firstSteps. 4. Compile and download. With the robot connected by USB cable or with Bluetooth switched on, click on the bottom left-hand side controller button. This compiles the program and downloads it into the NXT but does not run it. 5. Prepare to run the program. Disconnect the USB cable if used. Use the buttons on the NXT to navigate to the "Run" instruction. Starting at "My files," press the orange button three times. The screen then displays the file name, firstSteps and the word "Run." Put the robot on the floor, making sure there is at least 1.5 meters of clear space ahead of it. 6. All ready? Press the orange button. After a second or two delay, the robot moves forward, stops, says its party piece, and reverses back to its starting position. Because of the differing power levels in the two move blocks, the robot runs faster when it is returning to its starting position. Press the button again to repeat this star performance.

7. Switching off. When you are satisfied that the robot is behaving according to plan, conserve the power supply by switching it off. Three presses on the dark grey button bring you back to the "Switch off?" screen. Then press the orange button to switch off.

Out of Memory?

You may get the "Out of Memory?" message during the compiling stage. At this stage, you will have to free some memory space before compiling can be completed. Often some of the files you find in memory are no longer needed. When you delete a program file, the files associated with it (e.g., sound files) are not automatically deleted. Sound files take up a lot of memory. For example, the "Have a nice day" file takes 5 kilobytes out of the total 130.7 kilobytes available. Be sure to delete it before going on to compile another program that does not use it.

To manage memory, either click on the "Manage Memory" button in the window that appears during compiling, or click on the top left-hand button of the on-screen controller. In the panel that appears, click on the "Memory" tab. You will see a display of the current memory usage. Select and delete files that are no longer needed.

(Continues...)



Excerpted from Programming Lego Mindstorms NXT by Owen Bishop Copyright © 2008 by Elsevier, Inc.. Excerpted by permission of Syngress Publishing, Inc.. All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.

Table of Contents

1 Robots come alive
2 Talking to your robot
3 How to ?
How to use sensors
How to control motors
How to process data
How to navigate the robot
How to make it intelligent
4 Visual Programming Language
5 Programming projects
10 programs in total.
For the QuickStart vehicle and/or the Tribot:
Maze solver, learning best path.
Use a marker pen to copy a drawing or sketch a picture.
Use a marker pen to write its name.
Play Nim against human opponent and learn to win.

For the Tribot:

Find and pick up a light-colored object; drop it in a dark corner, as a Squirrel might do.
A sort of ?roulette? game for those who like to gamble. Robot takes a randomly chosen path on a board marked with a numbered grid. Stops at random.

For the RoboArm:

Sort a pile of toy building bricks by color (using light sensor or color sensor).
Build a ?house? from toy bricks; then (optionally) knock it down!
Throw a ball into a cup, and learn to improve accuracy.
Play Scissors, Paper, Stone against human opponent and learn to win more often.
Play the Breakaway strategic board game against a human opponent.

For Spike:

Search for light-colored object, avoiding obstacles in its path (example of subsumption architecture.

For Alpha Rex:

Song and Dance Act: repeats if applauded, sulks if not.
Navigate using compass sensor.

What People are Saying About This

From the Publisher

Teach your robot new tricks! With this projects-based approach you can program your Mindstorms NXT robot to solve a maze, build a house, run an obstacle course, and many other activities

From the B&N Reads Blog

Customer Reviews