Rev 32 | Rev 34 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 32 | Rev 33 | ||
---|---|---|---|
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 | Orientation_t Orientation = ORIENTATION_FC; |
72 | 72 | ||
73 | uint16_t Led_Timer = 0; |
73 | uint16_t Led_Timer = 0; |
74 | 74 | ||
75 | typedef struct |
75 | typedef struct |
76 | { |
76 | { |
77 | int16_t Range; |
77 | int16_t Range; |
78 | int16_t Offset; |
78 | int16_t Offset; |
79 | } Scaling_t; |
79 | } Scaling_t; |
80 | 80 | ||
81 | typedef struct |
81 | typedef struct |
82 | { |
82 | { |
83 | Scaling_t MagX; |
83 | Scaling_t MagX; |
84 | Scaling_t MagY; |
84 | Scaling_t MagY; |
85 | Scaling_t MagZ; |
85 | Scaling_t MagZ; |
86 | Scaling_t AccX; |
86 | Scaling_t AccX; |
87 | Scaling_t AccY; |
87 | Scaling_t AccY; |
88 | Scaling_t AccZ; |
88 | Scaling_t AccZ; |
89 | } Calibration_t; |
89 | } Calibration_t; |
90 | 90 | ||
91 | Calibration_t eeCalibration EEMEM; // calibration data in EEProm |
91 | Calibration_t eeCalibration EEMEM; // calibration data in EEProm |
92 | Calibration_t Calibration; // calibration data in RAM |
92 | Calibration_t Calibration; // calibration data in RAM |
93 | 93 | ||
94 | // magnet sensor variable |
94 | // magnet sensor variable |
95 | int16_t RawMagnet1a, RawMagnet1b; // raw magnet sensor data |
95 | int16_t RawMagnet1a, RawMagnet1b; // raw magnet sensor data |
96 | int16_t RawMagnet2a, RawMagnet2b; |
96 | int16_t RawMagnet2a, RawMagnet2b; |
97 | int16_t RawMagnet3a, RawMagnet3b; |
97 | int16_t RawMagnet3a, RawMagnet3b; |
98 | int16_t UncalMagX, UncalMagY, UncalMagZ; // sensor signal difference without Scaling |
98 | int16_t UncalMagX, UncalMagY, UncalMagZ; // sensor signal difference without Scaling |
99 | int16_t MagX = 0, MagY = 0, MagZ = 0; // rescaled magnetic field readings |
99 | int16_t MagX = 0, MagY = 0, MagZ = 0; // rescaled magnetic field readings |
100 | 100 | ||
101 | // acceleration sensor variables |
101 | // acceleration sensor variables |
102 | int16_t RawAccX = 0, RawAccY = 0, RawAccZ = 0; // raw acceleration readings |
102 | int16_t RawAccX = 0, RawAccY = 0, RawAccZ = 0; // raw acceleration readings |
103 | int16_t AccX = 0, AccY = 0, AccZ = 0; // rescaled acceleration readings |
103 | int16_t AccX = 0, AccY = 0, AccZ = 0; // rescaled acceleration readings |
104 | int16_t AccAttitudeNick = 0, AccAttitudeRoll = 0; // nick and roll angle from acc |
104 | int16_t AccAttitudeNick = 0, AccAttitudeRoll = 0; // nick and roll angle from acc |
105 | 105 | ||
106 | int16_t Heading = -1; // the current compass heading in deg |
106 | int16_t Heading = -1; // the current compass heading in deg |
107 | 107 | ||
108 | 108 | ||
109 | void CalcFields(void) |
109 | void CalcFields(void) |
110 | { |
110 | { |
111 | UncalMagX = (RawMagnet1a - RawMagnet1b); |
111 | UncalMagX = (RawMagnet1a - RawMagnet1b); |
112 | UncalMagY = (RawMagnet3a - RawMagnet3b); |
112 | UncalMagY = (RawMagnet3a - RawMagnet3b); |
113 | UncalMagZ = (RawMagnet2a - RawMagnet2b); |
113 | UncalMagZ = (RawMagnet2a - RawMagnet2b); |
114 | 114 | ||
115 | 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); |
116 | else MagX = 0; |
116 | else MagX = 0; |
117 | 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); |
118 | else MagY = 0; |
118 | else MagY = 0; |
119 | 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); |
120 | else MagZ = 0; |
120 | else MagZ = 0; |
121 | 121 | ||
122 | if(AccPresent) |
122 | if(AccPresent) |
123 | { |
123 | { |
124 | AccX = (RawAccX - Calibration.AccX.Offset); |
124 | AccX = (RawAccX - Calibration.AccX.Offset); |
125 | AccY = (RawAccY - Calibration.AccY.Offset); |
125 | AccY = (RawAccY - Calibration.AccY.Offset); |
126 | AccZ = (Calibration.AccZ.Offset - RawAccZ); |
126 | AccZ = (Calibration.AccZ.Offset - RawAccZ); |
127 | 127 | ||
128 | #if (BOARD == 10) // the hardware 1.0 has the LIS3L02AL |
128 | #if (BOARD == 10) // the hardware 1.0 has the LIS3L02AL |
129 | // acc mode assumes orientation like FC |
129 | // acc mode assumes orientation like FC |
130 | if(AccX > 136) AccAttitudeNick = -800; |
130 | if(AccX > 136) AccAttitudeNick = -800; |
131 | else |
131 | else |
132 | if(AccX < -136) AccAttitudeNick = 800; |
132 | if(AccX < -136) AccAttitudeNick = 800; |
133 | else AccAttitudeNick = (int16_t)(-1800.0 * asin((double) AccX / 138.0) / M_PI); |
133 | else AccAttitudeNick = (int16_t)(-1800.0 * asin((double) AccX / 138.0) / M_PI); |
134 | 134 | ||
135 | 135 | ||
136 | if(AccY > 136) AccAttitudeRoll = 800; |
136 | if(AccY > 136) AccAttitudeRoll = 800; |
137 | else |
137 | else |
138 | if(AccY < -136) AccAttitudeRoll = -800; |
138 | if(AccY < -136) AccAttitudeRoll = -800; |
139 | else AccAttitudeRoll = (int16_t)( 1800.0 * asin((double) AccY / 138.0) / M_PI); |
139 | else AccAttitudeRoll = (int16_t)( 1800.0 * asin((double) AccY / 138.0) / M_PI); |
140 | 140 | ||
141 | #else // the hardware 1.1 has the LIS344ALH with a different axis definition (X -> -Y, Y -> X, Z -> Z) |
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 |
142 | // acc mode assumes orientation like FC |
143 | if(AccY > 136) AccAttitudeNick = 800; |
143 | if(AccY > 136) AccAttitudeNick = 800; |
144 | else |
144 | else |
145 | if(AccY < -136) AccAttitudeNick = -800; |
145 | if(AccY < -136) AccAttitudeNick = -800; |
146 | else AccAttitudeNick = (int16_t)( 1800.0 * asin((double) AccY / 138.0) / M_PI); |
146 | else AccAttitudeNick = (int16_t)( 1800.0 * asin((double) AccY / 138.0) / M_PI); |
147 | 147 | ||
148 | 148 | ||
149 | if(AccX > 136) AccAttitudeRoll = 800; |
149 | if(AccX > 136) AccAttitudeRoll = 800; |
150 | else |
150 | else |
151 | if(AccX < -136) AccAttitudeRoll = -800; |
151 | if(AccX < -136) AccAttitudeRoll = -800; |
152 | else AccAttitudeRoll = (int16_t)( 1800.0 * asin((double) AccX / 138.0) / M_PI); |
152 | else AccAttitudeRoll = (int16_t)( 1800.0 * asin((double) AccX / 138.0) / M_PI); |
153 | #endif |
153 | #endif |
154 | } |
154 | } |
155 | } |
155 | } |
156 | 156 | ||
157 | 157 | ||
158 | void CalcHeading(void) |
158 | void CalcHeading(void) |
159 | { |
159 | { |
160 | double nick_rad, roll_rad, Hx, Hy, Cx = 0.0, Cy = 0.0, Cz = 0.0; |
160 | double nick_rad, roll_rad, Hx, Hy, Cx = 0.0, Cy = 0.0, Cz = 0.0; |
161 | int16_t nick, roll; |
161 | int16_t nick, roll; |
162 | int16_t heading = -1; |
162 | int16_t heading = -1; |
163 | 163 | ||
164 | // blink code for normal operation |
164 | // blink code for normal operation |
165 | if(CheckDelay(Led_Timer)) |
165 | if(CheckDelay(Led_Timer)) |
166 | { |
166 | { |
167 | LED_GRN_TOGGLE; |
167 | LED_GRN_TOGGLE; |
168 | Led_Timer = SetDelay(500); |
168 | Led_Timer = SetDelay(500); |
169 | } |
169 | } |
170 | //MagX = 150; |
- | |
171 | //MagZ = 1000; |
- | |
172 | 170 | ||
173 | switch(Orientation) |
171 | switch(Orientation) |
174 | { |
172 | { |
175 | case ORIENTATION_NC: |
173 | case ORIENTATION_NC: |
176 | Cx = MagX; |
174 | Cx = MagX; |
177 | Cy = MagY; |
175 | Cy = MagY; |
178 | Cz = MagZ; |
176 | Cz = MagZ; |
179 | break; |
177 | break; |
180 | 178 | ||
181 | case ORIENTATION_FC: |
179 | case ORIENTATION_FC: |
182 | // rotation of 90 deg compared to NC setup |
180 | // rotation of 90 deg compared to NC setup |
183 | Cx = MagY; |
181 | Cx = MagY; |
184 | Cy = -MagX; |
182 | Cy = -MagX; |
185 | Cz = MagZ; |
183 | Cz = MagZ; |
186 | break; |
184 | break; |
187 | } |
185 | } |
188 | 186 | ||
189 | // calculate nick and roll angle in rad |
187 | // calculate nick and roll angle in rad |
190 | switch(AttitudeSource) |
188 | switch(AttitudeSource) |
191 | { |
189 | { |
192 | case ATTITUDE_SOURCE_I2C: |
190 | case ATTITUDE_SOURCE_I2C: |
193 | cli(); // stop interrupts |
191 | cli(); // stop interrupts |
194 | nick = I2C_WriteAttitude.Nick; |
192 | nick = I2C_WriteAttitude.Nick; |
195 | roll = I2C_WriteAttitude.Roll; |
193 | roll = I2C_WriteAttitude.Roll; |
196 | sei(); // start interrupts |
194 | sei(); // start interrupts |
197 | break; |
195 | break; |
198 | case ATTITUDE_SOURCE_UART: |
196 | case ATTITUDE_SOURCE_UART: |
199 | cli(); // stop interrupts |
197 | cli(); // stop interrupts |
200 | nick = ExternData.Attitude[NICK]; |
198 | nick = ExternData.Attitude[NICK]; |
201 | roll = ExternData.Attitude[ROLL]; |
199 | roll = ExternData.Attitude[ROLL]; |
202 | sei(); // start interrupts |
200 | sei(); // start interrupts |
203 | break; |
201 | break; |
204 | case ATTITUDE_SOURCE_ACC: |
202 | case ATTITUDE_SOURCE_ACC: |
205 | nick = AccAttitudeNick; |
203 | nick = AccAttitudeNick; |
206 | roll = AccAttitudeRoll; |
204 | roll = AccAttitudeRoll; |
207 | break; |
205 | break; |
208 | default: |
206 | default: |
209 | nick_rad = 0; |
207 | nick_rad = 0; |
210 | roll_rad = 0; |
208 | roll_rad = 0; |
211 | break; |
209 | break; |
212 | } |
210 | } |
213 | 211 | ||
214 | nick_rad = ((double)nick) * M_PI / (double)(1800.0); |
212 | nick_rad = ((double)nick) * M_PI / (double)(1800.0); |
215 | roll_rad = ((double)roll) * M_PI / (double)(1800.0); |
213 | roll_rad = ((double)roll) * M_PI / (double)(1800.0); |
216 | - | ||
217 | // nick_rad = 0; |
- | |
218 | // roll_rad = 0; |
214 | |
219 | 215 | ||
220 | // calculate attitude correction |
216 | // calculate attitude correction |
221 | Hx = Cx * cos(nick_rad) - Cz * sin(nick_rad); |
217 | Hx = Cx * cos(nick_rad) - Cz * sin(nick_rad); |
222 | Hy = Cy * cos(roll_rad) + Cz * sin(roll_rad); |
218 | Hy = Cy * cos(roll_rad) + Cz * sin(roll_rad); |
223 | 219 | ||
224 | DebugOut.Analog[27] = (int16_t)Hx; |
220 | DebugOut.Analog[27] = (int16_t)Hx; |
225 | DebugOut.Analog[28] = (int16_t)Hy; |
221 | DebugOut.Analog[28] = (int16_t)Hy; |
226 | 222 | ||
227 | // calculate Heading |
223 | // calculate Heading |
228 | heading = (int16_t)((180.0 * atan2(Hy, Hx)) / M_PI); |
224 | heading = (int16_t)((180.0 * atan2(Hy, Hx)) / M_PI); |
229 | // atan2 returns angular range from -180 deg to 180 deg in counter clockwise notation |
225 | // atan2 returns angular range from -180 deg to 180 deg in counter clockwise notation |
230 | // but the compass course is defined in a range from 0 deg to 360 deg clockwise notation. |
226 | // but the compass course is defined in a range from 0 deg to 360 deg clockwise notation. |
231 | if (heading < 0) heading = -heading; |
227 | if (heading < 0) heading = -heading; |
232 | else heading = 360 - heading; |
228 | else heading = 360 - heading; |
- | 229 | ||
233 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
230 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
234 | /* |
231 | /* // Alternative formula |
235 | Hx = Cx * (double)cos(nick_rad) + |
232 | Hx = Cx * (double)cos(nick_rad) + |
236 | Cy * (double)sin(nick_rad) * (double)sin(roll_rad) - |
233 | Cy * (double)sin(nick_rad) * (double)sin(roll_rad) - |
237 | Cz * (double)sin(nick_rad) * (double)cos(roll_rad); |
234 | Cz * (double)sin(nick_rad) * (double)cos(roll_rad); |
238 | |
235 | |
239 | Hy = Cy * (double)cos(roll_rad) + |
236 | Hy = Cy * (double)cos(roll_rad) + |
240 | Cz * (double)sin(roll_rad); |
237 | Cz * (double)sin(roll_rad); |
241 | |
238 | |
242 | if(Hx == 0 && Hy < 0) heading = 90; |
239 | if(Hx == 0 && Hy < 0) heading = 90; |
243 | else if(Hx == 0 && Hy > 0) heading = 270; |
240 | else if(Hx == 0 && Hy > 0) heading = 270; |
244 | else if(Hx < 0) heading = 180 - (atan(Hy / Hx) * 180.0) / M_PI; |
241 | else if(Hx < 0) heading = 180 - (atan(Hy / Hx) * 180.0) / M_PI; |
245 | else if(Hx > 0 && Hy < 0) heading = - (atan(Hy / Hx) * 180.0) / M_PI; |
242 | else if(Hx > 0 && Hy < 0) heading = - (atan(Hy / Hx) * 180.0) / M_PI; |
246 | else if(Hx > 0 && Hy > 0) heading = 360 - (atan(Hy / Hx) * 180.0) / M_PI; |
243 | else if(Hx > 0 && Hy > 0) heading = 360 - (atan(Hy / Hx) * 180.0) / M_PI; |
247 | 244 | ||
248 | if(abs(heading) < 361) Heading = heading; |
245 | if(abs(heading) < 361) Heading = heading; |
249 | */ |
246 | */ |
250 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
247 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
251 | 248 | ||
252 | cli(); // stop interrupts |
249 | cli(); // stop interrupts |
253 | if(abs(heading) < 361) Heading = heading; |
250 | if(abs(heading) < 361) Heading = heading; |
254 | else (Heading = -1); |
251 | else (Heading = -1); |
255 | sei(); // start interrupts |
252 | sei(); // start interrupts |
256 | 253 | ||
257 | } |
254 | } |
258 | 255 | ||
259 | 256 | ||
260 | void Calibrate(void) |
257 | void Calibrate(void) |
261 | { |
258 | { |
262 | uint8_t cal; |
259 | uint8_t cal; |
263 | static uint8_t calold = 0; |
260 | static uint8_t calold = 0; |
264 | static int16_t Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0; |
261 | static int16_t Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0; |
265 | static uint8_t blinkcount = 0; |
262 | static uint8_t blinkcount = 0; |
266 | static uint8_t invert_blinking = 0; |
263 | static uint8_t invert_blinking = 0; |
267 | 264 | ||
268 | // check both sources of communication for calibration request |
265 | // check both sources of communication for calibration request |
269 | if(I2C_WriteCal.CalByte) cal = I2C_WriteCal.CalByte; |
266 | if(I2C_WriteCal.CalByte) cal = I2C_WriteCal.CalByte; |
270 | else cal = ExternData.CalState; |
267 | else cal = ExternData.CalState; |
271 | 268 | ||
272 | 269 | ||
273 | if(cal > 5) cal = 0; |
270 | if(cal > 5) cal = 0; |
274 | // blink code for current calibration state |
271 | // blink code for current calibration state |
275 | if(cal) |
272 | if(cal) |
276 | { |
273 | { |
277 | if(CheckDelay(Led_Timer) || (cal != calold)) |
274 | if(CheckDelay(Led_Timer) || (cal != calold)) |
278 | { |
275 | { |
279 | if(blinkcount & 0x01) if(invert_blinking) LED_GRN_ON; else LED_GRN_OFF; |
276 | if(blinkcount & 0x01) if(invert_blinking) LED_GRN_ON; else LED_GRN_OFF; |
280 | else if(invert_blinking) LED_GRN_OFF; else LED_GRN_ON; |
277 | else if(invert_blinking) LED_GRN_OFF; else LED_GRN_ON; |
281 | 278 | ||
282 | // end of blinkcount sequence |
279 | // end of blinkcount sequence |
283 | if((blinkcount + 1 ) >= (2 * cal)) |
280 | if((blinkcount + 1 ) >= (2 * cal)) |
284 | { |
281 | { |
285 | blinkcount = 0; |
282 | blinkcount = 0; |
286 | Led_Timer = SetDelay(1000); |
283 | Led_Timer = SetDelay(1000); |
287 | } |
284 | } |
288 | else |
285 | else |
289 | { |
286 | { |
290 | blinkcount++; |
287 | blinkcount++; |
291 | Led_Timer = SetDelay(170); |
288 | Led_Timer = SetDelay(170); |
292 | } |
289 | } |
293 | } |
290 | } |
294 | } |
291 | } |
295 | else |
292 | else |
296 | { |
293 | { |
297 | if(invert_blinking) LED_GRN_ON; else LED_GRN_OFF; |
294 | if(invert_blinking) LED_GRN_ON; else LED_GRN_OFF; |
298 | } |
295 | } |
299 | 296 | ||
300 | // calibration state machine |
297 | // calibration state machine |
301 | switch(cal) |
298 | switch(cal) |
302 | { |
299 | { |
303 | case 1: // 1st step of calibration |
300 | case 1: // 1st step of calibration |
304 | // initialize ranges |
301 | // initialize ranges |
305 | // used to change the orientation of the MK3MAG in the horizontal plane |
302 | // used to change the orientation of the MK3MAG in the horizontal plane |
306 | Xmin = 10000; |
303 | Xmin = 10000; |
307 | Xmax = -10000; |
304 | Xmax = -10000; |
308 | Ymin = 10000; |
305 | Ymin = 10000; |
309 | Ymax = -10000; |
306 | Ymax = -10000; |
310 | Zmin = 10000; |
307 | Zmin = 10000; |
311 | Zmax = -10000; |
308 | Zmax = -10000; |
312 | Calibration.AccX.Offset = RawAccX; |
309 | Calibration.AccX.Offset = RawAccX; |
313 | Calibration.AccY.Offset = RawAccY; |
310 | Calibration.AccY.Offset = RawAccY; |
314 | Calibration.AccZ.Offset = RawAccZ; |
311 | Calibration.AccZ.Offset = RawAccZ; |
315 | invert_blinking = 0; |
312 | invert_blinking = 0; |
316 | break; |
313 | break; |
317 | 314 | ||
318 | case 2: // 2nd step of calibration |
315 | case 2: // 2nd step of calibration |
319 | // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane |
316 | // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane |
320 | if(UncalMagX < Xmin) Xmin = UncalMagX; |
317 | if(UncalMagX < Xmin) Xmin = UncalMagX; |
321 | if(UncalMagX > Xmax) Xmax = UncalMagX; |
318 | if(UncalMagX > Xmax) Xmax = UncalMagX; |
322 | if(UncalMagY < Ymin) Ymin = UncalMagY; |
319 | if(UncalMagY < Ymin) Ymin = UncalMagY; |
323 | if(UncalMagY > Ymax) Ymax = UncalMagY; |
320 | if(UncalMagY > Ymax) Ymax = UncalMagY; |
324 | invert_blinking = 1; |
321 | invert_blinking = 1; |
325 | break; |
322 | break; |
326 | 323 | ||
327 | case 3: // 3rd step of calibration |
324 | case 3: // 3rd step of calibration |
328 | // used to change the orientation of the MK3MAG vertical to the horizontal plane |
325 | // used to change the orientation of the MK3MAG vertical to the horizontal plane |
329 | invert_blinking = 0; |
326 | invert_blinking = 0; |
330 | break; |
327 | break; |
331 | 328 | ||
332 | case 4: |
329 | case 4: |
333 | // find Min and Max of the Z-Sensor |
330 | // find Min and Max of the Z-Sensor |
334 | if(UncalMagZ < Zmin) Zmin = UncalMagZ; |
331 | if(UncalMagZ < Zmin) Zmin = UncalMagZ; |
335 | if(UncalMagZ > Zmax) Zmax = UncalMagZ; |
332 | if(UncalMagZ > Zmax) Zmax = UncalMagZ; |
336 | invert_blinking = 1; |
333 | invert_blinking = 1; |
337 | break; |
334 | break; |
338 | 335 | ||
339 | case 5: |
336 | case 5: |
340 | // Save values |
337 | // Save values |
341 | if(cal != calold) // avoid continously writing of eeprom! |
338 | if(cal != calold) // avoid continously writing of eeprom! |
342 | { |
339 | { |
343 | Calibration.MagX.Range = Xmax - Xmin; |
340 | Calibration.MagX.Range = Xmax - Xmin; |
344 | Calibration.MagX.Offset = (Xmin + Xmax) / 2; |
341 | Calibration.MagX.Offset = (Xmin + Xmax) / 2; |
345 | Calibration.MagY.Range = Ymax - Ymin; |
342 | Calibration.MagY.Range = Ymax - Ymin; |
346 | Calibration.MagY.Offset = (Ymin + Ymax) / 2; |
343 | Calibration.MagY.Offset = (Ymin + Ymax) / 2; |
347 | Calibration.MagZ.Range = Zmax - Zmin; |
344 | Calibration.MagZ.Range = Zmax - Zmin; |
348 | Calibration.MagZ.Offset = (Zmin + Zmax) / 2; |
345 | Calibration.MagZ.Offset = (Zmin + Zmax) / 2; |
349 | if((Calibration.MagX.Range > 150) && (Calibration.MagY.Range > 150) && (Calibration.MagZ.Range > 150)) |
346 | if((Calibration.MagX.Range > 150) && (Calibration.MagY.Range > 150) && (Calibration.MagZ.Range > 150)) |
350 | { |
347 | { |
351 | // indicate write process by setting the led |
348 | // indicate write process by setting the led |
352 | LED_GRN_ON; |
349 | LED_GRN_ON; |
353 | eeprom_write_block(&Calibration, &eeCalibration, sizeof(Calibration)); |
350 | eeprom_write_block(&Calibration, &eeCalibration, sizeof(Calibration)); |
354 | Led_Timer = SetDelay(2000); |
351 | Led_Timer = SetDelay(2000); |
355 | // reset blinkcode |
352 | // reset blinkcode |
356 | blinkcount = 0; |
353 | blinkcount = 0; |
357 | } |
354 | } |
358 | } |
355 | } |
359 | invert_blinking = 0; |
356 | invert_blinking = 0; |
360 | break; |
357 | break; |
361 | 358 | ||
362 | default: |
359 | default: |
363 | break; |
360 | break; |
364 | } |
361 | } |
365 | calold = cal; |
362 | calold = cal; |
366 | } |
363 | } |
367 | 364 | ||
368 | 365 | ||
369 | void SetDebugValues(void) |
366 | void SetDebugValues(void) |
370 | { |
367 | { |
371 | DebugOut.Analog[0] = MagX; |
368 | DebugOut.Analog[0] = MagX; |
372 | DebugOut.Analog[1] = MagY; |
369 | DebugOut.Analog[1] = MagY; |
373 | DebugOut.Analog[2] = MagZ; |
370 | DebugOut.Analog[2] = MagZ; |
374 | DebugOut.Analog[3] = UncalMagX; |
371 | DebugOut.Analog[3] = UncalMagX; |
375 | DebugOut.Analog[4] = UncalMagY; |
372 | DebugOut.Analog[4] = UncalMagY; |
376 | DebugOut.Analog[5] = UncalMagZ; |
373 | DebugOut.Analog[5] = UncalMagZ; |
377 | switch(AttitudeSource) |
374 | switch(AttitudeSource) |
378 | { |
375 | { |
379 | case ATTITUDE_SOURCE_ACC: |
376 | case ATTITUDE_SOURCE_ACC: |
380 | DebugOut.Analog[6] = AccAttitudeNick; |
377 | DebugOut.Analog[6] = AccAttitudeNick; |
381 | DebugOut.Analog[7] = AccAttitudeRoll; |
378 | DebugOut.Analog[7] = AccAttitudeRoll; |
382 | break; |
379 | break; |
383 | 380 | ||
384 | case ATTITUDE_SOURCE_UART: |
381 | case ATTITUDE_SOURCE_UART: |
385 | DebugOut.Analog[6] = ExternData.Attitude[NICK]; |
382 | DebugOut.Analog[6] = ExternData.Attitude[NICK]; |
386 | DebugOut.Analog[7] = ExternData.Attitude[ROLL]; |
383 | DebugOut.Analog[7] = ExternData.Attitude[ROLL]; |
387 | break; |
384 | break; |
388 | 385 | ||
389 | 386 | ||
390 | case ATTITUDE_SOURCE_I2C: |
387 | case ATTITUDE_SOURCE_I2C: |
391 | DebugOut.Analog[6] = I2C_WriteAttitude.Nick; |
388 | DebugOut.Analog[6] = I2C_WriteAttitude.Nick; |
392 | DebugOut.Analog[7] = I2C_WriteAttitude.Roll; |
389 | DebugOut.Analog[7] = I2C_WriteAttitude.Roll; |
393 | break; |
390 | break; |
394 | } |
391 | } |
395 | DebugOut.Analog[8] = Calibration.MagX.Offset; |
392 | DebugOut.Analog[8] = Calibration.MagX.Offset; |
396 | DebugOut.Analog[9] = Calibration.MagX.Range; |
393 | DebugOut.Analog[9] = Calibration.MagX.Range; |
397 | DebugOut.Analog[10] = Calibration.MagY.Offset; |
394 | DebugOut.Analog[10] = Calibration.MagY.Offset; |
398 | DebugOut.Analog[11] = Calibration.MagY.Range; |
395 | DebugOut.Analog[11] = Calibration.MagY.Range; |
399 | DebugOut.Analog[12] = Calibration.MagZ.Offset; |
396 | DebugOut.Analog[12] = Calibration.MagZ.Offset; |
400 | DebugOut.Analog[13] = Calibration.MagZ.Range; |
397 | DebugOut.Analog[13] = Calibration.MagZ.Range; |
401 | DebugOut.Analog[14] = ExternData.CalState; |
398 | DebugOut.Analog[14] = ExternData.CalState; |
402 | DebugOut.Analog[15] = Heading; |
399 | DebugOut.Analog[15] = Heading; |
403 | DebugOut.Analog[16] = ExternData.UserParam[0]; |
400 | DebugOut.Analog[16] = ExternData.UserParam[0]; |
404 | DebugOut.Analog[17] = ExternData.UserParam[1]; |
401 | DebugOut.Analog[17] = ExternData.UserParam[1]; |
405 | DebugOut.Analog[18] = AccX; |
402 | DebugOut.Analog[18] = AccX; |
406 | DebugOut.Analog[19] = AccY; |
403 | DebugOut.Analog[19] = AccY; |
407 | DebugOut.Analog[20] = AccZ; |
404 | DebugOut.Analog[20] = AccZ; |
408 | DebugOut.Analog[21] = RawAccX; |
405 | DebugOut.Analog[21] = RawAccX; |
409 | DebugOut.Analog[22] = RawAccY; |
406 | DebugOut.Analog[22] = RawAccY; |
410 | DebugOut.Analog[23] = RawAccZ; |
407 | DebugOut.Analog[23] = RawAccZ; |
411 | DebugOut.Analog[24] = Calibration.AccX.Offset; |
408 | DebugOut.Analog[24] = Calibration.AccX.Offset; |
412 | DebugOut.Analog[25] = Calibration.AccY.Offset; |
409 | DebugOut.Analog[25] = Calibration.AccY.Offset; |
413 | DebugOut.Analog[26] = Calibration.AccZ.Offset; |
410 | DebugOut.Analog[26] = Calibration.AccZ.Offset; |
414 | DebugOut.Analog[29] = AttitudeSource; |
411 | DebugOut.Analog[29] = AttitudeSource; |
415 | } |
412 | } |
416 | 413 | ||
417 | void AccMeasurement(void) |
414 | void AccMeasurement(void) |
418 | { |
415 | { |
419 | if(AccPresent) |
416 | if(AccPresent) |
420 | { |
417 | { |
421 | RawAccX = (RawAccX + (int16_t)ADC_GetValue(ACC_X))/2; |
418 | RawAccX = (RawAccX + (int16_t)ADC_GetValue(ACC_X))/2; |
422 | RawAccY = (RawAccY + (int16_t)ADC_GetValue(ACC_Y))/2; |
419 | RawAccY = (RawAccY + (int16_t)ADC_GetValue(ACC_Y))/2; |
423 | RawAccZ = (RawAccZ + (int16_t)ADC_GetValue(ACC_Z))/2; |
420 | RawAccZ = (RawAccZ + (int16_t)ADC_GetValue(ACC_Z))/2; |
424 | } |
421 | } |
425 | else |
422 | else |
426 | { |
423 | { |
427 | RawAccX = 0; |
424 | RawAccX = 0; |
428 | RawAccY = 0; |
425 | RawAccY = 0; |
429 | RawAccZ = 0; |
426 | RawAccZ = 0; |
430 | } |
427 | } |
431 | } |
428 | } |
432 | 429 | ||
433 | int main (void) |
430 | int main (void) |
434 | { |
431 | { |
435 | // reset input pullup |
432 | // reset input pullup |
436 | DDRC &=~((1<<DDC6)); |
433 | DDRC &=~((1<<DDC6)); |
437 | PORTC |= (1<<PORTC6); |
434 | PORTC |= (1<<PORTC6); |
438 | 435 | ||
439 | LED_Init(); |
436 | LED_Init(); |
440 | TIMER0_Init(); |
437 | TIMER0_Init(); |
441 | USART0_Init(); |
438 | USART0_Init(); |
442 | ADC_Init(); |
439 | ADC_Init(); |
443 | I2C_Init(); |
440 | I2C_Init(); |
444 | 441 | ||
445 | sei(); // enable globale interrupts |
442 | sei(); // enable globale interrupts |
446 | 443 | ||
447 | if(AccPresent) |
444 | if(AccPresent) |
448 | { |
445 | { |
449 | USART0_Print("ACC present\n"); |
446 | USART0_Print("ACC present\n"); |
450 | } |
447 | } |
451 | 448 | ||
452 | LED_GRN_ON; |
449 | LED_GRN_ON; |
453 | 450 | ||
454 | Debug_Timer = SetDelay(200); |
451 | Debug_Timer = SetDelay(200); |
455 | Led_Timer = SetDelay(200); |
452 | Led_Timer = SetDelay(200); |
456 | 453 | ||
457 | // read calibration info from eeprom |
454 | // read calibration info from eeprom |
458 | eeprom_read_block(&Calibration, &eeCalibration, sizeof(Calibration)); |
455 | eeprom_read_block(&Calibration, &eeCalibration, sizeof(Calibration)); |
459 | 456 | ||
460 | ExternData.CalState = 0; |
457 | ExternData.CalState = 0; |
461 | I2C_WriteCal.CalByte = 0; |
458 | I2C_WriteCal.CalByte = 0; |
462 | 459 | ||
463 | 460 | ||
464 | // main loop |
461 | // main loop |
465 | while (1) |
462 | while (1) |
466 | { |
463 | { |
467 | FLIP_LOW; |
464 | FLIP_LOW; |
468 | Delay_ms(2); |
465 | Delay_ms(2); |
469 | RawMagnet1a = ADC_GetValue(MAG_X); |
466 | RawMagnet1a = ADC_GetValue(MAG_X); |
470 | RawMagnet2a = -ADC_GetValue(MAG_Y); |
467 | RawMagnet2a = -ADC_GetValue(MAG_Y); |
471 | RawMagnet3a = ADC_GetValue(MAG_Z); |
468 | RawMagnet3a = ADC_GetValue(MAG_Z); |
472 | AccMeasurement(); |
469 | AccMeasurement(); |
473 | Delay_ms(1); |
470 | Delay_ms(1); |
474 | 471 | ||
475 | FLIP_HIGH; |
472 | FLIP_HIGH; |
476 | Delay_ms(2); |
473 | Delay_ms(2); |
477 | RawMagnet1b = ADC_GetValue(MAG_X); |
474 | RawMagnet1b = ADC_GetValue(MAG_X); |
478 | RawMagnet2b = -ADC_GetValue(MAG_Y); |
475 | RawMagnet2b = -ADC_GetValue(MAG_Y); |
479 | RawMagnet3b = ADC_GetValue(MAG_Z); |
476 | RawMagnet3b = ADC_GetValue(MAG_Z); |
480 | AccMeasurement(); |
477 | AccMeasurement(); |
481 | Delay_ms(1); |
478 | Delay_ms(1); |
482 | 479 | ||
483 | CalcFields(); |
480 | CalcFields(); |
484 | 481 | ||
485 | if(ExternData.CalState || I2C_WriteCal.CalByte) Calibrate(); |
482 | if(ExternData.CalState || I2C_WriteCal.CalByte) Calibrate(); |
486 | else CalcHeading(); |
483 | else CalcHeading(); |
487 | 484 | ||
488 | // check data from USART |
485 | // check data from USART |
489 | USART0_ProcessRxData(); |
486 | USART0_ProcessRxData(); |
490 | 487 | ||
491 | if(NC_Connected) NC_Connected--; |
488 | if(NC_Connected) NC_Connected--; |
492 | if(FC_Connected) FC_Connected--; |
489 | if(FC_Connected) FC_Connected--; |
493 | // fall back to attitude estimation from acc sensor if NC or FC does'nt send attittude data |
490 | // fall back to attitude estimation from acc sensor if NC or FC does'nt send attittude data |
494 | if(!FC_Connected && ! NC_Connected) |
491 | if(!FC_Connected && ! NC_Connected) |
495 | { |
492 | { |
496 | AttitudeSource = ATTITUDE_SOURCE_ACC; |
493 | AttitudeSource = ATTITUDE_SOURCE_ACC; |
497 | Orientation = ORIENTATION_FC; |
494 | Orientation = ORIENTATION_FC; |
498 | } |
495 | } |
499 | 496 | ||
500 | if(PC_Connected) |
497 | if(PC_Connected) |
501 | { |
498 | { |
502 | USART0_EnableTXD(); |
499 | USART0_EnableTXD(); |
503 | USART0_TransmitTxData(); |
500 | USART0_TransmitTxData(); |
504 | PC_Connected--; |
501 | PC_Connected--; |
505 | } |
502 | } |
506 | else |
503 | else |
507 | { |
504 | { |
508 | USART0_DisableTXD(); |
505 | USART0_DisableTXD(); |
509 | } |
506 | } |
510 | } // while(1) |
507 | } // while(1) |
511 | } |
508 | } |
512 | 509 | ||
513 | 510 |