Subversion Repositories MK3Mag

Rev

Rev 21 | Rev 23 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 21 Rev 22
1
/*#######################################################################################
1
/*#######################################################################################
2
MK3Mag 3D-Magnet sensor
2
MK3Mag 3D-Magnet sensor
3
!!! THIS IS NOT FREE SOFTWARE !!!
3
!!! THIS IS NOT FREE SOFTWARE !!!
4
#######################################################################################*/
4
#######################################################################################*/
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6
// + Copyright (c) 05.2008 Holger Buss
6
// + Copyright (c) 05.2008 Holger Buss
7
// + Thanks to Ilja Fähnrich (P_Latzhalter)
7
// + Thanks to Ilja Fähnrich (P_Latzhalter)
8
// + Nur für den privaten Gebrauch
8
// + Nur für den privaten Gebrauch
9
// + www.MikroKopter.com
9
// + www.MikroKopter.com
10
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
11
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
11
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
12
// + mit unserer Zustimmung zulässig
12
// + mit unserer Zustimmung zulässig
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
14
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
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.
15
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
16
// + AUSNAHME: Ein bei www.mikrokopter.de erworbener vorbestückter MK3Mag darf als Baugruppe auch in kommerziellen Systemen verbaut werden
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
17
// + Im Zweifelsfall bitte anfragen bei: info@mikrokopter.de
18
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
18
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
19
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
20
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
20
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
21
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
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"
23
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
24
// + eindeutig als Ursprung verlinkt werden
24
// + eindeutig als Ursprung verlinkt werden
25
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
25
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
26
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
26
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
27
// + Benutzung auf eigene Gefahr
27
// + Benutzung auf eigene Gefahr
28
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
28
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
29
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
30
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
32
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
33
// + this list of conditions and the following disclaimer.
33
// + this list of conditions and the following disclaimer.
34
// +   * PORTING this software (or parts of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed
34
// +   * PORTING this software (or parts of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed
35
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
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.
36
// +     from this software without specific prior written permission.
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
38
// +     for non-commercial use (directly or indirectly)
38
// +     for non-commercial use (directly or indirectly)
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
40
// +     with our written permission
40
// +     with our written permission
41
// +     Exception: A preassembled MK3Mag, purchased from www.mikrokopter.de may be used as a part of commercial systems
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
42
// +     In case of doubt please contact: info@MikroKopter.de
43
// +   * If sources or documentations are redistributet on other webpages, our webpage (http://www.MikroKopter.de) must be
43
// +   * If sources or documentations are redistributet on other webpages, our webpage (http://www.MikroKopter.de) must be
44
// +     clearly linked as origin
44
// +     clearly linked as origin
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
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
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55
// +  POSSIBILITY OF SUCH DAMAGE.
55
// +  POSSIBILITY OF SUCH DAMAGE.
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57
#include <avr/interrupt.h>
57
#include <avr/interrupt.h>
58
#include <math.h>
58
#include <math.h>
59
#include <stdlib.h>
59
#include <stdlib.h>
60
#include <stdio.h>
60
#include <stdio.h>
61
 
61
 
62
#include "main.h"
62
#include "main.h"
63
#include "timer0.h"
63
#include "timer0.h"
64
#include "twislave.h"
64
#include "twislave.h"
65
#include "led.h"
65
#include "led.h"
66
#include "analog.h"
66
#include "analog.h"
67
#include "uart.h"
67
#include "uart.h"
68
 
68
 
69
 
69
 
70
int16_t RawMagnet1a, RawMagnet1b;  // raw AD-Data
70
int16_t RawMagnet1a, RawMagnet1b;  // raw AD-Data
71
int16_t RawMagnet2a, RawMagnet2b;
71
int16_t RawMagnet2a, RawMagnet2b;
72
int16_t RawMagnet3a, RawMagnet3b;
72
int16_t RawMagnet3a, RawMagnet3b;
73
 
73
 
74
uint16_t Led_Timer = 0;
74
uint16_t Led_Timer = 0;
75
 
75
 
76
typedef struct
76
typedef struct
77
{
77
{
78
        int16_t Range;
78
        int16_t Range;
79
        int16_t Offset;
79
        int16_t Offset;
80
} Scaling_t;
80
} Scaling_t;
81
 
