Quarky Advance Line Following

QRK Line Following
Extension Description

Introduction

The Advanced Line Following Extension gives you two distinct methods for navigating a line-following arena with Quarky:

With Adaptive Feedback (PID): Uses Proportional-Integral-Derivative control for precise, responsive navigation. Ideal for complex arenas and competition-grade performance.

Without Adaptive Feedback: Straightforward motor-speed control without PID. Best for simple arenas or as a starting point before tuning PID parameters.

The extension features the following sections:

  • Common Blocks
  • Two IR (Infrared) Pin Settings Blocks
  • Three IR Pin Settings Blocks
  • Line Follower Blocks without Adaptive Feedback
  • Line Follower Blocks with Adaptive Feedback (PID)

Please make sure to use PictoBlox version 9.1.0 or later for Quarky’s latest Extension support. Click Download PictoBlox to visit the download page, select your operating system, and follow the on-screen instructions to install the latest version.Download PictoBlox

Common Blocks and Functions

Initialize Line Following

Sets up the advanced line following system once the circuits are connected to Quarky. Always use this block first, before any other line that follows it.

Block:

Initialize Line Following

Python:

from quarkyAdvanceLineFollowing import AdvanceLineFollowing

linefollow = AdvanceLineFollowing(IR_NUM = 2)

The two supported IR sensor configurations are:

  1. 2 IR Sensors (Analog pins only) Connect sensors to analog pins on Quarky or the Quarky Expansion Board. Can also use Quarky’s built-in IR sensors.
  2. 3 IR Sensors (Digital pins only) Connect external sensors to digital pins. The threshold is set using the potentiometer on each sensor module, not in code.

Two External IR Sensors connected (via Analog Pins) to the Quarky Expansion Board

Three External IR Sensors connected (via Digital Pins) to the Quarky Expansion Board

Calibrate IR

The robot moves forward and reverse for a specific duration while collecting sample data from the IR sensors on both white and black surfaces. Run this once before starting line following.

Block:

Calibrating Quarky’s internal (built-in) IR sensors:

  • Prerequisite: Ensure the Initialize block has been used first.
  • Position Quarky a few steps before a checkpoint on a straight section of the arena.
  • Start calibration. The robot must traverse the checkpoint and adjacent white area a total of three times.
  • Threshold values for left and right IR sensors are shown on the PictoBlox screen after calibration.

Calibrating Quarky's internal (built-in) IR sensors

Calibrating external IR sensors (Pins A1, A2, or A3):

Follow this exact block order:

  1. Initialize block
  2. Set pin assignment blocks (change left IR to A1 pin, change right IR to A2 pin, etc.)
  3. Calibrate IR block

Note: Failing to follow this exact sequence will cause the system to display calibration values for the internal sensors instead of the connected external ones.

Calibrating external IR sensors Pins

  • Ensure both external sensors are mounted at a consistent, low height.
  • Calibrate on a straight section of the arena with one distinct, clear checkpoint to ensure accurate data sampling.
  • Resulting threshold values for the selected external pins (e.g., A1 and A2) are displayed on the PictoBlox screen immediately after calibration is complete.

Turn the Robot on a Junction 

Turns the robot left or right at a junction, continuing movement until the next black line is detected.

Block: 

Turn the Robot on a Junction

Python:

turnrobotusingir(‘left’,’next’)

Note: You need to set the IR threshold to adjust the robot’s turning behaviour correctly.

Set the Robot’s Turning Speed

Sets the motor speed (voltage supplied to the motors) used during junction turns.

Block:

Set the Robot's Turning Speed

Python: 

setturingspeed(50)

Note: High-speed motors operate only at higher voltages and may not respond to lower speed values. 

Turn On / Off Line Follower LED Feedback

Controls the LED feedback feature of the line follower sensor, enabling or disabling it.

  • When Enabled (ON): The line follower module’s LEDs illuminate when the sensor detects a line (typically black or white).
  • When Disabled (OFF): The LEDs remain off. This can be beneficial for conserving power or preventing light interference in specific scenarios.

