Rev 385 |
Blame |
Last modification |
View Log
| RSS feed
/*
Copyright 2007, Niklas Nold
This program (files math.c and math.h) is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation;
either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
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
*/
#include "main.h"
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};
//############################################################################
// Arkustangens2 im Gradmaß
signed int atan2_i
(signed int x
, signed int y
)
//############################################################################
{
int i
,angle
;
int8_t m
;
if (!x
&& !y
) return 0; //atan2 = 0 für x und y = 0
if (y
< 0)
{
m
=-1;
}
else m
=1;
if (x
==0) return (90*m
); // atan2 = 90° für x = 0
i
= abs(((long)y
*50) / x
); // Berechne i für die Lookup table (Schrittweite atan(x) ist 0.02 -> *50)
if (i
<270) angle
= pgm_read_byte
(&pgm_atan
[i
]); // Lookup für 1° bis 79°
else if (i
>5750) angle
= 90; // Grenzwert ist 90°
else if (i
>=1910) angle
= 89; // 89° bis 80° über Wertebereiche
else if (i
>=1150) angle
= 88;
else if (i
>=820) angle
= 87;
else if (i
>=640) angle
= 86;
else if (i
>=520) angle
= 85;
else if (i
>=440) angle
= 84;
else if (i
>=380) angle
= 83;
else if (i
>=335) angle
= 82;
else if (i
>=299) angle
= 81;
else angle
= 80; // (i>=270)
if (x
> 0) return (angle
*m
); // Quadrant I und IV
else if ((x
< 0) && (m
> 0)) return (-angle
+ 180); // Quadrant II
else return (angle
- 180); // x < 0 && y < 0 Quadrant III
}
const float pgm_sinus_f
[91] PROGMEM
= {0.0,0.01745241,0.03489950,0.05233595,0.06975647,0.08715574,0.10452846,0.12186934,0.13917310,0.15643446,0.17364817,0.19080899,0.20791168,0.22495105,0.24192189,0.25881904,0.27563735,0.29237170,0.30901699,0.32556815,0.34202013,0.35836794,0.37460658,0.39073112,0.40673663,0.42261825,0.43837113,0.45399049,0.46947155,0.48480961,0.49999999,0.51503806,0.52991925,0.54463902,0.55919289,0.57357642,0.58778524,0.60181501,0.61566146,0.62932038,0.64278759,0.65605901,0.66913059,0.68199834,0.69465835,0.70710676,0.71933978,0.73135368,0.74314481,0.75470956,0.76604443,0.77714594,0.78801074,0.79863549,0.80901698,0.81915203,0.82903756,0.83867055,0.84804808,0.85716728,0.86602539,0.87461969,0.88294758,0.89100651,0.89879403,0.90630777,0.91354544,0.92050484,0.92718384,0.93358041,0.93969261,0.94551856,0.95105650,0.95630474,0.96126169,0.96592582,0.97029572,0.97437006,0.97814759,0.98162718,0.98480775,0.98768833,0.99026806,0.99254615,0.99452189,0.99619469,0.99756405,0.99862953,0.99939083,0.99984769,1.0};
inline float pgm_read_float
(const float *addr
)
{
union
{
uint16_t i
[2]; // 2 16-bit-Worte
float f
;
} u
;
u.
i[0]=pgm_read_word
((PGM_P
)addr
);
u.
i[1]=pgm_read_word
((PGM_P
)addr
+2);
return u.
f;
}
//############################################################################
// Kosinusfunktion im Gradmaß
float cos_f
(signed int winkel
)
//############################################################################
{
return (sin_f
(90-winkel
));
}
//############################################################################
// Sinusfunktion im Gradmaß
float sin_f
(signed int winkel
)
//############################################################################
{
short int m
,n
;
float sinus
;
//winkel = winkel % 360;
if (winkel
< 0)
{
m
= -1;
winkel
= abs(winkel
);
}
else m
= +1;
// Quadranten auswerten
if ((winkel
> 90 ) && (winkel
<= 180)) {winkel
= 180 - winkel
; n
= 1;}
else if ((winkel
> 180 ) && (winkel
<= 270)) {winkel
= winkel
- 180; n
= -1;}
else if ((winkel
> 270) && (winkel
<= 360)) {winkel
= 360 - winkel
; n
= -1;}
else n
= 1; //0 - 90 Grad
sinus
= pgm_read_float
(&pgm_sinus_f
[winkel
]);
return (sinus
*m
*n
);
}
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};
//############################################################################
// Akurssinusfunktion im Gradmaß
int8_t asin_i
(signed int i
)
//############################################################################
{
signed char m
;
if (i
< 0) {m
=-1;i
=abs(i
);}
else m
=1;
if (i
> 200) i
= 200;
return (pgm_read_byte
(&pgm_asin
[i
]) * m
);
}