Home TUTORIALSArduino Tutorials for Beginners LINE FOLLOW 8X – PID

LINE FOLLOW 8X – PID

by Dileepa Jayawardena
4 comments 9410 views

Line Following Robot කෙනෙක් මුල සිට BeeIR 8X භාවිතයෙන් නිර්මාණය කිරිම පිලිබඳ මේ වීඩියෝවේදී කතා කරනවා.BeeIR 8X 3ක් දිනාගන්නත් අවස්ථාවක්.

0

0

#define M1   4
#define M1pwm  5
#define M2 7
#define M2pwm 6

void setup() {
  pinMode(M1, OUTPUT);
  pinMode(M1pwm, OUTPUT);
  pinMode(M2, OUTPUT);
  pinMode(M2pwm, OUTPUT);
}

void loop() {
  mdrive(255, 255);
}

void mdrive(int m1, int m2) {
  if (m1 > 0) {
    if (m1 > 255) {
      m1 = 255;
    }
    digitalWrite(M1, HIGH);
    analogWrite(M1pwm, 255 - m1);
  } else {
    if (m1 < -255) {
      m1 = -255;
    }
    digitalWrite(M1, LOW);
    analogWrite(M1pwm, m1 * -1);
  }

  if (m2 > 0) {
    if (m2 > 255) {
      m2 = 255;
    }
    digitalWrite(M2, HIGH);
    analogWrite(M2pwm, 255 - m2);
  } else {
    if (m2 < -255) {
      m2 = -255;
    }
    digitalWrite(M2, LOW);
    analogWrite(M2pwm, m2 * -1);
  }
}
MotorTesting.inoDownload (0.8 KB)

BeeLineSensorPro Libarary Download from github

#include 
BeeLineSensorPro sensor = BeeLineSensorPro((unsigned char[]) {
  A0, A1, A2, A3, A4, A5, A6, A7
}, LINE_BLACK);
void setup() {
  Serial.begin(115200);
  Serial.println("Calibrating ON");
  for (int i = 0; i < 100; i++) {
    sensor.calibrate();
  }
  Serial.println("Calibrating Completed");
}

void loop() {
   int err = sensor.readSensor();
  Serial.println(err);
}
SensorTesting.inoDownload (0.4 KB)

0

/*
LINE FOLLOW 8X
by Dileepa Jayawardena
myhub.lk
*/

#include 
#define M1 4
#define M1pwm 5
#define M2 7
#define M2pwm 6

BeeLineSensorPro sensor = BeeLineSensorPro((unsigned char[]) {
  A0, A1, A2, A3, A4, A5, A6, A7
}, LINE_BLACK);

void setup() {
  Serial.begin(115200);
  pinMode(M1, OUTPUT);
  pinMode(M1pwm, OUTPUT);
  pinMode(M2, OUTPUT);
  pinMode(M2pwm, OUTPUT);

  for (int i = 0; i < 50; i++) {
    sensor.calibrate();
    mdrive(-120, 120);
  }
  mdrive(0, 0);
  delay(100);

  for (int i = 0; i < 100; i++) {
    sensor.calibrate();
    mdrive(120, -120);
  }
  mdrive(0, 0);
  delay(100);
  for (int i = 0; i < 40; i++) {
    sensor.calibrate();
    mdrive(-120, 120);
  }
  mdrive(0, 0);
  delay(1000);

}

float kP = 0.2;
float kD = 0.8;
int last_value;
void loop() {
  int err = sensor.readSensor();
  Serial.println(err);
  int m1 =120;
  int m2 = 120;

  int diff = err * kP + (err - last_value) * kD;
  //int diff=err*kP;
  last_value = err;

  mdrive(m1 - diff, m2+ diff);
}

void mdrive(int m1, int m2) {
  if (m1 > 0) {
    if (m1 > 255) {
      m1 = 255;
    }
    digitalWrite(M1, HIGH);
    analogWrite(M1pwm, 255 - m1);
  } else {
    if (m1 < -255) {
      m1 = -255;
    }
    digitalWrite(M1, LOW);
    analogWrite(M1pwm, m1 * -1);
  }

  if (m2 > 0) {
    if (m2 > 255) {
      m2 = 255;
    }
    digitalWrite(M2, HIGH);
    analogWrite(M2pwm, 255 - m2);
  } else {
    if (m2 < -255) {
      m2 = -255;
    }
    digitalWrite(M2, LOW);
    analogWrite(M2pwm, m2 * -1);
  }
}
LineFollower8X.inoDownload (1.6 KB)

0

Related Posts

4 comments

Niroshi December 20, 2019 - 3:22 pm

Oya ge web site eka wada karanna eka update karannako

Reply
Niroshi December 20, 2019 - 3:23 pm

Sensor testing eka donload wennane

Reply
Niroshi December 23, 2019 - 8:41 am

Aiya me robo ge chasi ekka ganna barida

Reply
admin January 31, 2020 - 9:30 am

මෙතනින් ගන්න පුලුවන්
https://www.duino.lk/Robotics/Beedroid-DIY-Smart-Robot-Car-Kit

Reply

Leave a Comment