Guest User

Robotc Line Follow and Obstacle Avoidence

a guest
Nov 18th, 2014
963
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.99 KB | None | 0 0
  1. #pragma config(Sensor, S1,     sonar,          sensorSONAR)
  2. #pragma config(Sensor, S2,     rlight,         sensorLightActive)
  3. #pragma config(Sensor, S3,     llight,         sensorLightActive)
  4. #pragma config(Sensor, S4,     touch,          sensorTouch)
  5. #pragma config(Motor,  motorB,          lmotor,        tmotorNXT, PIDControl, encoder)
  6. #pragma config(Motor,  motorC,          rmotor,        tmotorNXT, PIDControl, encoder)
  7.  
  8. void findLine(int threshold) {
  9.     bool wave = false;
  10.     int motorHigh = 40;
  11.     int motorLow = 10;
  12.     int bypass = 0;
  13.  
  14.     while(SensorValue(llight) > threshold && SensorValue(rlight) > threshold) {
  15.         if(wave) {
  16.             motor[lmotor] = motorHigh;
  17.             motor[rmotor] = motorLow;
  18.         } else {
  19.             motor[rmotor] = motorLow;
  20.             motor[lmotor] = motorHigh;
  21.         }
  22.         bypass++;
  23.  
  24.         if(bypass >= 4000) {
  25.             wave = !wave;
  26.             bypass = 0;
  27.         }
  28.     }
  29. }
  30.  
  31. void navigateBottle() {//TODO: Fix this up
  32.     while(SensorValue(llight) = 0) {
  33.         if(SensorValue(sonar) < 5) {
  34.             motor[lmotor] = 0;
  35.             motor[rmotor] = 50;
  36.         }else{
  37.             motor[lmotor] = 50;
  38.             motor[rmotor] = 50;
  39.             wait1Msec(1000);
  40.             motor[lmotor] = 50;
  41.             motor[rmotor] = 0;
  42.         }
  43.     }
  44. }
  45.  
  46. void followLine(int threshold) {
  47.     int motorHigh = 30;
  48.     int motorLow = 0;
  49.     int counter = 0;
  50.  
  51.     while(SensorValue(touch) == 0) {
  52.         if(SensorValue(llight) > threshold && SensorValue(rlight) > threshold) {
  53.             counter++;
  54.             if(counter >= 8000) {
  55.                 motor[lmotor] = 0;
  56.                 motor[rmotor] = 0;
  57.                 writeDebugStreamLine("Temporary crazy moment");
  58.                 wait1Msec(1000);
  59.                 return;
  60.             }
  61.         }
  62.  
  63.         if(SensorValue(llight) < threshold && SensorValue(rlight) < threshold) {
  64.             writeDebugStreamLine("Turning");
  65.  
  66.             if(SensorValue(llight) < SensorValue(rlight)) {
  67.                 motor[lmotor] = motorHigh;
  68.                 motor[rmotor] = motorLow;
  69.             }else{
  70.                 motor[lmotor] = motorLow;
  71.                 motor[rmotor] = motorHigh;
  72.             }
  73.             counter = 0;
  74.  
  75.             wait1Msec(50);
  76.         } else {
  77.             if(SensorValue(llight) < threshold) {
  78.                 motor[lmotor] = motorHigh;
  79.                 motor[rmotor] = motorLow;
  80.                 counter = 0;
  81.             }
  82.  
  83.             if(SensorValue(rlight) < threshold) {
  84.                 motor[lmotor] = motorLow;
  85.                 motor[rmotor] = motorHigh;
  86.                 counter = 0;
  87.             }
  88.         }
  89.     }
  90. }
  91.  
  92. task main() {
  93.     while(SensorValue(touch) == 0) { }
  94.     int thresholdValue = (SensorValue(llight) + SensorValue(rlight)) / 2;
  95.  
  96.     writeDebugStreamLine("Threshold %d", thresholdValue);
  97.     wait1Msec(1000);
  98.  
  99.     while(SensorValue(touch) == 0) { }
  100.     wait1Msec(1000);
  101.  
  102.     followLine(thresholdValue);
  103. }
Advertisement
Add Comment
Please, Sign In to add comment