DAQ and PID Control in C# - Telemark University Collegehome.hit.no/~hansha/documents/lab/Lab...
Transcript of DAQ and PID Control in C# - Telemark University Collegehome.hit.no/~hansha/documents/lab/Lab...
Telemark University College
Department of Electrical Engineering, Information Technology and Cybernetics
Faculty of Technology, Postboks 203, Kjølnes ring 56, N-3901 Porsgrunn, Norway. Tel: +47 35 57 50 00 Fax: +47 35 57 54 01
DAQ and PID Control in C#
HANS-PETTER HALVORSEN, 2012.08.20
ii
Preface
C# is a powerful programming language, but has few built-in features for measurement and control
applications. Measurement Studio is an add-on to Visual Studio which makes it easier to create such
applications. With Measurement Studio we can implement Data Acquisition and a graphical HMI.
In this assignment we will create an application that communicates with the USB-6008 DAQ device.
We will also create a discrete PI controller which will be used to control either the Air Heater or the
Level Tank. Finding proper PI parameters and make sure they work properly is part of the task. We
will also create a discrete model of the Air Heater/Level Tank. We will also create HMI with Process
mimic and Plots.
Go to the following website for more background information about the Lab Work:
→ http://home.hit.no/~hansha/?lab=csharp_pid
iii
Table of Contents
Preface ......................................................................................................................................................ii
Table of Contents .................................................................................................................................... iii
1 Introduction .................................................................................................................................... 1
1.1 Visual Studio 2010 ................................................................................................................... 1
1.2 Measurement Studio 2010 ...................................................................................................... 1
1.3 System ..................................................................................................................................... 1
2 DAQ in C# ........................................................................................................................................ 4
Task 1: My First DAQ App ............................................................................................................ 4
3 Control Application ......................................................................................................................... 6
Task 2: Control Application .......................................................................................................... 6
4 Discretization .................................................................................................................................. 8
Task 3: Discretization ................................................................................................................... 8
1
1 Introduction
1.1 Visual Studio 2010
Microsoft Visual Studio is an integrated development environment (IDE) from Microsoft. It can be
used to develop console and graphical user interface applications along with Windows Forms
applications, web sites, web applications, and web services in both native code together with
managed code for all platforms supported by Microsoft Windows, Windows Phone, Windows CE,
.NET Framework, .NET Compact Framework and Microsoft Silverlight.
The latest version of Visual Studio is Visual Studio 2010.
1.2 Measurement Studio 2010
C# is a powerful programming language, but has few built-in features for measurement and control
applications. Measurement Studio is an add-on to Visual Studio which makes it easier to create such
applications. With Measurement Studio we can implement Data Acquisition and a graphical HMI.
If we use Visual Studio 2010, we need Measurement Studio 2010
1.3 System
Select one of the following processes:
Level Measurement in a Level Tank Process
Temperature Measurement in an Air Heater Process
Level Tank Process:
A figure of the Level Tank Process:
2 Introduction
Lab Work: Data Acquisition and PID Control in C#
A proper mathematical model of the water tank is as follows:
or
[ ]
Where:
[cm] is the level in the water tank
[V] is the pump control signal to the pump
[cm2] is the cross-sectional area in the tank
[(cm3/s)/V] is the pump gain
[cm3/s] is the outflow through the valve (this outflow can be modeled more accurately
taking into account the valve characteristic expressing the relation between pressure drop
across the valve and the flow through the valve).
For more details about the process, see the Lab Equipment document available from:
http://home.hit.no/~hansha/.
Air Heater Process:
A figure of the Air Heater Process:
3 Introduction
Lab Work: Data Acquisition and PID Control in C#
A simple mathematical model of the process:
{ [ ]}
Where:
is the temperature in the air tube
[ ] is the control signal to the heater.
[ ] is the time-constant.
[ ] is the heater gain.
[ ] is the time-delay representing air transportation and sluggishness in the heater.
[ ] is the environmental (room) temperature. It is the temperature in the outlet air of
the air tube when the control signal to the heater has been set to zero for relatively long
time (some minutes).
For more details about the process, see the Lab Equipment document available from:
http://home.hit.no/~hansha/.
4
2 DAQ in C#
In this task we will test the USB-6008 DAQ device and implement functions for Analog In and Analog
Out in Visual Studio and C#.
Connect your DAQ device and use MAX (Measurement and Automation Explorer) to make sure it
works properly.
Task 1: My First DAQ App
In this task we will learn how to communicate with the USB-6008 DAQ unit from C#.
Create the following application in Visual Studio 2010 and C#:
We start by connecting the Analog In and Analog Out wires together (a so called Loopback test).
When we click the “Write Data” button, the value entered in the text box “Analog Out” will be
written to the DAQ device. If we have connected the Analog In and Analog Out wires together we will
read the same value in the “Analog In” textbox when we push the “Get Data” button.
5 DAQ in C#
Lab Work: Data Acquisition and PID Control in C#
When you have managed to create the simple DAQ application above, you should extend your
application with the following features:
Robustness:
Make sure the program is robust to wrong user input. In the “Analog Out” box only values between 0
and 5 should be allowed. You should do this check both in your User Interface and before you write
the value to the DAQ device.
Scaling:
The voltage value you read from the DAQ device should be scaled to an engineering unit.
Level Tank:
Air Heater:
[End of Task]
6
3 Control Application
Now we have learned how to communicate with a DAQ device in C#, next will be to create a simple
Control Application that controls the Level in the Level Tank or the Temperature in the Air Heater.
Task 2: Control Application
Create an application that manually controls your process (Level Tank/Air Heater).
You can, e.g., use a Slide Control as your manual controller. You may use some of the controllers that
are installed with Measurement Studio to mimic your process, e.g., a Tank Control or a Thermometer
Control.
In the developing phase you can connect the Analog In and Analog Out wires together (a so called
Loopback wiring), in that way you don’t need to use the real process. When you have finished the
development and tested that the program works as expected, you can connect it to the real process
and test it.
Level Tank:
If you are using the Level Tank, the application could look something like this:
Air Heater:
If you are using the Air Heater, the application could look something like this:
7 DAQ in C#
Lab Work: Data Acquisition and PID Control in C#
Extend you application where you plot the Level (Level Tank) or the Temperature (Air Heater).
You may want to use the “WaveformGraph” Control in the Measurement Studio palette.
Example:
[End of Task]
8
4 Discretization
In this task we will create discrete versions of the process (Air Heater or Level Tank) and a discrete
PI(D) controller.
Task 3: Discretization
In this task we will create our own discrete PI controller and implement in C#.
In addition we should create a discrete Low-pass filter. It should also be possible to use a simulated
model instead of the real process, so you also need to make a discrete model of your system.
Extend you existing program so you can switch between manual control or PI controller.
The User Interface could look something like this:
(The User Interface is just an example; feel free to create your own special user Interface)
Test your application on the real process. Find proper PI parameters. Make sure the PI controller
works well on both the real process and the simulator. Make a change in the reference and see if the
controller can do the job. You should also try to make a change in the disturbance, e.g. manually
adjust if you use the water Tank.
9 Discretization
Lab Work: Data Acquisition and PID Control in C#
Show the results with and without the low-pass filter.
[End of Task]
Telemark University College
Faculty of Technology
Kjølnes Ring 56
N-3918 Porsgrunn, Norway
www.hit.no
Hans-Petter Halvorsen, M.Sc.
Telemark University College
Department of Electrical Engineering, Information Technology and Cybernetics
E-mail: [email protected]
Blog: http://home.hit.no/~hansha/