Writing Efficient NXT-G Programs

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Writing Efficient NXT-G Programs

Introduction
The NXT-G programming language that is the core of the LEGO MINDSTORMS Education NXT Software makes building complex programs quick and easy. Complex programs can be built by combining simpler blocks. Each program you build and then download to the NXT Intelligent Brick becomes a file directly accessible from the NXT brick menu. However, complex programs can quickly fill the limited flash memory available on the brick. This guide describes techniques you can use to write efficient programs for creating complex behaviors with a minimum use of flash memory. These guidelines are particularly relevant to NXT competitions, such as FIRST LEGO League, in which competitors often download several complex programs to a single NXT brick.

Writing Efficient NXT-G Programs


Maximizing Free Space
Start by maximizing the free space on your brick before you download your own programs. When an NXT brick is in factory default condition (brand new or recently updated firmware provided by LEGO Education), the bricks flash memory includes a set of default files used for built-in features such as Try Me program and on-brick programming. These default files are called system files and they dont normally appear in LEGO MINDSTORMS Education NXT Software v1.0. To see these files: 1. Open the NXT Window 2. Click on the Memory tab 3. Check the box next to Show System Files It is always safe to delete any of these files with the Delete button; however, you will temporarily lose the built-in features they provide. After manually deleting system files, use the Delete All button to further maximize usable space for your programs. Clicking this button will delete any non-system files on your brick and will optimize the flash memory usage of the system files remaining on the brick. For more information about the system files and managing memory on the NXT in general, refer to the Help topic Files and Memory on the NXT included with LEGO MINDSTORMS Education NXT Software v1.0.

Writing Efficient NXT-G Programs


Programming Techniques
Keep these guidelines in mind while you write large NXT-G programs. You will find that programs using these techniques can perform more complex tasks while using less flash memory.

1. Maximize Code Re-use


Code re-use is perhaps the most important concept to understand when attempting to write efficient NXT-G programs. Each NXT-G block added to a program increases the size of the program file in the NXT bricks memory. However, each copy of the same type of block automatically shares the code of the first instance of the block. For example, placing one Move block in your program requires a large amount of code but each additional Move block you place adds a relatively small amount of code to your program file because a lot of the code can be reused. 1.1. Move and Motor Blocks The concept of code re-use applies to almost all NXT-G blocks. However, the basic motor control blocks (Move and Motor) included with LEGO MINDSTORMS Education Software v1.0 deserve special attention for three reasons: 1. Move and Motor blocks are very commonly used. 2. Move and Motor blocks are often interchangeable because they provide very similar functionality. 3. Both Move and Motor blocks require relatively large amounts of code in your programs. If your program uses just one Motor block and one Move block, the minimum program size will be about 8 KB. Alternately, if you use two Move blocks (no Motor blocks) to perform the same actions, the minimum program size will be less than 6 KB. This is true because multiple Move blocks automatically share code but Move and Motor blocks do not. So, you can make smaller programs if you use only one type of motor control block, either Move or Motor, in a program. To help you choose, review some of the important differences between Move and Motor blocks: Move blocks provide automatic synchronization and turning using two motors simultaneously. Motor blocks affect one motor at a time. Move blocks provide automatic precision stopping. Motor blocks allow manual control of power ramping instead. Motor blocks allow you to enable or disable power control manually. This feature can be used for more predictable movement at relatively slow output speeds.

Writing Efficient NXT-G Programs


