Coding4Fun Articles

Tips and Tricks for Lego Mindstorms Programming

Description

  Get up and running quickly with LEGO Mindstorms on your PC and to take best advantage of the .NET power now available for it. This article shows you some of the quirks and pitfalls of setting up your system and acquaints you with the challenges of programming using the "integrated" model.

Difficulty: Intermediate
Time Required: 1-3 hours
Cost: $100-$200
Software: Lego Mindstorms 2.5 SDK, Visual Studio Express Editions
Hardware:
Download:

 

 

Updated May 4, 2006

Programming with the Microsoft .NET Interface for LEGO Mindstorms can be a great way to introduce yourself to the world of robotics while having fun with one of the world's most popular educational toys. It can also present a few challenges along the way if you are new to LEGO Mindstorms or hardware programming in general. The following topics are designed to help you get up and running quickly with LEGO Mindstorms on your PC and to take best advantage of the .NET power now available for it.

Acquiring and Setting Up LEGO Mindstorms Robotics Invention System 2.0

Where to buy LEGO Robotics Invention System (RIS) 2.0

LEGO Mindstorms RIS 2.0 is available directly from LEGO or online shopping sites such as Amazon as of  May 2006 (note: Lego NXT will be releasing soon). Aside from your PC, the kit comes with all the hardware and software needed to create your first LEGO robot. You should install the LEGO software from the kit and use the built-in visual programming tool to create simple projects, then track your progress through a series of LEGO robotics learning challenges.

However, to take advantage of the Microsoft .NET Interface to program your robot, you still need to download and install some special developer software, as explained below.

What to expect when installing the LEGO RIS 2.0 software

The RIS 2.0 software on the CD that comes with the LEGO kit will install and run on Windows 98, Windows ME, and Windows XP. It may install on other Windows versions, but there is no support by LEGO and the software is very likely to crash, according to some discussion forum threads. We strongly recommend using a version of Windows XP for programming with .NET 2.0 and the LEGO Mindstorms .NET Interface. There is a required reboot after installation is finished.

After installing the software from the LEGO Mindstorms RIS 2.0 CD onto your computer, you will likely need to install one, possibly two, software patches from LEGO:

  • For all XP systems there is a bug where the LEGO RIS Software is started each time the PC system is restarted. Download RIS20XPPatch.exe from the LEGO Web site and install the patch following their instructions.
  • For PCs using Hyper-Threading (HT), you will need to install an updated IR tower driver to prevent system lockups. This patch file is also found on the LEGO Web site. Download and save the Tower164 patch, double-click it, then follow the on-screen instructions to finalize the patch installation. It is also recommended you install it if the PC has a dual-core CPU. (NOTE: Some dual-core systems have issues running the LEGO software even after this patch has been installed.)
What to expect the first time you run the LEGO RIS 2.0 software

First, plug the LEGO infrared (IR) tower into a USB port. The hardware wizard should detect the device and install it automatically using default selections. (PLEASE NOTE: The hardware wizard will need to run every time you connect the IR tower to the PC.)

Next, make sure your speaker volume is turned up so you can follow the directions given when the application is running. Start the LEGO RIS 2.0 Software from the desktop shortcut or from the Programs Menu. The first time you run the program select the top menu option, Run. (PLEASE NOTE: During installation this software will take control of your screen and you will not be able to switch between other running programs.) You will be guided through the setup of the IR tower, the downloading of the LEGO RCX firmware via the IR tower to the RCX brick, and the testing of the effective range of communication between the IR tower and the RCX brick. Once finished, you may exit the program. The Constructopedia booklet included with the LEGO kit contains additional information on this on page 4.

PLEASE NOTE THE FOLLOWING:

  • Do not run other applications while performing the download of firmware (basically, the LEGO operating system) to the RCX brick.
  • It is not unusual to require several attempts to download the firmware to the RCX brick. Three or four attempts before succeeding is not unusual.
  • The download of the firmware will take approximately four minutes. During that time you should see a faint flashing green light coming from the IR tower and a counter on the face of the RCX brick going from 0000 to 2500. If the download fails you will be given the opportunity to retry.
  • After completing the download of the firmware, the LEGO program will download five proprietary sample applications to the RCX brick. This will take approximately one minute. The counter on the face of the RCX brick will go from 1 to 5 as the sample applications are downloaded. If the download fails, you will be given the opportunity to retry.
  • If the batteries drain too low or if for some reason you must remove the batteries from the RCX brick, it will be necessary to re-download the firmware.
Where to get the software needed to program the RIS 2.0

If you have not done so already, please download and install a copy of Visual Studio 2005 Express Editions onto your Microsoft Windows XP computer. Installing Visual Studio will also ensure you have Microsoft .NET 2.0 installed as well.

Why Windows XP? The RIS 2.0 system was originally designed for Windows 98. Although it has support for Windows XP, it does not work well with Windows 2000 or Windows 2003 Server. Additionally, the .NET Interface requires .NET 2.0, which generally requires more hardware horsepower than found on most Windows 98 and Windows ME computers. We therefore recommend sticking with a version of Windows XP for your Mindstorms RIS 2.0 coding projects.

Next you will need to install the LEGO Mindstorms Software Development Kit (SDK) 2.5. This can be downloaded for free from LEGO.

Finally, download one or more of the LEGO Mindstorms Starter Kits. The starter kits are available in each of the Visual Studio Express Edition development languages: C++, C#, J#, and Visual Basic. Each starter kit includes the compiled version of the Microsoft .NET Interface for LEGO Mindstorms, C4F.LegoRcx.Dll. This DLL file is the link between your code and other DLL files that are included in the LEGO Mindstorms SDK. If you would like to explore just how this works, the C++ version of the SDK contains the source code for the .NET Interface itself.

Once that is installed, please follow the directions in the article Introduction to Programming LEGO Mindstorms for an explanation of which redistributable files from the SDK you will need to reference and distribute with your LEGO Mindstorms applications. Basically, you will need to place the .NET Interface for LEGO Mindstorms file, C4F.LegoRcx.DLL, in the same folder as your project executable along with GhostAPI.DLL from the LEGO RIS SDK 2.5 and the proper driver for the IR tower being used.

Trying to use these support files from the GAC (a system-wide known location for .NET assemblies on a PC) has generally resulted in unstable application performance with the LEGO robots.

Tips for setting up LEGO software and the Microsoft .NET Interface for LEGO Mindstorms

At this point it should be clear that you need to follow a number of setup steps before you actually get down to programming your LEGO robots with .NET. Please keep in mind that this is all a hobbyist programming extension to another company's product and it is pushing some limitations on both sides. The following is intended to provide some realistic expectations for what can be accomplished with this release of the .NET Interface for LEGO Mindstorms.

There are some known issues to be aware of:

  • Some newer computers will not recognize the LEGO IR tower. For example, we have been unable to install the IR tower driver on a Toshiba Portege 3500 Tablet PC. A similar problem has been mentioned on some web forums for certain Dell desktop models, but we do not have a list of model numbers.
  • Postings on several different forums related to LEGO Mindstorms RIS 2.0 have indicated a problem getting the LEGO software to communicate with the IR tower on some PCs with dual-core processors. Use of the Tower164.exe patch discussed earlier does not address this problem directly, but there are unconfirmed reports that it has, in some cases, resolved the issue.
  • Changing the batteries in the RCX brick or letting the RCX brick sit for long times between uses can require that the LEGO firmware be re-downloaded by starting the LEGO RIS 2.0 software, selecting the menu option Settings, and then selecting Download Firmware. If you are prompted for a password, simply enter your name and press enter.

The challenges of integrated ("tethered") programming

Because the actual running of the .NET code is on the PC, the term used to describe the programming relationship with the LEGO RCX brick is integrated programming. Think of the IR tower as a tether—a lifeline—between the PC running your application and the RCX brick that must execute physical actions based on instructions from the PC. The IR tower is what makes this possible. If the link with the tower breaks at either end, the integrated execution of the program dies.

Because the RCX brick is a very simplistic computer compared to your PC, you will need to keep in mind its limitations when developing your code. For example, the RCX brick does not raise events. The IR tower must poll the brick for changes in hardware state and the information gathered must then be interpreted by your .NET program as being an event that needs to be handled or not. Likewise, the tower using the relatively slow infrared signals form of communication may experience backlogs in instructions to the brick from the computer. In some test application development we noted the following issues may arise:

Communications latency. There may be a several-second delay between when an event occurs on the RCX brick and when it is recognized by the PC. Long delays can cause an event to be lost all together. Limiting situations and circumstances that can cause tower interference can be important in these cases, such as blocking direct sunlight on the tower, keeping the robot within a reasonable range of the tower (about 8'-10'), and not placing obstacles in the signal path between the RCX brick and the tower.

Losing commands. This has most often occurred when issuing motor commands from a sensor event handler. In fact, commands may not actually be lost but the sensor referenced may not be active and therefore events that would affect motors will not occur. Best to keep motor commands in discrete methods called as necessary.

