Subversion Repositories MK3Mag

Rev

Rev 18 | Rev 20 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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