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