Code Explanation

Share for us
Share on facebook
Share on twitter
Share on pinterest
Share on whatsapp

There are two folders under /home/pi: SunFounder_PiCar for controlling motors and steering and SunFounder_PiCar-V for pan/tilt and wireless control. Here look into the code of PiCar-V.

Since too many contents and instructions are involved in the code, we will not cover every detail. For more knowledge about Python 2, Python 3, PyQt, Django, HTML, and CSS in the programs, you can visit related websites or buy books to learn by yourself. In the following part, we will go through the overall structure and the process in brief.

First, let’s check the files in the code folder:

■      .git is a hidden directory to store all the information of a Git repository; it’s generated automatically after you create the repository.

■      client is to store the code of the PC client.

■      datasheet stores the manual of some chips used in the hardware modules.

■      mjpg-streamer is an open source streaming media library, through which the data of the camera is transferred.

■      Remote_control is to store the code of the web server; it controls the motor and servo of the car based on the API request.

■      .gitignore records the requests of file types to be ignored when the Github repository is synchronized.

■ is a Python script written by Python 2 to configure and detect the I2C connection.

■      install_dependencies, an executable bash script for simple installation and environment configuration.

■      LICENSE, as the name suggests, is a text file of GNU V2 license.

■ and show record some information normally for statement and prompts.

Server Code

The server code is based on Django 1.10 (adapt to the latest release if needed). If you are interested in this, you can visit the Django website, or read The Django Book to learn more at Here we will just learn how the web server works. Note: The code may be updated irregularly on Github to fix bugs and release some functions update. So the code here is the initial version. You can view the updates in the Github repository at:

Open the code directory and check the file by ls:

pi@raspberry:~ $ cd SunFounder_PiCar-V/pi@raspberry:~/SunFounder_PiCar-V $ lsdatasheet     install_dependencies     mjpg-streamer  remote_controlclient  LICENSE       show

remote_control is the main code directory of the web server. Open the code directory by cd remote_control, and check the file by ls:

pi@raspberry:~/SunFounder_PiCar-V $ cd remote_controlpi@raspberry:~/SunFounder_PiCar-V/remote_control $ lsdb.sqlite3  remote_control  start  static 
  • db.sqlite3 is generated when you create a Django project.
  • is the main program of the Django project and is generated when the project is created. It normally does not need to be modified.
  • remote_control includes the main code files.
  • start is a small script written to run sudo python runserver, and servo installation with attribute install just for convenience.
  • static is to store some static pictures on the web.

The Django web server normally runs sudo python runserver to start. The address means the listening address covers all the addresses on the LAN, and the port number is 8000. Here we will just focus on the code in remote_control folder. Go to the directory via cd remote_control:

pi@raspberry:~/SunFounder_PiCar-V/remote_control $ cd remote_controlpi@raspberry:~/SunFounder_PiCar-V/remote_control/remote_control $ lsdriver  templates
  • driver stores the drivers of the car.
  • is automatically generated when you create a Django project which is a standard necessary document of python package, just leave it there.
  • is automatically generated and stores the related settings.
  • templates is a Django app for storing the webs in the html format.
  • is generated automatically to configure the URL to associate with the code.
  • is the code for page control which is associated by the URL. It calls the templates to show the page and the driver to control the car.
  • is generated automatically and does not need changes. For more, please visit the official website of Django.

So here is how the code works: Run the main program which will be associated with automatically, to respond to the URL. When you run the web browser like the Chrome to visit http://<rpi_ip_address>:<port> or visit the configured API via the client, the will turn to due to the association of the urls.pyThen the processes this and returns the templates to the browser. In addition, it will call the driver package on the basis of the parameters set in the browser to control the car. 

Now open the folder driver and check:

pi@raspberry:~/SunFounder_PiCar-V/remote_control/remote_control $ cd driver/pi@raspberry:~/SunFounder_PiCar-V/remote_control/remote_control/driver $  config

The driver folder mainly includes the driver modules for controlling the pan and tilt and camera streamer. for controlling the pan-and-tilt.

config stores the calibration data. is the essential file of the package and you can just leave it alone. is a video streaming service based on the MJPG-streamer.

Exit and open the folder templates to view:

pi@raspberry:~/SunFounder_PiCar-V/remote_control/remote_control/driver $ cd ../pi@raspberry:~/SunFounder_PiCar-V/remote_control/remote_control $ cd templates/   migrations   templates

This folder is created by the startapp just for calling the templates conveniently. Therefore, the files have not been changed except for the templates. Open the templates again:

pi@raspberry:~/SunFounder_PiCar-V/remote_control/remote_control/templates $ cd templatespi@raspberry:~/SunFounder_PiCar-V/remote_control/remote_control/templates/templates $ lsbase.html  cali.html  run.html

