Subversion Repositories MK3Mag

Rev

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