tableLookup.h File Reference

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define EXTERN   extern

Functions

EXTERN unsigned short lookupTwoDTableUS (twoDTableUS *, unsigned short) TEXT
 Two D table read function.
EXTERN unsigned short lookupPagedMainTableCellValue (mainTable *, unsigned short, unsigned short, unsigned char) TEXT
 Main table read function.
EXTERN unsigned short setPagedMainTableCellValue (unsigned char, mainTable *, unsigned short, unsigned short, unsigned short) TEXT
 Set a main table cell value.
EXTERN unsigned short setPagedMainTableRPMValue (unsigned char, mainTable *, unsigned short, unsigned short) TEXT
 Set an RPM axis value.
EXTERN unsigned short setPagedMainTableLoadValue (unsigned char, mainTable *, unsigned short, unsigned short) TEXT
 Set a load axis value.
EXTERN unsigned short setPagedTwoDTableCellValue (unsigned char, twoDTableUS *, unsigned short, unsigned short) TEXT
 Set a two D table cell value.
EXTERN unsigned short setPagedTwoDTableAxisValue (unsigned char, twoDTableUS *, unsigned short, unsigned short) TEXT
 Set a two D axis value.
EXTERN unsigned short validateMainTable (mainTable *) TEXT
 Validate a main table.
EXTERN unsigned short validateTwoDTable (twoDTableUS *) TEXT
 Validate a two D table.


Define Documentation

#define EXTERN   extern

Definition at line 44 of file tableLookup.h.


Function Documentation

EXTERN unsigned short lookupPagedMainTableCellValue ( mainTable Table,
unsigned short  realRPM,
unsigned short  realLoad,
unsigned char  RAMPage 
)

Main table read function.

Looks up a value from a main table using interpolation.

The process :

Take a table with two movable axis sets and two axis lengths, loop to find which pairs of axis values and indexs we are between, interpolate two pairs down to two values, interpolate two values down to one value.

Table size :

To reduce the table size from 19x24 to something smaller, simply reduce the RPMLength and LoadLength fields to lower values. Increasing the size of either axis is not currently possible.

Values outside the table :

Given that the axis lists are in order, a data point outside the table will give the value adjacent to it, and one outside one of the four corners will give the corner value. This is a clean and reasonable behaviour in my opinion.

Reminder : X/RPM is horizontal, Y/Load is vertical

Warning:
This function relies on the axis values being a sorted list from low to high. If this is not the case behaviour is undefined and could include memory corruption and engine damage.
Author:
Fred Cooke
Parameters:
Table is a pointer to the table to read from.
realRPM is the current RPM for which a table value is required.
realLoad is the current load for which a table value is required.
RAMPage is the RAM page that the table is stored in.
Returns:
The interpolated value for the location specified.

Definition at line 112 of file tableLookup.c.

References mainTable::Load, mainTable::LoadLength, RPAGE, mainTable::RPM, mainTable::RPMLength, and mainTable::Table.

Referenced by generateDerivedVars().

00112                                                                                                                                       {
00113 
00114     /* Save the RPAGE value for restoration and switch pages. */
00115     unsigned char oldRPage = RPAGE;
00116     RPAGE = RAMPage;
00117 
00118     /* Find the bounding axis values and indices for RPM */
00119     unsigned char lowRPMIndex = 0;
00120     unsigned char highRPMIndex = Table->RPMLength - 1;
00121     /* If never set in the loop, low value will equal high value and will be on the edge of the map */
00122     unsigned short lowRPMValue = Table->RPM[0];
00123     unsigned short highRPMValue = Table->RPM[Table->RPMLength -1];
00124 
00125     unsigned char RPMIndex;
00126     for(RPMIndex=0;RPMIndex<Table->RPMLength;RPMIndex++){
00127         if(Table->RPM[RPMIndex] < realRPM){
00128             lowRPMValue = Table->RPM[RPMIndex];
00129             lowRPMIndex = RPMIndex;
00130         }else if(Table->RPM[RPMIndex] > realRPM){
00131             highRPMValue = Table->RPM[RPMIndex];
00132             highRPMIndex = RPMIndex;
00133             break;
00134         }else if(Table->RPM[RPMIndex] == realRPM){
00135             lowRPMValue = Table->RPM[RPMIndex];
00136             highRPMValue = Table->RPM[RPMIndex];
00137             lowRPMIndex = RPMIndex;
00138             highRPMIndex = RPMIndex;
00139             break;
00140         }
00141     }
00142 
00143     /* Find the bounding cell values and indices for Load */
00144     unsigned char lowLoadIndex = 0;
00145     unsigned char highLoadIndex = Table->LoadLength -1;
00146     /* If never set in the loop, low value will equal high value and will be on the edge of the map */
00147     unsigned short lowLoadValue = Table->Load[0];
00148     unsigned short highLoadValue = Table->Load[Table->LoadLength -1];
00149 
00150     unsigned char LoadIndex;
00151     for(LoadIndex=0;LoadIndex<Table->LoadLength;LoadIndex++){
00152         if(Table->Load[LoadIndex] < realLoad){
00153             lowLoadValue = Table->Load[LoadIndex];
00154             lowLoadIndex = LoadIndex;
00155         }else if(Table->Load[LoadIndex] > realLoad){
00156             highLoadValue = Table->Load[LoadIndex];
00157             highLoadIndex = LoadIndex;
00158             break;
00159         }else if(Table->Load[LoadIndex] == realLoad){
00160             lowLoadValue = Table->Load[LoadIndex];
00161             highLoadValue = Table->Load[LoadIndex];
00162             lowLoadIndex = LoadIndex;
00163             highLoadIndex = LoadIndex;
00164             break;
00165         }
00166     }
00167 
00168     /* Obtain the four corners surrounding the spot of interest */
00169     unsigned short lowRPMLowLoad = Table->Table[(Table->LoadLength * lowRPMIndex) + lowLoadIndex];
00170     unsigned short lowRPMHighLoad = Table->Table[(Table->LoadLength * lowRPMIndex) + highLoadIndex];
00171     unsigned short highRPMLowLoad = Table->Table[(Table->LoadLength * highRPMIndex) + lowLoadIndex];
00172     unsigned short highRPMHighLoad = Table->Table[(Table->LoadLength * highRPMIndex) + highLoadIndex];
00173 
00174     /* Restore the ram page before doing the math */
00175     RPAGE = oldRPage;
00176 
00177     /* Find the two side values to interpolate between by interpolation */
00178     unsigned short lowRPMIntLoad = lowRPMLowLoad + (((signed long)((signed long)lowRPMHighLoad - lowRPMLowLoad) * (realLoad - lowLoadValue))/ (highLoadValue - lowLoadValue));
00179     unsigned short highRPMIntLoad = highRPMLowLoad + (((signed long)((signed long)highRPMHighLoad - highRPMLowLoad) * (realLoad - lowLoadValue))/ (highLoadValue - lowLoadValue));
00180 
00181     /* Interpolate between the two side values and return the result */
00182     return lowRPMIntLoad + (((signed long)((signed long)highRPMIntLoad - lowRPMIntLoad) * (realRPM - lowRPMValue))/ (highRPMValue - lowRPMValue));
00183 }

EXTERN unsigned short lookupTwoDTableUS ( twoDTableUS Table,
unsigned short  Value 
)

Two D table read function.

Looks up a value from a two D table using interpolation.

Author:
Fred Cooke
Parameters:
Table is a pointer to the table to read from.
Value is the position value used to lookup the return value.
Returns:
the interpolated value for the position specified

Definition at line 197 of file tableLookup.c.

References twoDTableUS::Axis, and twoDTableUS::Values.

Referenced by generateDerivedVars().

00197                                                                            {
00198 
00199     /* Find the bounding axis indices, axis values and lookup values */
00200     unsigned char lowIndex = 0;
00201     unsigned char highIndex = 15;
00202     /* If never set in the loop, low value will equal high value and will be on the edge of the map */
00203     unsigned short lowAxisValue = Table->Axis[0];
00204     unsigned short highAxisValue = Table->Axis[15];
00205     unsigned short lowLookupValue = Table->Values[0];
00206     unsigned short highLookupValue = Table->Values[15];
00207 
00208     unsigned char Index;
00209     for(Index=0;Index<16;Index++){
00210         if(Table->Axis[Index] < Value){
00211             lowIndex = Index;
00212             lowAxisValue = Table->Axis[Index];
00213             lowLookupValue = Table->Values[Index];
00214         }else if(Table->Axis[Index] > Value){
00215             highIndex = Index;
00216             highAxisValue = Table->Axis[Index];
00217             highLookupValue = Table->Values[Index];
00218             break;
00219         }else if(Table->Axis[Index] == Value){
00220             return Table->Values[Index]; // If right on, just return the value
00221         }
00222     }
00223 
00224 
00225     /* Interpolate and return the value */
00226     return lowLookupValue + (((signed long)((signed long)highLookupValue - lowLookupValue) * (Value - lowAxisValue))/ (highAxisValue - lowAxisValue));
00227 }

