0% found this document useful (0 votes)
12 views5 pages

Arduinonanofuzzypid

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

Arduinonanofuzzypid

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <QTRSensors.

h>

#include <PID_v1.h>

// Motor driver pins

Const int PWM_A = 5;

Const int PWM_B = 10;

Const int IN1 = 6;

Const int IN2 = 7;

Const int IN3 = 9;

Const int IN4 = 8;

Const int stby = 12;

#define Sensorcount 8

#define EMITTER_PIN 13

Const int IndicatorLed = 13;

// QTR-8RC sensor array

QTRSensorsRC qtrrc((unsigned char[]) {A0, A1, A2, A3, A4, A5, 3, 4},
Sensorcount, EMITTER_PIN);

Unsigned int sensorValues[8];

// PID constants

Double Kp = 1.0, Ki = 0.0, Kd = 0.0;

Double setpoint = 3500; // Middle of the line

Double input, output;


// PID object

PID myPID(&input, &output, &setpoint, Kp, Ki, Kd, DIRECT);

Void setup() {

// Motor pins

pinMode(IndicatorLed, OUTPUT);

pinMode(PWM_A, OUTPUT);

pinMode(PWM_B, OUTPUT);

digitalWrite(IndicatorLed, HIGH);

pinMode(IN1, OUTPUT);

pinMode(IN2, OUTPUT);

pinMode(IN3, OUTPUT);

pinMode(IN4, OUTPUT);

pinMode(stby,OUTPUT);

digitalWrite(stby, HIGH);

// Initialize PID

For (int I = 0; I < 200; i++)

Delay(5);

myPID.SetMode(AUTOMATIC);

myPID.SetOutputLimits(-255, 255);
}

Void loop() {

// Read sensor values and compute line position

digitalWrite(stby,HIGH);

int position = qtrrc.readLine(sensorValues);

// Calculate error

Input = position;

// Run PID loop

myPID.Compute();

// Fuzzy adjustments to PID parameters (example logic)

If (abs(setpoint – input) < 100) {

// Line is close to the center

Kp = 0.5;

Ki = 0.2;

Kd = 0.1;

} else if (abs(setpoint – input) < 500) {

// Line is moderately off-center

Kp = 1.0;

Ki = 0.5;

Kd = 0.3;

} else {

// Line is far off-center

Kp = 2.0;
Ki = 1.0;

Kd = 0.5;

// Adjust PID parameters based on fuzzy logic

myPID.SetTunings(Kp, Ki, Kd);

// Control motors based on PID output

Int motorSpeedA = 255 – output;

Int motorSpeedB = 255 + output;

// Ensure motor speeds are within bounds

motorSpeedA = constrain(motorSpeedA, 0, 255);

motorSpeedB = constrain(motorSpeedB, 0, 255);

// Set motor directions

If (motorSpeedA > 0) {

analogWrite(PWM_A, motorSpeedA);

digitalWrite(IN1, HIGH);

digitalWrite(IN2, LOW);

} else {

analogWrite(PWM_A, -motorSpeedA);

digitalWrite(IN1, LOW);

digitalWrite(IN2, HIGH);

If (motorSpeedB > 0) {
analogWrite(PWM_B, motorSpeedB);

digitalWrite(IN3, HIGH);

digitalWrite(IN4, LOW);

} else {

analogWrite(PWM_B, -motorSpeedB);

digitalWrite(IN3, LOW);

digitalWrite(IN4, HIGH);

You might also like