View unanswered posts | View active topics It is currently Tue Dec 01, 2015 12:57 pm

Reply to topic  [ 4 posts ] 
Line tracking using two sensors 
Author Message

Joined: Fri Mar 25, 2011 11:45 am
Posts: 13
Post 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!

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;     }

Fri Mar 25, 2011 12:54 pm
Site Admin
Site Admin

Joined: Mon Jun 08, 2009 4:50 pm
Posts: 70
Post 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.

Try this code instead:

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:
    {   // do what you want here, I'm just putting a stop:
      motor[MotorLeft] = 0;     
      motor[MotorRight] = 0;     

Remember to play around with the threshold values and motor speeds. I hope that this helps you get moving in the right direction.

Bence Feher

Undergraduate Intern - NREC, Robotics Academy
ROBOTC - Testing/Documentation/Developer

Computer Science, Japanese, East Asian Studies
University of Pittsburgh, Konan University 甲南大学

Tue Apr 05, 2011 10:58 am

Joined: Sat Mar 01, 2014 3:52 am
Posts: 1
Post Re: Line tracking using two sensors
hey I have been getting this message:
Couldn't open '#include' file 'drivers/HTMAG-driver.h'

I can't find where to get the driver, or why it won't work for me.
Any help would be much appreciated.

Sat Mar 01, 2014 3:58 am
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3589
Location: Rotterdam, The Netherlands
Post Re: Line tracking using two sensors
Hi Matmccann,

Please create your own new thread, instead of tacking onto this one. Then give us a LOT more info about what it is was you were trying to do in the first place.

= Xander

| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]

Sat Mar 01, 2014 4:25 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.