Preprints of the 8th IFAC Symposium on Advanced Control of Chemical Processes The International Federation of Automatic Control Furama Riverfront, Singapore, July 10-13, 2012

GNURadio, , Xcos and COMEDI for Data Acquisition and Control: An Open Source Alternative to LabVIEW

Jagdish Y. Patil ∗ Balashish Dubey ∗∗ Kannan M. Moudgalya ∗∗∗ Rakesh Peter ∗∗∗∗

∗ Systems & Control Engineering, IIT Bombay, Mumbai 400076, [email protected] ∗∗ Chemical Engineering, IIT Bombay, Mumbai 400076, [email protected] ∗∗∗ Chemical Engineering and Systems & Control Engineering, IIT Bombay, Mumbai 400076, [email protected] ∗∗∗∗ Dept. of Computer Science, Amrita University, Coimbatore, [email protected]

Abstract: This article describes an attempt to create an open source equivalent to the data acquisition and control software LabVIEW. The proposed solution uses GNURadio, OpenCV, Scilab, Xcos and COMEDI on . GNURadio gives a convenient graphical front end. The computations can be carried out using OpenCV or Scilab and Xcos. The device drivers given in COMEDI are used to access real time data. We also present PyGTK based improved graphical user interface for GNURadio.

1. INTRODUCTION open source software systems also and further increase the capabilities of the solution proposed in this work. Engineering institutes and colleges in a developing country This paper is organised as follows. Section 2 describes the need a low cost solution to data acquisition and control GNURadio in brief. Section 3 talks about how we have applications. The only available solutions are proprietary, integrated the open source libraries and packages into and are often expensive, at least to the industry. As a GNURadio. In Section 4, we explain how we interfaced result, the students who are trained only on a propriety the Single Board Heater System (SBHS) through USB software find themselves handicapped on joining industry. and through Scilab and COMEDI. Section 5 compares the As the required information on the drivers of many popular GNURadio solution with LabVIEW and points out the devices are not available, one has to depend on propriety required improvements. data acquisition and control systems. The Linux project Control and Measurement Device Interface [COMEDI, 2. GNURADIO 2012] has drivers to many popular devices and thus helps partially address this problem. Realising the importance of GNURadio is a free software toolkit for learning, building, drivers in the public domain, some governments have also and deploying software defined radio systems. It is based started making this a requirement in their procurement on ++, Python and its graphical environment resembles policy [Committee, 2011]. Our group has also made at- LabVIEW. We selected GNURadio in this work because it tempts to access hardware through open source software provides a great flexibility to build GUI (Graphical User [Arora et al., 2010, Moudgalya and Arora, 2010, Arora Interface). For example, it has knobs and sliders using et al., 2011]. Nevertheless, to the best of our knowledge which, parameter values can be changed at run time, see there have not been many efforts to find an open source Fig. 4. Some of these features are more difficult to imple- equivalent to a complete data acquisition and control sys- ment through Scilab or Xcos. GNURadio can be utilised tem, such as LabVIEW [NI, 2012]. in the development of flexible and scalable, measurement This paper deals with the creation of an open source and control applications rapidly and at minimal cost. alternative for LabVIEW, using GNURadio [2012], Scilab An application is created by connecting various blocks. [2012], Xcos [Scilab, 2012], OpenCV [2012] and COMEDI Conceptually, blocks process infinite streams of data flow- [2012], all of which are open source software. While some ing from their input ports to their output ports. The software, such as Scilab, can run in many platforms, attributes of a block include the number of input and some others, such as COMEDI (Control and Measurement output ports they have, as well as the type of data that Device Interface), run only on Linux. By the very nature of flow through each one of them. The most frequently used open source software, it is possible to integrate many other types are short, float and complex. Some important sample ? This work is partly funded by the National Mission on Education blocks such as sources, sinks, simple mathematical op- through ICT, http://www.sakshat.ac.in. The early work on GNU- erations block, filters block, type conversions block, and Radio was done by Ankit, Weissalisa and Sonal. variables block come bundled along with GNURadio.

