Thursday, 15 November 2012

Source Code for Mobile Robot Tracking System (Receiver) 21

**Coding for Receiver(Robot)**


#include <16f877A.h> //use pic16f877a
#device adc=10    //set 10-bit adc
#use delay(clock = 20000000)  //clock=20mhz
#fuses hs, noprotect, nowdt  //default setting

#byte PORTA=5  //port a address
#byte PORTB=6  //port b address
#byte PORTC=7  //port c address
#byte PORTD=8  //port d address
#byte PORTE=9  //port e address

//system variables
long int mymax;
long int rxir;
long int rxir_org;
int speed1=100;
int speed2=100;
int turn_en=0;
int run=0;
int on_timer=0;      
int en_robot=0;

//modules to drive motor1
void motor1_fwd();
void motor1_rev();
void motor1_stop();

//modules to drive motor2
void motor2_fwd();
void motor2_rev();
void motor2_stop();

void main()
{
   //setup io for each pic pin
   set_tris_a(0b00000101);
   set_tris_b(0b11000000);
   set_tris_c(0b00000000);
   set_tris_d(0b00000000);
   set_tris_e(0b00000000);
   setup_ccp1(CCP_PWM);
   setup_ccp2(CCP_PWM);
   setup_timer_2(T2_DIV_BY_16, 100, 1);
   setup_port_a(ALL_ANALOG);
   setup_adc(ADC_CLOCK_INTERNAL);
   set_pwm1_duty(speed1);
   set_pwm2_duty(speed1);

   //stop robot
   motor1_stop();
   motor2_stop();

   output_high(pin_d7);  //on red led
   output_high(pin_d6);  //on green led
   output_high(pin_d5);  //on blue led
   delay_ms(1000);
   output_low(pin_d7);  //off red led
   output_low(pin_d6);  //off green led
   output_low(pin_d5);  //off blue led
   delay_ms(1000);

   set_adc_channel(2);
   delay_ms(10);
   rxir_org=read_adc();
   rxir_org=rxir_org+10;

   do
   {    
      set_adc_channel(0);
      delay_ms(10);
      mymax=read_adc();

      set_adc_channel(2);
      delay_ms(10);
      rxir=read_adc();
 
      if(rxir>rxir_org) //if transmitter signal detected
      {
         output_high(pin_d5);  //on blue led
         on_timer=20;      
         en_robot=1;
      }
      else  //if transmitter data not detected
      {
         if(on_timer>0) //if timer >0
         {
            on_timer=on_timer-1; //count down
         }
         else  //if timer =0
         {
            output_low(pin_d5);  //off blue led            
            en_robot=0;
         }
      }

      if(input(pin_b7)==0) //if green button pressed
      {
         set_adc_channel(2);
         delay_ms(10);
         rxir_org=read_adc();
         rxir_org=rxir_org+20;
         delay_ms(10);
         run=1;    
      }

      if(input(pin_b6)==0) //if red button pressed
      {
         run=0;    
      }

      if(run==1)  //under run mode
      {
         output_low(pin_d7);   //off red led
         output_high(pin_d6);  //on green led

         if(en_robot==1)   //if robot enabled
         {
            //start robot
            set_pwm1_duty(speed1);
            set_pwm2_duty(speed1);

            if(mymax<=240)   //if no obstacle in range
            {
               //forward robot
               motor1_fwd();
               motor2_fwd();      

               if(turn_en==1)
               {
                  turn_en=0;
               }
               else
               {
                  turn_en=1;
               }
            }
            else if(mymax>240 && mymax<320)   //if obstacle just in range
            {
               //stop robot
               motor1_stop();
               motor2_stop();                
            }      
            else if(mymax>=320)   //if obstacle too close
            {
               //reverse robot
               motor1_rev();
               motor2_rev();                
            }      
         }
         else
         {
            set_pwm1_duty(speed2);
            set_pwm2_duty(speed2);

            if(turn_en==1)
            {
               //turn to left
               motor1_fwd();
               motor2_rev();                
            }
            else
            {
               //turn to right
               motor1_rev();
               motor2_fwd();                            
            }
         }
      }
      else  //under stop mode
      {
         output_high(pin_d7);  //on red led
         output_low(pin_d6);  //off green led
   
         //stop robot
         motor1_stop();
         motor2_stop();
      }

      delay_ms(250);

   }while(1);
}

void motor1_fwd() //module to forward motor1
{
   output_high(PIN_C3);
   output_low(PIN_C0);
}

void motor1_rev() //module to reverse motor1
{
   output_low(PIN_C3);
   output_high(PIN_C0);
}

void motor1_stop()   //module to stop motor1
{
   output_low(PIN_C3);
   output_low(PIN_C0);
}
void motor2_fwd() //module to forward motor2
{
   output_high(PIN_D0);
   output_low(PIN_D1);
}

void motor2_rev() //module to reverse motor2
{
   output_low(PIN_D0);
   output_high(PIN_D1);
}

void motor2_stop()   //module to stop motor2
{
   output_low(PIN_D0);
   output_low(PIN_D1);
}

No comments:

Post a Comment