Subversion Repositories MK3Mag

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ingob 1
 signed int OffsetN, OffsetR, OffsetZ;
2
/*
3
 signed int CalTabelleHorizontalN[4] = {67,-450,77,-411};    // Vier Messwerte 90° Horizontal gedreht
4
 signed int CalTabelleHorizontalR[4] = {-310,175,183,-333};  
5
 signed int CalTabelleHorizontalZ[4] = {-100,-390,-400,100}; // Messwinkel des Z-Sensors auch horizontal gemessen
6
*/
7
 signed int CalTabelleHorizontalN[4] = {  75,-180,-440,-166};    // Vier Messwerte 90° Horizontal gedreht
8
 signed int CalTabelleHorizontalR[4] = {  -5,-230,  66, 246};  
9
 signed int CalTabelleHorizontalZ[4] = {-290,-280,  44,  33}; // Messwinkel des Z-Sensors auch horizontal gemessen
10
 
11
  signed int RawMagnet1a,RawMagnet1b;  // reine AD-Messung
12
  signed int RawMagnet2a,RawMagnet2b;
13
  signed int RawMagnet3a,RawMagnet3b;
14
  signed int UncalMagnetN,UncalMagnetR,UncalMagnetZ;  // Messwert-Delta ohne Offset- und Verstärker korrektur
15
  signed int MagnetN,MagnetR,MagnetZ;                 // Kalibrierte Messerte
16
  unsigned int PwmHeading = 0;
17
  unsigned char PC_Connected = 0;
3 ingob 18
  int Heading;
1 ingob 19
#include "main.h"
20
 
21
 
22
//############################################################################
23
//
24
void Wait(unsigned char dauer)
25
//############################################################################
26
{
27
    dauer = (unsigned char)TCNT0 + dauer;
28
    while((TCNT0 - dauer) & 0x80);
29
}
30
 
31
void CalcFields(void)
32
{
33
 UncalMagnetN = (2 * UncalMagnetN + (RawMagnet1a - RawMagnet1b)) / 3;
34
 UncalMagnetR = (2 * UncalMagnetR + (RawMagnet3a - RawMagnet3b)) / 3;
35
 UncalMagnetZ = (2 * UncalMagnetZ + (RawMagnet2a - RawMagnet2b)) / 3;
36
 MagnetN = UncalMagnetN - OffsetN;
37
 MagnetR = UncalMagnetR - OffsetR;
38
 MagnetZ = UncalMagnetZ - OffsetZ;
39
}
40
 
41
//------------------------------------------------------
42
void CalcHeading(void)
43
{
44
   double nick_rad, roll_rad, Hx, Hy, Cx, Cy, Cz;
45
   float nick, roll, XEx, XEy, YEy, YEz, YE, XE, XrawCal, YrawCal, ZrawCal, XEz;
46
   int heading, azimuthgrad;
47
 
48
   nick_rad = ((double)WinkelOut.Winkel[0]) * M_PI / (double)(180);
49
   roll_rad = ((double)WinkelOut.Winkel[1]) * M_PI / (double)(180);
50
   //roll_rad = 0;   nick_rad = 0;
51
 
52
/*
53
   Cx =  (double) (MicroMag.Axis[Y_AXIS] + CY_OFFSET) * 0.707f - (double) (MicroMag.Axis[X_AXIS]+ CX_OFFSET) * 0.707f;
54
   Cy =  ((double) (MicroMag.Axis[Y_AXIS]+ CY_OFFSET) * 0.707f + (double) (MicroMag.Axis[X_AXIS]+ CX_OFFSET) * 0.707f);
55
   Cz = -MicroMag.Axis[Z_AXIS] + CZ_OFFSET;
56
*/
57
   Cx = MagnetN;
58
   Cy = MagnetR;
59
   Cz = -MagnetZ;
60
 
61
   Hx = Cx * (double)cos(nick_rad) +
62
        Cy * (double)sin(nick_rad) * (double)sin(roll_rad) -
63
        Cz * (double)sin(nick_rad) * (double)cos(roll_rad);      
64
 
65
   Hy = Cy * (double)cos(roll_rad) +
66
        Cz * (double)sin(roll_rad);
67
 
68
 
69
   if(Hx == 0 && Hy < 0) heading = 90;
70
   else if(Hx == 0 && Hy > 0) heading = 270;
71
   else if(Hx < 0) heading  = 180 - (atan(Hy / Hx) * 180 / M_PI);
72
   else if(Hx > 0 && Hy < 0) heading = - (atan(Hy / Hx) * 180 / M_PI);
73
   else if(Hx > 0 && Hy > 0) heading  = 360 - (atan(Hy / Hx) * 180 / M_PI);
74
 
75
  // DebugOut.Analog[14] = heading;
76
  // if (FromFlightCtrl.IntegralNick > 0) heading = heading + FromFlightCtrl.IntegralNick/60;
77
 
78
 if(heading < 361) DebugOut.Analog[14] = heading;
3 ingob 79
 Heading = heading;
1 ingob 80
 PwmHeading = heading + 10;
81
//   MicroMag.Heading = heading;
82
//   DebugOut.Analog[14] = heading;
83
}
84
 
