Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
308 | osiair | 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, |
||
20 | 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, |
||
21 | 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, |
||
22 | 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, |
||
23 | 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, |
||
24 | 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, |
||
25 | 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, |
||
26 | 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, |
||
27 | 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, |
||
28 | 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, |
||
29 | 79,79,79,79,79,79,79,79,79,79}; |
||
30 | |||
31 | // Arkustangens2 im Gradmaß |
||
32 | signed int atan2_i(signed int x, signed int y) |
||
33 | { |
||
34 | int i,angle; |
||
35 | int8_t m; |
||
36 | |||
37 | if (!x && !y) return 0; //atan2 = 0 für x und y = 0 |
||
38 | |||
39 | if (y < 0) m=-1; |
||
40 | else m=1; |
||
41 | |||
42 | if (x==0) return (90*m); // atan2 = 90° für x = 0 |
||
43 | |||
44 | i = abs(((float)y / x) * 50); // Berechne i für die Lookup table (Schrittweite atan(x) ist 0.02 -> *50) |
||
45 | |||
46 | if (i<270) angle = pgm_read_byte(&pgm_atan[i]); // Lookup für 1° bis 79° |
||
47 | else if (i>5750) angle = 90; // Grenzwert ist 90° |
||
48 | else if (i>=1910) angle = 89; // 89° bis 80° über Wertebereiche |
||
49 | else if (i>=1150) angle = 88; |
||
50 | else if (i>=820) angle = 87; |
||
51 | else if (i>=640) angle = 86; |
||
52 | else if (i>=520) angle = 85; |
||
53 | else if (i>=440) angle = 84; |
||
54 | else if (i>=380) angle = 83; |
||
55 | else if (i>=335) angle = 82; |
||
56 | else if (i>=299) angle = 81; |
||
57 | else angle = 80; // (i>=270) |
||
58 | |||
59 | if (x > 0) return (angle*m); // Quadrant I und IV |
||
60 | else if ((x < 0) && (y >= 0)) return ((angle*-1) + 180); // Quadrant II |
||
61 | else return (angle - 180); // x < 0 && y < 0 Quadrant III |
||
62 | } |
||
63 | |||
64 | |||
65 | //Sinusfunktion von 0° bis 359°. Ergebnis wurde mit 1000 multipliziert, damit als Definition platzsparend int |
||
66 | //verwendet werden kann. d.h., es muss also in der späteren Berechnung noch mit 0,001 multipliziert werden //(090907Kr) |
||
67 | const int c_sin[] = {0,17,34,52,69,87,104,121,139,156,173,190, |
||
68 | 207,224,241,258,275,292,309,325,342,358,374,390,406,422,438,453,469,484,500,515,529,544,559,573,587,601,615,629, |
||
69 | 642,656,669,682,694,707,719,731,743,754,766,777,788,798,809,819,829,838,848,857,866,874,882,891,898,906,913,920, |
||
70 | 927,933,939,945,951,956,961,965,970,974,978,981,984,987,990,992,994,996,997,998,999,999,1000,999,999,998,997,996, |
||
71 | 994,992,990,987,984,981,978,974,970,965,961,956,951,945,939,933,927,920,913,906,898,891,882,874,866,857,848,838, |
||
72 | 829,819,809,798,788,777,766,754,743,731,719,707,694,682,669,656,642,629,615,601,587,573,559,544,529,515,500,484, |
||
73 | 469,453,438,422,406,390,374,358,342,325,309,292,275,258,241,224,207,190,173,156,139,121,104,87,69,52,34,17,0,-17, |
||
74 | -34,-52,-69,-87,-104,-121,-139,-156,-173,-190,-207,-224,-241,-258,-275,-292,-309,-325,-342,-358,-374,-390,-406, |
||
75 | -422,-438,-453,-469,-484,-500,-515,-529,-544,-559,-573,-587,-601,-615,-629,-642,-656,-669,-682,-694,-707,-719, |
||
76 | -731,-743,-754,-766,-777,-788,-798,-809,-819,-829,-838,-848,-857,-866,-874,-882,-891,-898,-906,-913,-920,-927, |
||
77 | -933,-939,-945,-951,-956,-961,-965,-970,-974,-978,-981,-984,-987,-990,-992,-994,-996,-997,-998,-999,-999,-1000, |
||
78 | -999,-999,-998,-997,-996,-994,-992,-990,-987,-984,-981,-978,-974,-970,-965,-961,-956,-951,-945,-939,-933,-927, |
||
79 | -920,-913,-906,-898,-891,-882,-874,-866,-857,-848,-838,-829,-819,-809,-798,-788,-777,-766,-754,-743,-731,-719, |
||
80 | -707,-694,-682,-669,-656,-642,-629,-615,-601,-587,-573,-559,-544,-529,-515,-500,-484,-469,-453,-438,-422,-406, |
||
81 | -390,-374,-358,-342,-325,-309,-292,-275,-258,-241,-224,-207,-190,-173,-156,-139,-121,-104,-87,-69,-52,-34,-17}; |
||
82 | |||
83 | //Cosinusfunktion von 0° bis 359°. Ergebnis wurde mit 1000 multipliziert, damit als Definition platzsparend int |
||
84 | //verwendet werden kann. D.h., es muss also in der späteren Berechnung noch mit 0,001 multipliziert werden //(090907Kr) |
||
85 | const int c_cos[] = {1000,999,999,998,997,996,994,992,990,987,984,981, |
||
86 | 978,974,970,965,961,956,951,945,939,933,927,920,913,906,898,891,882,874,866,857,848,838,829,819,809,798,788,777, |
||
87 | 766,754,743,731,719,707,694,682,669,656,642,629,615,601,587,573,559,544,529,515,500,484,469,453,438,422,406,390, |
||
88 | 374,358,342,325,309,292,275,258,241,224,207,190,173,156,139,121,104,87,69,52,34,17,0,-17,-34,-52,-69,-87,-104, |
||
89 | -121,-139,-156,-173,-190,-207,-224,-241,-258,-275,-292,-309,-325,-342,-358,-374,-390,-406,-422,-438,-453,-469, |
||
90 | -484,-500,-515,-529,-544,-559,-573,-587,-601,-615,-629,-642,-656,-669,-682,-694,-707,-719,-731,-743,-754,-766, |
||
91 | -777,-788,-798,-809,-819,-829,-838,-848,-857,-866,-874,-882,-891,-898,-906,-913,-920,-927,-933,-939,-945,-951, |
||
92 | -956,-961,-965,-970,-974,-978,-981,-984,-987,-990,-992,-994,-996,-997,-998,-999,-999,-1000,-999,-999,-998,-997, |
||
93 | -996,-994,-992,-990,-987,-984,-981,-978,-974,-970,-965,-961,-956,-951,-945,-939,-933,-927,-920,-913,-906,-898, |
||
94 | -891,-882,-874,-866,-857,-848,-838,-829,-819,-809,-798,-788,-777,-766,-754,-743,-731,-719,-707,-694,-682,-669, |
||
95 | -656,-642,-629,-615,-601,-587,-573,-559,-544,-529,-515,-500,-484,-469,-453,-438,-422,-406,-390,-374,-358,-342, |
||
96 | -325,-309,-292,-275,-258,-241,-224,-207,-190,-173,-156,-139,-121,-104,-87,-69,-52,-34,-17,0,17,34,52,69,87, |
||
97 | 104,121,139,156,173,190,207,224,241,258,275,292,309,325,342,358,374,390,406,422,438,453,469,484,500,515,529,544, |
||
98 | 559,573,587,601,615,629,642,656,669,682,694,707,719,731,743,754,766,777,788,798,809,819,829,838,848,857,866,874, |
||
99 | 882,891,898,906,913,920,927,933,939,945,951,956,961,965,970,974,978,981,984,987,990,992,994,996,997,998,999,999}; |
||
100 | |||
101 | |||
102 | |||
103 | const float pgm_sinus_f [91] PROGMEM = {0.000,0.017,0.035,0.052,0.070,0.087,0.105, |
||
104 | 0.122,0.139,0.156,0.174,0.191,0.208,0.225,0.242,0.259,0.276,0.292,0.309,0.326, |
||
105 | 0.342,0.358,0.375,0.391,0.407,0.423,0.438,0.454,0.469,0.485,0.500,0.515,0.530, |
||
106 | 0.545,0.559,0.574,0.588,0.602,0.616,0.629,0.643,0.656,0.669,0.682,0.695,0.707, |
||
107 | 0.719,0.731,0.743,0.755,0.766,0.777,0.788,0.799,0.809,0.819,0.829,0.839,0.848, |
||
108 | 0.857,0.866,0.875,0.883,0.891,0.899,0.906,0.914,0.921,0.927,0.934,0.940,0.946, |
||
109 | 0.951,0.956,0.961,0.966,0.970,0.974,0.978,0.982,0.985,0.988,0.990,0.993,0.995, |
||
110 | 0.996,0.998,0.999,0.999,1.000,1.000}; |
||
111 | |||
112 | inline float pgm_read_float(const float *addr) |
||
113 | { |
||
114 | union |
||
115 | { |
||
116 | uint16_t i[2]; // 2 16-bit-Worte |
||
117 | float f; |
||
118 | } u; |
||
119 | |||
120 | u.i[0]=pgm_read_word((PGM_P)addr); |
||
121 | u.i[1]=pgm_read_word((PGM_P)addr+2); |
||
122 | |||
123 | return u.f; |
||
124 | } |
||
125 | |||
126 | // Kosinusfunktion im Gradmaß |
||
127 | float cos_f(signed int winkel) |
||
128 | { |
||
129 | return (sin_f(90-winkel)); |
||
130 | } |
||
131 | |||
132 | // Sinusfunktion im Gradmaß |
||
133 | float sin_f(signed int winkel) |
||
134 | { |
||
135 | short int m,n; |
||
136 | float sinus; |
||
137 | |||
138 | //winkel = winkel % 360; |
||
139 | |||
140 | if (winkel < 0) |
||
141 | { |
||
142 | m = -1; |
||
143 | winkel = abs(winkel); |
||
144 | } |
||
145 | else m = +1; |
||
146 | |||
147 | // Quadranten auswerten |
||
148 | if ((winkel > 90 ) && (winkel <= 180)) |
||
149 | {winkel = 180 - winkel; n = 1;} |
||
150 | else if ((winkel > 180 ) && (winkel <= 270)) |
||
151 | {winkel = winkel - 180; n = -1;} |
||
152 | else if ((winkel > 270) && (winkel <= 360)) |
||
153 | {winkel = 360 - winkel; n = -1;} |
||
154 | else |
||
155 | n = 1; //0 - 90 Grad |
||
156 | |||
157 | |||
158 | sinus = pgm_read_float(&pgm_sinus_f[winkel]); |
||
159 | |||
160 | return (sinus*m*n); |
||
161 | } |
||
162 | |||
163 | |||
164 | 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, |
||
165 | 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, |
||
166 | 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, |
||
167 | 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, |
||
168 | 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, |
||
169 | 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, |
||
170 | 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, |
||
171 | 69,70,71,72,73,74,75,76,77,79,80,82,84,90}; |
||
172 | |||
173 | // Akurssinusfunktion im Gradmaß |
||
174 | int8_t asin_i(signed int i) |
||
175 | { |
||
176 | signed char m; |
||
177 | |||
178 | if (i < 0) {m=-1;i=abs(i);} |
||
179 | else m=1; |
||
180 | |||
181 | return (pgm_read_byte(&pgm_asin[i]) * m); |
||
182 | } |