Subversion Repositories MK3Mag

Rev

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

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