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