Real World Instrumentation with Python
Download
Introduction
This is a book about automated instrumentation, and the automated control systems
used with automated instrumentation. We will look at how to use the Python programming
language to quickly and easily implement automated instrumentation and
control systems.
Automated instrumentation can be found in a wide variety of settings, ranging from
research laboratories to industrial plants. As soon as people realized that collecting data
over time was a useful endeavor, they also realized that they needed some way to capture
and record the data. Of course, one could sit with a clock and a pad of paper, staring
at thermometers, dials, and gauges, and write down numbers or other information
every few minutes or so, but that gets tedious rather quickly. It’s much easier—and
more reliable—if the process can be automated. Fortunately, technology has advanced
significantly since the days of handwritten logbooks and clockwork-driven strip chart
recorders.
Nowadays, one can purchase inexpensive instrumentation for a wide variety of physical
phenomena and use a computer to capture the data. Once a computer is connected to
instrumentation, the possibilities for data collection, analysis, and control begin to
expand in all directions, with the only real limitations being the ability to implement
the necessary software and the implementer’s creativity.
The primary objective of this book is to show you how to create software that you can
use to get a capable and user-friendly instrumentation or control application up and
running with a minimum of hassle. To this end, we will work through the steps necessary
to create applications that incorporate low-level interfaces to the real world via
various types of input/output hardware. We will also examine some proven methods
for creating programs that are robust and reliable. Special attention will be paid to
designing the algorithms necessary to acquire and process the data. Finally, we will see
how to display the results to a user and accept command inputs. It is my desire that
you will find ideas here that you might take away and creatively apply to meet your
own needs in a wide variety of settings.
Who Is This Book For?
This is a hands-on text intended for people who want or need to implement instrumentation
systems, also known as data acquisition and control systems. You might be
a researcher, a software developer, a student, a project lead, an engineer, or a hobbyist.
The application might be an automated electronics test system, an analysis process in
a laboratory, or some other type of automated instrumentation.
One of the objectives with the software in this book is that it be as platform-independent
as possible. I am going to assume that you are comfortable with at least the Windows
platform, and Windows XP in particular. With Linux I’ll be referring to the Ubuntu
distribution, but the discussion should apply to any recent Linux distribution and I will
assume that you know how to use either the csh or bash command-line shells.
Since this is a book about interfacing to the real world via physical hardware, some
electronics are involved, but I am not going to assume that you have an extensive background
in electrical engineering. Chapter 2 contains an overview of the basics of electronics
theory as it relates to instrumentation, for those who might benefit from it. It
turns out that it really doesn’t take a deep level of electronics knowledge to successfully
interface a computer with the physical world. But, as with anything else involving
technology, it never hurts to know as much as possible, just on the off chance that
things don’t quite work out as expected the first time.
Regardless of the type of work you do, or where you do it, the main thing I am assuming
that we have in common is a need to capture some data, and perhaps to generate control
signals, and to do so through some kind of computer interface. Most importantly, we
need the instrumentation and control software we create to be accurate, reliable, and
relatively painless to implement
How This Book Is Organized
This book is organized into 14 chapters and 2 appendixes. The first 12 chapters set the
stage for the implementation examples described in Chapter 14. Chapters 1 through
6 introduce basic concepts that the advanced reader may elect to skip over. Here’s a
closer look at what you’ll find in each chapter:
Chapter 1, Introduction to Instrumentation
Chapter 1 provides an overview of what instrumentation is, how control systems
work, and how these concepts are used in the real world. The examples covered
include automatic outdoor lights, test instrumentation in an electronics engineering
environment, control of a thermal chamber in a laboratory, and batch chemical
processing.
Chapter 2, Essential Electronics
Because this is a hands-on book, we will need to know something about the physical
hardware we want to interface to and have at least a general idea of how it
works. This chapter starts off with an introduction to the basic concepts of electricity
and electronics. It then explores the functional building blocks for data acquisition
and control, including discrete digital interfaces, analog interfaces, and
counters and timers. Lastly, it reviews the basic concepts behind serial and parallel
interfaces. If you are already familiar with electric circuit theory and devices, you
could skip this chapter. However, I would recommend that you still at least skim
through the material, on the off chance that there might be something unique here
that you can make use of later.
Chapter 3, The Python Programming Language
Although this book is not a tutorial on Python, this chapter provides an introduction
to the core concepts of Python and summarizes the basics of the language. The
primary emphasis is on the features of Python that will be used frequently in this book. This chapter also provides a brief overview of the tools available to make life
easier for the person doing the programming, and where to go about finding them.
Chapter 4, The C Programming Language
Here, the C programming language is introduced in a high-level overview. The
objective is to provide enough information to enable you to understand the examples
in this book, without delving into the arcane details. Fortunately, C is a relatively
simple language, and the information in this chapter should be sufficient to
get you started on creating your own extensions for Python.
Chapter 5, Python Extensions
This chapter describes how a Python extension is created, and what extensions are
typically used for. Examples are provided, both in this chapter and in later chapters,
for you to use as templates for your own efforts.
Chapter 6, Hardware: Tools and Supplies
Although is it possible that one could implement an instrumentation system and
never touch a soldering iron, there is a high probability that some screwdrivers,
wire cutters, and a digital multimeter (DMM) will come in handy. In this chapter
I provide a list of what I would consider to be a basic toolkit for doing instrumentation
work. It isn’t much and could all easily fit in a small box on a shelf somewhere.
However, there could very well come a time when you really need to see
what’s going on in your system. To this end, I’ve included a discussion of the two
pieces of test equipment that can help you eliminate the guesswork and quickly
get to the root of an interface or control problem: the oscilloscope and the logic
analyzer. This chapter also covers what types of instruments are available and provides
some suggestions for deciding between buying new equipment or picking up
something used.
Chapter 7, Physical Interfaces
Chapter 7 examines the types of interfaces one is most likely to encounter when
attempting to interface Python to data acquisition or control instrumentation.
RS-232 and RS-485, the two most commonly encountered types of serial interfaces,
are examined from an instrument interface perspective. This chapter also covers
the basics of USB and GPIB/IEEE-488 interfaces, along with a discussion of where
one might expect to encounter them. Finally, we turn our attention to I/O hardware
designed to be plugged into the bus of a PC, typically PCI-type circuit boards, and
what one can typically expect in terms of API support from the hardware vendor.
Chapter 8, Getting Started
This chapter contains a description of a proven approach to software development.
It is included here because, when implementing an instrument system in any language,
it is essential to plan and define what is to be implemented, and then to test
the result against the expectations captured in a set of requirements. By extending
the reach of Python into the real world, we open the door for the uncertainties and
vagueness of the real world to wander back in and impact—sometimes severely—
the instrumentation software.
Chapter 9, Control System Concepts
A book on real-world data acquisition and control would be incomplete without
a discussion of control systems and the theory behind them. Chapter 9 expands
on the concepts introduced in Chapter 1 with detailed examinations of common
control system concepts and models, including topics such as feedback, “bangbang”
controllers, and Proportional-Integral-Derivative (PID) controls. It also provides
an introduction to basic control system analysis and provides some guidelines
for choosing an appropriate model. Lastly, we’ll look at how the mathematics of
control systems translates into actual Python code.
Chapter 10, Building and Using Simulators
Chapter 10 examines simulators and how they can be leveraged to speed up the
development process, provide a safe environment in which to test out ideas, and
provide some invaluable (and otherwise unattainable) insights into the behavior
of not only the instrumentation software, but also the device or system being simulated.
Whether because the instrumentation hardware just isn’t available yet or
because the target system is too valuable to risk damaging, a simulation can be a
quick and easy way to get the software running, test it, and have a high degree of
confidence that it will work correctly in the real world.
Chapter 11, Instrumentation Data I/O
In this chapter we’ll look at how to use the interfaces that were introduced in
Chapter 7 to move data between the real world and your applications. We’ll start
with a discussion of interface formats and protocols in order to define the basic
concepts we will need for the upcoming software examples, and then we’ll take a
quick tour of some packages that are available for interface support in Python with
the pySerial, pyParallel, and PyVISA packages. Lastly, I’ll show you some techniques
to read and write instrumentation data. We’ll take a look at blocking versus
nonblocking I/O, asynchronous input and output events, and how to manage potential
data I/O errors to help make your applications more robust.
Chapter 12, Reading and Writing Data Files
Chapter 12 examines some of the implementation considerations and techniques
for saving instrumentation data in a variety of file formats, from plain ASCII and
CSV files to binary files and databases. We’ll also examine Python’s configuration
data file capabilities, and see how easy it is to store and retrieve configuration
parameters using Python’s library methods.
Chapter 13, User Interfaces
Unless an application is deeply embedded or specifically designed to run as a background
process, it will probably need some type of user interface. Chapter 13 examines
what one can do with just the command line and the curses screen control
package for Python, and how to use an ANSI-capable terminal emulator program
to display data and accept input. The chapter wraps up with a look at the TkInter
GUI toolkit provided with the standard Python distribution, and also provides an
overview of the wxPython GUI package
Chapter 14, Real World Examples
In Chapter 14 we look at several different types of devices used for data acquisition
and control applications. This chapter starts with an example of capturing the
continuous data output from a digital multimeter. We then examine a common
type of data acquisition device that uses a serial interface for command and data
exchanges. Lastly, we wrap up with a detailed look at a data I/O device with a USB
interface and its associated API DLL provided by the vendor. The selected devices
illustrate key concepts shared by almost all instrumentation components, and the
examples draw on earlier chapters to show how the theory is put into practice.