Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Look Up Tables (http://www.chiefdelphi.com/forums/showthread.php?t=35619)

amateurrobotguy 28-02-2005 18:08

Look Up Tables
 
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 28-02-2005 18:39

Re: Look Up Tables
 
To search for an object in an array, do a small for loop:
Code:

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 01-03-2005 12:02

Re: Look Up Tables
 
Quote:

Originally Posted by amateurrobotguy
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 01-03-2005 13:09

Re: Look Up Tables
 
Quote:

Originally Posted by amateurrobotguy
...Like on table 1 find this number. Then from that number, goto table 2 with the same row and find that number.

Code:

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 01-03-2005 17:19

Re: Look Up Tables
 
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 08-03-2005 21:50

Re: Look Up Tables
 
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);



Quote:

Originally Posted by ace123
To search for an object in an array, do a small for loop:
Code:

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 09-03-2005 00:08

Re: Look Up Tables
 
The code
Code:

int val = *((&table) + index);
is equivalent to
Code:

int val=table[index];
In fact, I think they compile down to the same code at the end.

This reminds me of an IOCCC entry [hint file] that used index[array] instead of array[index] because the pointer math is in reality the same.

Matt Leese 09-03-2005 00:14

Re: Look Up Tables
 
Quote:

Originally Posted by ace123
The code
Code:

int val = *((&table) + index);
is equivalent to
Code:

int val=table[index];
In fact, I think they compile down to the same code at the end.

Technically, that should be
Code:

int val = *(table + index);
because table is a pointer by default. Writing
Code:

int table[];
is just short hand for
Code:

int *table;
Sorry for being pedantic but in programming it can be important.

Matt


All times are GMT -5. The time now is 04:38.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi