We present an Ada-based interface to Lego Mindstorms ™, a programmable robotics kit that has attracted considerable attention in the computing community. We discuss our motivations for choosing Ada over other high-level languages, and our particular implementation over possible alternatives. Robotics and Ada combine very nicely for teaching basic computing concepts to both technical and non-technical majors, as shown with several examples.
This has led computer science faculty to constantly experiment with new approaches to introductory computer science courses [HP98] [UW99]. Educators are always looking for new approaches that can a) provide a more “hands-on” approach to computing, one that is seen as both relevant and interesting by the student, and b) teach the same concepts more efficiently, presenting the essential material while requiring less time for faculty to present and students to master.
We believe that recent technological advances in the marketplace make the use of simple robotics projects a viable way to address some of these problems. We discuss our proposed system below.
Mindstorms is a product from Lego Corporation that enables kids (and, judging from the number of web sites, many enthusiastic adults) to construct programmable robots. The robots are built of conventional lego parts attached to a programmable Lego brick called the RCX, which contains three input ports and three output ports attached to a Hitachi H8/3292 microcontroller. A simple robot built around the RCX is shown in Figure 1. The RCX is the yellow box in the center; its input ports are connected to the black wires.
The kit also comes with a Windows-based visual programming environment for programming the robots you build, shown in Figure 2. The resulting code is downloaded into the RCX through an infra-red transmitter (supplied with the kit) that plugs into the PC serial port.
While the visual programming environment provided with the RCX is perfectly adequate for its intended market, we believe it unsuited for teaching introductory computer science. Most of the programming tasks students are likely to encounter when they need to use the computer to solve a problem are not likely to be solvable with visual programming languages, nor do we see this changing in the foreseeable future. Additionally, RCX code contains no variables. This would substantially limit the kind of problems our students could solve. Finally, there's no subroutine stack in the visual environment: you can't have subroutines (called "my commands" in the RCX world, see Figure 2) call other subroutines. Again, this seems unnecessarily restrictive for computer science instruction.
There are many candidates for a high level language to replace the RCX code interface. We chose Ada for several reasons. Ada has a long track record of use in introductory computer science courses [Fe99] [Le95]. Ada also embodies software engineering principles in a much more rigorous way than C or C++. Visual Basic was, we believed, too Windows-specific for the kind of general problem solving we wanted to teach, and requires users to be exposed to some rather intimidating technological baggage in order to write RCX programs. C presents well-known problems with arrays and pointers that we wished to avoid, particularly since the vast majority of students in our course will not become computer science majors. (The course we are targeting is required for all students at our institution, both technical and non-technical, regardless of their chosen field of study).
Choosing Ada also permitted us to leverage our existing Ada expertise and courseware. If we had seen evidence that other high level languages offered compelling advantages, we could have used them for the robot projects and left Ada separate for the more traditional programming efforts typical of introductory computer science classes. No such evidence was found.
Once the language was chosen, we faced different implementation choices. One possibility was to compile Ada code directly to Hitachi bytecodes. This approach is the cleanest and most efficient, but would have required considerable effort to build an Ada/Hitachi cross-compiler. We could also have discarded the RCX firmware and compiled to a different operating environment. Again, a considerable amount of effort would have been required, so this approach was temporarily shelved, although it is a target for future projects.
Instead, we chose to leverage existing resources in the Mindstorms user community. There is a C interface to Mindstorms called Not Quite C, or NQC for short, developed by David Baum [Ba99]. Using aflex and ayacc, Ada versions of lex and yacc developed at the University of California at Irvine, we are building an Ada-to-NQC translator that takes simple Ada programs (strictly speaking, an Ada subset for Mindstorms that we will define) and converts them to their NQC equivalents. This enables us to get the Ada advantages of Ada programming for our students while at the same time saving us the effort of writing a back end for a new hardware architecture and OS.
We provide student access to the translator through a new button on AdaGIDE, the GUI interface to our Ada compiler:
If translation is successful, the NQC compiler and downloader will be invoked to send the program to the student's robot.
Since our course is a computer science course and not a robotics course, and since it is intended for both technical and non-technical majors, we focus on the programming of the robots and basic computer science constructs, not the construction of the robots themselves. Basic concepts can be illustrated with a very simple robot with two motors connected to separate wheels and output ports, and a Mindstorms touch sensor connected to a bumper and an input port.
Our goals are to introduce students to basic computing ideas, including sequential control flow, selection, iteration, input/output, arrays, graphics, procedures, and file processing. Some of these concepts easily lend themselves to robotics, others are a better suited for a more conventional paradigm.
Sequential control flow is the easiest to demonstrate. Here is a simple sequential Ada program that makes a robot move forward for two seconds, stop, turn right, go forward for one second, and stop:
beginMotors_Onfwd(Outputs => Output_A + Output_B); --can use additive notation to combine ports Wait(Time => 20); --Time intervals are in tenths of a second Motors_Off(Outputs => Output_A + Output_B);
Motors_Onfwd(Outputs => Output_A);--moving left wheel causes turn to right Wait(Time => 10);
Motors_Off(Outputs => Output_A);
Selection is not too much harder, and dramatically shows students the power of decision making in a computer by giving a device the ability to react to its environment. The code below shows how to make a robot react if it runs into something by backing up, turning right for one second, and continuing:
if Sensor_1 = 1 then --contact with the bumper activates a touch sensor Motors_OnRev(Outputs => Output_A + Output_B); --back up robot Wait(Time => 10); --for one second Motors_Off(Outputs => Output_A); --start the turn Wait(Time => 10);
Motors_OnFwd(Outputs => Output_A + Output_B);--and go forward again
Iteration can be shown with simple loop constructs. Here is a program fragment that causes the robot to play successively higher tones (the RCX unit comes with a built-in speaker):
forI in 1..10 loop Play_Tone(Freq => I * 100, Duration => 10);
The advantages of procedures for repetitive tasks can be shown in a number of ways. Here's a procedure that makes the robot turn back and forth a given number of times:
Shake(Times : in Integer) is
for i in 1..Times loopMotors_OnFwd(Outputs => Output_A);
Motors_OnRev(Outputs => Output_B);
Motors_OnRev(Outputs => Output_A);
Motors_OnFwd(Outputs => Output_B);
Although NQC and the RCX support additional features like data logging and a simple display, it is our current belief that topics that might use those features (like file processing, I/O, and graphics) do not map particularly well to simple robotics projects. At this point, our plans are to teach these topics using conventional programming exercises.
We have only scratched the surface of the possibilities that Mindstorms presents for computer science education. Much of what is described here is dictated by our target audience of both technical and non-technical majors. If similar approaches are taken for the standard introductory course for computer science majors, considerably more sophisticated tasks can be attempted. Institutions that want to try something similar, however, need to be aware of important logistical issues, including inventory control for dozens or perhaps hundreds of Mindstorms kits.
Nonetheless, future work includes incorporating Mindstorms into our upper level programming courses, where we can develop more advanced programming challenges and demonstrate more sophisticated programming concepts like real time processing. We also are developing a simulator for a simple robot and the Ada interface, so that students can work on their robot programming projects outside the laboratory. The NQC and RCX firmware still impose limitations on how subroutining and other coding is done, so it would be useful to compile to a different operating environment like Markus Noga's LegOS [No99]. Finally, we intend to perform controlled studies to evaluate the effectiveness of using robots in promoting student mastery of basic computer science concepts.
This work was funded by a grant from the Institute for Information Technology Applications, whose support is gratefully acknowledged.
[Ba99] Baum, The NQC web site
[Fe99] Feldman, Ada as a Foundation Programming Language, Fall 1999
[HP98] N. Herrmann and J. Popyack, “Creating an Authentic Learning Experience in Introductory
Programming Courses”, ACM SIGCSE Bulletin, Vol 27, No 1, pp 199-203.
[Kn99] Knudsen, The Unofficial Guide to LEGO MINDSTORMS Robots , O'Reilly & Associates, ISBN: 1565926927, buy it online here
[Le95] Levy, Computer Language Usage In CS 1: Survey Results, September 1995 SIGCSE
Bulletin, Volume 27, Number 3
[No99] Noga, The LegOS Operating System web site
[UW99] M. Urban-Lurain and D. Weinshank, “I Do and I Understand: Mastery Model Learning for
a Large Non-Major Course”, Proceedings of the 30th SIGCSE Technical Symposium on
Computer Science Education, New Orleans, LA, 1999, pp 150-154.