Вы находитесь на странице: 1из 9

CHAPTER 3

IMPLEMENTATION AND OPERATION

This chapter presents the construction of the hardware components focusing


mainly on the connection among the involved modules, motors and power supplies. To
discuss the operation with respect to the flow chart, there is two sections, the server
operation and the client operation. Then, the interaction of the server with the sensors
and motors is revealed.

3.1. Implementation of Circuit and Hardware Components


The basic requirements to build a robot car frame are four wheels and an acrylic
car chassis. Four DC Motors connected to this wheels will reside in this car chassis. To
drive these DC motors, a motor driver is needed. Since the motor driver used in this
project has only two channels of output, the motors on the right side of the car are
connected in parallel to connect to one of these outputs and the motors on the other side
to other channel.
This motor driver can be supplied with 7.4V which is obtained by connecting
two 3.7 V batteries in series. It has four input ports IN1, IN2, IN3 and IN4, and two
Enable ports. While two enable ports are connected to the 5 V output of the motor
driver, the four input ports are connected to the Raspberry Pi’s GPIO pins of 12, 16, 20,
and 21 respectively. Although the Enable pins are usually used to control the speed of
the motors by varying the average voltage applied on them instead of a constant 5 V,
the four input pins are used to control both the speed and the direction since Raspberry
Pi has plenty of GPIO pins that support PWM.
The robot car also has some an Ultrasonic Ranging Sensor and IR Reflective
Sensors. The Ultrasonic Ranging Sensor is attached to the back of the car to detect the
obstacles behind the car. It has four pins, VCC, Trigger, Echo and Ground. The power
supply (VCC) of the sensor is connected to 5 V output of the Raspberry Pi. To emit an
ultrasonic wave, the Raspberry Pi produces an electric pulse with 10µs pulse width
from its GPIO23 pin which is connected to the Trigger pin. Then the Echo pin
41

connected to GPIO24 will change to Logic HIGH. When the ultrasonic wave reflected
back to the sensor, the Echo pin will change to Logic LOW again. The time it takes the
Echo pin to change HIGH to LOW is used to calculate the distance between the car and
the obstacle. Here, the output of the Echo pin is 5V but the GPIO pin can only accept
3.3V. A simple solution to solve this problem is to use a voltage divider circuit. It
contains two resistors (1kΩ and 2kΩ) connected in series, with the input voltage of 5V
applied across the resistor pair and the output voltage of approximately 3.3333V across
the 2kΩ. This 3.3333V is now applied to the GPIO24 confidently.
The four IR Reflective Sensors are attached to the four corner of the car facing
the ground to detect the edge. IR Reflective Sensors have three pins, Signal, Power
Supply, and Ground. All of the Power Supply pins and Ground pins are connected to
the 3.3V output and Ground of the Raspberry Pi respectively. The signal pins of the IR
Sensor are connected to the GPIO pins, 17(front-left), 27(front-right), 22(back-left),
and 10(back-right).
To capture the video along the track, a Pi Camera module which is compatible
with the Raspberry Pi is connected with the CSI (Camera Serial Interface) from the
Raspberry Pi using a 15-pin ribbon cable. Since the viewing angle of the camera is
limited, a Servo Motor is needed to improve it.

Figure 3.1. Circuit Diagram of the System


42

A Servo Motor is used to rotate the elevation angle of the Pi Camera. It has three
pins, Power Supply, Control and Ground. The 5V Power Supply for the Servo Motor
is connected with the Enable pin of the DC Motor Driver. The Control and Ground pins
are connected with the GPIO25 and Ground pins of the Raspberry Pi respectively.
The main component of the robot car is the Raspberry Pi, it is used as a single
board computer. It control all the other parts of the robot car and also used to connect
with the Client’s Browser using wireless connection Wi-Fi. The Micro USB power
supply pin of the Raspberry Pi supply by using 5V-2A power bank.

3.2. Operation
The operation can be divided into two parts. One is for the server and other is
for the client. The server also performs the process of acquisition of sensor’s data and
controlling the motors.

3.2.1. Operation of Server


