Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
178 | Nick666 | 1 | /* |
2 | This program (files math.c and math.h) is free software; you can redistribute it and/or modify |
||
3 | it under the terms of the GNU General Public License as published by the Free Software Foundation; |
||
4 | either version 3 of the License, or (at your option) any later version. |
||
5 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
||
6 | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
7 | GNU General Public License for more details. You should have received a copy of the GNU General Public License |
||
8 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
9 | |||
10 | Please note: All the other files for the project "Mikrokopter" by H.Buss are under the license (license_buss.txt) published by www.mikrokopter.de |
||
11 | */ |
||
12 | |||
13 | #include "main.h" |
||
14 | |||
15 | const unsigned int pgm_sinus[91] PROGMEM = {0,17,35,52,70,87,105,122,139,156,174,191,208,225,242,259,276,292,309,326,342,358,375,391,407,423,438,454,469,485,500,515,530,545,559,574,588,602,616,629,643,656,669,682,695,707,719,731,743,755,766,777,788,799,809,819,829,839,848,857,866,875,883,891,899,906,914,921,927,934,940,946,951,956,961,966,970,974,978,982,985,988,990,993,995,996,998,999,999,1000,1000}; |
||
16 | |||
17 | |||
18 | //----------------------------------------------- |
||
19 | // Fast arctan2 |
||
20 | // http://www.dspguru.com/comp.dsp/tricks/alg/fxdatan2.htm |
||
21 | //----------------------------------------------- |
||
22 | signed int arctan_i(signed int x, signed int y) |
||
23 | { |
||
24 | float rad,r; |
||
25 | signed int angle; |
||
26 | |||
27 | |||
28 | if ( !x && !y) return 0; |
||
29 | |||
30 | y = abs(y); |
||
31 | |||
32 | if (x>=0) |
||
33 | { |
||
34 | r = (x - y) / (x + y); |
||
35 | rad = 0.1963*r*r*r - 0.9817*r + 0.7854; |
||
36 | } |
||
37 | else |
||
38 | { |
||
39 | r = (x + y) / (y - x); |
||
40 | rad = 0.1963*r*r*r - 0.9817*r + 2.3562; |
||
41 | } |
||
42 | |||
43 | angle = (360 * rad) / 6.2832; |
||
44 | |||
45 | if (y < 0) return(-angle); // negate if in quad III or IV |
||
46 | else return(angle); |
||
47 | } |
||
48 | |||
49 | // cosinus Funktion: Eingabewert Winkel in Grad, Rueckgabe =cos(winkel)*1000 |
||
50 | signed int cos_i(signed int winkel) |
||
51 | { |
||
52 | winkel = sin_i(90-winkel); |
||
53 | return winkel; |
||
54 | } |
||
55 | |||
56 | |||
57 | // sinus Funktion: Eingabewert Winkel in Grad, Rueckgabe =sin(winkel)*1000 |
||
58 | signed int sin_i(signed int winkel) |
||
59 | { |
||
60 | short int m; |
||
61 | winkel = winkel % 360; |
||
62 | |||
63 | if (winkel < 0) |
||
64 | { |
||
65 | m = -1; |
||
66 | winkel = abs(winkel); |
||
67 | } |
||
68 | else m = +1; |
||
69 | |||
70 | // Quadranten auswerten |
||
71 | if ((winkel > 90 ) && (winkel <= 180)) winkel = winkel - 90; |
||
72 | else if ((winkel > 180 ) && (winkel <= 270)) winkel = winkel - 180; |
||
73 | else if ((winkel > 270) && (winkel <= 360)) winkel = winkel - 270; |
||
74 | // else //0 - 90 Grad |
||
75 | |||
76 | winkel = pgm_read_word(&pgm_sinus[winkel]); |
||
77 | |||
78 | return (winkel*m); |
||
79 | |||
80 | } |
||
81 | |||
82 |