Вы находитесь на странице: 1из 4

INTERPOLATION IN MICROCONTROLLER BASED INSTRUMENTATION AND CONTROL PROJECTS

Assoc. Prof. Dr. Dogan Ibrahim


Department Of Computer Engineering, Near East University, Mersin 10, Turkey dogan@neu.edu.tr

ABSTRACT
Most instrumentation and control projects are nowadays designed using microprocessor or microcontroller based systems. In such applications it is often required to find the value of a function between two measured points. This is known as interpolation and this paper describes some of the important interpolation techniques. Set Point Computer Plant

Fig. 1. Computer control system 1. INTRODUCTION


Digital control is now widely used in almost all industrial and commercial processes. As shown in Figure 1, a digital computer is the main processing element in such applications. The comp uter acts like a digital controller which receives the error signal and provides a correction signal to the plant in order to achieve the required response. Analog to digital converters (A/D) are usually used to convert the analog signals into digital form so that they can be processed by the digital computer. Similarly, digital to analog converter (D/A) circuits convert the computer output into analog form which then drives the plant as required. The processing element used in many digital control applications until the late 1980s was a mini computer (e.g. PDP11 series 1 ), equipped with large amounts of memory, hard disk, and input-output interface circuitry. Such computers were programmed using a high-level language such as the ALGOL 60, PASCAL, or FORTRAN. The cost of the initial system cost and the cost of the maintenance were usually very high and only large organisations could afford to have such systems. With the availability of the low cost microprocessors and microcontrollers, most industrial and commercial processes are now controlled using these low cost programmable devices. Some of the popular microcontrollers used in instrumentation and control applications are the PIC series 2 , and 8051 series 3 . These are usually 8-bit microcontrollers which are equipped with CPU, memory, input-output circuitry, and timer circuitry. Some models incorporate multi-channel A/D and D/A converters, timers, counters etc. In microprocessor and instrumentation applications sensors are used to detect the state of external physical quantities such as light, temperature, force, speed etc. In such applications the mathematical relationship between the physical quantity to be measured and the output signal of the sensor are not know. But the output signal is known at various points of the quantity to be measured. If we need to know the output signal corresponding to a point between the known points then we have to interpolate between the known points in order to calculate the sensor output at the required point. This paper describes the simple linear interpolation tachnique and the more complex Lagrange interpolator4 . Examples of C programming language functions are given which describe how the interpolation can be carried out in real-time systems in practise. Although these functions have been compiled using the PIC C compiler they should be compatible with any C compiler which supports floating point arithmetic.

22

2. LINEAR INTERPOLATION
Interpolation is usually defined as a means to estimate an intermediate value based on two known values. For example, suppose that the voltage-temperature relationship of a temperature sensor has been measured and this relationship is as given in Table 1. Suppose that we want to calculate the voltage output of the sensor when the temperature is 11C. Let us see how this can be done using the linear interpolation technique. where,

f(x) f(a) = m (x a)

(3)

f(b) f(a) m = ------------b a Combining equations (3) and (4), we obtain: bx x-a f(x) = ---------- f(a) + -------- f(b) ba b a

(4)

(5)

Table 1.Voltage-temperature relationship of the temperature sensor


Temperature (C) 2 4 6 8 10 12 14 Voltage (mV) 9 25 49 81 121 169 225

Equation (5) is the linear interpolation e quation and can be used to calculate an intermediate value between two known data points. For the temperature sensor example, we have: a = 10 b = 12 x = 11 f(a) = 121 f(b) = 169 f(x) = ?

Linear interpolation is commonly used in many applications since it is easy to implement and generally gives good estimates. Figure 2 shows the principle of linear interpolation. Assume that f(x) is the function we want to interpolate (in this example, the relationship between the temperature and the output voltage of a sensor) where two points a and b are known on this function. Further assume that the value of the function f(a) at a and f(b) at b are known. The problem is to estimate the value of the function f(x) at a point x between a and b. In linear interpolation a straight line is drawn between the known data points and the equation of this line is derived. The value of the function at any point x on this line is calculated as an estimate. The accuracy of the estimate depends on how close the known data points are and also on what type of function we have. The equation of a straight line with two points (x1, y1) and (x2, y2) is given by the well known formula: y y1 = m (x x1) and, m = (y2 y1) / (x2 x1) (2) (1)

In this example, the point we want to interpolate is 11. The lower and the upper known points are a = 10 and b = 11. The output voltage of the sensor at these points are known to be f(a) = 121mV and f(b) = 169mV. Using equation (5), we can calculate the output voltage at 11C as: 12 11 11 - 10 f(x) = ---------- x 121 + ----------x 169 12 10 12 10 or, f(x) = 145mV olur. In reality, the exact value is f(x) = 144mV.

f(b) f(a)

a Fig. 2. Function f(x) and a straight line

