Subversion Repositories MK3Mag

Rev

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