85
 
86
 
87
//############################################################################
88
//Hauptprogramm
89
int main (void)
90
//############################################################################
91
{
92
    DDRC  = 0x08;
93
    PORTC = 0x08;
94
    DDRD  = 0xf4;
95
    PORTD = 0xA0;
96
    DDRB  = 0x04;
97
    PORTB = 0x35;
98
 
99
    LED_ON;
100
 
101
    UART_Init();
102
    Timer0_Init();
103
    ADC_Init();
3 ingob 104
        InitIC2_Slave();
1 ingob 105
    sei();//Globale Interrupts Einschalten
106
    Debug_Timer = SetDelay(100);   // Sendeintervall    
3 ingob 107
 
1 ingob 108
 
109
    OffsetN = (CalTabelleHorizontalN[0] + CalTabelleHorizontalN[1] + CalTabelleHorizontalN[2] + CalTabelleHorizontalN[3]) / 4;
110
    OffsetR = (CalTabelleHorizontalR[0] + CalTabelleHorizontalR[1] + CalTabelleHorizontalR[2] + CalTabelleHorizontalR[3]) / 4;
111
    OffsetZ = (CalTabelleHorizontalZ[0] + CalTabelleHorizontalZ[1] + CalTabelleHorizontalZ[2] + CalTabelleHorizontalZ[3]) / 4;
112
 
3 ingob 113
    VersionInfo.Hauptversion = VERSION_HAUPTVERSION;
114
    VersionInfo.Nebenversion = VERSION_NEBENVERSION;
1 ingob 115
    VersionInfo.PCKompatibel = 7;
116
 
117
 
118
    while (1)
119
        {
120
    LED_ON;
121
         FLIP_LOW;
122
         Delay_ms(2);
123
         RawMagnet1a = MessAD(0);
124
         RawMagnet2a = MessAD(1);
125
         RawMagnet3a = MessAD(7);
126
         Delay_ms(1);
127
LED_OFF;
128
         FLIP_HIGH;
129
         Delay_ms(2);
130
         RawMagnet1b = MessAD(0);
131
         RawMagnet2b = MessAD(1);
132
         RawMagnet3b = MessAD(7);
133
         Delay_ms(1);
134
 
135
         CalcFields();
136
         DebugOut.Analog[0] = MagnetN;
137
         DebugOut.Analog[1] = MagnetR;
138
         DebugOut.Analog[2] = MagnetZ;
139
         DebugOut.Analog[3] = UncalMagnetN;
140
         DebugOut.Analog[4] = UncalMagnetR;
141
         DebugOut.Analog[5] = UncalMagnetZ;
142
/*
143
         DebugOut.Analog[3] = RawMagnet1a;
144
         DebugOut.Analog[4] = RawMagnet1b;
145
         DebugOut.Analog[5] = RawMagnet3a;
146
         DebugOut.Analog[6] = RawMagnet3b;*/
147
         DebugOut.Analog[6] = WinkelOut.Winkel[0];
148
         DebugOut.Analog[7] = WinkelOut.Winkel[1];
149
 
150
         CalcHeading();
151
         BearbeiteRxDaten();
152
         PC_Connected = 100;
153
         if(PC_Connected)
154
          {
155
            PC_Connected--;        
156
            DatenUebertragung();
157
            DDRD  |= 0x02; // TXD-Leitung
158
          }  
159
          else
160
           {
161
            DDRD &= ~0x02; // TXD-Leitung
162
           }
163
        } // while(1) - Hauptschleife
164
}
165