Rev 528 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 528 | Rev 529 | ||
---|---|---|---|
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 | get_dist Funktion fuer Entfernungsermittlung |
18 | get_dist Funktion fuer Entfernungsermittlung |
19 | Stand 12.10.2007 |
19 | Stand 12.10.2007 |
20 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
20 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
21 | */ |
21 | */ |
22 | #include "main.h" |
22 | #include "main.h" |
- | 23 | #include "math.h" |
|
23 | 24 | ||
24 | 25 | ||
25 | // arctan Funktion: Eingabewert x,y Rueckgabe =arctan(x,y) in grad |
26 | // arctan Funktion: Eingabewert x,y Rueckgabe =arctan(x,y) in grad |
26 | int arctan_i( signed int x, signed int y) |
27 | int arctan_i( signed int x, signed int y) |
27 | { |
28 | { |
28 | short int change_xy = 0; |
29 | short int change_xy = 0; |
29 | signed int i; |
30 | signed int i; |
30 | long signed int wert; |
31 | long signed int wert; |
31 | int return_value; |
32 | int return_value; |
32 | 33 | ||
33 | if ((abs(x)) > (abs(y))) // x,y Werte vertauschen damit arctan <45 grad bleibt |
34 | if ((abs(x)) > (abs(y))) // x,y Werte vertauschen damit arctan <45 grad bleibt |
34 | { |
35 | { |
35 | change_xy = 1; |
36 | change_xy = 1; |
36 | i = x; |
37 | i = x; |
37 | x = y; |
38 | x = y; |
38 | y = i; |
39 | y = i; |
39 | } |
40 | } |
40 | 41 | ||
41 | // Quadranten ermitteln |
42 | // Quadranten ermitteln |
42 | 43 | ||
43 | // Wert durch lineare Interpolation ermitteln |
44 | // Wert durch lineare Interpolation ermitteln |
44 | if ((y == 0) && (x == 0)) wert =1; // Division durch 0 nicht erlaubt |
45 | if ((y == 0) && (x == 0)) wert =1; // Division durch 0 nicht erlaubt |
45 | else wert= abs(((long)x*1000)/((long)y)); |
46 | else wert= abs(((long)x*1000)/((long)y)); |
46 | 47 | ||
47 | if (wert <=268) //0...0.0,268 entsprechend 0..15 Grad |
48 | if (wert <=268) //0...0.0,268 entsprechend 0..15 Grad |
48 | { |
49 | { |
49 | return_value = (signed int)((wert*100)/(268-0)*(15-0)/100) +0; |
50 | return_value = (signed int)((wert*100)/(268-0)*(15-0)/100) +0; |
50 | } |
51 | } |
51 | else if (wert <=578) //0,268...0.0,568 entsprechend 15..30 Grad |
52 | else if (wert <=578) //0,268...0.0,568 entsprechend 15..30 Grad |
52 | { |
53 | { |
53 | return_value = (signed int)((((wert-268)*100)/(578-268)*(30-15))/100) +15; |
54 | return_value = (signed int)((((wert-268)*100)/(578-268)*(30-15))/100) +15; |
54 | } |
55 | } |
55 | else //0,568...1 entsprechend 30..45 Grad |
56 | else //0,568...1 entsprechend 30..45 Grad |
56 | { |
57 | { |
57 | return_value = (signed int)((((wert-578)*50)/(1000-578)*(45-30))/50) +30; |
58 | return_value = (signed int)((((wert-578)*50)/(1000-578)*(45-30))/50) +30; |
58 | } |
59 | } |
59 | 60 | ||
60 | if (change_xy == 0) return_value = 90-return_value; //Quadrant 45..90 Grad |
61 | if (change_xy == 0) return_value = 90-return_value; //Quadrant 45..90 Grad |
61 | if ((x >= 0) && (y <0)) return_value = - return_value; |
62 | if ((x >= 0) && (y <0)) return_value = - return_value; |
62 | else if ((x < 0) && (y >= 0)) return_value = - return_value; |
63 | else if ((x < 0) && (y >= 0)) return_value = - return_value; |
63 | 64 | ||
64 | return return_value; |
65 | return return_value; |
65 | } |
66 | } |
66 | 67 | ||
67 | 68 | ||
68 | // cosinus Funktion: Eingabewert Winkel in Grad, Rueckgabe =cos(winkel)*1000 |
69 | // cosinus Funktion: Eingabewert Winkel in Grad, Rueckgabe =cos(winkel)*1000 |
69 | signed int cos_i(signed int winkel) |
70 | signed int cos_i(signed int winkel) |
70 | { |
71 | { |
71 | winkel = sin_i(90-winkel); |
72 | winkel = sin_i(90-winkel); |
72 | return winkel; |
73 | return winkel; |
73 | } |
74 | } |
74 | 75 | ||
75 | 76 | ||
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 | 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 |
78 | //von Nick666, Stand 28.9.2007 |
78 | // sinus Funktion: Eingabewert Winkel in Grad, Rueckgabe =sin(winkel)*1000 |
79 | // sinus Funktion: Eingabewert Winkel in Grad, Rueckgabe =sin(winkel)*1000 |
79 | signed int sin_i(signed int winkel) |
80 | signed int sin_i(signed int winkel) |
80 | { |
81 | { |
81 | short int m,n; |
82 | short int m,n; |
82 | 83 | ||
83 | if (abs(winkel) >=360) winkel = winkel % 360; |
84 | if (abs(winkel) >=360) winkel = winkel % 360; |
84 | if (winkel < 0) |
85 | if (winkel < 0) |
85 | { |
86 | { |
86 | m = -1; |
87 | m = -1; |
87 | winkel = abs(winkel); |
88 | winkel = abs(winkel); |
88 | } |
89 | } |
89 | else m = +1; |
90 | else m = +1; |
90 | n =1; |
91 | n =1; |
91 | 92 | ||
92 | // Quadranten auswerten |
93 | // Quadranten auswerten |
93 | if ((winkel > 90 ) && (winkel <= 180)) winkel = 180 - winkel; |
94 | if ((winkel > 90 ) && (winkel <= 180)) winkel = 180 - winkel; |
94 | else if ((winkel > 180 ) && (winkel <= 270)) |
95 | else if ((winkel > 180 ) && (winkel <= 270)) |
95 | { |
96 | { |
96 | winkel = winkel -180; |
97 | winkel = winkel -180; |
97 | n = -1; |
98 | n = -1; |
98 | } |
99 | } |
99 | else if ((winkel > 270) && (winkel <= 360)) |
100 | else if ((winkel > 270) && (winkel <= 360)) |
100 | { |
101 | { |
101 | winkel = 360 - winkel; |
102 | winkel = 360 - winkel; |
102 | n = -1; |
103 | n = -1; |
103 | } |
104 | } |
104 | // else //0 - 90 Grad |
105 | // else //0 - 90 Grad |
105 | 106 | ||
106 | winkel = pgm_read_word(&pgm_sinus[winkel]); |
107 | winkel = pgm_read_word(&pgm_sinus[winkel]); |
107 | return (winkel*m*n); |
108 | return (winkel*m*n); |
108 | } |
109 | } |
109 | 110 | ||
110 | // Aus x,y und Winkel Distanz ermitteln |
111 | // Aus x,y und Winkel Distanz ermitteln |
111 | long get_dist(signed int x, signed int y, signed int phi) |
112 | long get_dist(signed int x, signed int y, signed int phi) |
112 | { |
113 | { |
113 | long dist; |
114 | long dist; |
114 | if (abs(x) > abs(y) ) |
115 | if (abs(x) > abs(y) ) |
115 | { |
116 | { |
116 | dist = (long) x; //Groesseren Wert wegen besserer Genauigkeit nehmen |
117 | dist = (long) x; //Groesseren Wert wegen besserer Genauigkeit nehmen |
117 | dist = abs((dist *1000) / (long) sin_i(phi)); |
118 | dist = abs((dist *1000) / (long) sin_i(phi)); |
118 | } |
119 | } |
119 | else |
120 | else |
120 | { |
121 | { |
121 | dist = (long) y; |
122 | dist = (long) y; |
122 | dist = abs((dist *1000) / (long) cos_i(phi)); |
123 | dist = abs((dist *1000) / (long) cos_i(phi)); |
123 | } |
124 | } |
124 | return dist; |
125 | return dist; |
125 | } |
126 | } |
126 | 127 | ||
127 | 128 |