Tuesday, August 26, 2014

LabVIEW How-to, with the AndroiDAQ Data Acquisition Module

The AndroiDAQ module was designed as an easy to use and complete data acquisition module for multiple platforms. Not only can it be used with the Android operating system, but it can also be easily used with National Instrument’s LabVIEW, as well as JAVA, Python, and other programming languages. To demonstrate this, Controlled Capture Systems offers a complete data acquisition suite for AndroiDAQ and LabVIEW called AndroiDAQ DEMO for LabVIEW. The term DEMO is short for Dream, Engineer, Make, and Operate. The open source nature of DEMO allows users of LabVIEW to open and view the LabVIEW source wiring diagrams to understand the functions of how to control the AndroiDAQ module under USB, Wi-Fi, or Bluetooth to measure the 8-channels of its analog to digital converter (ADC), or control, as inputs or outputs, the 16-channels of digital IO. The wiring diagrams also show you how to read and/or clear or set the built-in AndroiDAQ micro-SD card and AndroiDAQ’s real-time clock. This article is going to explain several of the sub-VIs (or sub-virtual instruments, which are what LabVIEW methods or functions are called) of the DEMO for LabVIEW application, so that you can apply these sub-VIs to your own custom application with the AndroiDAQ module.

As stated above, the AndroiDAQ module can be controlled via USB, Wi-Fi, or Bluetooth. LabVIEW makes it easy to communicate with the AndroiDAQ module using the common VISA communication VIs. The AndroiDAQ module uses an FTDI FT232R USB to UART converter integrated circuit to connect to the USB port. FTDI offers both VSP (virtual serial port) and D2XX drivers (dynamic link library types) for various desktop and laptop operating systems, which are available for download at:

http://www.ftdichip.com/FTDrivers.htm

To use AndroiDAQ with LabVIEW, the D2XX driver must be installed to allow you to control the FTDI chip, as the D2XX drivers allow direct access to the USB device through dynamically linked libraries, making it a perfect fit for LabVIEW’s Call Library Function nodes. After you install the FTDI drivers onto your computer system you can plug in the AndroiDAQ module into the USB port. After plugging it in your computer's operating system and LabVIEW should recognize AndroiDAQ as a new FTDI USB device; if not, check your FTDI driver installation and ensure that you have the correct one installed for your operating system. FTDI has several LabVIEW examples at:

http://www.ftdichip.com/Support/SoftwareExamples/CodeExamples/LabVIEW.htm

We recommend that you download and review these examples to understand how the FTDI driver is utilized in LabVIEW. We also include the D2XX drivers in our LabVIEW DEMO application zip file, under the name CDM_X.XX.exe, which is available for download at:

https://groups.google.com/forum/?fromgroups#!forum/androidaq-demo-enhancement

The AndroiDAQ DEMO for LabVIEW application utilizes the FTDI driver LabVIEW methods or functions to check if the USB port senses a FTDI chip. If it does sense a FTDI chip on the USB port, the DEMO application requests the USB COM port number and then uses LabVIEW’s VISA communication VIs to connect and communicate with the AndroiDAQ module. Below are LabVIEW wiring diagrams that show how DEMO is wired to perform these functions. This wiring diagram can be seen in DEMO by double clicking the CWDConnectionStatus.vi:
Setup FTDI chip, detect COM port and connect status

If you follow the above wiring diagram, you will see that the FT_Get_Device Description_By_Index VI is called, which looks at all COM ports on the computer system and then reports back if it found any FTDI chip. Then the FT_ Open_Device_By_Description and Get_COM_Port_Number VIs are run, which gives us which COM port number that the FTDI chip is connected to. The FT_Status LabVIEW Property node returns a numerical value, which if not zero, is converted to a ‘true’ Boolean value if the FTDI chip is found. The FT_Status RingText property is used to show the various text codes outputted if an FTDI chip is found and also if there is a problem with communications with the chip. 

