0% found this document useful (0 votes)
141 views30 pages

Flagbot Robotics Design Report

FlagBot is an autonomous robot designed to navigate a path, stop in front of a wall, raise a flag, and return to its starting point. It uses an Arduino Uno board to control motors, sensors and lights. The robot has a chassis, drive system, power system and flag. It uses line tracking sensors to follow a path and an ultrasonic sensor to detect the wall. Software routines control tracking, stopping, turning and flag raising. When complete, FlagBot will compete in a contest to demonstrate its capabilities.

Uploaded by

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

Flagbot Robotics Design Report

FlagBot is an autonomous robot designed to navigate a path, stop in front of a wall, raise a flag, and return to its starting point. It uses an Arduino Uno board to control motors, sensors and lights. The robot has a chassis, drive system, power system and flag. It uses line tracking sensors to follow a path and an ultrasonic sensor to detect the wall. Software routines control tracking, stopping, turning and flag raising. When complete, FlagBot will compete in a contest to demonstrate its capabilities.

Uploaded by

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

FlagBot

Version 1.0
Robotics Design Report
Team 1, Group C
English for Computer Science 3
Foundation Year
Vietnamese-German University
April 12, 2018
(This page intentionally left blank)
Dinh Quy Tri Thong
Student ID: 13249
Group C2, CS 2017

April 7, 2018

Richard G. Bradley, MAL, MSM


Vietnamese German University
Foundation Year Studies
Nam Kỳ Khởi Nghĩa Street, Hòa Phú District
Bình Dương New City, Bình Dương Province

Dear Mr. Bradley,

Enclosed is our Robotics Design Report for our class project, FlagBot, submitted to partially meet
the requirements for English for Computer Science 3.

This Report discusses the context for the project, gives a detailed description of the mechanical,
sensor, control, and software sub-systems, addresses the feasibility of the project and includes all
code produced for this project.

I appreciate the time you are taking to review this Report and hope that it meets your approval. If
you have any more questions feel free to contact me by e-mail: [email protected].

Sincerely yours,

Dinh Quy Tri Thong,


Team Leader

Enclosure: Robotics Design Report for FlagBot (1 Copy)


(This page intentionally left blank)
The Vietnamese-German University
Foundation Year
English for Computer Science 3

Robotics Design Report


for
FlagBot
Version 1.0

Nguyen Tien Dat - 13948


Hoang Van Thien - 13302
Phan Nhat Nguyen - 13089
Dinh Quy Tri Thong - 13249
Nguyen Tat Dat – 13816

Instructor: Richard Bradley, MAL, MSM


Due date: April 12, 2018
(This page intentionally left blank)
Disclaimer

We declare that this report is a product of our own work, unless otherwise referenced. We also
declare that all opinions, results, conclusions and recommendations are our own and may not
represent the policies or opinions of the Vietnamese-German University.

Dinh Quy Tri Thong


13249
Team Leader

Nguyen Tien Dat


13948

Hoang Van Thien


13302

Phan Nhat Nguyen


13089

Nguyen Tat Dat


13816

i
(This page intentionally left blank)

ii
Abstract

FlagBot, Version 1.0

Team 1, Group CD

FlagBot, Version 1.0, is an in-class project that was assembled and programmed in order to

meet the demands of the course for Computer Science 3. Besides, it is required to perform all the

tasks in the contest which will be held at the end of the course.

FlagBot is an independent, automatic, and self-sufficient ground vehicle. This robot can start

automatically when the barrier is removed, then navigate through a given path (which will not curve

greater than 45 degrees) until it is 10 cm in front of to the wall. At that point, the wheels stop, a flag

attached with the robot will be raised. Finally, the robot must return to the starting point. When the

robot turns left or right, the light on that direction will light up accordingly. When the robot stops,

both lights must be turned on. Otherwise, the lights remained off. The FlagBot’s structure is based

on the Turtle Robot chassis and uses an Arduino Uno board as its main controller.

Key words: Arduino Uno, line tracking, integrated circuit

iii
(This page intentionally left blank)

iv
Table of Contents

