Advertisement
ozanselte

Line Follower - RobotC

Nov 19th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.50 KB | None | 0 0
  1. #pragma config(Sensor, S1,     sSol,           sensorLightActive)
  2. #pragma config(Sensor, S2,     sOrta,          sensorLightActive)
  3. #pragma config(Sensor, S3,     sSag,           sensorLightActive)
  4. #pragma config(Motor,  motorB,          mSol,          tmotorNXT, PIDControl, encoder)
  5. #pragma config(Motor,  motorC,          mSag,          tmotorNXT, PIDControl, encoder)
  6. //*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
  7.  
  8. /*
  9.     S1 Sol Sensor
  10.     S2 Orta Sensor
  11.     S3 Sag Sensor
  12.  
  13.     MB Sol
  14.     MC Sag
  15. */
  16.  
  17. short hizT = 70, hizO = 2, durum[2] = {0 ,0};
  18.  
  19. void rIleri(){
  20.     motor[mSag] = hizT;
  21.     motor[mSol] = hizT;
  22. }
  23.  
  24. void rGeri(){
  25.     motor[mSag] = -hizT;
  26.     motor[mSol] = -hizT;
  27. }
  28.  
  29. void rSaga(){
  30.     switch(durum[1]){
  31.         case 1:
  32.             motor[mSol] = (hizT / hizO);
  33.             motor[mSag] = -(hizT / hizO);
  34.             break;
  35.         case 2:
  36.             motor[mSol] = hizT;
  37.             motor[mSag] = -hizT;
  38.             break;
  39.     }
  40. }
  41.  
  42. void rSola(){
  43.     switch(durum[1]){
  44.         case 1:
  45.             motor[mSol] = -(hizT / hizO);
  46.             motor[mSag] = (hizT / hizO);
  47.             break;
  48.         case 2:
  49.             motor[mSol] = -hizT;
  50.             motor[mSag] = hizT;
  51.             break;
  52.     }
  53. }
  54.  
  55. task main(){
  56.     sleep(1500);
  57.  
  58.     short c[3] = {0, 0, 0};
  59.     short isik[3] = {SensorValue[sSol], SensorValue[sOrta], SensorValue[sSag]};
  60.     short oran = (isik[0] + isik[2]) / 2;
  61.  
  62.     while(1){
  63.         isik[0] = SensorValue[sSol];
  64.         isik[1] = SensorValue[sOrta];
  65.         isik[2] = SensorValue[sSag];
  66.  
  67.         for(short i = 0; i < 3; i++){
  68.             if(isik[i] < (oran - 5)){
  69.                 c[i] = 1;
  70.             }
  71.             else{
  72.                 c[i] = 0;
  73.             }
  74.         }
  75.  
  76.         //++Ileri++//
  77.         if((c[0] && !c[1] && c[2]) || (!c[0] && c[1] && !c[2])){
  78.             durum[0] = 3;
  79.             durum[1] = 0;
  80.             rIleri();
  81.         }
  82.  
  83.         //++Saga++//
  84.         else if(!c[0] && !c[1] && c[2]){ //saga dik
  85.             durum[0] = 1;
  86.             durum[1] = 1;
  87.             rSaga();
  88.         }
  89.         else if(!c[0] && c[1] && c[2]){ //saga normal
  90.             durum[0] = 1;
  91.             durum[1] = 2;
  92.             rSaga();
  93.         }
  94.  
  95.         //++Sola++//
  96.         else if(c[0] && !c[1] && !c[2]){ //sola dik
  97.             durum[0] = 1;
  98.             durum[1] = 1;
  99.             rSola();
  100.         }
  101.         else if(c[0] && c[1] && !c[2]){ //sola normal
  102.             durum[0] = 1;
  103.             durum[1] = 2;
  104.             rSola();
  105.         }
  106.  
  107.         nxtDisplayTextLine(1, "Izmir Ataturk Lisesi");
  108.         nxtDisplayTextLine(2, "Robotics Team - 2013");
  109.         nxtDisplayTextLine(3, "Oran: %d", oran);
  110.         nxtDisplayTextLine(4, "Sol: %d, %d", c[0], isik[0]);
  111.         nxtDisplayTextLine(5, "Orta: %d, %d", c[1], isik[1]);
  112.         nxtDisplayTextLine(6, "Sag: %d, %d", c[2], isik[2]);
  113.         nxtDisplayTextLine(7, "Durum: %d, %d", durum[0], durum[1]);
  114.         nxtDisplayTextLine(8, "Hiz: &d, %d", hizT, (hizT/hizO));
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement