Tips and Tricks for Lego Mindstorms Programming
- Posted: Oct 26, 2006 at 12:46PM
- 10,485 views
- 1 comment
Loading user information from Channel 9
Something went wrong getting user information from Channel 9
Loading user information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
|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.|
Time Required: 1-3 hours
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.
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.
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:
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:
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.
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:
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
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.
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.
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:
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.
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.