Trig functions on Atmega168 - arduino

Your source for all things Atmel.

Moderator: phalanx

Post Reply
thefatmoop
Posts: 31
Joined: Tue Dec 30, 2008 1:19 pm

Trig functions on Atmega168 - arduino

Post by thefatmoop » Thu Apr 08, 2010 9:42 pm

looked around a little. Is there anyway to calculate trig funtions without a lookup table? =/

motopic
Posts: 272
Joined: Thu Jun 15, 2006 11:33 am

Re: Trig functions on Atmega168 - arduino

Post by motopic » Fri Apr 09, 2010 8:48 am

Do you mean without using the built-in ones?

http://arduino.cc/en/Reference/Sin

OrlandoArias
Posts: 23
Joined: Sun Jan 10, 2010 1:22 pm
Location: The Wired 2.0

Re: Trig functions on Atmega168 - arduino

Post by OrlandoArias » Fri Apr 09, 2010 3:10 pm

Quick note I'd like to add to this. Most likely the provided functions in the arduino core library are just directly inherited from avr-libc. I have to look at the sources, but I doubt they would reinvent the wheel. If that is the case, please look at the following link, specifically at the time it takes to execute these functions in your AVR chip.

http://www.nongnu.org/avr-libc/user-man ... marks.html

If your application is timing critical, a lookup table may be the best method. As an example, sin(1.2345) will take 1653 / (16 MHz) = 103.3125 microseconds to execute. Using a lookup table not only may reduce the time it takes for the function to execute, it may even make your memory (both flash and RAM) footprint smaller.
C code. C code run. Run, code run... Please?

thefatmoop
Posts: 31
Joined: Tue Dec 30, 2008 1:19 pm

Re: Trig functions on Atmega168 - arduino

Post by thefatmoop » Sat Apr 10, 2010 2:26 pm

ohh duhh... i didn't look to see if one was included

haha 103 microseconds per calculation wow! i guess it's going to take some major code tweaking to get about 4 sig figs on an integration

for what it's worth here's the code for c++ using the rectangle method to approx an integration

Code: Select all

double f(double x){

	return sin(x)/x;

}

double integrate(double a,double b,int N){
     double dx = (b - a) / N;//delta x, not instantaneous
	 double sum = 0;

	 for(int iter = 1; iter < N; iter++){
	     double x = a + iter*dx;
		 sum += f(x);
	 }
     return sum*dx;
}

double integrateFull(double a, double b){
	double area1 = integrate(a,b,1);
	double area2 = integrate(a,b,2);
	int N=2;

	while(fabs(area1 - area2)> .00001){
		area1 = area2;
		N+=5;
		area2 = integrate(a,b,N);
	}
	cout << "\nN is:"<<N<<endl;
	return area2;
}
to verify code is working:
Image

stevech
Support Volunteer
Posts: 2907
Joined: Sat Jun 05, 2004 8:51 pm
Location: USA, California

Re: Trig functions on Atmega168 - arduino

Post by stevech » Sat Apr 10, 2010 10:28 pm

Floating point on a $3 microprocessor a'int fast!

Best to find a way to get the algorithm to work with you using fixed point arithmetic. If you need speed.

ruurdjan
Posts: 14
Joined: Tue Feb 23, 2010 1:53 am

Re: Trig functions on Atmega168 - arduino

Post by ruurdjan » Wed Apr 14, 2010 5:03 am

thefatmoop wrote:ohh duhh... i didn't look to see if one was included
to verify code is working:
Image
Reminds me of:
Image

(xkcd.com)

Post Reply