ROBOTC.net forumshttp://www.robotc.net/forums/ Line tracking using two sensorshttp://www.robotc.net/forums/viewtopic.php?f=45&t=3148 Page 1 of 1

Author:  Netherlands [ Fri Mar 25, 2011 12:54 pm ]
Post subject:  Line tracking using two sensors

Hello guys,
First I will introduce myself; I am Melvin, a 17-year old dutch student. Last year I won at the robocup junior, the Rescue League (green map, easiest). We had won that league with a program, programmed in Robolab, that was pretty easy.
This year we wanted to participate the rescue arena, therefor we switched to RobotC. Robolab was too limited to do program the things that the arena requires.

We've started with programming a line follow program with 2 sensors. But after 2 weeks programming, the program is still not working. I dont know where exactly the fault is because the program looks really logical. Below you'll see our program with some explanation. Could someone please take a look and help us out? Thanks!    Code:task main (){  while (true){    int Left = SensorValue(EyeLeft);                      int Right = SensorValue (EyeRight);    //black = 12;    //white   = 3;    if (SensorValue(EyeLeft) == Left && SensorValue(EyeRight) == Right){      // both sensors see white, the robot must go straight      motor[MotorLeft] = 35;            motor[MotorRight] = 35;     }    if (SensorValue(EyeLeft) > Left ){                  // left sensor sees black, so the robot must go to the right               motor[MotorLeft] = -20;      motor[MotorRight] = 50;      }    if (SensorValue(EyeRight) > Right){                  // right sensor sees black, so the robot must go to the left      motor[MotorLeft] = 50;      motor[MotorRight] = -20;     }              }}    Author:  bfeher [ Tue Apr 05, 2011 10:58 am ]
Post subject:  Re: Line tracking using two sensors

Hi Melvin,

Rather than setting 'Left' and 'Right' to the current sensor reading, try using a fixed threshold value. A good threshold value is the sensor reading of dark, added to sensor reading of light, then all divided by 2 [ (Light + Dark) / 2 ].

Also linking the 'if' conditions together with 'else' and 'else if' may prove to work better than separating them.

Keep in mind that if you are using multiple sensors to follow the same line, combining their logic helps a lot too. You have 2 sensors each with 2 states (light or dark) so you have 2^2 possible outcomes:

LEFT = light, RIGHT = light: go forward
LEFT = light, RIGHT = dark: turn right
LEFT = dark, RIGHT = light: turn left
LEFT = dark, RIGHT = dark: ? stop ?

Remember if the sensors look like this: L | R where the '|' is the black line, then if L sees the line you need to turn back to the LEFT to straighten out.    Code:task main (){  int LeftThreshold = 8;      // feel free to change this based on your own calculations!  int RightThreshold = 8;     // and this!  (I used the values you have below)      //black = 12;  //white   = 3;    while (true)  {    // LEFT = light, RIGHT = light:    if(SensorValue(EyeLeft) < LeftThreshold  &&  SensorValue(EyeRight) < RightThreshold)    {   // go FORWARD      motor[MotorLeft] = 35;      motor[MotorRight] = 35;    }    // LEFT = light, RIGHT = dark:    else if(SensorValue(EyeLeft) < LeftThreshold  &&  SensorValue(EyeRight) > RightThreshold)    {   // go LEFT               motor[MotorLeft] = -20;      motor[MotorRight] = 50;          }        // LEFT = dark, RIGHT = light:    else if(SensorValue(EyeLeft) > LeftThreshold  &&  SensorValue(EyeRight) < RightThreshold)    {   // go RIGHT      motor[MotorLeft] = 50;      motor[MotorRight] = -20;         }    // LEFT = dark, RIGHT = dark:    else    {   // do what you want here, I'm just putting a stop:      motor[MotorLeft] = 0;            motor[MotorRight] = 0;         }  }}    