EXTERN unsigned short setPagedMainTableCellValue ( unsigned char  RPageValue,
mainTable Table,
unsigned short  RPMIndex,
unsigned short  LoadIndex,
unsigned short  cellValue 
)

Set a main table cell value.

Sets the value of a cell in a main table. This is used when configuring a table via a communication interface.

Author:
Fred Cooke
Parameters:
RPageValue The page of RAM that the table is in.
Table A pointer to the table to adjust.
RPMIndex The RPM position of the cell to adjust.
LoadIndex The load position of the cell to adjust.
cellValue The value to set the cell to.
Returns:
An error code. Zero means success, anything else is a failure.

Definition at line 284 of file tableLookup.c.

References invalidMainTableLoadIndex, invalidMainTableRPMIndex, mainTable::LoadLength, RPAGE, and mainTable::Table.

Referenced by decodePacketAndRespond().

00284                                                                                                                                                                   {
00285     unsigned char oldRPage = RPAGE;
00286     unsigned short errorID = 0;
00287     RPAGE = RPageValue;
00288     if(RPMIndex < Table->RPMLength){
00289         if(LoadIndex < Table->LoadLength){
00290             Table->Table[(Table->LoadLength * RPMIndex) + LoadIndex] = cellValue;
00291         }else{
00292             errorID = invalidMainTableLoadIndex;
00293         }
00294     }else{
00295         errorID = invalidMainTableRPMIndex;
00296     }
00297     RPAGE = oldRPage;
00298     return errorID;
00299 }

EXTERN unsigned short setPagedMainTableLoadValue ( unsigned char  RPageValue,
mainTable Table,
unsigned short  LoadIndex,
unsigned short  LoadValue 
)

Set a load axis value.

Sets the value of a load axis cell in a table. This is used when configuring the table via a comms interface.

Author:
Fred Cooke
Parameters:
RPageValue The page of RAM that the table is in.
Table is a pointer to the table to adjust.
LoadIndex The load position of the cell to adjust.
LoadValue The value to set the load axis cell to.
Returns:
An error code. Zero means success, anything else is a failure.

Definition at line 339 of file tableLookup.c.

References errorBaseMainTableLoad, mainTable::Load, mainTable::LoadLength, RPAGE, and setAxisValue().

Referenced by decodePacketAndRespond().

00339                                                                                                                                          {
00340     unsigned char oldRPage = RPAGE;
00341     RPAGE = RPageValue;
00342     unsigned short errorID = setAxisValue(LoadIndex, LoadValue, Table->Load, Table->LoadLength, errorBaseMainTableLoad);
00343     RPAGE = oldRPage;
00344     return errorID;
00345 }

Here is the call graph for this function:

EXTERN unsigned short setPagedMainTableRPMValue ( unsigned char  RPageValue,
mainTable Table,
unsigned short  RPMIndex,
unsigned short  RPMValue 
)

Set an RPM axis value.

Sets the value of an RPM axis cell in a table. This is used when configuring the table via a comms interface.

Author:
Fred Cooke
Parameters:
RPageValue The page of RAM that the table is in.
Table is a pointer to the table to adjust.
RPMIndex The RPM position of the cell to adjust.
RPMValue The value to set the RPM axis cell to.
Returns:
An error code. Zero means success, anything else is a failure.

Definition at line 316 of file tableLookup.c.

References errorBaseMainTableRPM, RPAGE, mainTable::RPM, mainTable::RPMLength, and setAxisValue().

Referenced by decodePacketAndRespond().

00316                                                                                                                                       {
00317     unsigned char oldRPage = RPAGE;
00318     RPAGE = RPageValue;
00319     unsigned short errorID = setAxisValue(RPMIndex, RPMValue, Table->RPM, Table->RPMLength, errorBaseMainTableRPM);
00320     RPAGE = oldRPage;
00321     return errorID;
00322 }

Here is the call graph for this function:

EXTERN unsigned short setPagedTwoDTableAxisValue ( unsigned char  RPageValue,
twoDTableUS Table,
unsigned short  axisIndex,
unsigned short  axisValue 
)

Set a two D axis value.

Sets the value of an axis cell in a table. This is used when configuring the table via a comms interface.

Author:
Fred Cooke
Parameters:
RPageValue The page of RAM that the table is in.
Table is a pointer to the table to adjust.
axisIndex The position of the axis cell to adjust.
axisValue The value to set the axis cell to.
Returns:
An error code. Zero means success, anything else is a failure.

Definition at line 389 of file tableLookup.c.

References twoDTableUS::Axis, errorBaseTwoDTableAxis, RPAGE, and setAxisValue().

Referenced by decodePacketAndRespond().

