Gem #112: Lego Mindstorms Ada Environment — Part 1

by Pat Rogers—AdaCore

Let’s get started...

The Lego Mindstorms processor has limited storage available, certainly not enough for a nontrivial application and a complete run-time library. Therefore, the GNAT tool-chain does not implement the full Ada language for this platform. A considerable number of language capabilities are still included, especially the Ravenscar tasking profile, but the entire language is not available.

For example, full exception semantics are not implemented. Programmers may raise user-defined and language-defined exceptions, and may handle them locally, but unhandled (and reraised) exceptions do not propagate dynamically up the call chain as they do in full Ada. Instead, an unhandled exception results in a call to a single global routine referred to as the “last chance handler,” so called because it does not return to the application. Developers may define application-specific last chance handler replacements for the default implementation.

The default implementation shipped with this compiler simply shuts down the Mindstorms processor. However, the Ada interfaces define an alternative that displays the file name and line number corresponding to the location of the exception, using the LCD on the Mindstorms “brick.” The routine also briefly buzzes the speaker to get the user’s attention. It then goes into an infinite loop so that the user can note the location of the exception. This implementation is declared in the NXT.Last_Chance package, shown below.

<b>with</b> System;
<b>package</b> NXT.Last_Chance <b>is</b>
   <b>procedure</b> Last_Chance_Handler
     (Source_Location : System.Address;
      Line            : Integer);
   <b>pragma</b> Export (C, Last_Chance_Handler, "__gnat_last_chance_handler");
<b>end</b> NXT.Last_Chance;

Note the pragma exporting the name as "__gnat_last_chance_handler", the routine name invoked by the run-time library when an unhandled exception is raised. Applications do not call this routine themselves, since it does not return to the caller and shuts down the system. Instead, they override the symbol and have the linker include their version of the routine in the executable so that the run-time library can call it if necessary. In the case of the Mindstorms interfaces, the user specifies the package NXT.Last_Chance in a with-clause, typically in the main program.

<b>with</b> NXT.Last_Chance;
<b>procedure</b> ...

You can see the source for the body of the procedure, indeed the sources for all the interfaces, in the “drivers” directory under your installation root. By default this would be:


Although not a very large or complicated procedure, in such a memory-constrained environment every byte may be precious, so you should take the size of the code into account. The procedure uses the audio interface to buzz the speaker, for example, and the LCD display package is also pulled in. If you were not otherwise using those packages (and their dependents), this could make a difference. However, there is no requirement for you to use this version of the last chance handler, so you can simply comment out or remove the with-clause if you don’t want the fancier handler and associated code included.

About the Author

Pat Rogers has been a computing professional since 1975, primarily working on microprocessor-based real-time applications in Ada, C, C++ and other languages, including high-fidelity flight simulators and Supervisory Control and Data Acquisition (SCADA) systems controlling hazardous materials. Having first learned Ada in 1980, he was director of the Ada9X Laboratory for the U.S. Air Force’s Joint Advanced Strike Technology Program, Principle Investigator in distributed systems and fault tolerance research projects using Ada for the U.S. Air Force and Army, and Associate Director for Research at the NASA Software Engineering Research Center. He has B.S. and M.S. degrees in computer systems design and computer science from the University of Houston and a Ph.D. in computer science from the University of York, England. As a member of the Senior Technical Staff at AdaCore, he specializes in supporting real-time/embedded systems developers, creates and provides training courses, and is project leader and a developer of the GNATbench Eclipse plug-in for Ada. He also has a 3rd Dan black belt in Tae Kwon Do and is founder of the AdaCore club “The Wicked Uncles”.

Last Updated: 10/26/2017
Posted on: 10/17/2011