Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
891 | Nick666 | 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 | // SPI-Schnittstelle initialisieren |
||
30 | SPCR = (1<<SPIE)|(1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0); // Interrupt an, Master, 156 kHz Oszillator |
||
31 | |||
32 | DDRB |= (1<<PB7)|(1<<PB5); // MOSI, SCK Ausgang |
||
33 | DDRC |= (1<<PC4)|(1<<PC5); // PC5 (RESET) und PC4 (SSNOT) als Ausgang |
||
34 | PORTC |= (1<<PC4); // PC4 (SSNOT) auf High -> MM3 passiv |
||
35 | PORTC &= ~(1<<PC5); // PC5 (RESET) auf Low |
||
36 | |||
37 | // Init Statemachine |
||
38 | MM3.AXIS = MM3_X; |
||
39 | MM3.STATE = MM3_RESET; |
||
40 | |||
41 | // Kalibrierung aus dem EEprom lesen |
||
42 | eeprom_read_block(&MM3_calib,&ee_calib,sizeof(struct MM3_calib_struct)); |
||
43 | } |
||
44 | |||
45 | |||
46 | //############################################################################ |
||
47 | // Wird in der SIGNAL (SIG_OVERFLOW0) aufgerufen |
||
48 | void timer0_MM3(void) |
||
49 | //############################################################################ |
||
50 | { |
||
51 | switch (MM3.STATE) |
||
52 | { |
||
53 | case MM3_RESET: |
||
54 | PORTC &= ~(1<<PC4); // MM3 aktiv |
||
55 | PORTC |= (1<<PC5); // MM3 Reset |
||
56 | MM3.STATE = MM3_START_TRANSFER; |
||
57 | return; |
||
58 | |||
59 | case MM3_START_TRANSFER: |
||
60 | PORTC &= ~(1<<PC5); // PC5 auf Low (war ~125 µs auf High) |
||
61 | |||
62 | if (MM3.AXIS == MM3_X) SPDR = MM3_PERIOD_512 + MM3_X_AXIS; // Schreiben ins SPDR löst automatisch SPI-Übertragung (MOSI und MISO) aus |
||
63 | else if (MM3.AXIS == MM3_Y) SPDR = MM3_PERIOD_512 + MM3_Y_AXIS; // Micromag Period Select ist 256 (0x30) |
||
64 | else SPDR = MM3_PERIOD_512 + MM3_Z_AXIS; //if (MM3.AXIS == MM3_Z) |
||
65 | |||
892 | Nick666 | 66 | MM3.DRDY = SetDelay(8); // Laut Datenblatt max. Zeit bis Messung fertig |
891 | Nick666 | 67 | MM3.STATE = MM3_WAIT_DRDY; |
68 | return; |
||
69 | |||
70 | case MM3_WAIT_DRDY: |
||
71 | if (CheckDelay(MM3.DRDY)) {SPDR = 0x00;MM3.STATE = MM3_DRDY;} // Irgendwas ins SPDR, damit Übertragung ausgelöst wird, wenn Wartezeit vorbei |
||
72 | return; // Jetzt gehts weiter in SIGNAL (SIG_SPI) |
||
73 | } |
||
74 | } |
||
75 | |||
76 | |||
77 | //############################################################################ |
||
78 | // SPI byte ready |
||
79 | SIGNAL (SIG_SPI) |
||
80 | //############################################################################ |
||
81 | { |
||
82 | static char tmp; |
||
83 | int value; |
||
84 | |||
85 | switch (MM3.STATE) |
||
86 | { |
||
87 | case MM3_DRDY: // 1. Byte ist da, zwischenspeichern |
||
88 | tmp = SPDR; |
||
89 | SPDR = 0x00; // Übertragung von 2. Byte auslösen |
||
90 | MM3.STATE = MM3_BYTE2; |
||
91 | return; |
||
92 | |||
93 | case MM3_BYTE2: // 2. Byte der entsprechenden Achse ist da |
||
94 | value = tmp; |
||
95 | value <<= 8; // 1. Byte an MSB-Stelle rücken |
||
96 | value |= SPDR; // 2. Byte dranpappen |
||
97 | |||
98 | if(abs(value) < Max_Axis_Value) // Spikes filtern. Zuweisung nur, wenn Max-Wert nicht überschritten |
||
99 | switch (MM3.AXIS) |
||
100 | { |
||
101 | case MM3_X: |
||
102 | MM3.x_axis = value; |
||
103 | MM3.AXIS = MM3_Y; |
||
104 | break; |
||
105 | case MM3_Y: |
||
106 | MM3.y_axis = value; |
||
107 | MM3.AXIS = MM3_Z; |
||
108 | break; |
||
109 | default: //case MM3_Z: |
||
110 | MM3.z_axis = value; |
||
111 | MM3.AXIS = MM3_X; |
||
112 | } |
||
113 | PORTC |= (1<<PC4); // MM3 passiv |
||
114 | MM3.STATE = MM3_RESET; |
||
115 | } |
||
116 | } |
||
117 | |||
118 | //############################################################################ |
||
119 | // Kompass kalibrieren |
||
120 | void calib_MM3(void) |
||
121 | //############################################################################ |
||
122 | { |
||
123 | int16_t x_min=0,x_max=0,y_min=0,y_max=0,z_min=0,z_max=0; |
||
124 | int16_t x_axis, y_axis, z_axis; |
||
125 | uint8_t measurement=50,beeper=0; |
||
126 | |||
127 | GRN_ON; |
||
128 | ROT_OFF; |
||
129 | |||
130 | while (measurement) |
||
131 | { |
||
132 | uint8_t tmp_sreg = SREG; |
||
133 | cli(); |
||
134 | x_axis = MM3.x_axis; |
||
135 | y_axis = MM3.y_axis; |
||
136 | z_axis = MM3.z_axis; |
||
137 | SREG = tmp_sreg; |
||
138 | |||
139 | if (x_axis > x_max) x_max = x_axis; |
||
140 | else if (x_axis < x_min) x_min = x_axis; |
||
141 | |||
142 | if (y_axis > y_max) y_max = y_axis; |
||
143 | else if (y_axis < y_min) y_min = y_axis; |
||
144 | |||
145 | if (z_axis > z_max) z_max = z_axis; |
||
146 | else if (z_axis < z_min) z_min = z_axis; |
||
147 | |||
148 | if (!beeper) |
||
149 | { |
||
150 | ROT_FLASH; |
||
151 | GRN_FLASH; |
||
152 | } |
||
153 | beeper--; |
||
154 | |||
155 | // Schleife mit 100 Hz |
||
156 | Delay_ms(10); |
||
157 | |||
158 | // Wenn Gas zurück genommen wird, Kalibrierung mit 1/2 Sekunde Verzögerung beenden |
||
159 | if (PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] < 100) measurement--; |
||
160 | } |
||
161 | |||
162 | // Wertebereich der Achsen |
||
163 | MM3_calib.X_range = (x_max - x_min); |
||
164 | MM3_calib.Y_range = (y_max - y_min); |
||
165 | MM3_calib.Z_range = (z_max - z_min); |
||
166 | |||
167 | // Offset der Achsen |
||
168 | MM3_calib.X_off = (x_max + x_min) /2; |
||
169 | MM3_calib.Y_off = (y_max + y_min) /2; |
||
170 | MM3_calib.Z_off = (z_max + z_min) /2; |
||
171 | |||
172 | // und im EEProm abspeichern |
||
173 | eeprom_write_block(&MM3_calib,&ee_calib,sizeof(struct MM3_calib_struct)); |
||
174 | } |
||
175 | |||
176 | |||
177 | //############################################################################ |
||
178 | // Neigungskompensierung und Berechnung der Ausrichtung |
||
179 | int heading_MM3(void) |
||
180 | //############################################################################ |
||
181 | { |
||
182 | int16_t sin_nick, cos_nick, sin_roll, cos_roll; |
||
183 | int16_t mm3_x_axis, mm3_y_axis, mm3_z_axis; |
||
184 | int32_t Hx, Hy, Hz, x_corr, y_corr; |
||
185 | int16_t heading; |
||
186 | int8_t tilt; |
||
187 | |||
188 | // 16bit-Werte lesen |
||
189 | uint8_t tmp_sreg = SREG; |
||
190 | cli(); |
||
191 | mm3_x_axis = MM3.x_axis; |
||
192 | mm3_y_axis = MM3.y_axis; |
||
193 | mm3_z_axis = MM3.z_axis; |
||
194 | SREG = tmp_sreg; |
||
195 | |||
196 | int temp = Aktuell_az - acc_neutral.compass; |
||
197 | // Lage-Berechnung mittels Acc-Messwerte |
||
198 | tilt = atan2_i(temp,AdWertAccNick*64); |
||
199 | sin_nick = sin_i(tilt); |
||
200 | cos_nick = cos_i(tilt); |
||
201 | |||
202 | tilt = atan2_i(temp,AdWertAccRoll*64); |
||
203 | sin_roll = sin_i(tilt); |
||
204 | cos_roll = cos_i(tilt); |
||
205 | |||
206 | /* |
||
207 | // Lage-Berechnung mittels Gyro-Integral |
||
208 | uint16_t div_faktor; |
||
209 | div_faktor = (uint16_t)EE_Parameter.UserParam3 *8; |
||
210 | |||
211 | tilt = (IntegralNick /div_faktor); |
||
212 | sin_nick = sin_i(tilt); |
||
213 | cos_nick = cos_i(tilt); |
||
214 | |||
215 | tilt = (IntegralRoll /div_faktor); |
||
216 | sin_roll = sin_i(tilt); |
||
217 | cos_roll = cos_i(tilt); |
||
218 | */ |
||
219 | // Offset und Normalisierung |
||
220 | Hx = (((int32_t)(mm3_x_axis - MM3_calib.X_off)) *512) /MM3_calib.X_range; |
||
221 | Hy = (((int32_t)(mm3_y_axis - MM3_calib.Y_off)) *512) /MM3_calib.Y_range; |
||
222 | Hz = (((int32_t)(mm3_z_axis - MM3_calib.Z_off)) *512) /MM3_calib.Z_range; |
||
892 | Nick666 | 223 | |
891 | Nick666 | 224 | // Neigungskompensierung |
225 | x_corr = Hx * cos_nick; |
||
226 | x_corr -= Hz * sin_nick; |
||
227 | x_corr /= 1024; |
||
228 | |||
229 | y_corr = Hy * cos_roll; |
||
230 | y_corr += Hz * sin_roll; |
||
231 | y_corr /= 16; // atan2_i erwartet y_corr *64. Deshalb /16 und nicht /1024 |
||
232 | |||
233 | // Winkelberechnung |
||
234 | heading = atan2_i(x_corr, y_corr); |
||
235 | |||
236 | // Skalieren von +-180° auf 0-360° |
||
237 | if (heading < 0) heading = -heading; |
||
238 | else heading = 360 - heading; |
||
239 | |||
240 | return (heading); |
||
241 | } |