81
 
82
typedef struct
82
typedef struct
83
{
83
{
84
        Scaling_t X;
84
        Scaling_t X;
85
        Scaling_t Y;
85
        Scaling_t Y;
86
        Scaling_t Z;
86
        Scaling_t Z;
87
} Calibration_t;
87
} Calibration_t;
88
 
88
 
89
Calibration_t eeCalibration EEMEM; // calibration data in EEProm
89
Calibration_t eeCalibration EEMEM; // calibration data in EEProm
90
Calibration_t Calibration;         // calibration data in RAM
90
Calibration_t Calibration;         // calibration data in RAM
91
 
91
 
92
 
92
 
93
int16_t UncalMagnetX, UncalMagnetY, UncalMagnetZ;       // sensor signal difference without Scaling
93
int16_t UncalMagnetX, UncalMagnetY, UncalMagnetZ;       // sensor signal difference without Scaling
94
int16_t MagnetX, MagnetY, MagnetZ;                                      // rescaled magnetic field readings
94
int16_t MagnetX, MagnetY, MagnetZ;                      // rescaled magnetic field readings
95
 
95
 
96
uint8_t PC_Connected = 0;
96
uint8_t PC_Connected = 0;
97
 
97
 
98
int16_t Heading = -1;
98
int16_t Heading = -1;
99
 
99
 
100
 
100
 
101
void CalcFields(void)
101
void CalcFields(void)
102
{
102
{
103
        UncalMagnetX = (1 * UncalMagnetX + (RawMagnet1a - RawMagnet1b)) / 2;
103
        UncalMagnetX = (1 * UncalMagnetX + (RawMagnet1a - RawMagnet1b)) / 2;
104
        UncalMagnetY = (1 * UncalMagnetY + (RawMagnet3a - RawMagnet3b)) / 2;
104
        UncalMagnetY = (1 * UncalMagnetY + (RawMagnet3a - RawMagnet3b)) / 2;
105
        UncalMagnetZ = (1 * UncalMagnetZ + (RawMagnet2a - RawMagnet2b)) / 2;
105
        UncalMagnetZ = (1 * UncalMagnetZ + (RawMagnet2a - RawMagnet2b)) / 2;
106
 
106
 
-
 
107
        if(Calibration.X.Range != 0) MagnetX = (1024L * (int32_t)(UncalMagnetX - Calibration.X.Offset)) / (Calibration.X.Range);
107
        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);
108
        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);
109
        MagnetZ = (1024L * (int32_t)(UncalMagnetZ - Calibration.Z.Offset)) / (Calibration.Z.Range);
112
        else MagnetZ = 0;
110
}
113
}
111
 
114
 
112
 
115
 