There are three HTML files that also consist of layers. The low level base.html stores contents contained on each page such as the <head> of HTML, the overall layout, and contents of the home page by default. The surface layer: cali.html for calibration and run.html to control the car.

So that’s the code of the server. Next we will come to the code of the client written by PyQt.

Client Code

In Windows, you can check the contents of client directory by grouping files by file type:

  •, is the main part of the code of the client.
  • is the python code converted from the UI resource file of Qt, which is the icons.qrc file beside, by pyrcc5 –o icons.qrc.
  • icons.qrc is the Qt resource file and restores the location of the resource pictures.
  • .ui is the ui file, or the graphical user interface (GUI) file designed by the Qt tool. Open it by a text editor and you will see the code is written similar to an xml file.
  • images stores the picture resources used in designing the UI (user interface). Do not rename or move the folder and the files in it, or else the corresponding content on the UI will be missing because it fails to find the graphic resource.
  • _pycache_ not shown in the figure above, is a cache folder automatically generated when you run a .py file of the client. If you’ve just downloaded it and not run yet, it’s not generated.

The code of the client is written in Python 3, and the GUI is designed by the Qt tool. The open source library PyQt is used in the to load the .ui file written by Qt into the python code.

For more information about PyQt, please visit the website:

The interfaces go one by one like this:

1. The login page: enter the Raspberry Pi’s IP address and click Log in go to the operation interface.

2. The operation interface will show the view captured by the camera. You can control the movement of the car on this page. You can click the return button, to go back to login, and the settings button to go to calibration interface.

3. The calibration interface: you can control and adjust the car by keyboard. Click OK to save the calibration data, and Cancel to return to the upper level interface.

Now after going through the code, you may wonder actually how to design the graphical interface and use PyQt. So let’s take a closer look!

1. Designing the Interface

Let’s start with the design interface.

In Windows, if you install the PyQt by the .exe package, select the option to install all, so the GUI design tools like the Qt Designer will be installed automatically. Or you can install the entire kit of Qt by yourself. Download the package (may need some setting and registration):

Open the .ui file with a Qt tool after the installation is completed. The Qt designer in Qt tools is a GUI design tool.

Design the first interface (login), with the GUI design tool. In the left column is the Widget Box section. You can just drag the components available here to the edit window in the center to see the effect directly. In the right column, on top is Object Inspector, where you can check the association and category of the added objects. Under this section, the property of the object is shown. You can set the attributes in this part, to differentiate all the widget objects.

Let’s take the picture of the login interface as an example. Drag a Label from Widget Box to the picture, then click on the label and there is a square around, by which you can change its size and position. Then in the property part, select pixmap under text and click the box next to it, and on the pop-up drop-down list, select to read the picture from resource or from file.

If you choose to read from file, find the picture to be used, select it and click OK; to read from resource, create the resource file first. It is very simple – by the pencil icon (edit button) in the window of creating from resource. Click the icon, and select to create a resource and load it.

The setting of other components such as button, line edit and on, is just similar, though the properties are different. The name of the objects whose property has been changed will be marked bold automatically for convenience in next check. You can open those .ui files to see the property details that can be changed, or the widget type by objects in Object Inspector. After editing, you can select Window -> Preview in the Qt Designer to see the effect, or by the shortcut Ctrl + R.

Don’t need to worry if you are not familiar with Qt because great details are given in the Qt document. If you don’t know which property is for what use, you can check Help or the Qt assistant. And those sample sketches of Qt will help a lot for you to learn about Qt.

2. Programing the Functions

Now the interface design is finished. However, the interface files are just some static images and cannot be put into use. So the next step, write the to implement all the interfaces with the corresponding functions as well as redirections. The PyQt is like this:

In Line 5, enter the name of the ui file you designed in the quotation marks.

In Lin 7e, “uic” is the contents in the previously imported PyQt; the built-in function loads the ui files.

In Line 9-13, the class defined is just your GUI window. Define (def) the corresponding functions in the class and write the functions in them. The first def __init __ (self) is the initialization function which is a constructor in C++. It means when the instance is created, the program will run the code and initialize the instance.

Line 15-19 is the entry of the __main__ function, where it starts to execute the whole program.

So here’s how the program goes:

1. Run the __main__ function and create an instance app of QApplication, which is the GUI application.

2. Create an instance window of the MyApp class type. After the creation is done, run the __init__ initialization function of the class.

3. The window instance is displayed.

4. Wait for a while. If the instance app of the GUI application sends an exit signal app.exec_(), then enter sys.exit() to exit Python.

So that’s all for the code. Here we’ve only looked at the entry-level usage. For more detailed code explanation, go to the file. There you can see very detailed comments.