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
194 salvo 17
Stand 1.10.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
194 salvo 24
int arctan_i( signed int x,  signed int y)
149 salvo 25
{
155 salvo 26
	short int change_xy = 0;
194 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
190 salvo 42
	if ((y == 0) && (x == 0))  wert =1; // Division durch 0 nicht erlaubt
43
	else wert= abs((x*1000)/y);
155 salvo 44
 
45
	if (wert <=268) //0...0.0,268  entsprechend 0..15 Grad
46
	{
194 salvo 47
		return_value = (signed int)((wert*100)/(268-0)*(15-0)/100) +0;
155 salvo 48
	}
49
	else if (wert <=578) //0,268...0.0,568  entsprechend 15..30 Grad
50
	{
194 salvo 51
		return_value = (signed int)((((wert-268)*100)/(578-268)*(30-15))/100) +15;
155 salvo 52
	}
53
	else //0,568...1  entsprechend 30..45 Grad
54
	{
194 salvo 55
		return_value = (signed int)((((wert-578)*50)/(1000-578)*(45-30))/50) +30;
155 salvo 56
	}
57
 
161 salvo 58
	if (change_xy == 0)  return_value = 90-return_value; //Quadrant 45..90 Grad
59
	if ((x >= 0) && (y <0)) return_value = - return_value;
60
	else if ((x < 0) && (y >= 0)) return_value = - return_value;
155 salvo 61
 
62
return return_value;
149 salvo 63
}
64
 
65
 
66
// cosinus Funktion: Eingabewert Winkel in Grad, Rueckgabe =cos(winkel)*1000
67
signed int cos_i(signed int winkel)
68
{
69
  winkel = sin_i(90-winkel);
70
  return winkel;
71
}
72
 
73
 
183 salvo 74
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};
75
//von Nick666, Stand 28.9.2007
149 salvo 76
// sinus Funktion: Eingabewert Winkel in Grad, Rueckgabe =sin(winkel)*1000
77
signed int sin_i(signed int winkel)
78
{
190 salvo 79
 short int m,n;
183 salvo 80
 
81
 if (abs(winkel) >=360) winkel = winkel % 360;
82
 if (winkel < 0)
149 salvo 83
 {
84
	m = -1;
183 salvo 85
	winkel = abs(winkel);
149 salvo 86
 }
183 salvo 87
 else m = +1;
190 salvo 88
 n =1;
89
 
183 salvo 90
 // Quadranten auswerten
190 salvo 91
 if ((winkel > 90 ) && (winkel <= 180)) winkel = 180 - winkel;
92
 else if ((winkel > 180 ) && (winkel <= 270))
93
 {
94
 	winkel = winkel -180;
95
	n = -1;
96
 }
97
  else if ((winkel > 270) && (winkel <= 360))
98
 {
99
 	winkel = 360 - winkel;
100
	n = -1;
101
 }
183 salvo 102
 // else //0 - 90 Grad
149 salvo 103
 
183 salvo 104
 winkel = pgm_read_word(&pgm_sinus[winkel]);
190 salvo 105
 return (winkel*m*n);
149 salvo 106
}
107
 
224 salvo 108
// Aus x,y und Winkel Distanz ermitteln
109
long get_dist(signed int x, signed int y, signed int phi)
110
{
111
	long dist;
112
	if (abs(x) > abs(y) )
113
	{
114
		dist = (long) x; //Groesseren Wert wegen besserer Genauigkeit nehmen
115
		dist = abs((dist *1000) / (long) sin_i(phi));
116
	}
117
	else
118
	{
119
		dist = (long) y;
120
		dist = abs((dist *1000) / (long) cos_i(phi));
121
	}
122
  return dist;
123
}
124