Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
550 | walter | 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 | struct MM3_calib_struct ee_calib EEMEM; // Reservierung im EEPROM |
||
19 | |||
20 | struct MM3_working_struct MM3; |
||
21 | struct MM3_calib_struct MM3_calib; |
||
22 | |||
23 | |||
24 | //############################################################################ |
||
25 | // Initialisierung |
||
26 | void init_MM3(void) |
||
27 | //############################################################################ |
||
28 | { |
||
29 | SPCR = (1<<SPIE)|(1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0); //Interrupt an, Master, 156 kHz Oszillator |
||
30 | //SPSR = (1<<SPI2X); |
||
31 | |||
32 | DDRB |= (1<<PB7)|(1<<PB5)|(1<<PB2); // J8, MOSI, SCK Ausgang |
||
33 | DDRB &= ~(1<<PB6); // MISO Eingang |
||
34 | |||
35 | |||
36 | //PORTD &= ~(1<<PD3); // J5 auf Low |
||
37 | DDRC |= (1<<PC6); // J9 Output |
||
38 | PORTC &= ~(1<<PC6); // J9 auf Low |
||
39 | |||
40 | MM3.AXIS = MM3_X; |
||
41 | MM3.STATE = MM3_RESET; |
||
42 | |||
43 | // Kalibrierung aus dem EEprom lesen |
||
44 | eeprom_read_block(&MM3_calib,&ee_calib,sizeof(struct MM3_calib_struct)); |
||
45 | } |
||
46 | |||
47 | |||
48 | //############################################################################ |
||
49 | // Wird in der SIGNAL (SIG_OVERFLOW0) aufgerufen |
||
50 | void timer0_MM3(void) |
||
51 | //############################################################################ |
||
52 | { |
||
53 | switch (MM3.STATE) |
||
54 | { |
||
55 | case MM3_RESET: |
||
56 | PORTB |= (1<<PB2); // J8 auf High, MM3 Reset |
||
57 | MM3.STATE = MM3_START_TRANSFER; |
||
58 | return; |
||
59 | |||
60 | case MM3_START_TRANSFER: |
||
61 | PORTB &= ~(1<<PB2); // J8 auf Low (war ~125 µs auf High) |
||
62 | |||
63 | if (MM3.AXIS == MM3_X) SPDR = 0x31; // Schreiben ins SPDR löst automatisch Übertragung (MOSI und MISO) aus |
||
64 | else if (MM3.AXIS == MM3_Y) SPDR = 0x32; // Micromag Period Select ist auf 256 (0x30) |
||
65 | else if (MM3.AXIS == MM3_Z) SPDR = 0x33; // 1: x-Achse, 2: Y-Achse, 3: Z-Achse |
||
66 | |||
67 | MM3.DRDY = SetDelay(8); // Laut Datenblatt max. Zeit bis Messung fertig (bei PS 256 eigentlich 4 ms) |
||
68 | MM3.STATE = MM3_WAIT_DRDY; |
||
69 | return; |
||
70 | |||
71 | case MM3_WAIT_DRDY: |
||
72 | if (CheckDelay(MM3.DRDY)) {SPDR = 0x00;MM3.STATE = MM3_DRDY;} // Irgendwas ins SPDR, damit Übertragung ausgelöst wird, wenn Wartezeit vorbei |
||
73 | return; // Jetzt gehts weiter in SIGNAL (SIG_SPI) |
||
74 | } |
||
75 | } |
||
76 | |||
77 | |||
78 | //############################################################################ |
||
79 | // SPI byte ready |
||
80 | SIGNAL (SIG_SPI) |
||
81 | //############################################################################ |
||
82 | { |
||
83 | static char tmp; |
||
84 | int wert; |
||
85 | |||
86 | switch (MM3.STATE) |
||
87 | { |
||
88 | case MM3_DRDY: // 1. Byte ist da, zwischenspeichern |
||
89 | tmp = SPDR; |
||
90 | SPDR = 0x00; // Übertragung von 2. Byte auslösen |
||
91 | MM3.STATE = MM3_BYTE2; |
||
92 | return; |
||
93 | |||
94 | case MM3_BYTE2: // 2. Byte der entsprechenden Achse ist da |
||
95 | wert = tmp; |
||
96 | wert <<= 8; // 1. Byte an MSB-Stelle rücken |
||
97 | wert |= SPDR; // 2. Byte dranpappen |
||
98 | |||
99 | if(abs(wert) < Max_Axis_Value) // Spikes filtern. Zuweisung nur, wenn Max-Wert nicht überschritten |
||
100 | switch (MM3.AXIS) |
||
101 | { |
||
102 | case MM3_X: |
||
103 | MM3.x_axis = wert; |
||
104 | MM3.AXIS = MM3_Y; |
||
105 | break; |
||
106 | case MM3_Y: |
||
107 | MM3.y_axis = wert; |
||
108 | MM3.AXIS = MM3_Z; |
||
109 | break; |
||
110 | case MM3_Z: |
||
111 | MM3.z_axis = wert; |
||
112 | MM3.AXIS = MM3_X; |
||
113 | } |
||
114 | |||
115 | MM3.STATE = MM3_RESET; |
||
116 | } |
||
117 | } |
||
118 | |||
119 | //############################################################################ |
||
120 | // Kompass kalibrieren |
||
121 | void calib_MM3(void) |
||
122 | //############################################################################ |
||
123 | { |
||
124 | signed int x_min=0,x_max=0,y_min=0,y_max=0,z_min=0,z_max=0; |
||
125 | uint8_t measurement=50,beeper=0; |
||
126 | unsigned int timer; |
||
127 | |||
128 | GRN_ON; |
||
129 | ROT_OFF; |
||
130 | |||
131 | while (measurement) |
||
132 | { |
||
133 | //H_earth = MM3.x_axis*MM3.x_axis + MM3.y_axis*MM3.y_axis + MM3.z_axis*MM3.z_axis; |
||
134 | |||
135 | if (MM3.x_axis > x_max) x_max = MM3.x_axis; |
||
136 | else if (MM3.x_axis < x_min) x_min = MM3.x_axis; |
||
137 | |||
138 | if (MM3.y_axis > y_max) y_max = MM3.y_axis; |
||
139 | else if (MM3.y_axis < y_min) y_min = MM3.y_axis; |
||
140 | |||
141 | if (MM3.z_axis > z_max) z_max = MM3.z_axis; |
||
142 | else if (MM3.z_axis < z_min) z_min = MM3.z_axis; |
||
143 | |||
144 | if (!beeper) |
||
145 | { |
||
146 | ROT_FLASH; |
||
147 | GRN_FLASH; |
||
148 | beeptime = 50; |
||
149 | beeper = 50; |
||
150 | } |
||
151 | beeper--; |
||
152 | |||
153 | // Schleife mit 100 Hz |
||
154 | timer = SetDelay(10); |
||
155 | while(!CheckDelay(timer)); |
||
156 | |||
157 | // Wenn Gas zurück genommen wird, Kalibrierung mit 1/2 Sekunde Verzögerung beenden |
||
158 | if (PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] < 100) measurement--; |
||
159 | } |
||
160 | |||
161 | // Wertebereich der Achsen |
||
162 | MM3_calib.X_range = (x_max - x_min); |
||
163 | MM3_calib.Y_range = (y_max - y_min); |
||
164 | MM3_calib.Z_range = (z_max - z_min); |
||
165 | |||
166 | // Offset der Achsen |
||
167 | MM3_calib.X_off = (x_max + x_min) / 2; |
||
168 | MM3_calib.Y_off = (y_max + y_min) / 2; |
||
169 | MM3_calib.Z_off = (z_max + z_min) / 2; |
||
170 | |||
171 | // und im EEProm abspeichern |
||
172 | eeprom_write_block(&MM3_calib,&ee_calib,sizeof(struct MM3_calib_struct)); |
||
173 | } |
||
174 | |||
175 | |||
176 | //############################################################################ |
||
177 | // Neigungskompensierung und Berechnung der Ausrichtung |
||
178 | signed int heading_MM3(void) |
||
179 | //############################################################################ |
||
180 | { |
||
181 | float sin_nick, cos_nick, sin_roll, cos_roll; |
||
182 | float x_corr, y_corr; |
||
183 | signed int x_axis,y_axis,z_axis,heading; |
||
184 | signed int nicktilt,rolltilt; |
||
185 | unsigned int div_faktor; |
||
186 | |||
187 | div_faktor = (uint16_t)EE_Parameter.UserParam1 * 8; |
||
188 | |||
189 | // Berechung von sinus und cosinus |
||
190 | nicktilt = (IntegralNick/div_faktor); |
||
191 | sin_nick = sin_f(nicktilt); |
||
192 | cos_nick = cos_f(nicktilt); |
||
193 | |||
194 | rolltilt = (IntegralRoll/div_faktor); |
||
195 | sin_roll = sin_f(rolltilt); |
||
196 | cos_roll = cos_f(rolltilt); |
||
197 | |||
198 | // Offset |
||
199 | x_axis = (MM3.x_axis - MM3_calib.X_off); |
||
200 | y_axis = (MM3.y_axis - MM3_calib.Y_off); |
||
201 | z_axis = (MM3.z_axis - MM3_calib.Z_off); |
||
202 | |||
203 | // Normierung Wertebereich |
||
204 | if ((MM3_calib.X_range > MM3_calib.Y_range) && (MM3_calib.X_range > MM3_calib.Z_range)) |
||
205 | { |
||
206 | y_axis = ((long)y_axis * MM3_calib.X_range) / MM3_calib.Y_range; |
||
207 | z_axis = ((long)z_axis * MM3_calib.X_range) / MM3_calib.Z_range; |
||
208 | } |
||
209 | else if ((MM3_calib.Y_range > MM3_calib.X_range) && (MM3_calib.Y_range > MM3_calib.Z_range)) |
||
210 | { |
||
211 | x_axis = ((long)x_axis * MM3_calib.Y_range) / MM3_calib.X_range; |
||
212 | z_axis = ((long)z_axis * MM3_calib.Y_range) / MM3_calib.Z_range; |
||
213 | } |
||
214 | else //if ((MM3_calib.Z_range > MM3_calib.X_range) && (MM3_calib.Z_range > MM3_calib.Y_range)) |
||
215 | { |
||
216 | x_axis = ((long)x_axis * MM3_calib.Z_range) / MM3_calib.X_range; |
||
217 | y_axis = ((long)y_axis * MM3_calib.Z_range) / MM3_calib.Y_range; |
||
218 | } |
||
219 | |||
220 | //DebugOut.Analog[9] = x_axis; |
||
221 | //DebugOut.Analog[10] = y_axis; |
||
222 | //DebugOut.Analog[11] = z_axis; |
||
223 | |||
224 | // Neigungskompensation |
||
225 | x_corr = x_axis * cos_nick; |
||
226 | x_corr += y_axis * sin_roll * sin_nick; |
||
227 | x_corr -= z_axis * cos_roll * sin_nick; |
||
228 | |||
229 | y_corr = y_axis * cos_roll; |
||
230 | y_corr += z_axis * sin_roll; |
||
231 | |||
232 | // Winkelberechnung |
||
233 | heading = atan2_i(x_corr, y_corr); |
||
234 | if (heading < 0) heading = -heading; |
||
235 | else heading = 360 - heading; |
||
236 | |||
237 | /* |
||
238 | if (!x_corr && y_corr <0) return (90); |
||
239 | if (!x_corr && y_corr >0) return (270); |
||
240 | |||
241 | heading = atan(y_corr/x_corr)*57.29578; |
||
242 | if (x_corr < 0) heading = 180-heading; |
||
243 | if (x_corr > 0 && y_corr < 0) heading = -heading; |
||
244 | if (x_corr > 0 && y_corr > 0) heading = 360 - heading; |
||
245 | */ |
||
246 | return (heading); |
||
247 | } |