Programming Techniques
1.2. Loops and My Blocks Even with automatic code re-use, every copy of every block in your program uses up some space in the program file. You can further reduce the size of complex programs by identifying parts of your programs that perform the same action multiple times or in multiple places. If your program needs to perform an action more than once with no other action in between repetitions, place the blocks for that action in a loop rather than copying the needed blocks. If your program needs to perform the same action with other actions in between, place the blocks for that action in a My Block and drop copies of the My Block where needed. Also use My Blocks if you need to perform the same action in two or more different sequences inside a Switch block. Code is automatically shared between copies of the same My Block just as code is shared among the same type NXT-G blocks. If you find that your program needs to perform almost the same action multiple times, remember that you can use data hubs on both built-in NXT-G blocks and your own My Blocks to choose different behavior conditionally. If you use a data hub on a My Block, plan ahead to decide which values you may need on the data hub before you create the My Block. 1.3. Combining Multiple Programs Even though code is automatically shared between blocks within a program, multiple programs never share code with each other. So if you download two programs to the NXT brick and these programs perform very similar actions (for example, they may both use the same My Block), the code for that action must be duplicated in both program files. For competitions, it is very common to write separate programs for each challenge that must be solved. If this causes you to run out of space, consider combining these programs into a single program that allows you to conditionally choose which action to take. For example, you can write a program that takes input from an NXT Left or Right Button. Then during the competition, you can press the Left Button to solve challenge 1 or the Right Button to solve challenge 2.

Writing Efficient NXT-G Programs


Programming Techniques

2. Minimize Usage of Sound and Display Blocks


The NXT-G Sound and Display blocks provide fun enhancements to NXT bots, but competitive bots rarely need these features at competition time. These blocks may also be useful for debugging your program, but you can usually remove them at competition time to save space. My Blocks can make this process easier. For example, if you use one or more Display blocks to display the value of variables at various points in your program, place all of these debugging blocks in a My Block and just delete the My Block(s) when you need to shrink your program. Sound blocks configured to play sound files and Display blocks configured to display image files require a lot of memory on the NXT brick because extra files are downloaded along with your program. Sound files are particularly large. For example, the ! Fanfare sound effect included with LEGO MINDSTORMS Education NXT v1.0 requires over 20 KB of flash memory.

3. Minimize Usage of File Access or Record/Play Blocks


The File Access and Record/Play blocks both require extra files in addition to the main program file. Like recorded sounds, these files can quickly use up precious flash memory. If you do use these blocks in your program, periodically check for old files you no longer need using the Memory tab of the NXT Window. Note that files created by the File Access block will be listed under the Other category.

4. Use Numbers or Logic Values Instead of Text


NXT-G allows you to use text to pass around complex data or debug your program, but each copy of a text string requires more space in the program file than any single number or logic value. If it is reasonable to represent the same data using numeric or logical values, use these instead of text.

Writing Efficient NXT-G Programs


Programming Techniques

5. Use Mini Blocks


A special group of add-on blocks for NXT-G, called mini blocks (originally communicated under the name performance blocks), provide some of the functionality of common NXT-G blocks while using less space on average. The mini blocks for Move, Motor, Sound and Display will be available for downloading in Fall of 2006 (check out http://www.firstlegoleague.org). Mini blocks are very similar to the blocks that were included with the LEGO MINDSTORMS Education NXT Software v1.0. However, these blocks have been optimized so they are smaller when they are downloaded to the NXT brick. The mini blocks achieve the smaller size for the following reasons: 1. The blocks have more efficient code behind the scenes. 2. Some of the blocks have fewer data hub options than the blocks that shipped with the LEGO MINDSTORMS Education NXT Software v1.0. For instance, the Mini Display block does not have an Action plug. You must choose (Image, Text, Drawing, Reset) in the configuration pane which allows the software to further optimize the downloaded program code. Blocks configured in this way provide the same functionality as non-mini blocks. 3. On some blocks, the configuration pane itself has been simplified. For instance, the Mini Move block gives the user the option of selecting two motors instead of one, two or three motors as the original Move block does. By simplifying the options available, the code required for the block is also simpler, resulting in smaller program files on the brick. In almost every situation, using mini blocks correctly will result in smaller programs. Section 1 of this document discusses how to re-use code efficiently. In particular, recommendations for the use of Move and Motor blocks are described. However, when you use the Mini Move and Mini Motor blocks, things are a bit different. For these blocks, limiting the type of Duration (unlimited, seconds or rotations) will provide you with the greatest memory savings. In most cases, using the Mini Move or the Mini Motor blocks and limiting the type of Duration will reduce the overall program size and the resulting program will be much smaller than a similar program made using blocks in LEGO MINDSTORMS Education NXT Software v1.0.

Prepared by National Instruments and the LEGO Group. (C) Copyright 2006. Version 1.1 (2006-09-27)

You might also like