© IFAC, 2012. All rights reserved. 626 8th IFAC Symposium on Advanced Control of Chemical Processes Furama Riverfront, Singapore, July 10-13, 2012

3. INTEGRATING SOFTWARE PACKAGES WITH GNURADIO

For online control and signal processing, we need good mathematical tools and the ability to incorporate new algorithms. Scilab and Xcos can meet this requirement. Another important requirement is real time data acqui- sition, for which we have used COMEDI. An excellent tool for image processing is OpenCV. In this section, we describe a few combinations in which we have used these tools.

3.1 GNURadio with OpenCV

To equip GNURadio with image processing capability, Fig. 1. Inverting and extracting the input image in GNU- OpenCV (Open Source Computer Vision Library) is used. Radio OpenCV is a library of programming functions mainly aimed at real time computer vision. It focuses mainly on real-time image processing. This library is written We will begin our discussion with blocks that are coded in C as well as C++. Image processing is any form of in Python. A unique ID has to be assigned to every block signal processing for which the input is an image, such which can be alphanumeric in nature. If two blocks share as a photograph; the output of image processing may be the same ID, the second one to be generated will overwrite either an image or, a set of characteristics or parameters the first. related to the image. Most image-processing techniques The graphical user interface of GNURadio is called GNU- involve treating the image as a two-dimensional signal and Radio Companion (GRC). Using GRC, one can write GUI applying standard signal-processing techniques to it. for GNURadio. In GRC, applications are represented in Now we will discuss how to integrate OpenCV with GNU- terms of flow graphs. The flow graph in Fig. 1 shows the Radio. To compile and run OpenCV code in GNURadio, inversion of an image. GRC is written in Python. GRC we need to set the path of installed OpenCV in corre- works only with Python code. GRC can accommodate any sponding Makefile of a block in GNURadio. We also need GUI development tool written in Python. Python GTK is to include the following OpenCV modules in the same used to create most of the GUI in GRC [GTK, 2012]. Makefile, For creating new application we have to connect the ap- (1) cv - Main OpenCV functions. propriate blocks, by clicking which, can set the attributes. (2) cvaux - Auxiliary (experimental) OpenCV functions. The blocks can be placed in the canvas of GRC by simple (3) cxcore - Data structures and linear algebra support. drag and drop mechanism just like in LabVIEW. After (4) highgui - GUI functions. compiling the flow graph, GRC converts it into a python file and stores it in a specified path with a specified name. More details are given in appendix 1. We can later directly execute this file from the terminal. At the time of execution, all the blocks in the flow graph Fig. 1 shows the inverting and extracting the input image should be in the python path, meaning that all the blocks in GNURadio. The application is created by connecting should be accessible through an import statement. The 3 blocks, viz. image source, invert image and sub image. scheduler in GRC uses Python built-in module threading The image source extracts the image from path specified to control the start, stop or wait operations of the signal in its block attribute and passes it to the next block. As the name suggests, the invert image block inverts a gray flow graph. scale image and passes the image pixel values to the sub Every block in GRC corresponds to an XML file that de- image block, which extracts the region of interest from an scribes the block’s parameters, inputs, outputs, and other image and displays it in an image window along with the attributes. For each block, there is a separate XML file. output image. Besides this, we also have added two more GRC has a list of available blocks at the time of start up. blocks i.e. image sink and image file blocks. These display In order for a new block written in Python to be in this list, the output image in an image window and create an image an appropriate XML file should have the above mentioned file in the specified format, respectively. attributes. In addition, the corresponding Python code should be in an appropriate path. OpenCV supported formats are BMP, DIB, JPEG, JPG, JPE, PNG, PBM, PGM, PPM,SR, RAS, TIFF and TIF. Next, we will discuss the handling of C++ code, in which OpenCV is widely used in following areas: 2D and 3D most device drivers are written. One should convert the feature tool kits, Egomotion estimation, Facial recognition device drivers into Python modules and follow the above system, Gesture recognition, Human-computer interaction mentioned process so that they are available for click, drag (HCI), Mobile robotics, Motion understanding, Object and drop operation in GRC. Conversion of C++ code into identification, Segmentation and Recognition, Stereopsis Python is done through SWIG (Simplified Wrapper and Stereo vision (depth perception from 2 cameras), Structure Interface Generator). SWIG generates wrappers around from motion (SFM) and Motion tracking. We can see that C++ functions and classes, so that they can be called from the combination (GNURadio+OpenCV) can be used to Python. acquire data in an image form and analysed.

