akssil-ga
Published © TAPR-OHL

Developing a Line Following Robot with AI-enabled Tasks

Line-following robot with AI smarts—Wameedh Scientific Club's Arduino and Raspberry Pi combo redefines robotics. Join the revolution!

AdvancedFull instructions providedOver 3 days379
Developing a Line Following Robot with AI-enabled Tasks

Things used in this project

Story

Read more

Schematics

schematic diagram

it will be provided as soon as possible

Code

python code

Python
##
##
import cv2
import RPi.GPIO as GPIO
from PIL import Image
from util import get_limits
GPIO.setmode(GPIO.BCM) 

Insan = 17
Voiture =27
GPIO.setup(Insan, GPIO.OUT)
GPIO.setup(Voiture, GPIO.OUT)

rpin = 13
bpin = 5
gpin = 6

GPIO.setmode(GPIO.BCM)
GPIO.setup(gpin, GPIO.OUT) 
GPIO.setup(rpin, GPIO.OUT)
GPIO.setup(bpin, GPIO.OUT)

green = [102, 172, 46]  
red = [42, 51, 230]
blue = [224, 169, 53]

objet = cv2.VideoCapture(0)
couleur = cv2.VideoCapture(1)

classNames = []
classFile = "coco.names"
with open(classFile,"rt") as f:
    classNames = f.read().rstrip("\n").split("\n")

configPath = "ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt"
weightsPath = "frozen_inference_graph.pb"

net = cv2.dnn_DetectionModel(weightsPath,configPath)
net.setInputSize(320,320)
net.setInputScale(1.0/ 127.5)
net.setInputMean((127.5, 127.5, 127.5))
net.setInputSwapRB(True)


def getObjects(img, thres, nms, draw=True, objects=[]):
    classIds, confs, bbox = net.detect(img,confThreshold=thres,nmsThreshold=nms)
    #print(classIds,bbox)
    if len(objects) == 0: objects = classNames
    objectInfo =[]
    if len(classIds) != 0:
        for classId, confidence,box in zip(classIds.flatten(),confs.flatten(),bbox):
            className = classNames[classId - 1]
            if className in objects:
                objectInfo.append([box,className])
                if (draw):
                    largeur = box[2] 
                    longueur = box[3] 
                    area = longueur*largeur
                    if area>0:
                        if classNames[classId-1] == 'person':
                            print('person')
                            GPIO.output(Insan, GPIO.HIGH)
                        elif classNames[classId-1] == 'car' or classNames[classId-1] == 'truck':
                            print('car')
                            GPIO.output(Voiture, GPIO.HIGH)
                        

    return img,objectInfo

def detect_color(photo, lawen, pin, koki):
    hsvImage = cv2.cvtColor(photo, cv2.COLOR_BGR2HSV)
    lowerLimit, upperLimit = get_limits(color=lawen)
    mask = cv2.inRange(hsvImage, lowerLimit, upperLimit)
    mask_ = Image.fromarray(mask)
    bbox = mask_.getbbox()
    if bbox is not None:
        print(koki)
        GPIO.output(pin, GPIO.HIGH)
    else :
        GPIO.output(pin, GPIO.LOW) 
    
          
if _name_ == "_main_":

    objet.set(3,640)
    objet.set(4,480)
  
    while True:
        success, img = objet.read()
        result, objectInfo = getObjects(img,0.38,0.2, objects=['truck','person','car'])
        if objectInfo:
            pass
        else:
            GPIO.output(Insan, GPIO.LOW)
            GPIO.output(Voiture, GPIO.LOW) 
                
        ret, photo = couleur.read()
        detect_color(photo, blue,bpin, "azre9")
        detect_color(photo, green,gpin, "a5der")
        detect_color(photo, red, rpin, "a7mer")           
           
        cv2.imshow('photo', photo) 
        cv2.imshow("Output",img)
        key = cv2.waitKey(1)
        if key==ord('q'):
            break
objet.release()
couleur.release()
cv2.destroyAllWindows()

Arduino code

Arduino
unsigned long currentTime;
unsigned long startTime;
const int motor1SpeedPin = 5;
const int motor1DirectionPin1 = 3;
const int motor1DirectionPin2 = 4;  
const int motor2SpeedPin = 9;    
const int motor2DirectionPin1 = 2;  
const int motor2DirectionPin2 = 7;

#define middleIR A3
#define rightIR A4
#define ext_rightIR A5
#define leftIR A2
#define ext_leftIR A1
//int akssil;

