Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
205 | Nick666 | 1 | /* |
256 | Nick666 | 2 | |
3 | Copyright 2007, Niklas Nold |
||
4 | |||
205 | Nick666 | 5 | This program (files compass.c and compass.h) is free software; you can redistribute it and/or modify |
256 | Nick666 | 6 | it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; |
205 | Nick666 | 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 |
||
256 | Nick666 | 10 | GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License |
205 | Nick666 | 11 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
12 | |||
257 | Nick666 | 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 |
205 | Nick666 | 14 | */ |
15 | |||
16 | #include "main.h" |
||
17 | |||
18 | MM3_struct MM3; |
||
341 | Nick666 | 19 | int8_t Kompass_Offset[2] EEMEM; // X_off[0], Y_off[1], Z_off[2] |
20 | int8_t X_off, Y_off, Z_off; |
||
205 | Nick666 | 21 | |
22 | |||
23 | //############################################################################ |
||
341 | Nick666 | 24 | // Initialisierung |
342 | Nick666 | 25 | void MM3_init(void) |
205 | Nick666 | 26 | //############################################################################ |
27 | { |
||
226 | Nick666 | 28 | SPCR = (1<<SPIE)|(1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0); //Interrupt an, Master, 156 kHz Oszillator |
219 | Nick666 | 29 | //SPSR = (1<<SPI2X); |
226 | Nick666 | 30 | |
31 | DDRB |= (1<<PB7)|(1<<PB5)|(1<<PB2); // J8, MOSI, SCK Ausgang |
||
32 | |||
239 | Nick666 | 33 | PORTD &= ~(1<<PD3); // J5 auf Low |
34 | |||
205 | Nick666 | 35 | MM3.AXIS = MM3_X; |
36 | MM3.STATE = MM3_RESET; |
||
341 | Nick666 | 37 | |
38 | // Kalibrierung aus dem EEprom lesen |
||
39 | X_off = (int8_t)eeprom_read_byte(&Kompass_Offset[0]); |
||
40 | Y_off = (int8_t)eeprom_read_byte(&Kompass_Offset[1]); |
||
41 | Z_off = (int8_t)eeprom_read_byte(&Kompass_Offset[2]); |
||
205 | Nick666 | 42 | } |
43 | |||
44 | |||
45 | //############################################################################ |
||
341 | Nick666 | 46 | // Wird in der SIGNAL (SIG_OVERFLOW0) aufgerufen |
205 | Nick666 | 47 | void MM3_timer0(void) |
48 | //############################################################################ |
||
49 | { |
||
50 | switch (MM3.STATE) |
||
51 | { |
||
52 | case MM3_RESET: |
||
53 | PORTB |= (1<<PB2); // J8 auf High, MM3 Reset |
||
54 | MM3.STATE = MM3_START_TRANSFER; |
||
275 | Nick666 | 55 | return; |
205 | Nick666 | 56 | |
57 | case MM3_START_TRANSFER: |
||
226 | Nick666 | 58 | PORTB &= ~(1<<PB2); // J8 auf Low (war ~125 µs auf High) |
205 | Nick666 | 59 | |
341 | Nick666 | 60 | if (MM3.AXIS == MM3_X) SPDR = 0x31; // Schreiben ins SPDR löst automatisch Übertragung (MOSI und MISO) aus |
61 | else if (MM3.AXIS == MM3_Y) SPDR = 0x32; // Micromag Period Select ist auf 256 (0x30) |
||
62 | else if (MM3.AXIS == MM3_Z) SPDR = 0x33; // 1: x-Achse, 2: Y-Achse, 3: Z-Achse |
||
205 | Nick666 | 63 | |
341 | Nick666 | 64 | MM3.DRDY = SetDelay(8); // Laut Datenblatt max. Zeit bis Messung fertig (bei PS 256 eigentlich 4 ms) |
205 | Nick666 | 65 | MM3.STATE = MM3_WAIT_DRDY; |
275 | Nick666 | 66 | return; |
205 | Nick666 | 67 | |
68 | case MM3_WAIT_DRDY: |
||
278 | Nick666 | 69 | if (CheckDelay(MM3.DRDY)) {SPDR = 0x00;MM3.STATE = MM3_DRDY;} // Irgendwas ins SPDR, damit Übertragung ausgelöst wird, wenn Wartezeit vorbei |
341 | Nick666 | 70 | return; // Jetzt gehts weiter in SIGNAL (SIG_SPI) |
349 | Nick666 | 71 | /* |
341 | Nick666 | 72 | case MM3_TILT: // Zeitnahe Speicherung der aktuellen Neigung in ° |
261 | Nick666 | 73 | MM3.NickGrad = IntegralNick/(EE_Parameter.UserParam1*8); |
74 | MM3.RollGrad = IntegralRoll/(EE_Parameter.UserParam2*8); |
||
281 | Nick666 | 75 | |
261 | Nick666 | 76 | MM3.AXIS = MM3_X; |
77 | MM3.STATE = MM3_RESET; |
||
275 | Nick666 | 78 | return; |
349 | Nick666 | 79 | */ |
205 | Nick666 | 80 | } |
81 | } |
||
82 | |||
341 | Nick666 | 83 | |
205 | Nick666 | 84 | //############################################################################ |
341 | Nick666 | 85 | // SPI byte ready |
205 | Nick666 | 86 | SIGNAL (SIG_SPI) |
87 | //############################################################################ |
||
206 | Nick666 | 88 | { |
205 | Nick666 | 89 | switch (MM3.STATE) |
90 | { |
||
280 | Nick666 | 91 | case MM3_DRDY: // 1. Byte ist da, abspeichern, an die MSB-Stelle rücken |
92 | if (MM3.AXIS == MM3_X) |
||
93 | { |
||
94 | MM3.x_axis = SPDR; |
||
95 | MM3.x_axis <<= 8; |
||
96 | } |
||
97 | else if (MM3.AXIS == MM3_Y) |
||
98 | { |
||
99 | MM3.y_axis = SPDR; |
||
100 | MM3.y_axis <<= 8; |
||
101 | } |
||
341 | Nick666 | 102 | else // if (MM3.AXIS == MM3_Z) |
280 | Nick666 | 103 | { |
104 | MM3.z_axis = SPDR; |
||
105 | MM3.z_axis <<= 8; |
||
106 | } |
||
107 | |||
108 | SPDR=0x00; // Übertragung von 2. Byte auslösen |
||
109 | MM3.STATE=MM3_BYTE2; |
||
275 | Nick666 | 110 | return; |
280 | Nick666 | 111 | |
112 | case MM3_BYTE2: // 2. Byte der entsprechenden Achse ist da |
||
113 | if (MM3.AXIS == MM3_X) |
||
114 | { |
||
115 | MM3.x_axis |= SPDR; |
||
341 | Nick666 | 116 | // Spikes filtern |
117 | if (abs(MM3.x_axis) < Max_Axis_Value) MM3.x_axis_old = MM3.x_axis; |
||
118 | else MM3.x_axis = MM3.x_axis_old; |
||
280 | Nick666 | 119 | MM3.AXIS = MM3_Y; |
120 | MM3.STATE = MM3_RESET; |
||
121 | } |
||
122 | else if (MM3.AXIS == MM3_Y) |
||
123 | { |
||
341 | Nick666 | 124 | MM3.y_axis |= SPDR; |
125 | if (abs(MM3.y_axis) < Max_Axis_Value) MM3.y_axis_old = MM3.y_axis; |
||
126 | else MM3.y_axis = MM3.y_axis_old; |
||
280 | Nick666 | 127 | MM3.AXIS = MM3_Z; |
128 | MM3.STATE = MM3_RESET; |
||
129 | } |
||
341 | Nick666 | 130 | else // if (MM3.AXIS == MM3_Z) |
280 | Nick666 | 131 | { |
132 | MM3.z_axis |= SPDR; |
||
341 | Nick666 | 133 | if (abs(MM3.z_axis) < Max_Axis_Value) MM3.z_axis_old = MM3.z_axis; |
134 | else MM3.z_axis = MM3.z_axis_old; |
||
349 | Nick666 | 135 | MM3.AXIS = MM3_X; |
136 | MM3.STATE = MM3_RESET; |
||
280 | Nick666 | 137 | } |
138 | |||
278 | Nick666 | 139 | return; |
205 | Nick666 | 140 | } |
226 | Nick666 | 141 | } |
205 | Nick666 | 142 | |
341 | Nick666 | 143 | //############################################################################ |
144 | // Kompass kalibrieren |
||
145 | void MM3_calib(void) |
||
146 | //############################################################################ |
||
147 | { |
||
148 | signed int x_min=0,x_max=0,y_min=0,y_max=0,z_min=0,z_max=0; |
||
149 | uint8_t measurement=50,beeper=0; |
||
150 | unsigned int timer; |
||
151 | |||
152 | while (measurement) |
||
153 | { |
||
154 | //H_earth = MM3.x_axis*MM3.x_axis + MM3.y_axis*MM3.y_axis + MM3.z_axis*MM3.z_axis; |
||
155 | |||
156 | if (MM3.x_axis > x_max) x_max = MM3.x_axis; |
||
157 | else if (MM3.x_axis < x_min) x_min = MM3.x_axis; |
||
158 | |||
159 | if (MM3.y_axis > y_max) y_max = MM3.y_axis; |
||
160 | else if (MM3.y_axis < y_min) y_min = MM3.y_axis; |
||
161 | |||
162 | if (MM3.z_axis > z_max) z_max = MM3.z_axis; |
||
163 | else if (MM3.z_axis < z_min) z_min = MM3.z_axis; |
||
164 | |||
165 | if (!beeper) |
||
166 | { |
||
167 | beeper = 50; |
||
168 | beeptime = 50; |
||
169 | } |
||
170 | beeper--; |
||
171 | |||
172 | // Schleife mit 100 Hz voll ausreichend |
||
173 | timer = SetDelay(10); |
||
174 | while(!CheckDelay(timer)); |
||
175 | |||
176 | // Wenn Gas zurück genommen wird, Kalibrierung mit Verzögerung beenden |
||
177 | if (PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] < 100) measurement--; |
||
178 | } |
||
179 | |||
343 | Nick666 | 180 | // Offset der Achsen berechnen |
341 | Nick666 | 181 | X_off = (x_max + x_min) / 2; |
182 | Y_off = (y_max + y_min) / 2; |
||
183 | Z_off = (z_max + z_min) / 2; |
||
184 | |||
343 | Nick666 | 185 | // und im EEProm abspeichern |
341 | Nick666 | 186 | eeprom_write_byte(&Kompass_Offset[0], X_off); |
187 | eeprom_write_byte(&Kompass_Offset[1], Y_off); |
||
188 | eeprom_write_byte(&Kompass_Offset[2], Z_off); |
||
189 | |||
190 | } |
||
191 | |||
192 | |||
193 | //############################################################################ |
||
194 | // Neigungskompensierung und Berechnung der Ausrichtung |
||
226 | Nick666 | 195 | signed int MM3_heading(void) |
341 | Nick666 | 196 | //############################################################################ |
226 | Nick666 | 197 | { |
198 | float sin_nick, cos_nick, sin_roll, cos_roll; |
||
341 | Nick666 | 199 | signed int x_corr, y_corr, heading; |
200 | signed int x_axis,y_axis,z_axis; |
||
349 | Nick666 | 201 | |
202 | MM3.NickGrad = IntegralNick/(EE_Parameter.UserParam1*8); |
||
203 | MM3.RollGrad = IntegralRoll/(EE_Parameter.UserParam2*8); |
||
204 | |||
255 | Nick666 | 205 | // Berechung von sinus und cosinus |
240 | Nick666 | 206 | sin_nick = sin_f(MM3.NickGrad); |
207 | cos_nick = cos_f(MM3.NickGrad); |
||
208 | sin_roll = sin_f(MM3.RollGrad); |
||
341 | Nick666 | 209 | cos_roll = cos_f(MM3.RollGrad); |
210 | |||
343 | Nick666 | 211 | // Offset der Achsen nur bei Bedarf (also hier) berücksichtigen |
341 | Nick666 | 212 | x_axis = MM3.x_axis - X_off; |
213 | y_axis = MM3.y_axis - Y_off; |
||
214 | z_axis = MM3.z_axis - Z_off; |
||
226 | Nick666 | 215 | |
255 | Nick666 | 216 | // Neigungskompensation |
341 | Nick666 | 217 | x_corr = (cos_nick * x_axis) + (((sin_roll * y_axis) - (cos_roll * z_axis)) * sin_nick); |
218 | y_corr = ((cos_roll * y_axis) + (sin_roll * z_axis)); |
||
235 | Nick666 | 219 | |
255 | Nick666 | 220 | // Winkelberechnung |
282 | Nick666 | 221 | heading = atan2_i(x_corr, y_corr); |
226 | Nick666 | 222 | |
223 | return (heading); |
||
205 | Nick666 | 224 | } |