Block:

Turn On Off Line Follower LED Feedback

Python: 

linefollow.setLedFeedback(“on”)

Set Forward Coast Delay

Controls the robot’s forward coasting duration (in milliseconds) after detecting a line terminal during a junction turn. The coast delay determines how long the robot continues to move freely after the line terminal is detected. Optimising this value achieves smoother, more precise movement and better control over the stopping distance.

Block: 

Set Forward Coast Delay

Python: 

linefollow.setturningforwarddelay(120)

Note: This block can be used together with the “Turn the robot on a junction” block for best results.

Settings for Two IR Sensors

Note: Two IR sensor line following works with analog values only. For external sensors, ensure the analog output is connected to the analog pins of Quarky (refer to Section 1.1).

Set IR Sensor Pin Connections

Set which two IR sensors to use for line following for left and right sensing, respectively. Select between the onboard Quarky IR sensors (IR-L or IR-R) or external IR sensors by selecting the analog pin to which they are connected.

Block:

Set IR Sensor Pin Connections

Python: 

setlinefollowerparameter(‘left’,’A1′)

Parameter Options Description
Side left, right Which sensor position to configure
Pin IR-L, IR-R, A1, A2, A3 Onboard sensors or an external analog pin

Set IR Threshold

Sets the IR threshold for the onboard or external IR sensor connected to an analog pin of Quarky. Use the “read analog pin” block to get actual sensor values from your arena before setting this.

Block:

Set IR Threshold

Python:

setirthreshold(‘left’,1200)

Settings for Three IR Sensors 

Note: Three IR sensor line following works with digital values only. The threshold is set by the potentiometers on the sensors as they work with digital values. 

Set IR Sensors (Three-Sensor Mode)

Sets the three pins to which the IR sensors are connected for line following — for left, middle, and right sensing, respectively.

Block: 

Set IR Sensors Three Sensor Mode

Python:

setirsensor(“D1″,”D2″,”D3”)

Line Following without Adaptive Feedback

This section contains blocks and Python functions for regular line following without adaptive feedback.

Set Line Following Parameters

The parameters in this block are the same as the regular line following blocks in the Robot Extension of the Quarky board. They define the speeds of motors in different scenarios while following the black line on a white path.

Block:

Set Line Following Parameters

Python:

setlinefollowerparameter(35,40,10)

Parameter Description
F The speed at which the robot moves forward when both sensors detect white
T1 The speed of the forward-moving motor is set to turn the robot along the black line gently
T2 Speed of the reverse-moving motor to turn the robot along the black line gently

Do Line Following Until Checkpoint Detected

The robot performs line following until the specified number of junctions or checkpoints on the line-following arena are detected — that is, until it detects black on both IR sensors simultaneously.

Block: 

Do Line Following Until Checkpoint Detected

Python:

dolinefollowinguntilcrossinglinesdetect()

Note: This block will not work correctly if the robot starts positioned on a junction, as both IR sensors already detect black, and the stop condition triggers immediately.

 Line Following with Adaptive Feedback (PID)

This section contains blocks and Python functions that use adaptive feedback (based on a PID filter) for line following.

Set PID Constant Parameters

Sets the three PID constants  Proportional, Integral, and Derivative — that are used to improve feedback from the IR sensors and refine the robot’s movement.

Block:

Set PID Constant Parameters

Python:

setPIDconstantparameter(0.8,0.01,0.2)

Proportional (Kp) This component enables the robot to make immediate, precise adjustments based on its distance from the line. If the robot drifts too far to the right, it steers left, and if it veers too far to the left, it steers right to correct its course.

Integral (Ki) The integral component monitors how long the robot has been off the line. If the deviation persists for an extended time, this part applies a larger correction to bring the robot back on track more effectively.

Derivative (Kd) The derivative component anticipates future errors by analysing the rate at which the robot drifts from the line. This ensures smooth, predictive adjustments, helping to prevent overshooting and maintain stability.