Disclaimer.............................................................................................................................................i
Abstract...............................................................................................................................................iii
Table of Contents.................................................................................................................................v
Acknowledgements.............................................................................................................................vi
1. Introduction......................................................................................................................................1
1.1. Purpose......................................................................................................................................1
1.2. Audience....................................................................................................................................1
1.3. Project Scope.............................................................................................................................1
2. Context of the Project.......................................................................................................................2
2.1. Requirements and Constraints...................................................................................................2
2.1.1. Requirements......................................................................................................................2
2.1.2. Constraints..........................................................................................................................2
2.2. Current System..........................................................................................................................3
3. Overview of the Proposed System...................................................................................................4
3.1. Description................................................................................................................................4
3.2. Mechanical sub-systems............................................................................................................4
3.2.1. Chassis................................................................................................................................5
3.2.2. Drive sub-system................................................................................................................5
3.2.3. Power sub-system...............................................................................................................5
3.2.4. Flag sub-system..................................................................................................................5
3.3. Sensor sub-system.....................................................................................................................6
3.3.1. Ultrasonic sensor................................................................................................................6
3.3.2. Line tracking sensor...........................................................................................................6
3.4. Control sub-system....................................................................................................................6
3.4.1. Arduino Board....................................................................................................................6
3.4.2. Motor Control Shield..........................................................................................................6
3.5. Software sub-system..................................................................................................................7
3.5.1. Overall Design....................................................................................................................7
3.5.2. Routines..............................................................................................................................8
3.5.2.1. Tracking and signaling routine..................................................................................8
3.5.2.2. Stopping routine........................................................................................................9
3.5.2.3. Turning around routine..............................................................................................9
3.5.2.4. Flag raising routine....................................................................................................9
3.5.3. Device controlling..............................................................................................................9
3.5.3.1. Distance measuring...................................................................................................9
3.5.3.2. Track recognizing......................................................................................................9
3.5.3.3. Wheel controlling and light signaling.....................................................................10
3.6. Communications Interfaces.....................................................................................................10
3.6.1. LEDs signaling.................................................................................................................10
4. Conclusion......................................................................................................................................11
5. References......................................................................................................................................12
Appendix A: Glossary of Terms...........................................................................................................1
Appendix B: Gantt Chart......................................................................................................................1
Appendix C: FlagBot Sketch................................................................................................................1

v
Acknowledgements

I would like to express my deep appreciation to the lecturers of the Vietnamese-German

University for their explanations and guidance.

I would also like to thank all my friends and classmates for their suggestions and assistance

during the writing of this Software Requirements Specification.

During this project, tasks were divided up as follows:

• Nguyen Tien Dat and Dinh Quy Tri Thong wrote reports and helped assembling the robot.

• Hoang Van Thien was responsible for building and programming the robot, as well as

providing support in report writing.

• Phan Nhat Nguyen designed the group’s presentations and helped assembling the robot.

• Nguyen Tat Dat was responsible for supplying materials and looking for reference sources.

Dinh Quy Tri Thong


Team Leader, Team 1

vi
1. Introduction

1.1. Purpose

This Robotics Design Report for FlagBot, Version 1.0, is submitted for partial fulfillment of the

requirements of the course “English for Computer Science 3” at the Vietnamese-German University

and includes descriptions of the system, specifically of the mechanical sub-systems, and software

development.

1.2. Audience

The intended audience of this document is the course instructor, who will use it as the basis for a

determination of a portion of our grade for the class “English for Computer Science 3”.

1.3. Project Scope

This report provides a detailed description of the line-following robot’s design, including the

mechanical, control, and software sub-systems, together with the communication methods. The

project’s schedule and the code are also included in the appendices. The design of the flag attached

with the robot, however, is not included and described in the report.

1
2. Context of the Project

2.1. Requirements and Constraints

2.1.1. Requirements

The robot’s operations must obey the contest rules as provided in [1]:

 The robot must be equipped with two rear LEDs. When turning left or right, the LED on that

side will light up to indicate its turning direction. If the robot does not move, both LEDs

must light up. Otherwise, the LEDs remain off.

 The robot must be able to detect when the barrier is removed. This implies that we need a

distance sensor.

 Then, the robot must be able to navigate and follow along a given path, which is colored in