113
void CalcHeading(void)
116
void CalcHeading(void)
114
{
117
{
115
        double nick_rad, roll_rad, Hx, Hy, Cx, Cy, Cz;
118
        double nick_rad, roll_rad, Hx, Hy, Cx, Cy, Cz;
116
        int16_t heading = -1;
119
        int16_t heading = -1;
117
 
120
 
118
        // blink code for normal operation
121
        // blink code for normal operation
119
        if(CheckDelay(Led_Timer))
122
        if(CheckDelay(Led_Timer))
120
        {
123
        {
121
                LED_GRN_TOGGLE;
124
                LED_GRN_TOGGLE;
122
                Led_Timer = SetDelay(500);
125
                Led_Timer = SetDelay(500);
123
        }
126
        }
124
 
127
 
125
 
128
 
126
        Cx = MagnetX;
129
        Cx = MagnetX;
127
        Cy = MagnetY;
130
        Cy = MagnetY;
128
        Cz = MagnetZ;
131
        Cz = MagnetZ;
129
 
132
 
130
        if(ExternData.Orientation == 1)
133
        if(ExternData.Orientation == 1)
131
        {
134
        {
132
                Cx = MagnetX;
135
                Cx = MagnetX;
133
                Cy = -MagnetY;
136
                Cy = -MagnetY;
134
                Cz = MagnetZ;
137
                Cz = MagnetZ;
135
        }
138
        }
136
 
139
 
137
        // calculate nick and roll angle in rad
140
        // calculate nick and roll angle in rad
138
        nick_rad = ((double)ExternData.Attitude[NICK]) * M_PI / (double)(1800.0);
141
        nick_rad = ((double)ExternData.Attitude[NICK]) * M_PI / (double)(1800.0);
139
        roll_rad = ((double)ExternData.Attitude[ROLL]) * M_PI / (double)(1800.0);
142
        roll_rad = ((double)ExternData.Attitude[ROLL]) * M_PI / (double)(1800.0);
140
        // calculate attitude correction
143
        // calculate attitude correction
141
        Hx = Cx * (double)cos(nick_rad) - Cz * (double)sin(nick_rad);
144
        Hx = Cx * (double)cos(nick_rad) - Cz * (double)sin(nick_rad);
142
        Hy = Cy * (double)cos(roll_rad) + Cz * (double)sin(roll_rad);
145
        Hy = Cy * (double)cos(roll_rad) + Cz * (double)sin(roll_rad);
143
 
146
 
144
        // calculate Heading
147
        // calculate Heading
145
        heading = (int16_t)((180.0 * atan2(Hy, Hx)) / M_PI);
148
        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
149
        // 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.
150
        // but the compass course is defined in a range from 0 deg to 360 deg clockwise notation.
148
        if (heading < 0) heading = -heading;
151
        if (heading < 0) heading = -heading;
149
        else heading = 360 - heading;
152
        else heading = 360 - heading;
150
 
153
 
151
        if(abs(heading) < 361) Heading = heading;
154
        if(abs(heading) < 361) Heading = heading;
152
        else (Heading = -1);
155
        else (Heading = -1);
153
}
156
}
154
 
157
 
155
 
158
 
