finalthesisdraft

22
J ACOBS U NIVERSITY B REMEN GUIDED R ESEARCH IN E LECTRICAL E NGINEERING Bluetooth Controlled Intelligent Car Author: Syedzaada Mouhammad Alee Kazmi Supervisor: Dr. Fangning Hu A thesis submitted in fulfillment of the requirements for the degree of Bachelor of Electrical and Computer Engineering in the School of Engineering and Computer Sciences May 25, 2016

Upload: syed-kazmi

Post on 22-Jan-2018

231 views

Category:

Documents


1 download

TRANSCRIPT

JACOBS UNIVERSITY BREMEN

GUIDED RESEARCH IN ELECTRICAL ENGINEERING

Bluetooth Controlled Intelligent Car

Author:Syedzaada MouhammadAlee Kazmi

Supervisor:Dr. Fangning Hu

A thesis submitted in fulfillment of the requirementsfor the degree of Bachelor of Electrical and Computer Engineering

in the

School of Engineering and Computer Sciences

May 25, 2016

ii

Declaration of AuthorshipI, Syedzaada Mouhammad Alee Kazmi, declare that this thesis titled, “Blue-tooth Controlled Intelligent Car” and the work presented in it are my own.I confirm that:

• This work was done wholly or mainly while in candidature for a re-search degree at this University.

• Where any part of this thesis has previously been submitted for a de-gree or any other qualification at this University or any other institu-tion, this has been clearly stated.

• Where I have consulted the published work of others, this is alwaysclearly attributed.

• Where I have quoted from the work of others, the source is alwaysgiven. With the exception of such quotations, this thesis is entirelymy own work.

• I have acknowledged all main sources of help.

• Where the thesis is based on work done by myself jointly with others,I have made clear exactly what was done by others and what I havecontributed myself.

Signed:

Date: May 25, 2016

iii

JACOBS UNIVERSITY BREMEN

AbstractEngineering Faculty

School of Engineering and Computer Sciences

Bachelor of Electrical and Computer Engineering

Bluetooth Controlled Intelligent Car

by Syedzaada Mouhammad Alee Kazmi

ENGLISH VERSIONBriefly, my project is divided into two steps. The first being coding a dis-tance detection for the arduino using the sonic detector provided with itand then mounting that on a remote controlled RC car. Following that, thearduino will also be programmed to remotely use bluetooth in its detectionso that the remote controlled(RC) car can be controlled through a portablephone or laptop. If possibly completed, I should be able to remotely controlan RC car without direct visual sight as the sonar detector should be able toguide me.

GERMAN VERSIONKurz gesagt, ist mein Projekt in zwei Schritte unterteilt: Die Programmierungeiner Abstandserfassung für den Arduino mit dem Schalldetektor, und danndie Montage auf einem ferngesteuerten RC-Car. Im Anschluss daran wirdder Arduino programmiert werden, um die Fernsteuerung des Autos durchein tragbares Telefon oder Laptop zu ermoeglichen. Nach Abschluss derArbeit, sollte ich in der Lage sein ein RC-Auto nur mit Sonar-Detektor, ohnedirekten Sichtkontakt, fernzusteuern.

iv

AcknowledgementsI would like to express my deepest appreciation for those that have pro-

vided me the possibility to complete this endeavour. First of all, I wouldlike to be thankful to Dr. Fangning Hu for letting me take on this projectwith her. Secondly, I would like to thank my friend Dorin Gabriel Clisu forbeing an honest friend. Lastly, I would like to thank Jason Becker for creat-ing the music he creates.

Dedicated to JASoN Becker

v

Contents

Declaration of Authorship ii

Abstract iii

Acknowledgements iv

1 Introduction to the Arduino 11.1 Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

1.2.1 Arduino IDE . . . . . . . . . . . . . . . . . . . . . . . 21.2.2 Processing . . . . . . . . . . . . . . . . . . . . . . . . . 31.2.3 Paint . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

2 Explanation of the Code 42.1 Arduino IDE Code . . . . . . . . . . . . . . . . . . . . . . . . 42.2 Processing Code . . . . . . . . . . . . . . . . . . . . . . . . . . 7

3 Running the Experiment 123.1 Power Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . 123.2 Signal Chain . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123.3 Building and Assembling the Vehicle . . . . . . . . . . . . . . 133.4 Simulation Results . . . . . . . . . . . . . . . . . . . . . . . . 13

4 Conclusion 15

