Subversion Repositories MK3Mag

Rev

Rev 3 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

 signed int OffsetN, OffsetR, OffsetZ;
/*
 signed int CalTabelleHorizontalN[4] = {67,-450,77,-411};    // Vier Messwerte 90° Horizontal gedreht
 signed int CalTabelleHorizontalR[4] = {-310,175,183,-333};  
 signed int CalTabelleHorizontalZ[4] = {-100,-390,-400,100}; // Messwinkel des Z-Sensors auch horizontal gemessen
*/

 signed int CalTabelleHorizontalN[4] = {  75,-180,-440,-166};    // Vier Messwerte 90° Horizontal gedreht
 signed int CalTabelleHorizontalR[4] = {  -5,-230,  66, 246};  
 signed int CalTabelleHorizontalZ[4] = {-290,-280,  44,  33}; // Messwinkel des Z-Sensors auch horizontal gemessen

  signed int RawMagnet1a,RawMagnet1b;  // reine AD-Messung
  signed int RawMagnet2a,RawMagnet2b;
  signed int RawMagnet3a,RawMagnet3b;
  signed int UncalMagnetN,UncalMagnetR,UncalMagnetZ;  // Messwert-Delta ohne Offset- und Verstärker korrektur
  signed int MagnetN,MagnetR,MagnetZ;                 // Kalibrierte Messerte
  unsigned int PwmHeading = 0;
  unsigned char PC_Connected = 0;
#include "main.h"


//############################################################################
//
void Wait(unsigned char dauer)
//############################################################################
{
    dauer = (unsigned char)TCNT0 + dauer;
    while((TCNT0 - dauer) & 0x80);
}

void CalcFields(void)
{
 UncalMagnetN = (2 * UncalMagnetN + (RawMagnet1a - RawMagnet1b)) / 3;
 UncalMagnetR = (2 * UncalMagnetR + (RawMagnet3a - RawMagnet3b)) / 3;
 UncalMagnetZ = (2 * UncalMagnetZ + (RawMagnet2a - RawMagnet2b)) / 3;
 MagnetN = UncalMagnetN - OffsetN;
 MagnetR = UncalMagnetR - OffsetR;
 MagnetZ = UncalMagnetZ - OffsetZ;
}

//------------------------------------------------------
void CalcHeading(void)
{
   double nick_rad, roll_rad, Hx, Hy, Cx, Cy, Cz;
   float nick, roll, XEx, XEy, YEy, YEz, YE, XE, XrawCal, YrawCal, ZrawCal, XEz;
   int heading, azimuthgrad;
   
   nick_rad = ((double)WinkelOut.Winkel[0]) * M_PI / (double)(180);
   roll_rad = ((double)WinkelOut.Winkel[1]) * M_PI / (double)(180);
   //roll_rad = 0;   nick_rad = 0;

/*
   Cx =  (double) (MicroMag.Axis[Y_AXIS] + CY_OFFSET) * 0.707f - (double) (MicroMag.Axis[X_AXIS]+ CX_OFFSET) * 0.707f;
   Cy =  ((double) (MicroMag.Axis[Y_AXIS]+ CY_OFFSET) * 0.707f + (double) (MicroMag.Axis[X_AXIS]+ CX_OFFSET) * 0.707f);
   Cz = -MicroMag.Axis[Z_AXIS] + CZ_OFFSET;
*/

   Cx = MagnetN;
   Cy = MagnetR;
   Cz = -MagnetZ;
   
   Hx = Cx * (double)cos(nick_rad) +
        Cy * (double)sin(nick_rad) * (double)sin(roll_rad) -
        Cz * (double)sin(nick_rad) * (double)cos(roll_rad);      
               
   Hy = Cy * (double)cos(roll_rad) +
        Cz * (double)sin(roll_rad);
 
               
   if(Hx == 0 && Hy < 0) heading = 90;
   else if(Hx == 0 && Hy > 0) heading = 270;
   else if(Hx < 0) heading  = 180 - (atan(Hy / Hx) * 180 / M_PI);
   else if(Hx > 0 && Hy < 0) heading = - (atan(Hy / Hx) * 180 / M_PI);
   else if(Hx > 0 && Hy > 0) heading  = 360 - (atan(Hy / Hx) * 180 / M_PI);

  // DebugOut.Analog[14] = heading;
  // if (FromFlightCtrl.IntegralNick > 0) heading = heading + FromFlightCtrl.IntegralNick/60;
   
 if(heading < 361) DebugOut.Analog[14] = heading;
 AnFlightCtrl.Heading = heading;
 PwmHeading = heading + 10;
//   MicroMag.Heading = heading;
//   DebugOut.Analog[14] = heading;
}



//############################################################################
//Hauptprogramm
int main (void)
//############################################################################
{
    DDRC  = 0x08;
    PORTC = 0x08;
    DDRD  = 0xf4;
    PORTD = 0xA0;
    DDRB  = 0x04;
    PORTB = 0x35;
       
    LED_ON;
   
    UART_Init();
    Timer0_Init();
    ADC_Init();
    sei();//Globale Interrupts Einschalten
    Debug_Timer = SetDelay(100);   // Sendeintervall    
    InitIC2_Slave(0x50);                           

    OffsetN = (CalTabelleHorizontalN[0] + CalTabelleHorizontalN[1] + CalTabelleHorizontalN[2] + CalTabelleHorizontalN[3]) / 4;
    OffsetR = (CalTabelleHorizontalR[0] + CalTabelleHorizontalR[1] + CalTabelleHorizontalR[2] + CalTabelleHorizontalR[3]) / 4;
    OffsetZ = (CalTabelleHorizontalZ[0] + CalTabelleHorizontalZ[1] + CalTabelleHorizontalZ[2] + CalTabelleHorizontalZ[3]) / 4;

    VersionInfo.Hauptversion = 0;
    VersionInfo.Nebenversion = 3;
    VersionInfo.PCKompatibel = 7;
 
       
    while (1)
        {
    LED_ON;
         FLIP_LOW;
         Delay_ms(2);
         RawMagnet1a = MessAD(0);
         RawMagnet2a = MessAD(1);
         RawMagnet3a = MessAD(7);
         Delay_ms(1);
LED_OFF;
         FLIP_HIGH;
         Delay_ms(2);
         RawMagnet1b = MessAD(0);
         RawMagnet2b = MessAD(1);
         RawMagnet3b = MessAD(7);
         Delay_ms(1);

         CalcFields();
         DebugOut.Analog[0] = MagnetN;
         DebugOut.Analog[1] = MagnetR;
         DebugOut.Analog[2] = MagnetZ;
         DebugOut.Analog[3] = UncalMagnetN;
         DebugOut.Analog[4] = UncalMagnetR;
         DebugOut.Analog[5] = UncalMagnetZ;
/*
         DebugOut.Analog[3] = RawMagnet1a;
         DebugOut.Analog[4] = RawMagnet1b;
         DebugOut.Analog[5] = RawMagnet3a;
         DebugOut.Analog[6] = RawMagnet3b;*/

         DebugOut.Analog[6] = WinkelOut.Winkel[0];
         DebugOut.Analog[7] = WinkelOut.Winkel[1];

         CalcHeading();
         BearbeiteRxDaten();
         PC_Connected = 100;
         if(PC_Connected)
          {
            PC_Connected--;        
            DatenUebertragung();
            DDRD  |= 0x02; // TXD-Leitung
          }  
          else
           {
            DDRD &= ~0x02; // TXD-Leitung
           }
        } // while(1) - Hauptschleife
}