Sale!

ESP8266 – QTR-8RC – PID Line Follower Robot : Code and Project Files

2,999.00

ESP8266 Line Follower Robot

SKU: CODE-PID-LFR-BOT-ESP8266 Categories: ,

Description

Specification of PID Line Follower Code

  • Algorithm: Proportional Integral Differential Algorithm (PID)
  • Microcontroller Supported: ESP8266
  • Sensor Support: QTR-8RC Sensor
  • Special Feature: Auto Sensor Calibration, PWM Based Motor Control for Smooth Operation
  • IDE Support: Arduino IDE Project
  • Motor Driver Support: Dual Channel Motor Driver Support e.g. L293D, L298, TB6612FNG
  • Motor Speed Support: Maximum 400RPM at 4cm Wheel Diameter
  • Maximum Robot Speed: 0.5 meters per second [m/s] 
  • Tutorial: Step By Step Guide

Code / Software Description

The code for the ESP8266 Line Follower Robot is available in Arduino IDE.

The firmware project has three library files esp8266_qtr.h , esp8266_motor.h and one main.c file. The library file code is written in C. The library files are all well documented. The APIs exposed can be used in Arduino IDE. The programming language used in C.

/** @name QTR_CONTROL
    Public Functions to Control QTR Sensor
*/
void QTR_Init(uint8_t *SensorPin, uint8_t EmitterPin);
void QTR_DeInit();
void QTR_Calibrate(uint16_t *CalibratedMinimum, uint16_t *CalibratedMaximum, uint8_t ReadMode);
void QTR_CalibrateSensor(uint8_t ReadMode);
void QTR_ResetCalibration();
void QTR_ReadSensor(uint16_t *SensorValues, uint8_t ReadMode);
void QTR_ReadRaw(uint16_t *SensorValues);
void QTR_ReadCalibrated(uint16_t *SensorValues, uint8_t ReadMode);
uint16_t QTR_ReadLine(uint16_t *SensorValues, uint8_t ReadMode);
inline void QTR_EmitterOn();
inline void QTR_EmitterOff();
/**@}*/
/** @name MOTOR_CONTROL
    Public Functions to Control Motor
*/
/**@{*/
void Motor_Init(void);
void Motor_DeInit(void);
void Motor_SetSpeed(int16_t M1Speed, int16_t M2Speed);
void Motor_SetSpeedM1(int16_t M1Speed);
void Motor_SetSpeedM2(int16_t M2Speed);
/**@}*/

PID Line Follower Robot Code Walkthrough

The first few lines of code declare all the variables needed for the PID algorithm.

uint16_t LFR_SensorValue[QTR_SENSOR_COUNT];       /**< Array to Save Raw IR Sensor values of QTR-8RC */
uint16_t LFR_Position = 0;         /**< Variable to Save the QTR-8RC Sensor Position */
int16_t  LFR_Proportional = 0;     /**< Variable to Save the Proportional Output of PID Control Algorithm */
int16_t  LFR_LastProportional = 0; /**< Variable to Save the Previous Proportional Output of PID Control Algorithm */
int16_t  LFR_Derivative = 0;       /**< Variable to Save the Derivative Output of PID Control Algorithm */
int64_t  LFR_Integral = 0;         /**< Variable to Save the Integral Output of PID Control Algorithm */
int16_t  LFR_ControlOutput = 0;    /**< Variable to Save the Final Control Output of PID Control Algorithm */

uint8_t  Kd = 7;
uint8_t  Kp = 2;
uint16_t Ki = 16000;

uint16_t Speed = LFR_MAX_MOTOR_SPEED;

The function “LFR_Initialize()” initializes all used peripherals of the ESP8266. The QTR_X_PIN macros are defined in the esp8266_qtr.h file.

void LFR_Initialize()
{
  uint8_t QTR_Pins[] = {QTR_1_PIN, QTR_2_PIN, QTR_3_PIN, QTR_4_PIN, QTR_5_PIN, QTR_6_PIN};
  QTR_Init(QTR_Pins, QTR_EMITTER_PIN);    /**< Initializes the QTR-8RC Sensor */
  Motor_Init();                           /**< Initializes the Motors */
  delay(2000);                            /**< Pause ; Useful to Align the Robot Manually if Outside the Line */
}