black and 18 mm wide. This implies that we need to use at least one line tracking sensor.

 The robot must be able to stop 10cm in front of a wall. This implies that we need a distance

sensor.

 The robot will then raise a flag with “VGU” written on it at the end of the course. This

implies that we need to use a servo motor.

 Finally, the robot must do a half-circle turn, following the path to the starting point.

2.1.2. Constraints

Given the availability of devices supplied by VGU and the contest rules in [1], we have

identified that the following constraints may limit the options for us:

 Hardware:

 Chassis: Turtle Robot chassis

 Controllers: Arduino Uno micro-controller and DFRobot Motor Shield L298P

 Sensors: Tracker Sensor V4, Ultrasonic Sensor URM37 V3.2

 Motors: Servo Motor V9 and DC Motor

2
 Battery: maximum of 20V

 No chemical reactions are allowed. The robot must not cause damage to people and

other equipments.

 All borrowed equipments must be returned to the lab after use.

 Software: a compiled Arduino program uploaded to the Arduino board via the Arduino

IDE v1.8.5.

 Manpower: five amateur developers

 Time: seven weeks

2.2. Current System

There is no current system to be replaced.

3
3. Overview of the Proposed System

3.1. Description

The Flagbot includes the Arduino Uno as the main controller. Besides, the robot has two wheels

powered by DC motors and controlled by a motor shield. An ultrasonic sensor is used to determine

the distance from the FlagBot to the starting gate and to the wall at the end of the path. The three

line tracking sensors are responsible for tracking the line. Additionally, a servo is needed to raised

the flag when the robot is 10 cm in front of the wall.

Figure 1: Context diagram for FlagBot, version 1.0

3.2. Mechanical sub-systems

The FlagBot's mechanical sub-systems consists of the chassis, the drive sub-system, the power

sub-system, the flag sub-system.

4
3.2.1. Chassis

The chassis of the FlagBot consists of two plastic plates with to hold the equipments. The plates

are put parallel to each other. It is based on the Turtle Robot model.

3.2.2. Drive sub-system

The drive sub-system of FlagBot consists of the Arduino Uno, motor shield, two DC motors, and

three tracker sensors. The two motors are mounted to the chassis and connected with the Arduino

board via the motor shield. At the back of the robot, a caster is installed to keep balance.

According to [2], the connection between the DC motors and the Arduino board via the Arduino

Motor Shield (Model L298N) is configured as below:

• Right wheel: Pin 5 Arduino (speed control) and Pin 4 Arduino (direction control)

• Left wheel: Pin 6 Arduino (speed control) and Pin 7 Arduino (direction control)

• Each wheel’s two poles are connected to the shield’s motor terminal.

3.2.3. Power sub-system

The power source for the robot is a rechargeable 7.4V Li-Po battery. It is placed between the two

plates of the chassis. Two poles of the battery are connected to the Arduino’s GND and VIN pin.

Two wires are then used to connect the second GND pin and the 5V Pin of the Arduino to a

breadboard attached on the top of the chassis. The power wire and ground wire of any other device

can be plugged to the breadboard, if necessary.

3.2.4. Flag sub-system

The flag sub-system includes a servo (Servo Motor V9) mounted on the right hand side of the

chassis, attached to a flag with “VGU” written on it, in contrast colors. The rod of the flag is 10 cm.

The servo is controlled via pin 11.

5
3.3. Sensor sub-system

FlagBot’s sensor sub-system is responsible for recognizing the path and obstacles. The sub-

system consists of one ultrasonic sensor and three line tracking sensors.

3.3.1. Ultrasonic sensor

An ultrasonic sensor (Model URM37 V3.2) is used to detect the starting gate and the wall at the

end of the path. It is placed at the front of the robot. The sensor is configured in PWM passive

control mode, specified in [3], and is connected to the Arduino board as following:

• Pin PWM of the sensor connects with Pin 10 of the Arduino board

• Pin COMP/TRIG of the sensor connects with Pin 9 of the Arduino board

3.3.2. Line tracking sensor

Three line tracking sensors (Tracker Sensor V4) are used to keep track of the black colored path.

