Home / Programming / ROBOTC / A Smoother LineLeader

A Smoother LineLeader

Line Sensor Array for Mindstorms NXT - NXTLineLeaderSometimes even the greatest leaders need a little help.  This is also true of the Mindsensors LineLeader sensor. I’ve been working on maze solving robot and noticed that the robot’s state engine would sometimes be triggered by transient readings from the sensor. It would suddenly think that a crossing was detected and swerve to the right or left.

Rather than resorting to all sorts of difficult timing and additional states in my engine, I decided to fix the source of the issue, rather than the symptoms. A very simple threshold system would allow simple transient values to be absorbed. You have to keep in mind that this will only be of use to you if you are not using the sensor’s built-in PID regulator.

#include "drivers/MSLL-driver.h"

ubyte decay[8] = {0, 0, 0, 0, 0, 0, 0, 0};

int getAverage () {
  int rawsensorstatus = 0;
  int dampenedsensorstatus = 0;
  int total = 0;
  int activesensors = 0;

  // Read the sensor status.
  // This is a byte value where each bit represents the status
  // of the sensor, 0 is nothing, 1 is line detected
  rawsensorstatus = LLreadResult(S1); // change to whatever port yours is on

  // Add some dampening
  for (int i = 0; i < 8; i++) {
    if ((rawsensorstatus >> i) & 1)
      decay[i] = (decay[i] < 3) ? decay[i] + 1 : decay[i];
    else
      decay[i] = (decay[i] > 0) ? decay[i] - 1 : decay[i];

    dampenedsensorstatus += ((decay[i] / 2) << i);
  }

  // Calculate the weighted average
  for (int i = 0; i < 8; i++) {
    if ((dampenedsensorstatus >> i) & 1) {
      total += 10 * (i+1);
      activesensors++;
    }
  }

  return (total / activesensors);
}

I find this has a very calming effect on my robot’s behaviour and allows me to keep the state engine much simpler.  You will need to use the MSLL driver that comes with my driver suite.

About Xander

Xander Soldaat is a Software Engineer and former Infrastructure Architect. He loves building and programming robots. He recently had the opportunity to turn his robotics hobby into his profession and has started working for Robomatter, the makers of ROBOTC and Robot Virtual Words.