Following the wiring diagram passed the CWDConnectionStatus.vi in DEMO, we then enter a communications setup while loop (see below), which uses the VISA communications VIs to configure the serial COM port’s protocols. Special characters are also enabled in VISA to provide end of line implementation, via the ASCII line feed character (TermChar 10). You will note that in this diagram that we also control Wi-Fi (TCP) and Bluetooth setup, depending on if the FTDI chip on USB is detected. If it is not detected, the application opens a dialog box to ask you how you would like to connect to the AndroiDAQ module. The choices are Wi-Fi or Bluetooth. Depending on which choice is made by the user, another dialog box opens, via BluetoothDialog.vi or WiFiDialog.vi, which asks for the Bluetooth COM port name or TCP address and port number, respectively. After the user’s input, the communications while loop then runs the appropriate conditional statement to setup VISA/TCP communication for that selected communications protocol using the input from the user.

USB Setup using VISA VIs and linefeed character as TermChar

Bluetooth Setup, similar to USB setup

TCP or Wi-Fi Setup using VISA TCP VIs 

After all VISA protocol settings are set, the communications setup while loop exits giving us a VISA or TCP string reference data type and also an Error out data type to use with other VISA functions. These data types allow us to read to and write from the COM or TCP port via VISA read/write VIs or TCP read/write VIs. Following along the wiring diagram of DEMO, we then enter the main state machine, or main while loop, of the AndroiDAQ communications engine, which also controls the user’s interface. I suggest that you review this main while loop wiring diagram in DEMO carefully, to understand how all the functions and controls work in the DEMO application; I will though explain some of the communications intricacies with AndroiDAQ below.

AndroiDAQ’s firmware is programmed to operate under a menu driven command structure; meaning, you only need to send the proper character to get AndroiDAQ’s attention and then tell it which menu item, or firmware sub-routine, to run, followed by a ‘carriage return’. Below is a listing of all the standard AndroiDAQ data acquisition menu item commands and numbers for your reference. When sending a serial command to the AndroiDAQ module you simply send an ASCII zero (0) and then the menu item number for the command, and then either press the enter key on your computer system, or programmatically send a carriage return. For example, sending 0 5 ‘carriage return’, prompts the AndroiDAQ module to read and send through its UART, the real-time clock’s current date and time setting. In textual based code, this example would look like this: 05\r. 

Number to send:
Firmware sub-routine ran, in italics:
0
Prepare and receive the 16 channel configuration settings. SetPins
1
Set the 16 channels to the received configuration settings. SetConfig
2
Read input channels and send reading to Serial. GetInputs
3
Not used, this is a test loop for your subroutine development using testloop
4
Read ADC voltages and send these readings to Serial. GetVolts
5
Read AndroiDAQ Real Time Clock (RTC) and send values to Serial. GetTime
6
Auto-set RTC with system values. Note: you need to send date/time information to AndroiDAQ firmware in yy_MM_dd_E_kk_mm_ss format. SetTime
7
Read AndroiDAQ log file (log.txt) send strings to Serial. ReadLog
8
Delete AndroiDAQ log file. DeleteLog
9
Stop continuous input reading
10
Start continuous input reading
11
Start continuous voltage reading
12
Stop continuous voltage reading
13
Center Pan and Tilt example
14
Move Servo Motor
15
Move Stepper Motor

As you can see, it is relatively easy to communicate with the AndroiDAQ module under USB, Wi-Fi, or Bluetooth using this menu driven command structure. This menu driven command structure also allows you to easily add to the AndroiDAQ’s firmware code your own firmware sub-routines, which you then can assign a menu item number under the menuloop() function in the firmware. This allows you to run your subroutine from serial commands under Android, LabVIEW, or other languages. For example, you could write a firmware sub-routine to have AndroiDAQ continually read two I/O port digital states and then when both of these I/O port states are high, or true, have AndroiDAQ trigger an ADC read routine, which can read from any or all channels of the ADC, and then multiply the ADC reading(s) by some conversion factor, and then record the calculated number(s) to the micro-SD card and output that data to the UART for USB, Wi-Fi, or Bluetooth retrieval, and then repeat this loop or stop the reading of the I/O channels all together. All this and more is possible via user modification, or addition to, the AndroiDAQ firmware code using the simple Propeller SPIN programming language by Parallax, as AndroiDAQ uses the Propeller multi-core micro-controller chip for all of its processing work. More information about the SPIN programming language and the Propeller chip can by found in the Propeller Manual, which you can download here:

http://www.parallax.com/downloads/propeller-manual

So let’s say that you wrote the above example sub-routine to implement it in AndroiDAQ’s firmware and you assigned it to say menu item 16 or another empty number. Now how would you write up a quick LabVIEW VI or application to have this function performed via USB, or wirelessly via Wi-Fi or Bluetooth? First you will need to decide which communication protocol that you want to use, USB, TCP, or Bluetooth, or you can decide to support all of them, and then utilize the various VISA/TCP commands to initialize the communications protocols, and then get a VISA/TCP reference to write the AndroiDAQ command via VISA/TCP write, and maybe read the returned values with the VISA/TCP read functions. Below is a simple wiring diagram showing how you could do this using USB. 

Wiring diagram for article example

Here I used the communications while loop from the DEMO application, which I simply “cut and paste” to insert it into this code example. The example VI detects if the FTDI chip is connected to USB via the CWDConnectStatus.vi. If it is sensed, the communications while loop configures the VISA protocols and then exits. It then enters the Main while loop to monitor the User’s interface. This example VI has on its front panel two button controls, one to run the AndroiDAQ function and the other to stop the VI or program. It also has a string indicator to show the returned serial data posed by the example above. The “Do Function” button control is used to trigger or control a true/false conditional statement in the Main while loop, which contains the sub-VIs or functions used to send the string data sequence to the AndroiDAQ module. When the “Do Function” button is true, or pressed, the functions in the “true” panel are ran, otherwise the blank “false” panel is ran by the main VI while loop. By simply changing the buttons state to “false” using a property node, after the “true” panel has completed its task, you can turn the button to its off or “false” condition so that the main while loop will then iterate and wait for any serial data from the AndroiDAQ module or other command. When there is data on the serial port, it will be displayed in the string indicator on the VI front panel. 

It is always good LabVIEW practice to close all VISA/TCP data type references when you exit your VI or LabVIEW program. Here, the Stop button from the example above is used to control the stopping of the VI. Closing the VISA/TCP references is done when the “stop” button is pressed, as the Main while loop exits and releases the VISA reference and error data types, which are wired out of the main while loop and into the VISA Close VI, as shown in the example image. 

To send the data to AndroiDAQ via the VISA write to port VI certain rules must be followed. The number sent must first be converted to a string and a leading zero is always added in front of this converted number. As stated before, this leading zero wakes up the AndroiDAQ module’s menuloop() sub-routine in the firmware, which will then cause it to wait for the next character sequence or menu item number. The next character sequence triggers which menu item or sub-routine function of the firmware will be ran. A carriage return is appended to this menu item character string to tell AndroiDAQ to use the read data or number to run that menu item function. This is all easily done using LabVIEW’s concatenate string function along with a LabVIEW carriage return constant (see image below). The VISA Write to port function is setup to be in little-endian mode when sending strings as this is the required mode by the Propeller chip.
How VISA Write is used

To use the VISA Read from port VI, one utilizes the Bytes At Port property node to control a conditional panel so that when data bytes are available, these bytes are read and converted into a string which is then wired up to the string indicator on the front panel. If no bytes are available on the port the conditional panel is “false” so nothing is ran in the main while loop. It is always suggested that a small delay be added into any while loop function in LabVIEW as this prevents port polling in a continuous fashion, saving CPU cycles and it makes the VI more memory efficient. This is done by adding a small delay using LabVIEW’s Wait VI. Here I recommend 10 or so milliseconds, unless you need a faster rate for loop iteration.
 
VISA Read Method

These are the basics of utilizing LabVIEW with the AndroiDAQ module. Studying the AndroiDAQ DEMO for LabVIEW application further will aid you in further the understanding of how to do other multiple functions as well, such as setting up all the AndroiDAQ channels as outputs, with each output set to output a different frequency signal, for example. I think you will agree that using LabVIEW and AndroiDAQ together gives you the best of both worlds when interfacing the outside world to computers, as LabVIEW is easy to learn and implement, and AndroiDAQ gives to you the most input and output digital and frequency domain I/O channels offered on the market today. After all, and in my opinion as a maker, one can never have too many I/O pins for your motion control or robotic project; now where did I put that gadget and how would I add a gyro and compass to this all…

No comments :