156
void Calibrate(void)
159
void Calibrate(void)
157
{
160
{
158
        uint8_t cal;
161
        uint8_t cal;
159
        static uint8_t calold = 0;
162
        static uint8_t calold = 0;
160
        static int16_t Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0;
163
        static int16_t Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0;
161
        static uint8_t blinkcount = 0;
164
        static uint8_t blinkcount = 0;
162
/*
165
 
163
        // check both sources of communication for calibration request
166
        // check both sources of communication for calibration request
164
        if(I2C_WriteCal.CalByte) cal = I2C_WriteCal.CalByte;
167
        if(I2C_WriteCal.CalByte) cal = I2C_WriteCal.CalByte;
165
        else                     cal = ExternData.CalState;*/
168
        else                     cal = ExternData.CalState;
166
 
-
 
167
        cal = ExternData.CalState;
-
 
-
 
169
 
168
 
170
        DebugOut.Analog[30] = cal;
169
        // blink code for current calibration state
171
        // blink code for current calibration state
170
        if(cal)
172
        if(cal)
171
        {
173
        {
172
                if(CheckDelay(Led_Timer) || (cal != calold))
174
                if(CheckDelay(Led_Timer) || (cal != calold))
173
                {
175
                {
174
                        if(blinkcount & 0x01) LED_GRN_OFF;
176
                        if(blinkcount & 0x01) LED_GRN_OFF;
175
                        else LED_GRN_ON;
177
                        else LED_GRN_ON;
176
 
178
 
177
                        // end of blinkcount sequence
179
                        // end of blinkcount sequence
178
                        if( (blinkcount + 1 ) >= (2 * cal) )
180
                        if( (blinkcount + 1 ) >= (2 * cal) )
179
                        {
181
                        {
180
                                blinkcount = 0;
182
                                blinkcount = 0;
181
                                Led_Timer = SetDelay(1000);
183
                                Led_Timer = SetDelay(1000);
182
                        }
184
                        }
183
                        else
185
                        else
184
                        {
186
                        {
185
                                blinkcount++;
187
                                blinkcount++;
186
                                Led_Timer = SetDelay(170);
188
                                Led_Timer = SetDelay(170);
187
                        }
189
                        }
188
                }
190
                }
189
        }
191
        }
190
        else
192
        else
191
        {
193
        {
192
                LED_GRN_OFF;
194
                LED_GRN_OFF;
193
        }
195
        }
194
 
196
 
195
 
197
 
196
        // calibration state machine
198
        // calibration state machine
197
        switch(cal)
199
        switch(cal)
198
        {
200
        {
199
                case 0: // no calibration
201
                case 0: // no calibration
200
                        break;
202
                        break;
201
 
203
 
202
                case 1: // 1st step of calibration
204
                case 1: // 1st step of calibration
203
                        // initialize ranges
205
                        // initialize ranges
204
                        // used to change the orientation of the MK3MAG in the horizontal plane
206
                        // used to change the orientation of the MK3MAG in the horizontal plane
205
                        Xmin =  10000;
207
                        Xmin =  10000;
206
                        Xmax = -10000;
208
                        Xmax = -10000;
207
                        Ymin =  10000;
209
                        Ymin =  10000;
208
                        Ymax = -10000;
210
                        Ymax = -10000;
209
                        Zmin =  10000;
211
                        Zmin =  10000;
210
                        Zmax = -10000;
212
                        Zmax = -10000;
211
                        break;
213
                        break;
212
 
214
 
213
                case 2: // 2nd step of calibration
215
                case 2: // 2nd step of calibration
214
                        // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane
216
                        // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane
215
                        if(UncalMagnetX < Xmin) Xmin = UncalMagnetX;
217
                        if(UncalMagnetX < Xmin) Xmin = UncalMagnetX;
216
                        if(UncalMagnetX > Xmax) Xmax = UncalMagnetX;
218
                        if(UncalMagnetX > Xmax) Xmax = UncalMagnetX;
217
                        if(UncalMagnetY < Ymin) Ymin = UncalMagnetY;
219
                        if(UncalMagnetY < Ymin) Ymin = UncalMagnetY;
218
                        if(UncalMagnetY > Ymax) Ymax = UncalMagnetY;
220
                        if(UncalMagnetY > Ymax) Ymax = UncalMagnetY;
219
                        break;
221
                        break;
220
 
222
 
221
                case 3: // 3rd step of calibration
223
                case 3: // 3rd step of calibration
222
                        // used to change the orietation of the MK3MAG vertical to the horizontal plane
224
                        // used to change the orietation of the MK3MAG vertical to the horizontal plane
223
                        break;
225
                        break;
224
 
226
 
225
                case 4:
227
                case 4:
226
                        // find Min and Max of the Z-Sensor
228
                        // find Min and Max of the Z-Sensor
227
                        if(UncalMagnetZ < Zmin) Zmin = UncalMagnetZ;
229
                        if(UncalMagnetZ < Zmin) Zmin = UncalMagnetZ;
228
                        if(UncalMagnetZ > Zmax) Zmax = UncalMagnetZ;
230
                        if(UncalMagnetZ > Zmax) Zmax = UncalMagnetZ;
229
                        break;
231
                        break;
230
 
232
 
231
                case 5:
233
                case 5:
232
                        // Save values
234
                        // Save values
233
                        if(cal != calold) // avoid continously wrinting of eeprom!
235
                        if(cal != calold) // avoid continously wrinting of eeprom!
234
                        {
236
                        {
235
                                Calibration.X.Range = Xmax - Xmin;
237
                                Calibration.X.Range = Xmax - Xmin;
236
                                Calibration.X.Offset = (Xmin + Xmax) / 2;
238
                                Calibration.X.Offset = (Xmin + Xmax) / 2;
237
                                Calibration.Y.Range = Ymax - Ymin;
239
                                Calibration.Y.Range = Ymax - Ymin;
238
                                Calibration.Y.Offset = (Ymin + Ymax) / 2;
240
                                Calibration.Y.Offset = (Ymin + Ymax) / 2;
239
                                Calibration.Z.Range = Zmax - Zmin;
241
                                Calibration.Z.Range = Zmax - Zmin;
240
                                Calibration.Z.Offset = (Zmin + Zmax) / 2;
242
                                Calibration.Z.Offset = (Zmin + Zmax) / 2;
241
                                if((Calibration.X.Range > 150) && (Calibration.Y.Range > 150) && (Calibration.Z.Range > 150))
243
                                if((Calibration.X.Range > 150) && (Calibration.Y.Range > 150) && (Calibration.Z.Range > 150))
242
                                {
244
                                {
243
                                        // indicate write process by setting the led
245
                                        // indicate write process by setting the led
244
                                        LED_GRN_ON;
246
                                        LED_GRN_ON;
245
                                        eeprom_write_block(&Calibration, &eeCalibration, sizeof(Calibration_t));
247
                                        eeprom_write_block(&Calibration, &eeCalibration, sizeof(Calibration_t));
246
                                        Delay_ms(2000);
248
                                        Delay_ms(2000);
247
                                        // reset led state
249
                                        // reset led state
248
                                        LED_GRN_OFF;
250
                                        LED_GRN_OFF;
249
                                        // reset  blinkcode
251
                                        // reset  blinkcode
250
                                        blinkcount = 0;
252
                                        blinkcount = 0;
251
                                        Led_Timer = SetDelay(1000);
253
                                        Led_Timer = SetDelay(1000);
252
                                }
254
                                }
253
                        }
255
                        }
254
                        break;
256
                        break;
255
 
257
 
256
                default:
258
                default:
257
                        break;
259
                        break;
258
        }
260
        }
259
        calold = cal;
261
        calold = cal;
260
}
262
}
261
 