Setting the sensor type. You may experience a narrow window of time in which this works. We recommend setting sensor types in the main form load event of your application.

Queued commands may still run. Even after stopping the running application on the PC, the robot may continue to run queued up commands. In such cases, it may be necessary to turn off the brick manually. Generally this is not because of a “haunted” RCX brick but because motor commands were left running.

We continue to explore resolutions and work-arounds for these issues, but you should be cognizant of them in the meantime when trying to create and debug your applications

Using the LEGO Mindstorms Programming Starter Kits

The Microsoft .NET Interface for LEGO Mindstorms Starter Kit consists of three demonstration applications that use the basic RoverBot configuration shown below. This robot uses two motors and a double bumper system with two pressure sensors to demonstrate how the robot can smartly react to bumping into other objects.

The Roverbot sample is a very simple .NET application showing how the robot will react to bumping into objects as it roves around the floor. If the right side bumper is pressed by running into an object, the robot will stop attempting to move forward, back up to the right and proceed to try passing the object on the left side. If it strikes the left bumper, then it takes the same actions but with back up to the left followed by an attempt to pass the obstacle on the right side.

The Remote Control sample adds a bit of fine tuning to the first sample. In this sample you determine the direction of the robot (and hopefully avoid obstacles) by using the arrow keys on the PC keyboard.

Both the Roverbot and Remote Control samples provide a visual display of the robot's trail.

The third application in the starter kit, Music Demo, demonstrates using the audio features of the RCX microprocessor brick to play back sounds entered into a PC screen display resembling a piano keyboard.

These are only very basic applications, but they are intended to act as building blocks in creating more sophisticated robot applications of your own. For example, a particular sound may play depending on the touch sensor that is activated. Or, an independently running Roverbot may be commanded to switch to remote control directions when faced with a particularly difficult obstacle and then back to independent roving once the obstacle is cleared.

Extending the .NET Interface for LEGO Mindstorms

Because the source code for the Microsoft .NET Interface for LEGO Mindstorms is included with the free C++ version of the Starter Kit, you can use this along with tools and information in the LEGO Mindstorms RIS SDK to modify and even extend its capabilities. For example, you may wish to add support for additional sensor types such as heat or sound sensors that you acquire from third party sources and wish to adapt for use with LEGO robots.

Additional Resources for Programming with LEGO Mindstorms

The LEGO Mindstorms section of Coding4Fun should definitely be bookmarked in your browser Favorites list. Here you will find the latest news, announcements of new features, updated downloads, and articles on cool LEGO Mindstorms projects with .NET. Additionally, you will want to check these other resources:

First LEGO League —a "minor league" version of the FIRST Robotics Challenge sponsored by FIRST and LEGO.

LUGNET —the LEGO Users Group Network, an international group with many resources on building Mindstorms robots.

The LEGO Mindstorms Community Web site—the latest information and support from LEGO along with discussion forums on programming Mindstorms robots.

In addition, a search on the web using MSN or Google search using keywords like "LEGO Mindstorms" will yield plenty of useful resources from other hobbyist coding enthusiasts using the LEGO Mindstorms Robotics Invention System.

What Is the Future of LEGO Mindstorms?

In addition to supporting LEGO Mindstorms RIS 2.0 with a .NET Interface, there is even more for hobbyist coders to look forward to with the impending release of the next generation of LEGO Mindstorms this summer. Dubbed Mindstorms NXT, this promises to be a huge opening for more coding for fun with a new 32-bit microprocessor for the robot, Bluetooth and USB support, a third motor, and new sound and ultrasound sensors!

All in all, this means there are going to be some terrific ways for students, hobbyists, and the just curious to get an introduction to robotics programming at a reasonable price. Using either the existing Mindstorms product or the upcoming NXT version, you can become familiar with the thrill of robotics programming while learning the challenges faced by robotic software developers to make reliable, smoothly operating, intelligent systems. And by tying it into one of the world's most recognizable toymaker brands, it can be a great way to attract your kids into joining you on a weekend development project.

John Wingfield is a .NET applications developer and technical project manager residing in Kirkland, WA. Currently on a contract assignment at Microsoft, he works with the Visual Studio Express Product Management Team and serves as content strategist for the MSDN Developer Center, Coding4Fun. You can reach him through his blogsite.

Tags:

Hardware, lego, Robot

The Discussion

  • zhe hu

    I installed c++ version of the sdk. where can I find the source code for the .net assembly C4F.LegoRcx.Dll

    thanks

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.