Line follower


#1
void modeC()
{
  uint8_t val;
  val = line.readSensors();
  if(moveSpeed >230)moveSpeed=230;
  switch (val)
  {
    case S1_IN_S2_IN:
      Forward();
      LineFollowFlag=10;
      break;

    case S1_IN_S2_OUT:
       Forward();
      if (LineFollowFlag>1) LineFollowFlag--;
      break;

    case S1_OUT_S2_IN:
      Forward();
      if (LineFollowFlag<20) LineFollowFlag++;
      break;

    case S1_OUT_S2_OUT:
      if(LineFollowFlag==10) Backward();
      if(LineFollowFlag<10) TurnLeft();
      if(LineFollowFlag>10) TurnRight();
      break;
  }

Can someone explain this line follower code to me ?
what is the flag !
and why 10?


#2

Not sure where you got the code, but unless it’s written by a code generator the author should be ashamed for not providing in-code comments as documentation. I’m also not a fan of cryptic variable names like S1 and S2 because the reader has to spend more cognitive effort to figure out what those variables stand for. IMHO, RIGHT_IN_LEFT_IN, etc., would have been much more obvious.

I’ve added comments that I think make sense. The LineFollowFlag uses 10 as a median value. Every time one of the sensors is not over the line (and remember there are two sensors in the line following sensor), the LineFollowFlag will be decremented or incremented to track the last state. When both of the sensors indicate they are not over the line, the program checks the last LineFollowFlag value to determine whether it needs to back up, turn left, or turn right.

Hope this helps.

Chuck

void modeC()
{
  uint8_t val;

  // capture the current value of the line sensor
  val = line.readSensors();

  // if the speed is greater than 230 set the moveSpeed value to 230
  // This is likely acting as a governor to control the maximum speed
  if(moveSpeed >230)moveSpeed=230;

  switch (val)
  {
    // Both line sensors are over the line
    case S1_IN_S2_IN:     
      Forward();          // move forward
      LineFollowFlag=10;  // State flag, 10 is the median value
      break;

    // Left sensor is over the line, Right sensor is not over the line
    case S1_IN_S2_OUT:    
       Forward();   // move forward
      // decrement the state flag to indicate that the right sensor
      // is not over the line
      if (LineFollowFlag>1) LineFollowFlag--;
      break;

    // Left sensor is not over the line, Right sensor is over the line
    case S1_OUT_S2_IN:
      Forward();  // Move forward
      // increment the state flag to indicate that the left sensor
      // is not over the line
      if (LineFollowFlag<20) LineFollowFlag++;
      break;

     // Both sensors are not over the line
    case S1_OUT_S2_OUT:
      // if both sensors are equally off, move backward
      if(LineFollowFlag==10) Backward();

      // if the Right sensor is not over the line, turn left
      if(LineFollowFlag<10) TurnLeft();

      // if the Left sensor was the last state before both sensors
      // were off of the line, turn right
      if(LineFollowFlag>10) TurnRight();
      break;
  }

#3

The code is from
makeblock\examples\Firmware_for_MegaPi\Firmware_for_MegaPi.ino :grin:


#4

I stand by my statement about comments in code. :slight_smile:

[Rant Mode On]
I’ve been writing software for <cough, cough> decades and the lack of comments in code are always carped on by the programmer as being useless because “if you know [language x] you should be able to just see what’s happening by looking at the code.” In my experience this translates to “I’m too lazy to document, that’s somebody else’s job” or “I’ll act like you’re an idiot because I’m too lazy to follow good practices.” Unless one is foolish enough to think that nobody else will ever need to modify “their” code (which most of the time “they” don’t own because the code belongs to the company paying them to write code), proper documentation saves time (which translates into money) and headaches.
[Rant Mode Off]

But your mileage may vary… :wink:


#5

how it says in first comment left sensor is out then increment to detect that the" right "sensor is out, confusing !
// Left sensor is not over the line, Right sensor is over the line
case S1_OUT_S2_IN:
Forward(); // Move forward
// increment the state flag to indicate that the right sensor
// is not over the line
if (LineFollowFlag<20) LineFollowFlag++;
break;


#6

I got bitten by cut and paste copying coupled with a minor emergency that I had to deal with at home. :smiley:
I’ve fixed the comments above and sorry for the confusion.


#7