Skip to main content

Using a CNN network with an AI robot car

The AI robot car is an extended version of the AI CAR. This article contains 2 parts, one is the server set up, second is the robot car set up.

SERVER SET UP

Watch the following video to complete the set up the server.

The version of the ubuntu must be 16.04.

ROBOT CAR SET UP

This robot car is using Raspberry Pi Zero's camera to send a picture to the server; this is analysed via the CNN network, then gives back the result, with then sent back to the Raspberry Pi Zero. Now the Raspberry Pi Zero is confident enough to send the 0-9 to Arduino Uno by GPIO.

The Arduino Uno is now controlling the motor.

L298N diagram from the L298 datasheet.

Capture45_5ce1a1d47058056a893ea2113cc36bfe796217ce.png

Watch these videos to complete the car.

More video here

Here is the Raspberry Pi program, this program divided into 2 parts, first it will send the image to the server; then it will give the GPIO pins to the LED shown for the result and to the Arduino board.

from picamera import PiCamera
from time import sleep
import requests
import datetime
# import serial
from gpiozero import LED
status_led = LED(3) #GPIO3 on rpi
led1 = LED(4)       #GPIO4 on rpi
led2 = LED(17)      #GPIO17 on rpi
led4 = LED(27)      #GPIO27 on rpi
led8 = LED(22)      #GPIO22 on rpi
LEFT_FORWARD = LED(5)       #GPIO5 on rpi
RIGHT_FORWARD = LED(6)      #GPIO6 on rpi
LEFT_BACK = LED(13)      #GPIO13 on rpi
RIGHT_BACK = LED(19)      #GPIO19 on rpi
#url = 'http://192.168.0.100/upload.php' #Local Recognition Server
#url = 'https://eiefyp.ml/v2/upload.php' #Remote Recognition Server
url = 'https://rcj-s1.12inf.com/v2/upload.php'

status_led.off()

# s = serial.Serial ("/dev/ttyS0", 9600)

camera = PiCamera()
camera.resolution = (200, 200)
camera.start_preview()

print('Full test start')

def changeLED(pre_no):
    if (pre_no == 0):
        led1.off()
        led2.off()
        led4.off()
        led8.off()
        LEFT_FORWARD.on()
        RIGHT_FORWARD.on()
        LEFT_BACK.off()
        RIGHT_BACK.off()
    elif (pre_no == 1):
        led1.on()
        led2.off()
        led4.off()
        led8.off()
        LEFT_FORWARD.off()
        RIGHT_FORWARD.off()
        LEFT_BACK.on()
        RIGHT_BACK.on()
    elif (pre_no == 2):
        led1.off()
        led2.on()
        led4.off()
        led8.off()
        LEFT_FORWARD.off()
        RIGHT_FORWARD.on()
        LEFT_BACK.off()
        RIGHT_BACK.off()
    elif (pre_no == 3):
        led1.on()
        led2.on()
        led4.off()
        led8.off()
        LEFT_FORWARD.on()
        RIGHT_FORWARD.off()
        LEFT_BACK.off()
        RIGHT_BACK.off()
    elif (pre_no == 4):
        led1.off()
        led2.off()
        led4.on()
        led8.off()
    elif (pre_no == 5):
        led1.on()
        led2.off()
        led4.on()
        led8.off()
    elif (pre_no == 6):
        led1.off()
        led2.on()
        led4.on()
        led8.off()
    elif (pre_no == 7):
        led1.on()
        led2.on()
        led4.on()
        led8.off()
    elif (pre_no == 8):
        led1.off()
        led2.off()
        led4.off()
        led8.on() 
    elif (pre_no == 9):
        led1.on()
        led2.off()
        led4.off()
        led8.on() 
    
while (True):
	print(datetime.datetime.now(), ' Capturing image')

	camera.capture('/home/pi/Desktop/image.jpg')

	print(datetime.datetime.now(), ' Start to upload image')
	files = {'img': open('/home/pi/Desktop/image.jpg', 'rb')}
	r = requests.post(url, files=files)
	response = r.text.split("\n")
	#print(response)
	if response[0]:
		result = int(response[0])
		confidence = int(response[1])
		print(datetime.datetime.now(), 'Predicted: '+str(result)+'('+str(confidence) + '%)')
		if confidence >= 95:
			if result < 0 or result > 9:
				continue
			else:
				changeLED(result)
				status_led.on()
				# s.write(str.encode(str(result))) #output using serial
				sleep(1)
				status_led.off()
				led1.on()
				led2.on()
				led4.on()
				led8.on()
		else:
			led1.on()
			led2.on()
			led4.on()
			led8.on()
		print(datetime.datetime.now(), ' Completed')
	else:
		print(datetime.datetime.now(), ' Failed to get response')

The Arduino UNO is now ready to receive the signal 0-9 from the raspberry pi.

The Arduino program.

int SLOW_SPEED = 150;
int FAST_SPEED = 200;

int Left_motor_go=8;     //IN1
int Left_motor_back=9;   //IN2

int Right_motor_go=10;   //IN3
int Right_motor_back=11; //IN4

int button=A2;
int buzzer=A3;

int readBit0 = 2;
int readBit1 = 3;
int readBit2 = 5;
int readBit3 = 6;

int mode = 0;

void setup() {
  Serial.begin(9600);
  pinMode(Left_motor_go,OUTPUT); // PIN 8 (PWM)
  pinMode(Left_motor_back,OUTPUT); // PIN 9 (PWM)
  pinMode(Right_motor_go,OUTPUT);// PIN 10 (PWM) 
  pinMode(Right_motor_back,OUTPUT);// PIN 11 (PWM)
  
  pinMode(button,INPUT); //button A2
  pinMode(buzzer,OUTPUT); //buzzer A3
  
  pinMode(readBit0, INPUT);
  pinMode(readBit1, INPUT);
  pinMode(readBit2, INPUT);
  pinMode(readBit3, INPUT);
}

void walk() {
  digitalWrite(Right_motor_go,HIGH);  //Set left motor as forward
  digitalWrite(Right_motor_back,LOW);     
  analogWrite(Right_motor_go,SLOW_SPEED);  //PWM of right motor [0, 255]
  analogWrite(Right_motor_back,0);
  digitalWrite(Left_motor_go,LOW);  //Set left motor as forward
  digitalWrite(Left_motor_back,HIGH);
  analogWrite(Left_motor_go,0);  //PWM of left motor [0, 255]
  analogWrite(Left_motor_back,SLOW_SPEED);
//  delay(time * 100);  //time interval
}

void run() {
  digitalWrite(Right_motor_go,HIGH);  //Set left motor as forward
  digitalWrite(Right_motor_back,LOW);     
  analogWrite(Right_motor_go,FAST_SPEED);  //PWM of right motor [0, 255]
  analogWrite(Right_motor_back,0);
  digitalWrite(Left_motor_go,LOW);  //Set left motor as forward
  digitalWrite(Left_motor_back,HIGH);
  analogWrite(Left_motor_go,0);  //PWM of left motor [0, 255]
  analogWrite(Left_motor_back,FAST_SPEED);
//  delay(time * 100);  //time interval
}

void brake() {
  digitalWrite(Right_motor_go,LOW);
  digitalWrite(Right_motor_back,LOW);
  digitalWrite(Left_motor_go,LOW);
  digitalWrite(Left_motor_back,LOW);
//  delay(time * 100);  //time interval
}

void left() {
  digitalWrite(Right_motor_go,HIGH);
  digitalWrite(Right_motor_back,LOW);
  analogWrite(Right_motor_go,150); 
  analogWrite(Right_motor_back,0);
  digitalWrite(Left_motor_go,LOW);  
  digitalWrite(Left_motor_back,LOW);
  analogWrite(Left_motor_go,0); 
  analogWrite(Left_motor_back,0);
//  delay(time * 100); //time interval
}

void right() {
  digitalWrite(Right_motor_go,LOW);
  digitalWrite(Right_motor_back,LOW);
  analogWrite(Right_motor_go,0); 
  analogWrite(Right_motor_back,0);
  digitalWrite(Left_motor_go,LOW);
  digitalWrite(Left_motor_back,HIGH);
  analogWrite(Left_motor_go,0); 
  analogWrite(Left_motor_back,150);
//  delay(time * 100);
}

void back() {
  digitalWrite(Right_motor_go,LOW); //Set right motor as backward
  digitalWrite(Right_motor_back,HIGH);
  analogWrite(Right_motor_go,0);
  analogWrite(Right_motor_back,SLOW_SPEED); //PWM of right motor [0, 255]
  digitalWrite(Left_motor_go,HIGH);  //Set left motor as backward
  digitalWrite(Left_motor_back,LOW);
  analogWrite(Left_motor_go,SLOW_SPEED);  //PWM of left motor [0, 255]
  analogWrite(Left_motor_back,0);
//  delay(time * 100); //time interval
}

void back_fast() {
  digitalWrite(Right_motor_go,LOW);  //Set right motor as backward
  digitalWrite(Right_motor_back,HIGH);
  analogWrite(Right_motor_go,0);
  analogWrite(Right_motor_back,FAST_SPEED);  //PWM of right motor [0, 255]
  digitalWrite(Left_motor_go,HIGH);   //Set left motor as backward
  digitalWrite(Left_motor_back,LOW);
  analogWrite(Left_motor_go,FAST_SPEED);  //PWM of left motor [0, 255]
  analogWrite(Left_motor_back,0);
//  delay(time * 100); //time interval
}

void loop() {
  /*
   * Modes
   * 0: Stop
   * 1: Forward slowly
   * 2: Forward fast
   * 3: Back slowly
   * 4: Back fast
   * 5: Turn left
   * 6: Turn right
   * 
   */
  int mode = digitalRead(readBit0)*1+digitalRead(readBit1)*2+digitalRead(readBit2)*4+digitalRead(readBit3)*8;
  Serial.println(mode);
  if (mode == 1) {
    walk();
  } else if (mode == 2) {
    run();
  } else if (mode == 3) {
    back();
  } else if (mode == 4) {
    back_fast();
  } else if (mode == 5) {
    left();
  } else if (mode == 6) {
    right();
  } else {
    brake();
  }
}

 

The attached files contain the sample code of the AI car and Server.

Downloads

Brian0925 has not written a bio yet…
DesignSpark Electrical Logolinkedin