DonkeyCar

lang
en
date
Sep 30, 2023
slug
Projects-01-en
status
Published
tags
DonkeyCar
summary
Experience the DonkeyCar
type
Projects

System Installation

Download Burning Tool ( RaspberryPi )
Please select the corresponding version of your computer to download
notion imagenotion image
Select the file donkeycar-pi-buster-2023-08-01.zip from the server and download it .
notion imagenotion image
Click the last item to select your system, and choose the system you just downloaded to burn.
notion imagenotion image

Wi-Fi connection

We have installed the system, but without a monitor it is troublesome to link Wi-Fi through the graphical interface, so we add a configuration file to link Wi-Fi. donkeyCar stays connected wirelessly, which makes it easier for us to control it from the intranet.
Download this file
Open the sdcard with DonkeyCar on your computer and copy the wpa_supplicant.conf file to the root directory of the sdcard.
Using a vscode text editor, open the wpa_supplicant.conf file.
Enter the Wi-Fi name in ssid (line 5)
Enter the Wi-Fi password in psk (line 6)
notion imagenotion image
Click save after the modification is done, just put the system sd card into the cart and wait for the system to boot up the OLED screen will show the IP address.

OLED driver

In order to facilitate every time you start the cart does not need to connect to the monitor to check the IP, the IP address is displayed on the OLED, which is convenient for ssh direct connection. Because of the system update, the Docker virtual environment is used, so the driver configuration directory of the file has been changed, and the OLED will display garbled code because of the lack of driver configuration.
notion imagenotion image
First use the cd command to return to the home page
cd
Use the git command to download the required screen driver from GitHub.
git clone https://github.com/Nabaixin/TFOLED.git
After the download is complete, we use ls to view all the files in the current directory.
At this point we will see a TFLOED folder, which is the driver we need.
ls
Now we need to replace the original driver with ours, sudo to extract, cp -p to copy, followed by the directory where we want to paste the files.
sudo cp -P TFOLED/NBX_OLED/OLED.py /var/lib/docker/overlay2/b 76aa24aae347fe87c4738d315c2f1224f1b248cca93eb27e1324c174aaf8ac2/merged/virtualen v/python3/1ib/python3.7/site-packages/Adafruit_SSD1306/SSD1306•py
The full command is shown below
notion imagenotion image
Wait 30 seconds, the driver configuration is normal, the OLED screen is no longer splash screen, it has been displayed normally!
notion imagenotion image

Start the cart

Login to the background of the trolley
First of all, we open the browser, according to the IP of the trolley, enter the corresponding IP in the address bar of the browser, and remember to add port8888 after the IP address.
Example: My IP login address is 192.168.0.242:8888.
According to the IP address of your car, change it to the corresponding IP address.
notion imagenotion image
The login password for the background is:
jupyter
After successfully logging in, you will see the dashboard, we need to use the file management and terminal
notion imagenotion image
Create mycar
Open the terminal and perform the following steps
notion imagenotion image
Copy the following command to create mycar
pi@docker-raspberrypi:~$ ls
pi@docker-raspberrypi:~$ donkey createcar
pi@docker-raspberrypi:~$ cd mycar/
Calibrate the car
As the car will be running when calibrating it, it is important to keep it up during the operation.
notion imagenotion image
First perform the calibration and find the PWM range for the steering and throttle values.
The appropriate signal range for the cart is approximately 250-500.
Calibrate the throttle values
After the 373 values have been entered, turn on the motor switch of the cart.
pi@docker-raspberrypi:~$ donkey calibrate --channel 0 --bus 1
notion imagenotion image
Press q to exit after calibration
Calibrate direction value
pi@docker-raspberrypi:~$ donkey calibrate --channel 1 --bus 1
notion imagenotion image
Press q to exit after calibration
Modify the configuration file
We click on the myconfig.py file in the mycar folder on the left side of the dashboard, we need to change some configuration.
notion imagenotion image
After opening the file, we need to modify the following lines of code, removing the # character (comment) and a space.
Lines 53, 54, 55, 63, 64, 65, 66, 143, 146, and 291.
The last line of code (line 291), you need to add a comment, add the # sign in front of the code can be
After the modification, the following picture is shown
notion imagenotion image
notion imagenotion image
notion imagenotion image
Press command+s to save after all changes are made.
Start the cart
Let's go back to the terminal and enter the following code to start the trolley program.
pi@docker-raspberrypi:~$ python manage.py drive
notion imagenotion image
If you want to collect the data of the cart to drive automatically, you need to execute this command.
pi@docker-raspberrypi:~$ python manage.py drive --js
Enter the IP of the cart + port 8887 in your browser to access it.
For example, my access address is 192.168.0.242:8887.
notion imagenotion image
On the right side of the background camera screen is a touchpad to operate the cart, so you can run the cart happily!
If you want the car to run automatically, you need to run at least 10,000 pieces of data.

