Subversion Repositories MK3Mag

Rev

Rev 32 | Rev 34 | 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
32 holgerb 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
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32 holgerb 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
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32 holgerb 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
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32 holgerb 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
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32 holgerb 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.
32 holgerb 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)
32 holgerb 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
32 holgerb 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
32 holgerb 55
// +  POSSIBILITY OF SUCH DAMAGE.
5 hbuss 56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32 holgerb 57
#include <avr/interrupt.h>
58
#include <math.h>
59
#include <stdlib.h>
60
#include <stdio.h>
17 holgerb 61
 
1 ingob 62
#include "main.h"
32 holgerb 63
#include "timer0.h"
64
#include "twislave.h"
65
#include "led.h"
66
#include "analog.h"
67
#include "uart.h"
1 ingob 68
 
32 holgerb 69
 
70
AttitudeSource_t AttitudeSource = ATTITUDE_SOURCE_ACC;
71
Orientation_t Orientation = ORIENTATION_FC;
72
 
73
uint16_t Led_Timer = 0;
74
 
75
typedef struct
1 ingob 76
{
32 holgerb 77
        int16_t Range;
78
        int16_t Offset;
79
}  Scaling_t;
1 ingob 80
 
32 holgerb 81
typedef struct
82
{
83
        Scaling_t MagX;
84
        Scaling_t MagY;
85
        Scaling_t MagZ;
86
        Scaling_t AccX;
87
        Scaling_t AccY;
88
        Scaling_t AccZ;
89
}  Calibration_t;
90
 
91
Calibration_t eeCalibration EEMEM;      // calibration data in EEProm
92
Calibration_t Calibration;              // calibration data in RAM
93
 
94
// magnet sensor variable
95
int16_t RawMagnet1a, RawMagnet1b;                       // raw magnet sensor data
96
int16_t RawMagnet2a, RawMagnet2b;
97
int16_t RawMagnet3a, RawMagnet3b;
98
int16_t UncalMagX, UncalMagY, UncalMagZ;        // sensor signal difference without Scaling
99
int16_t MagX = 0, MagY = 0, MagZ = 0;           // rescaled magnetic field readings
100
 
101
// acceleration sensor variables
102
int16_t RawAccX = 0, RawAccY = 0, RawAccZ = 0;                  // raw acceleration readings
103
int16_t AccX = 0, AccY = 0, AccZ = 0;                                   // rescaled acceleration readings
104
int16_t AccAttitudeNick = 0, AccAttitudeRoll = 0;               // nick and roll angle from acc
105
 
106
int16_t Heading = -1;                                           // the current compass heading in deg
107
 
108
 
1 ingob 109
void CalcFields(void)
7 hbuss 110
{
32 holgerb 111
        UncalMagX = (RawMagnet1a - RawMagnet1b);
112
        UncalMagY = (RawMagnet3a - RawMagnet3b);
113
        UncalMagZ = (RawMagnet2a - RawMagnet2b);
7 hbuss 114
 
32 holgerb 115
        if(Calibration.MagX.Range != 0) MagX = (1024L * (int32_t)(UncalMagX - Calibration.MagX.Offset)) / (Calibration.MagX.Range);
116
        else MagX = 0;
117
        if(Calibration.MagY.Range != 0) MagY = (1024L * (int32_t)(UncalMagY - Calibration.MagY.Offset)) / (Calibration.MagY.Range);
118
        else MagY = 0;
119
        if(Calibration.MagY.Range != 0) MagZ = (1024L * (int32_t)(UncalMagZ - Calibration.MagZ.Offset)) / (Calibration.MagZ.Range);
120
        else MagZ = 0;
7 hbuss 121
 
32 holgerb 122
        if(AccPresent)
123
        {
124
                AccX = (RawAccX - Calibration.AccX.Offset);
125
                AccY = (RawAccY - Calibration.AccY.Offset);
126
                AccZ = (Calibration.AccZ.Offset - RawAccZ);
127
 
128
                #if (BOARD == 10) // the hardware 1.0 has the LIS3L02AL
129
                // acc mode assumes orientation like FC
130
                if(AccX >  136) AccAttitudeNick = -800;
131
                else
132
                if(AccX < -136) AccAttitudeNick = 800;
133
                else                    AccAttitudeNick = (int16_t)(-1800.0 * asin((double) AccX / 138.0) / M_PI);
134
 
135
 
136
                if(AccY >  136) AccAttitudeRoll = 800;
137
                else
138
                if(AccY < -136) AccAttitudeRoll = -800;
139
                else                    AccAttitudeRoll = (int16_t)( 1800.0 * asin((double) AccY / 138.0) / M_PI);
140
 
141
                #else // the hardware 1.1 has the LIS344ALH with a different axis definition (X -> -Y, Y -> X, Z -> Z)
142
                // acc mode assumes orientation like FC
143
                if(AccY >  136) AccAttitudeNick = 800;
144
                else
145
                if(AccY < -136) AccAttitudeNick = -800;
146
                else                    AccAttitudeNick = (int16_t)( 1800.0 * asin((double) AccY / 138.0) / M_PI);
147
 
148
 
149
                if(AccX >  136) AccAttitudeRoll = 800;
150
                else
151
                if(AccX < -136) AccAttitudeRoll = -800;
152
                else                    AccAttitudeRoll = (int16_t)( 1800.0 * asin((double) AccX / 138.0) / M_PI);
153
                #endif
154
        }
1 ingob 155
}
156
 
