Rev 235 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
205 | Nick666 | 1 | /* |
2 | This program (files compass.c and compass.h) is free software; you can redistribute it and/or modify |
||
3 | it under the terms of the GNU General Public License as published by the Free Software Foundation; |
||
4 | either version 3 of the License, or (at your option) any later version. |
||
5 | This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
||
6 | without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
7 | GNU General Public License for more details. You should have received a copy of the GNU General Public License |
||
8 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
9 | |||
10 | 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 |
||
11 | */ |
||
12 | |||
13 | #include "main.h" |
||
14 | |||
15 | MM3_struct MM3; |
||
16 | |||
17 | |||
18 | //############################################################################ |
||
19 | //Initialisierung der SPI-Schnittstelle |
||
20 | void init_spi(void) |
||
21 | //############################################################################ |
||
22 | { |
||
226 | Nick666 | 23 | SPCR = (1<<SPIE)|(1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0); //Interrupt an, Master, 156 kHz Oszillator |
219 | Nick666 | 24 | //SPSR = (1<<SPI2X); |
226 | Nick666 | 25 | |
26 | DDRB |= (1<<PB7)|(1<<PB5)|(1<<PB2); // J8, MOSI, SCK Ausgang |
||
27 | |||
239 | Nick666 | 28 | PORTD &= ~(1<<PD3); // J5 auf Low |
29 | |||
205 | Nick666 | 30 | MM3.AXIS = MM3_X; |
31 | MM3.STATE = MM3_RESET; |
||
32 | } |
||
33 | |||
34 | |||
35 | //############################################################################ |
||
36 | //Wird in der SIGNAL (SIG_OVERFLOW0) aufgerufen |
||
37 | void MM3_timer0(void) |
||
38 | //############################################################################ |
||
39 | { |
||
40 | switch (MM3.STATE) |
||
41 | { |
||
42 | case MM3_RESET: |
||
43 | PORTB |= (1<<PB2); // J8 auf High, MM3 Reset |
||
44 | MM3.STATE = MM3_START_TRANSFER; |
||
45 | break; |
||
46 | |||
47 | case MM3_START_TRANSFER: |
||
226 | Nick666 | 48 | PORTB &= ~(1<<PB2); // J8 auf Low (war ~125 µs auf High) |
205 | Nick666 | 49 | |
226 | Nick666 | 50 | if (MM3.AXIS == MM3_X) SPDR = 0x51; // Schreiben ins SPDR löst automatisch Übertragung (MOSI und MISO) aus |
51 | else if (MM3.AXIS == MM3_Y) SPDR = 0x52; // Micromag Period Select ist auf 1024 (0x50) |
||
52 | else if (MM3.AXIS == MM3_Z) SPDR = 0x53; // 1: x-Achse, 2: Y-Achse, 3: Z-Achse |
||
205 | Nick666 | 53 | |
232 | Nick666 | 54 | MM3.DRDY = SetDelay(15); // Laut Datenblatt max. Zeit bis Messung fertig (bei PS 1024) |
205 | Nick666 | 55 | MM3.STATE = MM3_WAIT_DRDY; |
56 | break; |
||
57 | |||
58 | case MM3_WAIT_DRDY: |
||
226 | Nick666 | 59 | if (CheckDelay(MM3.DRDY)) {SPDR = 0x00;MM3.STATE = MM3_DRDY;} // Irgendwas ins SPDR, damit Übertragung ausgelöst wird, wenn Wartezeit vorbei |
205 | Nick666 | 60 | break; |
61 | |||
62 | case MM3_IDLE: |
||
63 | break; |
||
64 | } |
||
65 | } |
||
226 | Nick666 | 66 | |
205 | Nick666 | 67 | |
68 | //############################################################################ |
||
69 | //SPI byte ready |
||
70 | SIGNAL (SIG_SPI) |
||
71 | //############################################################################ |
||
206 | Nick666 | 72 | { |
205 | Nick666 | 73 | switch (MM3.STATE) |
74 | { |
||
226 | Nick666 | 75 | case MM3_DRDY: |
76 | // 1. Byte ist da, abspeichern, an die MSB-Stelle rücken und Übertragung von 2. Byte auslösen |
||
205 | Nick666 | 77 | if (MM3.AXIS == MM3_X) {MM3.x_axis=SPDR; MM3.x_axis<<=8; SPDR=0x00; MM3.STATE=MM3_X_BYTE2; break;} |
78 | if (MM3.AXIS == MM3_Y) {MM3.y_axis=SPDR; MM3.y_axis<<=8; SPDR=0x00; MM3.STATE=MM3_Y_BYTE2; break;} |
||
79 | if (MM3.AXIS == MM3_Z) {MM3.z_axis=SPDR; MM3.z_axis<<=8; SPDR=0x00; MM3.STATE=MM3_Z_BYTE2; break;} |
||
80 | |||
226 | Nick666 | 81 | case MM3_X_BYTE2: // 2. Byte der entsprechenden Achse ist da. |
82 | MM3.x_axis |= SPDR; |
||
83 | MM3.x_axis -= OFF_X; // Sofort Offset aus der Kalibrierung berücksichtigen |
||
205 | Nick666 | 84 | MM3.AXIS = MM3_Y; |
85 | MM3.STATE = MM3_RESET; |
||
86 | break; |
||
87 | |||
88 | case MM3_Y_BYTE2: |
||
226 | Nick666 | 89 | MM3.y_axis |= SPDR; |
90 | MM3.y_axis -= OFF_Y; |
||
205 | Nick666 | 91 | MM3.AXIS = MM3_Z; |
92 | MM3.STATE = MM3_RESET; |
||
93 | break; |
||
94 | |||
95 | case MM3_Z_BYTE2: |
||
226 | Nick666 | 96 | MM3.z_axis |= SPDR; |
97 | MM3.z_axis -= OFF_Z; |
||
205 | Nick666 | 98 | MM3.AXIS = MM3_X; |
226 | Nick666 | 99 | MM3.STATE = MM3_RESET; |
227 | Nick666 | 100 | // Zeitnahe Speicherung der aktuellen Nick-/Rollneigung in ° |
232 | Nick666 | 101 | MM3.NickGrad = IntegralNick/(EE_Parameter.UserParam1*8); |
102 | MM3.RollGrad = IntegralRoll/(EE_Parameter.UserParam2*8); |
||
205 | Nick666 | 103 | break; |
104 | } |
||
226 | Nick666 | 105 | } |
205 | Nick666 | 106 | |
226 | Nick666 | 107 | signed int MM3_heading(void) |
108 | { |
||
109 | float sin_nick, cos_nick, sin_roll, cos_roll; |
||
110 | signed int x_corr, y_corr; |
||
111 | signed int heading; |
||
112 | |||
113 | //Berechung von sinus und cosinus |
||
229 | Nick666 | 114 | sin_nick = (float)sin_f(MM3.NickGrad); |
115 | cos_nick = (float)cos_f(MM3.NickGrad); |
||
116 | sin_roll = (float)sin_f(MM3.RollGrad); |
||
117 | cos_roll = (float)cos_f(MM3.RollGrad); |
||
226 | Nick666 | 118 | |
119 | //Neigungskompensation |
||
120 | y_corr = ((cos_roll * MM3.y_axis) + (sin_roll * MM3.z_axis)); |
||
121 | x_corr = (((sin_roll * MM3.y_axis) - (cos_roll * MM3.z_axis)) * sin_nick) + (cos_nick * MM3.x_axis); |
||
235 | Nick666 | 122 | |
226 | Nick666 | 123 | //Winkelberechnung |
232 | Nick666 | 124 | heading = arctan_i(x_corr, y_corr); |
226 | Nick666 | 125 | |
126 | return (heading); |
||
205 | Nick666 | 127 | } |