Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
716 | 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 |
||
728 | Nick666 | 34 | PORTC |= (1<<PC4); // PC4 (SSNOT) auf High -> MM3 passiv |
35 | PORTC &= ~(1<<PC5); // PC5 (RESET) auf Low |
||
716 | Nick666 | 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 | { |
||
728 | Nick666 | 53 | case MM3_RESET: |
54 | PORTC &= ~(1<<PC4); // MM3 aktiv |
||
55 | PORTC |= (1<<PC5); // MM3 Reset |
||
716 | Nick666 | 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 | |||
729 | Nick666 | 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) |
||
716 | Nick666 | 65 | |
734 | Nick666 | 66 | MM3.DRDY = SetDelay(10); // Laut Datenblatt max. Zeit bis Messung fertig (bei PS 512 eigentlich 8 ms) |
716 | 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; |
||
728 | Nick666 | 83 | int value; |
716 | Nick666 | 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 |
||
728 | Nick666 | 94 | value = tmp; |
95 | value <<= 8; // 1. Byte an MSB-Stelle rücken |
||
96 | value |= SPDR; // 2. Byte dranpappen |
||
716 | Nick666 | 97 | |
728 | Nick666 | 98 | if(abs(value) < Max_Axis_Value) // Spikes filtern. Zuweisung nur, wenn Max-Wert nicht überschritten |
716 | Nick666 | 99 | switch (MM3.AXIS) |
100 | { |
||
101 | case MM3_X: |
||
728 | Nick666 | 102 | MM3.x_axis = value; |
716 | Nick666 | 103 | MM3.AXIS = MM3_Y; |
104 | break; |
||
105 | case MM3_Y: |
||
728 | Nick666 | 106 | MM3.y_axis = value; |
716 | Nick666 | 107 | MM3.AXIS = MM3_Z; |
108 | break; |
||
109 | default: //case MM3_Z: |
||
728 | Nick666 | 110 | MM3.z_axis = value; |
716 | Nick666 | 111 | MM3.AXIS = MM3_X; |
112 | } |
||
734 | Nick666 | 113 | PORTC |= (1<<PC4); // MM3 passiv |
716 | Nick666 | 114 | MM3.STATE = MM3_RESET; |
115 | } |
||
116 | } |
||
117 | |||
118 | //############################################################################ |
||
119 | // Kompass kalibrieren |
||
120 | void calib_MM3(void) |
||
121 | //############################################################################ |
||
122 | { |
||
728 | Nick666 | 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 | uint8_t tmp_sreg; |
||
127 | |||
716 | Nick666 | 128 | GRN_ON; |
129 | ROT_OFF; |
||
130 | |||
131 | while (measurement) |
||
728 | Nick666 | 132 | { |
133 | tmp_sreg = SREG; |
||
134 | cli(); |
||
135 | x_axis = MM3.x_axis; |
||
136 | y_axis = MM3.y_axis; |
||
137 | z_axis = MM3.z_axis; |
||
138 | SREG = tmp_sreg; |
||
139 | |||
140 | if (x_axis > x_max) x_max = x_axis; |
||
141 | else if (x_axis < x_min) x_min = x_axis; |
||
716 | Nick666 | 142 | |
728 | Nick666 | 143 | if (y_axis > y_max) y_max = y_axis; |
144 | else if (y_axis < y_min) y_min = y_axis; |
||
716 | Nick666 | 145 | |
728 | Nick666 | 146 | if (z_axis > z_max) z_max = z_axis; |
147 | else if (z_axis < z_min) z_min = z_axis; |
||
716 | Nick666 | 148 | |
149 | if (!beeper) |
||
150 | { |
||
151 | ROT_FLASH; |
||
152 | GRN_FLASH; |
||
153 | beeptime = 50; |
||
154 | beeper = 50; |
||
155 | } |
||
156 | beeper--; |
||
728 | Nick666 | 157 | |
716 | Nick666 | 158 | // Schleife mit 100 Hz |
728 | Nick666 | 159 | Delay_ms(10); |
716 | Nick666 | 160 | |
161 | // Wenn Gas zurück genommen wird, Kalibrierung mit 1/2 Sekunde Verzögerung beenden |
||
162 | if (PPM_in[EE_Parameter.Kanalbelegung[K_GAS]] < 100) measurement--; |
||
163 | } |
||
164 | |||
165 | // Wertebereich der Achsen |
||
166 | MM3_calib.X_range = (x_max - x_min); |
||
167 | MM3_calib.Y_range = (y_max - y_min); |
||
168 | MM3_calib.Z_range = (z_max - z_min); |
||
169 | |||
170 | // Offset der Achsen |
||
171 | MM3_calib.X_off = (x_max + x_min) /2; |
||
172 | MM3_calib.Y_off = (y_max + y_min) /2; |
||
173 | MM3_calib.Z_off = (z_max + z_min) /2; |
||
174 | |||
175 | // und im EEProm abspeichern |
||
176 | eeprom_write_block(&MM3_calib,&ee_calib,sizeof(struct MM3_calib_struct)); |
||
177 | } |
||
178 | |||
179 | |||
180 | //############################################################################ |
||
181 | // Neigungskompensierung und Berechnung der Ausrichtung |
||
182 | int heading_MM3(void) |
||
183 | //############################################################################ |
||
184 | { |
||
185 | int16_t sin_nick, cos_nick, sin_roll, cos_roll; |
||
728 | Nick666 | 186 | int16_t mm3_x_axis, mm3_y_axis, mm3_z_axis; |
718 | Nick666 | 187 | int32_t Hx, Hy, Hz, x_corr, y_corr; |
716 | Nick666 | 188 | int16_t heading; |
718 | Nick666 | 189 | int8_t tilt; |
728 | Nick666 | 190 | uint8_t tmp_sreg; |
191 | |||
192 | // 16bit-Werte lesen |
||
193 | tmp_sreg = SREG; |
||
194 | cli(); |
||
195 | mm3_x_axis = MM3.x_axis; |
||
196 | mm3_y_axis = MM3.y_axis; |
||
197 | mm3_z_axis = MM3.z_axis; |
||
198 | SREG = tmp_sreg; |
||
199 | |||
200 | // Lage-Berechnung mittels Acc-Messwerte |
||
718 | Nick666 | 201 | tilt = atan2_i(Aktuell_az-556,AdWertAccNick*64); |
202 | sin_nick = sin_i(tilt); |
||
203 | cos_nick = cos_i(tilt); |
||
716 | Nick666 | 204 | |
718 | Nick666 | 205 | tilt = atan2_i(Aktuell_az-556,AdWertAccRoll*64); |
206 | sin_roll = sin_i(tilt); |
||
207 | cos_roll = cos_i(tilt); |
||
728 | Nick666 | 208 | /* |
209 | // Lage-Berechnung mittels Gyro-Integral |
||
740 | Nick666 | 210 | uint16_t div_faktor; |
718 | Nick666 | 211 | div_faktor = (uint16_t)EE_Parameter.UserParam3 *8; |
212 | |||
725 | Nick666 | 213 | tilt = (IntegralNick /div_faktor); |
718 | Nick666 | 214 | sin_nick = sin_i(tilt); |
215 | cos_nick = cos_i(tilt); |
||
216 | |||
725 | Nick666 | 217 | tilt = (IntegralRoll /div_faktor); |
718 | Nick666 | 218 | sin_roll = sin_i(tilt); |
219 | cos_roll = cos_i(tilt); |
||
728 | Nick666 | 220 | */ |
725 | Nick666 | 221 | // Offset und Normalisierung |
729 | Nick666 | 222 | Hx = (((int32_t)(mm3_x_axis - MM3_calib.X_off)) *512) /MM3_calib.X_range; |
223 | Hy = (((int32_t)(mm3_y_axis - MM3_calib.Y_off)) *512) /MM3_calib.Y_range; |
||
224 | Hz = (((int32_t)(mm3_z_axis - MM3_calib.Z_off)) *512) /MM3_calib.Z_range; |
||
716 | Nick666 | 225 | |
226 | // Neigungskompensierung |
||
227 | x_corr = Hx * cos_nick; |
||
228 | x_corr -= Hz * sin_nick; |
||
229 | x_corr /= 1024; |
||
230 | |||
231 | y_corr = Hy * cos_roll; |
||
232 | y_corr += Hz * sin_roll; |
||
728 | Nick666 | 233 | y_corr /= 16; // atan2_i erwartet y_corr *64. Deshalb /16 und nicht /1024 |
716 | Nick666 | 234 | |
235 | // Winkelberechnung |
||
236 | heading = atan2_i(x_corr, y_corr); |
||
237 | |||
238 | // Skalieren von +-180° auf 0-360° |
||
730 | Nick666 | 239 | if (heading < 0) heading = -heading; |
240 | else heading = 360 - heading; |
||
716 | Nick666 | 241 | |
242 | return (heading); |
||
243 | } |