The control output formula is: 

Control Output = Proportional + Integral + Derivative

So, the PID controller combines these three components to continuously adjust the robot’s movements, keeping it as close to the line as possible while moving smoothly and fast. Think of it as a smart system that balances and corrects itself as it moves, ensuring it follows the line accurately. PID line followers are commonly used in robotics competitions and educational settings to teach about control systems and automation. 

Note: For three IR sensor configurations, use Kp ≥ 7 for better results.

How Does PID Work in Quarky Line Following?

In Quarky’s “Do Line Following” block, the system uses two infrared (IR) sensors — one on the left and one on the right — to navigate along a line.

Sample analog values registered by the sensors:

Condition Left Sensor Right Sensor
On white surface 150 170
On black line 820 750
Robot drifted right 820 (black) 170 (white)

 

When the robot drifts right, the left sensor moves onto the black line while the right remains on white. The error is calculated as: 

Error = (Left Sensor Value − Right Sensor Value) / 10

      = (820 − 170) / 10

      = 65

Proportional Only (Kp = 0.5, Ki = 0, Kd = 0) 

Proportional = Kp × Error

             = 0.5 × 65

             = 32 (approximately)

Control Output = 32 + 0 + 0 = 32

Assuming base speed = 40, minimum speed = 0, maximum speed = 80: 

Left Motor Speed  = Base Speed − Control Output = 40 − 32 = 8

Right Motor Speed = Base Speed + Control Output = 40 + 32 = 72

Proportional and Integral Only (Kp = 0.5, Ki = 0.01, Kd = 0)

Consider a scenario where the robot is stuck, and the tyres are slipping due to excessive weight and a low battery. The integral term accumulates error over time to apply a growing correction.

Iteration 1:

I = I + Error = 0 + 65 = 65

Control Output = (0.5 × 65) + (0.01 × 65) + 0 = 32.65

Left Motor Speed  = 7.35

Right Motor Speed = 72.65

Iteration 2: 

I = 65 + 65 = 130

Control Output = (0.5 × 65) + (0.01 × 130) + 0 = 33.3

Left Motor Speed  = 6.7

Right Motor Speed = 73.3

Proportional, Integral, and Derivative (Kp = 0.5, Ki = 0.01, Kd = 0.2) 

In a small arena requiring a significant turn or U-turn, increasing Kp alone may cause excessive oscillations. 

If the error increases to 76:

D = Previous Error − Current Error = 65 − 76 = −11

Control Output = (0.5 × 76) + (0.01 × 76) + (0.2 × −11)

               = 38 + 0.76 − 2.2

               = 36.56

Left Motor Speed  = 3.44

Right Motor Speed = 76.56

In the next loop, with a new error of 43: 

I = 76 + 43 = 109

D = 76 − 43 = 33

Control Output = (0.5 × 43) + (0.01 × 109) + (0.2 × 33)

               = 21.5 + 1.09 + 6.6

               = 29.19

Left Motor Speed  = 10.81

Right Motor Speed = 69.19

This iterative process continues, with the derivative term helping to manage the robot’s response to changing errors and ultimately enhancing its stability. 

Set PID Speed Parameters

Defines the motor speed range used during PID-controlled line following.

Block:

Set PID Speed Parameters

Python:

setPIDspeedparameter(40,0,80)

Parameter Description
Base Speed The speed during line following
Min Speed The minimum motor speed when the robot is turning
Max Speed The maximum motor speed when the robot turns, and the black line is detected

Do Line Following with PID Until Checkpoint

Initiates line following using the PID algorithm and stops the robot when the specified number of crossing lines or checkpoints is detected. You must set the IR threshold to stop the robot at the crossing lines. To start line following, both PID speed and constant parameters must be set first.

Block:

Do Line Following with PID Until Checkpoint

Python:

dolinefollowingwithPID()

