Smart Video Car For Raspberry Pi
Smart Video Car For Raspberry Pi
About SunFounder
SunFounder is a technology company focused on Raspberry Pi and Arduino open source
community development. Committed to the promotion of open source culture, we strive to
bring the fun of electronics making to people all around the world and enable everyone to
be a maker. Our products include learning kits, development boards, robots, sensor modules
and development tools. In addition to high quality products, SunFounder also offers video
tutorials to help you make your own project. If you have interest in open source or making
something cool, welcome to join us!
er
In this book, we will show you how to build the smart car via description, illustrations of physical
components, and schematic diagrams of circuits, in both hardware and software respects.
d
You may visit our website www.sunfounder.com to download the related code and view the
un
user manual on LEARN -> Get Tutorials and watch related videos under VIDEO, or clone the
code on our page of github.com at
https://github.com/sunfounder/Sunfounder_Smart_Video_Car_Kit_for_RaspberryPi
o
Free Support
nF
If you have any TECHNICAL questions, add a topic under FORUM section on our website
and we'll reply as soon as possible.
Su
For NON-TECH questions like order and shipment issues, please send an email to
[email protected]. You're also welcomed to share your projects on FORUM.
Reprint 4.0
Contents
Introduction .............................................................................................................................................. 1
Components............................................................................................................................................. 2
A. Acrylic Plates .............................................................................................................................. 2
B. Mechanical Fasteners .............................................................................................................. 3
C. Drive Parts ................................................................................................................................... 4
D. Electrical Components ............................................................................................................ 5
E. Self-provided Parts .................................................................................................................... 8
Electrical Components Basics ............................................................................................................... 9
A. Raspberry Pi ................................................................................................................................ 9
B. Step-down DC-DC Converter Module ............................................................................... 10
C. Servo .......................................................................................................................................... 10
D. DC Motor Driver ....................................................................................................................... 11
E. USB Wi-Fi Adaptor .................................................................................................................... 11
er
F. Servo Controller ....................................................................................................................... 12
G. 18650*2 Battery Holder ........................................................................................................... 12
d
Assembly.................................................................................................................................................. 13
un
A. Back Half Chassis + Rear Wheels ......................................................................................... 13
B. Back Half Chassis + Copper Standoffs ............................................................................... 15
C. Upper Plate + Copper Standoffs ......................................................................................... 15
o
er
Mount Adjustment .................................................................................................................. 51
MJPG-streamer (Operation on Raspberry Pi) .................................................................................. 52
d
A. MJPG-streamer Installation ................................................................................................... 52
un
B. Testing ........................................................................................................................................ 53
Get on the Road! .................................................................................................................................. 55
A. Run TCP server (Operation on Raspberry Pi) ..................................................................... 55
o
Abstract ............................................................................................................................................ 58
Introduction of Socket ........................................................................................................... 58
Su
1. Server.................................................................................................................................. 59
Process Diagram of Server Program ................................................................................... 60
2. Client .................................................................................................................................. 60
Introduction of Tkinter ............................................................................................................ 60
Process Diagram of Client Program .................................................................................... 62
Summary .................................................................................................................................................. 63
Introduction
The SunFounder Smart Video Car Kit for Raspberry Pi is composed of Raspberry Pi, step-down
DC-DC converter module, USB camera, DC motor driver, and PCA9685-based servo
controller. From the perspective of software, the smart car is of client/server (C/S) structure.
The TCP server program is run on Raspberry Pi for direct control of the car. And the video
data are acquired and delivered via the open source software MJPG-streamer in a real-time
manner. The TCP client program is run on PC to send the control command. Both the client
and server programs are developed in Python.
The smart car is developed based on the open source hardware Raspberry Pi and integrates
the knowledge of mechanics, electronics and computer, thus having profound educational
significance.
d er
o un
nF
Su
1
Components
A. Acrylic Plates
d er
o un
nF
Su
2
B. Mechanical Fasteners
Parts Name Qty.
M2*8 screw 6
M2.5*6 screw 16
M3*8 screw 8
er
M3*10 screw 6
d
un
M3*30 screw 4
M4*25 screw 2
o
nF
M2 nut 6
M2.5 nut 16
M3 nut 20
M4 self-locking nut 2
3
C. Drive Parts
Parts Name Qty.
Tower Pro
Micro Servo 3
SG90
er
Gear reducer 2
d
o un
nF
Driven wheel 2
Su
Active wheel 2
4
D. Electrical Components
Parts Name Qty.
Raspberry Pi
1
Model B+
d er
16-Channel 12-bit
un
PWM driver 1
(servo controller)
o
nF
Su
L298N DC motor
1
driver
Step-down DC-
DC
1
converter
module
5
USB Wi-Fi
1
adapter
USB camera 1
d er
uno
18650*2
nF
1
Battery holder
Su
Ribbon 1
USB cable 1
6
Cross socket
1
wrench
Cross 1
screwdriver
d erCable Spiral
un
1
Wrap
o
nF
5
(F to F)
7
E. Self-provided Parts
The following parts are not included in the set.
18650 3.7V
rechargeable 2
Li-ion battery
er
TF Card 1
d
o un
nF
Su
8
Electrical Components Basics
A. Raspberry Pi
d er
un
The Raspberry Pi is a low cost, credit-card sized computer that plugs into a computer monitor
o
or TV, and uses a standard keyboard and mouse. It is a capable little device that enables
nF
people of all ages to explore computing, and to learn how to program in languages like
Scratch and Python. It's capable of doing everything you'd expect a desktop computer to
do, from browsing the internet and playing high-definition video, to making spreadsheets,
Su
What's more, the Raspberry Pi has the ability to interact with the outside world, and has been
used in a wide array of digital maker projects, from music machines and parent detectors to
weather stations and tweeting birdhouses with infra-red cameras.
In this kit, we use Raspberry Pi as the core controller of driving equipment like DC motor and
servo. With the device and a camera collaborated, video data can be acquired in a real-
time manner and delivered by Wi-Fi network.
9
B. Step-down DC-DC Converter Module
Built based on the chip XL1509, the module converts the battery output of 7.4V to 5V, so as
er
to supply power to Raspberry Pi and the servo. As a DC to DC converter IC, the chip has an
input voltage ranging from 4.5V to 40V and generates an output voltage of 5V with a current
d
of as high as 2A. Please note: only when the input voltage is up to 6.5V, a 5V output can be
supported.
un
C. Servo
o
nF
Su
In this smart car, one servo controls the direction of the car, and the other two, the movement
of the camera between X axis and Y axis, thus defining the coverage of the camera. A servo
is an automatic control system composed of DC motor, reduction gear set, sensor, and
control circuit. It defines the rotation angle of the output shaft via delivering specific PWM
signals.
Generally, a servo supports a maximum rotation angle of the shaft (like 180 degrees). It differs
from a common DC motor in the rotation mode: a DC motor rotates by circle when a servo
10
rotates in a certain degree and does not rotate in a round circle. Also, the former is used for
power supply by its whole-circle rotation, while the latter is applied to controlling the rotation
angle of an object (like joints of a robot).
D. DC Motor Driver
d er
o un
As the name suggests, the module is used to drive DC motors. The driver is built based on
nF
L298N. As a high-voltage and large-current chip for motor driving, encapsulated with 15 pins,
the chip has a maximum operating voltage of 46V and an instant peak current of as high as
3A, with an operating current of 2A and rated power of 25W. Thus, it is completely capable
Su
11
F. Servo Controller
The Servo Controller is built based on PCA9685. PCA9685 is a 16-channel LED controller with
I2C bus interface. The resolution ratio of each channel is 12 bits (212=4096 levels). The
controller works in a frequency between 40Hz and 1000Hz and its duty cycle can be adjusted
er
in a range of 0 to 100%. It provides PWM signals for the servo and controls the rotation angle
of the servo. Meanwhile, the module controls the duty cycle of the square waves output
d
from channel 14 and 15 to regulate the rotational speed of the DC motor, so as to control
un
the speed of the car.
The battery is to use two 18650 batteries to power the modules and servos on the car.
Note: Please pay attention to the cathode (-) and anode (+) marks inside the holder (near
each pole). Install the 18650 batteries accordingly: battery cathode to holder -, and battery
anode to holder +.
12
Assembly
A. Back Half Chassis + Rear Wheels
a) Assemble the following two acrylic b) When completed, the assembly should
plates look like the figure below.
c) Assemble the gear reducer, the active wheel and following acrylic plates with two
M3*30 screws and M3 nuts.
d er
o un
nF
Su
d) When completed, the assembly should look like the figure below.
13
e) Assemble the following two acrylic f) When completed, the assembly should
plates look like the figure below.
g) Assemble the gear reducer, the active wheel and the previously assembled part
with two M3*30 screws and M3 nuts.
d er
o un
nF
Su
h) When completed, the assembly should look like the figure below.
14
B. Back Half Chassis + Copper Standoffs
a) Assemble 4 M3*24 copper standoffs b) When completed, the assembly should
and 4 M3 nuts into the acrylic plate part look like the figure below.
as shown below.
d er
un
C. Upper Plate + Copper Standoffs
a) Assemble 16 M2.5*8 copper standoffs and 16 M2.5 nuts into the acrylic plate as shown
o
below.
nF
Pay attention to the face of the plate. Refer to the small hole in the plate as pointed by the
arrow in the following figure.
Su
15
b) When completed, the assembly should look like the figure below.
d er
o un
nF
Su
16
D. Battery Holder
a) Assemble the battery holder to the plate below with 2 M3*10 countersunk screws and
2 M3 nuts.
You can thread a ribbon through the plate below, so it will be easy to remove the
battery, which is up to you.
d er
un
b) When completed, the assembly should look like the figure below.
o
nF
Su
17
E. Back Chassis + Upper Plate
a) Connect the two assembled parts with 4 M3*8 screws.
d er
un
b) When completed, the assembly should look like the figure below.
o
nF
Su
18
c) The view from the back of the plate:
19
Circuit Connecting
Preview:
Looks complicated but don't worry! The detailed procedures will be given below, step by
step.
d er
o un
nF
Su
20
Step 1: Connect the two DC motors with the motor driver.
You may remove the L-shaped PCB connector and plug it back after wiring.
Note: It doesn't matter how to wire the motors. After all the assembly is done, if the car moves
in an opposite direction of what you control, just swap the wiring of the two motors and it will
work normally.
d er
o un
nF
21
Step 2: Connect the motor driver with the Raspberry Pi GPIO port based on the following
table.
d er
o un
nF
Su
22
Step 3: Connect the servo controller with the Raspberry Pi GPIO port as follows:
Raspberry Pi GPIO Port Servo Controller
Pin 2 (5V) VCC
Pin 3 (SDA) SDA
Pin 5 (SCL) SCL
GND GND
d er
o un
nF
Su
23
Step 4: Hook up the servo that controls the car's direction to CH0 of the servo controller, and
the two servos that control the view of the camera to CH14 and CH15 respectively, as shown
below:
d er
un
Step 5: Connect the motor driver with the servo controller.
o
nF
Su
24
Step 6: Connect the servo controller with the step-down DC-DC converter module. For the
connector, loosen the screws, insert the wire, and then tighten the screws with a screwdriver.
d er
o un
nF
25
Step 7: Connect the battery holder with the step-down DC-DC converter module and the
DC motor driver.
Note: Please DO NOT install the batteries at this step! Do it later when you've completed all
the wiring. And take care that the red wire is the power and should be connected to the
anode of the holder, when the black wire, i.e. the ground, to cathode.
+
-
d er
o un
nF
Su
Insert the four wires into two jacks of the connector, two in each. Pay attention: the red wires
to the anode of the step-down module, when the black ones to the cathode. Also DO NOT
plug a red wire with a black one into the same jack, or it'll cause a short circuit!
- +
26
The whole picture of wiring should be like this:
d er
o un
nF
Su
27
Step 8: Connect the Raspberry Pi with the step-down DC-DC converter module, the USB Wi-
Fi adapter and the USB camera.
d er
o un
nF
Su
28
Servo Calibration (Operation on Raspberry Pi)
In the subsequent part of Operation on Raspberry Pi, you need to log into the Raspberry Pi
REMOTELY instead of direct operation on a screen connected to the RPi – which is also
impossible because the HDMI port on the smart car is blocked and cannot connect to one
such display.
So the back part of the car is completed. Next we'll move on to the front part. But before
assembly, since servos are used in this part, they need some configuration for protection. We
need to make the servo rotate to the 90 degrees when it's mounted, so the rotating range
can match with the application in the car. Otherwise, damages may be caused to servos.
PLEASE DO FOLLOW THIS STEP BEFORE MOVING ON!
And since the servos used in this kit are adjusted by software and there's no such physical
sticking point as other servos, here we need to configure the servo via software. First you
need to finish some software installation before the configuration.
er
A. Burn the image
d
Take the following steps before inserting the TF card into the Raspberry Pi:
DESKTOP and RASPBIAN STRETCH LITE are available, but RASPBIAN STRETCH WITH DESKTOP
would be a better choice if you have no special requirements.
nF
3) Unzip the downloaded package and you will see the xxxx-xx-xx-raspbian- stretch.img file
inside. Note: This file is NOT extractable.
Su
4) Open the win32DiskImager and insert the SD card into the computer with a card reader.
Select the image file and the drive (the card) in the tool. Click Write to write the system
to the SD card. Wait for a while until the system is written to the card.
5) When the progress bar comes to the end and a prompt "write successful" appears, click
OK.
29
6) Create a blank file ssh under the /boot directory to enable remote login and delete the
suffix in the file name.
7) Create a WiFi configuration file wpa_supplicant.conf under /boot and add the following
to the file:
d er
un
B. Car Power Supply
o
Now you can unplug the TF card from the PC and insert it into the Raspberry Pi. For power
nF
1) Supply the Raspberry Pi independently with a 5V/2A power – the software installation
Su
and calibration needs a long time and the battery won't be able to support.
2) Also keep the power on by battery – the servos and motors are powered by the battery,
so ensure the power is on for them when the RPi is supplied separately.
Plug in the USB Wi-Fi adapter (skip this if you use a Raspberry Pi 3 with built-in WiFi device) and
complete the setting in the way you're comfortable with.
After the blinking indicator LED on the RPi turns into steady light on, you can check the IP
address for the router. Now log into the RPi.
30
d er
o un
nF
Su
Type in ssh pi@ip_address – ssh is the tool for remote login, pi, the user name, and
ip_address, as the name suggests, your Pi's IP address. For example:
ssh [email protected]
Press Enter to confirm. If you get a prompt that no ssh is found, you need to install an ssh tool
like Ubuntu and Debian by yourself:
sudo apt-get install ssh
31
For Windows Users
If your computer runs on Windows, you need to pen the Bash shell with the help of some
software. Here we recommend a tool PuTTY.
1. Download PuTTY
2. Open PuTTY and click Session on the left tree-alike structure (generally it's collapsed
upon PuTTY startup).Enter the IP address of the RPi you just got in the textbox under Host
Name (or IP address) and 22 under Port (by default it is 22)
d er
o un
nF
Su
3. Click Open. Note that when you first log in to the Raspberry Pi with the IP address, you'll
be prompted with a security reminder. Just click Yes. When the PuTTY window prompts
login as: type in the user name of the RPi: pi, and password: raspberry (the default one,
if you haven't changed it).
Note: When you're typing the password in, the window shows nothing just null, but you're
in fact is typing things in. So just focus on typing it right and press Enter. After you log in the
RPi successfully, the window will display as follows.
32
This window is just like the Command Line window in Linux.
er
git clone https://github.com/sunfounder/Sunfounder_Smart_Video_Car_Kit_for_Raspb
erryPi.git
sudo raspi-config
Enable I2C:
Select Advanced Options => I2C => <Yes> => <Ok> => <Yes>
Select <Finish>. Close the window.
If a message of rebooting appears, click <No>. Before reboot, we still need to complete
some configurations.
G. Start calibration
Run the calibration server on the Raspberry Pi, and wait for client to connect.
Make sure that the circuit is connected properly. Power the smart car, open a terminal in
Linux. Connect with your Raspberry Pi via the ssh. Go to the directory
Sunfounder_Smart_Video_Car_Kit_for_RaspberryPi/server, run the server cali_server.py: if the
servo gets stuck with an abnormal sound, unplug the wires at once, and redo servo
adjustment for calibration in case of further damages.
33
cd ~/Sunfounder_Smart_Video_Car_Kit_for_RaspberryPi/server
sudo python cali_server.py
Then the contents of config are printed and the last line: Waiting for connection…
At this time, the servo might move a bit (this is the original position set).
er
Note: During the subsequent assembly, cali_server.py should keep running.
d
o un
nF
Su
34
Continue to Assemble
Note: Please keep cali_server.py running in the whole process of assembly.
A. Front Wheels
a) Fasten the F694ZZ flange bearing, driven wheel and following acrylic plates with an
M4*25 screw and an M4 self-locking nut in the way as shown below. Use the cross socket
wrench to fasten the M4 self-locking nut.
Pay attention to
er
the direction of
this plate
d
o un
nF
b) When completed, the assembly c) Bear in mind that DO NOT over-tighten the nut
should look like the figure below. or else the wheel cannot turn flexibly. Neither too
loose, in case the gap between the parts is too
large.
Su
35
d) Fasten the F694ZZ flange bearing, driven wheel and following acrylic plates with an
M4*25 screw and an M4 self-locking nut in the way shown as below.
Pay attention to
the direction of
this plate
er
e) When completed, the assembly f) Bear in mind that DO NOT over tighten the nut
should look like the figure below. d or else the wheel cannot turn flexibly. Neither too
loose, in case the gap between the parts is too
un
large.
o
nF
Su
36
B. Steering Linkage + Servo Rocker Arm
a) Connect the following acrylic plate to the second hole of the rocker arm (see the figure
below) with an M2*8 self-tapping screw.
The M2*8 self-tapping screw is contained in the package of the servo; it is one of the
two longest screws.
Also the rocker arm is packaged together with the servo. Note: Be careful with the
screw of pricking your fingers.
d er
o un
b) Tighten the screw first, then loosen it a little to ensure the Steering Linkage’s movement,
nF
37
C. Steering Servo + Upper Plate
a) Connect the servo to the acrylic plate below with two M2*8 screws and M2 nuts.
er
b) When completed, the assembly should look like the figure below.
d
o un
nF
Su
Note: The servo should be placed with its output shaft toward the front of the plate (see
the following figure).
38
D. Steering Servo + Steering Linkage
a) Connect the following parts with an M2*4 screw.
The M2*4 screw is contained in the package of the servo; it is the shortest of the screws
in the package.
Keep the Steering Linkage perpendicular with the middle of the car; otherwise,
remove it and mount again. DO NOT directly turn it in case of damaging the servo.
Minor errors are allowed, which can corrected later by programming.
d er
o un
b) When completed, the assembly should look like the figure below.
nF
Su
39
E. Front Chassis + Upper Plate
a) Connect the following parts and wheels with M3*8 screws, M3*24 copper standoffs and
M3 nuts, 4 for each.
d er
o un
nF
b) When completed, the assembly should look like the figure below.
Su
40
F. Plates + Servo Rocker Arms
a) Connect the rocker arm of the b) When completed, the assembly should
servo to the acrylic plate below with 4 be like the figure below.
M1.2*4 screws.
The rocker arm is packaged
together with the servo.
d er
un
c) Connect the rocker arm of the d) When completed, the assembly should
servo to the acrylic plate below with be like the figure below.
four M1.2*4 screws.
o
41
G. Pan/Tilt Servo + Plate
a) Connect the servo to the acrylic plate b) When completed, the assembly should
below with two M2*8 screws and M2 nuts, look like the figure below.
and we name it "pan servo".
d er
un
c) Connect the servo to the acrylic plate d) When completed, the assembly should
o
below with two M2*8 screws and M2 nuts look like the figure below.
and we name it "tilt servo".
nF
Su
42
H. Pan Servo Plate + Tilt Servo Plate
a) Connect the two plate parts b) When completed, the assembly should
together with two M3*10 screws and look like the figure below.
M3 nuts. The two plates should be
perpendicular to each other.
d er
un
c) Connect the two parts at a right d) When completed, the assembly should
o
angle with two M3*10 screws and M3 look like the figure below.
nF
nuts.
Su
43
I. Servos + Rocker Arm Plates
a) Connect the following parts without any b) When completed, the assembly should
screws. look like the figure below.
The two plates as indicated with arrows
below should be assembled in parallel;
otherwise, remove and mount again. DO
NOT directly rotate them in case of damages
to the servo.
d er
un
Top view: c) Then fasten them with an M2*4 screw.
Front view:
44
J. Mount + Car
Assemble the mount part and the car with two M3*10 screws and M3 nuts.
d er
o un
nF
When completed, the assembly should look like the figure below.
Su
45
------------------------ Now the car is completed. Congratulations! -----------------------------
The car should be assembled successfully as shown below:
To organize the wires, you can use the cable spiral wrap.
d er
o un
nF
Bottom view:
Su
46
Car Calibration (Operation on PC)
A. Get the source code
Download the source code directly from Github to your PC at:
https://github.com/sunfounder/Sunfounder_Smart_Video_Car_Kit_for_RaspberryPi,
Sunfounder_Smart_Video_Car_Kit_for_RaspberryPi.
Click Download ZIP on the sidebar of the page, as shown below.
d er
o un
nF
Su
After download, unzip the file. Find the file whose name containing "–master" and delete its
suffix.
The file name then should be: Sunfounder_Smart_Video_Car_Kit_for_RaspberryPi.
47
C. Run cali_client
Run the calibration client on PC to connect the server on Raspberry Pi.
er
Pi.
d
o un
nF
Su
After modification, save the file and click the Run menu, and select Run Module.
48
Find the variable of HOST:
er
Enter your own address of the Raspberry Pi there. Press Ctrl+O to save and Ctrl+X exit.
Run cali_client.py:
sudo python cali_client.py
d
un
No matter what system your computer is running on, Linux or Windows, when you run
cali_client.py, a window Raspberry Pi Smart Video Car Calibration will pop up:
o
nF
Su
In the terminal remotely connected with the Raspberry Pi, the IP address of the PC will be
printed.
49
Then you can start calibrating. Before that, take out your package box and place it vertically
with the side face to the table. Put the car onside the box and keep it balanced. The purpose
is to keep the wheels of the car off the table.
By default, the front wheels should be directly pointed towards the front; the camera on the
tilt servo should be face up no matter what directions the pan servo is pointed at.
er
D. Start Calibration
d
On the calibration UI there are three sections: Motor, Turning, and Mount.
o un
nF
Su
Motor Adjustment
Click Run. The car will walk forward. Check whether both the back wheels move forward
together. If either fails to do so, your wiring may be wrong. But don't worry! You don't need
to rewire; just click the corresponding Reverse in Motor section of the Calibration window
shown above. After clicking, observe whether the wheel you just adjusted is turning forward.
50
If the reversing works normally, click Run again to stop the wheels from spinning.
Turning Adjustment
Currently the front wheels should be pointed at the exact front direction. But if it is not, you
need to make some adjustments. In the Turning section in the Calibration window, click the
left ( ) and right ( ) arrow buttons in the upper line to make fine adjustments, and
those in the lower to coarsely adjust the turning direction. Keep adjusting until the wheels is
oriented to the front exactly. Then you may place the car on the table and click Run to verify
whether it runs in a straight line. If not, perform the adjustment again till it does.
er
Mount Adjustment
d
o un
nF
Now the pan servo on the car should be pointed at the exact front direction and the camera
Su
face up. But if not, you may need to adjust them similarly. In the Mount section, there are the
adjustments for the pan servo and tilt one. Also you have two kinds of adjustments, fine and
coarse. Keep adjusting until they are pointed at the right direction.
51
MJPG-streamer (Operation on Raspberry Pi)
A. MJPG-streamer Installation
Introduction
The acquisition and transmission of video data by the SunFounder Smart Video Car is fulfilled
based on MJPG-streamer.
MJPG-streamer is a command line application that copies JPG-frame from a single input
plugin to multiple output plugins. It can be used to stream JPEG files over an IP-based network
from the webcam to a viewer like Firefox, Cambozola and Videolanclient or even to a
Windows mobile device running the TCPMP-Player.
It was written for embedded devices with very limited resources in terms of RAM and CPU. Its
origin, the "uvc_streamer" was written, because Linux-UVC compatible cameras directly
produce JPEG-data, allowing fast and performant M-JPEG streams even from an embedded
er
device running OpenWRT. The input module "input_uvc.so" captures such JPG frames from a
connected webcam.
Installation
d
un
Plug the USB camera into Raspberry Pi, and run the command lsusb. The GEMBIRD represents
the USB camera; since it is printed on the screen, it indicates the system has recognized the
o
camera.
nF
lsusb
You'll see:
pi@raspberrypi:~ $ lsusb
Su
You'll see:
pi@raspberrypi:~ $ ls /dev/vid*
/dev/video0
52
sudo apt-get install libjpeg8-dev
sudo apt-get install imagemagick
Install:
sudo make DESTDIR=/usr install
B. Testing
Operation on Raspberry Pi
er
Then the video data acquisition will start, like this:
d
o un
nF
Su
Type in the following address (replace 192.168.0.126 with your Raspberry Pi IP address) at the
address bar of your browser (Firefox is recommended): (operation on PC)
http://192.168.0.126:8080/stream.html
Press Enter and you will see the view captured by the camera displayed on the screen in a
real-time manner.
53
Su
nF
54
oun
der
Get on the Road!
A. Run TCP server (Operation on Raspberry Pi)
Now a terminal's already open for remote login to the Raspberry Pi and run the mjpg-
streamer, and you need to keep it RUNNING. Open one more to log into the Raspberry Pi to
run tcp_server.
Go to the directory with cd.
cd ~/Sunfounder_Smart_Video_Car_Kit_for_RaspberryPi/server
The server program on the Raspberry Pi will be running and waiting for the client to connect
to the Raspberry Pi.
Click File -> Open -> Sunfounder_Smart_Video_Car_Kit_for_RaspberryPi -> client -> client
App.py to open this file, modify the value of the HOST for the IP address of the Raspberry Pi.
55
After modification, save the file and select Run -> Run Module.
For Linux users:
Open another terminal again. But DO NOT log into the Raspberry Pi remotely in this terminal.
Go to the client with cd and edit the file client.py:
Similar to changes in calibration, change the IP address in client program in your PC:
d er
o un
nF
After the alteration, press Ctrl + O and save and Ctrl + X to exit.
Su
No matter what system your computer is running on, Linux or Windows, when you run the
client_App.py, the following window will appear on your screen:
56
You can click buttons such as Forward and Backward to control the car moving remotely.
Or click X+, X-, Y+, and Y- to control the coverage of the camera.
er
Note:
The server program must be run before you run the client program. Some settings must be
d
completed for the server before the service is done. A communication endpoint needs to
be created for the server to "listen" to requests from the client. Take the server as a
un
receptionist or an operator of the bus phone in a company. Once the phone and device
installation is completed and the receptionist or operator is in place, the service begins.
o
nF
Su
57
Program Analysis and Explanation
Abstract
From the perspective of software, the smart car is of C/S structure. The TCP server program is
run on Raspberry Pi to listen to the command from the client and control the car accordingly.
The client program is run on the PC and connected with the server through the TCP, which
provides the user with a graphical user interface (GUI) to conveniently control the Raspberry
Pi remotely. Both the client and server programs are written in Python.
Make sure that the circuit is connected properly. Power the smart car, log in the Raspberry
Pi remotely, go to the directory Sunfounder_Smart_Video_Car_Kit_for_RaspberryPi and check
the files under it.
cd ~/Sunfounder_Smart_Video_Car_Kit_for_RaspberryPi
ls
er
pi@raspberrypi ~/Sunfounder_Smart_Video_Car_Kit_for_RaspberryPi $ ls
client datasheet html_server i2cHelper.py mjpg-streamer README.md server
d
You can see seven files under the directory: client, datasheet, html_server, i2cHelper.py,
un
mjpg-streamer, a file README.md, and server.
Wherein,
o
html_server, the web server run on the Raspberry Pi for Android app client,
i2cHelper, a simple script to help you set up i2c on the Raspberry Pi,
mjpg-streamer, the camera driver to acquire and upload images,
Su
58
1. Server
Here we provide a pseudocode which creates a universal TCP server for explanation. Note
that this is just one of the methods for server design. After you have a good knowledge about
it, you can alter the pseudocode as you want:
s = socket( ) # Create a socket for the server.
s.bind( ) # Bind the address to the socket.
s.listen( ) # Listen to the connection.
inf_loop: # Indefinite loop of the server.
c = s.accept( ) # Accept the connection from the client.
comm_loop: # Communication loop.
c.recv( )/c.send( ) # Dialog (receiving or sending data)
c.close( ) # Close the socket of the client.
s.close( ) # Close the socket of the server (optional).
All kinds of socket can be created via the function socket.socket( ) and then bound with IP
er
address and port by the function bind( ). Since TCP is a connection-oriented communication
system, some settings need to be completed before the TCP server starts operation. The TCP
d
server must "listen" to connections from the client.
un
After the settings are done, the server will enter an indefinite loop. A simple, like single-thread,
server will call the function accept( ) to wait for the coming connection. By default, the
function accept( ) is a blocking one, which means it is suspended before the connection
o
comes. Once a connection is received, the function accept( ) returns a separate client
socket for the subsequent communication. After the temporary socket is created,
nF
communication begins. Both the server and client use the new socket for data sending and
receiving. The communication does not end until either end closes the connection or sends
a null character string.
Su
59
Process Diagram of Server Program
d er
un
2. Client
o
It is easier to create a TCP client than to do a server. Take the following pseudocode:
c = socket( ) # Create a client socket.
nF
As mentioned above, all sockets are created via the function socket.socket( ). Then, the
function connect( ) can be called to connect the server. After the connection is built, the
dialog between the client and the server is enabled. When the dialog ends, the client can
close the socket and the connection.
Introduction of Tkinter
Developed based on Tkinter, our client program carries graphical interfaces. Tkinter is a GUI
widget set for Python. We can develop application programs with graphical interfaces fast
by Python language based on it. It is quite easy to use Tkinter. All you have to do is to import
the module into Python.
a) Import the Tkinter module (by import Tkinter or from Tkinter import *).
b) Create a top window object to contain the whole GUI program.
60
c) Create the GUI module needed on the object and enable the functions.
d) Connect the GUI modules with the code at the system back-end.
e) Enter the main event loop.
Take a simple GUI program:
Create a file Tk_test.py under the path /home:
touch Tk_test.py
er
top = Tk() # Create a top window
top.title('Sunfounder.com')
d
un
label = Label(top, text='Hello Geeks !', fg='blue') # Create a label and set
its foreground color as blue
label.pack() # layout
o
61
Process Diagram of Client Program
d er
o un
nF
Su
62
Summary
In this manual, having learned the related components for building the car kit, you've gone
through the assembly of the mechanical parts and electrical modules with the knowledge
of Raspberry Pi as well as a brief introduction of the key parts like servo, Wi-Fi adapter, etc.
Also you've got a lot of software and coding, which lays a solid foundation for your future
journey of exploring open-source field.
The SunFounder Smart Video Car for Raspberry Pi is not only a toy, but more a meaningful
development kit for Raspberry Pi. After all the study and hands-on practice of the kit, you
should have a better understanding of Raspberry Pi. Now, get started to make better work!
d er
o un
nF
Su
63
Copyright Notice
All contents including but not limited to texts, images, and code in this manual are owned
by the SunFounder Company. You should only use it for personal study, investigation,
enjoyment, or other non-commercial or nonprofit purposes, under the related regulations
and copyrights laws, without infringing the legal rights of the author and relevant right
holders. For any individual or organization that uses these for commercial profit without
permission, the Company reserves the right to take legal action.
d er
o un
nF
Su
64