Subversion Repositories MK3Mag

Rev

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