PDA

View Full Version : Look Up Tables

amateurrobotguy
02-28-2005, 05:08 PM
First, I would like to say thanks to all the people that give support to me and others on these forumns.

Next, since I can't use math functions, it has been suggested that a look-up table be used. I can make the tables, but how to I read data from them? Like on table 1 find this number. Then from that number, goto table 2 with the same row and find that number.

const int table_length = 18;
rom const int x[]= {0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255};
rom const int y[]= {0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255};

Basically I want something like:

Search x(135)
a=x postition
goto y(a)

ace123
02-28-2005, 05:39 PM
To search for an object in an array, do a small for loop:
int search_in_table_x(int lookfor)
int i;
for (i=0;i<table_length;++i) {
if (x[i]==lookfor) {
return i;
}
}
return -1;
}
function Default_Routine(void) {
int ind, x, y;
// ...
ind=search_in_table_x(135);
if (ind!=-1) {
x=x[ind];
y=y[ind];
pwm03=x; // then do something using those numbers?
pwm04=y;
}
}
(Check for -1 after using the function.)

I'm not sure exactly why you need a lookup table.

I'm pretty sure that whatever you are trying to do could be approximated faster and better with a small divide by 16 or something.

One thing to remember is that you shouldn't overcomplicate things like this. Something that can be solved with some complex math function with sines and powers or a lookup table could often be solved just as easily with a simple divide by 8 or scaling and adding without a noticable loss in accuracy.

Imagine if you are at the competition and your robot goes crazy. You would not want to debug through loads of complex math functions. It wouldn't be possible except for an extremely obvious error.
You would end up either wasting all of your precious practice day debugging it and getting nowhere or else giving up and making it simple.

Squirrelrock
03-01-2005, 11:02 AM
First, I would like to say thanks to all the people that give support to me and others on these forumns.

Next, since I can't use math functions, it has been suggested that a look-up table be used. I can make the tables, but how to I read data from them? Like on table 1 find this number. Then from that number, goto table 2 with the same row and find that number.

const int table_length = 18;
rom const int x[]= {0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255};
rom const int y[]= {0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255};

Basically I want something like:
Search x(135)
a=x postition
goto y(a)

If you're trying to have a table for autonomous points to drive to or some such nonsense, make a separate program to handle the tables on another computer that then inputs those cooridinates to the OI to send to the RC to drive to. (Contact Scitobor 617 for details on this program he's writing.)

If you're using these tables for speed control, then use % of the distance traveled (use an encoder) to determine speed.

If it's neither, I have no clue. :D

Squirrel

Alan Anderson
03-01-2005, 12:09 PM
...Like on table 1 find this number. Then from that number, goto table 2 with the same row and find that number.

const int table_length = 18;
rom const int x[]= {0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255};
rom const int y[]= {0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180, 195, 210, 225, 240, 255};

Basically I want something like:

Search x(135)
a=x postition
goto y(a)
Your example is probably poorly chosen to indicate what you want. The x and y arrays are identical, so whatever you put in as your search is what you get out as the result.

Lookup tables are most easily used as replacements for a simple y=f(x) computation when f() involves lots of multiplication. I don't know what function you're trying to perform with your pair of tables, but what you asked for reduces to a simple f(x) = x.

amateurrobotguy
03-01-2005, 04:19 PM
Thanks guys for the help. I know exactly what I am going to do to get what I want accomplished. I speak generally because I am kinda a hermit when it comes to giving away robot secreats. :cool:

xchezhd
03-08-2005, 08:50 PM
you could use a little pointer math once you find the index from the first table.

int* y_ptr = &y; // can't remember if you need the & here,
// check during compile
value_from_y = *(y_ptr + index);

To search for an object in an array, do a small for loop:
int search_in_table_x(int lookfor)
int i;
for (i=0;i<table_length;++i) {
if (x[i]==lookfor) {
return i;
}
}
return -1;
}
function Default_Routine(void) {
int ind, x, y;
// ...
ind=search_in_table_x(135);
if (ind!=-1) {
x=x[ind];
y=y[ind];
pwm03=x; // then do something using those numbers?
pwm04=y;
}
}
(Check for -1 after using the function.)

I'm not sure exactly why you need a lookup table.

I'm pretty sure that whatever you are trying to do could be approximated faster and better with a small divide by 16 or something.

One thing to remember is that you shouldn't overcomplicate things like this. Something that can be solved with some complex math function with sines and powers or a lookup table could often be solved just as easily with a simple divide by 8 or scaling and adding without a noticable loss in accuracy.

Imagine if you are at the competition and your robot goes crazy. You would not want to debug through loads of complex math functions. It wouldn't be possible except for an extremely obvious error.
You would end up either wasting all of your precious practice day debugging it and getting nowhere or else giving up and making it simple.

ace123
03-08-2005, 11:08 PM
The codeint val = *((&table) + index);is equivalent toint val=table[index]; In fact, I think they compile down to the same code at the end.

This reminds me of an IOCCC entry (http://ioccc.org/1987/korn.c) [hint file (http://ioccc.org/1987/korn.hint)] that used index[array] instead of array[index] because the pointer math is in reality the same.

Matt Leese
03-08-2005, 11:14 PM
The code
int val = *((&table) + index);
is equivalent to
int val=table[index];
In fact, I think they compile down to the same code at the end.

Technically, that should be
int val = *(table + index);
because table is a pointer by default. Writing int table[]; is just short hand for
int *table;
Sorry for being pedantic but in programming it can be important.

Matt