#include "C:\Users\Honza\Documents\pic\azimut\main.h"



#define PIN_SDA  PIN_C4
#define PIN_SCL  PIN_C3
#use i2c(master, sda=PIN_SDA, scl=PIN_SCL)
#use rs232(baud=9600,parity=N,xmit=PIN_C7,rcv=PIN_C6,bits=8) //rcv TXD xmit RXD
#include <math.h>

//Akcelerometr
#define AK_W  0x38 //adresa akcelerometru zápis
#define AK_R  0x39 //adresa akcelerometru ètení
#define AK_XH 0x01 //osa X LSB
#define AK_XL 0x02 //osa X MSB
#define AK_YH 0x03 //osa Y LSB
#define AK_YL 0x04 //osa Y MSB
#define AK_ZH 0x05 //osa Z LSB
#define AK_ZL 0x06 //osa Z MSB

//Magnetometr
#define MAG_W  0x3C //adresa akcelerometru zápis
#define MAG_R  0x3D //adresa akcelerometru ètení
#define MAG_XH 0x03 //osa X LSB
#define MAG_XL 0x04 //osa X MSB
#define MAG_ZH 0x05 //osa Y LSB
#define MAG_ZL 0x06 //osa Y MSB
#define MAG_YH 0x07 //osa Z LSB
#define MAG_YL 0x08 //osa Z MSB





//pøipojení motorù
//AIN1 - pro vysku slunce
#define AIN1 PIN_D0 
#define AIN2 PIN_D1 
//motor A -cerveny vodic na AOUT1
//motor A -modry vodic na Aout2

//AIN2 - pro azimut
#define BIN1 PIN_D2 
#define BIN2 PIN_D3 
//motor B - èerveny vodic na BOUT2
//motor B - modrý vodic na BOUT1

signed int16 X, Y, Z,AX, AY, AZ;

void setAK (void) //nastaveni akcelerometru
{
   i2c_start();
   I2C_Write(AK_W);
   I2C_write(0x2A);
   I2C_write(0x01); //nastaví aktivní stav
 
   i2c_stop(); 
}

void setmag (void)
{
  i2c_start();
  I2C_Write(MAG_W);     // W
  I2C_Write(0x00);   
  I2C_Write(0x78);
  i2c_stop();
  Delay_ms(6);
   
  i2c_start();
  I2C_Write(MAG_W);     // W
  I2C_Write(0x01);   
  I2C_Write(0x00);
  i2c_stop();

  Delay_ms(6);

  i2c_start();
  I2C_Write(MAG_W);     // W
  I2C_Write(0x02);   
  I2C_Write(0x00);
  i2c_stop();
  Delay_ms(6);  
}

int16 akR (int8 H, int8 L) //vycitani hodnot z akcelerometru
{
unsigned int8 XL=0,XH=0;
signed int16 x;

   i2c_start();
   I2C_Write(AK_W);
   I2C_write(H);
   i2c_start();
   I2C_Write(AK_R);
   XH=i2c_read(0);
   i2c_stop(); 

   i2c_start();
   I2C_Write(AK_W);
   I2C_write(L);
   i2c_start();
   I2C_Write(AK_R);
   XL=i2c_read(0);
   i2c_stop();
   
   x = (((unsigned int16) XH << 8) + XL ); //prevod na 16bit hodnotu
   x=x/4;
   
   return x;
}

int16 magR (int8 H, int8 L) //vycitani hodnot z akcelerometru
{
unsigned int8 XL=0,XH=0;
signed int16 x;

   i2c_start();
   I2C_Write(MAG_W);
   I2C_write(H);
   i2c_start();
   I2C_Write(MAG_R);
   XH=i2c_read(0);
   i2c_stop(); 

   i2c_start();
   I2C_Write(MAG_W);
   I2C_write(L);
   i2c_start();
   I2C_Write(MAG_R);
   XL=i2c_read(0);
   i2c_stop();
   
  x = (((unsigned int16) XH << 8) + XL );

   
   return x;
}

float vyska (void) //vypocet aktualni vysky panelu
{
printf("Akcelerometr5:  \r\n",);

X= akR (AK_XH, AK_XL);  
Y= akR (AK_YH, AK_YL); 
Z= akR (AK_ZH, AK_ZL); 

AX=abs(X);
AY=abs(Y)+250;
AZ=abs(Z)+250;

float a, b;
a=(float)Y/Z;
b=atan(a);
b = (b/3.14)*180;
b=abs(b);
   
if(((AX>AY) || (AX>AZ))) //indikace prevraceni panelu
   { 
   printf("Prevracený panel)\r\n", );
   }
else
   {
   if(Z==0) //osetreni proti deleni 0
   { 
      if(Y>0)
         { 
         b=180;
         }
      else
         {
         b=0;
         }
   }  
   else
      {
      if(Z>0)
       {
       if(Y>=0)
       {
         b=90+b;
       }
       else
       {
         b=90-b;
       }
       }
      else
       {
       if(Y>=0)
       {
         b=180-b;
       }
       else
       {
         b=270+b;
       }
       }
         
      }   
   
   
   
  }
   printf("uhel namìreny %10.2f \r\n", b);
      return b;
   
}

