Subversion Repositories MK3Mag

Rev

Rev 27 | 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;
7 hbuss 71
 
21 killagreg 72
uint16_t Led_Timer = 0;
73
 
23 killagreg 74
struct Scaling_t
1 ingob 75
{
19 killagreg 76
        int16_t Range;
77
        int16_t Offset;
23 killagreg 78
} ;
1 ingob 79
 
23 killagreg 80
struct Calibration_t
19 killagreg 81
{
28 killagreg 82
        struct Scaling_t MagX;
83
        struct Scaling_t MagY;
84
        struct Scaling_t MagZ;
85
        struct Scaling_t AccX;
86
        struct Scaling_t AccY;
87
        struct Scaling_t AccZ;
23 killagreg 88
} ;
19 killagreg 89
 
28 killagreg 90
struct Calibration_t eeCalibration EEMEM;       // calibration data in EEProm
91
struct Calibration_t Calibration;               // calibration data in RAM
19 killagreg 92
 
28 killagreg 93
// magnet sensor variable
94
int16_t RawMagnet1a, RawMagnet1b;                       // raw magnet sensor data
95
int16_t RawMagnet2a, RawMagnet2b;
96
int16_t RawMagnet3a, RawMagnet3b;
97
int16_t UncalMagX, UncalMagY, UncalMagZ;        // sensor signal difference without Scaling
98
int16_t MagX, MagY, MagZ;                                       // rescaled magnetic field readings
99
// acc sensor variables
100
int16_t RawAccX, RawAccY, RawAccZ;                      // raw acceleration readings
101
int16_t AccX, AccY, AccZ;                                       // rescaled acceleration readings
102
uint8_t AccPresent = 0;
19 killagreg 103
uint8_t PC_Connected = 0;
104
 
21 killagreg 105
int16_t Heading = -1;
19 killagreg 106
 
107
 
1 ingob 108
void CalcFields(void)
7 hbuss 109
{
28 killagreg 110
        UncalMagX = (RawMagnet1a - RawMagnet1b);
111
        UncalMagY = (RawMagnet3a - RawMagnet3b);
112
        UncalMagZ = (RawMagnet2a - RawMagnet2b);
7 hbuss 113
 
28 killagreg 114
        if(Calibration.MagX.Range != 0) MagX = (1024L * (int32_t)(UncalMagX - Calibration.MagX.Offset)) / (Calibration.MagX.Range);
115
        else MagX = 0;
116
        if(Calibration.MagY.Range != 0) MagY = (1024L * (int32_t)(UncalMagY - Calibration.MagY.Offset)) / (Calibration.MagY.Range);
117
        else MagY = 0;
118
        if(Calibration.MagY.Range != 0) MagZ = (1024L * (int32_t)(UncalMagZ - Calibration.MagZ.Offset)) / (Calibration.MagZ.Range);
119
        else MagZ = 0;
1 ingob 120
}
121
 
20 killagreg 122
 
