Subversion Repositories MK3Mag

Rev

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