4
\$\begingroup\$

I've been trying to figure out how to implement trig functions in my PicBasic projects. I could use a lookup table but that limits my output. I need to be able to use sin, cos, tan, and their inverses in order to do a time based calculation.

Should I just give up and start looking at programming PICs in C? I've got a programmer and a couple of PICs next to me and would rather use these before jumping on the Arduino bandwagon, or would it really be that much easier to use an Arduino?

\$\endgroup\$

5 Answers 5

3
\$\begingroup\$

This topic has been discussed at length on StackOverflow, it is fairly processor-independent and language-independent except for the relative costs of multiplication/addition/lookup, which will make certain implementations more attractive.

The atan2() function, or rectangular-to-polar conversion, is not completely implementable by lookup tables of reasonable size; CORDIC is one option.

\$\endgroup\$
2
\$\begingroup\$

Have a look at the CORDIC algorithms in section 31.2 of the fxtbook: http://www.jjj.de/fxt/

\$\endgroup\$
1
\$\begingroup\$

A little late, but here is a PicBasic Pro solution for Trig functions. It uses a Cordic assembly include file for fast calculations. Gives the result of Sin(x) and Cos(x), or the result of atan2(x,y) with about 16 bit precision.

http://www.picbasic.co.uk/forum/showthread.php?t=10528

\$\endgroup\$
1
\$\begingroup\$

You are coding on a PIC, how many bits is your angle? If you have an 8 bit number coming in, a look up table can be easy, if you have a 16 bit number coming in, then it is a different story.

Normally in PICs 8 or 10 bit number is about as big as you will get, a look up table is probably okay. People often get lost in getting a high accuracy number, normally you do not need one. I had someone reading a 12 bit number of a sensor, using doubles and getting a really accurate conversion out. It worked perfectly when he used a voltage source as an input for testing, but you add a real sensor with it's error and you find all of the accuracy is a waste of time.

There are cases where I would suggest using a high quality sin/cos/tan function, I have never encountered one on a PIC. Forgive me if you need a high quality sign function, I am just answering what would be the right answer for 95% of cases.

\$\endgroup\$
2
  • \$\begingroup\$ Plus if you were looking at angles with an accuracy of 1 degree, you would only need 90ish values in the table. All the others can be derived from the angles in the first quadrant. If you do this for sine and cosine, you can derive the values for tangent from these as: tan(theta) = sin(theta)/cos(theta). Do you need to do arcsin, arccos and arctan too? \$\endgroup\$
    – Amos
    Nov 23, 2009 at 9:35
  • \$\begingroup\$ Agreed Amos, I like your solution also, but multiplication show be chosen based on the presence of hardware for it normally, not all PICs have hardware multipliers. In reality, you can use a look up table for the degrees and a simple linear approximation from there and never know the difference also if it is that important. \$\endgroup\$
    – Kortuk
    Nov 23, 2009 at 16:31
0
\$\begingroup\$

Depending on whether time is a factor bear in mind that all trig functions have a Taylor series for them, for instance sine has:

taylor series for sine

The terms continue so the next would be + x^9/9! You increase the number of terms to get more accuracy. But if you need to do the calculation in any reasonable amount of time then this might not be the best solution. There's a long discussion about accuracy etc at the Taylor Series Wikipedia Page.

As ever YMMV.

There's an Arduino implementation of an accurate sine wave generator within the Arduino DDS Sinewave Generator project and from the looks of things they're using a lookup table too.

\$\endgroup\$
2
  • \$\begingroup\$ Taylor series are great for pure math but not particularly good for practical computations. \$\endgroup\$
    – Jason S
    Dec 1, 2009 at 0:12
  • \$\begingroup\$ Jason, I disagree. Taylor series are great for approximating smooth functions. You will need hardware multiplication to compute the powers though! \$\endgroup\$
    – drxzcl
    Feb 19, 2012 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy