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