01225 650150 | 0800 772 0795 sales@austinconsultants.co.uk

Introduction

Python has recently gathered much interest as a language of choice for data analysis. Some of the key points driving Python’s adoption are:

  • Open Source – free to install.
  • Community and industry support.
  • Easy to learn.

Availability of popular data analysis packages, such as numpy, pandas, and powerful machine learning libraries, such as scikit-learn and Tensorflow.

At the recent NIDAys 2018 London, our team demonstrated a prognostics application running Python directly on a cRIO to monitor the performance of a domestic heating pump system. This application utilises the reliability and robustness of the National Instruments hardware platform combined with the flexibility of Python.

Tutorial

This quick tutorial will demonstrate how easy it is to set up a Python development environment on a National Instruments device.

 

Accessing CompactRIO

First, we want to get access to the terminal, which is achieved by enabling Secure Shell access (SSH) on the CompactRIO device:

  1. Power on and connect your device to your network.
  2. Open NI MAX and find your device under Remote Systems.
  3. Inside Startup Settings window, check the box Enable Secure Shell Server (sshd).
  4. Save your changes and the device will reboot.
  5. Use an ssh client of your choice to connect to the device.

Full information on how to access the Secure Shell on NI devices is provided on the NI Knowledge Base at: https://knowledge.ni.com/KnowledgeArticleDetails%3Fid%3DkA00Z000000P8bQSAS+

 

Installing Anaconda Distribution

Once inside the terminal, we are now in the position to install a Python package manager.  The two most popular options are pip and conda.

The easiest distribution to get started with is Anaconda Inc.’s Miniconda: https://conda.io/miniconda.html

We recommend starting with miniconda distribution because a) it does not require much disk space upon the first installation, and b) provides access to pre-built binary packages, overcoming the requirement of setting up a (cross-) compilation toolchain.

For the purpose of this tutorial, we will assume that a) the CompactRIO in question has access to the internet in order to download the installer and access online package repositories, and b) the user is logged in as admin.

Before making any long-term commitments, the developer should decide which Python version they want to use. It is a rather involved topic, which we consider outside the scope of this tutorial. However, since Python 2.X is gradually being phased out, we would recommend using Python 3.X.

The other bit of information required to set up the Python environment is to select the appropriate CPU architecture. There are two major options: 32-bit and 64-bit. One can find out what type of CPU they are using by typing uname -m in the terminal. Since CompactRIO is powered by a 64-bit CPU, we want a matching Miniconda installer. We now have all the information necessary to proceed with the installation:

  1. Make sure your CompactRIO can access the internet.
  2. To download the installer, from your terminal type: wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
  3. Once the download is complete, you can run the installer by: sh Miniconda3-latest-Linux-x86_64.sh -b
  4. The installer prepends a shell variable inside a file located at /home/admin/.bashrc (assuming you are logged in as admin), so, in order to activate the changes made by the installer, run: source /home/admin/.bashrc
  5. Finally, we can verify that the installation process worked: running python –version should output Python 3.6.5 :: Anaconda, Inc.

More information on installing Anaconda is available on their website: https://conda.io/docs/user-guide/install/index.html

 

Interfacing Python with CompactRIO: NI-DAQmx Python Library

Now that we have Python interpreter and package manager installed on the system, we can start interfacing Python code with NI hardware. NI provides a python library for this very purpose: https://nidaqmx-python.readthedocs.io/en/

To obtain the library, in your terminal run: pip install nidaqmx.

Once the package is installed, we can confirm that it is now working by checking if running python -c “import nidaqmx.system; print(nidaqmx.system.System.local().driver_version)”, which should print out driver information, similar to: DriverVersion(major_version=18, minor_version=1, update_version=0).

Having successfully installed nidaqmx, we can start using Python to program the CompactRIO. For example, to read data from Dev1/ai0 channel (for more information on channel naming, see: https://knowledge.ni.com/KnowledgeArticleDetails?id=kA00Z000000P8qaSAC&l=en-GB), we can start by writing a script using your text editor of choice (there are multiple alternatives available via opkg package manager from NI repositories, including nano, vim and emacs; for more information see http://www.ni.com/white-paper/14626/en/#toc4).

For the purpose of this tutorial, let us create a file named sample_ai0.py.

The first thing we need to do is to tell Python interpreter that you want to import the nidaqmx module, which is done by: import nidaqmx

Next, we create a Task context and add a voltage channel to it:  with nidaqmx.Task() as task:  task.ai_channels.add_ai_voltage_chan(“Dev1/ai0”)

We will query the channel ten times to print ai0 channel samples:   for i range(10): print(task.read(number_of_samples_per_channel=1))

The above code snippet calls task.read() ten times, asking all channels, which, in our case is one, for one sample.

Finally, to put everything together, the contents of sample_ai0.py should be: import nidaqmx with nidaqmx.Task() as task:        task.ai_channels.add_ai_voltage_chan(“Dev1/ai0”)    for i range(10):        print(task.read(number_of_samples_per_channel=1))

We can run the script by typing python sample_ai0.py, which will output:

[0.0003243534345]

[0.0003243534345]

This concludes our getting-started tutorial on setting up a Python development environment on a ComapactRIO. We hope you found it interesing.