Designing Your Model: Model-Code Correspondence : Lookup Table Implementation

Lookup Table Implementation

Rational Statemate allows the definition of lookup tables to represent the type of non-linear Y=F(X) functions that are so common in the world of microcontrollers. The data for a lookup table can be defined manually in Rational Statemate, or imported from any ASCII data file. You can elect to have linear interpolation between defined points, or a histogram-like mode. Upper and lower bounds can be defined, as can the search order to use (low-to-high, high-to-low).

In the sample lookup table below, the input is defined as Integer, and the return value of the function is defined as Real.

 

Using the settings linear interpolation, high-to-low search order, lower bound = 0, upper bound = 4, the following code will be generated:

double LOOKUP1(int IN1)
{

/*

Interpolation Function:

if(In < X2 && In >= X1)

Out = (Y2-Y1)/(X2-X1)*(In-X1)+Y1

*/

double LOOKUP1_retval;

if(IN1 < 1)

LOOKUP1_retval = (0);

else if(IN1 >= 1000)

LOOKUP1_retval = (4);

else if(IN1 >= 100)

LOOKUP1_retval = (4 - 3)/((double)1000 - 100)*(IN1 -

100) + 3;

else if(IN1 >= 10)

LOOKUP1_retval = (3 - 2)/((double)100 - 10)*(IN1 -

10) + 2;

else if(IN1 >= 1)

LOOKUP1_retval = (2 - 1)/((double)10 - 1)*(IN1 - 1)

+ 1;

return(LOOKUP1_retval);

}