Subversion Repositories MK3Mag

Rev

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

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