Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
149 salvo 1
/*
2
This program (files math.c and math.h) is free software; you can redistribute it and/or modify
242 salvo 3
it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation;
149 salvo 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
242 salvo 7
GNU General Public License and GNU Lesser General Public License for more details.
258 salvo 8
You should have received a copy of GNU General Public License (License_GPL.txt)  and
242 salvo 9
GNU Lesser General Public License (License_LGPL.txt) along with this program.
10
If not, see <http://www.gnu.org/licenses/>.
149 salvo 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
258 salvo 18
get_dist Funktion fuer Entfernungsermittlung
291 salvo 19
Stand 12.10.2007
149 salvo 20
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21
*/
155 salvo 22
#include "main.h"
149 salvo 23
 
155 salvo 24
 
149 salvo 25
// arctan Funktion: Eingabewert x,y Rueckgabe =arctan(x,y) in grad
194 salvo 26
int arctan_i( signed int x,  signed int y)
149 salvo 27
{
155 salvo 28
        short int change_xy = 0;
194 salvo 29
        signed int i;
155 salvo 30
        long signed int wert;
31
        int return_value;
149 salvo 32
 
155 salvo 33
        if ((abs(x)) > (abs(y)))  // x,y Werte vertauschen damit arctan <45 grad bleibt
149 salvo 34
        {      
155 salvo 35
                change_xy       = 1;
36
                i                       = x;
37
                x                       = y;
38
                y                       = i;
149 salvo 39
        }
155 salvo 40
 
41
        // Quadranten ermitteln
149 salvo 42
 
155 salvo 43
        // Wert durch lineare Interpolation ermitteln 
190 salvo 44
        if ((y == 0) && (x == 0))  wert =1; // Division durch 0 nicht erlaubt
266 salvo 45
        else wert= abs(((long)x*1000)/((long)y));
155 salvo 46
 
47
        if (wert <=268) //0...0.0,268  entsprechend 0..15 Grad
48
        {
194 salvo 49
                return_value = (signed int)((wert*100)/(268-0)*(15-0)/100) +0;
155 salvo 50
        }
51
        else if (wert <=578) //0,268...0.0,568  entsprechend 15..30 Grad
52
        {      
194 salvo 53
                return_value = (signed int)((((wert-268)*100)/(578-268)*(30-15))/100) +15;             
155 salvo 54
        }
55
        else //0,568...1  entsprechend 30..45 Grad
56
        {      
194 salvo 57
                return_value = (signed int)((((wert-578)*50)/(1000-578)*(45-30))/50) +30;              
155 salvo 58
        }
59
 
161 salvo 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;
155 salvo 63
 
64
return return_value;
149 salvo 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
 
183 salvo 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
149 salvo 78
// sinus Funktion: Eingabewert Winkel in Grad, Rueckgabe =sin(winkel)*1000
79
signed int sin_i(signed int winkel)
80
{
190 salvo 81
 short int m,n;
183 salvo 82
 
83
 if (abs(winkel) >=360) winkel = winkel % 360;
84
 if (winkel < 0)
149 salvo 85
 {
86
        m = -1;
183 salvo 87
        winkel = abs(winkel);
149 salvo 88
 }
183 salvo 89
 else m = +1;
190 salvo 90
 n =1;
91
 
183 salvo 92
 // Quadranten auswerten
190 salvo 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
 }
183 salvo 104
 // else //0 - 90 Grad
149 salvo 105
 
183 salvo 106
 winkel = pgm_read_word(&pgm_sinus[winkel]);
190 salvo 107
 return (winkel*m*n);
149 salvo 108
}
109
 
224 salvo 110
// Aus x,y und Winkel Distanz ermitteln
266 salvo 111
long get_dist(signed int x, signed int y, signed int phi)
224 salvo 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
        }
266 salvo 124
  return dist;
224 salvo 125
}
126