Rev 28 | Rev 30 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 28 | Rev 29 | ||
---|---|---|---|
1 | /*####################################################################################### |
1 | /*####################################################################################### |
2 | MK3Mag 3D-Magnet sensor |
2 | MK3Mag 3D-Magnet sensor |
3 | !!! THIS IS NOT FREE SOFTWARE !!! |
3 | !!! THIS IS NOT FREE SOFTWARE !!! |
4 | #######################################################################################*/ |
4 | #######################################################################################*/ |
5 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
6 | // + Copyright (c) 05.2008 Holger Buss |
6 | // + Copyright (c) 05.2008 Holger Buss |
7 | // + Thanks to Ilja Fähnrich (P_Latzhalter) |
7 | // + Thanks to Ilja Fähnrich (P_Latzhalter) |
8 | // + Nur für den privaten Gebrauch |
8 | // + Nur für den privaten Gebrauch |
9 | // + www.MikroKopter.com |
9 | // + www.MikroKopter.com |
10 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
10 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
11 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
11 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
12 | // + mit unserer Zustimmung zulässig |
12 | // + mit unserer Zustimmung zulässig |
13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
14 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
14 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
15 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
15 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
16 | // + AUSNAHME: Ein bei www.mikrokopter.de erworbener vorbestückter MK3Mag darf als Baugruppe auch in kommerziellen Systemen verbaut werden |
16 | // + AUSNAHME: Ein bei www.mikrokopter.de erworbener vorbestückter MK3Mag darf als Baugruppe auch in kommerziellen Systemen verbaut werden |
17 | // + Im Zweifelsfall bitte anfragen bei: info@mikrokopter.de |
17 | // + Im Zweifelsfall bitte anfragen bei: info@mikrokopter.de |
18 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
18 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
19 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
19 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
20 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
20 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
21 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
21 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
22 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
22 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
23 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
23 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
24 | // + eindeutig als Ursprung verlinkt werden |
24 | // + eindeutig als Ursprung verlinkt werden |
25 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
25 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
26 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
26 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
27 | // + Benutzung auf eigene Gefahr |
27 | // + Benutzung auf eigene Gefahr |
28 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
28 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
29 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
29 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
30 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
30 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
32 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
32 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
33 | // + this list of conditions and the following disclaimer. |
33 | // + this list of conditions and the following disclaimer. |
34 | // + * PORTING this software (or parts of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed |
34 | // + * PORTING this software (or parts of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed |
35 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
35 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
36 | // + from this software without specific prior written permission. |
36 | // + from this software without specific prior written permission. |
37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
38 | // + for non-commercial use (directly or indirectly) |
38 | // + for non-commercial use (directly or indirectly) |
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
40 | // + with our written permission |
40 | // + with our written permission |
41 | // + Exception: A preassembled MK3Mag, purchased from www.mikrokopter.de may be used as a part of commercial systems |
41 | // + Exception: A preassembled MK3Mag, purchased from www.mikrokopter.de may be used as a part of commercial systems |
42 | // + In case of doubt please contact: info@MikroKopter.de |
42 | // + In case of doubt please contact: info@MikroKopter.de |
43 | // + * If sources or documentations are redistributet on other webpages, our webpage (http://www.MikroKopter.de) must be |
43 | // + * If sources or documentations are redistributet on other webpages, our webpage (http://www.MikroKopter.de) must be |
44 | // + clearly linked as origin |
44 | // + clearly linked as origin |
45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
46 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
46 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
57 | #include <avr/interrupt.h> |
57 | #include <avr/interrupt.h> |
58 | #include <math.h> |
58 | #include <math.h> |
59 | #include <stdlib.h> |
59 | #include <stdlib.h> |
60 | #include <stdio.h> |
60 | #include <stdio.h> |
61 | 61 | ||
62 | #include "main.h" |
62 | #include "main.h" |
63 | #include "timer0.h" |
63 | #include "timer0.h" |
64 | #include "twislave.h" |
64 | #include "twislave.h" |
65 | #include "led.h" |
65 | #include "led.h" |
66 | #include "analog.h" |
66 | #include "analog.h" |
67 | #include "uart.h" |
67 | #include "uart.h" |
68 | 68 | ||
69 | 69 | ||
70 | AttitudeSource_t AttitudeSource = ATTITUDE_SOURCE_ACC; |
70 | AttitudeSource_t AttitudeSource = ATTITUDE_SOURCE_ACC; |
- | 71 | Orientation_t Orientation = ORIENTATION_FC; |
|
71 | 72 | ||
72 | uint16_t Led_Timer = 0; |
73 | uint16_t Led_Timer = 0; |
73 | 74 | ||
74 | struct Scaling_t |
75 | struct Scaling_t |
75 | { |
76 | { |
76 | int16_t Range; |
77 | int16_t Range; |
77 | int16_t Offset; |
78 | int16_t Offset; |
78 | } ; |
79 | } ; |
79 | 80 | ||
80 | struct Calibration_t |
81 | struct Calibration_t |
81 | { |
82 | { |
82 | struct Scaling_t MagX; |
83 | struct Scaling_t MagX; |
83 | struct Scaling_t MagY; |
84 | struct Scaling_t MagY; |
84 | struct Scaling_t MagZ; |
85 | struct Scaling_t MagZ; |
85 | struct Scaling_t AccX; |
86 | struct Scaling_t AccX; |
86 | struct Scaling_t AccY; |
87 | struct Scaling_t AccY; |
87 | struct Scaling_t AccZ; |
88 | struct Scaling_t AccZ; |
88 | } ; |
89 | }; |
89 | 90 | ||
90 | struct Calibration_t eeCalibration EEMEM; // calibration data in EEProm |
91 | struct Calibration_t eeCalibration EEMEM; // calibration data in EEProm |
91 | struct Calibration_t Calibration; // calibration data in RAM |
92 | struct Calibration_t Calibration; // calibration data in RAM |
92 | 93 | ||
93 | // magnet sensor variable |
94 | // magnet sensor variable |
94 | int16_t RawMagnet1a, RawMagnet1b; // raw magnet sensor data |
95 | int16_t RawMagnet1a, RawMagnet1b; // raw magnet sensor data |
95 | int16_t RawMagnet2a, RawMagnet2b; |
96 | int16_t RawMagnet2a, RawMagnet2b; |
96 | int16_t RawMagnet3a, RawMagnet3b; |
97 | int16_t RawMagnet3a, RawMagnet3b; |
97 | int16_t UncalMagX, UncalMagY, UncalMagZ; // sensor signal difference without Scaling |
98 | int16_t UncalMagX, UncalMagY, UncalMagZ; // sensor signal difference without Scaling |
98 | int16_t MagX, MagY, MagZ; // rescaled magnetic field readings |
99 | int16_t MagX = 0, MagY = 0, MagZ = 0; // rescaled magnetic field readings |
- | 100 | ||
99 | // acc sensor variables |
101 | // acceleration sensor variables |
100 | int16_t RawAccX, RawAccY, RawAccZ; // raw acceleration readings |
102 | int16_t RawAccX = 0, RawAccY = 0, RawAccZ = 0; // raw acceleration readings |
101 | int16_t AccX, AccY, AccZ; // rescaled acceleration readings |
103 | int16_t AccX = 0, AccY = 0, AccZ = 0; // rescaled acceleration readings |
102 | uint8_t AccPresent = 0; |
- | |
103 | uint8_t PC_Connected = 0; |
104 | int16_t AccAttitudeNick = 0, AccAttitudeRoll = 0; // nick and roll angle from acc |
104 | 105 | ||
105 | int16_t Heading = -1; |
106 | int16_t Heading = -1; // the current compass heading in deg |
106 | 107 | ||
107 | 108 | ||
108 | void CalcFields(void) |
109 | void CalcFields(void) |
109 | { |
110 | { |
110 | UncalMagX = (RawMagnet1a - RawMagnet1b); |
111 | UncalMagX = (RawMagnet1a - RawMagnet1b); |
111 | UncalMagY = (RawMagnet3a - RawMagnet3b); |
112 | UncalMagY = (RawMagnet3a - RawMagnet3b); |
112 | UncalMagZ = (RawMagnet2a - RawMagnet2b); |
113 | UncalMagZ = (RawMagnet2a - RawMagnet2b); |
113 | 114 | ||
114 | if(Calibration.MagX.Range != 0) MagX = (1024L * (int32_t)(UncalMagX - Calibration.MagX.Offset)) / (Calibration.MagX.Range); |
115 | if(Calibration.MagX.Range != 0) MagX = (1024L * (int32_t)(UncalMagX - Calibration.MagX.Offset)) / (Calibration.MagX.Range); |
115 | else MagX = 0; |
116 | else MagX = 0; |
116 | if(Calibration.MagY.Range != 0) MagY = (1024L * (int32_t)(UncalMagY - Calibration.MagY.Offset)) / (Calibration.MagY.Range); |
117 | if(Calibration.MagY.Range != 0) MagY = (1024L * (int32_t)(UncalMagY - Calibration.MagY.Offset)) / (Calibration.MagY.Range); |
117 | else MagY = 0; |
118 | else MagY = 0; |
118 | if(Calibration.MagY.Range != 0) MagZ = (1024L * (int32_t)(UncalMagZ - Calibration.MagZ.Offset)) / (Calibration.MagZ.Range); |
119 | if(Calibration.MagY.Range != 0) MagZ = (1024L * (int32_t)(UncalMagZ - Calibration.MagZ.Offset)) / (Calibration.MagZ.Range); |
119 | else MagZ = 0; |
120 | else MagZ = 0; |
- | 121 | ||
- | 122 | if(AccPresent) |
|
- | 123 | { |
|
- | 124 | AccX = (RawAccX - Calibration.AccX.Offset); |
|
- | 125 | AccY = (RawAccY - Calibration.AccY.Offset); |
|
- | 126 | AccZ = (Calibration.AccZ.Offset - RawAccZ); |
|
- | 127 | ||
- | 128 | #if (BOARD == 10) // the hardware 1.0 has the LIS3L02AL |
|
- | 129 | // acc mode assumes orientation like FC |
|
- | 130 | if(AccX > 136) AccAttitudeNick = -800; |
|
- | 131 | else |
|
- | 132 | if(AccX < -136) AccAttitudeNick = 800; |
|
- | 133 | else AccAttitudeNick = (int16_t)(-1800.0 * asin((double) AccX / 138.0) / M_PI); |
|
- | 134 | ||
- | 135 | ||
- | 136 | if(AccY > 136) AccAttitudeRoll = 800; |
|
- | 137 | else |
|
- | 138 | if(AccY < -136) AccAttitudeRoll = -800; |
|
- | 139 | else AccAttitudeRoll = (int16_t)( 1800.0 * asin((double) AccY / 138.0) / M_PI); |
|
- | 140 | ||
- | 141 | #else // the hardware 1.1 has the LIS344ALH with a different axis definition (X -> -Y, Y -> X, Z -> Z) |
|
- | 142 | // acc mode assumes orientation like FC |
|
- | 143 | if(AccY > 136) AccAttitudeNick = 800; |
|
- | 144 | else |
|
- | 145 | if(AccY < -136) AccAttitudeNick = -800; |
|
- | 146 | else AccAttitudeNick = (int16_t)( 1800.0 * asin((double) AccY / 138.0) / M_PI); |
|
- | 147 | ||
- | 148 | ||
- | 149 | if(AccX > 136) AccAttitudeRoll = 800; |
|
- | 150 | else |
|
- | 151 | if(AccX < -136) AccAttitudeRoll = -800; |
|
- | 152 | else AccAttitudeRoll = (int16_t)( 1800.0 * asin((double) AccX / 138.0) / M_PI); |
|
- | 153 | #endif |
|
- | 154 | } |
|
120 | } |
155 | } |
121 | 156 | ||
122 | 157 | ||
123 | void CalcHeading(void) |
158 | void CalcHeading(void) |
124 | { |
159 | { |
125 | double nick_rad, roll_rad, Hx, Hy, Cx, Cy, Cz; |
160 | double nick_rad, roll_rad, Hx, Hy, Cx = 0.0, Cy = 0.0, Cz = 0.0; |
126 | int16_t heading = -1; |
161 | int16_t heading = -1; |
127 | 162 | ||
128 | // blink code for normal operation |
163 | // blink code for normal operation |
129 | if(CheckDelay(Led_Timer)) |
164 | if(CheckDelay(Led_Timer)) |
130 | { |
165 | { |
131 | LED_GRN_TOGGLE; |
166 | LED_GRN_TOGGLE; |
132 | Led_Timer = SetDelay(500); |
167 | Led_Timer = SetDelay(500); |
133 | } |
168 | } |
- | 169 | ||
- | 170 | switch(Orientation) |
|
- | 171 | { |
|
134 | 172 | case ORIENTATION_NC: |
|
135 | Cx = MagX; |
173 | Cx = MagX; |
136 | Cy = MagY; |
174 | Cy = MagY; |
- | 175 | Cz = MagZ; |
|
137 | Cz = MagZ; |
176 | break; |
- | 177 | ||
138 | 178 | case ORIENTATION_FC: |
|
139 | if(ExternData.Orientation == 1) |
- | |
140 | { |
179 | // rotation of 90 deg compared to NC setup |
141 | Cx = MagX; |
180 | Cx = MagY; |
142 | Cy = -MagY; |
181 | Cy = -MagX; |
- | 182 | Cz = MagZ; |
|
143 | Cz = MagZ; |
183 | break; |
144 | } |
184 | } |
145 | 185 | ||
146 | // calculate nick and roll angle in rad |
186 | // calculate nick and roll angle in rad |
147 | switch(AttitudeSource) |
187 | switch(AttitudeSource) |
148 | { |
188 | { |
149 | case ATTITUDE_SOURCE_I2C: |
189 | case ATTITUDE_SOURCE_I2C: |
150 | nick_rad = ((double)I2C_WriteAttitude.Nick) * M_PI / (double)(1800.0); |
190 | nick_rad = ((double)I2C_WriteAttitude.Nick) * M_PI / (double)(1800.0); |
151 | roll_rad = ((double)I2C_WriteAttitude.Roll) * M_PI / (double)(1800.0); |
191 | roll_rad = ((double)I2C_WriteAttitude.Roll) * M_PI / (double)(1800.0); |
152 | break; |
192 | break; |
153 | 193 | ||
154 | case ATTITUDE_SOURCE_UART: |
194 | case ATTITUDE_SOURCE_UART: |
155 | nick_rad = ((double)ExternData.Attitude[NICK]) * M_PI / (double)(1800.0); |
195 | nick_rad = ((double)ExternData.Attitude[NICK]) * M_PI / (double)(1800.0); |
156 | roll_rad = ((double)ExternData.Attitude[ROLL]) * M_PI / (double)(1800.0); |
196 | roll_rad = ((double)ExternData.Attitude[ROLL]) * M_PI / (double)(1800.0); |
157 | break; |
197 | break; |
158 | 198 | ||
159 | case ATTITUDE_SOURCE_ACC: |
199 | case ATTITUDE_SOURCE_ACC: |
160 | if(AccX > 125) nick_rad = M_PI / 2; |
- | |
161 | else |
- | |
162 | if(AccX < -125) nick_rad = -M_PI / 2; |
- | |
163 | else |
- | |
164 | { |
- | |
165 | nick_rad = asin((double) AccX / 125.0); |
200 | nick_rad = ((double)AccAttitudeNick) * M_PI / (double)(1800.0); |
166 | } |
- | |
167 | - | ||
168 | if(AccY > 125) roll_rad = M_PI / 2; |
- | |
169 | else |
- | |
170 | if(AccY < -125) roll_rad = -M_PI / 2; |
- | |
171 | else |
- | |
172 | { |
- | |
173 | roll_rad = asin((double) AccY / 125.0); |
201 | roll_rad = ((double)AccAttitudeRoll) * M_PI / (double)(1800.0); |
174 | } |
- | |
175 | break; |
202 | break; |
- | 203 | ||
- | 204 | default: |
|
- | 205 | nick_rad = 0; |
|
- | 206 | roll_rad = 0; |
|
- | 207 | break; |
|
176 | } |
208 | } |
177 | 209 | ||
178 | // calculate attitude correction |
210 | // calculate attitude correction |
179 | Hx = Cx * cos(nick_rad) - Cz * sin(nick_rad); |
211 | Hx = Cx * cos(nick_rad) - Cz * sin(nick_rad); |
180 | Hy = Cy * cos(roll_rad) + Cz * sin(roll_rad); |
212 | Hy = Cy * cos(roll_rad) + Cz * sin(roll_rad); |
- | 213 | ||
- | 214 | DebugOut.Analog[27] = (int16_t)Hx; |
|
- | 215 | DebugOut.Analog[28] = (int16_t)Hy; |
|
- | 216 | ||
181 | 217 | ||
182 | // calculate Heading |
218 | // calculate Heading |
183 | heading = (int16_t)((180.0 * atan2(Hy, Hx)) / M_PI); |
219 | heading = (int16_t)((180.0 * atan2(Hy, Hx)) / M_PI); |
184 | // atan2 returns angular range from -180 deg to 180 deg in counter clockwise notation |
220 | // atan2 returns angular range from -180 deg to 180 deg in counter clockwise notation |
185 | // but the compass course is defined in a range from 0 deg to 360 deg clockwise notation. |
221 | // but the compass course is defined in a range from 0 deg to 360 deg clockwise notation. |
186 | if (heading < 0) heading = -heading; |
222 | if (heading < 0) heading = -heading; |
187 | else heading = 360 - heading; |
223 | else heading = 360 - heading; |
188 | 224 | ||
189 | if(abs(heading) < 361) Heading = heading; |
225 | if(abs(heading) < 361) Heading = heading; |
190 | else (Heading = -1); |
226 | else (Heading = -1); |
191 | } |
227 | } |
192 | 228 | ||
193 | 229 | ||
194 | void Calibrate(void) |
230 | void Calibrate(void) |
195 | { |
231 | { |
196 | uint8_t cal; |
232 | uint8_t cal; |
197 | static uint8_t calold = 0; |
233 | static uint8_t calold = 0; |
198 | static int16_t Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0; |
234 | static int16_t Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0; |
199 | static uint8_t blinkcount = 0; |
235 | static uint8_t blinkcount = 0; |
200 | 236 | ||
201 | // check both sources of communication for calibration request |
237 | // check both sources of communication for calibration request |
202 | if(I2C_WriteCal.CalByte) cal = I2C_WriteCal.CalByte; |
238 | if(I2C_WriteCal.CalByte) cal = I2C_WriteCal.CalByte; |
203 | else cal = ExternData.CalState; |
239 | else cal = ExternData.CalState; |
204 | 240 | ||
205 | 241 | ||
206 | if(cal > 5) cal = 0; |
242 | if(cal > 5) cal = 0; |
207 | // blink code for current calibration state |
243 | // blink code for current calibration state |
208 | if(cal) |
244 | if(cal) |
209 | { |
245 | { |
210 | if(CheckDelay(Led_Timer) || (cal != calold)) |
246 | if(CheckDelay(Led_Timer) || (cal != calold)) |
211 | { |
247 | { |
212 | if(blinkcount & 0x01) LED_GRN_OFF; |
248 | if(blinkcount & 0x01) LED_GRN_OFF; |
213 | else LED_GRN_ON; |
249 | else LED_GRN_ON; |
214 | 250 | ||
215 | // end of blinkcount sequence |
251 | // end of blinkcount sequence |
216 | if( (blinkcount + 1 ) >= (2 * cal) ) |
252 | if( (blinkcount + 1 ) >= (2 * cal) ) |
217 | { |
253 | { |
218 | blinkcount = 0; |
254 | blinkcount = 0; |
219 | Led_Timer = SetDelay(1000); |
255 | Led_Timer = SetDelay(1000); |
220 | } |
256 | } |
221 | else |
257 | else |
222 | { |
258 | { |
223 | blinkcount++; |
259 | blinkcount++; |
224 | Led_Timer = SetDelay(170); |
260 | Led_Timer = SetDelay(170); |
225 | } |
261 | } |
226 | } |
262 | } |
227 | } |
263 | } |
228 | else |
264 | else |
229 | { |
265 | { |
230 | LED_GRN_OFF; |
266 | LED_GRN_OFF; |
231 | } |
267 | } |
232 | 268 | ||
233 | // calibration state machine |
269 | // calibration state machine |
234 | switch(cal) |
270 | switch(cal) |
235 | { |
271 | { |
236 | case 1: // 1st step of calibration |
272 | case 1: // 1st step of calibration |
237 | // initialize ranges |
273 | // initialize ranges |
238 | // used to change the orientation of the MK3MAG in the horizontal plane |
274 | // used to change the orientation of the MK3MAG in the horizontal plane |
239 | Xmin = 10000; |
275 | Xmin = 10000; |
240 | Xmax = -10000; |
276 | Xmax = -10000; |
241 | Ymin = 10000; |
277 | Ymin = 10000; |
242 | Ymax = -10000; |
278 | Ymax = -10000; |
243 | Zmin = 10000; |
279 | Zmin = 10000; |
244 | Zmax = -10000; |
280 | Zmax = -10000; |
245 | Calibration.AccX.Offset = RawAccX; |
281 | Calibration.AccX.Offset = RawAccX; |
246 | Calibration.AccY.Offset = RawAccY; |
282 | Calibration.AccY.Offset = RawAccY; |
247 | Calibration.AccZ.Offset = RawAccZ; |
283 | Calibration.AccZ.Offset = RawAccZ; |
248 | break; |
284 | break; |
249 | 285 | ||
250 | case 2: // 2nd step of calibration |
286 | case 2: // 2nd step of calibration |
251 | // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane |
287 | // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane |
252 | if(UncalMagX < Xmin) Xmin = UncalMagX; |
288 | if(UncalMagX < Xmin) Xmin = UncalMagX; |
253 | if(UncalMagX > Xmax) Xmax = UncalMagX; |
289 | if(UncalMagX > Xmax) Xmax = UncalMagX; |
254 | if(UncalMagY < Ymin) Ymin = UncalMagY; |
290 | if(UncalMagY < Ymin) Ymin = UncalMagY; |
255 | if(UncalMagY > Ymax) Ymax = UncalMagY; |
291 | if(UncalMagY > Ymax) Ymax = UncalMagY; |
256 | break; |
292 | break; |
257 | 293 | ||
258 | case 3: // 3rd step of calibration |
294 | case 3: // 3rd step of calibration |
259 | // used to change the orientation of the MK3MAG vertical to the horizontal plane |
295 | // used to change the orientation of the MK3MAG vertical to the horizontal plane |
260 | break; |
296 | break; |
261 | 297 | ||
262 | case 4: |
298 | case 4: |
263 | // find Min and Max of the Z-Sensor |
299 | // find Min and Max of the Z-Sensor |
264 | if(UncalMagZ < Zmin) Zmin = UncalMagZ; |
300 | if(UncalMagZ < Zmin) Zmin = UncalMagZ; |
265 | if(UncalMagZ > Zmax) Zmax = UncalMagZ; |
301 | if(UncalMagZ > Zmax) Zmax = UncalMagZ; |
266 | break; |
302 | break; |
267 | 303 | ||
268 | case 5: |
304 | case 5: |
269 | // Save values |
305 | // Save values |
270 | if(cal != calold) // avoid continously writing of eeprom! |
306 | if(cal != calold) // avoid continously writing of eeprom! |
271 | { |
307 | { |
272 | Calibration.MagY.Range = Xmax - Xmin; |
308 | Calibration.MagX.Range = Xmax - Xmin; |
273 | Calibration.MagX.Offset = (Xmin + Xmax) / 2; |
309 | Calibration.MagX.Offset = (Xmin + Xmax) / 2; |
274 | Calibration.MagY.Range = Ymax - Ymin; |
310 | Calibration.MagY.Range = Ymax - Ymin; |
275 | Calibration.MagY.Offset = (Ymin + Ymax) / 2; |
311 | Calibration.MagY.Offset = (Ymin + Ymax) / 2; |
276 | Calibration.MagZ.Range = Zmax - Zmin; |
312 | Calibration.MagZ.Range = Zmax - Zmin; |
277 | Calibration.MagZ.Offset = (Zmin + Zmax) / 2; |
313 | Calibration.MagZ.Offset = (Zmin + Zmax) / 2; |
278 | if((Calibration.MagX.Range > 150) && (Calibration.MagY.Range > 150) && (Calibration.MagZ.Range > 150)) |
314 | if((Calibration.MagX.Range > 150) && (Calibration.MagY.Range > 150) && (Calibration.MagZ.Range > 150)) |
279 | { |
315 | { |
280 | // indicate write process by setting the led |
316 | // indicate write process by setting the led |
281 | LED_GRN_ON; |
317 | LED_GRN_ON; |
282 | eeprom_write_block(&Calibration, &eeCalibration, sizeof(Calibration)); |
318 | eeprom_write_block(&Calibration, &eeCalibration, sizeof(Calibration)); |
283 | Delay_ms(2000); |
319 | Led_Timer = SetDelay(2000); |
284 | // reset led state |
- | |
285 | LED_GRN_OFF; |
- | |
286 | // reset blinkcode |
320 | // reset blinkcode |
287 | blinkcount = 0; |
321 | blinkcount = 0; |
288 | Led_Timer = SetDelay(1000); |
- | |
289 | } |
322 | } |
290 | } |
323 | } |
291 | break; |
324 | break; |
292 | 325 | ||
293 | default: |
326 | default: |
294 | break; |
327 | break; |
295 | } |
328 | } |
296 | calold = cal; |
329 | calold = cal; |
297 | } |
330 | } |
298 | 331 | ||
299 | 332 | ||
300 | void SetDebugValues(void) |
333 | void SetDebugValues(void) |
301 | { |
334 | { |
302 | DebugOut.Analog[0] = MagX; |
335 | DebugOut.Analog[0] = MagX; |
303 | DebugOut.Analog[1] = MagY; |
336 | DebugOut.Analog[1] = MagY; |
304 | DebugOut.Analog[2] = MagZ; |
337 | DebugOut.Analog[2] = MagZ; |
305 | DebugOut.Analog[3] = UncalMagX; |
338 | DebugOut.Analog[3] = UncalMagX; |
306 | DebugOut.Analog[4] = UncalMagY; |
339 | DebugOut.Analog[4] = UncalMagY; |
307 | DebugOut.Analog[5] = UncalMagZ; |
340 | DebugOut.Analog[5] = UncalMagZ; |
308 | switch(AttitudeSource) |
341 | switch(AttitudeSource) |
309 | { |
342 | { |
310 | case ATTITUDE_SOURCE_ACC: |
343 | case ATTITUDE_SOURCE_ACC: |
311 | - | ||
- | 344 | DebugOut.Analog[6] = AccAttitudeNick; |
|
- | 345 | DebugOut.Analog[7] = AccAttitudeRoll; |
|
312 | break; |
346 | break; |
313 | 347 | ||
314 | case ATTITUDE_SOURCE_UART: |
348 | case ATTITUDE_SOURCE_UART: |
315 | DebugOut.Analog[6] = ExternData.Attitude[NICK]; |
349 | DebugOut.Analog[6] = ExternData.Attitude[NICK]; |
316 | DebugOut.Analog[7] = ExternData.Attitude[ROLL]; |
350 | DebugOut.Analog[7] = ExternData.Attitude[ROLL]; |
317 | break; |
351 | break; |
318 | 352 | ||
319 | 353 | ||
320 | case ATTITUDE_SOURCE_I2C: |
354 | case ATTITUDE_SOURCE_I2C: |
321 | DebugOut.Analog[6] = I2C_WriteAttitude.Nick; |
355 | DebugOut.Analog[6] = I2C_WriteAttitude.Nick; |
322 | DebugOut.Analog[7] = I2C_WriteAttitude.Roll; |
356 | DebugOut.Analog[7] = I2C_WriteAttitude.Roll; |
323 | break; |
357 | break; |
324 | } |
358 | } |
325 | DebugOut.Analog[8] = Calibration.MagX.Offset; |
359 | DebugOut.Analog[8] = Calibration.MagX.Offset; |
326 | DebugOut.Analog[9] = Calibration.MagX.Range; |
360 | DebugOut.Analog[9] = Calibration.MagX.Range; |
327 | DebugOut.Analog[10] = Calibration.MagY.Offset; |
361 | DebugOut.Analog[10] = Calibration.MagY.Offset; |
328 | DebugOut.Analog[11] = Calibration.MagY.Range; |
362 | DebugOut.Analog[11] = Calibration.MagY.Range; |
329 | DebugOut.Analog[12] = Calibration.MagZ.Offset; |
363 | DebugOut.Analog[12] = Calibration.MagZ.Offset; |
330 | DebugOut.Analog[13] = Calibration.MagZ.Range; |
364 | DebugOut.Analog[13] = Calibration.MagZ.Range; |
331 | DebugOut.Analog[14] = ExternData.CalState; |
365 | DebugOut.Analog[14] = ExternData.CalState; |
332 | DebugOut.Analog[15] = Heading; |
366 | DebugOut.Analog[15] = Heading; |
333 | DebugOut.Analog[16] = ExternData.UserParam[0]; |
367 | DebugOut.Analog[16] = ExternData.UserParam[0]; |
334 | DebugOut.Analog[17] = ExternData.UserParam[1]; |
368 | DebugOut.Analog[17] = ExternData.UserParam[1]; |
335 | DebugOut.Analog[18] = AccX; |
369 | DebugOut.Analog[18] = AccX; |
336 | DebugOut.Analog[19] = AccY; |
370 | DebugOut.Analog[19] = AccY; |
337 | DebugOut.Analog[20] = AccZ; |
371 | DebugOut.Analog[20] = AccZ; |
338 | DebugOut.Analog[21] = RawAccX; |
372 | DebugOut.Analog[21] = RawAccX; |
339 | DebugOut.Analog[22] = RawAccY; |
373 | DebugOut.Analog[22] = RawAccY; |
340 | DebugOut.Analog[23] = RawAccZ; |
374 | DebugOut.Analog[23] = RawAccZ; |
341 | DebugOut.Analog[24] = Calibration.AccX.Offset; |
375 | DebugOut.Analog[24] = Calibration.AccX.Offset; |
342 | DebugOut.Analog[25] = Calibration.AccY.Offset; |
376 | DebugOut.Analog[25] = Calibration.AccY.Offset; |
343 | DebugOut.Analog[26] = Calibration.AccZ.Offset; |
377 | DebugOut.Analog[26] = Calibration.AccZ.Offset; |
- | 378 | DebugOut.Analog[29] = AttitudeSource; |
|
344 | } |
379 | } |
345 | 380 | ||
346 | void AccMeasurement(void) |
381 | void AccMeasurement(void) |
347 | { |
382 | { |
348 | if(AccPresent) |
383 | if(AccPresent) |
349 | { |
384 | { |
350 | RawAccX = (RawAccX + (int16_t)ADC_GetValue(ADC2))/2; |
385 | RawAccX = (RawAccX + (int16_t)ADC_GetValue(ACC_X))/2; |
351 | RawAccY = (RawAccY + (int16_t)ADC_GetValue(ADC3))/2; |
386 | RawAccY = (RawAccY + (int16_t)ADC_GetValue(ACC_Y))/2; |
352 | RawAccZ = (RawAccZ + (int16_t)ADC_GetValue(ADC6))/2; |
387 | RawAccZ = (RawAccZ + (int16_t)ADC_GetValue(ACC_Z))/2; |
353 | } |
388 | } |
354 | else |
389 | else |
355 | { |
390 | { |
356 | RawAccX = 0; |
391 | RawAccX = 0; |
357 | RawAccY = 0; |
392 | RawAccY = 0; |
358 | RawAccZ = 0; |
393 | RawAccZ = 0; |
359 | } |
394 | } |
360 | AccX = ((RawAccX - Calibration.AccX.Offset) + AccX * 7) / 8; |
- | |
361 | AccY = ((RawAccY - Calibration.AccY.Offset) + AccY * 7) / 8; |
- | |
362 | AccZ = ((Calibration.AccZ.Offset - RawAccZ) + AccZ * 7) / 8; |
- | |
363 | } |
395 | } |
364 | - | ||
365 | - | ||
366 | 396 | ||
367 | int main (void) |
397 | int main (void) |
368 | { |
398 | { |
369 | // reset input pullup |
399 | // reset input pullup |
370 | DDRC &=~((1<<DDC6)); |
400 | DDRC &=~((1<<DDC6)); |
371 | PORTC |= (1<<PORTC6); |
401 | PORTC |= (1<<PORTC6); |
372 | 402 | ||
373 | LED_Init(); |
403 | LED_Init(); |
374 | TIMER0_Init(); |
404 | TIMER0_Init(); |
375 | USART0_Init(); |
405 | USART0_Init(); |
376 | ADC_Init(); |
406 | ADC_Init(); |
377 | I2C_Init(); |
407 | I2C_Init(); |
378 | 408 | ||
379 | sei(); // enable globale interrupts |
409 | sei(); // enable globale interrupts |
- | 410 | ||
- | 411 | if(AccPresent) |
|
- | 412 | { |
|
- | 413 | USART0_Print("ACC present\n"); |
|
- | 414 | } |
|
380 | 415 | ||
381 | LED_GRN_ON; |
416 | LED_GRN_ON; |
382 | 417 | ||
383 | Debug_Timer = SetDelay(200); |
418 | Debug_Timer = SetDelay(200); |
384 | Led_Timer = SetDelay(200); |
419 | Led_Timer = SetDelay(200); |
385 | 420 | ||
386 | // read calibration info from eeprom |
421 | // read calibration info from eeprom |
387 | eeprom_read_block(&Calibration, &eeCalibration, sizeof(Calibration)); |
422 | eeprom_read_block(&Calibration, &eeCalibration, sizeof(Calibration)); |
388 | - | ||
389 | ExternData.Orientation = 0; |
423 | |
390 | ExternData.CalState = 0; |
424 | ExternData.CalState = 0; |
391 | I2C_WriteCal.CalByte = 0; |
425 | I2C_WriteCal.CalByte = 0; |
392 | 426 | ||
393 | 427 | ||
394 | // main loop |
428 | // main loop |
395 | while (1) |
429 | while (1) |
396 | { |
430 | { |
397 | FLIP_LOW; |
431 | FLIP_LOW; |
398 | Delay_ms(2); |
432 | Delay_ms(2); |
399 | RawMagnet1a = ADC_GetValue(ADC0); |
433 | RawMagnet1a = ADC_GetValue(MAG_X); |
400 | RawMagnet2a = -ADC_GetValue(ADC1); |
434 | RawMagnet2a = -ADC_GetValue(MAG_Y); |
401 | RawMagnet3a = ADC_GetValue(ADC7); |
435 | RawMagnet3a = ADC_GetValue(MAG_Z); |
402 | AccMeasurement(); |
436 | AccMeasurement(); |
403 | Delay_ms(1); |
437 | Delay_ms(1); |
404 | 438 | ||
405 | FLIP_HIGH; |
439 | FLIP_HIGH; |
406 | Delay_ms(2); |
440 | Delay_ms(2); |
407 | RawMagnet1b = ADC_GetValue(ADC0); |
441 | RawMagnet1b = ADC_GetValue(MAG_X); |
408 | RawMagnet2b = -ADC_GetValue(ADC1); |
442 | RawMagnet2b = -ADC_GetValue(MAG_Y); |
409 | RawMagnet3b = ADC_GetValue(ADC7); |
443 | RawMagnet3b = ADC_GetValue(MAG_Z); |
410 | AccMeasurement(); |
444 | AccMeasurement(); |
411 | Delay_ms(1); |
445 | Delay_ms(1); |
412 | 446 | ||
413 | CalcFields(); |
447 | CalcFields(); |
414 | 448 | ||
415 | if(ExternData.CalState || I2C_WriteCal.CalByte) Calibrate(); |
449 | if(ExternData.CalState || I2C_WriteCal.CalByte) Calibrate(); |
416 | else CalcHeading(); |
450 | else CalcHeading(); |
417 | 451 | ||
418 | // check data from USART |
452 | // check data from USART |
419 | USART0_ProcessRxData(); |
453 | USART0_ProcessRxData(); |
- | 454 | ||
- | 455 | if(NC_Connected) NC_Connected--; |
|
- | 456 | if(FC_Connected) FC_Connected--; |
|
- | 457 | // fall back to attitude estimation from acc sensor if NC or FC does'nt send attittude data |
|
- | 458 | if(!NC_Connected && ! NC_Connected) |
|
- | 459 | { |
|
- | 460 | AttitudeSource = ATTITUDE_SOURCE_ACC; |
|
420 | USART0_TransmitTxData(); |
461 | Orientation = ORIENTATION_FC; |
- | 462 | } |
|
421 | 463 | ||
422 | if(PC_Connected) |
464 | if(PC_Connected) |
423 | { |
465 | { |
424 | USART0_EnableTXD(); |
466 | USART0_EnableTXD(); |
425 | USART0_TransmitTxData(); |
467 | USART0_TransmitTxData(); |
426 | PC_Connected--; |
468 | PC_Connected--; |
427 | } |
469 | } |
428 | else |
470 | else |
429 | { |
471 | { |
430 | USART0_DisableTXD(); |
472 | USART0_DisableTXD(); |
431 | } |
473 | } |
432 | } // while(1) |
474 | } // while(1) |
433 | } |
475 | } |
434 | 476 | ||
435 | 477 |