float azimut (void) //vypocet aktualni vysky panelu
{
X= magR (MAG_XH, MAG_XL);  
Y= magR (MAG_YH, MAG_YL); 
Z= magR (MAG_ZH, MAG_ZL); 




AX=abs(X);
AY=abs(Y);
AZ=abs(Z);

float a, b;
a=(float)Y/X;
b=atan(a);
b = (b/3.14)*180;
b=abs(b);
   

   

   if(X==0) //osetreni proti deleni 0
   { 
      if(Y>0)
         { 
         b=90;
         }
      else
         {
         b=270;
         }
   }  
   else
      {
      if(X>0)
       {
       if(Y>=0)
       {
         b=b;
       }
       else
       {
         b=360-b;
       }
       }
      else
       {
       if(Y>=0)
       {
         b=180-b;
       }
       else
       {
         b=180+b;
       }
       }
         
      }   

      return b;
   
}

void motorA (int8 H) //pro ovladani prvniho motoru nastaveni vysky
{
switch(H){
   case 1:  //reverzní chod
   output_low (AIN2);
   output_high (AIN1);  
   break;

case 2: //dopøedu
   output_low (AIN1);
   output_high (AIN2);   
   break;

default:
   output_low (AIN2);
   output_low (AIN1);
         }
}

void motorB (int8 H) //pro ovladani prvniho motoru nastaveni vysky
{
switch(H){
   case 1:  //reverzní chod
   output_low (BIN2);
   output_high (BIN1);  
   break;

case 2: //dopøedu
   output_low (BIN1);
   output_high (BIN2);   
   break;

default:
   output_low (BIN2);
   output_low (BIN1);
         }
}

void vyska_set (int8 H) //slouzi pro nastaveni nove vysky panelu
{
printf("Akcelerometr4:  \r\n",);
float a;
int16 b,c;

a=vyska();
b= (int16) a;

if(b>350)
   {
   b=0;
   }
else
   {

   }  
c=abs(H-b); 

while(c>2) //maximalni odchylka uhlu, aby nebylo potreba panelem hybat
   {
   while(H!=b) //probiha dokud se uhel panelu nerovna zadanemu na cele stupne
      {

      if(H>b)
         {
         motorA(2);
    
         }
      else
      {
      motorA(1);
      }

      delay_ms (50); //cas sepnuti motoru

      motorA(3); //vypne motor
      delay_ms (50); //doma na ustaleni panelu pred merenim
      a=vyska();
      b= (int16) a;
  
      if(b>350) //osetreni pro uhel 0. Zabezpeci ze neprejde stav z 0 na 359 kdy by se solar zacal tocit na druhou stranu
         {
         b=0;
         }
      else
         {
         b=b;
         }
      c=abs(H-b);
      }
   }
motorA(3); //vypne motor
printf("Podaøené nastavení výška: %Ld\r\n", b);
}

void azimut_set (int8 H) //slouzi pro nastaveni nove vysky panelu
{
float a;
int16 b,c;

a=azimut();
b= (int16) a;
//printf("auhel pro mereni: %Ld(procenta)\r\n", b);
if(b>350)
   {
   b=0;
   }
else
   {

   }  
c=abs(H-b); 

while(c>3) //maximalni odchylka uhlu, aby nebylo potreba panelem hybat
   {
   while(H!=b) //probiha dokud se uhel panelu nerovna zadanemu na cele stupne
      {

      if(H>b)
         {
         motorB(2);
     
         }
      else
      {
      motorB(1);
      }

      delay_ms (50); //cas sepnuti motoru

      motorB(3); //vypne motor
      delay_ms (50); //doma na ustaleni panelu pred merenim
      a=azimut();
      b= (int16) a;
     
      if(b>350) //osetreni pro uhel 0. Zabezpeci ze neprejde stav z 0 na 359 kdy by se solar zacal tocit na druhou stranu
         {
         b=0;
         }
      else
         {
         b=b;
         }
      c=abs(H-b);
      }
   }
motorA(3); //vypne motor
printf("Podaøené nastavení azimut: %Ld\r\n", b);
}


void main()
{

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_2);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_ccp1(CCP_OFF);
   setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
   
   setup_adc_ports(PIN_A0); //piny pro A/D RA0
   setup_adc_ports(PIN_A1); //piny pro A/D RA1
   
printf("Akcelerometr:  \r\n",);

motorA(3);

float a, b;
unsigned int value1=10,value2=10;


setAK(); //nastaveni akcelerometru
setmag();  
printf("Akcelerometr1:  \r\n",);
  while(TRUE)
 { 

printf("Akcelerometr2:  \r\n",);



        
      
      ///  set_adc_channel(0); //nastavi AD na kanál 0 (RA0)
      ///   read_adc(ADC_START_ONLY);              // Spustime A/D prevod
      // Delay_ms(1);  
        //  while(!adc_done());                    // Cekame na dokonceni prevodu
       //  value1=read_adc();            // Precteme hodnotu z prevodniku
         vyska_set (90);
        azimut_set(0);
         printf("zadaný azimut %d \r\n", value1);
         
       //  set_adc_channel(1); //nastavi AD na kanál 1 (RA1)
     // read_adc(ADC_START_ONLY);              // Spustime A/D prevod
    //  Delay_ms(1); 
    //  while(!adc_done());                    // Cekame na dokonceni prevodu
   //   value2=read_adc(); 
        //vyska_set (90);
      

delay_ms (2000);

  
}
}