Subversion Repositories MK3Mag

Rev

Rev 19 | Rev 21 | 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
 
20 killagreg 109
 
1 ingob 110
void CalcHeading(void)
111
{
19 killagreg 112
        double nick_rad, roll_rad, Hx, Hy, Cx, Cy, Cz;
113
        int16_t heading = -1;
1 ingob 114
 
7 hbuss 115
 
19 killagreg 116
        Cx = MagnetX;
117
        Cy = MagnetY;
118
        Cz = MagnetZ;
7 hbuss 119
 
19 killagreg 120
        if(ExternData.Orientation == 1)
121
        {
122
                Cx = MagnetX;
123
                Cy = -MagnetY;
124
                Cz = MagnetZ;
125
        }
1 ingob 126
 
20 killagreg 127
        // calculate nick and roll angle in rad
128
        nick_rad = ((double)ExternData.Attitude[NICK]) * M_PI / (double)(1800.0);
129
        roll_rad = ((double)ExternData.Attitude[ROLL]) * M_PI / (double)(1800.0);
130
        // calculate attitude correction
131
        Hx = Cx * (double)cos(nick_rad) - Cz * (double)sin(nick_rad);
132
        Hy = Cy * (double)cos(roll_rad) + Cz * (double)sin(roll_rad);
19 killagreg 133
 
20 killagreg 134
        // calculate Heading
135
        heading = (int16_t)((180.0 * atan2(Hy, Hx)) / M_PI);
136
        // atan2 returns angular range from -180 deg to 180 deg in counter clockwise notation
137
        // but the compass course is defined in a range from 0 deg to 360 deg clockwise notation.
138
        if (heading < 0) heading = -heading;
139
        else heading = 360 - heading;
19 killagreg 140
 
141
        if(abs(heading) < 361) Heading = heading;
20 killagreg 142
        else (Heading = -1);
1 ingob 143
}
144
 
145
 
7 hbuss 146
void Calibrate(void)
147
{
19 killagreg 148
        uint8_t cal;
149
        static int16_t Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0;
150
 
151
        // check both sources of communication for calibration request
152
        if(I2C_WriteCal.CalByte) cal = I2C_WriteCal.CalByte;
153
        else                     cal = ExternData.CalState;
154
 
155
        // calibration state machine
156
        switch(cal)
157
        {
158
                case 0: // no calibration
159
                        LED_GRN_ON;
160
                        break;
161
 
162
                case 1: // 1st step of calibration
163
                        // initialize ranges
164
                        // used to change the orientation of the MK3MAG in the horizontal plane
165
                        LED_GRN_OFF;
166
                        Xmin =  10000;
167
                        Xmax = -10000;
168
                        Ymin =  10000;
169
                        Ymax = -10000;
170
                        Zmin =  10000;
171
                        Zmax = -10000;
172
                        break;
173
 
174
                case 2: // 2nd step of calibration
175
                        // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane
176
                        LED_GRN_ON;
177
                        if(UncalMagnetX < Xmin) Xmin = UncalMagnetX;
178
                        if(UncalMagnetX > Xmax) Xmax = UncalMagnetX;
179
                        if(UncalMagnetY < Ymin) Ymin = UncalMagnetY;
180
                        if(UncalMagnetY > Ymax) Ymax = UncalMagnetY;
181
                        break;
182
 
183
                case 3: // 3rd step of calibration
184
                        // used to change the orietation of the MK3MAG vertical to the horizontal plane
185
                        LED_GRN_OFF;
186
                        break;
187
 
188
                case 4:
189
                        // find Min and Max of the Z-Sensor
190
                        LED_GRN_ON;
191
                        if(UncalMagnetZ < Zmin) Zmin = UncalMagnetZ;
192
                        if(UncalMagnetZ > Zmax) Zmax = UncalMagnetZ;
193
                        break;
194
 
195
                case 5:
196
                        LED_GRN_OFF; // Save values
197
                        Calibration.X.Range = Xmax - Xmin;
198
                        Calibration.X.Offset = (Xmin + Xmax) / 2;
199
                        Calibration.Y.Range = Ymax - Ymin;
200
                        Calibration.Y.Offset = (Ymin + Ymax) / 2;
201
                        Calibration.Z.Range = Zmax - Zmin;
202
                        Calibration.Z.Offset = (Zmin + Zmax) / 2;
203
                        if((Calibration.X.Range > 150) && (Calibration.Y.Range > 150) && (Calibration.Z.Range > 150))
204
                        {
205
                                eeprom_write_block(&Calibration, &eeCalibration, sizeof(Calibration_t));
206
                                Delay_ms(2000);
207
                        }
208
                        LED_GRN_ON;
209
                        break;
210
 
211
                default:
212
                        LED_GRN_ON;
213
                        break;
214
        }
7 hbuss 215
}
1 ingob 216
 
