How to use NI ELVIS III with Python

Publish Date: Jun 20, 2018 | 1 Ratings | 5.00 out of 5 | Print | Submit your review

Overview

In this document, we will walk you through the setup, transfer of files, and the use of a Python program on NI ELVIS III. The NI ELVIS III solution for project-based learning can be programmed with python to help students or educators who are familiar with Python syntax to rapidly acquire measurements using common SSH clients. Attached there are a total of 9 examples included which illustrate the use of the “NI ELVIS III Academic IO helper library”, various IO types and the ability to record data in CSV files. The purpose of these examples is to introduce you to how to setup NI ELVIS to be programmed with Python, and act as a starting point for anyone to leverage Python as needed for acquisition.

Table of Contents

  1. Software Setup
  2. Transferring the Python Examples to the Target
  3. Running and Exploring the Python Examples
  4. Wrapper Functions Documentation
  5. Summary

1. Software Setup

In this section we will install the software needed to communicate to the NI ELVIS III and install the required packages to use the Python FPGA API.

  1. Install the NI Measurement Live Support Files: here
  2. Connect the NI ELVIS III to the Internet using the Ethernet Port or Wifi so that the Python libraries can be installed from the Internet. It is possible to bridge the USB connection to the Internet on your computer but this is not covered in this tutorial and is not recommended.
  3. Enable the Secure Shell Server on the NI ELVIS III through the NI Measurement and Automation Explorer
  • Open NI Measurement and Automation Explorer
  • Expand Remote Devices
  • Locate the NI ELVIS III and select it
  • Navigate to the "Systems Settings" tab at the bottom of the window if not already there
  • Click on the Enable the Secure Shell Server box after scrolling down the window to System Settings

  • Click on the Save button to save the configuration to the NI ELVIS III. When prompted to restart click Yes.

 

4. Confirm System Time is accurate to the current local date and time, without this step you will be unable to install the python packages from the Internet.

  • Open NI Measurement and Automation Explorer
  • Expand Remote Devices
  • Locate the NI ELVIS III and select it
  • Navigate to the "Time Settings" tab located at the bottom of the window
  • Set the Time, Date, and Time Zone (the date is set by clicking on the calendar)
  • Click "Save" located at the top of the window to apply the changes to the target.

5. Install and open your favorite SSH client. If you do not have one, we recommend PuTTY: https://the.earth.li/~sgtatham/putty/latest/w32/putty.exe

  • Configure PuTTY or another client as follows:

 

Host Name: <The IP Address found at the top of the NI ELVIS III on the Display.>

Port: 22

Connection Type: SSH

6. Click the Open button. Note: Sometimes the SSH client will alert a Potential Security Breach due to a key mismatch. Simply click Yes to proceed the change it detected is expected.

Once the connection opens Log In as:

Username: admin

Password: <blank> (Just hit enter, there is no password by default)

7. Run the following Commands

opkg update

opkg install python-pip

pip install nifpga

After the pip install it will indicate that you should consider updating to a newer version. This is not necessary and was not done on the test hardware when developing this example.

The NI ELVIS III is now setup to run Python applications and communicate to the FPGA using the NI FPGA API.

    

Back to Top

2. Transferring the Python Examples to the Target

In this section we will transfer an FPGA Bitfile and the Python Example program to the NI ELVIS III. The directions are performed using the free FTP Client FileZilla. You are free to use any application you are comfortable with.

  1. Download and Install FileZilla: https://filezilla-project.org/download.php
  2. Log Into the NI ELVIS III in FileZilla:
  3. Configuration:

Host: sftp://<IP Address of NI ELVIS III>

Username: admin

Password: <blank>

4.  Press “Quickconnect”

5.  Navigate to the /home/admin directory

6.  Download the “ni_elvis_iii_py_examples.zip” attached to this document here and extract the archive.

7.  Drag the Bitfile 'NI ELVIS III Bitfile.lvbitx' from the unzipped folder into the /home/admin directory in FileZilla.

8.  Drag all of the Python files from the unzipped folder into the /home/admin directory in FileZilla.

 

Back to Top

3. Running and Exploring the Python Examples

In this section we will explore the Data Logger example by first running it and then exploring each section of the code with a focus on understanding and reuse. This can be considered a template for use by students or educators for future more complex programs.

 

Running the Example

After transferring the files according to the directions above, open the PuTTY Client (or similar client) used in the earlier Software Setup stage, connect to the NI ELVIS III as detailed above, and Enter the following commands:

cd /home/admin

python Python\ Example\ -\ Data\ Logger.py (using the Tab key you can auto complete the name of the file)

To stop the Program Press Ctrl+C

    

Understanding the Data Logger Example

We start the program by importing time and the NIELVISIIIAcademicIO. In this program time is used for our acquisition loops and the wrappers for the FPGA IO are found in the NIELVISIIIAcademicIO. 

 

As we log data it will be written to a csv file.

#

# Record data from myRIO Analog input and store to file

import time

importNIELVISIIIAcademicIO

 

import csv

 

from nifpga import Session

 

The Session command specifies the Bitfile and the RIO target. For customization simply change the Bitfile name to the name of your LabVIEW FPGA Bitfile:

with Session("NI ELVIS III Bitfile.lvbitx", "RIO0") as session:

 

The CSV File is then specified:

# Open CSV File for data logging.

