Subversion Repositories MK3Mag

Rev

Rev 20 | Rev 22 | 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
 
19 killagreg 70
int16_t RawMagnet1a, RawMagnet1b;  // raw AD-Data
71
int16_t RawMagnet2a, RawMagnet2b;
72
int16_t RawMagnet3a, RawMagnet3b;
7 hbuss 73
 
21 killagreg 74
uint16_t Led_Timer = 0;
75
 
19 killagreg 76
typedef struct
1 ingob 77
{
19 killagreg 78
        int16_t Range;
79
        int16_t Offset;
80
} Scaling_t;
1 ingob 81
 
19 killagreg 82
typedef struct
83
{
84
        Scaling_t X;
85
        Scaling_t Y;
86
        Scaling_t Z;
87
} Calibration_t;
88
 
89
Calibration_t eeCalibration EEMEM; // calibration data in EEProm
90
Calibration_t Calibration;         // calibration data in RAM
91
 
92
 
21 killagreg 93
int16_t UncalMagnetX, UncalMagnetY, UncalMagnetZ;       // sensor signal difference without Scaling
94
int16_t MagnetX, MagnetY, MagnetZ;                                      // rescaled magnetic field readings
19 killagreg 95
 
96
uint8_t PC_Connected = 0;
97
 
21 killagreg 98
int16_t Heading = -1;
19 killagreg 99
 
100
 
1 ingob 101
void CalcFields(void)
7 hbuss 102
{
19 killagreg 103
        UncalMagnetX = (1 * UncalMagnetX + (RawMagnet1a - RawMagnet1b)) / 2;
104
        UncalMagnetY = (1 * UncalMagnetY + (RawMagnet3a - RawMagnet3b)) / 2;
105
        UncalMagnetZ = (1 * UncalMagnetZ + (RawMagnet2a - RawMagnet2b)) / 2;
7 hbuss 106
 
19 killagreg 107
        MagnetX = (1024L * (int32_t)(UncalMagnetX - Calibration.X.Offset)) / (Calibration.X.Range);
108
        MagnetY = (1024L * (int32_t)(UncalMagnetY - Calibration.Y.Offset)) / (Calibration.Y.Range);
109
        MagnetZ = (1024L * (int32_t)(UncalMagnetZ - Calibration.Z.Offset)) / (Calibration.Z.Range);
1 ingob 110
}
111
 
20 killagreg 112
 
1 ingob 113
void CalcHeading(void)
114
{
19 killagreg 115
        double nick_rad, roll_rad, Hx, Hy, Cx, Cy, Cz;
116
        int16_t heading = -1;
1 ingob 117
 
21 killagreg 118
        // blink code for normal operation
119
        if(CheckDelay(Led_Timer))
120
        {
121
                LED_GRN_TOGGLE;
122
                Led_Timer = SetDelay(500);
123
        }
7 hbuss 124
 
21 killagreg 125
 
19 killagreg 126
        Cx = MagnetX;
127
        Cy = MagnetY;
128
        Cz = MagnetZ;
7 hbuss 129
 
19 killagreg 130
        if(ExternData.Orientation == 1)
131
        {
132
                Cx = MagnetX;
133
                Cy = -MagnetY;
134
                Cz = MagnetZ;
135
        }
1 ingob 136
 
20 killagreg 137
        // calculate nick and roll angle in rad
138
        nick_rad = ((double)ExternData.Attitude[NICK]) * M_PI / (double)(1800.0);
139
        roll_rad = ((double)ExternData.Attitude[ROLL]) * M_PI / (double)(1800.0);
140
        // calculate attitude correction
141
        Hx = Cx * (double)cos(nick_rad) - Cz * (double)sin(nick_rad);
142
        Hy = Cy * (double)cos(roll_rad) + Cz * (double)sin(roll_rad);
19 killagreg 143
 
20 killagreg 144
        // calculate Heading
145
        heading = (int16_t)((180.0 * atan2(Hy, Hx)) / M_PI);
146
        // atan2 returns angular range from -180 deg to 180 deg in counter clockwise notation
147
        // but the compass course is defined in a range from 0 deg to 360 deg clockwise notation.
148
        if (heading < 0) heading = -heading;
149
        else heading = 360 - heading;
19 killagreg 150
 
151
        if(abs(heading) < 361) Heading = heading;
20 killagreg 152
        else (Heading = -1);
1 ingob 153
}
154
 
