Subversion Repositories MK3Mag

Rev

Rev 17 | Rev 33 | 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;
161
    int16_t nick, roll;
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
        }
170
//MagX = 150;
171
//MagZ = 1000;
7 hbuss 172
 
32 holgerb 173
        switch(Orientation)
174
        {
175
                case ORIENTATION_NC:
176
                        Cx = MagX;
177
                        Cy = MagY;
178
                        Cz = MagZ;
179
                        break;
7 hbuss 180
 
32 holgerb 181
                case ORIENTATION_FC:
182
                        // rotation of 90 deg compared to NC setup
183
                        Cx = MagY;
184
                        Cy = -MagX;
185
                        Cz = MagZ;
186
                        break;
187
        }
188
 
189
        // calculate nick and roll angle in rad
190
        switch(AttitudeSource)
191
        {
192
                case ATTITUDE_SOURCE_I2C:
193
           cli(); // stop interrupts
194
            nick = I2C_WriteAttitude.Nick;
195
                        roll = I2C_WriteAttitude.Roll;
196
           sei(); // start interrupts
197
                        break;
198
                case ATTITUDE_SOURCE_UART:
199
           cli(); // stop interrupts
200
            nick = ExternData.Attitude[NICK];
201
            roll = ExternData.Attitude[ROLL];
202
           sei(); // start interrupts
203
                        break;
204
                case ATTITUDE_SOURCE_ACC:
205
            nick = AccAttitudeNick;
206
            roll = AccAttitudeRoll;
207
                        break;
208
                default:
209
                        nick_rad = 0;
210
                        roll_rad = 0;
211
                break;
212
        }
213
 
214
    nick_rad = ((double)nick) * M_PI / (double)(1800.0);
215
    roll_rad = ((double)roll) * M_PI / (double)(1800.0);
216
 
217
//    nick_rad = 0;
218
//    roll_rad = 0;
219
 
220
        // calculate attitude correction
221
        Hx = Cx * cos(nick_rad) - Cz * sin(nick_rad);
222
        Hy = Cy * cos(roll_rad) + Cz * sin(roll_rad);
223
 
224
        DebugOut.Analog[27] = (int16_t)Hx;
225
        DebugOut.Analog[28] = (int16_t)Hy;
226
 
227
        // calculate Heading
228
        heading = (int16_t)((180.0 * atan2(Hy, Hx)) / M_PI);
229
        // atan2 returns angular range from -180 deg to 180 deg in counter clockwise notation
230
        // but the compass course is defined in a range from 0 deg to 360 deg clockwise notation.
231
        if (heading < 0) heading = -heading;
232
        else heading = 360 - heading;
233
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
234
/*
1 ingob 235
   Hx = Cx * (double)cos(nick_rad) +
236
        Cy * (double)sin(nick_rad) * (double)sin(roll_rad) -
237
        Cz * (double)sin(nick_rad) * (double)cos(roll_rad);      
238
 
239
   Hy = Cy * (double)cos(roll_rad) +
240
        Cz * (double)sin(roll_rad);
241
 
242
   if(Hx == 0 && Hy < 0) heading = 90;
243
   else if(Hx == 0 && Hy > 0) heading = 270;
7 hbuss 244
   else if(Hx < 0) heading  = 180 - (atan(Hy / Hx) * 180.0) / M_PI;
245
   else if(Hx > 0 && Hy < 0) heading = - (atan(Hy / Hx) * 180.0) / M_PI;
246
   else if(Hx > 0 && Hy > 0) heading  = 360 - (atan(Hy / Hx) * 180.0) / M_PI;
1 ingob 247
 
7 hbuss 248
 if(abs(heading) < 361) Heading = heading;
32 holgerb 249
*/
250
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
251
 
252
   cli(); // stop interrupts
253
        if(abs(heading) < 361) Heading = heading;
254
        else (Heading = -1);
255
   sei(); // start interrupts
256
 
1 ingob 257
}
258
 
259
 
7 hbuss 260
void Calibrate(void)
261
{
32 holgerb 262
        uint8_t cal;
263
        static uint8_t calold = 0;
264
        static int16_t Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0;
265
        static uint8_t blinkcount = 0;
266
        static uint8_t invert_blinking = 0;
267
 
268
        // check both sources of communication for calibration request
269
        if(I2C_WriteCal.CalByte) cal = I2C_WriteCal.CalByte;
270
        else                     cal = ExternData.CalState;
16 holgerb 271
 
32 holgerb 272
 
273
        if(cal > 5) cal = 0;
274
        // blink code for current calibration state
275
        if(cal)
276
        {
277
                if(CheckDelay(Led_Timer) || (cal != calold))
278
                {
279
                        if(blinkcount & 0x01) if(invert_blinking) LED_GRN_ON; else LED_GRN_OFF;
280
                        else if(invert_blinking) LED_GRN_OFF; else LED_GRN_ON;
281
 
282
                        // end of blinkcount sequence
283
                        if((blinkcount + 1 ) >= (2 * cal))
284
                        {
285
                                blinkcount = 0;
286
                                Led_Timer = SetDelay(1000);
287
                        }
288
                        else
289
                        {
290
                                blinkcount++;
291
                                Led_Timer = SetDelay(170);
292
                        }
293
                }
294
        }
295
        else
296
        {
297
                if(invert_blinking) LED_GRN_ON; else LED_GRN_OFF;
298
        }
299
 
300
        // calibration state machine
301
        switch(cal)
302
        {
303
                case 1: // 1st step of calibration
304
                        // initialize ranges
305
                        // used to change the orientation of the MK3MAG in the horizontal plane
306
                        Xmin =  10000;
307
                        Xmax = -10000;
308
                        Ymin =  10000;
309
                        Ymax = -10000;
310
                        Zmin =  10000;
311
                        Zmax = -10000;
312
                        Calibration.AccX.Offset = RawAccX;
313
                        Calibration.AccY.Offset = RawAccY;
314
                Calibration.AccZ.Offset = RawAccZ;
315
            invert_blinking = 0;
316
                        break;
317
 
318
                case 2: // 2nd step of calibration
319
                        // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane
320
                        if(UncalMagX < Xmin) Xmin = UncalMagX;
321
                        if(UncalMagX > Xmax) Xmax = UncalMagX;
322
                        if(UncalMagY < Ymin) Ymin = UncalMagY;
323
                        if(UncalMagY > Ymax) Ymax = UncalMagY;
324
            invert_blinking = 1;
325
                        break;
326
 
327
                case 3: // 3rd step of calibration
328
                        // used to change the orientation of the MK3MAG vertical to the horizontal plane
329
            invert_blinking = 0;
330
                        break;
331
 
332
                case 4:
333
                        // find Min and Max of the Z-Sensor
334
                        if(UncalMagZ < Zmin) Zmin = UncalMagZ;
335
                        if(UncalMagZ > Zmax) Zmax = UncalMagZ;
336
            invert_blinking = 1;
337
                        break;
338
 
339
                case 5:
340
                        // Save values
341
                        if(cal != calold) // avoid continously writing of eeprom!
342
                        {
343
                                Calibration.MagX.Range = Xmax - Xmin;
344
                                Calibration.MagX.Offset = (Xmin + Xmax) / 2;
345
                                Calibration.MagY.Range = Ymax - Ymin;
346
                                Calibration.MagY.Offset = (Ymin + Ymax) / 2;
347
                                Calibration.MagZ.Range = Zmax - Zmin;
348
                                Calibration.MagZ.Offset = (Zmin + Zmax) / 2;
349
                                if((Calibration.MagX.Range > 150) && (Calibration.MagY.Range > 150) && (Calibration.MagZ.Range > 150))
350
                                {
351
                                        // indicate write process by setting the led
352
                                        LED_GRN_ON;
353
                                        eeprom_write_block(&Calibration, &eeCalibration, sizeof(Calibration));
354
                                        Led_Timer = SetDelay(2000);
355
                                        // reset  blinkcode
356
                                        blinkcount = 0;
357
                                }
358
                        }
359
            invert_blinking = 0;
360
                        break;
361
 
362
                default:
363
                        break;
364
        }
365
        calold = cal;
7 hbuss 366
}
1 ingob 367
 
32 holgerb 368
 
7 hbuss 369
void SetDebugValues(void)
370
{
32 holgerb 371
        DebugOut.Analog[0] =  MagX;
372
        DebugOut.Analog[1] =  MagY;
373
        DebugOut.Analog[2] =  MagZ;
374
        DebugOut.Analog[3] =  UncalMagX;
375
        DebugOut.Analog[4] =  UncalMagY;
376
        DebugOut.Analog[5] =  UncalMagZ;
377
        switch(AttitudeSource)
378
        {
379
                case ATTITUDE_SOURCE_ACC:
380
                        DebugOut.Analog[6] =  AccAttitudeNick;
381
                        DebugOut.Analog[7] =  AccAttitudeRoll;
382
                        break;
383
 
384
                case ATTITUDE_SOURCE_UART:
385
                        DebugOut.Analog[6] =  ExternData.Attitude[NICK];
386
                        DebugOut.Analog[7] =  ExternData.Attitude[ROLL];
387
                        break;
388
 
389
 
390
                case ATTITUDE_SOURCE_I2C:
391
                        DebugOut.Analog[6] =  I2C_WriteAttitude.Nick;
392
                        DebugOut.Analog[7] =  I2C_WriteAttitude.Roll;
393
                        break;
394
        }
395
        DebugOut.Analog[8] =  Calibration.MagX.Offset;
396
        DebugOut.Analog[9] =  Calibration.MagX.Range;
397
        DebugOut.Analog[10] = Calibration.MagY.Offset;
398
        DebugOut.Analog[11] = Calibration.MagY.Range;
399
        DebugOut.Analog[12] = Calibration.MagZ.Offset;
400
        DebugOut.Analog[13] = Calibration.MagZ.Range;
401
        DebugOut.Analog[14] = ExternData.CalState;
402
        DebugOut.Analog[15] = Heading;
403
        DebugOut.Analog[16] = ExternData.UserParam[0];
404
        DebugOut.Analog[17] = ExternData.UserParam[1];
405
        DebugOut.Analog[18] = AccX;
406
        DebugOut.Analog[19] = AccY;
407
        DebugOut.Analog[20] = AccZ;
408
        DebugOut.Analog[21] = RawAccX;
409
        DebugOut.Analog[22] = RawAccY;
410
        DebugOut.Analog[23] = RawAccZ;
411
        DebugOut.Analog[24] = Calibration.AccX.Offset;
412
        DebugOut.Analog[25] = Calibration.AccY.Offset;
413
    DebugOut.Analog[26] = Calibration.AccZ.Offset;
414
    DebugOut.Analog[29] = AttitudeSource;
7 hbuss 415
}
416
 
16 holgerb 417
void AccMeasurement(void)
418
{
32 holgerb 419
        if(AccPresent)
420
        {
421
                RawAccX = (RawAccX + (int16_t)ADC_GetValue(ACC_X))/2;
422
                RawAccY = (RawAccY + (int16_t)ADC_GetValue(ACC_Y))/2;
423
                RawAccZ = (RawAccZ + (int16_t)ADC_GetValue(ACC_Z))/2;
424
        }
425
        else
426
        {
427
                RawAccX = 0;
428
                RawAccY = 0;
429
                RawAccZ = 0;
430
        }
16 holgerb 431
}
7 hbuss 432
 
1 ingob 433
int main (void)
434
{
32 holgerb 435
        // reset input pullup
436
        DDRC &=~((1<<DDC6));
437
        PORTC |= (1<<PORTC6);
17 holgerb 438
 
32 holgerb 439
    LED_Init();
440
    TIMER0_Init();
441
    USART0_Init();
1 ingob 442
    ADC_Init();
32 holgerb 443
        I2C_Init();
17 holgerb 444
 
32 holgerb 445
    sei(); // enable globale interrupts
446
 
447
    if(AccPresent)
448
    {
449
                USART0_Print("ACC present\n");
450
        }
451
 
452
    LED_GRN_ON;
453
 
454
    Debug_Timer = SetDelay(200);
455
    Led_Timer = SetDelay(200);
456
 
457
        // read calibration info from eeprom
458
        eeprom_read_block(&Calibration, &eeCalibration, sizeof(Calibration));
459
 
7 hbuss 460
    ExternData.CalState = 0;
461
    I2C_WriteCal.CalByte = 0;
32 holgerb 462
 
463
 
464
        // main loop
1 ingob 465
    while (1)
32 holgerb 466
    {
467
                FLIP_LOW;
468
                Delay_ms(2);
469
                RawMagnet1a = ADC_GetValue(MAG_X);
470
                RawMagnet2a = -ADC_GetValue(MAG_Y);
471
                RawMagnet3a = ADC_GetValue(MAG_Z);
472
                AccMeasurement();
473
                Delay_ms(1);
7 hbuss 474
 
32 holgerb 475
                FLIP_HIGH;
476
                Delay_ms(2);
477
                RawMagnet1b = ADC_GetValue(MAG_X);
478
                RawMagnet2b = -ADC_GetValue(MAG_Y);
479
                RawMagnet3b = ADC_GetValue(MAG_Z);
480
                AccMeasurement();
481
                Delay_ms(1);
1 ingob 482
 
32 holgerb 483
                CalcFields();
1 ingob 484
 
32 holgerb 485
                if(ExternData.CalState || I2C_WriteCal.CalByte) Calibrate();
486
                else CalcHeading();
16 holgerb 487
 
32 holgerb 488
                // check data from USART
489
        USART0_ProcessRxData();
490
 
491
                if(NC_Connected) NC_Connected--;
492
                if(FC_Connected) FC_Connected--;
493
                // fall back to attitude estimation from acc sensor if NC or FC does'nt send attittude data
494
                if(!FC_Connected && ! NC_Connected)
495
                {
496
                        AttitudeSource = ATTITUDE_SOURCE_ACC;
497
                        Orientation = ORIENTATION_FC;
498
                }
499
 
500
        if(PC_Connected)
501
        {
502
            USART0_EnableTXD();
503
            USART0_TransmitTxData();
504
            PC_Connected--;
505
                }
506
                else
507
                {
508
                        USART0_DisableTXD();
509
                }
510
        } // while(1)
1 ingob 511
}
512