263
 
262
 
264
 
263
void SetDebugValues(void)
265
void SetDebugValues(void)
264
{
266
{
265
        DebugOut.Analog[0] =  MagnetX;
267
        DebugOut.Analog[0] =  MagnetX;
266
        DebugOut.Analog[1] =  MagnetY;
268
        DebugOut.Analog[1] =  MagnetY;
267
        DebugOut.Analog[2] =  MagnetZ;
269
        DebugOut.Analog[2] =  MagnetZ;
268
        DebugOut.Analog[3] =  UncalMagnetX;
270
        DebugOut.Analog[3] =  UncalMagnetX;
269
        DebugOut.Analog[4] =  UncalMagnetY;
271
        DebugOut.Analog[4] =  UncalMagnetY;
270
        DebugOut.Analog[5] =  UncalMagnetZ;
272
        DebugOut.Analog[5] =  UncalMagnetZ;
271
        DebugOut.Analog[6] =  ExternData.Attitude[NICK];
273
        DebugOut.Analog[6] =  ExternData.Attitude[NICK];
272
        DebugOut.Analog[7] =  ExternData.Attitude[ROLL];
274
        DebugOut.Analog[7] =  ExternData.Attitude[ROLL];
273
        DebugOut.Analog[8] =  Calibration.X.Offset;
275
        DebugOut.Analog[8] =  Calibration.X.Offset;
274
        DebugOut.Analog[9] =  Calibration.X.Range;
276
        DebugOut.Analog[9] =  Calibration.X.Range;
275
        DebugOut.Analog[10] = Calibration.Y.Offset;
277
        DebugOut.Analog[10] = Calibration.Y.Offset;
276
        DebugOut.Analog[11] = Calibration.Y.Range;
278
        DebugOut.Analog[11] = Calibration.Y.Range;
277
        DebugOut.Analog[12] = Calibration.Z.Offset;
279
        DebugOut.Analog[12] = Calibration.Z.Offset;
278
        DebugOut.Analog[13] = Calibration.Z.Range;
280
        DebugOut.Analog[13] = Calibration.Z.Range;
279
        DebugOut.Analog[14] = ExternData.CalState;
281
        DebugOut.Analog[14] = ExternData.CalState;
280
        DebugOut.Analog[15] = Heading;
282
        DebugOut.Analog[15] = Heading;
281
        DebugOut.Analog[16] = ExternData.UserParam[0];
283
        DebugOut.Analog[16] = ExternData.UserParam[0];
282
        DebugOut.Analog[17] = ExternData.UserParam[1];
284
        DebugOut.Analog[17] = ExternData.UserParam[1];
-
 
285
        DebugOut.Analog[30] = I2C_WriteCal.CalByte;
283
        DebugOut.Analog[31] = PC_Connected;
286
        DebugOut.Analog[31] = PC_Connected;
284
}
287
}
285
 
288
 
286
 
289
 