155
 
7 hbuss 156
void Calibrate(void)
157
{
19 killagreg 158
        uint8_t cal;
21 killagreg 159
        static uint8_t calold = 0;
19 killagreg 160
        static int16_t Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0;
21 killagreg 161
        static uint8_t blinkcount = 0;
162
/*
19 killagreg 163
        // check both sources of communication for calibration request
164
        if(I2C_WriteCal.CalByte) cal = I2C_WriteCal.CalByte;
21 killagreg 165
        else                     cal = ExternData.CalState;*/
19 killagreg 166
 
21 killagreg 167
        cal = ExternData.CalState;
168
 
169
        // blink code for current calibration state
170
        if(cal)
171
        {
172
                if(CheckDelay(Led_Timer) || (cal != calold))
173
                {
174
                        if(blinkcount & 0x01) LED_GRN_OFF;
175
                        else LED_GRN_ON;
176
 
177
                        // end of blinkcount sequence
178
                        if( (blinkcount + 1 ) >= (2 * cal) )
179
                        {
180
                                blinkcount = 0;
181
                                Led_Timer = SetDelay(1000);
182
                        }
183
                        else
184
                        {
185
                                blinkcount++;
186
                                Led_Timer = SetDelay(170);
187
                        }
188
                }
189
        }
190
        else
191
        {
192
                LED_GRN_OFF;
193
        }
194
 
195
 
19 killagreg 196
        // calibration state machine
197
        switch(cal)
198
        {
199
                case 0: // no calibration
200
                        break;
201
 
202
                case 1: // 1st step of calibration
203
                        // initialize ranges
204
                        // used to change the orientation of the MK3MAG in the horizontal plane
205
                        Xmin =  10000;
206
                        Xmax = -10000;
207
                        Ymin =  10000;
208
                        Ymax = -10000;
209
                        Zmin =  10000;
210
                        Zmax = -10000;
211
                        break;
212
 
213
                case 2: // 2nd step of calibration
214
                        // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane
215
                        if(UncalMagnetX < Xmin) Xmin = UncalMagnetX;
216
                        if(UncalMagnetX > Xmax) Xmax = UncalMagnetX;
217
                        if(UncalMagnetY < Ymin) Ymin = UncalMagnetY;
218
                        if(UncalMagnetY > Ymax) Ymax = UncalMagnetY;
219
                        break;
220
 
221
                case 3: // 3rd step of calibration
222
                        // used to change the orietation of the MK3MAG vertical to the horizontal plane
223
                        break;
224
 
225
                case 4:
226
                        // find Min and Max of the Z-Sensor
227
                        if(UncalMagnetZ < Zmin) Zmin = UncalMagnetZ;
228
                        if(UncalMagnetZ > Zmax) Zmax = UncalMagnetZ;
229
                        break;
230
 
231
                case 5:
21 killagreg 232
                        // Save values
233
                        if(cal != calold) // avoid continously wrinting of eeprom!
19 killagreg 234
                        {
21 killagreg 235
                                Calibration.X.Range = Xmax - Xmin;
236
                                Calibration.X.Offset = (Xmin + Xmax) / 2;
237
                                Calibration.Y.Range = Ymax - Ymin;
238
                                Calibration.Y.Offset = (Ymin + Ymax) / 2;
239
                                Calibration.Z.Range = Zmax - Zmin;
240
                                Calibration.Z.Offset = (Zmin + Zmax) / 2;
241
                                if((Calibration.X.Range > 150) && (Calibration.Y.Range > 150) && (Calibration.Z.Range > 150))
242
                                {
243
                                        // indicate write process by setting the led
244
                                        LED_GRN_ON;
245
                                        eeprom_write_block(&Calibration, &eeCalibration, sizeof(Calibration_t));
246
                                        Delay_ms(2000);
247
                                        // reset led state
248
                                        LED_GRN_OFF;
249
                                        // reset  blinkcode
250
                                        blinkcount = 0;
251
                                        Led_Timer = SetDelay(1000);
252
                                }
19 killagreg 253
                        }
254
                        break;
255
 
256
                default:
257
                        break;
258
        }
21 killagreg 259
        calold = cal;
7 hbuss 260
}
1 ingob 261
 