csvfile = open('acquisitionOutput.csv', 'wb+')

file = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)

 

The AnalogInput function takes the session and then sets of

# Initialize a single Analog Input.

inputA0 = NIELVISIIIAcademicIO.AnalogInput(session, {'bank': 'A', 'channel': '0', 'minvalue': -10, 'maxvalue': 10, 'differential': False})

 

The Loop rate is changed by modifying the “rate” variable. It is specified in Hz. The loop itself waits in 0.001 second increments until the loop time has been met. Reading the Analog Input values is accomplished by calling the read() function.

# Set rate to change acquisition rate

rate = 10.0

loop_time = 1.0 / rate

next_time = time.time() + loop_time

try:

    while True:

        while time.time() < next_time:

            time.sleep(0.001)

        # Read value from Analog Input and record to file.

        value = inputA0.read()

        file.writerow([next_time, value])

 

        next_time += loop_time

        print(value)

except KeyboardInterrupt:

    # Use Ctrl-C to exit the script

    Pass

 

This brief overview of the Data Logger code should provide context on how specific NI ELVIS III syntax can be called with python in order to run a simple program.

 

Back to Top

4. Wrapper Functions Documentation

A number of wrappers functions are available to support quick development with python. To help explain how each of the following function works, there is an attached example contained in the "ni_elvis_iii_py_examples1.0.zip" file.

 

The functions are provided as is and while each example has been tested to work functionality is not guaranteed and it should not be used in a production environment.

 

Please Note: The NI ELVIS III Academic IO.py file contains functions other than those listed below. These functions are used inside of the classes listed below, and should not be used. Also IRQ (Interrupt) functionality is also included in the example library but no examples are provided at this time. The IRQ functions accurately reflects the process that should be used but are not utilized with an example application.

 

Analog Input

Name Description
_init_(self, session, *channels) This init can be used to initialize multiple channels at once by repeating the channels element. Channels is in the form:
{'bank':'A', 'channel': '0', 'minvalue': -10,'maxvalue': 10, 'differential': False},…
read() Reads from all channels initialized and returns a list of data containing the Bank, Channel, and Value.

 

Analog Output

Name Description
_init_(session, bank=’A’, channel=’0’) Each Analog Output Channel is setup separately by specifying which Bank and which Channel.
writesingle(write_value) Simply specify the value to write out and the Analog Output will output it within the range available on the Hardware.

 

Digital I/O

Name Description
_init_(session, bank=’A’) DIO is opened by Bank and then operated on individually. By default all DIO are set to Read. In order to Write from specific DIO you must call the dio_to_write() function.
usr_button_read() Reads the User Button. This can be done with any bank specified.
dio_to_write(*dio) Setup the DIO that you wish to write from. List the DIO numbers separated by commas. This sets their direction on the FPGA only.
dio_read(*dio) Read from the DIO indicated. List the DIO numbers separated by commas.
dio_write(val, *dio) Set the DIO indicated to the value specified. Use True or False.

 

Encoder

Name Description
_init_(session, bank=’A’, channel=’0’, encoder_mode=’quadrature’) Open an encoder by specifying the Bank and Channel. If you do not want quadrature interpretation simply set encoder_mode to anything other then quadrature.
read(reset_counter) Read will return the Encoder value and can be used to reset the counter if needed.

 

PWM

Name Description
_init_(session, bank=’A’, channel=’0’) The PWM is setup by specifying the Bank and Channel.
set_pwm(frequency, duty_cycle) Sets the output of the PWM to the specified Frequency and Duty Cycle.

 

LEDs

Name Description
_init_(session) Initializes a reference to communicate to the Onboard LEDs.
set(led=’led0’, valuetoset=True) Specify led0 through led3 and the value to set it to.

 

I2C

Name Description
_init_(session, i2c_bank=’A’, mode=’STANDARD’) Initializes a reference to communicate to the Onboard LEDs.
i2c_write(slave_address, bytes_to_write[], keep_bus_busy=True, timeout_ms=1000) Writes to a specified slave address the bytes specified and can keep the bus busy if needed.
i2c_read(slave_address, num_bytes_to_read, keep_bus_busy=False, timeout_ms=1000) Reads a specified number of bytes from the slave address.

 

SPI

Name

Description

_init_(session, frequency, spi_bank=’A’, clock_phase=’Leading’, clock_polarity=’Low’, data_direction=’Most Significant Bit First’, frame_length=8)

Initializes the SPI bus on the specified bank. To change any value with a string specified simply change the string as it is only looking for the listed value and the provided string is for convenience.

read(session, num_frames_to_read)

Specify the number of frames to read and the function will return the data from the SPI Bus.

writeread(session, bytes_to_write[])

This function will write the specified bytes onto the SPI Bus and return an equal number of bytes back.

 

Understanding the Wrapper Functions

It is recommended that each example in ni_elvis_iii_py_examples1.0.zip be opened and run to understand how to modify code to effectively use the wrapper functions.

 

Back to Top

5. Summary

With NI ELVIS III students and educators have access to a vast amount of I/O, the power of an FPGA and the opportunity to develop complex, real-world projects. By adding the functionality of Python programmability additional choice can be provided to rapidly develop applications that acquire and analyze measurements in native scripting language.

 

Back to Top

Bookmark & Share


Downloads


Ratings

Rate this document

Answered Your Question?
Yes No

Submit