Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
554 | Nick666 | 1 | /* |
2 | |||
3 | Copyright 2007, Niklas Nold |
||
4 | |||
5 | This program (files math.c and math.h) is free software; you can redistribute it and/or modify |
||
6 | it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; |
||
7 | either version 3 of the License, or (at your option) any later version. |
||
8 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
||
9 | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
10 | GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License |
||
11 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
12 | |||
13 | 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 |
||
14 | */ |
||
15 | |||
16 | #include "main.h" |
||
17 | |||
18 | |||
19 | const uint8_t pgm_atan[270] PROGMEM = {0,1,2,3,5,6,7,8,9,10,11,12,13,15,16,17,18,19,20,21,22,23,24,25,26,27,27,28,29,30,31,32,33,33,34,35,36,37,37,38,39,39,40,41,41,42,43,43,44,44,45,46,46,47,47,48,48,49,49,50,50,51,51,52,52,52,53,53,54,54,54,55,55,56,56,56,57,57,57,58,58,58,59,59,59,60,60,60,60,61,61,61,61,62,62,62,62,63,63,63,63,64,64,64,64,65,65,65,65,65,66,66,66,66,66,67,67,67,67,67,67,68,68,68,68,68,68,69,69,69,69,69,69,69,70,70,70,70,70,70,70,70,71,71,71,71,71,71,71,71,72,72,72,72,72,72,72,72,72,73,73,73,73,73,73,73,73,73,73,74,74,74,74,74,74,74,74,74,74,74,74,75,75,75,75,75,75,75,75,75,75,75,75,75,76,76,76,76,76,76,76,76,76,76,76,76,76,76,76,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79}; |
||
20 | |||
21 | //############################################################################ |
||
22 | // Arkustangens2 im Gradmaß |
||
23 | signed int atan2_i(signed int x, signed int y) |
||
24 | //############################################################################ |
||
25 | { |
||
26 | int i,angle; |
||
27 | int8_t m; |
||
28 | |||
29 | if (!x && !y) return 0; //atan2 = 0 für x und y = 0 |
||
30 | |||
31 | if (y < 0) m=-1; |
||
32 | else m=1; |
||
33 | |||
34 | if (x==0) return (90*m); // atan2 = 90° für x = 0 |
||
35 | |||
36 | i = abs(((long)y*50) / x); // Berechne i für die Lookup table (Schrittweite atan(x) ist 0.02 -> *50) |
||
37 | |||
38 | if (i<270) angle = pgm_read_byte(&pgm_atan[i]); // Lookup für 1° bis 79° |
||
39 | else if (i>5750) angle = 90; // Grenzwert ist 90° |
||
40 | else if (i>=1910) angle = 89; // 89° bis 80° über Wertebereiche |
||
41 | else if (i>=1150) angle = 88; |
||
42 | else if (i>=820) angle = 87; |
||
43 | else if (i>=640) angle = 86; |
||
44 | else if (i>=520) angle = 85; |
||
45 | else if (i>=440) angle = 84; |
||
46 | else if (i>=380) angle = 83; |
||
47 | else if (i>=335) angle = 82; |
||
48 | else if (i>=299) angle = 81; |
||
49 | else angle = 80; // (i>=270) |
||
50 | |||
51 | if (x > 0) return (angle*m); // Quadrant I und IV |
||
52 | else if ((x < 0) && (m > 0)) return (-angle + 180); // Quadrant II |
||
53 | else return (angle - 180); // x < 0 && y < 0 Quadrant III |
||
54 | } |
||
55 | |||
56 | |||
57 | const float pgm_sinus_f[91] PROGMEM = {0.0,0.0174524,0.0348995,0.0523360,0.0697565,0.0871557,0.1045285,0.1218693,0.1391731,0.1564345,0.1736482,0.1908090,0.2079117,0.2249510,0.2419219,0.2588190,0.2756373,0.2923717,0.3090170,0.3255681,0.3420201,0.3583679,0.3746066,0.3907311,0.4067366,0.4226182,0.4383711,0.4539905,0.4694715,0.4848096,0.5000000,0.5150381,0.5299193,0.5446390,0.5591929,0.5735764,0.5877852,0.6018150,0.6156615,0.6293204,0.6427876,0.6560590,0.6691306,0.6819983,0.6946584,0.7071068,0.7193398,0.7313537,0.7431448,0.7547096,0.7660444,0.7771459,0.7880107,0.7986355,0.8090170,0.8191520,0.8290376,0.8386706,0.8480481,0.8571673,0.8660254,0.8746197,0.8829476,0.8910065,0.8987940,0.9063078,0.9135454,0.9205048,0.9271838,0.9335804,0.9396926,0.9455186,0.9510565,0.9563047,0.9612617,0.9659258,0.9702957,0.9743701,0.9781476,0.9816272,0.9848077,0.9876883,0.9902681,0.9925461,0.9945219,0.9961947,0.9975640,0.9986295,0.9993908,0.9998477,1.0}; |
||
58 | |||
59 | inline float pgm_read_float(const float *addr) |
||
60 | { |
||
61 | union |
||
62 | { |
||
63 | uint16_t i[2]; // 2 16-bit-Worte |
||
64 | float f; |
||
65 | } u; |
||
66 | |||
67 | u.i[0]=pgm_read_word((PGM_P)addr); |
||
68 | u.i[1]=pgm_read_word((PGM_P)addr+2); |
||
69 | |||
70 | return u.f; |
||
71 | } |
||
72 | |||
73 | //############################################################################ |
||
74 | // Kosinusfunktion im Gradmaß |
||
75 | float cos_f(signed int winkel) |
||
76 | //############################################################################ |
||
77 | { |
||
78 | return (sin_f(90-winkel)); |
||
79 | } |
||
80 | |||
81 | //############################################################################ |
||
82 | // Sinusfunktion im Gradmaß |
||
83 | float sin_f(signed int winkel) |
||
84 | //############################################################################ |
||
85 | { |
||
86 | short int m,n; |
||
87 | float sinus; |
||
88 | |||
89 | //winkel = winkel % 360; |
||
90 | |||
91 | if (winkel < 0) |
||
92 | { |
||
93 | m = -1; |
||
94 | winkel = abs(winkel); |
||
95 | } |
||
96 | else m = +1; |
||
97 | |||
98 | // Quadranten auswerten |
||
99 | if (winkel <= 90) n=1; |
||
100 | else if ((winkel > 90 ) && (winkel <= 180)) {winkel = 180 - winkel; n = 1;} |
||
101 | else if ((winkel > 180 ) && (winkel <= 270)) {winkel = winkel - 180; n = -1;} |
||
102 | else {winkel = 360 - winkel; n = -1;} //if ((winkel > 270) && (winkel <= 360)) |
||
103 | |||
104 | sinus = pgm_read_float(&pgm_sinus_f[winkel]); |
||
105 | |||
106 | return (sinus*m*n); |
||
107 | } |
||
108 | |||
109 | /* |
||
110 | const uint8_t pgm_asin[201] PROGMEM = {0,0,1,1,1,1,2,2,2,3,3,3,3,4,4,4,5,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9,10,10,10,11,11,11,12,12,12,12,13,13,13,14,14,14,14,15,15,15,16,16,16,17,17,17,17,18,18,18,19,19,19,20,20,20,20,21,21,21,22,22,22,23,23,23,24,24,24,25,25,25,25,26,26,26,27,27,27,28,28,28,29,29,29,30,30,30,31,31,31,32,32,32,33,33,33,34,34,34,35,35,35,36,36,37,37,37,38,38,38,39,39,39,40,40,41,41,41,42,42,42,43,43,44,44,44,45,45,46,46,46,47,47,48,48,49,49,49,50,50,51,51,52,52,53,53,54,54,55,55,56,56,57,57,58,58,59,59,60,60,61,62,62,63,64,64,65,66,66,67,68,68,69,70,71,72,73,74,75,76,77,79,80,82,84,90}; |
||
111 | |||
112 | //############################################################################ |
||
113 | // Akurssinusfunktion im Gradmaß |
||
114 | int8_t asin_i(signed int i) |
||
115 | //############################################################################ |
||
116 | { |
||
117 | signed char m; |
||
118 | |||
119 | if (i < 0) {m=-1;i=abs(i);} |
||
120 | else m=1; |
||
121 | |||
122 | i = i % 200; |
||
123 | |||
124 | return (pgm_read_byte(&pgm_asin[i]) * m); |
||
125 | } |
||
126 | */ |