int speedright = 100;  // is two
int speedleft =110; // is one
int speedrighturn_1 = 150;
int speedlefturn_1 =180;
int speedrighturn_2 = 150;
int speedlefturn_2 =180;
int speedextrighturn = 150;
int speedextlefturn =180;


int last_value;
int t;

const int blue_pin = A0; //17 blue
const int car_pin = 12; //27 voiture
const int green_pin = 13; // 6 green
const int red_pin = 8; //13 red 

int wekt =100 ;
int bdl = 20;
int check_green=0;
int check_red=0;
int check_car=0;

void setup() {
  t=10;
  last_value=1;
  Serial.begin(9600);
  pinMode(motor1SpeedPin, OUTPUT);
  pinMode(motor1DirectionPin1, OUTPUT);
  pinMode(motor1DirectionPin2, OUTPUT);
  pinMode(motor2SpeedPin, OUTPUT);
  pinMode(motor2DirectionPin1, OUTPUT);
  pinMode(motor2DirectionPin2, OUTPUT);
  

  pinMode(middleIR, INPUT);
  pinMode(rightIR, INPUT);
  pinMode(leftIR, INPUT);
  pinMode(ext_rightIR, INPUT);
  pinMode(ext_leftIR, INPUT);
  pinMode(car_pin, INPUT);
  pinMode(green_pin, INPUT);
  pinMode(blue_pin, INPUT);
  pinMode(red_pin, INPUT);

  demarage();

 startTime = millis();
}

void loop() {
  linefollower();
  make_decisions();
  }
void linefollower() {
  bool middle = digitalRead(middleIR);
  bool left = digitalRead(leftIR);
  bool right = digitalRead(rightIR);
  bool ext_left = digitalRead(ext_leftIR);
  bool ext_right = digitalRead(ext_rightIR);
  
  if (!ext_right && !right && middle && !left && !ext_left) {
    last_value = 1;
    forwardD();
  } else if (right && middle && left ) {
    last_value = -1;
    stopDef();
  }
  
  else if (ext_right && right && !left && !ext_left) {
    //turnleft
    last_value= 30;
    sharpturnL();
  } else if (!ext_right && !right && left && ext_left) {
    //turnright
    last_value= -30;
    sharpturnR();
  }

  else if (ext_right && !right && !middle && !left && !ext_left) {
    last_value=20;
    left_turn_2();
  } else if (!ext_right && !right && !middle && !left && ext_left) {
    last_value=-20;
    right_turn_2();
  } 

  else if (!ext_right && right && !middle && !left && !ext_left) {
    last_value=10;
    left_turn_1();
  } else if (!ext_right && !right && !middle && left && !ext_left) {
    last_value=-10;
    right_turn_1();
  }

  else if (ext_right && right && middle && left && ext_left) {
  
    if (last_value == 1){
        forwardD();
        delay(t);
        last_value=1;
    }else if(last_value == -1){
        stopDef();
        delay(t);
        last_value=1;
    }else if(last_value == 10){
        left_turn_1();
        delay(t);
        last_value=1;
    }else if (last_value == -10){
        right_turn_1();
        delay(t);
        last_value=1;
    }else if(last_value == 20){
        left_turn_2();
        delay(t);
        last_value=1;
    }else if(last_value == -20){
        right_turn_2();
        delay(t);
        last_value=1;
    }else if(last_value == 30){
        sharpturnL();
        delay(t);
        last_value=1;
    }else if(last_value == -30){
        sharpturnL();
        delay(t);
        last_value=1;
    }
  }

  
}
void make_decisions(){

  bool car = digitalRead(car_pin);
  bool red = digitalRead(red_pin);
  bool green = digitalRead(green_pin);
  bool blue = digitalRead(blue_pin); 

  if(car){
   if(check_car==0){
      asber();
      delay(2000);
      demarage();
      last_value=1;
      check_car = 1;
      //déchargé
      //we9t li lzm
    }else {
      check_car = 1;
      //madir wlo
      last_value=1;
      
    }
  }
  if(red){
 
    if(check_red==0){
      asber();
      delay(5000);
      //demarage();
      //last_value=1;
      check_red = 1;
      //déchargé
      //we9t li lzm
    }
       else {
      check_red = 1;
      

      
    }
  }
  if(blue){
    currentTime = millis(); 
    unsigned long elapsedTime = currentTime - startTime;
    if (elapsedTime>=4000){
      asber();
      delay(2000);
     
      startTime = currentTime;
      //last_value=1;
      blue=0;
    }
  }
  if(green){
     if (check_green == 0) {
      asber();
      delay(5000);
      demarage();
      //last_value=1;
      delay(200);
      asber();
      delay(3000);
      check_green = 1;
      //chargé
    }
    else{
      check_green = 1;
    }
  }



} 


