How to automate measurements with Python: Page 2 of 4

April 19, 2016 //By Fabrizio Guerrieri
How to automate measurements with Python
Fabrizio Guerrieri, Sr. System/Application Engineer at Maxim Integrated, considers ways of automating measurements with Python.

Basic code structure
Below, you can find the first part of the automation script code listing. In Python, comments are preceded by #:

[Download all code from this article as a text file.]

import numpy as np                                          # 1
import pandas as pd                                        # 2
import visa, time                                               # 3

chroma = visa.instrument('GPIB::2')               # 4
daq = visa.instrument('GPIB::9')                     # 5

results = pd.DataFrame()                                 # 6
loads = np.arange(0,20+2,2)                           # 7

for load in loads:                                               # 8
# Measure the current and the voltage
# Save the results

Lines 1 to 3 import libraries that contain methods used later in the code:

    Numpy is a package used for scientific computing. In this example, Numpy is used to generate the array of output-current values.
    Pandas (a library for data manipulation and analysis) creates a very powerful data structure to store the results of our measurements.
    Visa is the PyVISA library that we use to control our instruments.
    Time is a handy library that we need to generate some time delays.

Note that the imported Numpy and Pandas libraries have been renamed to np and pd to keep the code clean. All the libraries mentioned in this article are either already available with your Python distribution, or they can be easily installed from online repositories.

Lines 4 to 5 create the objects that we will use to access the Chroma electronic load and the Keysight DAQ. This is where PyVISA comes into play: all we need to do is to call the instrument method and provide a string to indicate the GPIB address of the instruments on the bus.

Line 6 creates the results dataframe to store the measurement results. A dataframe is a two-dimensional labeled data structure with columns of potentially different data types. Using a dataframe instead of an array will allow us to reference to columns using an easy-to-remember string instead of number, and to mix numbers and text in the data itself.

Line 7 creates an array of real numbers from 0 to 20 with a step of 2. These numbers will represent the values of the output current in amps at which we want to measure VOUT.

Line 8 is used to construct the "for" loop. Note that the syntax is very easy to understand: every time the loop is executed a variable called load will be generated with a value equal to a new element of the loads array. The loop ends when all the elements of the array have been used. It is interesting to highlight how Python uses indentation to define code hierarchy, without relying on any type of parenthesis. This is very useful because it keeps the code clean and legible.

Now that we have the defined our main for loop, we need to talk to the instruments to program the current, then read the voltage, and save the results.

Design category: 

Vous êtes certain ?

Si vous désactivez les cookies, vous ne pouvez plus naviguer sur le site.

Vous allez être rediriger vers Google.