They are placed under the chassis and close to the ground. Pin 2, 8, and 12 are connected to the

signal wires of the left, middle, and right sensor, respectively. These pins are set as input pins to

receive digital signal of the sensors.

3.4. Control sub-system

FlagBot’s control sub-system consists of the Arduino board to control the FlagBot and the Motor

Control Shield to control the two motors.

3.4.1. Arduino Board

The Arduino board used in the robot is Arduino Uno. The Arduino Uno is based on the

ATmega328P. Further technical details of the board can be found in [4].

3.4.2. Motor Control Shield

The motor control shield (Model L298N) is attached with the Arduino board, and is configured

as instructed in [2]:

6
• Since the motors take power from the Arduino power supply, the power source selection

jumpers are set as VIN.

• FlagBot uses PWM control mode to control the wheels, hence the control mode selection

jumpers must be set accordingly.

3.5. Software sub-system

3.5.1. Overall Design

The FlagBot software is a compiled C++ program, starts immediately once the system is

powered and consists of the following routines:

 A setup process to declare the input and output pins for all of the appliances installed

 A routine to move the robot along the track and brighten the LEDs in accordance with the

robot’s movement

 A routine to stop the robot

 A routine to turn the robot around

 A routine to raise the flag

In the subsequent sections, we describe the function of each routine and the methods of handling

motors, sensors, and LEDs.

Figure 2: High-level flow-chart of FlagBot

7
3.5.2. Routines

3.5.2.1. Tracking and signaling routine

This routine tries to set the directions of the wheels, based on the signals from three line tracking

sensors, in order to keep track of the line. It is worth noting that the routine does not alter the

wheels’ speeds, and the wheels’ directions are controlled together with the signal lights within a

single function described in section 3.5.3.3.

The tracking algorithm is described as follow:

1. If the middle sensor sees black color, that means the robot can move forward and the routine

continues at step 3. Otherwise, the routine continues at step 2.

2. The robot tries to turn left or right in order to meet the black color at the middle sensor

again. It is sure that both left and right sensors cannot see the black color at once because

this will lead to the middle sensor to see color black, which conflicts with the condition in

step 1. There are three cases for the status of the two sensors:

a. Color black on the left, but not on the right: This shows that the path has a left turn. The

routine set the right wheel spin forward while the left wheel spin backward, making the

robot turn left and waits until the middle sensor sees color black.

b. Color black on the right, but not on the left: This shows that the path has a right turn.

The routine set the left wheel spin forward while the right wheel spin backward, making

the robot turn right and waits until the middle sensor sees color black.

c. Color black not on any side: This shows that the robot may accidentally run out of track.

The routine set both wheels spin backward. In this particular case, the LEDs will

automatically light up while the robot is moving, so this can be fixed by setting the

LEDs off immediately. The program then waits until any of the three sensors sees color

black again.

3. The routine sets both wheels spin forward and terminate.

8
3.5.2.2. Stopping routine

This routine simply sets the directions of both wheels as “backward” and their speeds as zero,

making the robot stop and both LEDs light up.

3.5.2.3. Turning around routine

Given the fact that while turning around, there must be one moment that all three line tracking

sensors do not see color black, this routine makes the robot turn right (left wheel spinning forward,

right wheel spinning backward) and waits until all the three sensors do not see color black. Next, it

keeps waiting until one of the three sensors see color black again and terminates at last.

3.5.2.4. Flag raising routine

As the robot reaches the wall and stops, the function will set the angle of the servo at 110

degrees to raise up the flag.

3.5.3. Device controlling

3.5.3.1. Distance measuring

The function measures the duration (in microseconds) of a low pulse from pin 10 (connected to

the PWM pin of the ultrasonic sensor). According to [3], each 50 microsecond pulse stands for a

distance of 1 cm from the ultrasonic sensor to the obstacle. Hence, the function returns the distance

(in centimeters) by dividing the time measured by 50. However, if the time is 50,000 microseconds,

the distance is classified as invalid and the function will return an error value of 999. We choose

this particular large value due to the fact that invalid distance usually happens when an obstacle is

placed too far away from the sensor.