void forwardD() {
  analogWrite(motor1SpeedPin, speedleft);
  digitalWrite(motor1DirectionPin1, HIGH);
  digitalWrite(motor1DirectionPin2, LOW);

  analogWrite(motor2SpeedPin, speedright);
  digitalWrite(motor2DirectionPin1, HIGH);
  digitalWrite(motor2DirectionPin2, LOW);
}                                                        
void sharpturnR() {
  analogWrite(motor1SpeedPin, speedextlefturn);
  digitalWrite(motor1DirectionPin1, LOW);
  digitalWrite(motor1DirectionPin2, HIGH);

  analogWrite(motor2SpeedPin, speedextrighturn);
  digitalWrite(motor2DirectionPin1, HIGH);
  digitalWrite(motor2DirectionPin2, LOW);
}

void sharpturnL() {
  analogWrite(motor1SpeedPin, speedextlefturn);
  digitalWrite(motor1DirectionPin1, HIGH);
  digitalWrite(motor1DirectionPin2, LOW);

  analogWrite(motor2SpeedPin, speedextrighturn);
  digitalWrite(motor2DirectionPin1, LOW);
  digitalWrite(motor2DirectionPin2, HIGH);
}

void left_turn_1() {
  analogWrite(motor1SpeedPin, speedlefturn_1);
  digitalWrite(motor1DirectionPin1, HIGH);
  digitalWrite(motor1DirectionPin2, LOW);

  analogWrite(motor2SpeedPin, speedrighturn_2);
  digitalWrite(motor2DirectionPin1, LOW);
  digitalWrite(motor2DirectionPin2, HIGH);
}

void right_turn_1() {
  analogWrite(motor1SpeedPin, speedlefturn_2);
  digitalWrite(motor1DirectionPin1, LOW);
  digitalWrite(motor1DirectionPin2, HIGH);

  analogWrite(motor2SpeedPin, speedrighturn_1);
  digitalWrite(motor2DirectionPin1, HIGH);
  digitalWrite(motor2DirectionPin2, LOW);
}

void left_turn_2() {
  analogWrite(motor1SpeedPin, speedlefturn_2);
  digitalWrite(motor1DirectionPin1, HIGH);
  digitalWrite(motor1DirectionPin2, LOW);

  analogWrite(motor2SpeedPin, speedrighturn_1);
  digitalWrite(motor2DirectionPin1, LOW);
  digitalWrite(motor2DirectionPin2, HIGH);
}

void right_turn_2() {
  analogWrite(motor1SpeedPin, speedlefturn_1);
  digitalWrite(motor1DirectionPin1, LOW);
  digitalWrite(motor1DirectionPin2, HIGH);

  analogWrite(motor2SpeedPin, speedrighturn_2);
  digitalWrite(motor2DirectionPin1, HIGH);
  digitalWrite(motor2DirectionPin2, LOW);
}

void stopDef() {
  analogWrite(motor1SpeedPin, 0);
  digitalWrite(motor1DirectionPin1, LOW);
  digitalWrite(motor1DirectionPin2, LOW);

  analogWrite(motor2SpeedPin, 0);
  digitalWrite(motor2DirectionPin1, LOW);
  digitalWrite(motor2DirectionPin2, LOW);
  delay(50);
}
void asber(){
  /*analogWrite(motor1SpeedPin, 0);
  digitalWrite(motor1DirectionPin1, LOW);
  digitalWrite(motor1DirectionPin2, LOW);

  analogWrite(motor2SpeedPin, 0);
  digitalWrite(motor2DirectionPin1, LOW);
  digitalWrite(motor2DirectionPin2, LOW);*/
  
  analogWrite(motor1SpeedPin, 0);
  digitalWrite(motor1DirectionPin1, LOW);
  digitalWrite(motor1DirectionPin2, LOW);

  analogWrite(motor2SpeedPin, 0);
  digitalWrite(motor2DirectionPin1, LOW);
  digitalWrite(motor2DirectionPin2, LOW);
  delay(50);
}
void demarage(){
    analogWrite(motor1SpeedPin, 180);
    digitalWrite(motor1DirectionPin1, HIGH);
    digitalWrite(motor1DirectionPin2, LOW);

    analogWrite(motor2SpeedPin, 180);
    digitalWrite(motor2DirectionPin1, HIGH);
    digitalWrite(motor2DirectionPin2, LOW);
    delay(400);
}

Credits

akssil-ga

akssil-ga

5 projects • 5 followers
wameedh scientific club

Comments