View Single Post
  #2   Spotlight this post!  
Unread 21-01-2004, 22:52
deltacoder1020's Avatar
deltacoder1020 deltacoder1020 is offline
Computer Guy
AKA: Dav
#1020 (The Indiana Prank Monkeys)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Muncie, Indiana
Posts: 340
deltacoder1020 has a spectacular aura aboutdeltacoder1020 has a spectacular aura about
Send a message via AIM to deltacoder1020
Re: Needing A header file

I've coded from scratch a basic sin lookup table and associated macros to fetch the correct values for sin() and cos(), either in float or char (float * 127, rounded) form. Note that using the sin_char() and cos_char() macros requires no floating-point arithmetic, if speed is your goal, those functions are for you. If you need more than approx. 2 digits accuracy for floating point values, feel free to replace the array of unsigned chars with unsigned ints, and modify the macros accordingly. Code is below.

Code:
//      LOOKUP.C
//      TRIG LOOKUP TABLE IMPLEMENTATION
//      -------------------------------------------------------
//      CREATED BY DAV YUST FOR 2004 FIRST ROBOTICS COMPETITION
//      RELEASED FOR PUBLIC USE UNDER FOLLOWING CONDITIONS:
//      1. PLEASE RELEASE ANY CHANGES YOU MAKE
//      2. PLEASE GIVE ME CREDIT (somewhere... anywhere)
//      3. LEAVE THIS NOTICE IN THE CODE UNCHANGED EXCEPT FOR
//          NOTICE OF UPDATES

//  This is a lookup table for the sin() function.
//  It accepts a degree value between 0 and 89, and
//  returns a char that is normal sin value multiplied
//  by 255 and rounded.
unsigned char SIN_LOOKUP[90] =
{ 0,  4,  9,  13, 18, 22, 27, 31, 35, 40,
  44, 49, 53, 57, 62, 66, 70, 75, 79, 83,
  87, 91, 96, 100,104,108,112,116,120,124,
  128,131,135,139,142,146,150,153,157,160,
  164,167,171,174,177,180,183,186,190,192,
  195,198,201,204,206,209,211,214,216,219,
  221,223,225,227,229,231,233,235,236,238,
  240,241,243,244,245,246,247,248,249,250,
  251,252,253,253,254,254,254,255,255,255 };

//////////////////////////////////////
//     MACROS FOR SIN FUNCTION      //
//////////////////////////////////////

//returns the standard float value from -1 to 1
//accuracy is approximately two digits
#define sin(x) ( ((x) < 90 ? (float)SIN_LOOKUP[(x)] : ( (x) < 180 ? (float)SIN_LOOKUP[179-(x)] : ( (x) < 270 ? -1.0 * (float)SIN_LOOKUP[(x)-180] : -1.0 * (float)SIN_LOOKUP[359-(x)]))) / 255.0)

//returns a signed char value from -128 to 127
//we have to divide by two here because a signed char
//can only go from -128 to 127.
#define sin_char(x) ( ((x) < 90 ? SIN_LOOKUP[(x)] : ( (x) < 180 ? SIN_LOOKUP[179-(x)] : ( (x) < 270 ? -1 * SIN_LOOKUP[(x)-180] : -1 * SIN_LOOKUP[359-(x)]))) >> 1)

//because cos is handily equal to sin(x - 90) (or sin(x+270)
#define cos(x) sin( ((x) + 270) % 360 )
#define cos_char(x) sin_char( ((x) + 270) % 360 )

////////////////////////////////////////////////////////////////////////////////
// ALL CODE BELOW FOR TESTING PURPOSES ONLY
////////////////////////////////////////////////////////////////////////////////

#include <stdio.h>

int main(int argc, char* argv[])
{
    printf("Testing SIN lookup table:\n");
    printf("    angle (deg)          sin()      sin_char()          cos()      cos_char()\n");
    int x;
    for(x = 0; x < 360; x += 15)
    {
        printf("%15i%15f%15i%15f%15i\n", x, sin(x), sin_char(x), cos(x), cos_char(x));
    }
    return 0;
}
__________________
Team 1020, the Indiana Prank Monkeys (www.team1020.org)

Last edited by deltacoder1020 : 21-01-2004 at 23:12.