In addition to using the keyboard to control the status of the car, we also wrote a ball tracking code.
Before installing OpenCV, you need to install the dependencies first. Different operating systems require different dependent libraries. (If you don’t know your operating system version, you can try to install both.)
If your operating system is RASPBERRY PI BUSTER, you need to install the following dependencies.
sudo apt-get install libatlas-base-dev libjasper-dev libqt4-test libwebp6 libtiff5 libopenexr23 libgstreamer1.0-0 libavcodec-dev libavformat-dev libswscale-dev libqtgui4 -y
If your operating system is RASPBERRY PI BULLSEYE, you need to install the following dependencies.
sudo apt-get install libatlas-base-dev libjasper-dev libwebp6 libtiff5 libopenexr25 libgstreamer1.0-0 libavcodec-dev libavformat-dev libswscale-dev -y
Next is to install OpenCV.
sudo pip3 install opencv-python==22.214.171.124
Run the Code
cd ~/SunFounder_PiCar-V/ball_track python3 ball_tracker.py
After the code runs, find a red ball (at least larger than the diameter of the camera) and put it within 10-50cm range in front of the camera to get the car to follow your ball.
If you want to know whether the car has catched your ball, you can do as follows:
Step 1: Enable the VNC on the Raspberry Pi.
Choose 3 Interfacing Options by press the down arrow key on your keyboard, then press the Enter key..
Press the left arrow key on your keyboard to choose Yes.
Finally select OK->Finish to exit the config.
Step 2: Download the VNC Viewer for your computer and install it.
During your installation on your Windows, if you encounter the following prompt, please click Continue to continue your installation.
Step 3: Open VNC Viewer you have installed, type in your Raspberry Pi IP address, and press the Enter button.
Step 4: Enter the username and password of your Raspberry Pi on this page, by default, they are pi and raspberry; then click OK.
When you log in with VNC Viewer, the screen may be black.If your screen is working, skip to Step 5.
Changing the resolution to the highest on the Raspberry Pi.
Choose 2 Display Options.
Choose D1 Resolution.
Change the default to a higher resolution like Mode 16, then click OK.
Reboot the Raspberry Pi, log in again, and it will work.
Step 5: Now, we enter the Raspberry Pi desktop, click Terminal and type the following commands to open the code ball_tracker.py.
sudo nano ball_tracker.py
Then modify the code as follows:
Show_image_enable = True
Draw_circle_enble = True
Press Ctrl+X and Y to save the change.
After you finish doing this step, you must log in the Raspberry Pi via VNC, or you can use a monitor; if not, the warning is as follows:
Step 6: Run the ball_tracker.py.
Step 7: After running the code ball_tracker.py, these four images will appear. If you place a red ball in front of the camera, you can see a green circle which represents that the car has catched your red ball.