287
int main (void)
290
int main (void)
288
{
291
{
-
 
292
        // reset input pullup
-
 
293
        DDRC &=~((1<<DDC6));
289
    Led_Init();
294
        PORTC |= (1<<PORTC6);
-
 
295
 
290
    LED_GRN_ON;
296
    LED_Init();
291
    TIMER0_Init();
297
    TIMER0_Init();
292
    USART0_Init();
298
    USART0_Init();
293
    ADC_Init();
299
    ADC_Init();
294
        I2C_Init();
300
        I2C_Init();
-
 
301
 
295
 
302
    sei(); // enable globale interrupts
296
 
-
 
297
    sei(); //Globale Interrupts Einschalten
-
 
298
 
303
 
299
 
304
    LED_GRN_ON;
300
 
305
 
301
    Debug_Timer = SetDelay(100);   // Sendeintervall
306
    Debug_Timer = SetDelay(200);
302
    Led_Timer = SetDelay(100);
307
    Led_Timer = SetDelay(200);
303
 
308
 
304
        // read calibration info from eeprom
309
        // read calibration info from eeprom
305
        eeprom_read_block(&Calibration, &eeCalibration, sizeof(Calibration_t));
310
        eeprom_read_block(&Calibration, &eeCalibration, sizeof(Calibration_t));
306
 
311
 
307
    ExternData.Orientation = 0;
312
    ExternData.Orientation = 0;
308
    ExternData.CalState = 0;
313
    ExternData.CalState = 0;
309
    I2C_WriteCal.CalByte = 0;
314
    I2C_WriteCal.CalByte = 0;
-
 
315
 
-
 
316
        Heading = 101;
310
 
317
 
311
        // main loop
318
        // main loop
312
    while (1)
319
    while (1)
313
    {
320
    {
-
 
321
 
-
 
322
 
314
                FLIP_LOW;
323
                FLIP_LOW;
315
                Delay_ms(2);
324
                Delay_ms(2);
316
                RawMagnet1a = ADC_GetValue(ADC0);
325
                RawMagnet1a = ADC_GetValue(ADC0);
317
                RawMagnet2a = -ADC_GetValue(ADC1);
326
                RawMagnet2a = -ADC_GetValue(ADC1);
318
                RawMagnet3a = ADC_GetValue(ADC7);
327
                RawMagnet3a = ADC_GetValue(ADC7);
319
                Delay_ms(1);
328
                Delay_ms(1);
320
 
329
 
321
 
330
 
322
                FLIP_HIGH;
331
                FLIP_HIGH;
323
                Delay_ms(2);
332
                Delay_ms(2);
324
                RawMagnet1b = ADC_GetValue(ADC0);
333
                RawMagnet1b = ADC_GetValue(ADC0);
325
                RawMagnet2b = -ADC_GetValue(ADC1);
334
                RawMagnet2b = -ADC_GetValue(ADC1);
326
                RawMagnet3b = ADC_GetValue(ADC7);
335
                RawMagnet3b = ADC_GetValue(ADC7);
327
                Delay_ms(1);
336
                Delay_ms(1);
328
 
337
 
329
                CalcFields();
338
                CalcFields();
330
 
339
 
331
                //if(ExternData.CalState || I2C_WriteCal.CalByte) Calibrate();
-
 
332
                if(ExternData.CalState) Calibrate();
340
                if(ExternData.CalState || I2C_WriteCal.CalByte) Calibrate();
333
                else CalcHeading();
341
                else CalcHeading();
334
 
342
 
335
                // check data from USART
343
                // check data from USART
336
        USART0_ProcessRxData();
344
        USART0_ProcessRxData();
337
 
345
 
338
        if(PC_Connected)
346
        if(PC_Connected)
339
        {
347
        {
340
            USART0_EnableTXD();
348
            USART0_EnableTXD();
341
            USART0_TransmitTxData();
349
            USART0_TransmitTxData();
342
            PC_Connected--;
350
            PC_Connected--;
343
                }
351
                }
344
                else
352
                else
345
                {
353
                {
346
                        USART0_DisableTXD();
354
                        USART0_DisableTXD();
347
                }
355
                }
348
        } // while(1)
356
        } // while(1)
349
}
357
}
350
 
358
 
351
 
359