Rev 242 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 242 | Rev 258 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | This program (files math.c and math.h) is free software; you can redistribute it and/or modify |
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 Lesser General Public License as published by the Free Software Foundation; |
3 | it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; |
4 | either version 3 of the License, or (at your option) any later version. |
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; |
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 |
6 | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
7 | GNU General Public License and GNU Lesser General Public License for more details. |
7 | GNU General Public License and GNU Lesser General Public License for more details. |
8 | You should have received a copy of GNU General Public License ((License_GPL.txt) and |
8 | You should have received a copy of GNU General Public License (License_GPL.txt) and |
9 | GNU Lesser General Public License (License_LGPL.txt) along with this program. |
9 | GNU Lesser General Public License (License_LGPL.txt) along with this program. |
10 | If not, see <http://www.gnu.org/licenses/>. |
10 | If not, see <http://www.gnu.org/licenses/>. |
11 | 11 | ||
12 | 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 |
12 | 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 |
13 | */ |
13 | */ |
14 | /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
14 | /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
15 | Peter Muehlenbrock |
15 | Peter Muehlenbrock |
16 | Winkelfunktionen sin, cos und arctan in |
16 | Winkelfunktionen sin, cos und arctan in |
17 | brute-force Art: Sehr Schnell, nicht sonderlich genau, aber ausreichend |
17 | brute-force Art: Sehr Schnell, nicht sonderlich genau, aber ausreichend |
18 | Sinus Funktion von Nick666 vereinfacht |
18 | get_dist Funktion fuer Entfernungsermittlung |
19 | Stand 1.10.2007 |
19 | Stand 1.10.2007 |
20 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
20 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
21 | */ |
21 | */ |
22 | #include "main.h" |
22 | #include "main.h" |
23 | 23 | ||
24 | 24 | ||
25 | // arctan Funktion: Eingabewert x,y Rueckgabe =arctan(x,y) in grad |
25 | // arctan Funktion: Eingabewert x,y Rueckgabe =arctan(x,y) in grad |
26 | int arctan_i( signed int x, signed int y) |
26 | int arctan_i( signed int x, signed int y) |
27 | { |
27 | { |
28 | short int change_xy = 0; |
28 | short int change_xy = 0; |
29 | signed int i; |
29 | signed int i; |
30 | long signed int wert; |
30 | long signed int wert; |
31 | int return_value; |
31 | int return_value; |
32 | 32 | ||
33 | if ((abs(x)) > (abs(y))) // x,y Werte vertauschen damit arctan <45 grad bleibt |
33 | if ((abs(x)) > (abs(y))) // x,y Werte vertauschen damit arctan <45 grad bleibt |
34 | { |
34 | { |
35 | change_xy = 1; |
35 | change_xy = 1; |
36 | i = x; |
36 | i = x; |
37 | x = y; |
37 | x = y; |
38 | y = i; |
38 | y = i; |
39 | } |
39 | } |
40 | 40 | ||
41 | // Quadranten ermitteln |
41 | // Quadranten ermitteln |
42 | 42 | ||
43 | // Wert durch lineare Interpolation ermitteln |
43 | // Wert durch lineare Interpolation ermitteln |
44 | if ((y == 0) && (x == 0)) wert =1; // Division durch 0 nicht erlaubt |
44 | if ((y == 0) && (x == 0)) wert =1; // Division durch 0 nicht erlaubt |
45 | else wert= abs((x*1000)/y); |
45 | else wert= abs(((long) x*1000)/((long)y)); |
46 | 46 | ||
47 | if (wert <=268) //0...0.0,268 entsprechend 0..15 Grad |
47 | if (wert <=268) //0...0.0,268 entsprechend 0..15 Grad |
48 | { |
48 | { |
49 | return_value = (signed int)((wert*100)/(268-0)*(15-0)/100) +0; |
49 | return_value = (signed int)((wert*100)/(268-0)*(15-0)/100) +0; |
50 | } |
50 | } |
51 | else if (wert <=578) //0,268...0.0,568 entsprechend 15..30 Grad |
51 | else if (wert <=578) //0,268...0.0,568 entsprechend 15..30 Grad |
52 | { |
52 | { |
53 | return_value = (signed int)((((wert-268)*100)/(578-268)*(30-15))/100) +15; |
53 | return_value = (signed int)((((wert-268)*100)/(578-268)*(30-15))/100) +15; |
54 | } |
54 | } |
55 | else //0,568...1 entsprechend 30..45 Grad |
55 | else //0,568...1 entsprechend 30..45 Grad |
56 | { |
56 | { |
57 | return_value = (signed int)((((wert-578)*50)/(1000-578)*(45-30))/50) +30; |
57 | return_value = (signed int)((((wert-578)*50)/(1000-578)*(45-30))/50) +30; |
58 | } |
58 | } |
59 | 59 | ||
60 | if (change_xy == 0) return_value = 90-return_value; //Quadrant 45..90 Grad |
60 | if (change_xy == 0) return_value = 90-return_value; //Quadrant 45..90 Grad |
61 | if ((x >= 0) && (y <0)) return_value = - return_value; |
61 | if ((x >= 0) && (y <0)) return_value = - return_value; |
62 | else if ((x < 0) && (y >= 0)) return_value = - return_value; |
62 | else if ((x < 0) && (y >= 0)) return_value = - return_value; |
63 | 63 | ||
64 | return return_value; |
64 | return return_value; |
65 | } |
65 | } |
66 | 66 | ||
67 | 67 | ||
68 | // cosinus Funktion: Eingabewert Winkel in Grad, Rueckgabe =cos(winkel)*1000 |
68 | // cosinus Funktion: Eingabewert Winkel in Grad, Rueckgabe =cos(winkel)*1000 |
69 | signed int cos_i(signed int winkel) |
69 | signed int cos_i(signed int winkel) |
70 | { |
70 | { |
71 | winkel = sin_i(90-winkel); |
71 | winkel = sin_i(90-winkel); |
72 | return winkel; |
72 | return winkel; |
73 | } |
73 | } |
74 | 74 | ||
75 | 75 | ||
76 | 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}; |
76 | 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}; |
77 | //von Nick666, Stand 28.9.2007 |
77 | //von Nick666, Stand 28.9.2007 |
78 | // sinus Funktion: Eingabewert Winkel in Grad, Rueckgabe =sin(winkel)*1000 |
78 | // sinus Funktion: Eingabewert Winkel in Grad, Rueckgabe =sin(winkel)*1000 |
79 | signed int sin_i(signed int winkel) |
79 | signed int sin_i(signed int winkel) |
80 | { |
80 | { |
81 | short int m,n; |
81 | short int m,n; |
82 | 82 | ||
83 | if (abs(winkel) >=360) winkel = winkel % 360; |
83 | if (abs(winkel) >=360) winkel = winkel % 360; |
84 | if (winkel < 0) |
84 | if (winkel < 0) |
85 | { |
85 | { |
86 | m = -1; |
86 | m = -1; |
87 | winkel = abs(winkel); |
87 | winkel = abs(winkel); |
88 | } |
88 | } |
89 | else m = +1; |
89 | else m = +1; |
90 | n =1; |
90 | n =1; |
91 | 91 | ||
92 | // Quadranten auswerten |
92 | // Quadranten auswerten |
93 | if ((winkel > 90 ) && (winkel <= 180)) winkel = 180 - winkel; |
93 | if ((winkel > 90 ) && (winkel <= 180)) winkel = 180 - winkel; |
94 | else if ((winkel > 180 ) && (winkel <= 270)) |
94 | else if ((winkel > 180 ) && (winkel <= 270)) |
95 | { |
95 | { |
96 | winkel = winkel -180; |
96 | winkel = winkel -180; |
97 | n = -1; |
97 | n = -1; |
98 | } |
98 | } |
99 | else if ((winkel > 270) && (winkel <= 360)) |
99 | else if ((winkel > 270) && (winkel <= 360)) |
100 | { |
100 | { |
101 | winkel = 360 - winkel; |
101 | winkel = 360 - winkel; |
102 | n = -1; |
102 | n = -1; |
103 | } |
103 | } |
104 | // else //0 - 90 Grad |
104 | // else //0 - 90 Grad |
105 | 105 | ||
106 | winkel = pgm_read_word(&pgm_sinus[winkel]); |
106 | winkel = pgm_read_word(&pgm_sinus[winkel]); |
107 | return (winkel*m*n); |
107 | return (winkel*m*n); |
108 | } |
108 | } |
109 | 109 | ||
110 | // Aus x,y und Winkel Distanz ermitteln |
110 | // Aus x,y und Winkel Distanz ermitteln |
111 | long get_dist(signed int x, signed int y, signed int phi) |
111 | int get_dist(signed int x, signed int y, signed int phi) |
112 | { |
112 | { |
113 | long dist; |
113 | long dist; |
114 | if (abs(x) > abs(y) ) |
114 | if (abs(x) > abs(y) ) |
115 | { |
115 | { |
116 | dist = (long) x; //Groesseren Wert wegen besserer Genauigkeit nehmen |
116 | dist = (long) x; //Groesseren Wert wegen besserer Genauigkeit nehmen |
117 | dist = abs((dist *1000) / (long) sin_i(phi)); |
117 | dist = abs((dist *1000) / (long) sin_i(phi)); |
118 | } |
118 | } |
119 | else |
119 | else |
120 | { |
120 | { |
121 | dist = (long) y; |
121 | dist = (long) y; |
122 | dist = abs((dist *1000) / (long) cos_i(phi)); |
122 | dist = abs((dist *1000) / (long) cos_i(phi)); |
123 | } |
123 | } |
124 | return dist; |
124 | return (int)dist; |
125 | } |
125 | } |
126 | 126 | ||
127 | 127 |