Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
This book is about learning how to write programs for LEGO MINDSTORMS NXT robots. The LEGO MINDSTORMS software and its NXT-G programming language are powerful tools that make it easy to write custom programs. This book will teach you how to get the most out of the NXT-G language as you acquire the programming skills necessary to successfully create your own programs.
This book is for anyone using NXT-G to program their NXT robots, whether you’re a young robotics enthusiast, an adult working with children to learn robotics, a parent, a FIRST LEGO League coach, or a teacher using NXT in the classroom. One of my goals in writing this book was to make the material accessible to young learners while going into enough depth to help students and teachers understand the hows-and-whys of NXT-G programming.
This book can be used with any NXT set and any version of the MINDSTORMS software. To test your programs, you’ll use a single, general-purpose robot that you can build with any NXT set. There are only a few relevant differences between the NXT software versions, and I’ll point them out as appropriate. Almost all the material presented in this book applies to any version of the MINDSTORMS software.
No previous programming experience is required. NXT-G is a great first programming language, and I’ll explain how to use the MINDSTORMS software and each of the elements of the NXT-G language.
This book focuses on programming NXT robots, rather than on the mechanical aspects of building them. You’ll learn how to work with the core parts of the NXT-G language, such as the blocks, data wires, files, and variables, and you’ll see how these pieces can work together. You’ll also learn some good programming practices, bad habits to avoid, and debugging strategies that will help keep your frustration level low so you can have fun while programming. You’ll find numerous NXT-G programs with step-by-step instructions and explanations, as well as many small examples designed to help you understand exactly how NXT-G works.
All of the programs in this book will work with the general-purpose robot or the NXT Intelligent Brick alone. This is a book about programming NXT, not about constructing robots. As such, I’ve devoted as many pages as possible to in-depth coverage of the most important programming topics.
The book begins with an introduction to the NXT set and the MINDSTORMS software. This is followed by the building instructions for the test robot. The next few chapters cover the basics of the NXT-G language, culminating in a maze-solving program in Chapter 7. Chapters covering the more advanced language features follow, and the book finishes up with a sophisticated line-following program. Here’s an overview of the contents of each chapter.
This chapter provides a brief introduction to the LEGO MINDSTORMS NXT set and the NXT-G language. I’ll also discuss the general process used for creating programs.
In this chapter you’ll find an in-depth tour through the features of the MINDSTORMS software. You’ll create some simple programs and learn the basic steps for debugging problems.
The building instructions for the test robot are given in this chapter. You’ll use this general-purpose robot to test the programs in the remainder of the book.
In this chapter you’ll learn about the NXT motors and the NXT-G blocks that control them. You’ll write several programs to learn how to make your robot move using the Move and Motor blocks.
This chapter covers the NXT sensors: the Touch, Sound, Light, Color, Ultrasonic, and Rotation Sensors. You’ll learn common uses for each sensor type, as well as how to use NXT-G to control the sensors. I’ve also included example programs that teach you how to use each sensor.
The Switch and Loop blocks are the main focus of this chapter. You’ll learn the Switch block’s various options for making decisions and how to use the Loop block to perform repeated actions. I’ll also discuss the Keep Alive block and the Stop block, as they also are related to program flow. You’ll use the Switch and Loop blocks, as well as some sensor and motor blocks, to write a simple line-following program.
By the time you get to this point in the book, you’ll have learned the basic features of NXT-G. This chapter walks you through the process of creating a complete program to solve a maze using a wall-following method. This chapter focuses on learning how to design, create, and debug a large program.
Data wires are one of the most powerful features of the NXT-G language. This chapter shows you what data wires are and how to use them effectively. The discussion includes coverage of data types and introduces some blocks that are used exclusively with data wires, including the Math, Number to Text, and Text blocks. To get some practice using data wires, you’ll write the SoundMachine program, which turns your robot into a sound generator.
In this chapter you’ll learn how to use a data wire to control a Switch block. You’ll also learn about the special rules in NXT-G for moving data between the blocks inside a Switch block and the blocks that come before or after the Switch block.
In this chapter you’ll learn how to use data wires to control a Loop block. You’ll learn about the NXT timers and then use the features of the Loop block to create three different programmable timers, each with its own advantages.
Variables are used for storing values that your program uses. In this chapter you’ll learn how to create and use variables, and you’ll see how they relate to data wires. I’ll also discuss constants, which are new to NXT-G 2.0.
In this chapter you’ll learn how to use the buttons on the NXT to control your program. You’ll also learn more about how to use the NXT’s display screen. You’ll write programs that let you enter a value into your program, using either numbers or pictures, as well as a program that turns the NXT into a sketch pad.
A My Block is a block you create by grouping other blocks together. In this chapter you’ll learn how to create a My Block, how to use My Blocks in your programs, and how to share My Blocks with other NXT users. I’ll discuss how to use variables and data wires with My Blocks and how to deal with broken My Blocks.
This chapter covers the Math, Logic, Range, and Random blocks. A section on how to use integer math is included for NXT-G 1.0 users, as well as a section covering floating-point math for NXT-G 2.0 users.
In this chapter you’ll learn how to use files to store information on the NXT, how to manage the NXT’s memory, and how to transfer files between the NXT and your computer.
The programs in this chapter show you how to use the NXT as a data logger. I’ll cover the basics of collecting and analyzing data, as well as some common problems. The new data-logging features of the LEGO MINDSTORMS Education Software 2.0 release are also discussed.
NXT-G uses multiple sequence beams to provide multitasking functionality. In this chapter you’ll learn why you might want to use more than one sequence beam and how to do so effectively. You’ll also learn about the ways that using multiple sequence beams can complicate a program and some tips for avoiding the most common problems.
In this chapter you’ll see how to use some advanced NXT-G features to create a complex line-following program. You’ll use files to configure the program settings and implement a proportional controller to create a fast and accurate line-following machine.
This appendix contains a list of websites that provide information about NXT-G programming.
This appendix discusses the changes in the NXT-G language that you’ll want to be aware of when upgrading to the new MINDSTORMS software.
To get the most out of this book, you should work through the step-by-step instructions for building the example programs on your computer as you are reading. Programming is a learn-by-doing activity, and you’ll learn a lot more by writing and experimenting with the programs than you will by just reading about them.
The programs and accompanying discussions will make the most sense if you read the chapters in order. Several of the example programs are introduced in the early chapters and then expanded in the later chapters as you learn more about the NXT-G language. By the time you get to the end of the book, you’ll have the knowledge and skills you need to be an expert NXT-G programmer.