vi

List of Figures

1.1 Arduino Uno . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 Arduino First Page . . . . . . . . . . . . . . . . . . . . . . . . 21.3 Arduino’s Serial Monitor . . . . . . . . . . . . . . . . . . . . . 21.4 Processing Start Up Screen . . . . . . . . . . . . . . . . . . . . 3

2.1 Arduino Uno . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82.2 rectMode(CORNERS) . . . . . . . . . . . . . . . . . . . . . . . 9

3.1 Arduino connected to the car . . . . . . . . . . . . . . . . . . 123.2 Vehicle Details . . . . . . . . . . . . . . . . . . . . . . . . . . . 133.3 Processing Screenshots . . . . . . . . . . . . . . . . . . . . . . 14

1

Chapter 1

Introduction to the Arduino

1.1 Hardware

The arduino is an open-source hardware microcontroller based kit for build-ing digital devices that can interact with further physical objects via a soft-ware interface. Different vendors produce different arduinos. The mostcommon one is the Arduino Uno wwhich was used for this thesis.

FIGURE 1.1: Arduino Uno

This particular arduino contains various pins that are formally statedas input or output pins. However, it must be noted that any pins can beconfigured for both input or output purposes. Apart from those pins, thereare also general power pins, namely the 5V and 3.3V pins. These are used topower various devices that are connected to the arduino. Lastly, a groundpin exists to provide common ground to all the connected devices.

2 Chapter 1. Introduction to the Arduino

1.2 Software

1.2.1 Arduino IDE

For communicating with the arduino, the open-source Arduino IDE is used.The Arduino IDE is based on a custom version of C++.

FIGURE 1.2: Arduino First Page

The arduino software communicates with the arduino hardware via theserial monitor which acts as a tether. It shows in real time the values re-ceived from the arduino hardware.

FIGURE 1.3: Arduino’s Serial Monitor

1.2. Software 3

It should be noted that the 9600 baud indicated in the figure can bethought of as the communicating speed with the arduino. The readingspeed of the serial monitor should be the same as the writing speed fromthe arduino which is set in the code itself else the serial monitor will onlyread junk values.

1.2.2 Processing

Another use of the serial monitor is for it to be a medium of connection withother software programs. The one which I used for this project is called"Processing".

FIGURE 1.4: Processing Start Up Screen

Processing contains useful visualization tools and thats why it was usedto handle that part. It imports data via the serial monitor too.

1.2.3 Paint

Last but not the least, Microsoft Paint was used to create the background ofthe processing visualization page

4

Chapter 2

Explanation of the Code

In this chapter, I will explain my code section by section and then finallycombine it with my partner’s code.

2.1 Arduino IDE Code

It should be noted that this was a group project so the code is jointly writtenand it is too much interwoven to be separated so I will begin by definingonly my parts and then giving a broad overview of everything as will bepresented during the thesis presentation. First of all I add the NewPingLibrary developed by Arduino itself. It saves a lot of time and effort to con-figure the sonic sensors as everything is already predefined. Next I definethe pin numbers where every pin is connected and the maximum distancethat the sonic sensors can sense-:

#include <NewPing.h>

#define TRIGGER_PIN1 12#define ECHO_PIN1 11#define MAX_DISTANCE 2000

#define TRIGGER_PIN2 10#define ECHO_PIN2 9

Now I create two new classes of sonic sensors and include the previ-ous defined constants in them. This tells the compiler that i am using twoseparate sonic sensors-:

NewPing sonar1(TRIGGER_PIN1, ECHO_PIN1, MAX_DISTANCE);NewPing sonar2(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE);

Now I explicitly tell the arduino to send data to the serial monitor at aspeed of 9600 baud. Note that baud was the old unit of measuring speedand 9600 baud is roughly equal to 1.20Kilobytes/second-:

void setup() {Serial.begin(9600);}

Now I define the loop part of the code where everything is run con-stantly. The .ping() function is used from the newping library and this isused to obtain the time it takes for the signal to return. Next this is multi-pled by the predefined speed of the sonic sensors which is a constant storedas-:

2.1. Arduino IDE Code 5

1

US _ROUNDTRIP _CM

The result is Distance=Speed*Time which is printed on the serial monitor.

void loop() {int uS1 = sonar1.ping();int uS2 = sonar2.ping();Serial.print(uS1 / US_ROUNDTRIP_CM);Serial.print(",");Serial.println(uS2 / US_ROUNDTRIP_CM);}