32 holgerb 157
 
1 ingob 158
void CalcHeading(void)
159
{
32 holgerb 160
        double nick_rad, roll_rad, Hx, Hy, Cx = 0.0, Cy = 0.0, Cz = 0.0;
33 killagreg 161
        int16_t nick, roll;
32 holgerb 162
        int16_t heading = -1;
1 ingob 163
 
32 holgerb 164
        // blink code for normal operation
165
        if(CheckDelay(Led_Timer))
166
        {
167
                LED_GRN_TOGGLE;
168
                Led_Timer = SetDelay(500);
169
        }
7 hbuss 170
 
32 holgerb 171
        switch(Orientation)
172
        {
173
                case ORIENTATION_NC:
174
                        Cx = MagX;
175
                        Cy = MagY;
176
                        Cz = MagZ;
177
                        break;
7 hbuss 178
 
32 holgerb 179
                case ORIENTATION_FC:
180
                        // rotation of 90 deg compared to NC setup
181
                        Cx = MagY;
182
                        Cy = -MagX;
183
                        Cz = MagZ;
184
                        break;
185
        }
186
 
187
        // calculate nick and roll angle in rad
188
        switch(AttitudeSource)
189
        {
190
                case ATTITUDE_SOURCE_I2C:
191
           cli(); // stop interrupts
192
            nick = I2C_WriteAttitude.Nick;
193
                        roll = I2C_WriteAttitude.Roll;
194
           sei(); // start interrupts
195
                        break;
196
                case ATTITUDE_SOURCE_UART:
197
           cli(); // stop interrupts
198
            nick = ExternData.Attitude[NICK];
199
            roll = ExternData.Attitude[ROLL];
200
           sei(); // start interrupts
201
                        break;
202
                case ATTITUDE_SOURCE_ACC:
203
            nick = AccAttitudeNick;
204
            roll = AccAttitudeRoll;
205
                        break;
206
                default:
207
                        nick_rad = 0;
208
                        roll_rad = 0;
209
                break;
210
        }
211
 
212
    nick_rad = ((double)nick) * M_PI / (double)(1800.0);
213
    roll_rad = ((double)roll) * M_PI / (double)(1800.0);
214
 
215
 
216
        // calculate attitude correction
217
        Hx = Cx * cos(nick_rad) - Cz * sin(nick_rad);
218
        Hy = Cy * cos(roll_rad) + Cz * sin(roll_rad);
219
 
220
        DebugOut.Analog[27] = (int16_t)Hx;
221
        DebugOut.Analog[28] = (int16_t)Hy;
222
 
223
        // calculate Heading
224
        heading = (int16_t)((180.0 * atan2(Hy, Hx)) / M_PI);
225
        // atan2 returns angular range from -180 deg to 180 deg in counter clockwise notation
226
        // but the compass course is defined in a range from 0 deg to 360 deg clockwise notation.
227
        if (heading < 0) heading = -heading;
228
        else heading = 360 - heading;
33 killagreg 229
 
32 holgerb 230
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33 killagreg 231
/* // Alternative formula
232
   Hx = Cx * (double)cos(nick_rad) +
1 ingob 233
        Cy * (double)sin(nick_rad) * (double)sin(roll_rad) -
33 killagreg 234
        Cz * (double)sin(nick_rad) * (double)cos(roll_rad);
235
 
236
   Hy = Cy * (double)cos(roll_rad) +
1 ingob 237
        Cz * (double)sin(roll_rad);
33 killagreg 238
 
1 ingob 239
   if(Hx == 0 && Hy < 0) heading = 90;
240
   else if(Hx == 0 && Hy > 0) heading = 270;
7 hbuss 241
   else if(Hx < 0) heading  = 180 - (atan(Hy / Hx) * 180.0) / M_PI;
242
   else if(Hx > 0 && Hy < 0) heading = - (atan(Hy / Hx) * 180.0) / M_PI;
243
   else if(Hx > 0 && Hy > 0) heading  = 360 - (atan(Hy / Hx) * 180.0) / M_PI;
1 ingob 244
 
7 hbuss 245
 if(abs(heading) < 361) Heading = heading;
32 holgerb 246
*/
247
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
248
 
33 killagreg 249
        cli(); // stop interrupts
32 holgerb 250
        if(abs(heading) < 361) Heading = heading;
251
        else (Heading = -1);
33 killagreg 252
        sei(); // start interrupts
253
 
1 ingob 254
}
255
 
