Go to Post I think Dean has surpassed my 12 minute prediction.... - Koko Ed [more]
Home
Go Back   Chief Delphi > Other > FIRST Tech Challenge
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 25-11-2006, 15:02
Unsung FIRST Hero
miketwalker miketwalker is offline
Robot Lifeguard
FRC #1902 (Exploding Bacon)
 
Join Date: Dec 2001
Rookie Year: 2002
Location: Orlando, FL
Posts: 878
miketwalker has a reputation beyond reputemiketwalker has a reputation beyond reputemiketwalker has a reputation beyond reputemiketwalker has a reputation beyond reputemiketwalker has a reputation beyond reputemiketwalker has a reputation beyond reputemiketwalker has a reputation beyond reputemiketwalker has a reputation beyond reputemiketwalker has a reputation beyond reputemiketwalker has a reputation beyond reputemiketwalker has a reputation beyond repute
Send a message via AIM to miketwalker
Trig. Functions in EasyC

I haven't been able to find an answer to this on any of the Vex forums so I figured I'd throw it here. The students on our Vex team are working on a concept with their autonomous and need to be able to use trig functions (primarily tangent) but they can do everything else they are working with in EasyC if they can get the value of a calculation using these trig. functions. Unlike MPLAB it seems that you can't just plug in a math header file and use it like in FRC, but perhaps I am wrong. Has anyone does this and/or any suggestions on how to go about doing it? Thanks!
__________________
Chopsaw? Chopsaw.
Reply With Quote
  #2   Spotlight this post!  
Unread 25-11-2006, 16:35
Bongle's Avatar
Bongle Bongle is offline
Registered User
FRC #2702 (REBotics)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2002
Location: Waterloo
Posts: 1,069
Bongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond repute
Send a message via MSN to Bongle
Re: Trig. Functions in EasyC

You can approximate sin very well between -PI/2 and PI/2 with the function f(theta) = theta - theta3/6 + theta5/120.

Once you've got that approximation, you can basically compute any sin value by using the periodic properties of sin.

sin(x) = sin(PI - x) between PI/2 and 3PI/2 (this recursively uses the bit we can accurately approximate)
sin(x) = sin(x - 2PI) between 3PI/2 and 2PI.

Here's the code for sin:
Code:
#define PI 3.14159

float ApproxSin(float theta)
{
    while(theta > (3*PI)/2)
    {
        theta -= 2*PI;
    }
    while(theta < -PI/2)
    {
        theta += 2*PI;
    }
    // theta is now between -PI/2 and 3*PI/2

    if(theta > -PI/2 && theta <= PI/2) // basic approximation
    {
        return theta - theta*theta*theta/6 + theta*theta*theta*theta*theta/120;
    }
    else if(theta > PI/2 && theta < 3*PI/2)
    {
        return ApproxSin(PI - theta);
    }
}
An approximation for cos is f(x) = 1 - x2/2 + x4/24, you can do the same thing with it, more or less.
Reply With Quote
  #3   Spotlight this post!  
Unread 27-11-2006, 13:42
BradAMiller BradAMiller is offline
Registered User
AKA: Brad
#0190 ( Gompei and the Herd)
Team Role: Mentor
 
Join Date: Mar 2004
Location: Worcester, MA
Posts: 591
BradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant future
Re: Trig. Functions in EasyC

Quote:
Originally Posted by miketwalker
I haven't been able to find an answer to this on any of the Vex forums so I figured I'd throw it here. The students on our Vex team are working on a concept with their autonomous and need to be able to use trig functions (primarily tangent) but they can do everything else they are working with in EasyC if they can get the value of a calculation using these trig. functions. Unlike MPLAB it seems that you can't just plug in a math header file and use it like in FRC, but perhaps I am wrong. Has anyone does this and/or any suggestions on how to go about doing it? Thanks!
The Microchip libaries include math functions. Be sure to add an include for math.h so that the correct types are passed to the functions.

Brad
__________________
Brad Miller
Robotics Resource Center
Worcester Polytechnic Institute
Reply With Quote
  #4   Spotlight this post!  
Unread 27-11-2006, 21:46
TubaMorg TubaMorg is offline
Programmermechanicalelect ricalcoach
AKA: Dan
FRC #1480 (Robatos Locos)
Team Role: Mentor
 