The complete code that will be used is as follows-:

#include <NewPing.h>

#define TRIGGER_PIN1 7#define ECHO_PIN1 6#define MAX_DISTANCE 2000

#define TRIGGER_PIN2 5#define ECHO_PIN2 4

NewPing sonar1(TRIGGER_PIN1, ECHO_PIN1, MAX_DISTANCE);NewPing sonar2(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE);

const int right = 13;const int left = 12;const int reverse = 11;const int forward = 10;int incomingByte;

void setup() {pinMode(forward, OUTPUT);pinMode(reverse, OUTPUT);pinMode(left, OUTPUT);pinMode(right, OUTPUT);Serial.begin(9600);}

void loop() {int uS1 = sonar1.ping();int uS2 = sonar2.ping();int distanceback=uS1/US_ROUNDTRIP_CM;int distancefront=uS2/US_ROUNDTRIP_CM;

if (Serial.available() > 0) {incomingByte = Serial.read();

if ((incomingByte == ’w’) && (distancefront > 13)){digitalWrite(forward, HIGH);digitalWrite(reverse, LOW);}

6 Chapter 2. Explanation of the Code

if (incomingByte == ’A’){digitalWrite(forward, LOW);}

if ((incomingByte == ’q’) && (distancefront > 13)){digitalWrite(forward, HIGH);digitalWrite(left, HIGH);digitalWrite(reverse, LOW);digitalWrite(right, LOW);}

if (incomingByte == ’I’) {digitalWrite(forward, LOW);digitalWrite(left, LOW);}

if ((incomingByte == ’e’) && (distancefront > 13)){digitalWrite(forward, HIGH);digitalWrite(right, HIGH);digitalWrite(reverse, LOW);digitalWrite(left, LOW);}

if (incomingByte == ’O’) {digitalWrite(forward, LOW);digitalWrite(right, LOW);}

if ((incomingByte == ’s’) && (distanceback > 13)){digitalWrite(reverse, HIGH);digitalWrite(forward, LOW);}

if (incomingByte == ’B’){digitalWrite(reverse, LOW);}

if (incomingByte == ’a’){digitalWrite(reverse,HIGH);digitalWrite(forward,LOW);digitalWrite(left, HIGH);digitalWrite(right, LOW);}if (incomingByte == ’C’){

digitalWrite(left, LOW);digitalWrite(reverse,LOW);}if (incomingByte == ’d’)

2.2. Processing Code 7

{digitalWrite(reverse,HIGH);digitalWrite(forward,LOW);digitalWrite(right, HIGH);digitalWrite(left, LOW);}

if (incomingByte == ’E’){digitalWrite(right, LOW);digitalWrite(reverse,LOW);}}{

Serial.print(distanceback);Serial.print(",");Serial.println(distancefront);

if ((distanceback<11)&&(distanceback!=0)){digitalWrite(reverse,HIGH);digitalWrite(forward,LOW);}if

((distanceback>=12)&&(incomingByte!=’s’)&&(incomingByte!=’a’)&&(incomingByte!=’d’)){digitalWrite(reverse,LOW);}if ((distancefront<11)&&(distancefront!=0)) {digitalWrite(forward,HIGH);digitalWrite(reverse,LOW);}if

((distancefront>=12)&&(incomingByte!=’w’)&&(incomingByte!=’q’)&&(incomingByte!=’e’)){digitalWrite(forward,LOW);}return loop();}}

In our code, we designed a security feature such that if the car is within10 cm of a forward or backward obstacle, it automatically reverses and for-wards respectively until 10 cm are left

2.2 Processing Code

In Processing, I imported the values from the arduino via the serial mon-itor as told in the first line. Then I begin to define some parameters. Theimportant one being String first value and String second value. These arebasically the data from the first sonic sensor and then the second one. Thevariable lf is used later in the code to specify the maximum length of a stringthat can be read from the serial monitor at a time.

import processing.serial.*;

8 Chapter 2. Explanation of the Code

int lf =10;Serial myPort;String myString=null;float num1;float num2;String firstvalue;String secondvalue;PImage bg;

This is the setup part where I define certain important parameters. Firstof all size(500,500) creates a window of 500 by 500 dimension where thevisualization is supposed to take place. The black line on the graph indi-cates the 10cm line, which tells the user that the car is 10cm away from anobstacle. The background of this window is then loaded from a file calledbackground.jpg. It looks as follows-:

FIGURE 2.1: Arduino Uno

Smooth(2) is an internal processing function that smooths out the figure. Iset the framerate to 60fps and tell the arduino which COM to use which isautomatically retrieved by ’Serial.list()[0]’

void setup(){size(500,500);bg=loadImage("background.jpg");smooth(2);frameRate(60);String portName = Serial.list()[0];myPort = new Serial(this,portName,9600);}

The next code is basically a string splitter. The arduino sends the ultrasonicreaders values in the following format-:

x,y

where x and y are integer lengths from the front and back ultrasonic readerrespectively. This is parsed as a string in processing. Now I need to break

2.2. Processing Code 9

this string up into x and y for which I make use of the following function.This was taken directly from the stackexchange site. Basically, it takes thestring "data" and reads until the char "separator". Then it reads again untilthe function ends and using the index, stores the first, second... string intothe return variable.

String getValue(String data, char separator, int index){int found = 0;int strIndex[] = {0, -1};int maxIndex = data.length()-1;

for(int i=0; i<=maxIndex && found<=index; i++){if(data.charAt(i)==separator || i==maxIndex){found++;strIndex[0] = strIndex[1]+1;strIndex[1] = (i == maxIndex) ? i+1 : i;}}

return found>index ? data.substring(strIndex[0],strIndex[1]) : "";

}

This is the main part of the code where every previous function is madeuse of. The background is declared. While the serial monitor is transmit-ting data, the first stream of data is read until the new line. That data issplit using the string splitting function and stored in first value and secondvalue. These are then converted to floats since they are still a string. Now Iuse a specific method to draw rectangles in the figure. It is as follows -:

FIGURE 2.2: rectMode(CORNERS)

As we can see, the coordinates of the vertices are given in the rectModefunction. I use this to draw two rectangles corresponding to the length fromthe obstacle. x1,y1 are fixed for both the rectangles since they stay at oneplace. However, the other end depends on the length of obstacle in frontof the sonic detector. Lastly, I have disregarded the 0 distance value sincethere is an internal error in the sonic sensors and they send out clusters of 0at times.

void draw(){background(bg);

10 Chapter 2. Explanation of the Code

while ( myPort.available() > 0){ // If data is available,myString = myPort.readStringUntil(lf);if (myString != null){firstvalue=getValue(myString,’,’,0);secondvalue=getValue(myString,’,’,1);num1=float(firstvalue); // Converts and prints floatnum2=float(secondvalue);if (num1==0){num1 = 10000;}if (num2==0){num2 = 10000;}rectMode(CORNERS);rect(230,500,130,100+10*num1);rect(370,500,270,100+10*num2);}}myPort.clear();}

Lastly, the next section is about reading the key entered from Process-ing and sending it to the Arduino IDE. This was written by my colleague,Nurlan Nurdildauly. There is a limitation to this code that we cannot presstwo keys at the same time. Therefore, we cannot accelerate and go rightat the same time by pressing forward and right. However, there is a quicksolution around this. It was to separately define 6 keys for forwardleft, for-wardright, backwardleft, backwardright, forward and backward only.

if(keyPressed == true) {if ( key == ’w’ ){ println("FORWARD"); myPort.write(key);

println(key); }}if(keyPressed == false) {if ( key == ’w’ ){ println("OFF"); myPort.write(’A’); }}

if (keyPressed == true) {if ( key == ’s’ ){ println("REVERSE"); myPort.write(key);

println(key); }}if(keyPressed == false) {if ( key == ’s’ ){ println("OFF"); myPort.write(’B’); }}

if(keyPressed == true) {if ( key == ’a’ ){ println("BACKWARDLEFT");

myPort.write(key); println(key); }}if(keyPressed == false) {if ( key == ’a’ ){ println("OFF"); myPort.write(’C’); }}

2.2. Processing Code 11

if(keyPressed == true) {if ( key == ’d’ ){ println("BACKWARDRIGHT");

myPort.write(key); println(key); }}if(keyPressed == false) {if ( key == ’d’ ){ println("OFF"); myPort.write(’E’); }}

if(keyPressed == true) {if ( key == ’q’ ){ println("FORWARDLEFT");

myPort.write(key); println(key); }}if(keyPressed == false) {if ( key == ’q’ ){ println("OFF"); myPort.write(’I’); }}

if(keyPressed == true) {if ( key == ’e’ ){ println("FORWARDRIGHT");

myPort.write(key); println(key); }}if(keyPressed == false) {if ( key == ’e’ ){ println("OFF"); myPort.write(’O’); }}}

12

Chapter 3

Running the Experiment

In this section, I would discuss about running the experiment. The dissas-sembled car is as follows-:

FIGURE 3.1: Arduino connected to the car

As we can see, the components used were an arduino, two ultrasonicsensors, the H bridge motor and the vehicle’s motor. The H bridge motor isused to power the vehicles motor.

3.1 Power Sources

We drew power from 4 energizer batteries having 1.5V, 2300mAh each.However, we soon discovered that this was not enough for all the compo-nents. Initially, we just thought of using more batteries but soon we foundout that it was the voltage and not the current creating the limitation. So,we took a parallel branch out of those 4 and gave that to the H motor bridgethat was drawing the most power.

3.2 Signal Chain

After I have uploaded the code in the arduino, processing refreshes andchecks if a key has been entered by the user. Simultaneously, arduino getsthe values of the distance by the sonic sensors. In our code we have givenprecedence to the automatic reverse and forward security feature over thekeyboard entry so even if the user presses forwards or backward and lessthen 10 cm of distance is left from either ways, the car will move away untilthe distance is greater then 10cm.

3.3. Building and Assembling the Vehicle 13

(A) Basic Circuit used (B) Car after assembling

FIGURE 3.2: Vehicle Details

3.3 Building and Assembling the Vehicle

The car was built in layers. The first layer consisted of just the Arduinoconnected to the ultrasonic sensors. Next, we added an H motor bridgeand two motors in front. Now, I wanted to close the top of the car so acouple of adjustments had to be made. First of all, we had to solder thepower cables onto the battery contacts. Then we had to solder again todraw voltage from the 3 batteries. Now we bundled the cables togetherand burnt few holes in the plastic covers to allow space for the cables tocome out. Lastly, we mounted the Arduino and the breadboard on top ofthe vehicle. Small components such as the distance detectors were taped onwhile the Arduino and breadboard itself were sealed by a plastic wire.

3.4 Simulation Results

I am going to show some of the simulation results I got for different valuesof the sonic sensors and how they are shown on the screen. As we can see,the obstacle is represented by the block at the top. The rectangle grows insize as the obstacle comes nearer. It should be noted that the sensors do notinterfere with each other’s readings.

14 Chapter 3. Running the Experiment

(A) Arduino sensors far away fromobstacle

(B) Forward sensor 10cm from obsta-cle

(C) Backward Sensor 10cm from ob-stacle

(D) Forward sensor almost touchingan obstacle while backward one far

away

(E) Backward sensor almost touch-ing an obstacle while forward one far

away(F) Both sensors 10 cm from obstacle

FIGURE 3.3: Processing Screenshots

15

Chapter 4

Conclusion

We accomplished a lot of goals in this thesis. First of all, I further exploredthe functionalities of the Arduino and its capabilities. I also learned howto solder and manage space in compartments that are not build to containmore cables. We also realized that weight is a huge issue that should not besimply underestimated. This costed us heavily as our prototype could notmove.

A quick solution could have been to remove the breadboard and just usewires to connect the modules. Furthermore, we could have used a LithiumPolymer battery instead of Lithium Ion. Next, I learned more about powerconsumption and how to deal with inadequate power issues. I realized thata device only needs to have the same voltage requirement as the powersupply can provide. Anything above is harmful and anything below wontpower the device and could cause frequent trips which are harmful bythemselves. However, current by the source only needs to exceed the cur-rent drawn by the load. Even if the current on the source is 10A, the loadwill only draw the amount it needs. Using this, we can use parallel andseries circuits to our advantage.

Wireless Communication is slowly replacing wires. Although we are re-stricted to slower speeds, higher latency and wireless interferance, overtime these issues should be solves as more funding is being put into wire-less research. Maybe, one day we would recycle all the cables on the planetto produce a cleaner and healthier world.

16

Bibliography

[1] Dr Fangning Hu. Embedded System Laboratory Course Lecture Notes. Bre-men, Germany, 2016

[2] Friends of Fritzing. Fritzing Arduino Official Learning Guide. FH Pots-dam, 2016

[3] Alvaro Luis Bustamante. String Splitting Code used on page 9, http://stackoverflow.com/a/14824108, 2012

[4] Daniel Shiffman. Learning Processing, https://processing.org/tutorials/drawing, 2008