Completed dynamic 2D table implementation (Untested)

This commit is contained in:
Josh Stewart 2014-02-27 13:26:04 +11:00
parent b27b8231d1
commit 78fdfbe2bb
3 changed files with 39 additions and 9 deletions

View File

@ -54,7 +54,7 @@ volatile unsigned long toothOneMinusOneTime = 0; //The 2nd to last time (micros(
struct table3D fuelTable; //8x8 fuel map
struct table3D ignitionTable; //8x8 ignition map
struct table2Dx4 taeTable; //4 bin TPS Acceleration Enrichment map (2D)
struct table2D taeTable; //4 bin TPS Acceleration Enrichment map (2D)
struct table2Dx10 WUETable; //10 bin Warm Up Enrichment map (2D)
unsigned long counter;

12
table.h
View File

@ -3,13 +3,15 @@ This file is used for everything related to maps/tables including their definiti
*/
#include <Arduino.h>
struct table2Dx4 {
const static byte xSize = 4;
struct table2D {
static byte xSize;
byte values[xSize];
int axisX[xSize];
byte *values;
int *axisX;
};
void table2D_setSize(struct table2D targetTable, byte newSize);
struct table2Dx10 {
const static byte xSize = 10;
@ -42,4 +44,4 @@ Eg: 2x2 table
*/
int get3DTableValue(struct table3D, int, int);
int get2DTableValue(struct table2D, int);
int table2D_getValue(struct table2D, int);

View File

@ -1,5 +1,33 @@
//This function simply pulls a 1D linear interpolated (ie averaged) value from a 2D table
int get2DTableValue(struct table2Dx4 fromTable, int X)
/*
Because the size of the table is dynamic, this functino is required to reallocate the array sizes
Note that this will clear all the existing values of the table
*/
void table2D_setSize(struct table2D targetTable, byte newSize)
{
//We are able to check whether this is the first time the table has been initialised by checking the xSize. By default it will be 0
if(targetTable.xSize == 0)
{
//Initialise the arrays
targetTable.values = (byte *)malloc(newSize * sizeof(byte));
targetTable.axisX = (int *)malloc(newSize * sizeof(int));
}
else
{
//Free the existing memory and then initialise to new size
free(targetTable.values);
free(targetTable.axisX);
targetTable.values = (byte *)malloc(newSize * sizeof(byte));
targetTable.axisX = (int *)malloc(newSize * sizeof(int));
}
targetTable.xSize = newSize;
}
/*
This function simply pulls a 1D linear interpolated (ie averaged) value from a 2D table
ie: Given a value on the X axis, it returns a Y value that coresponds to the point on the curve between the nearest two defined X values
*/
int table2D_getValue(struct table2D fromTable, int X)
{
int xMinValue = fromTable.axisX[0];
int xMaxValue = fromTable.axisX[fromTable.xSize-1];
@ -38,7 +66,7 @@ int get2DTableValue(struct table2Dx4 fromTable, int X)
//Non-Float version
int yVal;
yVal = ((m << 6) / n) * (abs(fromTable.values[xMax] - fromTable.values[xMin]));
yVal = ((long)(m << 6) / n) * (abs(fromTable.values[xMax] - fromTable.values[xMin]));
yVal = (yVal >> 6);
if (fromTable.values[xMax] > fromTable.values[xMin]) { yVal = fromTable.values[xMin] + yVal; }