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