256
 
7 hbuss 257
void Calibrate(void)
258
{
32 holgerb 259
        uint8_t cal;
260
        static uint8_t calold = 0;
261
        static int16_t Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0;
262
        static uint8_t blinkcount = 0;
263
        static uint8_t invert_blinking = 0;
33 killagreg 264
 
32 holgerb 265
        // check both sources of communication for calibration request
266
        if(I2C_WriteCal.CalByte) cal = I2C_WriteCal.CalByte;
267
        else                     cal = ExternData.CalState;
16 holgerb 268
 
32 holgerb 269
 
270
        if(cal > 5) cal = 0;
271
        // blink code for current calibration state
272
        if(cal)
273
        {
274
                if(CheckDelay(Led_Timer) || (cal != calold))
275
                {
276
                        if(blinkcount & 0x01) if(invert_blinking) LED_GRN_ON; else LED_GRN_OFF;
277
                        else if(invert_blinking) LED_GRN_OFF; else LED_GRN_ON;
278
 
279
                        // end of blinkcount sequence
280
                        if((blinkcount + 1 ) >= (2 * cal))
281
                        {
282
                                blinkcount = 0;
283
                                Led_Timer = SetDelay(1000);
284
                        }
285
                        else
286
                        {
287
                                blinkcount++;
288
                                Led_Timer = SetDelay(170);
289
                        }
290
                }
291
        }
292
        else
293
        {
294
                if(invert_blinking) LED_GRN_ON; else LED_GRN_OFF;
295
        }
296
 
297
        // calibration state machine
298
        switch(cal)
299
        {
300
                case 1: // 1st step of calibration
301
                        // initialize ranges
302
                        // used to change the orientation of the MK3MAG in the horizontal plane
303
                        Xmin =  10000;
304
                        Xmax = -10000;
305
                        Ymin =  10000;
306
                        Ymax = -10000;
307
                        Zmin =  10000;
308
                        Zmax = -10000;
309
                        Calibration.AccX.Offset = RawAccX;
310
                        Calibration.AccY.Offset = RawAccY;
311
                Calibration.AccZ.Offset = RawAccZ;
312
            invert_blinking = 0;
313
                        break;
314
 
315
                case 2: // 2nd step of calibration
316
                        // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane
317
                        if(UncalMagX < Xmin) Xmin = UncalMagX;
318
                        if(UncalMagX > Xmax) Xmax = UncalMagX;
319
                        if(UncalMagY < Ymin) Ymin = UncalMagY;
320
                        if(UncalMagY > Ymax) Ymax = UncalMagY;
321
            invert_blinking = 1;
322
                        break;
323
 
324
                case 3: // 3rd step of calibration
325
                        // used to change the orientation of the MK3MAG vertical to the horizontal plane
326
            invert_blinking = 0;
327
                        break;
328
 
329
                case 4:
330
                        // find Min and Max of the Z-Sensor
331
                        if(UncalMagZ < Zmin) Zmin = UncalMagZ;
332
                        if(UncalMagZ > Zmax) Zmax = UncalMagZ;
333
            invert_blinking = 1;
334
                        break;
335
 
336
                case 5:
337
                        // Save values
338
                        if(cal != calold) // avoid continously writing of eeprom!
339
                        {
340
                                Calibration.MagX.Range = Xmax - Xmin;
341
                                Calibration.MagX.Offset = (Xmin + Xmax) / 2;
342
                                Calibration.MagY.Range = Ymax - Ymin;
343
                                Calibration.MagY.Offset = (Ymin + Ymax) / 2;
344
                                Calibration.MagZ.Range = Zmax - Zmin;
345
                                Calibration.MagZ.Offset = (Zmin + Zmax) / 2;
346
                                if((Calibration.MagX.Range > 150) && (Calibration.MagY.Range > 150) && (Calibration.MagZ.Range > 150))
347
                                {
348
                                        // indicate write process by setting the led
349
                                        LED_GRN_ON;
350
                                        eeprom_write_block(&Calibration, &eeCalibration, sizeof(Calibration));
351
                                        Led_Timer = SetDelay(2000);
352
                                        // reset  blinkcode
353
                                        blinkcount = 0;
354
                                }
355
                        }
356
            invert_blinking = 0;
357
                        break;
358
 
359
                default:
360
                        break;
361
        }
362
        calold = cal;
7 hbuss 363
}
1 ingob 364
 