The robot is used as a server which means that it is needed to be started first
waiting a client connection. However, before the server program is started, it must be
connected to a wireless network. In this case, a Wi-Fi LAN will be used as the wireless
medium to transfer data between the server and the client. As soon as the server
program get started, it initialize its sensors, camera and the Flask web framework. Then,
the web server is started using the server’s current IP address and the port number
assigned by the user. In this case port 80 is used as it is a default port number for HTTP.
In this state, the server will wait until a client attempt to connect from the browser.
Since HTTP uses TCP connection, a TCP connection to the client is established.
To do so, the following three-way handshake occurs:
SYN: The active open is performed by the client sending a SYN to the server.
The client sets the segment's sequence number to a random value A.
SYN-ACK: In response, the server replies with a SYN-ACK. The
acknowledgment number is set to one more than the received sequence number i.e.
A+1, and the sequence number that the server chooses for the packet is another random
number, B.
ACK: Finally, the client sends an ACK back to the server. The sequence number
is set to the received acknowledgement value i.e. A+1, and the acknowledgement
number is set to one more than the received sequence number i.e. B+1.
43

Client Server
SYN

SYN-ACK

ACK

Figure 3.2. TCP Connection Establishment


At this point, both the client and server have received an acknowledgment of
the connection. The steps 1, 2 establish the connection parameter (sequence number)
for one direction and it is acknowledged. The steps 2, 3 establish the connection
parameter (sequence number) for the other direction and it is acknowledged. With
these, a full-duplex communication is established.
Once the server receives a HTTP request, it renders an HTML template back to
the client’s user agent (the browser). Then the Raspberry Pi will collect the data from
its sensors and the image from the Pi Camera module. According to the data from the
sensors, it decides to stop or keep doing what the user have commanded like driving
the car. It also performs some further processing on the image like compressing to be
convenient when transmitting. After that, the image and the sensor data all together can
be transferred to the client in a live streaming manner. The user from the client side can
then manage the car movement from these data. On receiving the user’s command, the
server will control the mobility of the car using its motors. If the connection is still
established, it will loop back to the earlier process of collecting the data and the image.
Subsequently, the following states will be repeated too until the connection to the client
still exist.
If another user tries to connect, the same process mentioned above will be
executed simultaneously for that user. Unless there is connected client, it will go back
to the connection setup state because a server is an always-on device in a network to
wait for a client. Therefore, the server program will only be stopped manually by the
administrator for maintenance.
44

Figure 3.3. Flow Chart of Client Process and Server Process

3.2.2. Operation of Client


On the client side, the user can easily access a website by typing the server IP
address in a browser. This surely set up a TCP connection with the server and an HTTP
request message will be sent with the URL ( / ) to the server over this connection. As a
respond, the server send back an HTML file. As soon as all of the objects in the HTML
is loaded, the client will listen to an event which indicates that an update video frame
and sensor data are received. Every time, the server pushes new data to the client, an
event is fired calling a function which displays the image and the sensor data in their
respective places. In this way, old data will be replaced with new data. Moreover, the
IR sensor data will be highlight with red color if the sensors detect an edge and prevent
the car from advancing. For the ultrasonic sensor reading, if the distance to the obstacle
at the back of the car is less than or equal to 17 cm, the display number will also be
highlighted with red color warning that the car is prevented from going backward and
crashing into the obstacle. Otherwise, all of the sensor data will be displayed with a
45

grey background. At the same time, every user command will be transmitted to the
server. When the user touches or presses on a button, the corresponding command such
as ‘forward’, ‘reverse’, ‘left’ and ‘right’ will be send at once but the ‘stop’ command
will be sent after the user release the button.

3.2.3. Acquisition of Sensors’ Data


In order to make the car acts intelligently and the algorithm helps the user in
driving the car, IR reflective sensors detecting edges and an Ultrasonic Ranging sensor
measuring the distance are connected to GPIO Pins of the Raspberry Pi as shown in
figure 3.1. Raspberry Pi will read these sensors periodically to interrupt risky car
movements and send these data to the user.
Whenever the server program reach the state of collecting sensor data and
capturing image, the Raspberry Pi will capture the image with the 640x480 resolution
first. After that, the raw image which is ready for further processing is compressed into
a JPEG format to be convenient while transmitting. Actually, this is the state where one
can easily add more sophisticated image processing methods to develop some
additional feature for the car in future. Then it will collect rest of the sensor data.
Firstly, the IR Sensor Modules will be read starting from the one at the front-
left corner of the car. In another word, logic value of GPIO 12 pin will be safe in a
variable for the later use. The values of remaining IR sensors modules, front-right
module, back-left module and back-right module, will also be recorded in a sequence.
These recorded values are Boolean Values that have one of two possible values (usually
denoted True and False), intended to represent the present of ground beneath the car
(False) and the present of edge (True).
Lastly, the Ultrasonic Ranging Sensor will measure the distance to the object
behind the car. As mentioned earlier, the Trigger pin of the sensor needs to go to high
state for 10micros in order to transmit ultrasonic waves. The Raspberry Pi will change
its GPIO 23 pin to high state, wait for 10 micro second, change the pin back to low state
and wait till the GPIO 24 pin, the Echo pin of the sensor, goes high. Once it goes high,
the system time expressed in seconds since the epoch (January 1, 1970), in UTC will
be recorded as a starting time and the processes will pause till the GPIO 24 pin go to
low state again. Once its state become low, the system time will be recorded again as
an ending time. The difference between the ending time and the starting time is the
pulse width which represents the round-trip time (RTT) of the ultrasonic wave to travel
46

