IT 548 Mechatronics Mindstorms Programming

Total Page:16

File Type:pdf, Size:1020Kb

IT 548 Mechatronics Mindstorms Programming

IT 548 Mechatronics – MindStorms® Programming

Mechatronics – MindStorms® Programming

Introduction: Lego MindStorms® uses a programming language called RCX. This language is similar to C, but has some major differences. The first, and probably most noticeable, of these is the programming interface provided with the kits. While it is possible to write the code in a text format, the kits provide a graphic user interface (GUI) to facilitate programming. This enables those unfamiliar with programming languages and syntax to quickly learn how to arrange algorithms for robotic behavior. The GUI uses “block language”, where Lego blocks represent portions of actual code. Below is shown a small block code next to its equivalent text code.

program test {

#include #include #include #include

macro FORWARD2 { direction [ A C ] [ ] on [ A C ] wait 200 float [ A C ] }

main { ext InterfaceType "kRoverBot" rcx_ClearTimers bbs_GlobalReset([A B C])

try { FORWARD2 } retry on fail

try { stop tasks } retry on fail }

}

As can be seen, many of the blocks can be expanded to see the smaller blocks of code that make up the routine. Also, take note of some of the other differences between the text version and C, such as header files, macros, order of arguments, and other syntax. The following will go over some of the basics of using the block GUI to program a MindStorms® robot.

©2003 Brigham Young University School of Technology 1 of 4 IT 548 Mechatronics – MindStorms® Programming Blocks: Below is a picture with the types of blocks available to program your robot.

All programs start with the big blue block with the picture of the RCX computer seen in the upper left hand corner. This block can be considered the main routine and is named by the name of the program saved.

The green blocks are commands found under the “Small Blocks” menu on the left. These will help set power levels, set directions, turn motors on and off, perform calculations, send messages to the IR tower, and make sounds. They are organized under the following submenus: Power, Sound, Comm, Variable, Reset, and Advanced.

The yellow block on the right is a user-defined block that can be created and used from the “My Blocks” menu on the left. In the text code, these are macros. When one of these is created, the user names it. Be careful when modifying these; any modification to one will change any other instances of blocks of the same name.

The red blocks allow for pauses in the execution of the program. This can help control how long the motors are on or have a routine continue until something else happens. They can be found in the “Wait” menu on the left.

©2003 Brigham Young University School of Technology 2 of 4 IT 548 Mechatronics – MindStorms® Programming The orange blocks in the bottom right are for loops. The typical loops are all represented, even infinite loops. These blocks can be found in the “Repeat” menu on the left.

The purple block on the right is a conditional statement that can be found in the “Yes or No” menu on the left.

Most of the blocks have conditions or settings that can be changed by clicking on the tab on the right side of the block. Some of the blocks can also collapse to hide any blocks contained inside or expand to show them by clicking on the gray square on the upper left of the block.

Interrupts: In addition to these blocks, there are interrupts available in the “Sensors” menu on the left. When a sensor is selected from this menu, it is placed on the same level as the main block and acts as an interrupt. Once the interrupt is in place, other blocks can be attached to it to create the interrupt routine. Below are examples of the different interrupts.

When an interrupt is placed, a series of setup windows pops up. The first of these assigns the port the sensor is associated with. The second assigns what sensor event causes the interrupt. The third allows testing of the interrupt before finishing. These settings can be changed any time by clicking on the tab on the right side of the interrupt block. Interrupt routines may also expand and collapse.

©2003 Brigham Young University School of Technology 3 of 4 IT 548 Mechatronics – MindStorms® Programming

Downloading and Running: When a program is complete or the current code needs to be tested, it needs to be downloaded to the RCX computer, the brain of a MindStorms® robot. There is a bar at the top left of the screen that has three buttons on it.

The RCX computer can hold up to five programs. The download slot can be changed under the “Settings” menu at the top of the screen. Make sure the robot is within the range of the IR tower and click the download button. If the program doesn’t download, an error window will pop up. When the program has downloaded successfully, the RCX computer will chirp.

When you are ready to run the program on the robot, there are two ways to do it. First, make sure the slot the program was downloaded to shows up on the right side of the RCX computer display. This can be changed by pressing the select button on the RCX computer. Then, either click run on the PC while the robot is still in range of the IR tower or push the run button on the RCX computer.

When testing is complete, either click the stop button on the PC while the robot is in range of the IR tower or push the run button on the RCX computer.

Other Features: There are some other features to be aware of when programming in this environment. First, any blocks that are not in some way attached to the main block are not considered part of the program. Drag any unwanted blocks to the trashcan icon in the lower left of the screen.

Often, a program will exceed the size of the screen. There are scrolling and zooming tools in the lower left of the screen.

The arrows scroll in the indicated direction and the “+” and “-“ buttons zoom in and out. Clicking the circle in the center of the arrows will realign the view to the origin with main in the upper left corner. Edge scrolling can also be enabled under the “Settings” menu at the top. This will cause the program view to move if the cursor is moved to an edge of the screen.

Under the “Edit” menu, a comment block may be added, a group of blocks may be copied, or a variable may be declared. ©2003 Brigham Young University School of Technology 4 of 4

Recommended publications