627 8th IFAC Symposium on Advanced Control of Chemical Processes Furama Riverfront, Singapore, July 10-13, 2012

of digital inputs/outputs; pulse and frequency counting; pulse generation; reading encoders; etc. These drivers work with Linux, and also with Linux combined with the real- time extensions RTAI and RTLinux. The COMEDI core, which ties all the drivers together, allows hardware inde- pendent applications to be written. COMEDI has high- level library and integrated real-time support for most hardware. One of the biggest benefits of using a PC-based DAQ device is that one can use software to customize the functionality and visualization of one’s measurement system. GNURadio provides an interface to DAQ devices supporting COMEDI. The codes in COMEDI are written in C and Python, making the interfacing with GNURadio easy. The details are in the appendix. Fig. 2. Demo of executing Scilab and Xcos from GNURadio 3.2 GNURadio with Scilab and Xcos 3.4 Graphical User Interface in GNURadio

To improve the functionality of GNURadio, Scilab and Good GUIs are critical for user acceptance and subsequent Xcos have been integrated with it. Scilab and Xcos can deployment of any software package. User interfaces deter- be used for image and video processing, and to carry out mine the parameters that a user must enter and display mathematical operations. Scilab has toolboxes for control the resultant outputs. While on one hand too many inputs system, communication, system identification, optimiza- may make the user feel incompetent, too many outputs tion, and many more. Xcos, the open source equivalent of undermine the importance of the critical information. A , is a graphical editor to design hybrid dynamical good GUI can effectively make the package a favorite systems models. Xcos comes with Scilab. amongst the end-users. Scilab is included in GNURadio through the call scilab The GUI supplied with the GNURadio package lacks in module. With this module, it is possible to call the Scilab terms of the simplicity of the design - it supplies the engine from C/C++ code. We also need api scilab to user with a lot of redundant information with a lot of read/write data from/to Scilab memory. Both of these intermediate blocks to process. The default shape of a modules come with Scilab. Details regarding the inter- block is rectangular. The colour of all the blocks are facing of Scilab with GNURadio is given in the ap- identical. These make the GUIs dry and difficult to use. pendix. It is also required that Scilab header files such We explain below how we manipulated the GUI code to as stack-c.h, call scilab.h and api scilab.h are installed in suit our objectives better. /usr/include/scilab directory. The code for the GUI iss python-GTK based. We have The most basic functions we need to call Scilab from modified the same by calling functions from the GTK+ C/C++ are StartScilab, SendScilabJob, SendScilabJobs library. We have been able to use images of the actual and TerminateScilab. StartScilab is used to initialize hardware to replace the standard block supplied in the and start scilab engine in Call Scilab. To send a task package. We have also switched off the parameters that from C/C++ code, we use SendScilabJob/SendScilabJobs. were displayed with the block. This helps the end-user These functions are provided in call scilab. SendScilabJob appreciate and relate to the experiment better instead of is used to send a task, while SendScilabJobs can send many feeling lost trying to make sense of all the blocks displayed. tasks for processing by Scilab engine. To stop the Scilab en- As not all the blocks would have their corresponding gine in C/C++ code, TerminateScilab is used. Using these physical realisations, we have added a circle and an arc functions and others defined in stack-c.h, call scilab.h and as alternative shapes for source/sinks/intermediate blocks. api scilab.h header files, we have created an application A colour scheme has also been introduced in the GUI to based on Scilab in GNURadio. make it look visually-appealing. The above discussed procedure is to be used to interface With the above said modifications in place, we have Xcos with GNURadio. To execute Xcos through GNURa- built a GUI capable of supplying our end-users with dio, we have used importXcosDiagram and scicos simulate the appropriate amount of information in a to-the-point functions. The importXcosDiagram function imports Xcos manner as shown in Fig. 3. Further modifications of the files into Scilab. The scicos simulate function is used to same can be done on a case to case basis to suit the desired simulate Xcos diagrams in batch mode. It requires the objectives better. scs m structure that can be obtained by loading in Scilab the *.cos/*.xcos files. Fig. 2 shows the demo of a PID con- 4. DATA ACQUISITION IN GNURADIO troller to control liquid level in a tank through GNURadio. GNURadio can act as a data acquisition system by adding 3.3 GNURadio with COMEDI necessary blocks to interact with the data acquisition de- vices and to set the control parameters. The different ways COMEDI is a free software project that develops drivers, to interface the SBHS through GNURadio is explained in tools, and libraries for various forms of data acquisition: this section. We also illustrate interfacing a DAQ card with reading and writing of analog signals; reading and writing GNURadio.

628 8th IFAC Symposium on Advanced Control of Chemical Processes Furama Riverfront, Singapore, July 10-13, 2012

Fig. 3. A typical GNURadio GUI developed in this work

4.1 Interfacing SBHS through USB present in ATOMS module of Scilab is used for communi- cation [Toolbox, 2012]. This toolbox has routines to open The Single Board Heater System (SBHS) is a lab-in-a- the COM port, write to it, read from it, etc. After loading box setup suitable for performing numerous control exper- this toolbox, the COM port is opened for Scilab. The iments [Arora et al., 2010, Moudgalya and Arora, 2010, fan speed and heater current can be controlled and the Arora et al., 2011]. It is based on ATmega16 microcon- temperature of the plate read, as explained earlier. troller and it communicates to a PC through a serial GNURadio can also access SBHS through Scilab. We interface. have included a block containing the path attribute for The user can choose the number of data bits, baud rate, this purpose in GNURadio. The path must set to the stop bits, parity, etc., for serial port communication. The serial toolbox directory. After executing the flow graph, setup is programmed to communicate at 9600 baud rate GNURadio initialises Scilab and loads the serial toolbox with 8 bits of data per character frame. The values of 253, in it. In this interface, the code for writing and reading the 254 and 255 are reserved as command words, respectively input-output parameters are partly written in Scilab. The to indicate fan speed input, heater power input and the input parameters are set in Xcos file. There is no provision sensed temperature output. The data range 0–252 is used in Xcos to change the input parameters in real time. It is to represent the data. also possible to control inputs from GNURadio directly as we discussed in section 4.1. Fig. 5 shows the execution of To access SBHS from GNURadio directly, we have created Scilab step test code through GNURadio on SBHS. two blocks namely SBHS source and plot sink. The com- munication is made through the serial port. The source Scilab-SBHS interface is sufficient if we don’t need to block is written in C++, while the sink block is written change the input in run-time. A flaw in Xcos causes it in Python. The source block in GNURadio interfaces with not to be suitable for applications in which inputs need SBHS. It opens the port for communication with SBHS. to change during run-time. In GNURadio-SBHS interface, we have overcome this drawback of Xcos. We can set input To set the fan speed, the source block sends 253 followed parameters as variable that can be altered using sliders at by the value taken from the block attribute. This process any instant. keeps on executing until one presses stop button on GRC. Because of this, it is possible to alter the input parameters during run time. The inputs can be changed with sliders 4.3 Interfacing DAQ Cards through COMEDI as shown in Fig. 4. This is the main advantage of using the GNURadio-SBHS interface over the Scilab-SBHS in- We have discussed GNURadio-COMEDI interface in the terface. The latter is explained in great detail in Arora previous section. Using a block included in GNURadio, et al. [2010]. The source block follows the same process to user can access any DAQ card present in the COMEDI set the heater value, except that it now sends 254 rather list. For this, we need to set the attribute, DAQ card name, than 253. The source block passes the temperature value as shown in Fig. 6. During run time, this block will load to the sink block to display it in a graphical form, in real- COMEDI library and start accessing the input signal. We time. interfaced four DAQ cards from the COMEDI list and a locally developed card. 4.2 Interfacing SBHS through Scilab and USB 5. COMPARING GNURADIO AND LABVIEW We have also interfaced SBHS through Scilab in GNURa- dio. Since it is easy to design and implement the control We have made a survey on LabVIEW and it’s functional- algorithms in Scilab, we propose this as the recommended ity, see Table! 1. The first column lists LabVIEW’s features interface. SBHS is first interfaced with Scilab through and the second lists the capability of the solution proposed serial communication. The serial communication toolbox in this work. The GNURadio based solution is comparable

629 8th IFAC Symposium on Advanced Control of Chemical Processes Furama Riverfront, Singapore, July 10-13, 2012

Fig. 4. Temperature Plot. It is possible to change the control parameters using the sliders in the left window.

Fig. 5. Xcos graph for step change response experiment in GNURadio

to LabVIEW in data acquisition, control, signal condition- Table 1. Comparison of LabVIEW and GNU- ing and signal processing. Radio on select features As the proposed solution is based on a collection of open LabVIEW GNURadio source software packages, the proposed solution is not as 1 Buses-MODBus, PROFIBus, Buses-USB, serial port, blue- robust. In comparison to LabVIEW, the proposed solution FIELDBus, USB, serial tooth has access to only about 400 DAQ cards. Moreover, the poet, devinet, GPIB, latter runs only on Linux. LabVIEW has a much better VXI, wireless/ethernet communication, NI wifi- GUI and also a lot easier to use. DAQ cards, bluethoot, RS-232, plug in boards. 6. CONCLUSION 2 Signal processing, analysis, Some of these are present and mathematical functions others can be implemented The paper introduces a low-cost open source data ac- through Scilab and OpenCV quisition system developed as a possible alternative to 3 Inbuilt controllers, system CACSD, a Scilab toolkit, can the proprietary software LabVIEW. The flexibility of this identification, control system be used system has been demonstrated through combinations of design 4 Can develop applications in Can be used Hybrid Au- the open source packages in the field of control, DSP, state chart diagrams tomata toolbox in Scilab [Na- image processing, etc. As open source software systems, by jafi and Nikoukhah, 2007] definition, can work any other similar system. As a result, 5 Data acquisition modules Use COMEDI and Scilab it is possible to extend the capabilities of the software de- present veloped in this work. In contrast, as the propriety software 6 Virtual Instrumentation Django based equivalent so- packages are closed, it may not be possible to extend the lution available capabilities and one may have to be at the mercy of the vendors. The solution provided in this work will benefit

630 8th IFAC Symposium on Advanced Control of Chemical Processes Furama Riverfront, Singapore, July 10-13, 2012

the Makefile of GNURadio as follow, AM CXXFLAGS = -lcxcore -lcv -lhighgui -lcvaux -lml AM CPPFLAGS = -I/usr/include/opencv So, while compiling OpenCV based code, GNURadio will look for it’s library and modules in above specified path. For GnuRadio + Scilab - To compile code based on Call Scilab, it is necessary to defines some arguments, variables and path as follows.

(1) CFLAGS - Call Scilab needs to have access to the headers of Scilab core and call Scilab module. a) In the binary version of Scilab, CFLAGS must be set to /path/to/scilab/include/scilab/core/ and /path/to/scilab/include/scilab/call scilab/. b) In the source tree of Scilab, CFLAGS must be set to /path/to/scilab/modules/core/includes/ and Fig. 6. Using USB DAQ Card in GNURadio /path/to/scilab/modules/call scilab/includes/. the academic institutions and small and medium scale (2) LD LIBRARY PATH - Paths to libscilab.so and libjavasci.so enterprises. It is especially suitable to entrepreneurs and To compile code based on call scilab used start ups that are strapped for funds, whether they are gcc(compiler name) -lscilab -L/path to from developed countries or not. The current focus of work /lib/scilab/ -I/path to/include/scilab/fielname.c is to make the interfaces robust and to create instructional material to popularise this methodology. To run an application based on Call Scilab, there are a few other things to set up. Some global variables must be set: REFERENCES (1) SCI - Path to Scilab files a) In the binary version of Scilab, SCI I. Arora, K.M. Moudgalya, and S.Malewar. A low will point to /path/to/scilab/share/scilab/ b) In the source cost, open source, single board heater system. In 4th tree of Scilab, SCI will point to the root of the source tree IEEE Int. Conf. on E-Learning in Industrial Electronics /path/to/scilab/source/tree/ (ICELIE), pages 7–12, Pheonix, AZ, USA, 7-10 Novem- (2) LD LIBRARY PATH - Paths to libscilab.so and libjavasci.so ber 2010. IEEE. (3) LD LIBRARY PATH (Java) - Paths to Java native libraries (libjava.so, libjvm.so, libhpi.so). It is usually provided by the I. Arora, K.M. Moudgalya, K. Venkata, V. Chakraborty, or by Scilab distribution. . Rokade, and R. Rakhi. A low cost, scalable, virtual control laboratory. In Proceedings of the 9th IEEE Series declarations needed to run the code based on Call Scilab. International Conference on Control and Automation (ICCA), 2011, Santiago, Chile, 19–21 December 2011. # With a Scilab source tree: COMEDI, 2012. URL http://www.comedi.org/. Seen on $ SCI={path to Scilab source tree} 30 April. $ export LD LIBRARY PATH=$SCI/modules/.libs/: Policy Committee. Policy on device drivers for pro- $SCI/modules/call scilab/.libs/: curement of hardware for e-governance. Tech- $SCI/modules/api scilab/.libs nical report, Indian Government, 2011. URL http://www.public-software.in/node/3414. # With a Scilab binary: GNURadio, 2012. URL http://gnuradio.org/redmine $ SCI= {path to Scilab binary} /projects/gnuradio/wiki. Seen on 30 April. $ export LD LIBRARY PATH=$SCI/lib/scilab/ Python GTK, 2012. URL http://www.pygtk.org/. Seen on 30 April. # Don’t forget to update i386 to whatever is necessary K. M. Moudgalya and I. Arora. A Virtual Laboratory for $ export LD LIBRARY PATH=$LD LIBRARY PATH: /usr/lib/jvm/java-6-openjdk/jre/lib/i386/: Distance Education. In Proceedings of 2nd Int. conf. on /usr/lib/jvm/java-6-openjdk//jre/lib/i386/server/ Technology for Education, T4E, IIT Bombay, India, 1–3 July 2010. IEEE. $ export LD LIBRARY PATH=$LD LIBRARY PATH: Masoud Najafi and Ramine Nikoukhah. Implementation of /usr/lib/jvm/java-6-openjdk/jre/lib/i386/native threads/ hybrid automata in scicos. IEEE Int. Conf. on Control $ export SCI=/path/to/scilab/ Applications, pages 819–824, 2007. Add above declarations in Ubuntu’s bashrc file. NI. LabVIEW, Seen on 30 April 2012 2012. URL For GNURadio + COMEDI - To make use of the data acquisi- http://www.ni.com/labview/. tion driver in GNURadio set the following path and variable. OpenCV, 2012. URL http://opencv.willowgarage.com /wiki/. Seen on 30 April. (1) Set AM CXXFLAGS as Scilab, 2012. URL http://www.scilab.org/. Seen on 30 -lcomedi -lm -L/path to comedi/lib -I/path to comedi/include April. in Makefile of corresponding block for COMEDI in GNURadio. Serial Toolbox, 2012. URL http://atoms.scilab.org (2) Add following variables in bashrc file of Ubuntu. SCI={path /toolboxes/serial. Seen on 30 April. to comedi} export LD LIBRARY PATH=$SCI/libcomedi.a export LD LIBRARY PATH=$SCI/libcomedi.la APPENDIX export LD LIBRARY PATH=$SCI/libcomedi.so export LD LIBRARY PATH=$SCI/libcomedi.so.0.8.1 For GNURadio + OpenCV - To integrate OpenCV with GNU- Radio includes two flags AM CXXFLAGS and AM CPPFLAGS in

631