Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Programming Lego Mindstorms NXT
Programming Lego Mindstorms NXT
Programming Lego Mindstorms NXT
Ebook319 pages2 hours

Programming Lego Mindstorms NXT

Rating: 3.5 out of 5 stars

3.5/5

()

Read preview

About this ebook

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
LanguageEnglish
PublisherSyngress
Release dateApr 18, 2011
ISBN9780080569963
Programming Lego Mindstorms NXT
Author

Owen Bishop

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 more from Owen Bishop

Related to Programming Lego Mindstorms NXT

Related ebooks

Robotics For You

View More

Related articles

Reviews for Programming Lego Mindstorms NXT

Rating: 3.25 out of 5 stars
3.5/5

2 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Programming Lego Mindstorms NXT - Owen Bishop

    Programming Lego Mindstorms NXT

    Owen Bishop

    Copyright

    Copyright © 2008 by Elsevier, Inc. All rights reserved.

    Elsevier, Inc., the author(s), and any person or firm involved in the writing, editing, or production (collectively Makers) of this book (the Work) do not guarantee or warrant the results to be obtained from the Work.

    There is no guarantee of any kind, expressed or implied, regarding the Work or its contents. The Work is sold AS IS and WITHOUT WARRANTY. You may have other legal rights, which vary from state to state.

    In no event will Makers be liable to you for damages, including any loss of profits, lost savings, or other incidental or consequential damages arising out from the Work or its contents. Because some states do not allow the exclusion or limitation of liability for consequential or incidental damages, the above limitation may not apply to you.

    You should always use reasonable care, including backup and other appropriate precautions, when working with computers, networks, data, and files.

    Syngress Media®, Syngress®, Career Advancement Through Skill Enhancement®, Ask the Author UPDATE®, and Hack Proofing®, are registered trademarks of Elsevier, Inc. Syngress: The Definition of a Serious Security Library™, Mission Critical™, and The Only Way to Stop a Hacker is to Think Like One™ are trademarks of Elsevier, Inc. Brands and product names mentioned in this book are trademarks or service marks of their respective companies.

    PUBLISHED BY

    Elsevier, Inc.

    30 Corporate Drive

    Burlington, MA 01803

    Programming Lego Mindstorms NXT

    Printed in the United States of America. Except as permitted under the Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher, with the exception that the program listings may be entered, stored, and executed in a computer system, but they may not be reproduced for publication.

    Printed in the United States of America

    1 2 3 4 5 6 7 8 9 0

    Page Layout and Art: SPi Publishing Services

    Copy Editor: Judy Eby

    For information on rights, translations, and bulk sales, contact Matt Pedersen, Commercial Sales Director and Rights, at Syngress Publishing; email m.pedersen@elsevier.com.

    Brief Table of Contents

    Copyright

    Brief Table of Contents

    Table of Contents

    List of Figures

    List of Tables

    Author

    Chapter 1. Robots and Programs

    Chapter 2. About NXT-G Programs

    Chapter 3. Data

    Chapter 4. Loops

    Chapter 5. Switches and Other Topics

    Chapter 6. Using the Light Sensor

    Chapter 7. Using the Touch Sensor and the NXT Buttons

    Chapter 8. Using the Ultrasonic Sensor

    Chapter 9. Sounds Received and Emitted

    Chapter 10. The Magnetic Compass Sensor

    Chapter 11. More Mathematics

    Chapter 12. Navigation

    Chapter 13. Games Robots Play

    Chapter 14. Databases for Robots

    Table of Contents

    Copyright

    Brief Table of Contents

    Table of Contents

    List of Figures

    List of Tables

    Author

    Chapter 1. Robots and Programs

    Introduction

    What is a Program?

    Designing a Robot

    Specify the Robot's Environment

    Intelligent or Smart?

    Summary

    Chapter 2. About NXT-G Programs

    Introduction

    Creating a Program

    First Steps

    Out of Memory?

    Coming for a Spin?

    Tying it Together

    Chapter 3. Data

    Introduction

    Sources of Data

    Variables

    Arithmetic Operators

    Logical Operators

    Flag Variables

    Chapter 4. Loops

    Introduction

    Outside Events

    Time Limit

    Counting the Loops

    Dance Some More

    Use Loops to Make Actions Repeat

    Another Way to Use the Loop Counter

    Logical Conclusions

    Use a Loop (or a Wait Block) to Wait for an Event to Occur

    Nested Loops

    The Wait Block

    Chapter 5. Switches and Other Topics

    The Switch Block

    Switch Blocks Direct Execution Along Alternative Pathways

    Simultaneous Processing

    Diagnostic Programming

    Chapter 6. Using the Light Sensor

    Introduction

    Light Sensor Directed Downward

    Lined in

    Line Follower

    Edge Follower

    Light Sensor Directed Upward

    Housefly

    Light Sensor Directed Forward

    Lightfinder1

    LightFinder2

    Almost an Interrupt

    LightFinder3

    LightFinder4

    Summary

    Chapter 7. Using the Touch Sensor and the NXT Buttons

    Introduction

    Waiting for Contact

    Bumper

    Waiting for Input

    Scanning the NXT Buttons

    Entering Numbers

    Chapter 8. Using the Ultrasonic Sensor

    Introduction

    Avoidance Routine for Alpha Rex

    Ultrasonic Search

    Chapter 9. Sounds Received and Emitted

    Introduction

    Start and Stop

    Start, Stop, and Turn

    One Clap or Two?

    Playing a Tune (with a Little Dance)

    Choreography

    Song Snippets

    Dance Snippets

    Chapter 10. The Magnetic Compass Sensor

    Introduction

    Heading in the Right Direction

    Polygon

    Circle

    Chapter 11. More Mathematics

    Introduction

    Floating Point Arithmetic

    Calculating Cosines

    Calculating Sines

    Square Root

    Factorials

    Combinations

    X modulo Y

    Modulo Counting

    Chapter 12. Navigation

    Introduction

    Distance Traveled

    Magnetic Compass

    Edge Follower 1

    No-go Response to a Line

    Steering Alpha Rex

    Marching Orders

    Rex On Line

    Rex On Edge

    Pathfinder

    Cosine Rule

    Chapter 13. Games Robots Play

    Introduction

    Maze Runner—Version 1

    Maze Runner—Version 2

    Maze Runner—Version 3

    Maze Learner

    Scissors, Paper, Stone

    Signalling by Semaphore

    Magic

    The Program

    Chapter 14. Databases for Robots

    Introduction

    Lookup Table for Weekdays

    Writable Lookup Table

    Another Way of Using a Lookup Table

    Reading Data from a File Access Block

    Acknowledgements

    List of Figures

    Figure 1.1. This is the computer screen during a NXT-G programming session. On the left-hand side is the palette from which you drag the icons that represent the programming blocks. The icons are assembled into a program on the grid of the working area. Below the working area is the configuration pane, which displays the detailed settings of the currently selected block.

    Figure 1.2. This version of the Tribot is equipped with bumper sensor, to detect objects that it runs into, and also has a light sensor pointing forward with which it can seek out light sources in the room.

    Figure 1.3. A NXT-G program consists of a number of programming blocks, which are executed in the order in which they are placed on the sequence beam. Sometimes the blocks are linked by data wires, which transmit data from block to block. This program switches on the drive motors for 1 second. Four seconds later, it starts to switch motor A on and off every 2 seconds.

    Figure 1.4. The gear wheels on the upper icon indicate that it is a move block. The lower icon is obviously a sound sensor block.

    Figure 1.5. Equipping a robot. This robot is going to be mobile, so it needs drive wheels with motors. The rear wheel is a caster. The robot has an ultrasonic sensor for navigation, and a bumper for detecting obstacles. It has an arm with a gripper for handling objects and a microphone for detecting spoken commands. Like all robots, it must have at least one microcontroller or microprocessor. To make the whole system operational it must have a program.

    Figure 1.6. The maze for training the flatworm is made of glass and filled with water. It has only one junction. If food is always placed in the left arm of the maze, the flatworm soon learns to turn left at the junction.

    Figure 2.1. Creating a program. A wait block is being dragged from the palette on the left and dropped on to the end of the sequence beam. The beam expands automatically to make room for it. The block will provide 1 second of silence after the second tone has been played.

    Figure 2.2. This rear view of the Quickstart buggy shows the two drive wheels and the rear caster wheel. The advantage of this arrangement is the stable three-point contact with the surface, which does not have to be absolutely flat.

    Figure 2.3. The icon for the move block shows the main settings, but you have to display the configuration pane (see Figure 2.4) to find out the details.

    Figure 2.4. The configuration pane of the move block shows all the settings, which you can change to suit your program.

    Figure 2.5. The Quickstart buggy is ready to run its first program.

    Figure 2.6. In the working area, the first program looks like this. Comments have been added to show some of the settings, but you do not need not include them. Just check the configuration panel of each block to make sure that you have edited the settings as specified in the comments.

    Figure 2.7. The next version of the program is like the first but ends in a spin.

    Figure 2.8. Data wires carry information between blocks. Here, a yellow wire carries the light intensity reading (numeric data) from the sensor block to the math block. The second wire, which is green, carries a logical signal from the math block to the move block. The signal controls the direction in which the motors turn.

    Figure 2.9. The program in Figure 2.8 can be made more compact by contracting the long data hubs. The software automatically re-routes the data wires. You can expand the hubs again if you want to add more data wires later.

    Figure 3.1. This version of the program in Figure 2.9 does the same thing but uses only two blocks. The math block of Figure 2.9 is redundant.

    Figure 3.2. A useful program snippet for displaying numbers. Make it more compact by retracting the long data hub. Make it easier to use by turning the three blocks on the right into a My Block.

    Figure 3.3. A variable block and its configuration pane. The three blocks listed in the menu are created automatically. When you create new variable blocks they appear in this menu. The menu is used for selecting the name of a block. For instance, the block in the working area has been designated as Logic 1 by clicking on that name in the list.

    Figure 4.1. This musical score is an example of a loop that is repeated once. The two pairs of dots on the right tell the musician to go back and play part of the score again. By contrast, a loop block usually repeats more than once, perhaps many times or even forever.

    Figure 4.2. This program runs forever in its loop.

    Figure 4.3. The loop in this program repeats until the orange Enter button is pressed.

    Figure 4.4. This loop repeats until the light intensity is greater than 50.

    Figure 4.5. The loop block (left) is ready to receive some of the blocks to be transferred from the right.

    Figure 4.6. Ready to dance some more! We use a loop to repeat a sequence of actions. When the loop has repeated five times the controller leaves the loop and plays the Applause file.

    Figure 4.7. Using the counter output of a loop block to generate a tone of gradually increasing pitch.

    Figure 4.8. Using a logical signal to end a loop. When both sensors are receiving light, the robot stops bleeping and thanks you.

    Figure 4.9. Nested loops.

    Figure 4.10. To start with, a wait block is enabled to receive input from a touch sensor, as its icon shows.

    Figure 5.1. Demonstrating how switch blocks work. The robot says light or dark, depending on whether or not its light sensor is receiving light.

    Figure 5.2. Configuring a switch block to be controlled by a numeric value.

    Figure 5.3. Configuring a switch block to be controlled by a logic value.

    Figure 5.4. An example of simultaneous processing.

    Figure 5.5. Checking the progress of a calculation by displaying the intermediate results.

    Figure 5.6. A useful my block that displays numeric results. This was generated from the program illustrated in Figure 5.5.

    Figure 5.7. This program indicates which branch is being followed by saying light or dark.

    Figure 5.8. A program module for reading logical values when debugging.

    Figure 5.9. The configuration pane when setting the program of Figure 5.8.

    Figure 6.1. The light sensor showing the photodiode (top) and the illuminating LED (bottom).

    Figure 6.2. The light sensor's icon and configuration pane.

    Figure 6.3. The extended data hub has seven single or double data plugs. The double plugs are for inputs, receiving the signal on the left. They also relay it unchanged to the corresponding plugs on the right. The single plugs (right side only) are for outputs from the processing circuits of the block.

    Figure 6.4. When mounted in the downwardly directed position, the diodes are about 1 centimeter above the surface.

    Figure 6.5. Another view of the downwardly directed light sensor.

    Figure 6.6. The light sensor is ready to mount on the front beam of the Quickstart.

    Figure 6.7. Ready to mount, another view.

    Figure 6.8. The LinedIn program prevents the robot from crossing a line.

    Figure 6.9. In the Line Follower program the robot actually follows one edge of the line.

    Figure 6.10. The Edge Follower program is simple and works well on paths that curve mainly in one direction.

    Figure 6.11. While the housefly (Tribot) is peacefully feeding on the cake icing, a branch of the program is continuously on the alert for the approach of a fly swatter.

    Figure 6.12. A forward pointing

    Enjoying the preview?
    Page 1 of 1