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