Subversion Repositories MK3Mag

Rev

Rev 18 | Rev 20 | 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>
5 hbuss 60
 
1 ingob 61
#include "main.h"
19 killagreg 62
#include "timer0.h"
63
#include "twislave.h"
64
#include "led.h"
65
#include "analog.h"
66
#include "uart.h"
1 ingob 67
 
68
 
19 killagreg 69
int16_t RawMagnet1a, RawMagnet1b;  // raw AD-Data
70
int16_t RawMagnet2a, RawMagnet2b;
71
int16_t RawMagnet3a, RawMagnet3b;
7 hbuss 72
 
19 killagreg 73
typedef struct
1 ingob 74
{
19 killagreg 75
        int16_t Range;
76
        int16_t Offset;
77
} Scaling_t;
1 ingob 78
 
19 killagreg 79
typedef struct
80
{
81
        Scaling_t X;
82
        Scaling_t Y;
83
        Scaling_t Z;
84
} Calibration_t;
85
 
86
Calibration_t eeCalibration EEMEM; // calibration data in EEProm
87
Calibration_t Calibration;         // calibration data in RAM
88
 
89
 
90
int16_t UncalMagnetX, UncalMagnetY, UncalMagnetZ;  // sensor signal difference without Scaling
91
int16_t MagnetX, MagnetY, MagnetZ;                               // rescaled magnetic field readings
92
 
93
uint8_t PC_Connected = 0;
94
 
95
int16_t Heading;
96
 
97
 
1 ingob 98
void CalcFields(void)
7 hbuss 99
{
19 killagreg 100
        UncalMagnetX = (1 * UncalMagnetX + (RawMagnet1a - RawMagnet1b)) / 2;
101
        UncalMagnetY = (1 * UncalMagnetY + (RawMagnet3a - RawMagnet3b)) / 2;
102
        UncalMagnetZ = (1 * UncalMagnetZ + (RawMagnet2a - RawMagnet2b)) / 2;
7 hbuss 103
 
19 killagreg 104
        MagnetX = (1024L * (int32_t)(UncalMagnetX - Calibration.X.Offset)) / (Calibration.X.Range);
105
        MagnetY = (1024L * (int32_t)(UncalMagnetY - Calibration.Y.Offset)) / (Calibration.Y.Range);
106
        MagnetZ = (1024L * (int32_t)(UncalMagnetZ - Calibration.Z.Offset)) / (Calibration.Z.Range);
1 ingob 107
}
108
 
109
void CalcHeading(void)
110
{
19 killagreg 111
        double nick_rad, roll_rad, Hx, Hy, Cx, Cy, Cz;
112
        int16_t heading = -1;
1 ingob 113
 
19 killagreg 114
        // calculate nick and roll angle i rad
115
        nick_rad = ((double)ExternData.Attitude[NICK]) * M_PI / (double)(1800);
116
        roll_rad = ((double)ExternData.Attitude[ROLL]) * M_PI / (double)(1800);
7 hbuss 117
 
19 killagreg 118
        Cx = MagnetX;
119
        Cy = MagnetY;
120
        Cz = MagnetZ;
7 hbuss 121
 
19 killagreg 122
        if(ExternData.Orientation == 1)
123
        {
124
                Cx = MagnetX;
125
                Cy = -MagnetY;
126
                Cz = MagnetZ;
127
        }
1 ingob 128
 
19 killagreg 129
        Hx = Cx * (double)cos(nick_rad) +
130
                Cy * (double)sin(nick_rad) * (double)sin(roll_rad) -
131
                Cz * (double)sin(nick_rad) * (double)cos(roll_rad);
132
 
133
        Hy = Cy * (double)cos(roll_rad) +
134
                Cz * (double)sin(roll_rad);
135
 
136
 
137
        if(Hx == 0 && Hy < 0) heading = 90;
138
        else if(Hx == 0 && Hy > 0) heading = 270;
139
        else if(Hx < 0) heading  = 180 - (atan(Hy / Hx) * 180.0) / M_PI;
140
        else if(Hx > 0 && Hy < 0) heading = - (atan(Hy / Hx) * 180.0) / M_PI;
141
        else if(Hx > 0 && Hy > 0) heading  = 360 - (atan(Hy / Hx) * 180.0) / M_PI;
142
 
143
        if(abs(heading) < 361) Heading = heading;
144
 
1 ingob 145
}
146
 
147
 