Notes:

  • Set the correct IR threshold for the sensor for better stopping accuracy.
  • If the robot does not stop at crossing lines or does not follow the line, consider aligning the external IR sensor parallel to the caster wheel.
  • Experiment with PID speed parameters and overall speed values to achieve better line following.
  • Ensure the external IR sensor is not placed too high or too low from the ground, and calibrate on the actual arena.

The extension helps you to speed up your quarky in the line following using blocks and Python functions.

Read More

PictoBlox Blocks

Configure Quarky for advanced line following by connecting two IR sensors to analog pins or three IR sensors to digital pins for optimal alignment and control.
This block makes the robot move forward and reverse for a set time, collecting IR sensor data on both white and black lines.
This block moves the robot forward and backward for a set time, collecting IR sensor data on both white and black lines.
This block allows you to adjust the robot’s turning speed.
Select left and right IR sensors for line following using either onboard or external sensors connected to analog pins.
Set the IR threshold for onboard or external sensors connected to Quarky’s analog pin.
Set the pins for the left, middle, and right IR sensors to enable line following. Note: For 3 IR Sensors, the threshold is set by the potentiometers on the sensors as they work with digital value.
This block sets motor speeds for Quarky’s line following, defining forward and turning speeds for tracking a black line on a white path.
This block helps robot to follows a line and stops at checkpoints, detecting these when both IR sensors sense black simultaneously.
PID functions use sensor feedback to improve the robot’s movement accuracy and stability.
This block is use for set the motor speed while doing the line following and Turning.
The robot uses a PID algorithm for line following, stopping at a crossing line when the IR threshold is met; set PID speed, constants, and adjust parameters for optimal performance.
All articles loaded
No more articles to load

Block Coding Examples

All articles loaded
No more articles to load

Python Functions

This function ensures proper sensor alignment for precise line-following control.
Syntax: AdvanceLineFollowing(ir_num = 2)
This function moves the robot forward and reverse for a set time, collecting IR sensor data on white and black lines.
Syntax: calibrateir()
This function allows the robot to turn left or right to follow the next black line by setting the IR threshold and adjusting turn speed.
Syntax: turnrobotusingir(turn_direction = ‘left’, next_line = ‘next’)
This function sets the motor speed for the robot’s turns.
Syntax: setturingspeed(turn_speed = 50)
Select the left and right IR sensors for line following, choosing between onboard Quarky IR sensors or external sensors connected to analog pins.
Syntax: setlinefollowerparameter(set_sensor = ‘left’, set_pin = ‘A1’)
This function sets the IR threshold for either the default sensor or one connected to an analog pin.
Syntax: setirthreshold(set_sensor = ‘left’, set_value = 750)
This function sets the three pins for IR sensors used in line following for left, middle, and right detection.
Syntax: setirsensor(set_left = ‘D1’, set_middle = ‘D2’, set_right = ‘D3’)
This function’s parameters match the Quarky board’s regular line following settings, defining motor speeds: F for forward on white, and T1 and T2 for gentle turning along the black line.
Syntax: setlinefollowerparameter(F = 35, T1 = 40, T2 = 10)
This function enables the robot to follow a line continuously until it detects a checkpoint (both IR sensors on black); it stops only at the checkpoint or with a reset.
Syntax: dolinefollowinguntilcheckpointdetect(next_checkpoint = 1)
PID control of line follower is a method consisting of proportional, integral, and derivative functions to improve the robot’s movement. 
Syntax: setPIDconstantparameter(kp = 0.8, ki = 0.01, kd = 0.2)
This function is used for set the Base Speed, Min Speed and Max Speed of motors for line following and turn.
Syntax: setPIDspeedparameter(base = 40, min = 0, max = 80)
This function starts line following with the PID algorithm, stopping at a checkpoint based on the IR threshold; set PID speed, constants, and adjust parameters for best performance.
Syntax: dolinefollowingwithPID(next_checkpoint = 1)
All articles loaded
No more articles to load
Table of Contents