Skip to content

[14] Line follower module

Description

White and black line detector for following some path on the floor. It reports line offset underneath the sensor which is used to steer a robot in the correct direction.

Features:

  • White/Black line position detection
  • Junction detection
  • Automatic line color detection
  • Automatic contrast calibration

Totem Module 14


Code examples

Arduino projects: 14_lineFollower

Module14 module;
Function usage (click to expand)
/* Reading sensor data */
// Get line position "3"
int pos = module.pos.get();
// Get line color 'W'
char color = module.color.get();
// Check if color is white "true"
bool isWhite = module.color.isWhite();
// Check if color is black "false"
bool isBlack = module.color.isBlack();
// Check if color (line) is detected "true"
bool isColor = module.color.isDetected();
// Check if junction is detected "false"
bool isJunction = module.junction.isDetected();
// Get current detection accuracy "50"
int accuracy = module.accuracy.get();
// Set new detection threshold
module.accuracy.setThreshold(20);
// Get raw sensor reading
uint8_t *contrast = NULL;
module.raw.get(contrast);
// contrast contains array: {21,30,65,70,70,68,36,20}
int sensorA = contrast[0]; // 21
int sensorD = contrast[3]; // 70
int sensorH = contrast[7]; // 20
/* All LED control */
// Turn all LED on
module.led.on();
// Turn all LED off
module.led.off();
// Set all LED on
module.led.set(HIGH);
// Toggle all LED on / off
module.led.toggle();
// Is any of LED on "true"
bool isAllOn = module.led.isOn();
// Set LEDs : on|off|off|on|on|off|off|on
module.led.setBinary(B10011001);
// Reset LEDs to default behavior
module.led.reset();
/* Single LED control */
// Turn LED A on
module.led.onX(0);
// Turn LED D off
module.led.offX(3);
// Toggle LED B on / off
module.led.toggleX(1);
// Set LED A off
module.led.setX(0, LOW);
// Check if LED A is on "false"
bool isAOn = module.led.isOnX(0);

Functions

Reading sensor data

(position) module.pos.get()

Line position below sensor. (negative, 0, positive) = (left, center, right).
Returns:
position - line position [-35:35]. 0 - center

(color) module.color.get()

Get currently detected line color. This can be White or Black. Color is returned in ASCII character. Use 'W', 'B' expression.
Returns:
color - line color (char) ['W' - white, 'B' - black]. 0 - not detected.

(state) module.color.isWhite()

Check if white line is detected.
Returns:
state - white line detected [true:false]

(state) module.color.isBlack()

Check if black line is detected.
Returns:
state - black line detected [true:false]

(state) module.color.isDetected()

Check if line color is detected.
Returns:
state - line detected [true:false]

(state) module.junction.isDetected()

Check if sensor is currently detecting line junction (cross). A horizontal lines in its follow path. This is used for invoking some kind of action or to change direction (turn robot left or right)
NOTE: This will also return true if line is not detected at all.
Returns:
state - junction detected [true:false]

(contrast) module.accuracy.get()

Percentage of current contrast difference between line and ground (maximum values).
Higher value means better detection accuracy. If value is lower than setThreshold(), sensor won't be able to work properly. In this case lower setThreshold() value or improve surface contrast.
Returns:
contrast - contrast difference [0:100]%

module.accuracy.setThreshold(contrast)

Minimum contrast difference between line and ground required to start detection.
Lower value means more sensitivity when detecting line, but more susceptible to noise and misreadings. Should be changed only when there is an issue with detection on particular surface.
Parameter:
contrast - detection start contrast [10:80]%. Default: 20

module.raw.get(data[8])

Returns 8 byte array of contrast reading from each sensor.
0% - light does not reflect (black), 100% - light does reflect (white).
Array index 0 corresponds to sensor A, 1 - B, ..., 7 - H.
Data is returned trough data array parameter.
Returns:
data[8] - 8 byte array of each sensor contrast [0:100]%

All LED control

module.led.on()

Turn all LED on.

module.led.off()

Turn all LED off.

module.led.set(state)

Set all LED to specific state (on / off).
Parameter:
state - state on / off [HIGH:LOW] or [true:false]

module.led.toggle()

Toggle all LED between on / off states.

(state) module.led.isOn()

Check if any of LED is on.
Returns:
state - is any LED on [true:false] or [HIGH:LOW].

module.led.setBinary(mask)

Set all LED state with single value mask.
Each bit in 8-bit value represents individual LED state. 1 - on, 0 - off.
Parameter:
state - binary mask [B00000000:B11111111].

module.led.reset()

Re-enable default behavior of LED. It is switched off when any of LED function is used.

Single LED control

Control specific LED by providing it's number. 0 - A, 1 - B, ..., 7 - H.

module.led.onX(number)

Turn single LED on.
Parameter:
number - LED array number [0:7]

module.led.offX(number)

Turn single LED off.
Parameter:
number - LED array number [0:7]

module.led.setX(number, state)

Set all LED to specific state (on / off).
Parameter:
number - LED array number [0:7]
state - state on / off [HIGH:LOW] or [true:false]

module.led.toggleX(number)

Toggle single LED between on / off states.
Parameter:
number - LED array number [0:7]

(state) module.led.isOnX(number)

Check if specific LED is on.
Parameter:
number - LED array number [0:7]
Returns:
state - is any LED on [true:false] or [HIGH:LOW].

Low level commands

These are low level TotemBUS commands accepted by module. Is not required when using objective API described above.

Command Parameters Description
sensor/threshold (int) Sensor threshold
sensor/range Returns:(int) Accuracy
sensor/raw Returns:(byte[8]) Raw each sensor contrast
line/junction Returns:(bool) Is junction detected
line/color Returns:(char) Detected line color
line/pos Returns:(int) Detected line position
led (byte) Write 8-bit mask of each LED on/off