7 hbuss 148
void Calibrate(void)
149
{
19 killagreg 150
        uint8_t cal;
151
        static int16_t Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0;
152
 
153
        // check both sources of communication for calibration request
154
        if(I2C_WriteCal.CalByte) cal = I2C_WriteCal.CalByte;
155
        else                     cal = ExternData.CalState;
156
 
157
        // calibration state machine
158
        switch(cal)
159
        {
160
                case 0: // no calibration
161
                        LED_GRN_ON;
162
                        break;
163
 
164
                case 1: // 1st step of calibration
165
                        // initialize ranges
166
                        // used to change the orientation of the MK3MAG in the horizontal plane
167
                        LED_GRN_OFF;
168
                        Xmin =  10000;
169
                        Xmax = -10000;
170
                        Ymin =  10000;
171
                        Ymax = -10000;
172
                        Zmin =  10000;
173
                        Zmax = -10000;
174
                        break;
175
 
176
                case 2: // 2nd step of calibration
177
                        // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane
178
                        LED_GRN_ON;
179
                        if(UncalMagnetX < Xmin) Xmin = UncalMagnetX;
180
                        if(UncalMagnetX > Xmax) Xmax = UncalMagnetX;
181
                        if(UncalMagnetY < Ymin) Ymin = UncalMagnetY;
182
                        if(UncalMagnetY > Ymax) Ymax = UncalMagnetY;
183
                        break;
184
 
185
                case 3: // 3rd step of calibration
186
                        // used to change the orietation of the MK3MAG vertical to the horizontal plane
187
                        LED_GRN_OFF;
188
                        break;
189
 
190
                case 4:
191
                        // find Min and Max of the Z-Sensor
192
                        LED_GRN_ON;
193
                        if(UncalMagnetZ < Zmin) Zmin = UncalMagnetZ;
194
                        if(UncalMagnetZ > Zmax) Zmax = UncalMagnetZ;
195
                        break;
196
 
197
                case 5:
198
                        LED_GRN_OFF; // Save values
199
                        Calibration.X.Range = Xmax - Xmin;
200
                        Calibration.X.Offset = (Xmin + Xmax) / 2;
201
                        Calibration.Y.Range = Ymax - Ymin;
202
                        Calibration.Y.Offset = (Ymin + Ymax) / 2;
203
                        Calibration.Z.Range = Zmax - Zmin;
204
                        Calibration.Z.Offset = (Zmin + Zmax) / 2;
205
                        if((Calibration.X.Range > 150) && (Calibration.Y.Range > 150) && (Calibration.Z.Range > 150))
206
                        {
207
                                eeprom_write_block(&Calibration, &eeCalibration, sizeof(Calibration_t));
208
                                Delay_ms(2000);
209
                        }
210
                        LED_GRN_ON;
211
                        break;
212
 
213
                default:
214
                        LED_GRN_ON;
215
                        break;
216
        }
7 hbuss 217
}
1 ingob 218
 
7 hbuss 219
 
220
void SetDebugValues(void)
221
{
19 killagreg 222
        DebugOut.Analog[0] =  MagnetX;
223
        DebugOut.Analog[1] =  MagnetY;
224
        DebugOut.Analog[2] =  MagnetZ;
225
        DebugOut.Analog[3] =  UncalMagnetX;
226
        DebugOut.Analog[4] =  UncalMagnetY;
227
        DebugOut.Analog[5] =  UncalMagnetZ;
228
        DebugOut.Analog[6] =  ExternData.Attitude[NICK];
229
        DebugOut.Analog[7] =  ExternData.Attitude[ROLL];
230
        DebugOut.Analog[8] =  Calibration.X.Offset;
231
        DebugOut.Analog[9] =  Calibration.X.Range;
232
        DebugOut.Analog[10] = Calibration.Y.Offset;
233
        DebugOut.Analog[11] = Calibration.Y.Range;
234
        DebugOut.Analog[12] = Calibration.Z.Offset;
235
        DebugOut.Analog[13] = Calibration.Z.Range;
236
        DebugOut.Analog[14] = ExternData.CalState;
237
        DebugOut.Analog[15] = Heading;
238
        DebugOut.Analog[16] = ExternData.UserParam[0];
239
        DebugOut.Analog[17] = ExternData.UserParam[1];
7 hbuss 240
}
241
 
242
 
1 ingob 243
int main (void)
244
{
19 killagreg 245
    Led_Init();
246
    LED_GRN_ON;
247
    TIMER0_Init();
248
    USART0_Init();
1 ingob 249
    ADC_Init();
19 killagreg 250
        I2C_Init();
1 ingob 251
 
19 killagreg 252
    sei(); //Globale Interrupts Einschalten
253
 
254
    Debug_Timer = SetDelay(100);   // Sendeintervall
255
 
256
        // read calibration info from eeprom
257
        eeprom_read_block(&Calibration, &eeCalibration, sizeof(Calibration_t));
258
 
7 hbuss 259
    ExternData.Orientation = 0;
260
    ExternData.CalState = 0;
261
    I2C_WriteCal.CalByte = 0;
19 killagreg 262
 
263
        // main loop
1 ingob 264
    while (1)
19 killagreg 265
    {
266
                FLIP_LOW;
267
                Delay_ms(2);
268
                RawMagnet1a = ADC_GetValue(ADC0);
269
                RawMagnet2a = -ADC_GetValue(ADC1);
270
                RawMagnet3a = ADC_GetValue(ADC7);
271
                Delay_ms(1);
7 hbuss 272
 
19 killagreg 273
                FLIP_HIGH;
274
                Delay_ms(2);
275
                RawMagnet1b = ADC_GetValue(ADC0);
276
                RawMagnet2b = -ADC_GetValue(ADC1);
277
                RawMagnet3b = ADC_GetValue(ADC7);
278
                Delay_ms(1);
1 ingob 279
 
19 killagreg 280
                CalcFields();
1 ingob 281
 
19 killagreg 282
                if(ExternData.CalState || I2C_WriteCal.CalByte) Calibrate();
283
                else CalcHeading();
284
 
285
                // check data from USART
286
        USART0_ProcessRxData();
287
 
288
        if(PC_Connected)
289
        {
290
            USART0_EnableTXD();
291
            USART0_TransmitTxData();
292
            PC_Connected--;
293
                }
294
                else
295
                {
296
                        USART0_DisableTXD();
297
                }
298
        } // while(1)
1 ingob 299
}
300