1 ingob 123
void CalcHeading(void)
124
{
19 killagreg 125
        double nick_rad, roll_rad, Hx, Hy, Cx, Cy, Cz;
126
        int16_t heading = -1;
1 ingob 127
 
21 killagreg 128
        // blink code for normal operation
129
        if(CheckDelay(Led_Timer))
130
        {
131
                LED_GRN_TOGGLE;
132
                Led_Timer = SetDelay(500);
133
        }
7 hbuss 134
 
28 killagreg 135
        Cx = MagX;
136
        Cy = MagY;
137
        Cz = MagZ;
7 hbuss 138
 
19 killagreg 139
        if(ExternData.Orientation == 1)
140
        {
28 killagreg 141
                Cx = MagX;
142
                Cy = -MagY;
143
                Cz = MagZ;
19 killagreg 144
        }
1 ingob 145
 
20 killagreg 146
        // calculate nick and roll angle in rad
28 killagreg 147
        switch(AttitudeSource)
148
        {
149
                case ATTITUDE_SOURCE_I2C:
150
                        nick_rad = ((double)I2C_WriteAttitude.Nick) * M_PI / (double)(1800.0);
151
                        roll_rad = ((double)I2C_WriteAttitude.Roll) * M_PI / (double)(1800.0);
152
                        break;
153
 
154
                case ATTITUDE_SOURCE_UART:
155
                        nick_rad = ((double)ExternData.Attitude[NICK]) * M_PI / (double)(1800.0);
156
                        roll_rad = ((double)ExternData.Attitude[ROLL]) * M_PI / (double)(1800.0);
157
                        break;
158
 
159
                case ATTITUDE_SOURCE_ACC:
160
                        if(AccX >  125) nick_rad = M_PI / 2;
161
                        else
162
                        if(AccX < -125) nick_rad = -M_PI / 2;
163
                        else
164
                        {
165
                                nick_rad = asin((double) AccX / 125.0);
166
                        }
167
 
168
                        if(AccY >  125) roll_rad = M_PI / 2;
169
                        else
170
                        if(AccY < -125) roll_rad = -M_PI / 2;
171
                        else
172
                        {
173
                        roll_rad = asin((double) AccY / 125.0);
174
                        }
175
                        break;
176
        }
177
 
20 killagreg 178
        // calculate attitude correction
27 killagreg 179
        Hx = Cx * cos(nick_rad) - Cz * sin(nick_rad);
180
        Hy = Cy * cos(roll_rad) + Cz * sin(roll_rad);
19 killagreg 181
 
20 killagreg 182
        // calculate Heading
183
        heading = (int16_t)((180.0 * atan2(Hy, Hx)) / M_PI);
184
        // atan2 returns angular range from -180 deg to 180 deg in counter clockwise notation
185
        // but the compass course is defined in a range from 0 deg to 360 deg clockwise notation.
186
        if (heading < 0) heading = -heading;
187
        else heading = 360 - heading;
19 killagreg 188
 
189
        if(abs(heading) < 361) Heading = heading;
20 killagreg 190
        else (Heading = -1);
1 ingob 191
}
192
 
193
 
7 hbuss 194
void Calibrate(void)
195
{
19 killagreg 196
        uint8_t cal;
21 killagreg 197
        static uint8_t calold = 0;
19 killagreg 198
        static int16_t Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0;
21 killagreg 199
        static uint8_t blinkcount = 0;
22 killagreg 200
 
19 killagreg 201
        // check both sources of communication for calibration request
202
        if(I2C_WriteCal.CalByte) cal = I2C_WriteCal.CalByte;
22 killagreg 203
        else                     cal = ExternData.CalState;
19 killagreg 204
 
23 killagreg 205
 
206
        if(cal > 5) cal = 0;
21 killagreg 207
        // blink code for current calibration state
208
        if(cal)
209
        {
210
                if(CheckDelay(Led_Timer) || (cal != calold))
211
                {
212
                        if(blinkcount & 0x01) LED_GRN_OFF;
213
                        else LED_GRN_ON;
214
 
215
                        // end of blinkcount sequence
216
                        if( (blinkcount + 1 ) >= (2 * cal) )
217
                        {
218
                                blinkcount = 0;
219
                                Led_Timer = SetDelay(1000);
220
                        }
221
                        else
222
                        {
223
                                blinkcount++;
224
                                Led_Timer = SetDelay(170);
225
                        }
226
                }
227
        }
228
        else
229
        {
230
                LED_GRN_OFF;
231
        }
232
 
19 killagreg 233
        // calibration state machine
234
        switch(cal)
235
        {
236
                case 1: // 1st step of calibration
237
                        // initialize ranges
238
                        // used to change the orientation of the MK3MAG in the horizontal plane
239
                        Xmin =  10000;
240
                        Xmax = -10000;
241
                        Ymin =  10000;
242
                        Ymax = -10000;
243
                        Zmin =  10000;
244
                        Zmax = -10000;
28 killagreg 245
                        Calibration.AccX.Offset = RawAccX;
246
                        Calibration.AccY.Offset = RawAccY;
247
                Calibration.AccZ.Offset = RawAccZ;
19 killagreg 248
                        break;
249
 
250
                case 2: // 2nd step of calibration
251
                        // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane
28 killagreg 252
                        if(UncalMagX < Xmin) Xmin = UncalMagX;
253
                        if(UncalMagX > Xmax) Xmax = UncalMagX;
254
                        if(UncalMagY < Ymin) Ymin = UncalMagY;
255
                        if(UncalMagY > Ymax) Ymax = UncalMagY;
19 killagreg 256
                        break;
257
 
258
                case 3: // 3rd step of calibration
26 killagreg 259
                        // used to change the orientation of the MK3MAG vertical to the horizontal plane
19 killagreg 260
                        break;
261
 
262
                case 4:
263
                        // find Min and Max of the Z-Sensor
28 killagreg 264
                        if(UncalMagZ < Zmin) Zmin = UncalMagZ;
265
                        if(UncalMagZ > Zmax) Zmax = UncalMagZ;
19 killagreg 266
                        break;
267
 
268
                case 5:
21 killagreg 269
                        // Save values
25 killagreg 270
                        if(cal != calold) // avoid continously writing of eeprom!
19 killagreg 271
                        {
28 killagreg 272
                                Calibration.MagY.Range = Xmax - Xmin;
273
                                Calibration.MagX.Offset = (Xmin + Xmax) / 2;
274
                                Calibration.MagY.Range = Ymax - Ymin;
275
                                Calibration.MagY.Offset = (Ymin + Ymax) / 2;
276
                                Calibration.MagZ.Range = Zmax - Zmin;
277
                                Calibration.MagZ.Offset = (Zmin + Zmax) / 2;
278
                                if((Calibration.MagX.Range > 150) && (Calibration.MagY.Range > 150) && (Calibration.MagZ.Range > 150))
21 killagreg 279
                                {
280
                                        // indicate write process by setting the led
281
                                        LED_GRN_ON;
23 killagreg 282
                                        eeprom_write_block(&Calibration, &eeCalibration, sizeof(Calibration));
21 killagreg 283
                                        Delay_ms(2000);
284
                                        // reset led state
285
                                        LED_GRN_OFF;
286
                                        // reset  blinkcode
287
                                        blinkcount = 0;
288
                                        Led_Timer = SetDelay(1000);
289
                                }
19 killagreg 290
                        }
291
                        break;
292
 
293
                default:
294
                        break;
295
        }
21 killagreg 296
        calold = cal;
7 hbuss 297
}
1 ingob 298
 
