PID LINE FOLLOWER ROBOT USING ARDUINO NANO
Most mechatronics and robotics enthusiasts will begin their robotics journey by developing a line follower robot. Because line follower robots are simple to construct and fun to watch. Since high school, I've been constructing line follower robots. To increase speed and stability, I experimented with various designs. Later on, I understood and learned that speed and stability are dependent not only on the design, but also on the algorithm. I'll show you how I used the PID algorithm to design and build my first line follower robot. So let's get this party started.
Arduino Nano
QTR 8 RC
DRV8835
N20 motors and clamp *2
AMS 1117 5.0 regulator*1
button switch*2
0805 SMD led*2
0805 1k resistor *2
0805 10k resistor *2
7.4volt battery
castor wheel
Most mechatronics and robotics enthusiasts will begin their robotics journey by developing a line follower robot. Because line follower robots are simple to construct and fun to watch. Since high school, I've been constructing line follower robots. To increase speed and stability, I experimented with various designs. Later on, I understood and learned that speed and stability are dependent not only on the design, but also on the algorithm. I'll show you how I used the PID algorithm to design and build my first line follower robot. So let's get th
is party started.
As i said earlier I am using the QTR sensor and PID algorithm. so in the program first I included the qtr8rc library of Pololu.
#include <QTRSensors.h>
Then with the help of the QTR library functions, i defined the sensors and other parameters
QTRSensors qtr;
const uint8_t SensorCount = 8;
uint16_t sensorValues[SensorCount];
Then I defined the ki, kp, kd constants. We should find the values of the constants by the trial and error method.
float Kp = 0;
float Ki = 0;
float Kd = 0;
int P;
int I;
int D;
int lastError = 0;
boolean onoff = false;
Next, I defined constant for defining the maximum and minimum speed of the robot. Also, I defined the pins for the motor driver and button
int mode = 8;
int aphase = 9;
int aenbl = 6;
int bphase = 5;
int benbl = 3;
int buttoncalibrate = 17
int buttonstart = 2;
In the setup section, I defined the pin modes and added the calibration programme
Serial.begin(9600);
qtr.setTypeRC();
qtr.setSensorPins((const uint8_t[]){10, 11, 12, 14, 15, 16, 18, 19}, SensorCount);
qtr.setEmitterPin(7);
pinMode(mode, OUTPUT);
pinMode(aphase, OUTPUT);
pinMode(aenbl, OUTPUT);
pinMode(bphase, OUTPUT);
pinMode(benbl, OUTPUT);
digitalWrite(mode, HIGH);
delay(500);
pinMode(LED_BUILTIN, OUTPUT);
boolean Ok = false;
while (Ok == false) {
if(digitalRead(buttoncalibrate) == HIGH)
{
calibration();
Ok = true;
}
}
forward_brake(0, 0);
}
void calibration() {
digitalWrite(LED_BUILTIN, HIGH);
for (uint16_t i = 0; i < 400; i++)
{
qtr.calibrate();
}
digitalWrite(LED_BUILTIN, LOW);
}
Finally in the loop section
I read the values of the qtr 8rc sensor and driver motors according to the readings of the qtr sensors.
if(digitalRead(buttonstart) == HIGH) {
onoff =! onoff;
if(onoff = true) {
delay(1000);
}
else {
delay(50);
}
}
if (onoff == true) {
PID_control();
}
else {
forward_brake(0,0);
}
}
void forward_brake(int posa, int posb) {
digitalWrite(aphase, LOW);
digitalWrite(bphase, HIGH);
analogWrite(aenbl, posa);
analogWrite(benbl, posb);
}
void PID_control() {
uint16_t position = qtr.readLineBlack(sensorValues);
int error = 3500 - position;
P = error;
I = I + error;
D = error - lastError;
lastError = error;
int motorspeed = P*Kp + I*Ki + D*Kd;
int motorspeeda = basespeeda + motorspeed;
int motorspeedb = basespeedb - motorspeed;
if (motorspeeda > maxspeeda) {
motorspeeda = maxspeeda;
}
if (motorspeeda < 0) {
motorspeeda = 0;
}
if (motorspeedb < 0) {
motorspeedb = 0;
}
forward_brake(motorspeeda, motorspeedb);
}
Aucun commentaire