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
3
it under the terms of the GNU 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 for more details. You should have received a copy of the GNU General Public License
8
along with this program. If not, see <http://www.gnu.org/licenses/>.
9
 
10
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
11
*/
12
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
Peter Muehlenbrock
14
Winkelfunktionen sin, cos und arctan in
15
brute-force Art: Sehr Schnell, nicht sonderlich genau, aber ausreichend
183 salvo 16
Sinus Funktion von Nick666 vereinfacht
17
Stand 28.9.2007
149 salvo 18
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19
*/
155 salvo 20
#include "main.h"
149 salvo 21
 
155 salvo 22
 
149 salvo 23
// arctan Funktion: Eingabewert x,y Rueckgabe =arctan(x,y) in grad
155 salvo 24
int arctan_i(long signed int x, long signed int y)
149 salvo 25
{
155 salvo 26
        short int change_xy = 0;
149 salvo 27
        signed int i;
155 salvo 28
        long signed int wert;
29
        int return_value;
149 salvo 30
 
155 salvo 31
        if ((abs(x)) > (abs(y)))  // x,y Werte vertauschen damit arctan <45 grad bleibt
149 salvo 32
        {      
155 salvo 33
                change_xy       = 1;
34
                i                       = x;
35
                x                       = y;
36
                y                       = i;
149 salvo 37
        }
155 salvo 38
 
39
        // Quadranten ermitteln
149 salvo 40
 
155 salvo 41
        // Wert durch lineare Interpolation ermitteln 
42
        wert= abs((x*1000)/y);
43
 
44
        if (wert <=268) //0...0.0,268  entsprechend 0..15 Grad
45
        {
46
                return_value = (int)((wert*100)/(268-0)*(15-0)/100) +0;
47
        }
48
        else if (wert <=578) //0,268...0.0,568  entsprechend 15..30 Grad
49
        {      
50
                return_value = (int)((((wert-268)*100)/(578-268)*(30-15))/100) +15;            
51
        }
52
        else //0,568...1  entsprechend 30..45 Grad
53
        {      
54
                return_value = (int)((((wert-578)*50)/(1000-578)*(45-30))/50) +30;             
55
        }
56
 
161 salvo 57
        if (change_xy == 0)  return_value = 90-return_value; //Quadrant 45..90 Grad
58
        if ((x >= 0) && (y <0)) return_value = - return_value;
59
        else if ((x < 0) && (y >= 0)) return_value = - return_value;
155 salvo 60
 
61
return return_value;
149 salvo 62
}
63
 
64
 
65
// cosinus Funktion: Eingabewert Winkel in Grad, Rueckgabe =cos(winkel)*1000
66
signed int cos_i(signed int winkel)
67
{
68
  winkel = sin_i(90-winkel);
69
  return winkel;
70
}
71
 
72
 
183 salvo 73
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};
74
//von Nick666, Stand 28.9.2007
149 salvo 75
// sinus Funktion: Eingabewert Winkel in Grad, Rueckgabe =sin(winkel)*1000
76
signed int sin_i(signed int winkel)
77
{
78
 short int m;
183 salvo 79
 
80
 if (abs(winkel) >=360) winkel = winkel % 360;
81
 if (winkel < 0)
149 salvo 82
 {
83
        m = -1;
183 salvo 84
        winkel = abs(winkel);
149 salvo 85
 }
183 salvo 86
 else m = +1;
87
 
88
 // Quadranten auswerten
89
 if ((winkel > 90 ) && (winkel <= 180)) winkel = winkel - 90;
90
 else if ((winkel > 180 ) && (winkel <= 270)) winkel = winkel - 180;
91
 else if ((winkel > 270) && (winkel <= 360)) winkel = winkel - 270;
92
 // else //0 - 90 Grad
149 salvo 93
 
183 salvo 94
 winkel = pgm_read_word(&pgm_sinus[winkel]);
95
 return (winkel*m);
149 salvo 96
}
97