7 hbuss 299
 
300
void SetDebugValues(void)
301
{
28 killagreg 302
        DebugOut.Analog[0] =  MagX;
303
        DebugOut.Analog[1] =  MagY;
304
        DebugOut.Analog[2] =  MagZ;
305
        DebugOut.Analog[3] =  UncalMagX;
306
        DebugOut.Analog[4] =  UncalMagY;
307
        DebugOut.Analog[5] =  UncalMagZ;
308
        switch(AttitudeSource)
309
        {
310
                case ATTITUDE_SOURCE_ACC:
311
 
312
                        break;
313
 
314
                case ATTITUDE_SOURCE_UART:
315
                        DebugOut.Analog[6] =  ExternData.Attitude[NICK];
316
                        DebugOut.Analog[7] =  ExternData.Attitude[ROLL];
317
                        break;
318
 
319
 
320
                case ATTITUDE_SOURCE_I2C:
321
                        DebugOut.Analog[6] =  I2C_WriteAttitude.Nick;
322
                        DebugOut.Analog[7] =  I2C_WriteAttitude.Roll;
323
                        break;
324
        }
325
        DebugOut.Analog[8] =  Calibration.MagX.Offset;
326
        DebugOut.Analog[9] =  Calibration.MagX.Range;
327
        DebugOut.Analog[10] = Calibration.MagY.Offset;
328
        DebugOut.Analog[11] = Calibration.MagY.Range;
329
        DebugOut.Analog[12] = Calibration.MagZ.Offset;
330
        DebugOut.Analog[13] = Calibration.MagZ.Range;
19 killagreg 331
        DebugOut.Analog[14] = ExternData.CalState;
332
        DebugOut.Analog[15] = Heading;
333
        DebugOut.Analog[16] = ExternData.UserParam[0];
334
        DebugOut.Analog[17] = ExternData.UserParam[1];
28 killagreg 335
        DebugOut.Analog[18] = AccX;
336
        DebugOut.Analog[19] = AccY;
337
        DebugOut.Analog[20] = AccZ;
338
        DebugOut.Analog[21] = RawAccX;
339
        DebugOut.Analog[22] = RawAccY;
340
        DebugOut.Analog[23] = RawAccZ;
341
        DebugOut.Analog[24] = Calibration.AccX.Offset;
342
        DebugOut.Analog[25] = Calibration.AccY.Offset;
343
    DebugOut.Analog[26] = Calibration.AccZ.Offset;
7 hbuss 344
}
345
 
