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