The function “LFR_Calibrate()” calibrates the QTR-8RC line sensor. The robot rotates in the same place and calibrates the sensor with all possible sensor values sampled during this time.

void LFR_Calibrate()
{
  Motor_SetSpeed(90, -90);         /**< Rotates the Robot 90, -90 300RPM */
  for (uint8_t i = 0; i < 40; i++) /**< Calibrate the QTR-8RC Sensor */
  {
    QTR_CalibrateSensor(QTR_EMITTERS_ON);
    delay(20);
  }
  Motor_SetSpeed(0, 0);            /**< Stops the Robot */
  delay(500);
  Motor_SetSpeed(-90, 90);         /**< Rotates the Robot */
  for (uint8_t i = 0; i < 80; i++) /**< Calibrate the QTR-8RC Sensor */
  {
    QTR_CalibrateSensor(QTR_EMITTERS_ON);
    delay(20);
  }
  Motor_SetSpeed(0, 0);            /**< Stops the Robot */
  delay(500);
  Motor_SetSpeed(90, -90);         /**< Rotates the Robot */
  for (uint8_t i = 0; i < 40; i++) /**< Calibrate the QTR-8RC Sensor */
  {
    QTR_CalibrateSensor(QTR_EMITTERS_ON);
    delay(20);
  }
  Motor_SetSpeed(0, 0);         /**< Stops the Robot */
  delay(2000);                  /**< Pause ; Useful to Realign the Robot Manually if Outside the Line */
}

The infinite while loop below is responsible for the PID algorithm. The current gains Kp=1/2, Ki=1/16000, and Kd=7 should make the Line Follower Robot work out of the box if a similar construction is done. It is good to note that PID coefficients depend heavily on physical parameters like the size of the robot, weight, proportions, battery voltage, friction, etc. It is always advised to tune your robot after any hardware change.

void loop()
{
  LFR_Position = QTR_ReadLine(LFR_SensorValue, QTR_EMITTERS_ON);    /**< Reads the QTR-8RC Line Sensor to Get the Line Position */

  LFR_Proportional = LFR_Position - QTR_LINE_MID_VALUE;             /**< Computes the Proportional Output of PID Control Algorithm */
  LFR_Derivative = LFR_Proportional - LFR_LastProportional;         /**< Computes the Derivative Output of PID Control Algorithm */
  LFR_Integral += LFR_Proportional;                                 /**< Computes the Integral Output of PID Control Algorithm */
  LFR_LastProportional = LFR_Proportional;                          /**< Saves the Old Proportional Output of PID Control Algorithm */

  LFR_ControlOutput = LFR_Proportional / Kp + LFR_Integral / Ki + LFR_Derivative * Kd; /**< Computes the Final Control Output of PID Control Algorithm 300RPM*/

  if (LFR_ControlOutput > Speed)
  {
    LFR_ControlOutput = Speed;    /**< Keeps The Motor Speed in Limit */
  }
  if (LFR_ControlOutput < -Speed)
  {
    LFR_ControlOutput = -Speed;    /**< Keeps The Motor Speed in Limit */
  }

  if (LFR_ControlOutput < 0)
  {
    Motor_SetSpeed(Speed + LFR_ControlOutput, Speed);    /**< Drives the Motor According to the Control Output */
  }
  else
  {
    Motor_SetSpeed(Speed, Speed - LFR_ControlOutput);    /**< Drives the Motor According to the Control Output */
  }
}

The speed of the line follower robot is controlled by the macro LFR_MAX_MOTOR_SPEED . It is advised to recalibrate the robot at a higher speed for optimum performance.

#define LFR_MAX_MOTOR_SPEED 125    /**< Sets the Maximum PWM Duty Cycle for Line Follower Robot 0=0% 255=100% */

Programming The ESP8266 Line Follower Robot

The project folder has all the required files. If you use Arduino IDE then open the Arduino project folder and flash the ESP8266 from Arduino IDE.

ESP8266 Line Follower Robot V1 Project Structure
ESP8266 Line Follower Robot V1 Project Structure

 

 

Reviews

There are no reviews yet.

Be the first to review “ESP8266 – QTR-8RC – PID Line Follower Robot : Code and Project Files”

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.