7 hbuss 217
 
218
void SetDebugValues(void)
219
{
19 killagreg 220
        DebugOut.Analog[0] =  MagnetX;
221
        DebugOut.Analog[1] =  MagnetY;
222
        DebugOut.Analog[2] =  MagnetZ;
223
        DebugOut.Analog[3] =  UncalMagnetX;
224
        DebugOut.Analog[4] =  UncalMagnetY;
225
        DebugOut.Analog[5] =  UncalMagnetZ;
226
        DebugOut.Analog[6] =  ExternData.Attitude[NICK];
227
        DebugOut.Analog[7] =  ExternData.Attitude[ROLL];
228
        DebugOut.Analog[8] =  Calibration.X.Offset;
229
        DebugOut.Analog[9] =  Calibration.X.Range;
230
        DebugOut.Analog[10] = Calibration.Y.Offset;
231
        DebugOut.Analog[11] = Calibration.Y.Range;
232
        DebugOut.Analog[12] = Calibration.Z.Offset;
233
        DebugOut.Analog[13] = Calibration.Z.Range;
234
        DebugOut.Analog[14] = ExternData.CalState;
235
        DebugOut.Analog[15] = Heading;
236
        DebugOut.Analog[16] = ExternData.UserParam[0];
237
        DebugOut.Analog[17] = ExternData.UserParam[1];
7 hbuss 238
}
239
 
240
 
1 ingob 241
int main (void)
242
{
19 killagreg 243
    Led_Init();
244
    LED_GRN_ON;
245
    TIMER0_Init();
246
    USART0_Init();
1 ingob 247
    ADC_Init();
19 killagreg 248
        I2C_Init();
1 ingob 249
 
19 killagreg 250
    sei(); //Globale Interrupts Einschalten
251
 
252
    Debug_Timer = SetDelay(100);   // Sendeintervall
253
 
254
        // read calibration info from eeprom
255
        eeprom_read_block(&Calibration, &eeCalibration, sizeof(Calibration_t));
256
 
7 hbuss 257
    ExternData.Orientation = 0;
258
    ExternData.CalState = 0;
259
    I2C_WriteCal.CalByte = 0;
19 killagreg 260
 
261
        // main loop
1 ingob 262
    while (1)
19 killagreg 263
    {
264
                FLIP_LOW;
265
                Delay_ms(2);
266
                RawMagnet1a = ADC_GetValue(ADC0);
267
                RawMagnet2a = -ADC_GetValue(ADC1);
268
                RawMagnet3a = ADC_GetValue(ADC7);
269
                Delay_ms(1);
7 hbuss 270
 
19 killagreg 271
                FLIP_HIGH;
272
                Delay_ms(2);
273
                RawMagnet1b = ADC_GetValue(ADC0);
274
                RawMagnet2b = -ADC_GetValue(ADC1);
275
                RawMagnet3b = ADC_GetValue(ADC7);
276
                Delay_ms(1);
1 ingob 277
 
19 killagreg 278
                CalcFields();
1 ingob 279
 
19 killagreg 280
                if(ExternData.CalState || I2C_WriteCal.CalByte) Calibrate();
281
                else CalcHeading();
282
 
283
                // check data from USART
284
        USART0_ProcessRxData();
285
 
286
        if(PC_Connected)
287
        {
288
            USART0_EnableTXD();
289
            USART0_TransmitTxData();
290
            PC_Connected--;
291
                }
292
                else
293
                {
294
                        USART0_DisableTXD();
295
                }
296
        } // while(1)
1 ingob 297
}
298