Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

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