00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00037 #define TABLELOOKUP_C
00038 #include "inc/freeEMS.h"
00039 #include "inc/commsISRs.h"
00040 #include "inc/tableLookup.h"
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00113 unsigned short lookupPagedMainTableCellValue(mainTable* Table, unsigned short realRPM, unsigned short realLoad, unsigned char RAMPage){
00114
00115
00116 unsigned char oldRPage = RPAGE;
00117 RPAGE = RAMPage;
00118
00119
00120 unsigned char lowRPMIndex = 0;
00121 unsigned char highRPMIndex = Table->RPMLength - 1;
00122
00123 unsigned short lowRPMValue = Table->RPM[0];
00124 unsigned short highRPMValue = Table->RPM[Table->RPMLength -1];
00125
00126 unsigned char RPMIndex;
00127 for(RPMIndex=0;RPMIndex<Table->RPMLength;RPMIndex++){
00128 if(Table->RPM[RPMIndex] < realRPM){
00129 lowRPMValue = Table->RPM[RPMIndex];
00130 lowRPMIndex = RPMIndex;
00131 }else if(Table->RPM[RPMIndex] > realRPM){
00132 highRPMValue = Table->RPM[RPMIndex];
00133 highRPMIndex = RPMIndex;
00134 break;
00135 }else if(Table->RPM[RPMIndex] == realRPM){
00136 lowRPMValue = Table->RPM[RPMIndex];
00137 highRPMValue = Table->RPM[RPMIndex];
00138 lowRPMIndex = RPMIndex;
00139 highRPMIndex = RPMIndex;
00140 break;
00141 }
00142 }
00143
00144
00145 unsigned char lowLoadIndex = 0;
00146 unsigned char highLoadIndex = Table->LoadLength -1;
00147
00148 unsigned short lowLoadValue = Table->Load[0];
00149 unsigned short highLoadValue = Table->Load[Table->LoadLength -1];
00150
00151 unsigned char LoadIndex;
00152 for(LoadIndex=0;LoadIndex<Table->LoadLength;LoadIndex++){
00153 if(Table->Load[LoadIndex] < realLoad){
00154 lowLoadValue = Table->Load[LoadIndex];
00155 lowLoadIndex = LoadIndex;
00156 }else if(Table->Load[LoadIndex] > realLoad){
00157 highLoadValue = Table->Load[LoadIndex];
00158 highLoadIndex = LoadIndex;
00159 break;
00160 }else if(Table->Load[LoadIndex] == realLoad){
00161 lowLoadValue = Table->Load[LoadIndex];
00162 highLoadValue = Table->Load[LoadIndex];
00163 lowLoadIndex = LoadIndex;
00164 highLoadIndex = LoadIndex;
00165 break;
00166 }
00167 }
00168
00169
00170 unsigned short lowRPMLowLoad = Table->Table[(Table->LoadLength * lowRPMIndex) + lowLoadIndex];
00171 unsigned short lowRPMHighLoad = Table->Table[(Table->LoadLength * lowRPMIndex) + highLoadIndex];
00172 unsigned short highRPMLowLoad = Table->Table[(Table->LoadLength * highRPMIndex) + lowLoadIndex];
00173 unsigned short highRPMHighLoad = Table->Table[(Table->LoadLength * highRPMIndex) + highLoadIndex];
00174
00175
00176 RPAGE = oldRPage;
00177
00178
00179 unsigned short lowRPMIntLoad = lowRPMLowLoad + (((signed long)((signed long)lowRPMHighLoad - lowRPMLowLoad) * (realLoad - lowLoadValue))/ (highLoadValue - lowLoadValue));
00180 unsigned short highRPMIntLoad = highRPMLowLoad + (((signed long)((signed long)highRPMHighLoad - highRPMLowLoad) * (realLoad - lowLoadValue))/ (highLoadValue - lowLoadValue));
00181
00182
00183 return lowRPMIntLoad + (((signed long)((signed long)highRPMIntLoad - lowRPMIntLoad) * (realRPM - lowRPMValue))/ (highRPMValue - lowRPMValue));
00184 }
00185
00186
00198 unsigned short lookupTwoDTableUS(twoDTableUS * Table, unsigned short Value){
00199
00200
00201 unsigned char lowIndex = 0;
00202 unsigned char highIndex = 15;
00203
00204 unsigned short lowAxisValue = Table->Axis[0];
00205 unsigned short highAxisValue = Table->Axis[15];
00206 unsigned short lowLookupValue = Table->Values[0];
00207 unsigned short highLookupValue = Table->Values[15];
00208
00209 unsigned char Index;
00210 for(Index=0;Index<16;Index++){
00211 if(Table->Axis[Index] < Value){
00212 lowIndex = Index;
00213 lowAxisValue = Table->Axis[Index];
00214 lowLookupValue = Table->Values[Index];
00215 }else if(Table->Axis[Index] > Value){
00216 highIndex = Index;
00217 highAxisValue = Table->Axis[Index];
00218 highLookupValue = Table->Values[Index];
00219 break;
00220 }else if(Table->Axis[Index] == Value){
00221 return Table->Values[Index];
00222 }
00223 }
00224
00225
00226
00227 return lowLookupValue + (((signed long)((signed long)highLookupValue - lowLookupValue) * (Value - lowAxisValue))/ (highAxisValue - lowAxisValue));
00228 }
00229
00230
00246 unsigned short setAxisValue(unsigned short index, unsigned short value, unsigned short axis[], unsigned short length, unsigned short errorBase){
00247 if(index >= length){
00248 return errorBase + invalidAxisIndex;
00249 }else{
00250 if(index > 0){
00251
00252 if(axis[index - 1] > value){
00253 return errorBase + invalidAxisOrder;
00254 }
00255 }
00256 if(index < (length -1)){
00257
00258 if(value > axis[index + 1]){
00259 return errorBase + invalidAxisOrder;
00260 }
00261 }
00262 }
00263
00264
00265 axis[index] = value;
00266 return 0;
00267 }
00268
00269
00285 unsigned short setPagedMainTableCellValue(unsigned char RPageValue, mainTable* Table, unsigned short RPMIndex, unsigned short LoadIndex, unsigned short cellValue){
00286 unsigned char oldRPage = RPAGE;
00287 unsigned short errorID = 0;
00288 RPAGE = RPageValue;
00289 if(RPMIndex < Table->RPMLength){
00290 if(LoadIndex < Table->LoadLength){
00291 Table->Table[(Table->LoadLength * RPMIndex) + LoadIndex] = cellValue;
00292 }else{
00293 errorID = invalidMainTableLoadIndex;
00294 }
00295 }else{
00296 errorID = invalidMainTableRPMIndex;
00297 }
00298 RPAGE = oldRPage;
00299 return errorID;
00300 }
00301
00302
00317 unsigned short setPagedMainTableRPMValue(unsigned char RPageValue, mainTable* Table, unsigned short RPMIndex, unsigned short RPMValue){
00318 unsigned char oldRPage = RPAGE;
00319 RPAGE = RPageValue;
00320 unsigned short errorID = setAxisValue(RPMIndex, RPMValue, Table->RPM, Table->RPMLength, errorBaseMainTableRPM);
00321 RPAGE = oldRPage;
00322 return errorID;
00323 }
00324
00325
00340 unsigned short setPagedMainTableLoadValue(unsigned char RPageValue, mainTable* Table, unsigned short LoadIndex, unsigned short LoadValue){
00341 unsigned char oldRPage = RPAGE;
00342 RPAGE = RPageValue;
00343 unsigned short errorID = setAxisValue(LoadIndex, LoadValue, Table->Load, Table->LoadLength, errorBaseMainTableLoad);
00344 RPAGE = oldRPage;
00345 return errorID;
00346 }
00347
00348
00363 unsigned short setPagedTwoDTableCellValue(unsigned char RPageValue, twoDTableUS* Table, unsigned short cellIndex, unsigned short cellValue){
00364 if(cellIndex > 15){
00365 return invalidTwoDTableIndex;
00366 }else{
00367 unsigned char oldRPage = RPAGE;
00368 RPAGE = RPageValue;
00369 Table->Values[cellIndex] = cellValue;
00370 RPAGE = oldRPage;
00371 return 0;
00372 }
00373 }
00374
00375
00390 unsigned short setPagedTwoDTableAxisValue(unsigned char RPageValue, twoDTableUS* Table, unsigned short axisIndex, unsigned short axisValue){
00391 unsigned char oldRPage = RPAGE;
00392 RPAGE = RPageValue;
00393 unsigned short errorID = setAxisValue(axisIndex, axisValue, Table->Axis, 16, errorBaseTwoDTableAxis);
00394 RPAGE = oldRPage;
00395 return errorID;
00396 }
00397
00398
00410 unsigned short validateMainTable(mainTable* Table){
00411
00412
00413
00414 if(Table->RPMLength > MAINTABLE_MAX_RPM_LENGTH){
00415 return invalidMainTableRPMLength;
00416 }else if(Table->LoadLength > MAINTABLE_MAX_LOAD_LENGTH){
00417 return invalidMainTableLoadLength;
00418 }else if((Table->RPMLength * Table->LoadLength) > MAINTABLE_MAX_MAIN_LENGTH){
00419 return invalidMainTableMainLength;
00420 }else{
00421
00422 unsigned char i;
00423 for(i=0;i<(Table->RPMLength - 1);i++){
00424 if(Table->RPM[i] > Table->RPM[i+1]){
00425 return invalidMainTableRPMOrder;
00426 }
00427 }
00428
00429 unsigned char j;
00430 for(j=0;j<(Table->LoadLength - 1);j++){
00431 if(Table->Load[j] > Table->Load[j+1]){
00432 return invalidMainTableLoadOrder;
00433 }
00434 }
00435
00436 return 0;
00437 }
00438 }
00439
00440
00452 unsigned short validateTwoDTable(twoDTableUS* Table){
00453
00454 unsigned char i;
00455 for(i=0;i<(TWODTABLEUS_LENGTH - 1);i++){
00456 if(Table->Axis[i] > Table->Axis[i+1]){
00457 return invalidTwoDTableAxisOrder;
00458 }
00459 }
00460 return 0;
00461 }