32 holgerb 365
 
7 hbuss 366
void SetDebugValues(void)
367
{
32 holgerb 368
        DebugOut.Analog[0] =  MagX;
369
        DebugOut.Analog[1] =  MagY;
370
        DebugOut.Analog[2] =  MagZ;
371
        DebugOut.Analog[3] =  UncalMagX;
372
        DebugOut.Analog[4] =  UncalMagY;
373
        DebugOut.Analog[5] =  UncalMagZ;
374
        switch(AttitudeSource)
375
        {
376
                case ATTITUDE_SOURCE_ACC:
377
                        DebugOut.Analog[6] =  AccAttitudeNick;
378
                        DebugOut.Analog[7] =  AccAttitudeRoll;
379
                        break;
380
 
381
                case ATTITUDE_SOURCE_UART:
382
                        DebugOut.Analog[6] =  ExternData.Attitude[NICK];
383
                        DebugOut.Analog[7] =  ExternData.Attitude[ROLL];
384
                        break;
385
 
386
 
387
                case ATTITUDE_SOURCE_I2C:
388
                        DebugOut.Analog[6] =  I2C_WriteAttitude.Nick;
389
                        DebugOut.Analog[7] =  I2C_WriteAttitude.Roll;
390
                        break;
391
        }
392
        DebugOut.Analog[8] =  Calibration.MagX.Offset;
393
        DebugOut.Analog[9] =  Calibration.MagX.Range;
394
        DebugOut.Analog[10] = Calibration.MagY.Offset;
395
        DebugOut.Analog[11] = Calibration.MagY.Range;
396
        DebugOut.Analog[12] = Calibration.MagZ.Offset;
397
        DebugOut.Analog[13] = Calibration.MagZ.Range;
398
        DebugOut.Analog[14] = ExternData.CalState;
399
        DebugOut.Analog[15] = Heading;
400
        DebugOut.Analog[16] = ExternData.UserParam[0];
401
        DebugOut.Analog[17] = ExternData.UserParam[1];
402
        DebugOut.Analog[18] = AccX;
403
        DebugOut.Analog[19] = AccY;
404
        DebugOut.Analog[20] = AccZ;
405
        DebugOut.Analog[21] = RawAccX;
406
        DebugOut.Analog[22] = RawAccY;
407
        DebugOut.Analog[23] = RawAccZ;
408
        DebugOut.Analog[24] = Calibration.AccX.Offset;
409
        DebugOut.Analog[25] = Calibration.AccY.Offset;
410
    DebugOut.Analog[26] = Calibration.AccZ.Offset;
411
    DebugOut.Analog[29] = AttitudeSource;
7 hbuss 412
}
413
 