7 hbuss 262
 
263
void SetDebugValues(void)
264
{
19 killagreg 265
        DebugOut.Analog[0] =  MagnetX;
266
        DebugOut.Analog[1] =  MagnetY;
267
        DebugOut.Analog[2] =  MagnetZ;
268
        DebugOut.Analog[3] =  UncalMagnetX;
269
        DebugOut.Analog[4] =  UncalMagnetY;
270
        DebugOut.Analog[5] =  UncalMagnetZ;
271
        DebugOut.Analog[6] =  ExternData.Attitude[NICK];
272
        DebugOut.Analog[7] =  ExternData.Attitude[ROLL];
273
        DebugOut.Analog[8] =  Calibration.X.Offset;
274
        DebugOut.Analog[9] =  Calibration.X.Range;
275
        DebugOut.Analog[10] = Calibration.Y.Offset;
276
        DebugOut.Analog[11] = Calibration.Y.Range;
277
        DebugOut.Analog[12] = Calibration.Z.Offset;
278
        DebugOut.Analog[13] = Calibration.Z.Range;
279
        DebugOut.Analog[14] = ExternData.CalState;
280
        DebugOut.Analog[15] = Heading;
281
        DebugOut.Analog[16] = ExternData.UserParam[0];
282
        DebugOut.Analog[17] = ExternData.UserParam[1];
21 killagreg 283
        DebugOut.Analog[31] = PC_Connected;
7 hbuss 284
}
285
 
286
 
1 ingob 287
int main (void)
288
{
19 killagreg 289
    Led_Init();
290
    LED_GRN_ON;
291
    TIMER0_Init();
292
    USART0_Init();
1 ingob 293
    ADC_Init();
19 killagreg 294
        I2C_Init();
1 ingob 295
 
21 killagreg 296
 
19 killagreg 297
    sei(); //Globale Interrupts Einschalten
298
 
21 killagreg 299
 
300
 
19 killagreg 301
    Debug_Timer = SetDelay(100);   // Sendeintervall
21 killagreg 302
    Led_Timer = SetDelay(100);
19 killagreg 303
 
304
        // read calibration info from eeprom
305
        eeprom_read_block(&Calibration, &eeCalibration, sizeof(Calibration_t));
306
 
7 hbuss 307
    ExternData.Orientation = 0;
308
    ExternData.CalState = 0;
309
    I2C_WriteCal.CalByte = 0;
19 killagreg 310
 
311
        // main loop
1 ingob 312
    while (1)
19 killagreg 313
    {
314
                FLIP_LOW;
315
                Delay_ms(2);
316
                RawMagnet1a = ADC_GetValue(ADC0);
317
                RawMagnet2a = -ADC_GetValue(ADC1);
318
                RawMagnet3a = ADC_GetValue(ADC7);
319
                Delay_ms(1);
7 hbuss 320
 
21 killagreg 321
 
19 killagreg 322
                FLIP_HIGH;
323
                Delay_ms(2);
324
                RawMagnet1b = ADC_GetValue(ADC0);
325
                RawMagnet2b = -ADC_GetValue(ADC1);
326
                RawMagnet3b = ADC_GetValue(ADC7);
327
                Delay_ms(1);
1 ingob 328
 
19 killagreg 329
                CalcFields();
1 ingob 330
 
21 killagreg 331
                //if(ExternData.CalState || I2C_WriteCal.CalByte) Calibrate();
332
                if(ExternData.CalState) Calibrate();
19 killagreg 333
                else CalcHeading();
334
 
335
                // check data from USART
336
        USART0_ProcessRxData();
337
 
338
        if(PC_Connected)
339
        {
340
            USART0_EnableTXD();
341
            USART0_TransmitTxData();
342
            PC_Connected--;
343
                }
344
                else
345
                {
346
                        USART0_DisableTXD();
347
                }
348
        } // while(1)
1 ingob 349
}
350