Line_Following-2022/pid-example/pid-example.ino

122 lines
2.7 KiB
Arduino
Raw Normal View History

2022-06-01 12:50:06 +03:00
//Make sure to install the library
#include <QTRSensors.h>
QTRSensors qtr;
const uint8_t SensorCount = 8;
uint16_t sensorValues[SensorCount];
2022-06-25 00:15:37 +03:00
float Kp = 0.07;//set up the constants value
float Ki = 0.0008;
float Kd = 0.34;
2022-06-01 12:50:06 +03:00
int P;
int I;
int D;
int lastError = 0;
boolean onoff = false;
//Increasing the maxspeed can damage the motors - at a value of 255 the 6V motors will receive 7,4 V
const uint8_t maxspeeda = 200;
const uint8_t maxspeedb = 200;
const uint8_t basespeeda = 170;
const uint8_t basespeedb = 170;
2022-06-01 12:50:06 +03:00
//Set up the drive motor carrier pins
int mode = 8;
2022-06-24 23:25:55 +03:00
int aphase = 5;
int aenbl = 3;
int bphase = 9;
int benbl = 6;
2022-06-01 12:50:06 +03:00
//Set up the buttons pins
int buttoncalibrate = 17; //pin A3
int buttonstart = 2;
void setup() {
Serial.begin(9600);
qtr.setTypeRC();
//Set up the sensor array pins
qtr.setSensorPins((const uint8_t[]){10, 11, 12, 14, 15, 16, 18, 19}, SensorCount);
2022-06-01 12:50:06 +03:00
qtr.setEmitterPin(7);//LEDON PIN
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) { //the loop won't start until the robot is calibrated
if(digitalRead(buttoncalibrate) == HIGH) {
2022-06-01 12:50:06 +03:00
calibration(); //calibrate the robot for 10 seconds
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);
}
void loop() {
if(digitalRead(buttonstart) == HIGH) {
onoff =! onoff;
if(onoff = true) {
2022-06-01 12:50:06 +03:00
delay(1000);//a delay when the robot starts
}
else {
delay(50);
}
}
if (onoff == true) {
PID_control();
}
else {
forward_brake(0,0);
2022-06-01 12:50:06 +03:00
}
}
void forward_brake(int posa, int posb) {
//set the appropriate values for aphase and bphase so that the robot goes straight
2023-07-01 00:03:56 +03:00
digitalWrite(aphase, HIGH);
digitalWrite(bphase, LOW );
2022-06-01 12:50:06 +03:00
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;
2022-06-01 12:50:06 +03:00
int motorspeeda = basespeeda + motorspeed;
int motorspeedb = basespeedb - motorspeed;
2022-06-01 12:50:06 +03:00
if (motorspeeda > maxspeeda) {
motorspeeda = maxspeeda;
}
if (motorspeedb > maxspeedb) {
motorspeedb = maxspeedb;
}
if (motorspeeda < 0) {
motorspeeda = 0;
}
if (motorspeedb < 0) {
motorspeedb = 0;
}
//Serial.print(motorspeeda);Serial.print(" ");Serial.println(motorspeedb);
2022-06-01 12:50:06 +03:00
forward_brake(motorspeeda, motorspeedb);
}