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