Rev 30 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 30 | Rev 31 | ||
---|---|---|---|
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 heading = -1; |
162 | int16_t heading = -1; |
162 | 163 | ||
163 | // blink code for normal operation |
164 | // blink code for normal operation |
164 | if(CheckDelay(Led_Timer)) |
165 | if(CheckDelay(Led_Timer)) |
165 | { |
166 | { |
166 | LED_GRN_TOGGLE; |
167 | LED_GRN_TOGGLE; |
167 | Led_Timer = SetDelay(500); |
168 | Led_Timer = SetDelay(500); |
168 | } |
169 | } |
169 | 170 | ||
170 | switch(Orientation) |
171 | switch(Orientation) |
171 | { |
172 | { |
172 | case ORIENTATION_NC: |
173 | case ORIENTATION_NC: |
173 | Cx = MagX; |
174 | Cx = MagX; |
174 | Cy = MagY; |
175 | Cy = MagY; |
175 | Cz = MagZ; |
176 | Cz = MagZ; |
176 | break; |
177 | break; |
177 | 178 | ||
178 | case ORIENTATION_FC: |
179 | case ORIENTATION_FC: |
179 | // rotation of 90 deg compared to NC setup |
180 | // rotation of 90 deg compared to NC setup |
180 | Cx = MagY; |
181 | Cx = MagY; |
181 | Cy = -MagX; |
182 | Cy = -MagX; |
182 | Cz = MagZ; |
183 | Cz = MagZ; |
183 | break; |
184 | break; |
184 | } |
185 | } |
185 | 186 | ||
186 | // calculate nick and roll angle in rad |
187 | // calculate nick and roll angle in rad |
187 | switch(AttitudeSource) |
188 | switch(AttitudeSource) |
188 | { |
189 | { |
189 | case ATTITUDE_SOURCE_I2C: |
190 | case ATTITUDE_SOURCE_I2C: |
- | 191 | cli(); // stop interrupts |
|
190 | nick_rad = ((double)I2C_WriteAttitude.Nick) * M_PI / (double)(1800.0); |
192 | nick = I2C_WriteAttitude.Nick; |
191 | roll_rad = ((double)I2C_WriteAttitude.Roll) * M_PI / (double)(1800.0); |
193 | roll = I2C_WriteAttitude.Roll; |
- | 194 | sei(); // start interrupts |
|
192 | break; |
195 | break; |
193 | - | ||
194 | case ATTITUDE_SOURCE_UART: |
196 | case ATTITUDE_SOURCE_UART: |
- | 197 | cli(); // stop interrupts |
|
195 | nick_rad = ((double)ExternData.Attitude[NICK]) * M_PI / (double)(1800.0); |
198 | nick = ExternData.Attitude[NICK]; |
196 | roll_rad = ((double)ExternData.Attitude[ROLL]) * M_PI / (double)(1800.0); |
199 | roll = ExternData.Attitude[ROLL]; |
- | 200 | sei(); // start interrupts |
|
197 | break; |
201 | break; |
198 | - | ||
199 | case ATTITUDE_SOURCE_ACC: |
202 | case ATTITUDE_SOURCE_ACC: |
200 | nick_rad = ((double)AccAttitudeNick) * M_PI / (double)(1800.0); |
203 | nick = AccAttitudeNick; |
201 | roll_rad = ((double)AccAttitudeRoll) * M_PI / (double)(1800.0); |
204 | roll = AccAttitudeRoll; |
202 | break; |
205 | break; |
203 | - | ||
204 | default: |
206 | default: |
205 | nick_rad = 0; |
207 | nick_rad = 0; |
206 | roll_rad = 0; |
208 | roll_rad = 0; |
207 | break; |
209 | break; |
208 | } |
210 | } |
- | 211 | nick_rad = ((double)nick) * M_PI / (double)(1800.0); |
|
- | 212 | roll_rad = ((double)roll) * M_PI / (double)(1800.0); |
|
209 | 213 | ||
210 | // calculate attitude correction |
214 | // calculate attitude correction |
211 | Hx = Cx * cos(nick_rad) - Cz * sin(nick_rad); |
215 | Hx = Cx * cos(nick_rad) - Cz * sin(nick_rad); |
212 | Hy = Cy * cos(roll_rad) + Cz * sin(roll_rad); |
216 | Hy = Cy * cos(roll_rad) + Cz * sin(roll_rad); |
213 | 217 | ||
214 | DebugOut.Analog[27] = (int16_t)Hx; |
218 | DebugOut.Analog[27] = (int16_t)Hx; |
215 | DebugOut.Analog[28] = (int16_t)Hy; |
219 | DebugOut.Analog[28] = (int16_t)Hy; |
216 | - | ||
217 | 220 | ||
218 | // calculate Heading |
221 | // calculate Heading |
219 | heading = (int16_t)((180.0 * atan2(Hy, Hx)) / M_PI); |
222 | heading = (int16_t)((180.0 * atan2(Hy, Hx)) / M_PI); |
220 | // atan2 returns angular range from -180 deg to 180 deg in counter clockwise notation |
223 | // atan2 returns angular range from -180 deg to 180 deg in counter clockwise notation |
221 | // but the compass course is defined in a range from 0 deg to 360 deg clockwise notation. |
224 | // but the compass course is defined in a range from 0 deg to 360 deg clockwise notation. |
222 | if (heading < 0) heading = -heading; |
225 | if (heading < 0) heading = -heading; |
223 | else heading = 360 - heading; |
226 | else heading = 360 - heading; |
- | 227 | ||
224 | 228 | cli(); // stop interrupts |
|
225 | if(abs(heading) < 361) Heading = heading; |
229 | if(abs(heading) < 361) Heading = heading; |
- | 230 | else (Heading = -1); |
|
- | 231 | sei(); // start interrupts |
|
226 | else (Heading = -1); |
232 | |
227 | } |
233 | } |
228 | 234 | ||
229 | 235 | ||
230 | void Calibrate(void) |
236 | void Calibrate(void) |
231 | { |
237 | { |
232 | uint8_t cal; |
238 | uint8_t cal; |
233 | static uint8_t calold = 0; |
239 | static uint8_t calold = 0; |
234 | static int16_t Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0; |
240 | static int16_t Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0; |
235 | static uint8_t blinkcount = 0; |
241 | static uint8_t blinkcount = 0; |
- | 242 | static uint8_t invert_blinking = 0; |
|
236 | 243 | ||
237 | // check both sources of communication for calibration request |
244 | // check both sources of communication for calibration request |
238 | if(I2C_WriteCal.CalByte) cal = I2C_WriteCal.CalByte; |
245 | if(I2C_WriteCal.CalByte) cal = I2C_WriteCal.CalByte; |
239 | else cal = ExternData.CalState; |
246 | else cal = ExternData.CalState; |
240 | 247 | ||
241 | 248 | ||
242 | if(cal > 5) cal = 0; |
249 | if(cal > 5) cal = 0; |
243 | // blink code for current calibration state |
250 | // blink code for current calibration state |
244 | if(cal) |
251 | if(cal) |
245 | { |
252 | { |
246 | if(CheckDelay(Led_Timer) || (cal != calold)) |
253 | if(CheckDelay(Led_Timer) || (cal != calold)) |
247 | { |
254 | { |
248 | if(blinkcount & 0x01) LED_GRN_OFF; |
255 | if(blinkcount & 0x01) if(invert_blinking) LED_GRN_ON; else LED_GRN_OFF; |
249 | else LED_GRN_ON; |
256 | else if(invert_blinking) LED_GRN_OFF; else LED_GRN_ON; |
250 | 257 | ||
251 | // end of blinkcount sequence |
258 | // end of blinkcount sequence |
252 | if( (blinkcount + 1 ) >= (2 * cal) ) |
259 | if((blinkcount + 1 ) >= (2 * cal)) |
253 | { |
260 | { |
254 | blinkcount = 0; |
261 | blinkcount = 0; |
255 | Led_Timer = SetDelay(1000); |
262 | Led_Timer = SetDelay(1000); |
256 | } |
263 | } |
257 | else |
264 | else |
258 | { |
265 | { |
259 | blinkcount++; |
266 | blinkcount++; |
260 | Led_Timer = SetDelay(170); |
267 | Led_Timer = SetDelay(170); |
261 | } |
268 | } |
262 | } |
269 | } |
263 | } |
270 | } |
264 | else |
271 | else |
265 | { |
272 | { |
266 | LED_GRN_OFF; |
273 | if(invert_blinking) LED_GRN_ON; else LED_GRN_OFF; |
267 | } |
274 | } |
268 | 275 | ||
269 | // calibration state machine |
276 | // calibration state machine |
270 | switch(cal) |
277 | switch(cal) |
271 | { |
278 | { |
272 | case 1: // 1st step of calibration |
279 | case 1: // 1st step of calibration |
273 | // initialize ranges |
280 | // initialize ranges |
274 | // used to change the orientation of the MK3MAG in the horizontal plane |
281 | // used to change the orientation of the MK3MAG in the horizontal plane |
275 | Xmin = 10000; |
282 | Xmin = 10000; |
276 | Xmax = -10000; |
283 | Xmax = -10000; |
277 | Ymin = 10000; |
284 | Ymin = 10000; |
278 | Ymax = -10000; |
285 | Ymax = -10000; |
279 | Zmin = 10000; |
286 | Zmin = 10000; |
280 | Zmax = -10000; |
287 | Zmax = -10000; |
281 | Calibration.AccX.Offset = RawAccX; |
288 | Calibration.AccX.Offset = RawAccX; |
282 | Calibration.AccY.Offset = RawAccY; |
289 | Calibration.AccY.Offset = RawAccY; |
283 | Calibration.AccZ.Offset = RawAccZ; |
290 | Calibration.AccZ.Offset = RawAccZ; |
- | 291 | invert_blinking = 0; |
|
284 | break; |
292 | break; |
285 | 293 | ||
286 | case 2: // 2nd step of calibration |
294 | case 2: // 2nd step of calibration |
287 | // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane |
295 | // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane |
288 | if(UncalMagX < Xmin) Xmin = UncalMagX; |
296 | if(UncalMagX < Xmin) Xmin = UncalMagX; |
289 | if(UncalMagX > Xmax) Xmax = UncalMagX; |
297 | if(UncalMagX > Xmax) Xmax = UncalMagX; |
290 | if(UncalMagY < Ymin) Ymin = UncalMagY; |
298 | if(UncalMagY < Ymin) Ymin = UncalMagY; |
291 | if(UncalMagY > Ymax) Ymax = UncalMagY; |
299 | if(UncalMagY > Ymax) Ymax = UncalMagY; |
- | 300 | invert_blinking = 1; |
|
292 | break; |
301 | break; |
293 | 302 | ||
294 | case 3: // 3rd step of calibration |
303 | case 3: // 3rd step of calibration |
295 | // used to change the orientation of the MK3MAG vertical to the horizontal plane |
304 | // used to change the orientation of the MK3MAG vertical to the horizontal plane |
- | 305 | invert_blinking = 0; |
|
296 | break; |
306 | break; |
297 | 307 | ||
298 | case 4: |
308 | case 4: |
299 | // find Min and Max of the Z-Sensor |
309 | // find Min and Max of the Z-Sensor |
300 | if(UncalMagZ < Zmin) Zmin = UncalMagZ; |
310 | if(UncalMagZ < Zmin) Zmin = UncalMagZ; |
301 | if(UncalMagZ > Zmax) Zmax = UncalMagZ; |
311 | if(UncalMagZ > Zmax) Zmax = UncalMagZ; |
- | 312 | invert_blinking = 1; |
|
302 | break; |
313 | break; |
303 | 314 | ||
304 | case 5: |
315 | case 5: |
305 | // Save values |
316 | // Save values |
306 | if(cal != calold) // avoid continously writing of eeprom! |
317 | if(cal != calold) // avoid continously writing of eeprom! |
307 | { |
318 | { |
308 | Calibration.MagX.Range = Xmax - Xmin; |
319 | Calibration.MagX.Range = Xmax - Xmin; |
309 | Calibration.MagX.Offset = (Xmin + Xmax) / 2; |
320 | Calibration.MagX.Offset = (Xmin + Xmax) / 2; |
310 | Calibration.MagY.Range = Ymax - Ymin; |
321 | Calibration.MagY.Range = Ymax - Ymin; |
311 | Calibration.MagY.Offset = (Ymin + Ymax) / 2; |
322 | Calibration.MagY.Offset = (Ymin + Ymax) / 2; |
312 | Calibration.MagZ.Range = Zmax - Zmin; |
323 | Calibration.MagZ.Range = Zmax - Zmin; |
313 | Calibration.MagZ.Offset = (Zmin + Zmax) / 2; |
324 | Calibration.MagZ.Offset = (Zmin + Zmax) / 2; |
314 | if((Calibration.MagX.Range > 150) && (Calibration.MagY.Range > 150) && (Calibration.MagZ.Range > 150)) |
325 | if((Calibration.MagX.Range > 150) && (Calibration.MagY.Range > 150) && (Calibration.MagZ.Range > 150)) |
315 | { |
326 | { |
316 | // indicate write process by setting the led |
327 | // indicate write process by setting the led |
317 | LED_GRN_ON; |
328 | LED_GRN_ON; |
318 | eeprom_write_block(&Calibration, &eeCalibration, sizeof(Calibration)); |
329 | eeprom_write_block(&Calibration, &eeCalibration, sizeof(Calibration)); |
319 | Led_Timer = SetDelay(2000); |
330 | Led_Timer = SetDelay(2000); |
320 | // reset blinkcode |
331 | // reset blinkcode |
321 | blinkcount = 0; |
332 | blinkcount = 0; |
322 | } |
333 | } |
323 | } |
334 | } |
- | 335 | invert_blinking = 0; |
|
324 | break; |
336 | break; |
325 | 337 | ||
326 | default: |
338 | default: |
327 | break; |
339 | break; |
328 | } |
340 | } |
329 | calold = cal; |
341 | calold = cal; |
330 | } |
342 | } |
331 | 343 | ||
332 | 344 | ||
333 | void SetDebugValues(void) |
345 | void SetDebugValues(void) |
334 | { |
346 | { |
335 | DebugOut.Analog[0] = MagX; |
347 | DebugOut.Analog[0] = MagX; |
336 | DebugOut.Analog[1] = MagY; |
348 | DebugOut.Analog[1] = MagY; |
337 | DebugOut.Analog[2] = MagZ; |
349 | DebugOut.Analog[2] = MagZ; |
338 | DebugOut.Analog[3] = UncalMagX; |
350 | DebugOut.Analog[3] = UncalMagX; |
339 | DebugOut.Analog[4] = UncalMagY; |
351 | DebugOut.Analog[4] = UncalMagY; |
340 | DebugOut.Analog[5] = UncalMagZ; |
352 | DebugOut.Analog[5] = UncalMagZ; |
341 | switch(AttitudeSource) |
353 | switch(AttitudeSource) |
342 | { |
354 | { |
343 | case ATTITUDE_SOURCE_ACC: |
355 | case ATTITUDE_SOURCE_ACC: |
344 | DebugOut.Analog[6] = AccAttitudeNick; |
356 | DebugOut.Analog[6] = AccAttitudeNick; |
345 | DebugOut.Analog[7] = AccAttitudeRoll; |
357 | DebugOut.Analog[7] = AccAttitudeRoll; |
346 | break; |
358 | break; |
347 | 359 | ||
348 | case ATTITUDE_SOURCE_UART: |
360 | case ATTITUDE_SOURCE_UART: |
349 | DebugOut.Analog[6] = ExternData.Attitude[NICK]; |
361 | DebugOut.Analog[6] = ExternData.Attitude[NICK]; |
350 | DebugOut.Analog[7] = ExternData.Attitude[ROLL]; |
362 | DebugOut.Analog[7] = ExternData.Attitude[ROLL]; |
351 | break; |
363 | break; |
352 | 364 | ||
353 | 365 | ||
354 | case ATTITUDE_SOURCE_I2C: |
366 | case ATTITUDE_SOURCE_I2C: |
355 | DebugOut.Analog[6] = I2C_WriteAttitude.Nick; |
367 | DebugOut.Analog[6] = I2C_WriteAttitude.Nick; |
356 | DebugOut.Analog[7] = I2C_WriteAttitude.Roll; |
368 | DebugOut.Analog[7] = I2C_WriteAttitude.Roll; |
357 | break; |
369 | break; |
358 | } |
370 | } |
359 | DebugOut.Analog[8] = Calibration.MagX.Offset; |
371 | DebugOut.Analog[8] = Calibration.MagX.Offset; |
360 | DebugOut.Analog[9] = Calibration.MagX.Range; |
372 | DebugOut.Analog[9] = Calibration.MagX.Range; |
361 | DebugOut.Analog[10] = Calibration.MagY.Offset; |
373 | DebugOut.Analog[10] = Calibration.MagY.Offset; |
362 | DebugOut.Analog[11] = Calibration.MagY.Range; |
374 | DebugOut.Analog[11] = Calibration.MagY.Range; |
363 | DebugOut.Analog[12] = Calibration.MagZ.Offset; |
375 | DebugOut.Analog[12] = Calibration.MagZ.Offset; |
364 | DebugOut.Analog[13] = Calibration.MagZ.Range; |
376 | DebugOut.Analog[13] = Calibration.MagZ.Range; |
365 | DebugOut.Analog[14] = ExternData.CalState; |
377 | DebugOut.Analog[14] = ExternData.CalState; |
366 | DebugOut.Analog[15] = Heading; |
378 | DebugOut.Analog[15] = Heading; |
367 | DebugOut.Analog[16] = ExternData.UserParam[0]; |
379 | DebugOut.Analog[16] = ExternData.UserParam[0]; |
368 | DebugOut.Analog[17] = ExternData.UserParam[1]; |
380 | DebugOut.Analog[17] = ExternData.UserParam[1]; |
369 | DebugOut.Analog[18] = AccX; |
381 | DebugOut.Analog[18] = AccX; |
370 | DebugOut.Analog[19] = AccY; |
382 | DebugOut.Analog[19] = AccY; |
371 | DebugOut.Analog[20] = AccZ; |
383 | DebugOut.Analog[20] = AccZ; |
372 | DebugOut.Analog[21] = RawAccX; |
384 | DebugOut.Analog[21] = RawAccX; |
373 | DebugOut.Analog[22] = RawAccY; |
385 | DebugOut.Analog[22] = RawAccY; |
374 | DebugOut.Analog[23] = RawAccZ; |
386 | DebugOut.Analog[23] = RawAccZ; |
375 | DebugOut.Analog[24] = Calibration.AccX.Offset; |
387 | DebugOut.Analog[24] = Calibration.AccX.Offset; |
376 | DebugOut.Analog[25] = Calibration.AccY.Offset; |
388 | DebugOut.Analog[25] = Calibration.AccY.Offset; |
377 | DebugOut.Analog[26] = Calibration.AccZ.Offset; |
389 | DebugOut.Analog[26] = Calibration.AccZ.Offset; |
378 | DebugOut.Analog[29] = AttitudeSource; |
390 | DebugOut.Analog[29] = AttitudeSource; |
379 | } |
391 | } |
380 | 392 | ||
381 | void AccMeasurement(void) |
393 | void AccMeasurement(void) |
382 | { |
394 | { |
383 | if(AccPresent) |
395 | if(AccPresent) |
384 | { |
396 | { |
385 | RawAccX = (RawAccX + (int16_t)ADC_GetValue(ACC_X))/2; |
397 | RawAccX = (RawAccX + (int16_t)ADC_GetValue(ACC_X))/2; |
386 | RawAccY = (RawAccY + (int16_t)ADC_GetValue(ACC_Y))/2; |
398 | RawAccY = (RawAccY + (int16_t)ADC_GetValue(ACC_Y))/2; |
387 | RawAccZ = (RawAccZ + (int16_t)ADC_GetValue(ACC_Z))/2; |
399 | RawAccZ = (RawAccZ + (int16_t)ADC_GetValue(ACC_Z))/2; |
388 | } |
400 | } |
389 | else |
401 | else |
390 | { |
402 | { |
391 | RawAccX = 0; |
403 | RawAccX = 0; |
392 | RawAccY = 0; |
404 | RawAccY = 0; |
393 | RawAccZ = 0; |
405 | RawAccZ = 0; |
394 | } |
406 | } |
395 | } |
407 | } |
396 | 408 | ||
397 | int main (void) |
409 | int main (void) |
398 | { |
410 | { |
399 | // reset input pullup |
411 | // reset input pullup |
400 | DDRC &=~((1<<DDC6)); |
412 | DDRC &=~((1<<DDC6)); |
401 | PORTC |= (1<<PORTC6); |
413 | PORTC |= (1<<PORTC6); |
402 | 414 | ||
403 | LED_Init(); |
415 | LED_Init(); |
404 | TIMER0_Init(); |
416 | TIMER0_Init(); |
405 | USART0_Init(); |
417 | USART0_Init(); |
406 | ADC_Init(); |
418 | ADC_Init(); |
407 | I2C_Init(); |
419 | I2C_Init(); |
408 | 420 | ||
409 | sei(); // enable globale interrupts |
421 | sei(); // enable globale interrupts |
410 | 422 | ||
411 | if(AccPresent) |
423 | if(AccPresent) |
412 | { |
424 | { |
413 | USART0_Print("ACC present\n"); |
425 | USART0_Print("ACC present\n"); |
414 | } |
426 | } |
415 | 427 | ||
416 | LED_GRN_ON; |
428 | LED_GRN_ON; |
417 | 429 | ||
418 | Debug_Timer = SetDelay(200); |
430 | Debug_Timer = SetDelay(200); |
419 | Led_Timer = SetDelay(200); |
431 | Led_Timer = SetDelay(200); |
420 | 432 | ||
421 | // read calibration info from eeprom |
433 | // read calibration info from eeprom |
422 | eeprom_read_block(&Calibration, &eeCalibration, sizeof(Calibration)); |
434 | eeprom_read_block(&Calibration, &eeCalibration, sizeof(Calibration)); |
423 | 435 | ||
424 | ExternData.CalState = 0; |
436 | ExternData.CalState = 0; |
425 | I2C_WriteCal.CalByte = 0; |
437 | I2C_WriteCal.CalByte = 0; |
426 | 438 | ||
427 | 439 | ||
428 | // main loop |
440 | // main loop |
429 | while (1) |
441 | while (1) |
430 | { |
442 | { |
431 | FLIP_LOW; |
443 | FLIP_LOW; |
432 | Delay_ms(2); |
444 | Delay_ms(2); |
433 | RawMagnet1a = ADC_GetValue(MAG_X); |
445 | RawMagnet1a = ADC_GetValue(MAG_X); |
434 | RawMagnet2a = -ADC_GetValue(MAG_Y); |
446 | RawMagnet2a = -ADC_GetValue(MAG_Y); |
435 | RawMagnet3a = ADC_GetValue(MAG_Z); |
447 | RawMagnet3a = ADC_GetValue(MAG_Z); |
436 | AccMeasurement(); |
448 | AccMeasurement(); |
437 | Delay_ms(1); |
449 | Delay_ms(1); |
438 | 450 | ||
439 | FLIP_HIGH; |
451 | FLIP_HIGH; |
440 | Delay_ms(2); |
452 | Delay_ms(2); |
441 | RawMagnet1b = ADC_GetValue(MAG_X); |
453 | RawMagnet1b = ADC_GetValue(MAG_X); |
442 | RawMagnet2b = -ADC_GetValue(MAG_Y); |
454 | RawMagnet2b = -ADC_GetValue(MAG_Y); |
443 | RawMagnet3b = ADC_GetValue(MAG_Z); |
455 | RawMagnet3b = ADC_GetValue(MAG_Z); |
444 | AccMeasurement(); |
456 | AccMeasurement(); |
445 | Delay_ms(1); |
457 | Delay_ms(1); |
446 | 458 | ||
447 | CalcFields(); |
459 | CalcFields(); |
448 | 460 | ||
449 | if(ExternData.CalState || I2C_WriteCal.CalByte) Calibrate(); |
461 | if(ExternData.CalState || I2C_WriteCal.CalByte) Calibrate(); |
450 | else CalcHeading(); |
462 | else CalcHeading(); |
451 | 463 | ||
452 | // check data from USART |
464 | // check data from USART |
453 | USART0_ProcessRxData(); |
465 | USART0_ProcessRxData(); |
454 | 466 | ||
455 | if(NC_Connected) NC_Connected--; |
467 | if(NC_Connected) NC_Connected--; |
456 | if(FC_Connected) FC_Connected--; |
468 | if(FC_Connected) FC_Connected--; |
457 | // fall back to attitude estimation from acc sensor if NC or FC does'nt send attittude data |
469 | // fall back to attitude estimation from acc sensor if NC or FC does'nt send attittude data |
458 | if(!FC_Connected && ! NC_Connected) |
470 | if(!FC_Connected && ! NC_Connected) |
459 | { |
471 | { |
460 | AttitudeSource = ATTITUDE_SOURCE_ACC; |
472 | AttitudeSource = ATTITUDE_SOURCE_ACC; |
461 | Orientation = ORIENTATION_FC; |
473 | Orientation = ORIENTATION_FC; |
462 | } |
474 | } |
463 | 475 | ||
464 | if(PC_Connected) |
476 | if(PC_Connected) |
465 | { |
477 | { |
466 | USART0_EnableTXD(); |
478 | USART0_EnableTXD(); |
467 | USART0_TransmitTxData(); |
479 | USART0_TransmitTxData(); |
468 | PC_Connected--; |
480 | PC_Connected--; |
469 | } |
481 | } |
470 | else |
482 | else |
471 | { |
483 | { |
472 | USART0_DisableTXD(); |
484 | USART0_DisableTXD(); |
473 | } |
485 | } |
474 | } // while(1) |
486 | } // while(1) |
475 | } |
487 | } |
476 | 488 | ||
477 | 489 |