Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
5 | hbuss | 1 | /*####################################################################################### |
7 | hbuss | 2 | MK3Mag 3D-Magnet sensor |
5 | hbuss | 3 | #######################################################################################*/ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
7 | hbuss | 5 | // + Copyright (c) 05.2008 Holger Buss |
6 | // + Thanks to Ilja Fähnrich |
||
5 | hbuss | 7 | // + Nur für den privaten Gebrauch |
8 | // + www.MikroKopter.com |
||
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
10 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
||
11 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
||
12 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
||
13 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
||
14 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
||
15 | // + Verkauf von Luftbildaufnahmen, usw. |
||
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
17 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
||
18 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
||
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
20 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
||
21 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
||
22 | // + eindeutig als Ursprung verlinkt werden |
||
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
24 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
||
25 | // + Benutzung auf eigene Gefahr |
||
26 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
||
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
28 | // + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
||
29 | // + mit unserer Zustimmung zulässig |
||
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
31 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
||
32 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
33 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
||
34 | // + this list of conditions and the following disclaimer. |
||
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. |
||
37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permittet |
||
38 | // + for non-commercial use (directly or indirectly) |
||
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
||
40 | // + with our written permission |
||
7 | hbuss | 41 | // + * If sources or documentations are redistributet on other webpages, our webpage (http://www.MikroKopter.de) must be |
42 | // + clearly linked as origin |
||
43 | // + * porting to systems other than hardware from www.mikrokopter.de is not allowed |
||
5 | hbuss | 44 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
45 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
46 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
47 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
||
48 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
||
49 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
||
50 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||
51 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||
52 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
||
53 | // + POSSIBILITY OF SUCH DAMAGE. |
||
54 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
55 | |||
7 | hbuss | 56 | signed int OffsetN, OffsetR, OffsetZ; |
1 | ingob | 57 | |
7 | hbuss | 58 | signed int RawMagnet1a,RawMagnet1b; // raw AD-Data |
1 | ingob | 59 | signed int RawMagnet2a,RawMagnet2b; |
60 | signed int RawMagnet3a,RawMagnet3b; |
||
7 | hbuss | 61 | signed int Xmin = 0, Xmax = 0; Ymin = 0, Ymax = 0; Zmin = 0, Zmax = 0; |
1 | ingob | 62 | signed int UncalMagnetN,UncalMagnetR,UncalMagnetZ; // Messwert-Delta ohne Offset- und Verstärker korrektur |
7 | hbuss | 63 | signed int MagnetN,MagnetR,MagnetZ; |
1 | ingob | 64 | unsigned int PwmHeading = 0; |
7 | hbuss | 65 | unsigned int PC_Connected = 0; |
66 | unsigned int Heading; |
||
1 | ingob | 67 | #include "main.h" |
68 | |||
7 | hbuss | 69 | uint16_t eeXmin EEMEM = 0; |
70 | uint16_t eeXmax EEMEM = 0; |
||
71 | uint16_t eeYmin EEMEM = 0; |
||
72 | uint16_t eeYmax EEMEM = 0; |
||
73 | uint16_t eeZmin EEMEM = 0; |
||
74 | uint16_t eeZmax EEMEM = 0; |
||
1 | ingob | 75 | |
7 | hbuss | 76 | |
1 | ingob | 77 | //############################################################################ |
78 | // |
||
79 | void Wait(unsigned char dauer) |
||
80 | //############################################################################ |
||
81 | { |
||
82 | dauer = (unsigned char)TCNT0 + dauer; |
||
83 | while((TCNT0 - dauer) & 0x80); |
||
84 | } |
||
85 | |||
86 | void CalcFields(void) |
||
7 | hbuss | 87 | { |
88 | UncalMagnetN = (1 * UncalMagnetN + (RawMagnet1a - RawMagnet1b)) / 2; |
||
89 | UncalMagnetR = (1 * UncalMagnetR + (RawMagnet3a - RawMagnet3b)) / 2; |
||
90 | UncalMagnetZ = (1 * UncalMagnetZ + (RawMagnet2a - RawMagnet2b)) / 2; |
||
91 | |||
92 | OffsetN = (Xmin + Xmax) / 2; |
||
93 | OffsetR = (Ymin + Ymax) / 2; |
||
94 | OffsetZ = (Zmin + Zmax) / 2; |
||
95 | |||
96 | MagnetN = (1024L * (long)(UncalMagnetN - OffsetN)) / (Xmax - Xmin); |
||
97 | MagnetR = (1024L * (long)(UncalMagnetR - OffsetR)) / (Ymax - Ymin); |
||
98 | MagnetZ = (1024L * (long)(UncalMagnetZ - OffsetZ)) / (Zmax - Zmin); |
||
1 | ingob | 99 | } |
100 | |||
101 | void CalcHeading(void) |
||
102 | { |
||
103 | double nick_rad, roll_rad, Hx, Hy, Cx, Cy, Cz; |
||
7 | hbuss | 104 | int heading; |
1 | ingob | 105 | |
7 | hbuss | 106 | nick_rad = ((double)ExternData.Winkel[0]) * M_PI / (double)(1800); |
107 | roll_rad = ((double)ExternData.Winkel[1]) * M_PI / (double)(1800); |
||
1 | ingob | 108 | |
109 | Cx = MagnetN; |
||
110 | Cy = MagnetR; |
||
7 | hbuss | 111 | Cz = MagnetZ; |
112 | |||
113 | if(ExternData.Orientation == 1) |
||
114 | { |
||
115 | Cx = MagnetR; |
||
116 | Cy = -MagnetN; |
||
117 | Cz = MagnetZ; |
||
118 | } |
||
119 | |||
1 | ingob | 120 | Hx = Cx * (double)cos(nick_rad) + |
121 | Cy * (double)sin(nick_rad) * (double)sin(roll_rad) - |
||
122 | Cz * (double)sin(nick_rad) * (double)cos(roll_rad); |
||
123 | |||
124 | Hy = Cy * (double)cos(roll_rad) + |
||
125 | Cz * (double)sin(roll_rad); |
||
126 | |||
127 | |||
128 | if(Hx == 0 && Hy < 0) heading = 90; |
||
129 | else if(Hx == 0 && Hy > 0) heading = 270; |
||
7 | hbuss | 130 | else if(Hx < 0) heading = 180 - (atan(Hy / Hx) * 180.0) / M_PI; |
131 | else if(Hx > 0 && Hy < 0) heading = - (atan(Hy / Hx) * 180.0) / M_PI; |
||
132 | else if(Hx > 0 && Hy > 0) heading = 360 - (atan(Hy / Hx) * 180.0) / M_PI; |
||
1 | ingob | 133 | |
7 | hbuss | 134 | if(abs(heading) < 361) Heading = heading; |
135 | PwmHeading = Heading + 10; |
||
1 | ingob | 136 | } |
137 | |||
138 | |||
7 | hbuss | 139 | void Calibrate(void) |
140 | { |
||
141 | unsigned char cal; |
||
142 | if(I2C_WriteCal.CalByte) cal = I2C_WriteCal.CalByte; |
||
143 | else cal = ExternData.CalState; |
||
144 | switch(cal) |
||
145 | { |
||
146 | case 0: |
||
147 | LED_ON; |
||
148 | break; |
||
149 | case 1: |
||
150 | Xmin = 10000; |
||
151 | Xmax = -10000; |
||
152 | Ymin = 10000; |
||
153 | Ymax = -10000; |
||
154 | Zmin = 10000; |
||
155 | Zmax = -10000; |
||
156 | LED_OFF; |
||
157 | break; |
||
158 | case 2: |
||
159 | LED_ON; // find Min and Max of the X- and Y-Sensors |
||
160 | if(UncalMagnetN < Xmin) Xmin = UncalMagnetN; |
||
161 | if(UncalMagnetN > Xmax) Xmax = UncalMagnetN; |
||
162 | if(UncalMagnetR < Ymin) Ymin = UncalMagnetR; |
||
163 | if(UncalMagnetR > Ymax) Ymax = UncalMagnetR; |
||
164 | break; |
||
165 | case 3: |
||
166 | LED_OFF; |
||
167 | break; |
||
168 | case 4: |
||
169 | LED_ON; // find Min and Max of the Z-Sensor |
||
170 | if(UncalMagnetZ < Zmin) Zmin = UncalMagnetZ; |
||
171 | if(UncalMagnetZ > Zmax) Zmax = UncalMagnetZ; |
||
172 | break; |
||
173 | case 5: |
||
174 | LED_OFF; // Save values |
||
175 | if((Xmax - Xmin) > 150 && (Ymax - Ymin) > 150 && (Zmax - Zmin) > 150) |
||
176 | { |
||
177 | eeprom_write_word(&eeXmin, Xmin); |
||
178 | eeprom_write_word(&eeXmax, Xmax); |
||
179 | eeprom_write_word(&eeYmin, Ymin); |
||
180 | eeprom_write_word(&eeYmax, Ymax); |
||
181 | eeprom_write_word(&eeZmin, Zmin); |
||
182 | eeprom_write_word(&eeZmax, Zmax); |
||
183 | Delay_ms(2000); |
||
184 | } |
||
185 | LED_ON; |
||
186 | break; |
||
187 | } |
||
188 | } |
||
1 | ingob | 189 | |
7 | hbuss | 190 | |
191 | void SetDebugValues(void) |
||
192 | { |
||
193 | DebugOut.Analog[0] = MagnetN; |
||
194 | DebugOut.Analog[1] = MagnetR; |
||
195 | DebugOut.Analog[2] = MagnetZ; |
||
196 | DebugOut.Analog[3] = UncalMagnetN; |
||
197 | DebugOut.Analog[4] = UncalMagnetR; |
||
198 | DebugOut.Analog[5] = UncalMagnetZ; |
||
199 | DebugOut.Analog[6] = ExternData.Winkel[0]; |
||
200 | DebugOut.Analog[7] = ExternData.Winkel[1]; |
||
201 | DebugOut.Analog[8] = Xmin; |
||
202 | DebugOut.Analog[9] = Xmax; |
||
203 | DebugOut.Analog[10] = Ymin; |
||
204 | DebugOut.Analog[11] = Ymax; |
||
205 | DebugOut.Analog[12] = Zmin; |
||
206 | DebugOut.Analog[13] = Zmax; |
||
207 | DebugOut.Analog[14] = ExternData.CalState; |
||
208 | DebugOut.Analog[15] = Heading; |
||
209 | DebugOut.Analog[16] = ExternData.UserParameter[0]; |
||
210 | DebugOut.Analog[17] = ExternData.UserParameter[1]; |
||
211 | } |
||
212 | |||
213 | |||
1 | ingob | 214 | //############################################################################ |
215 | //Hauptprogramm |
||
216 | int main (void) |
||
217 | //############################################################################ |
||
218 | { |
||
219 | DDRC = 0x08; |
||
220 | PORTC = 0x08; |
||
221 | DDRD = 0xf4; |
||
222 | PORTD = 0xA0; |
||
223 | DDRB = 0x04; |
||
224 | PORTB = 0x35; |
||
225 | |||
226 | LED_ON; |
||
227 | |||
228 | UART_Init(); |
||
229 | Timer0_Init(); |
||
230 | ADC_Init(); |
||
3 | ingob | 231 | InitIC2_Slave(); |
1 | ingob | 232 | sei();//Globale Interrupts Einschalten |
233 | Debug_Timer = SetDelay(100); // Sendeintervall |
||
3 | ingob | 234 | |
7 | hbuss | 235 | Xmin = eeprom_read_word(&eeXmin); |
236 | Xmax = eeprom_read_word(&eeXmax); |
||
237 | Ymin = eeprom_read_word(&eeYmin); |
||
238 | Ymax = eeprom_read_word(&eeYmax); |
||
239 | Zmin = eeprom_read_word(&eeZmin); |
||
240 | Zmax = eeprom_read_word(&eeZmax); |
||
1 | ingob | 241 | |
3 | ingob | 242 | VersionInfo.Hauptversion = VERSION_HAUPTVERSION; |
243 | VersionInfo.Nebenversion = VERSION_NEBENVERSION; |
||
1 | ingob | 244 | VersionInfo.PCKompatibel = 7; |
245 | |||
7 | hbuss | 246 | ExternData.Orientation = 0; |
247 | ExternData.CalState = 0; |
||
248 | I2C_WriteCal.CalByte = 0; |
||
1 | ingob | 249 | |
250 | while (1) |
||
251 | { |
||
252 | FLIP_LOW; |
||
253 | Delay_ms(2); |
||
254 | RawMagnet1a = MessAD(0); |
||
7 | hbuss | 255 | RawMagnet2a = -MessAD(1); |
1 | ingob | 256 | RawMagnet3a = MessAD(7); |
257 | Delay_ms(1); |
||
7 | hbuss | 258 | |
1 | ingob | 259 | FLIP_HIGH; |
260 | Delay_ms(2); |
||
261 | RawMagnet1b = MessAD(0); |
||
7 | hbuss | 262 | RawMagnet2b = -MessAD(1); |
1 | ingob | 263 | RawMagnet3b = MessAD(7); |
264 | Delay_ms(1); |
||
265 | |||
266 | CalcFields(); |
||
7 | hbuss | 267 | if(ExternData.CalState || I2C_WriteCal.CalByte) Calibrate(); |
268 | else CalcHeading(); |
||
269 | BearbeiteRxDaten(); |
||
1 | ingob | 270 | |
271 | if(PC_Connected) |
||
272 | { |
||
7 | hbuss | 273 | DDRD |= 0x02; // TXD-Portpin |
274 | UCR |= (1 << TXEN); |
||
275 | DatenUebertragung(); |
||
1 | ingob | 276 | PC_Connected--; |
277 | } |
||
278 | else |
||
279 | { |
||
7 | hbuss | 280 | UCR &= ~(1 << TXEN); |
281 | DDRD &= ~0x02; // TXD-Portpin |
||
1 | ingob | 282 | } |
7 | hbuss | 283 | } // while(1) |
1 | ingob | 284 | } |
285 |