Join Date: Jan 2006
Rookie Year: 2005
Location: Houston
Posts: 450
TubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond repute
Re: Trig. Functions in EasyC

I would like to suggest, without actually knowing your specific application, that you might want to utilize a lookup table. If it is a situation where you need to do the calculations many, many times, then a lookup table will save you computation time as trig functions do have some overhead. Precalculate your trig function for, say, 0-359 degrees and plug the values into an array (indexed 0-359 of course!). Then when needed you just index the array for the answer. If this is a navigation application 0-359 degrees should really be enough resolution, given the error inherent in the sensors. The only drawback is, of course, you have to use up memory to store the array of floats.
Reply With Quote
  #5   Spotlight this post!  
Unread 04-12-2006, 08:16
Bongle's Avatar
Bongle Bongle is offline
Registered User
FRC #2702 (REBotics)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2002
Location: Waterloo
Posts: 1,069
Bongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond reputeBongle has a reputation beyond repute
Send a message via MSN to Bongle
Re: Trig. Functions in EasyC

Quote:
Originally Posted by TubaMorg
I would like to suggest, without actually knowing your specific application, that you might want to utilize a lookup table. If it is a situation where you need to do the calculations many, many times, then a lookup table will save you computation time as trig functions do have some overhead. Precalculate your trig function for, say, 0-359 degrees and plug the values into an array (indexed 0-359 of course!). Then when needed you just index the array for the answer. If this is a navigation application 0-359 degrees should really be enough resolution, given the error inherent in the sensors. The only drawback is, of course, you have to use up memory to store the array of floats.
I always forget about lookup tables

I'm not sure how much code space the VEX controller has, but 360 floats in a table is 1440 bytes, which is quite a bit. Another idea in that vein would be to have a smaller table (45 entries) and just interpolate between them.
Code:
float GetSin(int degrees)
{
  int subDegrees = degrees & 7; // gets the last 3 bits
  int tableIndex = degrees / 8; // finds out the table entry we want
  return ((8-subDegrees)*table[tableIndex] + subDegrees*table[tableIndex+1])/8;
}
This function is a very simple linear interpolation. But it would allow you to have a much smaller table taking up much less memory. Another improvement would be to only have a table for between -PI/2 and PI/2, then using the same techniques I posted in my first post to get answers for PI/2 and beyond. This would again cut table size in half.
Reply With Quote
  #6   Spotlight this post!  
Unread 07-12-2006, 23:27
TubaMorg TubaMorg is offline
Programmermechanicalelect ricalcoach
AKA: Dan
FRC #1480 (Robatos Locos)
Team Role: Mentor
 
Join Date: Jan 2006
Rookie Year: 2005
Location: Houston
Posts: 450
TubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond reputeTubaMorg has a reputation beyond repute
Re: Trig. Functions in EasyC

This is good stuff! Excellent suggestions!

Quote:
Originally Posted by Bongle
I always forget about lookup tables

I'm not sure how much code space the VEX controller has, but 360 floats in a table is 1440 bytes, which is quite a bit. Another idea in that vein would be to have a smaller table (45 entries) and just interpolate between them.
Code:
float GetSin(int degrees)
{
  int subDegrees = degrees & 7; // gets the last 3 bits
  int tableIndex = degrees / 8; // finds out the table entry we want
  return ((8-subDegrees)*table[tableIndex] + subDegrees*table[tableIndex+1])/8;
}
This function is a very simple linear interpolation. But it would allow you to have a much smaller table taking up much less memory. Another improvement would be to only have a table for between -PI/2 and PI/2, then using the same techniques I posted in my first post to get answers for PI/2 and beyond. This would again cut table size in half.
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Organize EasyC functions in a fashion we like? Chris_Elston Programming 3 20-02-2006 18:53
speed of math.h trig functions? Jared Russell Programming 4 07-02-2006 07:13
Calling C functions from EasyC koenig3456 Programming 4 27-01-2006 16:03
Trig functions and type conversion kaszeta Programming 6 14-01-2006 23:34
Return of the Inverse Trig Functions Leo M Programming 3 24-01-2002 08:12


All times are GMT -5. The time now is 19:27.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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