28 killagreg 346
void AccMeasurement(void)
347
{
348
        if(AccPresent)
349
        {
350
                RawAccX = (RawAccX + (int16_t)ADC_GetValue(ADC2))/2;
351
                RawAccY = (RawAccY + (int16_t)ADC_GetValue(ADC3))/2;
352
                RawAccZ = (RawAccZ + (int16_t)ADC_GetValue(ADC6))/2;
353
        }
354
        else
355
        {
356
                RawAccX = 0;
357
                RawAccY = 0;
358
                RawAccZ = 0;
359
        }
360
                AccX = ((RawAccX - Calibration.AccX.Offset) + AccX * 7) / 8;
361
                AccY = ((RawAccY - Calibration.AccY.Offset) + AccY * 7) / 8;
362
                AccZ = ((Calibration.AccZ.Offset - RawAccZ) + AccZ * 7) / 8;
363
}
7 hbuss 364
 
28 killagreg 365
 
366
 
1 ingob 367
int main (void)
368
{
22 killagreg 369
        // reset input pullup
370
        DDRC &=~((1<<DDC6));
371
        PORTC |= (1<<PORTC6);
372
 
373
    LED_Init();
19 killagreg 374
    TIMER0_Init();
375
    USART0_Init();
1 ingob 376
    ADC_Init();
19 killagreg 377
        I2C_Init();
1 ingob 378
 
22 killagreg 379
    sei(); // enable globale interrupts
21 killagreg 380
 
22 killagreg 381
    LED_GRN_ON;
19 killagreg 382
 
22 killagreg 383
    Debug_Timer = SetDelay(200);
384
    Led_Timer = SetDelay(200);
21 killagreg 385
 
19 killagreg 386
        // read calibration info from eeprom
23 killagreg 387
        eeprom_read_block(&Calibration, &eeCalibration, sizeof(Calibration));
19 killagreg 388
 
7 hbuss 389
    ExternData.Orientation = 0;
390
    ExternData.CalState = 0;
391
    I2C_WriteCal.CalByte = 0;
19 killagreg 392
 
22 killagreg 393
 
19 killagreg 394
        // main loop
1 ingob 395
    while (1)
19 killagreg 396
    {
397
                FLIP_LOW;
398
                Delay_ms(2);
399
                RawMagnet1a = ADC_GetValue(ADC0);
400
                RawMagnet2a = -ADC_GetValue(ADC1);
401
                RawMagnet3a = ADC_GetValue(ADC7);
28 killagreg 402
                AccMeasurement();
19 killagreg 403
                Delay_ms(1);
7 hbuss 404
 
19 killagreg 405
                FLIP_HIGH;
406
                Delay_ms(2);
407
                RawMagnet1b = ADC_GetValue(ADC0);
408
                RawMagnet2b = -ADC_GetValue(ADC1);
409
                RawMagnet3b = ADC_GetValue(ADC7);
28 killagreg 410
                AccMeasurement();
19 killagreg 411
                Delay_ms(1);
1 ingob 412
 
19 killagreg 413
                CalcFields();
1 ingob 414
 
22 killagreg 415
                if(ExternData.CalState || I2C_WriteCal.CalByte) Calibrate();
19 killagreg 416
                else CalcHeading();
417
 
418
                // check data from USART
419
        USART0_ProcessRxData();
23 killagreg 420
                USART0_TransmitTxData();
19 killagreg 421
 
422
        if(PC_Connected)
423
        {
424
            USART0_EnableTXD();
425
            USART0_TransmitTxData();
426
            PC_Connected--;
427
                }
428
                else
429
                {
430
                        USART0_DisableTXD();
431
                }
432
        } // while(1)
1 ingob 433
}
434