between the obstacle and the sensor. To calculate the distance, equation 2.2 is used.
Then the result representing the distance in cm will be saved as a floating point number.

3.2.4. Controlling the Motors


Most of the output devices in the project are motors. They are 4 DC motors and
a Servo Motor. After receiving the command from the client, the motors are driven
according to the command by altering the pulse width of the GPIO pins connected to
the motor driver in a programmed manner as shown in table 3.1.

Table 3.1. Truth Table To Control the Speed and Direction

Motor 1 and 2 (Left side) Motor 3 and 4 (Right side)


Duty Cycle
IN1 IN2 IN3 IN4 Command
of PWM
(GPIO 12) (GPIO 16) (GPIO 20) (GPIO 21)

1 0 1 0 Forward 45%
(1.49 V)

0 1 0 1 Backward 35%
(1.16V)

0 1 1 0 Left 80%
(2.64V)

1 0 0 1 Right 80%
(2.64V)

0 0 0 0 Stop 0%
(0V)

If the command is “Forward”, the car will go forward and all of the motors will
turn in forward direction. To make Motors 1 and 2 go forward, set IN1=HIGH and
IN2=LOW. To make Motors 3 and 4 go forward, set IN3=HIGH and IN4=LOW. In
fact, a PWM signal with a 45% duty cycle is generated by the Raspberry PI instead of
giving a constant 3.3 V output when the input pin IN1 and IN3 needed to go HIGH.
Therefore, the average voltage at these input pins drop to 1.49 V and the motor turning
at a moderate speed a human can handle. Likewise, an equivalent of Logic LOW output
(0 V) is obtained by changing the duty cycle to 0%.
47

If the command is “Backward”, all of the motors need to turn in the


counterclockwise direction and Raspberry Pi will set IN1=LOW, IN2=HIGH,
IN3=LOW, and IN4=HIGH. To turn the car left, drive the Motor 1 and 2 backwards
and Motor 3 and 4 forward. When turning the car right, the case is opposite for the
turning left. The same method is used to obtain the specific average voltage to adjust
the speed.
In addition, when the command is “Forward”, the front IR sensor data which
are saved as variables will be checked. Only if these values are False, does the car
advance in a way mentioned above. If these values are True meaning that the car detects
an edge, it stops immediately. When the command is “Backward”, both the back IR
sensor data and the ultrasonic sensor data are needed to check. If the Ultrasonic sensor
measurement is less than or equal to 17 cm, the car is also stopped and prevented from
moving backward.
If the command is “Up”, the Raspberry pi will turn the servo motor up by 10
degrees. In other word, the duty cycle of the PWM signal is increased by 0.6% (pulse
width = 0.12 millisecond). Therefore, there will be 19 steps to complete a 180 degrees
turn as shown in the following table. The maximum duty cycle is 12.8 % (pulse width
= 2.56 m sec) and the minimum duty cycle is 2% (pulse width = 0.4 m sec).

Table 3.2. Nineteen Steps In Turning the Servo 180 Degrees

Servo Angle 0° 10° 20° 30° 40° 50° 60°

Pulse Width 0.4 m 0.52 m 0.64 m 0.76 m 0.88 m 1.00 m 1.12 m


sec sec sec sec sec sec sec

Servo Angle 70° 80° 90° 100° 110° 120° 130°

Pulse Width 1.24 m 1.36 m 1.48 m 1.6 m 1.72 m 1.84 m 1.96 m


sec sec sec sec sec sec sec

Servo Angle 140° 150° 160° 170° 180°

Pulse Width 2.08 m 2.2 m 2.32 m 2.44 m 2.56 m


sec sec sec sec sec
48

If the command is “Down”, the servo angle will decrease by 10 degrees and the
duty cycle decrease by 0.7%.

Вам также может понравиться