Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
205 Nick666 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
#include "main.h"
14
 
232 Nick666 15
/*
205 Nick666 16
//-----------------------------------------------
226 Nick666 17
// Fast arctan2 with max error of .07 rads
205 Nick666 18
// http://www.dspguru.com/comp.dsp/tricks/alg/fxdatan2.htm
19
//-----------------------------------------------
229 Nick666 20
signed int arctan_f(signed int x, signed int y)
205 Nick666 21
{  
226 Nick666 22
        float rad,r;
23
        short int m;
24
        #define coeff_1         0.7854
25
        #define coeff_2         2.3562
26
        #define rad2grad        57.2958  
205 Nick666 27
 
28
   if (!x && !y) return 0;
29
 
30
   if (y < 0) {y = abs(y); m = -1;}
31
   else m = 1;
32
 
33
   if (x>=0)
34
   {
226 Nick666 35
      r = (float)(x - y) / (float)(x + y);
36
      rad = coeff_1 - coeff_1*r;
205 Nick666 37
   }
38
   else
39
   {
226 Nick666 40
      r = (float)(x + y) / (float)(y - x);
41
      rad = coeff_2 - coeff_1*r;
205 Nick666 42
   }
226 Nick666 43
 
44
   rad *= rad2grad;
45
 
214 Nick666 46
   return(rad*m);
205 Nick666 47
}
232 Nick666 48
*/
205 Nick666 49
 
232 Nick666 50
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};
51
 
226 Nick666 52
signed int arctan_i(signed int x,  signed int y)
214 Nick666 53
{
232 Nick666 54
        int i,angle;
55
        int8_t m;
214 Nick666 56
 
232 Nick666 57
        if (!x && !y) return 0;         //atan2 = 0 für x und y = 0
58
 
59
        if (y < 0) m=-1;
60
        else m=1;
61
 
62
        if (x==0) return (90*m);                // atan2 = 90° für x = 0
63
 
64
        i = abs(((float)y / x) * 50.0);         // Berechne i für die Lookup table (Schrittweite atan(x) ist 0.02 -> *50)
214 Nick666 65
 
232 Nick666 66
        if (i<270) angle = pgm_read_byte(&pgm_atan[i]); // Lookup für 1° bis 79°
67
        else if (i>5750) angle = 90;                                            // Grenzwert ist 90°
68
        else if (i>=1910) angle = 89;                                           // 89° bis 80° über Wertebereiche
69
        else if (i>=1150) angle = 88;
70
        else if (i>=820) angle = 87;
71
        else if (i>=640) angle = 86;
72
        else if (i>=520) angle = 85;
73
        else if (i>=440) angle = 84;
74
        else if (i>=380) angle = 83;
75
        else if (i>=335) angle = 82;
76
        else if (i>=299) angle = 81;
77
        else angle = 80; // (i>=270)
226 Nick666 78
 
232 Nick666 79
        if (x > 0) return (angle*m);    // Quadrant I und IV
80
        else if ((x < 0) && (y >= 0)) return ((angle*-1) + 180);        // Quadrant II
81
        else return (angle - 180); // x < 0 && y < 0    Quadrant III
214 Nick666 82
}
83
 
226 Nick666 84
 
85
 
86
 
229 Nick666 87
 
88
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};
89
 
90
inline float pgm_read_float(const float *addr)
91
{      
92
        union
93
        {
94
                uint16_t i[2];  // 2 16-bit-Worte
95
                float f;
96
        } u;
97
 
98
        u.i[0]=pgm_read_word((PGM_P)addr);
99
        u.i[1]=pgm_read_word((PGM_P)addr+2);
100
 
101
        return u.f;
102
}
103
 
104
// cosinus Funktion: Eingabewert Winkel in Grad
105
float cos_f(signed int winkel)
106
{
107
 return (sin_f(90-winkel));
205 Nick666 108
}
109
 
229 Nick666 110
// sinus Funktion: Eingabewert Winkel in Grad
111
float sin_f(signed int winkel)
205 Nick666 112
{
113
 short int m,n;
229 Nick666 114
 float sinus;
205 Nick666 115
 
116
 //winkel = winkel % 360;
117
 
118
 if (winkel < 0)
119
 {
120
        m = -1;
121
        winkel = abs(winkel);
122
 }
123
 else m = +1;
124
 
125
 // Quadranten auswerten
126
 if ((winkel > 90 ) && (winkel <= 180)) {winkel = 180 - winkel; n = 1;}
127
 else if ((winkel > 180 ) && (winkel <= 270)) {winkel = winkel - 180; n = -1;}
128
 else if ((winkel > 270) && (winkel <= 360)) {winkel = 360 - winkel; n = -1;}
129
 else n = 1; //0 - 90 Grad
130
 
229 Nick666 131
sinus = pgm_read_float(&pgm_sinus_f[winkel]);
205 Nick666 132
 
229 Nick666 133
return (sinus*m*n);
205 Nick666 134
}
135
 
136
 
232 Nick666 137
/*
226 Nick666 138
 
229 Nick666 139
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};
140
 
141
// cosinus Funktion: Eingabewert Winkel in Grad, Rueckgabe =cos(winkel)*1000
142
signed int cos_i(signed int winkel)
143
{
144
  return (sin_i(90-winkel));
226 Nick666 145
}
146
 
229 Nick666 147
// sinus Funktion: Eingabewert Winkel in Grad, Rueckgabe =sin(winkel)*1000
148
signed int sin_i(signed int winkel)
226 Nick666 149
{
150
 short int m,n;
151
 
152
 //winkel = winkel % 360;
153
 
154
 if (winkel < 0)
155
 {
156
        m = -1;
157
        winkel = abs(winkel);
158
 }
159
 else m = +1;
160
 
161
 // Quadranten auswerten
162
 if ((winkel > 90 ) && (winkel <= 180)) {winkel = 180 - winkel; n = 1;}
163
 else if ((winkel > 180 ) && (winkel <= 270)) {winkel = winkel - 180; n = -1;}
164
 else if ((winkel > 270) && (winkel <= 360)) {winkel = 360 - winkel; n = -1;}
165
 else n = 1; //0 - 90 Grad
166
 
229 Nick666 167
winkel = pgm_read_word(&pgm_sinus[winkel]);
226 Nick666 168
 
229 Nick666 169
return (winkel*m*n);
226 Nick666 170
}
232 Nick666 171
*/