3.5.3.2. Track recognizing

The function reads simultaneously three digital signals from the left, middle, and right line

tracking sensor. The signal received from a sensor is low if the recognized color is black, and is

9
high otherwise [5]. Since the signal of each sensor is binary, the function combines three signals

and returns a three-bit integer.

3.5.3.3. Wheel controlling and light signaling

The function’s parameters are the position of the wheel (left or right), the expected speed, and

the expected direction. The speed of the right (or left) wheel is set by writing a PWM signal from 0

to 255 to pin 5 (or pin 6). The direction of the right (or left) wheel is set by writing a digital signal

to pin 4 (or pin 7). This digital signal will be set low if the expected direction is “forward,” and will

be set high otherwise.

When a wheel’s direction is set backward, the LED on that side will illuminate. The light is

brightened by sending a high digital signal to the pin connected with that light. Please refer to

section 3.6.1 to see the pin configuration for the LEDs.

The robot can stop by calling two functions for two wheels, each function set the wheel’s speed

as zero and the direction as backward, so that both LEDs will light up. If the robot needs to turn left,

the right wheel’s direction is set backward and the left wheel’s direction is set forward, making the

robot turn left and the left LED light up. The similar rule applies when the robot needs to turn right.

3.6. Communications Interfaces

3.6.1. LEDs signaling

Two rear LEDs are attached on the top of the chassis, one on the left and one on the right side of

the robot. When turning left or right, the LED on that side will light up to indicate its turning

direction. If the robot does not move, both LEDs must light up. Otherwise, the LEDs remain off.

• The right LED is controlled via pin 3.

• The left LED is controlled via pin 13.

10
4. Conclusion

This report presents a description of an independent, automatic, and self-sufficient ground

vehicle controlled by an Arduino Uno. The report includes the overall requirements and constraints,

as well as the mechanical, control, sensor, and software sub-systems, and the communication signal

of the robot. The robot can start automatically when the barrier is removed, then navigate through a

given 18 mm black path until it is 10 cm in front of the wall. At that point, the wheels stop, a flag

attached with the robot will be raised. After that, the robot must turn around and return to the

starting point, following the same path.

11
5. References

[1] R.Bradley, Specification for CS Flagbot, The Vietnamese-German University, HCMC, VN:
VGU 2017.

[2] DFRobot Wiki. (2017, June). Arduino Motor Shield (L298N) (SKU:DRI0009). [Online].
Available: https://www.dfrobot.com/wiki/index.php/Arduino_Motor_Shield_(L298N)_
(SKU:DRI0009)

[3] DFRobot Wiki. (2017, May). URM37 V3.2 Ultrasonic Sensor (SKU:SEN0001). [Online].
Available: https://www.dfrobot.com/wiki/index.php/URM37_V3.2_Ultrasonic_Sensor_
(SKU:SEN0001)

[4] Arduino Store. (undated). Arduino Uno Rev3. [Online]. Viewed 2018 April 11. Available:
https://store.arduino.cc/usa/arduino-uno-rev3

[5] DFRobot Wiki. (2017, August). Line Tracking Sensor for Arduino V4 SKU:SEN0017. [Online].
Available: https://www.dfrobot.com/wiki/index.php/Line_Tracking_Sensor_for_Arduino
_V4_SKU:SEN0017

12
Appendix A: Glossary of Terms

Table 1: Glossary of terms

Terms Definitions
Analog signal A type of signal that is continuously variable
Arduino Uno Rev3 An open-source micro-controller board developed by Arduino
ATmega328P A low-power single-chip micro-controller created by Atmel
C++ A general-purpose object-oriented programming language, and
is an extension of the C language
DC Direct Current, the unidirectional flow of electric charge
Digital signal A type of signal with two distinguishable levels
GND Ground, the reference point in an electrical circuit from which
voltages are measured, a common return path for the current.
IDE Integrated Development Environment, a software suite that
consists of the basic tools to develop software
LED Light-Emitting Diode, a two-lead semiconductor light source
Li-Po battery A rechargeable battery of lithium-ion technology using a
polymer electrolyte instead of a liquid one
Micro-controller A small computer on a single integrated circuit
Pin A part used for validating electronic transactions