16 holgerb 414
void AccMeasurement(void)
415
{
32 holgerb 416
        if(AccPresent)
417
        {
418
                RawAccX = (RawAccX + (int16_t)ADC_GetValue(ACC_X))/2;
419
                RawAccY = (RawAccY + (int16_t)ADC_GetValue(ACC_Y))/2;
420
                RawAccZ = (RawAccZ + (int16_t)ADC_GetValue(ACC_Z))/2;
421
        }
422
        else
423
        {
424
                RawAccX = 0;
425
                RawAccY = 0;
426
                RawAccZ = 0;
427
        }
16 holgerb 428
}
7 hbuss 429
 
1 ingob 430
int main (void)
431
{
32 holgerb 432
        // reset input pullup
433
        DDRC &=~((1<<DDC6));
434
        PORTC |= (1<<PORTC6);
17 holgerb 435
 
32 holgerb 436
    LED_Init();
437
    TIMER0_Init();
438
    USART0_Init();
1 ingob 439
    ADC_Init();
32 holgerb 440
        I2C_Init();
17 holgerb 441
 
32 holgerb 442
    sei(); // enable globale interrupts
443
 
444
    if(AccPresent)
445
    {
446
                USART0_Print("ACC present\n");
447
        }
448
 
449
    LED_GRN_ON;
450
 
451
    Debug_Timer = SetDelay(200);
452
    Led_Timer = SetDelay(200);
453
 
454
        // read calibration info from eeprom
455
        eeprom_read_block(&Calibration, &eeCalibration, sizeof(Calibration));
456
 
7 hbuss 457
    ExternData.CalState = 0;
458
    I2C_WriteCal.CalByte = 0;
32 holgerb 459
 
460
 
461
        // main loop
1 ingob 462
    while (1)
32 holgerb 463
    {
464
                FLIP_LOW;
465
                Delay_ms(2);
466
                RawMagnet1a = ADC_GetValue(MAG_X);
467
                RawMagnet2a = -ADC_GetValue(MAG_Y);
468
                RawMagnet3a = ADC_GetValue(MAG_Z);
469
                AccMeasurement();
470
                Delay_ms(1);
7 hbuss 471
 
32 holgerb 472
                FLIP_HIGH;
473
                Delay_ms(2);
474
                RawMagnet1b = ADC_GetValue(MAG_X);
475
                RawMagnet2b = -ADC_GetValue(MAG_Y);
476
                RawMagnet3b = ADC_GetValue(MAG_Z);
477
                AccMeasurement();
478
                Delay_ms(1);
1 ingob 479
 
32 holgerb 480
                CalcFields();
1 ingob 481
 
32 holgerb 482
                if(ExternData.CalState || I2C_WriteCal.CalByte) Calibrate();
483
                else CalcHeading();
16 holgerb 484
 
32 holgerb 485
                // check data from USART
486
        USART0_ProcessRxData();
487
 
488
                if(NC_Connected) NC_Connected--;
489
                if(FC_Connected) FC_Connected--;
490
                // fall back to attitude estimation from acc sensor if NC or FC does'nt send attittude data
491
                if(!FC_Connected && ! NC_Connected)
492
                {
493
                        AttitudeSource = ATTITUDE_SOURCE_ACC;
494
                        Orientation = ORIENTATION_FC;
495
                }
496
 
497
        if(PC_Connected)
498
        {
499
            USART0_EnableTXD();
500
            USART0_TransmitTxData();
501
            PC_Connected--;
502
                }
503
                else
504
                {
505
                        USART0_DisableTXD();
506
                }
507
        } // while(1)
1 ingob 508
}
509