Scripting languages
Lua - an alternative to PLC programming?
When programming real-time applications, developers still fall back on their usual, albeit often complex, languages such as C and C++ or traditional PLC programming. Scripting languages such as Lua are an interesting alternative.
Scripting languages such as Lua are relatively quick to learn, you don't have to use a dedicated compiler and the source code is platform-independent - so it can be used on Windows, Mac OS X and Linux. The deliberate simplicity can also be seen in the scope of the language: While C++ comprises around 2000 language elements and C around 850, Lua has just 250 commands with which it can be programmed efficiently. Admittedly: In terms of performance, Lua lags behind programming languages such as C and C++; however, this speed disadvantage can be compensated for with the help of suitable DLL and real-time libraries.
Figure 1: Lua commands can be instantiated on real-time machines via RPC using simple script commands.
© Profimatics/eclipse.orgSo what is so special about scripting languages such as Lua for programming real-time applications in the field of industrial automation? To answer this question, it is worth taking a look at a possible combination of Lua and a real-time operating system, as is the case with the RT-Lua development package from Profimatics in Barmstedt. Here, direct access to the intimate real-time operating system environment of Tenasys is possible by means of prepared interfaces; this allows the direct use of real-time functionality from a scripting language (Figure 1). This includes inter-process communication, real-time access to the existing hardware (I/O, PCI bus etc.) and also the network functions (TCP/IP etc.).
The connection between the scripting language and the real-time platform can be established in several ways. A common approach is an RPC connection, normally based on the TCP/IP protocol. This allows the developer to access the real-time environment using standardized interfaces without having to use special or proprietary protocols. However, this RPC-based access to a real-time OS machine can also be realized in the form of a client-server implementation. This allows Lua script commands to be executed directly on the server, variables to be created and new functions to be loaded onto the server. In addition, Intime's RPC library is also available for other operating systems such as Linux and Mac OS X. This means that functions can also be executed from these operating systems in real time on a corresponding runtime system.
Lua for rapid prototyping and testing
Figure 2: The code snippet shows a function that sends a CAN message as soon as the memory area 'can_mem' has been displayed.
Against this background, the Lua scripting language can now also be used for rapid prototyping and testing of real-time systems, making it possible, for example, to commission special real-time hardware with just a few script commands. This does not even require the integration of special drivers, which are programmed in C or C++ as standard. This is already being implemented in the field of medical applications, for example, where the SJA1000 CAN controller is used. The code excerpt in Figure 2 is intended to show how easy it is to write to the registers of a CAN controller chip in order to send a CAN message.
This driverless addressing of hardware with the help of Lua is possible because privileged commands can be executed natively under Intime, which are otherwise only available under Windows in ring 0 - i.e. at kernel level. In order to make these commands more readily available, they can be combined in a library in Lua. These include, for example, calls for reading and writing IO ports, for direct access to PCI components and for displaying memory areas of a PCI card.
Ideal field of application - robot control
Lua can also be integrated into existing applications, as Bosch Rexroth has done with Indra Motion MLC - an integrated engineering and runtime system for logic, motion, robotics and hydraulics tasks.
RT-Lua consists of modular software components that require no installation and are each the size of a conventional JPEG.
© Profimatics/eclipse.orgThis means that complex movements can be executed with simple script commands, which allows simplified programming of robots and other machines without PLC knowledge. Indra Motion MLC uses two features of Lua for this purpose: on the one hand, Lua is extended with the existing functionalities of the target system (extensible), and on the other hand, the scripting language is integrated into the application (embeddable) and made available to the user.
Lua is interesting not least for C and C++ developers, as existing functions or applications can be provided with a Lua API using suitable tools. Wrappers are used to check C/C++ functions for their real-time suitability. This saves the effort of having your own test framework, as debugging can then be carried out using Lua script commands. In this context, applications can be developed interactively; individual commands can be entered within the Lua console, which are then translated into byte code and executed in the real-time environment.
However, it is more convenient to debug Lua real-time applications within the EclipseLDT development environment. Here developers can set breakpoints as usual, view variables, change them and more. Communication between EclipseLDT and the debugger is implemented via a TCP/IP connection.
The necessary ingredients
Using EclipseLDT, Lua real-time applications can be debugged directly on the target computer.
© Profimatics/eclipse.orgThere are two ways to integrate existing code into Lua: Either you program the API by hand, which can be very time-consuming for larger projects. Or you can use the SWIG (Simplified Wrapper and Interface Generator), which can be used to make modules written in C or C++ available within Lua. The source code of the original library remains unchanged and additional C functions, so-called wrappers, are created. The wrappers therefore serve as an interface between the library and the target language and can be called at any time from the target language, taking over and passing parameters and delivering results.
As already mentioned, Profimatics has put together a complete package with RT-Lua that can be used precisely for the application scenarios described. This package consists of four components: An Intime for Windows or Intime-Distributed-RTOS runtime system environment, EclipseLDT as the development environment (including debugger), the Lua interpreter itself and three add-ons that come as standard with RT-Lua. These are a Modbus server and client for communication control between PLC and PC systems, the open-source MQTT protocol (Message Queue Telemetry Transport) for M2M communication and SQLite for saving data.
Network scanners for real-time environments can be programmed efficiently and quickly using two Lua commands - hpe.open and hpe.receivepacket.
© Profimatics/eclipse.orgThe three add-ons are all written in C and are primarily used for simple and fast communication with the outside world. The socket interface can not only be used by the user, but is also used internally by the EclipseLDT development environment to communicate with the target system.
The Eclipse used is EclipseLDT (https://eclipse.org/ldt), a cross-platform development environment for Lua programmers. EclipseLDT offers all the features that can be expected from a modern development environment: Color highlighting of syntax, online help, breakpoints, dynamic changing of variables and dynamic execution of code at runtime. Eclipse-LDT is available as a server application under In-time and RT-Lua has the corresponding client to turn it into a complete development environment.
The special feature of the Intime real-time platform is its explicit hardware partitioning. This means that PC resources such as the available processors can be assigned to a combination of deterministic and non-deterministic applications without them influencing each other. Based on the existing APIs, access to the real-time platform using Lua is possible without any major technical effort. Last but not least, access to industrial Ethernet-based fieldbus systems will be possible in future using the RT-Lua add-on. This means that the Intime High Performance Ethernet driver (HPE) can also be used in combination with Lua. This also includes the use of the special Ethernet controller I210 functionality from Intel.
Author: Robert Bühlmann is CTO of Profimatics in Barmstedt.
Interesting facts about Lua
Lua combines a procedural syntax (similar to that of Pascal) with powerful data description constructs based on associative arrays and extensible semantics. Lua also supports object-oriented and data-driven programming. On top of this, the language can be executed interactively, is fast in execution and robust. It is subject to a permissive open source license, which was published by the Massachusetts Institute of Technology in 1988 and can be used for commercial products. The Lua Virtual Machine is written in ANSI-C and can be ported to any system for which an ANSI-C compiler exists. An operating system is not absolutely necessary.
Lua is used in numerous well-known applications such as Adobe Lightroom, Wireshark and Wikipedia. But Lua is also very popular with computer game developers, for example to control complex game characters with just a few commands.















