Subversion Repositories FlightCtrl

Rev

Rev 308 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
308 osiair 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 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.  
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 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
9
GNU Lesser General Public License (License_LGPL.txt) along with this program.
10
If not, see <http://www.gnu.org/licenses/>.
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
13
*/
14
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
15
Peter Muehlenbrock
16
Winkelfunktionen sin, cos und arctan in
17
brute-force Art: Sehr Schnell, nicht sonderlich genau, aber ausreichend
18
get_dist Funktion fuer Entfernungsermittlung
19
Stand 12.10.2007
20
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21
*/
22
#include "main.h"
23
 
24
 
25
// arctan Funktion: Eingabewert x,y Rueckgabe =arctan(x,y) in grad
26
int arctan_i( signed int x,  signed int y)
27
{
28
        short int change_xy = 0;
29
        signed int i;
30
        long signed int wert;
31
        int return_value;
32
 
33
        if ((abs(x)) > (abs(y)))  // x,y Werte vertauschen damit arctan <45 grad bleibt
34
        {      
35
                change_xy       = 1;
36
                i                       = x;
37
                x                       = y;
38
                y                       = i;
39
        }
40
 
41
        // Quadranten ermitteln
42
 
43
        // Wert durch lineare Interpolation ermitteln 
44
        if ((y == 0) && (x == 0))  wert =1; // Division durch 0 nicht erlaubt
45
        else wert= abs(((long)x*1000)/((long)y));
46
 
47
        if (wert <=268) //0...0.0,268  entsprechend 0..15 Grad
48
        {
49
                return_value = (signed int)((wert*100)/(268-0)*(15-0)/100) +0;
50
        }
51
        else if (wert <=578) //0,268...0.0,568  entsprechend 15..30 Grad
52
        {      
53
                return_value = (signed int)((((wert-268)*100)/(578-268)*(30-15))/100) +15;             
54
        }
55
        else //0,568...1  entsprechend 30..45 Grad
56
        {      
57
                return_value = (signed int)((((wert-578)*50)/(1000-578)*(45-30))/50) +30;              
58
        }
59
 
60
        if (change_xy == 0)  return_value = 90-return_value; //Quadrant 45..90 Grad
61
        if ((x >= 0) && (y <0)) return_value = - return_value;
62
        else if ((x < 0) && (y >= 0)) return_value = - return_value;
63
 
64
return return_value;
65
}
66
 
67
 
68
// cosinus Funktion: Eingabewert Winkel in Grad, Rueckgabe =cos(winkel)*1000
69
signed int cos_i(signed int winkel)
70
{
71
  winkel = sin_i(90-winkel);
72
  return winkel;
73
}
74
 
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};
77
//von Nick666, Stand 28.9.2007
78
// sinus Funktion: Eingabewert Winkel in Grad, Rueckgabe =sin(winkel)*1000
79
signed int sin_i(signed int winkel)
80
{
81
 short int m,n;
82
 
83
 if (abs(winkel) >=360) winkel = winkel % 360;
84
 if (winkel < 0)
85
 {
86
        m = -1;
87
        winkel = abs(winkel);
88
 }
89
 else m = +1;
90
 n =1;
91
 
92
 // Quadranten auswerten
93
 if ((winkel > 90 ) && (winkel <= 180)) winkel = 180 - winkel;
94
 else if ((winkel > 180 ) && (winkel <= 270))
95
 {
96
        winkel = winkel -180;
97
        n = -1;
98
 }
99
  else if ((winkel > 270) && (winkel <= 360))
100
 {
101
        winkel = 360 - winkel;
102
        n = -1;
103
 }
104
 // else //0 - 90 Grad
105
 
106
 winkel = pgm_read_word(&pgm_sinus[winkel]);
107
 return (winkel*m*n);
108
}
109
 
110
// Aus x,y und Winkel Distanz ermitteln
111
long get_dist(signed int x, signed int y, signed int phi)
112
{
113
        long dist;
114
        if (abs(x) > abs(y) )
115
        {
116
                dist = (long) x; //Groesseren Wert wegen besserer Genauigkeit nehmen
117
                dist = abs((dist *1000) / (long) sin_i(phi));
118
        }
119
        else
120
        {
121
                dist = (long) y;
122
                dist = abs((dist *1000) / (long) cos_i(phi));
123
        }
124
  return dist;
125
}
126
 