Pulse A single vibration or short burst of sound, electric current, light,


or other wave
PWM Pulse-Width Modulation, a modulation technique used to encode
a message into a pulsing signal
Sensor A device that detects or measures a physical property and
records, indicates, or otherwise responds to it
Servo A servomotor is a rotary actuator that allows precise control of
angular position
Ultrasonic sensor A device that can measure the distance to an object by using
sound waves
V (7.4V battery) Volt, the derived unit for electric potential, electric potential
difference (voltage), and electromotive force.
VIN The abbreviation of “volt in”

A-1
Appendix B: Gantt Chart

Week 1 Week 2 Week 3 Week 4 Week 5 Week 6 Week 7 Week 8


Analyze Constraints and
Requirements █████
Build Chassis █████
Wire the Arduino, motors ultrasonic
sensor, and tracker sensors █████
Test Motor Controlling █████
Test Line Following and Obstacle
Detecting █████
Wire the LEDs and Servo ██
Test brake/signal lights █
Test servo controlling ██
Compare performance with other
group █████
Improve performance █████
Research the devices █████ █████
Enhance code’s readability █████
Write Report █████ █████ █████ █████
Contest █

Deliverables Legend
Constraints and requirements Week 2 Critical Path █████
Progress Report 1 Week 5 Non-Critical █████
Progress Report 2 Week 6 Slack Time █████
Final Report Week 7
B-1
Appendix C: FlagBot Sketch

/*
* Project: FlagBot
* Vietnamese-German University
* Team 1 (Hoang Van Thien, Dinh Quy Tri Thong, Nguyen Tien Dat, Nguyen Tat Dat, Phan Nhat
Nguyen)
* Move the robot through a black path, stop at 10 cm in front of the wall, raise the flag, return to
the starting point
* Reference sources:
* L298N Manual, by DFRobot:
https://www.dfrobot.com/wiki/index.php/Arduino_Motor_Shield_(L298N)_(SKU:DRI0009)
* URM37 V3.2 Manual, by DFRobot:
https://www.dfrobot.com/wiki/index.php/URM37_V3.2_Ultrasonic_Sensor_(SKU:SEN0001)
* Tracker Sensor V4, by DFRobot:
https://www.dfrobot.com/wiki/index.php/Line_Tracking_Sensor_for_Arduino_V4_SKU:SEN0017
* Connections:
* Two poles of Right DC Motor -> two terminals M1+ and M1- (Motor Shield)
* Two poles of Left DC Motor -> two terminals M2+ and M2- (Motor Shield)
* E1 (Motor Shield) -> Pin 5 (Arduino) (built-in connection, controlling Right DC Motor's speed)
* M1 (Motor Shield) -> Pin 4 (Arduino) (built-in connection, controlling Right DC Motor's
direction)
* E2 (Motor Shield) -> Pin 6 (Arduino) (built-in connection, controlling Left DC Motor's speed)
* M2 (Motor Shield) -> Pin 7 (Arduino) (built-in connection, controlling Left DC Motor's
direction)
* Right Tracker Sensor -> Pin 12 (Arduino)
* Middle Tracker Sensor -> Pin 8 (Arduino)
* Left Tracker Sensor -> Pin 2 (Arduino)
* Pin PWM (URM V3.2) -> Pin 10 (Arduino)
* Pin COMP/TRIG (URM V3.2) -> Pin 9 (Arduino)
* Pin 3 (Arduino) -> Right LED -> GND (Arduino)
* Pin 13 (Arduino) -> Left LED -> GND (Arduino)
* Servo -> Pin 11 (Arduino)
* Note: The power wires and ground wires of Servo, Tracker Sensors, and Ultrasonic Sensors are
respectively connected to VIN and GND of the Arduino
*
*/

#include <Servo.h>

// Notes: The right device has index 0, the left device has index 1
const int right = 0, left = 1, ignore = -1, forward = 0, backward = 1;
const int white = 1, black = 0;
int URPWM = 10, URTRIG = 9;
int pinSpeeds[2] = {5, 6};
int pinDirections[2] = {4, 7};
int pinTrackers[3] = {12, 8, 2};
int wheelSpeeds[2] = {100, 105};
int threshold, dir;
int servoPin = 11, servoPosition;
int pinLEDs[2] = {3, 13};

