Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
205 | 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 | //----------------------------------------------- |
||
226 | Nick666 | 16 | // Fast arctan2 with max error of .07 rads |
205 | Nick666 | 17 | // http://www.dspguru.com/comp.dsp/tricks/alg/fxdatan2.htm |
18 | //----------------------------------------------- |
||
226 | Nick666 | 19 | signed char arctan_f(signed int x, signed int y) |
205 | Nick666 | 20 | { |
226 | Nick666 | 21 | float rad,r; |
22 | short int m; |
||
23 | #define coeff_1 0.7854 |
||
24 | #define coeff_2 2.3562 |
||
25 | #define rad2grad 57.2958 |
||
205 | Nick666 | 26 | |
27 | if (!x && !y) return 0; |
||
28 | |||
29 | if (y < 0) {y = abs(y); m = -1;} |
||
30 | else m = 1; |
||
31 | |||
32 | if (x>=0) |
||
33 | { |
||
226 | Nick666 | 34 | r = (float)(x - y) / (float)(x + y); |
35 | rad = coeff_1 - coeff_1*r; |
||
205 | Nick666 | 36 | } |
37 | else |
||
38 | { |
||
226 | Nick666 | 39 | r = (float)(x + y) / (float)(y - x); |
40 | rad = coeff_2 - coeff_1*r; |
||
205 | Nick666 | 41 | } |
226 | Nick666 | 42 | |
43 | rad *= rad2grad; |
||
44 | |||
214 | Nick666 | 45 | return(rad*m); |
205 | Nick666 | 46 | } |
47 | |||
214 | Nick666 | 48 | /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
49 | Peter Muehlenbrock |
||
50 | arctan in brute-force Art |
||
51 | Stand 1.10.2007 |
||
52 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
53 | */ |
||
54 | // arctan Funktion: Eingabewert x,y Rueckgabe =arctan(x,y) in grad |
||
226 | Nick666 | 55 | signed int arctan_i(signed int x, signed int y) |
214 | Nick666 | 56 | { |
57 | short int change_xy = 0; |
||
58 | signed int i; |
||
59 | long signed int wert; |
||
60 | int return_value; |
||
61 | |||
62 | if ((abs(x)) > (abs(y))) // x,y Werte vertauschen damit arctan <45 grad bleibt |
||
63 | { |
||
64 | change_xy = 1; |
||
65 | i = x; |
||
66 | x = y; |
||
67 | y = i; |
||
68 | } |
||
69 | |||
70 | // Quadranten ermitteln |
||
71 | |||
72 | // Wert durch lineare Interpolation ermitteln |
||
226 | Nick666 | 73 | if (!y && !x) return 0; // Division durch 0 nicht erlaubt |
74 | |||
214 | Nick666 | 75 | else wert= abs((x*1000)/y); |
76 | |||
77 | if (wert <=268) //0...0.0,268 entsprechend 0..15 Grad |
||
78 | { |
||
79 | return_value = (signed int)((wert*100)/(268-0)*(15-0)/100) +0; |
||
80 | } |
||
81 | else if (wert <=578) //0,268...0.0,568 entsprechend 15..30 Grad |
||
82 | { |
||
83 | return_value = (signed int)((((wert-268)*100)/(578-268)*(30-15))/100) +15; |
||
84 | } |
||
85 | else //0,568...1 entsprechend 30..45 Grad |
||
86 | { |
||
87 | return_value = (signed int)((((wert-578)*50)/(1000-578)*(45-30))/50) +30; |
||
88 | } |
||
89 | |||
90 | if (change_xy == 0) return_value = 90-return_value; //Quadrant 45..90 Grad |
||
91 | if ((x >= 0) && (y <0)) return_value = - return_value; |
||
92 | else if ((x < 0) && (y >= 0)) return_value = - return_value; |
||
93 | |||
94 | return return_value; |
||
95 | } |
||
96 | |||
226 | Nick666 | 97 | |
98 | |||
99 | 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}; |
||
100 | |||
205 | Nick666 | 101 | // cosinus Funktion: Eingabewert Winkel in Grad, Rueckgabe =cos(winkel)*1000 |
102 | signed int cos_i(signed int winkel) |
||
226 | Nick666 | 103 | { |
104 | return (sin_i(90-winkel)); |
||
205 | Nick666 | 105 | } |
106 | |||
107 | // sinus Funktion: Eingabewert Winkel in Grad, Rueckgabe =sin(winkel)*1000 |
||
108 | signed int sin_i(signed int winkel) |
||
109 | { |
||
110 | short int m,n; |
||
111 | |||
112 | //winkel = winkel % 360; |
||
113 | |||
114 | if (winkel < 0) |
||
115 | { |
||
116 | m = -1; |
||
117 | winkel = abs(winkel); |
||
118 | } |
||
119 | else m = +1; |
||
120 | |||
121 | // Quadranten auswerten |
||
122 | if ((winkel > 90 ) && (winkel <= 180)) {winkel = 180 - winkel; n = 1;} |
||
123 | else if ((winkel > 180 ) && (winkel <= 270)) {winkel = winkel - 180; n = -1;} |
||
124 | else if ((winkel > 270) && (winkel <= 360)) {winkel = 360 - winkel; n = -1;} |
||
125 | else n = 1; //0 - 90 Grad |
||
126 | |||
127 | winkel = pgm_read_word(&pgm_sinus[winkel]); |
||
128 | |||
129 | return (winkel*m*n); |
||
130 | } |
||
131 | |||
132 | |||
226 | Nick666 | 133 | |
134 | const float pgm_sinus_f [91] PROGMEM = {0.00,0.02,0.03,0.05,0.07,0.09,0.10,0.12,0.14,0.16,0.17,0.19,0.21,0.23,0.24,0.26,0.28,0.29,0.31,0.33,0.34,0.36,0.38,0.39,0.41,0.42,0.44,0.45,0.47,0.49,0.50,0.52,0.53,0.55,0.56,0.57,0.59,0.60,0.62,0.63,0.64,0.66,0.67,0.68,0.70,0.71,0.72,0.73,0.74,0.76,0.77,0.78,0.79,0.80,0.81,0.82,0.83,0.84,0.85,0.86,0.87,0.88,0.88,0.89,0.90,0.91,0.91,0.92,0.93,0.93,0.94,0.95,0.95,0.96,0.96,0.97,0.97,0.97,0.98,0.98,0.98,0.99,0.99,0.99,0.99,1.00,1.00,1.00,1.00,1.00,1.00}; |
||
135 | |||
136 | inline float pgm_read_float(const float *addr) |
||
137 | { |
||
138 | union |
||
139 | { |
||
140 | uint16_t i[2]; // 2 16-bit-Worte |
||
141 | float f; |
||
142 | } u; |
||
143 | |||
144 | u.i[0]=pgm_read_word((PGM_P)addr); |
||
145 | u.i[1]=pgm_read_word((PGM_P)addr+2); |
||
146 | |||
147 | return u.f; |
||
148 | } |
||
149 | |||
150 | // cosinus Funktion: Eingabewert Winkel in Grad |
||
151 | float cos_f(signed int winkel) |
||
152 | { |
||
153 | return (sin_f(90-winkel)); |
||
154 | } |
||
155 | |||
156 | // sinus Funktion: Eingabewert Winkel in Grad |
||
157 | float sin_f(signed int winkel) |
||
158 | { |
||
159 | short int m,n; |
||
160 | float sinus; |
||
161 | |||
162 | //winkel = winkel % 360; |
||
163 | |||
164 | if (winkel < 0) |
||
165 | { |
||
166 | m = -1; |
||
167 | winkel = abs(winkel); |
||
168 | } |
||
169 | else m = +1; |
||
170 | |||
171 | // Quadranten auswerten |
||
172 | if ((winkel > 90 ) && (winkel <= 180)) {winkel = 180 - winkel; n = 1;} |
||
173 | else if ((winkel > 180 ) && (winkel <= 270)) {winkel = winkel - 180; n = -1;} |
||
174 | else if ((winkel > 270) && (winkel <= 360)) {winkel = 360 - winkel; n = -1;} |
||
175 | else n = 1; //0 - 90 Grad |
||
176 | |||
177 | sinus = pgm_read_float(&pgm_sinus_f[winkel]); |
||
178 | |||
179 | return (sinus*m*n); |
||
180 | } |