Rev 333 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
333 | osiair | 1 | /* |
2 | |||
3 | Copyright 2007, Niklas Nold |
||
4 | |||
5 | This program (files compass.c and compass.h) is free software; you can redistribute it and/or modify |
||
6 | it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; |
||
7 | either version 3 of the License, or (at your option) any later version. |
||
8 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
||
9 | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
10 | GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License |
||
11 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
12 | |||
13 | 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 |
||
14 | */ |
||
15 | |||
16 | #include "main.h" |
||
17 | |||
18 | MM3_struct MM3; |
||
19 | |||
20 | #define long int m; |
||
21 | |||
22 | //############################################################################ |
||
23 | //Initialisierung der SPI-Schnittstelle |
||
24 | void init_spi(void) |
||
25 | //############################################################################ |
||
26 | { |
||
27 | SPCR = (1<<SPIE)|(1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0); //Interrupt an, Master, 156 kHz Oszillator |
||
28 | //SPSR = (1<<SPI2X); |
||
29 | |||
30 | DDRB |= (1<<PB7)|(1<<PB5)|(1<<PB2); // J8, MOSI, SCK Ausgang |
||
31 | |||
32 | PORTD &= ~(1<<PD3); // J5 auf Low |
||
33 | |||
34 | MM3.AXIS = MM3_X; |
||
35 | MM3.STATE = MM3_RESET; |
||
36 | } |
||
37 | |||
38 | |||
39 | //############################################################################ |
||
40 | //Wird in der SIGNAL (SIG_OVERFLOW0) aufgerufen |
||
41 | void MM3_timer0(void) |
||
42 | //############################################################################ |
||
43 | { |
||
44 | switch (MM3.STATE) |
||
45 | { |
||
46 | case MM3_RESET: |
||
47 | PORTB |= (1<<PB2); // J8 auf High, MM3 Reset |
||
48 | MM3.STATE = MM3_START_TRANSFER; |
||
49 | return; |
||
50 | |||
51 | case MM3_START_TRANSFER: |
||
52 | PORTB &= ~(1<<PB2); // J8 auf Low (war ~125 µs auf High) |
||
53 | |||
54 | if (MM3.AXIS == MM3_X) SPDR = 0x51; // Schreiben ins SPDR löst automatisch Übertragung (MOSI und MISO) aus |
||
55 | else if (MM3.AXIS == MM3_Y) SPDR = 0x52; // Micromag Period Select ist auf 1024 (0x50) |
||
56 | else if (MM3.AXIS == MM3_Z) SPDR = 0x53; // 1: x-Achse, 2: Y-Achse, 3: Z-Achse |
||
57 | |||
58 | MM3.DRDY = SetDelay(15); // 15 Laut Datenblatt max. Zeit bis Messung fertig (bei PS 1024) |
||
59 | MM3.STATE = MM3_WAIT_DRDY; |
||
60 | return; |
||
61 | |||
62 | case MM3_WAIT_DRDY: |
||
63 | if (CheckDelay(MM3.DRDY)) {SPDR = 0x00;MM3.STATE = MM3_DRDY;} // Irgendwas ins SPDR, damit Übertragung ausgelöst wird, wenn Wartezeit vorbei |
||
64 | return; // Jetzt gehts weiter in SIGNAL (SIG_SPI) |
||
65 | |||
66 | case MM3_TILT: // Zeitnahe Speicherung der aktuellen Neigung in ° |
||
338 | osiair | 67 | MM3.NickGrad = asin_i((float)Aktuell_ax/208*200); |
68 | MM3.RollGrad = asin_i((float)Aktuell_ay/208*200); |
||
333 | osiair | 69 | |
70 | MM3.AXIS = MM3_X; |
||
71 | MM3.STATE = MM3_RESET; |
||
72 | return; |
||
73 | } |
||
74 | } |
||
75 | |||
76 | |||
77 | //############################################################################ |
||
78 | //SPI byte ready |
||
79 | SIGNAL (SIG_SPI) |
||
80 | //############################################################################ |
||
81 | { |
||
82 | switch (MM3.STATE) |
||
83 | { |
||
84 | case MM3_DRDY: // 1. Byte ist da, abspeichern, an die MSB-Stelle rücken |
||
85 | if (MM3.AXIS == MM3_X) |
||
86 | { |
||
87 | MM3.x_axis = SPDR; |
||
88 | MM3.x_axis <<= 8; |
||
89 | } |
||
90 | else if (MM3.AXIS == MM3_Y) |
||
91 | { |
||
92 | MM3.y_axis = SPDR; |
||
93 | MM3.y_axis <<= 8; |
||
94 | } |
||
95 | else // if (MM3.AXIS == MM3_Z) |
||
96 | { |
||
97 | MM3.z_axis = SPDR; |
||
98 | MM3.z_axis <<= 8; |
||
99 | } |
||
100 | |||
101 | SPDR=0x00; // Übertragung von 2. Byte auslösen |
||
102 | MM3.STATE=MM3_BYTE2; |
||
103 | return; |
||
104 | |||
105 | case MM3_BYTE2: // 2. Byte der entsprechenden Achse ist da |
||
338 | osiair | 106 | |
107 | // versuch die werte nicht über min und max steigen zu lassen um so die spikes zu verhindern |
||
108 | // verhindert hats nicht aber deutlich minimiert. Evtl kommen die restlichen |
||
109 | |||
110 | |||
333 | osiair | 111 | if (MM3.AXIS == MM3_X) |
112 | { |
||
113 | MM3.x_axis |= SPDR; |
||
114 | MM3.x_axis -= OFF_X; // Sofort Offset aus der Kalibrierung berücksichtigen |
||
338 | osiair | 115 | if (MM3.x_axis > MM3_Xmax || MM3.x_axis < MM3_Xmin) |
116 | { |
||
117 | MM3.x_axis = MM3.x_axis_last_valid; |
||
118 | } |
||
119 | else |
||
120 | { |
||
121 | //m = (MM3.x_axis + MM3.x_axis_last_valid) /2; |
||
122 | MM3.x_axis_last_valid = MM3.x_axis; |
||
123 | //MM3.x_axis = m; |
||
124 | } |
||
125 | |||
333 | osiair | 126 | MM3.AXIS = MM3_Y; |
127 | MM3.STATE = MM3_RESET; |
||
128 | } |
||
129 | else if (MM3.AXIS == MM3_Y) |
||
130 | { |
||
131 | MM3.y_axis |= SPDR; |
||
132 | MM3.y_axis -= OFF_Y; |
||
133 | |||
134 | if (MM3.y_axis > MM3_Ymax || MM3.y_axis < MM3_Ymin) |
||
135 | { |
||
136 | MM3.y_axis = MM3.y_axis_last_valid; |
||
137 | } |
||
138 | else |
||
139 | { |
||
140 | //m = (MM3.y_axis + MM3.y_axis_last_valid) /2; |
||
141 | MM3.y_axis_last_valid = MM3.y_axis; |
||
142 | //MM3.y_axis = m; |
||
143 | |||
144 | } |
||
145 | |||
338 | osiair | 146 | MM3.AXIS = MM3_Z; |
147 | MM3.STATE = MM3_RESET; |
||
148 | } |
||
149 | else // if (MM3.AXIS == MM3_Z) |
||
150 | { |
||
151 | MM3.z_axis |= SPDR; |
||
152 | MM3.z_axis -= OFF_Z; |
||
333 | osiair | 153 | |
154 | if (MM3.z_axis > MM3_Zmax || MM3.z_axis < MM3_Zmin) |
||
155 | { |
||
156 | MM3.z_axis = MM3.z_axis_last_valid; |
||
157 | } |
||
158 | else |
||
159 | { |
||
160 | //m = (MM3.z_axis + MM3.z_axis_last_valid) /2; |
||
161 | MM3.z_axis_last_valid = MM3.z_axis; |
||
162 | //MM3.z_axis = m; |
||
163 | } |
||
164 | |||
338 | osiair | 165 | MM3.STATE = MM3_TILT; |
166 | } |
||
333 | osiair | 167 | return; |
168 | } |
||
169 | } |
||
170 | |||
171 | signed int MM3_heading(void) |
||
172 | { |
||
173 | float sin_nick, cos_nick, sin_roll, cos_roll; |
||
174 | signed int x_corr, y_corr; |
||
175 | signed int heading; |
||
176 | |||
177 | |||
178 | |||
179 | // Berechung von sinus und cosinus |
||
180 | sin_nick = sin_f(MM3.NickGrad); |
||
181 | cos_nick = cos_f(MM3.NickGrad); |
||
182 | sin_roll = sin_f(MM3.RollGrad); |
||
183 | cos_roll = cos_f(MM3.RollGrad); |
||
184 | |||
185 | // Neigungskompensation |
||
186 | //x_corr = (cos_nick * MM3.x_axis) + (((sin_roll * MM3.y_axis) - (cos_roll * MM3.z_axis)) * sin_nick); |
||
187 | |||
188 | x_corr = (cos_nick * MM3.x_axis) - (((sin_roll * MM3.y_axis) - (cos_roll * MM3.z_axis)) * sin_nick); |
||
189 | y_corr = ((cos_roll * MM3.y_axis) + (sin_roll * MM3.z_axis)); |
||
190 | |||
191 | // Winkelberechnung |
||
192 | heading = atan2_i(x_corr, y_corr); |
||
193 | |||
194 | return (heading); |
||
195 | } |