127
 
128
// noch mehr von Nick666
129
 
130
const uint8_t pgm_atan[270] PROGMEM = {0,1,2,3,5,6,7,8,9,10,11,12,13,15,16,17,18,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,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,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,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,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,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,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,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,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};
131
 
132
// Arkustangens2 im Gradmaß
133
signed int atan2_i(signed int x,  signed int y)
134
{
135
        int i,angle;
136
        int8_t m;
137
 
138
        if (!x && !y) return 0;         //atan2 = 0 für x und y = 0
139
 
140
        if (y < 0) m=-1;
141
        else m=1;
142
 
143
        if (x==0) return (90*m);                // atan2 = 90° für x = 0
144
 
145
        i = abs(((float)y / x) * 50);           // Berechne i für die Lookup table (Schrittweite atan(x) ist 0.02 -> *50)
146
 
147
        if (i<270) angle = pgm_read_byte(&pgm_atan[i]); // Lookup für 1° bis 79°
148
        else if (i>5750) angle = 90;                                            // Grenzwert ist 90°
149
        else if (i>=1910) angle = 89;                                           // 89° bis 80° über Wertebereiche
150
        else if (i>=1150) angle = 88;
151
        else if (i>=820) angle = 87;
152
        else if (i>=640) angle = 86;
153
        else if (i>=520) angle = 85;
154
        else if (i>=440) angle = 84;
155
        else if (i>=380) angle = 83;
156
        else if (i>=335) angle = 82;
157
        else if (i>=299) angle = 81;
158
        else angle = 80; // (i>=270)
159
 
160
        if (x > 0) return (angle*m);    // Quadrant I und IV
161
        else if ((x < 0) && (y >= 0)) return ((angle*-1) + 180);        // Quadrant II
162
        else return (angle - 180); // x < 0 && y < 0    Quadrant III
163
}
164
 
165
 
166
 
167
 
168
 
169
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};
170
 
171
inline float pgm_read_float(const float *addr)
172
{      
173
        union
174
        {
175
                uint16_t i[2];  // 2 16-bit-Worte
176
                float f;
177
        } u;
178
 
179
        u.i[0]=pgm_read_word((PGM_P)addr);
180
        u.i[1]=pgm_read_word((PGM_P)addr+2);
181
 
182
        return u.f;
183
}
184
 
309 osiair 185
// Kosinusfunktion im Gradmaß
186
float cos_f(signed int winkel)
187
{
188
 return (sin_f(90-winkel));
189
}
308 osiair 190
 
191
// Sinusfunktion im Gradmaß
192
float sin_f(signed int winkel)
193
{
194
 short int m,n;
195
 float sinus;
196
 
197
 //winkel = winkel % 360;
198
 
199
 if (winkel < 0)
200
 {
201
        m = -1;
202
        winkel = abs(winkel);
203
 }
204
 else m = +1;
205
 
206
 // Quadranten auswerten
207
 if ((winkel > 90 ) && (winkel <= 180)) {winkel = 180 - winkel; n = 1;}
208
 else if ((winkel > 180 ) && (winkel <= 270)) {winkel = winkel - 180; n = -1;}
209
 else if ((winkel > 270) && (winkel <= 360)) {winkel = 360 - winkel; n = -1;}
210
 else n = 1; //0 - 90 Grad
211
 
212
sinus = pgm_read_float(&pgm_sinus_f[winkel]);
213
 
214
return (sinus*m*n);
215
}
216
 
217
 
218
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};
219
 
220
// Akurssinusfunktion im Gradmaß
221
int8_t asin_i(signed int i)
222
{
223
        signed char m;
224
 
225
        if (i < 0) {m=-1;i=abs(i);}
226
        else m=1;
227
 
228
        return (pgm_read_byte(&pgm_asin[i]) * m);
229
}