RickMarsen
Joined: 31 Oct 2008 Posts: 17
|
2D Lookup Table Smoothing |
Posted: Sat Oct 30, 2010 4:20 pm |
|
|
I’m wondering if any of you guys have a good suggestion on how to “linearize” the parsing of a 2D lookup table (i.e. array).
I have a program that puts out a duty cycle determined by an ADC reading and a calculated RPM value. I am doing this by using a 16x16 lookup table populated with duty values (ranging from 0 to 1023).
My program works quite well except that my algorithm resolves any in-between ADC readings to an adjacent array index making my control output too “grainy”. I think I can figure out how I would “smooth” my output in a one-dimensional array situation but not in two.
Here is the relevant code:
Code: |
long motor_duty_table[16][16]; //Filled by a PC application via RS232
long vert_index_resolver[16] = {0,68,136,204,272,340,408,476,544,612,680,748,816,884,952,1020}; //Used to determine the vertical array index
long horiz_index_resolver[16] = {0,1000,2000,3000,4000,5000,6000,7000,8000,9000,10000,11000,12000,13000,14000,15000}; //Used to determine the horizontal array index
// This Function takes a current ADC reading or the current RPM reading and returns the corresponding array index
int resolve_index(long reading, long *array)
{
int result = 0xFF;
i = 0;
while((result == 0xFF) && (i<16))
{
if(i<15)
{
if((reading >= array[i]) && (reading < array[i+1])) result = i;
else result = 0xFF;
}
else
{
if(reading >= array[i]) result = i;
else result = 0xFF;
}
i++;
}
return result;
}
//The function is used like this:
int i, j;
i = resolve_index(ADC_reading,vert_index_resolver);
j = resolve_index(RPM_reading,horiz_index_resolver);
set_pwm2_duty(motor_duty_table[i][j]); |
|
|