If the two points are (a, f(a)) and (b, f(b)) and the function if f(x), then equation 1 can be written as:

In order to increase the accuracy we can use bigger tables with more points. But this is usually

23

a problem in microcontroller systems where the memory is usually very limited.

we can calculate the output of the sensor at 11C by using the statement: t = interpolate(11.0) in our main C program, where t stores the return value of the function.

2.1 C Function For Linear Interpolator


A C function is given in Figure 3 which shows how the linear interpolation can be carried out in practise on a microcontroller. This function was compiled and tested on the PIC C compiler using a popular 8-bit PIC16F8772 microcontroller.

2. Lagrange Interpolator
We have seen that the linear interpolator can give accurate results even with small number of known data points. This accuracy can be improved by increasing the size of the table and making sure that the data points are close to each other. The closer the data points the more accurate the interpolation will be. In this section we shall be looking at the Lagrange interpolator which is very efficient even with small number of table entries. This interpolator is also well suited to microprocessor and microcontroller applications. Lagrange interpolator fits a polynomial to the data points and then an estimate can easily be made. Lagrange interpolator is based upon the Weierstrasss well known theorem which states that if x0, x1, x2, ., xn are distinct, then for any y0, y1, y2,., yn there exists a unique polynomial of degree n or less, such that: F(xi ) = y i , I = 0,1,2,3,..,n

/*================================ Linear Interpolator Function %%%%%%%%%%%%%%% This function implements the linear interpolation Algorithm for the temperature sensor example given above. x is the input variable to the function. The function table is stored in two arrays called x_values and y_values and it is assumed that x is within the x_values. Change these values for your own application.

Author: Dogan Ibrahim Date: April 2003 ================================= */ float interpolate(float x) { const float x_values[ ] = {2.0,4.0,6.0,8.0,10.0, 12.0,14.0}; const float y_values[ ] = {9.0,25.0,49.0,81.0, 121.0,169.0,225.0}; unsigned char a, b, i; float y, y1, y2, fa, fb; i =0; while(x > x_values[i])i++; a = i; b = i 1; fa = y_values(a); fb = y_values(b); y1 = fa * (b x)/(b a); y2 = fb * ((x a)/(b a); y = y1 + y2; return(y); }

The proff of Lagrange interpolator is not given here and can be found in the literature 4 . A C function is given here which can be used in real-time on microprocessor and microcontroller systems in order to interpolate accurately with a small table. The function is given in Figure 4 and is implemented using the PIC C compiler for the PIC series of microcontrollers. The function receives the x value as a floating point number and then interpolates using the tables x_values and y_values and the result is returned as a floating point number. You should change t he contents of tables x_values and y_values for your own applications.

Fig. 3. Linear interpolator C function

24

/*================================ Lagrange Interpolator Function %%%%%%%%%%%%%%%%% This function implements the Lagrange interpolation algorithm for the temperature sensor example given above. x is the input variable to the function. The function table is stored in two arrays called x_values and y_values and it is assumed that x is within the x_values. n is the size of the table. Change these values for your own application.

much more accurate than the result obtained with linear interpolator (the exact value is 144). One disadvantage of the Lagrange interpolator is that the algorithm could be time consuming when large tables are used.

3. CONCLUSIONS
This paper has deascribed the two well known interpolation techniques: the linear interpolator, and the Lagrange interpolator. Linear interpolator is easy to implement and it is gives very quick results. Lagrange interpolator is more complex, slower, but gives more accurate results. It is suggested that you should use the linear interpolator if the accuracy is not very critical and if the computational speed is more important. On the other hand, the Lagrange interpolator should be used for very accurate results or for the applications where the memory capacity is rather limited.

Author: Dogan Ibrahim Date: April 2003 ================================= */ float lagrange(float x) { const float x_values[ ] = {2.0,4.0,6.0,8.0,10.0, 12.0,14.0}; const float y_values[ ] = {9.0,25.0,49.0,81.0, 121.0,169.0,225.0}; unsigned char n, i, j; float sum, p; n = 7; sum = 0; for(i=0; i<=n; i++) { p = 1.0; for(j=0; j<=n; j++) { if(i != j) p=p * ((x x_values[j]) / (x_values[i]-x_values[j])); } sum = sum + p * y _values[i]; } return(sum); }

REFERENCES
[1] [2] [3] [4] Compaq web site: http://www.compaq.com Microchip web site: http://www.microchip.com atmel web site: http://www.atmel.com Lagrange interpolation web site: http://mpec.sc.mahidol.ac.th/numer/ STEP23.HTM

Fig. 4 Lagrange interpolator C function


we can calculate the output of the sensor at 11C by using the statement: t = lagrange(11.0) in our main C program, where t is the value returned by the function. The Lagrange interpolator in this example gives the output voltage of the sensor as 143.995mV which is

25

Вам также может понравиться