Rev 265 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
205 | Nick666 | 1 | /* |
256 | Nick666 | 2 | |
3 | Copyright 2007, Niklas Nold |
||
4 | |||
205 | Nick666 | 5 | This program (files math.c and math.h) is free software; you can redistribute it and/or modify |
256 | Nick666 | 6 | it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; |
205 | Nick666 | 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 |
||
256 | Nick666 | 10 | GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License |
205 | Nick666 | 11 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
12 | |||
257 | Nick666 | 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 |
205 | Nick666 | 14 | */ |
15 | |||
16 | #include "main.h" |
||
17 | |||
18 | |||
272 | Nick666 | 19 | const uint8_t pgm_atan[540] PROGMEM = {0,1,1,2,2,3,3,4,5,5,6,6,7,7,8,9,9,10,10,11,11,12,12,13,13,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,22,22,23,23,24,24,25,25,26,26,27,27,27,28,28,29,29,30,30,31,31,31,32,32,33,33,33,34,34,35,35,35,36,36,37,37,37,38,38,38,39,39,39,40,40,40,41,41,41,42,42,42,43,43,43,44,44,44,44,45,45,45,46,46,46,46,47,47,47,47,48,48,48,48,49,49,49,49,50,50,50,50,51,51,51,51,52,52,52,52,52,53,53,53,53,53,54,54,54,54,54,55,55,55,55,55,56,56,56,56,56,56,57,57,57,57,57,58,58,58,58,58,58,58,59,59,59,59,59,59,60,60,60,60,60,60,60,61,61,61,61,61,61,61,61,62,62,62,62,62,62,62,62,63,63,63,63,63,63,63,63,64,64,64,64,64,64,64,64,64,65,65,65,65,65,65,65,65,65,65,66,66,66,66,66,66,66,66,66,66,67,67,67,67,67,67,67,67,67,67,67,67,68,68,68,68,68,68,68,68,68,68,68,68,69,69,69,69,69,69,69,69,69,69,69,69,69,69,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,71,71,71,71,71,71,71,71,71,71,71,71,71,71,71,71,72,72,72,72,72,72,72,72,72,72,72,72,72,72,72,72,72,72,72,73,73,73,73,73,73,73,73,73,73,73,73,73,73,73,73,73,73,73,73,74,74,74,74,74,74,74,74,74,74,74,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,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,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,77,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,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,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79}; |
232 | Nick666 | 20 | |
272 | Nick666 | 21 | // Arkustangens2 im Gradmaß |
22 | signed int atan2_i(signed int x, signed int y) |
||
214 | Nick666 | 23 | { |
232 | Nick666 | 24 | int i,angle; |
25 | int8_t m; |
||
214 | Nick666 | 26 | |
232 | Nick666 | 27 | if (!x && !y) return 0; //atan2 = 0 für x und y = 0 |
28 | |||
29 | if (y < 0) m=-1; |
||
30 | else m=1; |
||
31 | |||
32 | if (x==0) return (90*m); // atan2 = 90° für x = 0 |
||
33 | |||
272 | Nick666 | 34 | i = abs(((double)y / x) * 100); // Berechne i für die Lookup table (Schrittweite atan(x) ist 0.01 -> *100) |
214 | Nick666 | 35 | |
272 | Nick666 | 36 | if (i<540) angle = pgm_read_byte(&pgm_atan[i]); // Lookup für 1° bis 79° |
37 | else if (i>11458) angle = 90; // Grenzwert ist 90° |
||
38 | else if (i>3818) angle = 89; // 89° bis 80° über Wertebereiche |
||
39 | else if (i>2290) angle = 88; |
||
40 | else if (i>1634) angle = 87; |
||
41 | else if (i>1270) angle = 86; |
||
42 | else if (i>1038) angle = 85; |
||
43 | else if (i>877) angle = 84; |
||
44 | else if (i>759) angle = 83; |
||
45 | else if (i>669) angle = 82; |
||
46 | else if (i>597) angle = 81; |
||
47 | else angle = 80; // (i>539) |
||
226 | Nick666 | 48 | |
232 | Nick666 | 49 | if (x > 0) return (angle*m); // Quadrant I und IV |
50 | else if ((x < 0) && (y >= 0)) return ((angle*-1) + 180); // Quadrant II |
||
51 | else return (angle - 180); // x < 0 && y < 0 Quadrant III |
||
214 | Nick666 | 52 | } |
53 | |||
226 | Nick666 | 54 | |
55 | |||
56 | |||
229 | Nick666 | 57 | |
58 | const float pgm_sinus_f [91] PROGMEM = {0.000,0.017,0.035,0.052,0.070,0.087,0.105,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,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,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,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,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,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,0.996,0.998,0.999,0.999,1.000,1.000}; |
||
59 | |||
60 | inline float pgm_read_float(const float *addr) |
||
61 | { |
||
62 | union |
||
63 | { |
||
64 | uint16_t i[2]; // 2 16-bit-Worte |
||
65 | float f; |
||
66 | } u; |
||
67 | |||
68 | u.i[0]=pgm_read_word((PGM_P)addr); |
||
69 | u.i[1]=pgm_read_word((PGM_P)addr+2); |
||
70 | |||
71 | return u.f; |
||
72 | } |
||
73 | |||
272 | Nick666 | 74 | |
75 | // Kosinusfunktion im Gradmaß |
||
76 | float cos_f(signed int alpha) |
||
229 | Nick666 | 77 | { |
272 | Nick666 | 78 | return (sin_f(90-alpha)); |
205 | Nick666 | 79 | } |
80 | |||
272 | Nick666 | 81 | |
82 | // Sinusfunktion im Gradmaß |
||
83 | float sin_f(signed int alpha) |
||
205 | Nick666 | 84 | { |
85 | short int m,n; |
||
229 | Nick666 | 86 | float sinus; |
205 | Nick666 | 87 | |
272 | Nick666 | 88 | //alpha = alpha % 360; |
205 | Nick666 | 89 | |
272 | Nick666 | 90 | if (alpha < 0) |
205 | Nick666 | 91 | { |
92 | m = -1; |
||
272 | Nick666 | 93 | alpha = abs(alpha); |
205 | Nick666 | 94 | } |
95 | else m = +1; |
||
96 | |||
97 | // Quadranten auswerten |
||
272 | Nick666 | 98 | if ((alpha > 90 ) && (alpha <= 180)) {alpha = 180 - alpha; n = 1;} |
99 | else if ((alpha > 180 ) && (alpha <= 270)) {alpha = alpha - 180; n = -1;} |
||
100 | else if ((alpha > 270) && (alpha <= 360)) {alpha = 360 - alpha; n = -1;} |
||
205 | Nick666 | 101 | else n = 1; //0 - 90 Grad |
102 | |||
272 | Nick666 | 103 | sinus = pgm_read_float(&pgm_sinus_f[alpha]); |
205 | Nick666 | 104 | |
229 | Nick666 | 105 | return (sinus*m*n); |
205 | Nick666 | 106 | } |
265 | Nick666 | 107 | |
108 | |||
109 | 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}; |
||
110 | |||
111 | |||
272 | Nick666 | 112 | // Arkussinusfunktion im Gradmaß |
265 | Nick666 | 113 | signed char asin_i(signed int i) |
114 | { |
||
115 | signed char m; |
||
116 | |||
117 | if (i < 0) {m=-1;i=abs(i);} |
||
118 | else m=1; |
||
119 | |||
120 | return (pgm_read_byte(&pgm_asin[i]) * m); |
||
121 | } |