00389                                                                                                                                            {
00390     unsigned char oldRPage = RPAGE;
00391     RPAGE = RPageValue;
00392     unsigned short errorID = setAxisValue(axisIndex, axisValue, Table->Axis, 16, errorBaseTwoDTableAxis);
00393     RPAGE = oldRPage;
00394     return errorID;
00395 }

Here is the call graph for this function:

EXTERN unsigned short setPagedTwoDTableCellValue ( unsigned char  RPageValue,
twoDTableUS Table,
unsigned short  cellIndex,
unsigned short  cellValue 
)

Set a two D table cell value.

Sets the value of a cell in a two D table. This is used when configuring the table via a comms interface.

Author:
Fred Cooke
Parameters:
RPageValue The page of RAM that the table is in.
Table is a pointer to the table to adjust.
cellIndex The position of the cell to adjust.
cellValue The value to set the cell to.
Returns:
An error code. Zero means success, anything else is a failure.

Definition at line 362 of file tableLookup.c.

References invalidTwoDTableIndex, RPAGE, and twoDTableUS::Values.

Referenced by decodePacketAndRespond().

00362                                                                                                                                            {
00363     if(cellIndex > 15){
00364         return invalidTwoDTableIndex;
00365     }else{
00366         unsigned char oldRPage = RPAGE;
00367         RPAGE = RPageValue;
00368         Table->Values[cellIndex] = cellValue;
00369         RPAGE = oldRPage;
00370         return 0;
00371     }
00372 }

EXTERN unsigned short validateMainTable ( mainTable Table  ) 

Validate a main table.

Check that the configuration of the table is valid. Assumes pages are correctly set.

Todo:
more detail here....
Author:
Fred Cooke
Parameters:
Table is a pointer to the table to be validated.
Returns:
An error code. Zero means success, anything else is a failure.

Definition at line 409 of file tableLookup.c.

References invalidMainTableLoadLength, invalidMainTableLoadOrder, invalidMainTableMainLength, invalidMainTableRPMLength, invalidMainTableRPMOrder, mainTable::Load, mainTable::LoadLength, MAINTABLE_MAX_LOAD_LENGTH, MAINTABLE_MAX_MAIN_LENGTH, MAINTABLE_MAX_RPM_LENGTH, mainTable::RPM, and mainTable::RPMLength.

Referenced by decodePacketAndRespond().

00409                                                   {
00410     /* If required and only if required extend this to take r and f pages and check */
00411     /* any main table, not just a freshly received untrusted ones in linear space   */
00412 
00413     if(Table->RPMLength > MAINTABLE_MAX_RPM_LENGTH){
00414         return invalidMainTableRPMLength;
00415     }else if(Table->LoadLength > MAINTABLE_MAX_LOAD_LENGTH){
00416         return invalidMainTableLoadLength;
00417     }else if((Table->RPMLength * Table->LoadLength) > MAINTABLE_MAX_MAIN_LENGTH){
00418         return invalidMainTableMainLength;
00419     }else{
00420         /* Check the order of the RPM axis */
00421         unsigned char i;
00422         for(i=0;i<(Table->RPMLength - 1);i++){
00423             if(Table->RPM[i] > Table->RPM[i+1]){
00424                 return invalidMainTableRPMOrder;
00425             }
00426         }
00427         /* Check the order of the Load axis */
00428         unsigned char j;
00429         for(j=0;j<(Table->LoadLength - 1);j++){
00430             if(Table->Load[j] > Table->Load[j+1]){
00431                 return invalidMainTableLoadOrder;
00432             }
00433         }
00434         /* If we made it this far all is well */
00435         return 0;
00436     }
00437 }

EXTERN unsigned short validateTwoDTable ( twoDTableUS Table  ) 

Validate a two D table.

Check that the order of the axis values is correct and therefore that the table is valid too.

Author:
Fred Cooke
Parameters:
Table is a pointer to the table to be validated.
Returns:
An error code. Zero means success, anything else is a failure.

Definition at line 451 of file tableLookup.c.

References twoDTableUS::Axis, invalidTwoDTableAxisOrder, and TWODTABLEUS_LENGTH.

Referenced by decodePacketAndRespond().

00451                                                     {
00452     /* Check the order of the axis */
00453     unsigned char i;
00454     for(i=0;i<(TWODTABLEUS_LENGTH - 1);i++){
00455         if(Table->Axis[i] > Table->Axis[i+1]){
00456             return invalidTwoDTableAxisOrder;
00457         }
00458     }
00459     return 0;
00460 }


Generated on Mon Jan 26 00:17:17 2009 for FreeEMS by  doxygen 1.5.8