The EV3 Robot: An Embedded System Running Linux
(Backward compatible with NxtJlib)
Since a few years the Lego Mindstorms NXT brick is a good choice for the teaching and hobby environment. Our NXT based libraries NxtJLib for Java, Android and Python and especially the NXT Robot Simulation is widely used and our support for the NXT continues. But the good old NXT brick is more and more replaced by the new EV3 brick that, from outside, looks almost the same but has a completely different inner working. The microprocessor is a 32-bit ARM9-based Sitara AM1808 from Texas Instruments clocked at 300 MHz running under the Linux operating system. It can run the ARMv5 port of Java SE Embedded Version 7 and 8 from Oracle.
Unofficially the NXT will continue to be supported by Lego until 2015. What this means is unclear, but you may get more and more difficulties to purchase sensors and spare parts. Because the leJOS and NxtJLib/NxtJibA Java frameworks are now available for the EV3, you can easily port your programs from the NXT to the EV3. From the educational point of view, the EV3 brings nothing extremely new into the classroom. So you can easily stay with your NXT for the next couple of years. But it is a wise decision to switch to the EV3 for new projects and investments.
When started without any SD card, the Lego menu is displayed and the system is protected from outside access to the Linux file system. But if booted with a properly configured SD card, the Lego menu can be circumvented and the Linux file system can be made visible to a SSH client connecting via WLAN, USB NDIS or Bluetooth PAN. In this mode the EV3 acts as an embedded microcontroller system with integrated robotics hardware (motors and a multitude of sensors). Now the EV3 becomes a rich teaching platform for all kind of activities beyond simple robotics, like client-server programming, Linux shell scripting, etc.
leJOS not only provides an easy access to the Linux OS, but a also a great library of Java classes to control attached motors and sensors. It uses the Java Native Interface (JNI) to call the underlying C-routines provided in public domain by the Lego/LabView group.
In principle any other programming language running under Linux could interface with the underlying C-routines, but the construction effort should not be underestimated.
The NXT brick was used in two distinct modes: When booted, a server-like program is started that listens for a remote client connected via USB or Bluetooth using a propriety byte-based protocol. This mode of operation is also called Direct Mode, because the remote computer sends commands to the brick where they are executed immediately. The brick then replies with confirmation messages or sensor data. In Direct Mode, the computer can be programmed in many different languages, among them Java, C/C++, Microsoft's C#, Python, Android Java, Apple's Objective-C, etc.
In the Standalone Mode, also called Autonomous Mode, a robotics program is written on a developing system and compiled either to microprocessor's machine code or to byte code that is interpreted by a virtual machine running on the brick. After compilation/linking the program is downloaded to the brick where it is executed. This mode has the clear advantage that the robot runs independent of any host system. For Java the leJOS firmware needs to be installed on the brick that provides a VM for a subset of Java.
This NXT scenario changes for the new EV3: Neither Lego nor leJOS provides a kind of server to handle requests from remote host, so the Direct Mode is not supported. In contrast to the NXT, the leJOS group decided not to write a propriety firmware but to run the Linux OS already installed on the brick. Oracle helped them to implement their embedded version of the Java VM. This was a wise decision, because now the EV3 may use standard software from the Linux and Java world.
2 The EV3JLibA Library (Autonomous Mode)
Like the former NxtJLibA, the class library EV3JLibA simplifies the use of the leJOS library in autonomous mode. It has a clean OOP design and sits on top of the leJOS library. Because EV3JLibA uses the underlying leJOS library, all leJOS classes are still available. In EV3JLibA the new Lego EV3 motors and sensors are prioritized. Class names without a special prefix correspond to the new Lego EV3 actors/sensors. Here a simple demonstration example of a robot bumper (the Gear class corresponds roughly to the Pilot class of the leJOS library):
(Please refer to the NxtJLib website to get information about the general library concept.)
As you see, the NxtRobot class of the former NxtJLibA is replaced by the LegoRobot class (in all NXT, EV3 and simulations libraries).
3 The BrickGate Server
In many robotics applications, the robot is controlled at runtime from a remote computer, a smartphone or a special purpose remote console with buttons, joysticks, etc. This kind of application (analog to the Direct Mode on NXT) is now also available on the EV3. An IP socket server called BrickGate is started as autonomous program on the EV3 that listens for a remote IP client to connect. A user friendly text based protocol is used for the communication between the client and the server. The client sends a command string to inform the server which method to invoke and the server replies with a result string or sensor data. Evidently the client can be written in any programming language that supports socket communication.
(BrickGate includes the autonomous invocation of Python scripts. It is a major upgrade of the former EV3DirectServer whose source code is still public and contained in the EV3JLib distribution.)
In the leJOS menu, the Brickgate program can be defined as Default Application to start it with one single click when the brick is booted.
leJOS provides a set of classes for remote access of the brick using the RMI (remote method invocation). This looks great to simplify the communication protocol and was used in a first version of the Brickgage server. In many remote applications the client polls robot sensors repeatedly to decide which action to perform. The time between the issue of the polling command and the response of 100 ms is often the upper limit to react fast enough. Unfortunately using RMI, the system was too slow to get acceptable results. BrickGate uses basic IP socket communication and provides response times as low as 10 ms on a WLAN link and in the order of 50 ms for a Bluetooth PAN link. As usual with IP and Java, no guarantee can be given that these time limits are never exceeded.
The BrickGatealso integrates a Dynamic Update Client (DUC) that conforms to the DUC protocol of No-IP Free Dynamic DNS (http://www.no-ip.com). This allows to access the EV3 through a cheap router that gets a changing IP address from the provider's DHCP. But putting all these things together, the EV3 and its attached actors/sensors may be controlled via Internet by a remote client anywhere on the world.
(Please consult the website of BrickGate to get more information and learn how to download and install it. Instead of the original leJOS SD card distribution you may install our expanded distribution that contains BrickGate)
4 The EV3JLib Library (Direct Mode)
Now we have a socket server running on the EV3, it is rather straightforward to build applications to control the EV3 remotely like in until now with the NXT in direct mode. To port easily programs between the autonomous mode to the direct mode and vice versa, we apply the same class design for the autonomous and direct libraries.
The default constructor (with no parameters) of class LegoRobot opens a dialog box, where the IP address of the EV3 brick can be entered. (You can also pass the IP address in string format to the constructor. In this case no input box is shown.)
The following programs has the same purpose as the autonomous program above. When the touch sensor is hit, the robot moves slightly back and makes a left turn. Then it moves forward again.
(If you have the BrickGate already installed because you use our expanded leJOS distribution, attach two motors at port A and B and a touch sensor at port 1. Otherwise consult the website of the BrickGate to download and install it. Use WebStart to see how the program runs.)
As you see, the differences between the two programs are minimal: Instead of checking for the Escape button to be pressed, an endless loop is used and robot.exit() removed. The direct mode program stops by closing the connection dialog. Because the IP link to the EV3 is broken at this moment, the BrickGate returns gently to its Wait for a connection state. On the other hand if BrickGate is shut down or the EV3 turned off, the client programs detects the broken link and displays a message box. Even in a unexpected interruption neither the server nor the clients hang.
5 The EV3JLib Library For Jython (Direct Mode)
The EV3JLib Java class library can also be used with Jython. See the Jython website for more information. Is it included in the distribution of the TigerJython IDE. (See also the online tutorial for Python (in German, to be translated in English soon).
6 The EV3JLib Library In Simulation Mode
Part of the EV3JLib Java class library is also available in the LegoRobot simulation packages (former NxtSim). It is distributed with the JGameGrid library. As shown in the following example, the code remains almost the same apart the modified package name and the addition of an environment where the layout of obstacles (detected by the touch sensor) and the background (as seen by the light sensor) are defined.
Execute the program locally using WebStart.
7 The EV3JLib Library for Android
The direct mode library EV3JLib has been ported to Android and is distributed as part of the JDroidLib framework that makes it extremely simple to create app for smartphones and tablets to control the EV3 brick. Consult the JDroidLib website for more information. The following code is the complete program for the touch demo app running in a console screen window.
The communication link between the Android device and the EV3 is established using the Bluetooth PAN protocol. You have to perform a Bluetooth pairing process on the Android device (but not on the EV3) and select the EV3 like a tethering Internet access point (only supported by Android version 3 up). On the EV3 brick, the BrickGate must be up and running. The program terminates with "Connection lost" when BrickGate is stopped by pressing the EV3 Down+Enter button comibination.