Building DeepPiCar


This page contains a few notes taken while building Deep Pi Car.

Disclaimer: As an Amazon Associate I earn from qualifying purchases

Goal

Building a tiny self driving car.

End Result

Resources

This sample project.

This medium blog.

Equipment

Setting up

On the local machine, setup the picar environment

1
2
3
4
5
pipenv --python 3.7
pipenv install pyqt5
pipenv install requests
cd client
python client.py

On the picar itself, you may run into some errors while importing cv2. Exporting the variable LD_PRELOAD may help resolve the issue.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
pi@model-t: $ sudo apt-get install libhdf5-dev -y && sudo apt-get install libhdf5-serial-dev -y && sudo apt-get install libatlas-base-dev -y && sudo apt-get install libjasper-dev -y && sudo apt-get install libqtgui4 -y && sudo apt-get install libqt4-test -y
pi@model-t: $ pip3 install opencv-python
pi@model-t: $ pip3 install matplotlib
pi@model-t:~/projects/SunFounder_PiCar-V/remote_control $ python3
Python 3.7.3 (default, Apr  3 2019, 05:39:12)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/pi/.local/lib/python3.7/site-packages/cv2/__init__.py", line 3, in <module>
    from .cv2 import *
ImportError: /home/pi/.local/lib/python3.7/site-packages/cv2/cv2.cpython-37m-arm-linux-gnueabihf.so: undefined symbol: __atomic_fetch_add_8
>>>
pi@model-t:~/projects/SunFounder_PiCar-V/remote_control $ LD_PRELOAD=/usr/lib/arm-linux-gnueabihf/libatomic.so.1.2.0 python3
Python 3.7.3 (default, Apr  3 2019, 05:39:12)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import cv2
>>>

To make this simpler, here is some config I’ve added in the pi’s ~/.bashrc:

1
2
3
4
# For errors repoprted while installing tensorflow
PATH=$PATH:/home/pi/.local/bin
# For errors while using cv2 under python3
LD_PRELOAD=/usr/lib/arm-linux-gnueabihf/libatomic.so.1.2.0

When remotely viewing the pi’s screen through VNC, the following lines may be needed to set the proper resolution in the pi’s /boot/config.txt:

1
2
3
4
5
# Uncomment and update, for VNC viewer screen resolution
framebuffer_width=2560
framebuffer_height=1440
# If above doesn't work, also comment the line below
#dtoverlay=vc4-fkms-v3d

Importing tensorflow into python caused some warning about Hadoop. The message can be ignored in our case, as we’re not using it. See https://github.com/tensorflow/tensorflow/issues/32885

1
2
3
4
5
>>> import tensorflow
2019-12-10 18:51:26.217429: E tensorflow/core/platform/hadoop/hadoop_file_system.cc:132] HadoopFileSystem load error: libhdfs.so: cannot open shared object file: No such file or directory

python3 -c 'import tensorflow as tf; print(tf.__version__)'
1.14.0

However, I also ran into the following error Error ModuleNotFoundError: No module named '_edgetpu_cpp_wrapper'.

At this time, https://github.com/f0cal/google-coral/issues/13 suggests the following solution

1
2
cd /usr/local/lib/python3.7/dist-packages/edgetpu/swig/
sudo cp _edgetpu_cpp_wrapper.cpython-35m-arm-linux-gnueabihf.so _edgetpu_cpp_wrapper.cpython-37m-arm-linux-gnueabihf.so

Some errors while trying to manually steer the PiCar may happen, with something like the following line

1
picar.Servo.Servo(1)

1
2
3
4
5
  File "/usr/local/lib/python3.7/dist-packages/SunFounder_PiCar-1.0.1-py3.7.egg/picar/SunFounder_PCA9685/Servo.py", line 36, in __init__
    self.pwm = PCA9685.PWM(bus_number=bus_number, address=address)
  File "/usr/local/lib/python3.7/dist-packages/SunFounder_PiCar-1.0.1-py3.7.egg/picar/SunFounder_PCA9685/PCA9685.py", line 47, in __init__
    self.bus = smbus.SMBus(self.bus_number)
TypeError: an integer is required (got type NoneType)

Just set the default

1
        self.pan_servo = picar.Servo.Servo(1, bus_number=1)