C-1
Servo servo;

void setup()
{
Serial.begin(9600);
pinMode(pinDirections[0], OUTPUT); // Right Motor's direction pin
pinMode(pinDirections[1], OUTPUT); // Left Motor's direction pin
pinMode(pinSpeeds[0], OUTPUT); // Right Motor's speed pin
pinMode(pinSpeeds[1], OUTPUT); // Left Motor's speed pin
pinMode(pinTrackers[0], INPUT); // Right Line tracking Sensor's pin
pinMode(pinTrackers[1], INPUT); // Middle Line tracking Sensor's pin
pinMode(pinTrackers[2], INPUT); // Left Line tracking Sensor's pin
servo.attach(servoPin); // Servo
servo.write(20); // Set servo's initial angle 20 degrees
threshold = 10; // Stopping point in front of the wall, in cm
pinMode(pinLEDs[0], OUTPUT); // Right LED's pin
pinMode(pinLEDs[1], OUTPUT); // Left LED's pin
}

/*
* Control the wheel:
* - side: 0 (right) or 1 (left)
* - Speed: an integer from 0 to 255, or -1 to set the default speed
* - Direction: 0 (forward) or 1 (backward) or -1 to not change the Direction
*/
void runWheel(int side, int Speed, int Direction)
{
if (Direction != ignore) {
if (Direction == forward) {
digitalWrite(pinDirections[side], LOW);
digitalWrite(pinLEDs[side], LOW);
} else {
digitalWrite(pinDirections[side], HIGH);
digitalWrite(pinLEDs[side], HIGH);
}
}
if (Speed == ignore) Speed = wheelSpeeds[side];
analogWrite(pinSpeeds[side], Speed);
}

unsigned int getDistance()


{
unsigned long LowLevelTime = pulseIn(URPWM, LOW) ;
unsigned int DistanceMeasured = 0;
if(LowLevelTime>=50000) {
Serial.print("Invalid");
return 999;
} else {
DistanceMeasured = LowLevelTime / 50; // every 50us low level stands for 1cm
}
return DistanceMeasured;
}

C-2
int getLineStatus()
{
int l = digitalRead(pinTrackers[2]), m = digitalRead(pinTrackers[1]), r =
digitalRead(pinTrackers[0]);
// Return a three-bit integer representation of the three signals from sensors
return l*4 + m*2 + r;
}

void tracking()
{
while (getDistance() > threshold) {
int lineStatus = getLineStatus();
if ((lineStatus & 0b010) != 0) { // cannot move forward anymore
if (lineStatus == 0b111) { // cannot move anywhere
// Reminder: -1 for speed means setting the default speed
runWheel(left, -1, backward);
runWheel(right, -1, backward);
// Wait until any of the three sensors sees color black
while (getLineStatus() == 0b111 && getDistance() > threshold);
} else if ((lineStatus & 0b001) != 0) { // cannot turn right, so turn left
runWheel(left, -1, backward);
runWheel(right, -1, forward);
// Wait until the middle sensor sees color black
while ((getLineStatus() & 0b010) != 0 && getDistance() > threshold);
} else { // Turn right
runWheel(left, -1, forward);
runWheel(right, -1, backward);
// Wait until the middle sensor sees color black
while ((getLineStatus() & 0b010) != 0 && getDistance() > threshold);
}
}
// Go straight forward
runWheel(left, -1, forward);
runWheel(right, -1, forward);
}
}

void Stop()
{
runWheel(left, 0, backward);
runWheel(right, 0, backward);
}

void loop()
{
Stop();
while (getDistance() <= threshold); // Wait until the gate opens
tracking();
Stop();
servo.write(110); // Raise the flag
// Turn right

C-3
runWheel(left, -1, forward);
runWheel(right, -1, backward);
while ((getLineStatus() & 0b010) == 0);
while ((getLineStatus() & 0b010) != 0);
tracking();
Stop();
while(1); // Infinite loop to make the program idle
}

C-4

You might also like