Data training

We have now completed a series of configuration files, has allowed the car to run, now we want to let the car through the camera to learn the driving route, so that the car through the data learning, to be able to run automatically.
ZIP learning data compression
First we go to the mycar folder
cd ~/mycar/
The learning data is saved in the data directory under the mycar folder, choose the most satisfactory data and delete the rest of the data, so as to ensure the accuracy of the data.
If there is a data.zip file in the past, use the rm command to delete it.
rm data.zip
Use the zip command to compress the data folder.
zip -r data.zip data/
Wait for the compression to complete and there will be a data.zip archive in the mycar folder
Learning through Colab (GPU);
We enter Colab and create a new .ipynb file, add the code list copy the code below and run it
Confirm Colab OS
!cat /etc/os-release
Uninstall existing CUDA
Since we don't know what's installed on the colab instance, it's okay to get an error here.
!apt --purge remove "cublas*" "cuda*"
Add CUDA-10.1 repository
!apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64/7fa2af80.pub !wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-keyring_1.1-1_all.deb !dpkg -i cuda-keyring_1.1-1_all.deb
CUDA-10.1 installation, takes about 10 minutes
!apt-get update !apt-get install -y cuda-10.0
CuDNN-7.6.5 installation
!wget https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64/libcudnn7_7.6.5.32-1+cuda10.0_amd64.deb !wget https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64/libcudnn7-dev_7.6.5.32-1+cuda10.0_amd64.deb !wget https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64/libnccl2_2.6.4-1+cuda10.0_amd64.deb !wget https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64/libnccl-dev_2.6.4-1+cuda10.0_amd64.deb !dpkg -i \ libcudnn7_7.6.5.32-1+cuda10.0_amd64.deb \ libcudnn7-dev_7.6.5.32-1+cuda10.0_amd64.deb \ libnccl2_2.6.4-1+cuda10.0_amd64.deb \ libnccl-dev_2.6.4-1+cuda10.0_amd64.deb
Install TensorFlow
Install the same version of TensorFlow that is installed on DonkeyCar's RaspberryPi3. The default tensorflow/cuda/pip package version from Google Colab will be updated. Since Tensorflow does not maintain backward compatibility, Colab uses the same version of Tensorflow as the Raspberry Pi version of DonkeyCar's standard TensorFlow 1.13.1. Google Colab no longer supports Tensorflow 1.x, so it can no longer be found using pip. Therefore, we will install Tensorflow 1.13.1 using Miniconda.
%%bash MINICONDA_INSTALLER_SCRIPT=Miniconda3-4.5.4-Linux-x86_64.sh MINICONDA_PREFIX=/usr/local wget https://repo.continuum.io/miniconda/$MINICONDA_INSTALLER_SCRIPT chmod +x $MINICONDA_INSTALLER_SCRIPT ./$MINICONDA_INSTALLER_SCRIPT -b -f -p $MINICONDA_PREFIX
import sys _ = (sys.path.append("/usr/local/lib/python3.6/site-packages")) !pip install -U pip !pip uninstall -y tensorflow !pip install tensorflow-gpu==1.13.1 h5py==2.10.0 protobuf==3.19.6 numpy==1.19.5 pillow==8.4.0 cycler==0.11.0 decorator==4.4.2 python-dateutil==2.8.2 wcwidth==0.2.6
Please perform a RESTART RUNTIME. The installed Tensorflow 1.13.1 will remain even after the restart. After the reboot, you can start from the next step.
Re-adding the Python PATH after a reboot
import sys _ = (sys.path.append("/usr/local/lib/python3.6/site-packages"))
Check the TensorFlow version
!pip freeze | grep -i -e tensorflow
!python -c "import warnings;warnings.simplefilter(action='ignore', category=FutureWarning);import tensorflow as tf; print(tf.__version__)"
Install DonkeyCar
Match the version of DonkeyCar installed on the Raspberry Pi.
# 如果找不到 imageio,DonkeyCar 自动安装的 imageio 版本为 2.16.0,最新截至 2023/03/02。 这需要 numpy1.20.0 或更高版本。 # 截至2023/03/02,可与google colab一起使用的numpy最高为1.19.5,因此请指定imageio版本并安装。 !pip install imageio==2.15.0
!git clone https://github.com/autorope/donkeycar.git %cd donkeycar !git checkout be45ae038cb00d81c5450b9a481dbf5aaa03e688 !pip install -e .[pc]
Create the DonkeyCar project
!donkey createcar --path /content/mycar
Set CACHE_IMAGES to False. This is set to reduce memory consumption during learning. Comment out GYM_CONF["guid"]. As of 20200825, this appears to be enabled, as development takes place mostly in virtual tournaments, but is not necessary during learning.
!sed -i 's/# CACHE_IMAGES = True/CACHE_IMAGES = False/g' /content/mycar/myconfig.py !sed -i 's/use_multiprocessing = False/use_multiprocessing = True/g' /content/mycar/train.py !sed -i 's/^GYM_CONF/# GYM_CONF/g' /content/mycar/myconfig.py
Uploading data.zip and extracting data
Upload data.zip (learning data collected with DonkeyCar). How to upload data.zip to colab via browser and Upload data.zip to colab via Google Drive There are two ways. If the communication speed is too slow using the browser method, please try using the Google Drive method.
When uploading data.zip through a browser
Upload data.zip (learning data collected with DonkeyCar). When you run the following code, a file upload form will appear. The file is sent to /content/mycar/ and data.zip is being extracted. The directory structure of the dataset is /content/mycar/data/tub_1_xxxx.
%cd /content/mycar/ import os import shutil from google.colab import files if(os.path.exists("/content/mycar/data.zip")): os.remove("/content/mycar/data.zip") if(os.path.exists("/content/mycar/data")): shutil.rmtree("/content/mycar/data") uploaded = files.upload() file_name = list(uploaded.keys())[0] file_path = "/content/mycar/" + file_name print("-----------------------------------------------------") print("【処理結果】%sにデータをアップロードしました。" % file_path) print("-----------------------------------------------------") !unzip -o data.zip
When uploading data.zip via Google Cloud Drive
Uploading data through your browser is very slow, so use it if you want to do it quickly through Google Drive. By executing the code below and authenticating yourself, you will be able to use the data.zip file uploaded to Google Drive via Google Colab.
from google.colab import drive drive.mount('/content/drive') !ls '/content/drive/My Drive' | grep data
Google Drive is installed in /content/drive/MyDrive. Copy the data.zip file to /content/mycar/ and extract it. If you uploaded to any folder in Google Cloud Drive, replace it with "/content/drive/MyDrive/any folder name/data.zip".
%cd /content/mycar/ import os import shutil if(os.path.exists("/content/mycar/data.zip")): os.remove("/content/mycar/data.zip") if(os.path.exists("/content/mycar/data")): shutil.rmtree("/content/mycar/data") !ls -l '/content/drive/MyDrive/data.zip' !cp '/content/drive/MyDrive/data.zip' /content/mycar/data.zip !unzip data.zip
Double-click or press Enter to edit
Getting Started
Now that we have created our Colab environment, we will learn about DonkeyCar.
The learning target will be all the folders present in /content/mycar/data/.
type = "linear" trained_mode = "/content/mycar/models/mypilot.h5" %cd /content/mycar !python manage.py train --model={trained_mode} --type={type}
Confirming Learning Outcomes
DonkeyCar is able to analyze a trained model and output it as a video. Check how well the trained model turned out. Since each model has completely different layers, makemovie can only be used for linear models.
Install the model visualization package
!pip uninstall -y keras-vis !pip install git+https://github.com/sctse999/keras-vis !apt install -y ffmpeg !pip install opencv-python==4.1.2.30
Creating video files
Multiple tubs are not supported, so you must specify a data directory using the --tub=tub_xxxx option.
!ls /content/mycar/data
Select one of the displayed data directories and rewrite it as tub_xxxx. You can easily check if the directory does not have a large number of data items.
%cd /content/mycar mp4_path = 'tub_movie.mp4' !donkey makemovie --tub=data/tub_xxxx --model models/mypilot.h5 --type linear --salient --out=$mp4_path
Video Download
Downloads the created analysis video to your PC. You can check the response of the model by playing the downloaded video on your PC. Green line: Human operation. Blue line: AI judgment.
from google.colab import files files.download('/content/mycar/'+mp4_path)
Migrating Learning Results
Learning results are saved in /content/mycar/models/mypilot.h5. Run the cell below and download the H5 file. Copy the learning results to ~/mycar/models/mypilot.h5 on the RaspberryPi and DonkeyCar will run automatically.
from google.colab import files files.download('/content/mycar/models/mypilot.h5')
At this point already both files have been downloaded locally by the
notion imagenotion image
My Learning Model Analysis Video
 
 
对于本文内容有任何疑问, 可与我联系.