What I needed:
- Raspberry pi 3 model B, picamera, SD card, monitor, external mouse, female-male jumper wires.
- External battery pack 5V, ~1A -> 2.5A.
- A GOOD RC Car (The project will be VERY troublesome if you have a mediocre RC Car with mediocre servo). Make sure that the car is controllable in both speed and direction. A fast car will make it hard to control in general. An inconsistent servo will make it hard to predict the angle to which the car moves when turn. Do Not make the same mistake as me.
The goal of this project is to:
- Learn how Raspberry Pi GPIOs and PiCamera works
- Learn how to create interactive GUI with pygame
- Use Intel OpenCV to detect lanes.
- Apply previously learned NN models to something more fun and practical
The Project can be divided into 5 steps:
- Setting up the Raspberry Pi and the RC Car
- Use Socketing to feed images from the Raspberry Pi to the Computer
- Train a model (Feel free to use other libraries such as Keras or Tensorflow)
Step 1) Setting up the Raspberry Pi and the RC Car:
- Install Raspian, set up wifi, remote desktop, WinSCP for sharing file. Tutorials: https://pythonprogramming.net/introduction-raspberry-pi-tutorials/
- I need a way to send signal to the RC Car from the Raspberry Pi. In order to do so, I use bskari lib: https://github.com/bskari/pi-rc. Basically, run his programs on the raspberry pi will iterate through all the possible frequencies, burst micro sec, and spacing to find the correct frequencies that move your car (my freq is 26.995). Afterward, test all the repeats from 0 -> ~155 signal to find the appropriate commands (forward, backward, left, right, forward left, etc). -> Export to a JavaScripe Object Notation (JSON) file.
Step 2) Use Socketing to feed images from the Raspberry Pi to the Computer
- Set up a socket connection between your computer and the raspberry pi for image streaming: http://picamera.readthedocs.io/en/release-1.10/recipes1.html. Or you can use multithreading as well to stream more fps.
- Create a GUI and connect command signals to keyboard buttons. See control.py and pygameContol.py
- Read stream images, preprocess it, and save the data before feeding it through the NN (resizing and normalizing are important, convert to gray scale is optional). See LaptopServerDataCollection.py
Step 2.5) Putting everything together:
- Remote desktop to your RaspPi, start bskari pi_pcm so that control.py able to send signal command to the RaspPi.
- Another Remote desktop to your RaspPi, start RaspSensorClientInput.py
- On your Desktop, run LaptopServerDataCollection.py, you will see a pygame window with piCamera vision that will take arrow inputs from your keyboard. Press 's' to start collecting data and 'p' to pause. Quit pygame window if done collecting. The program will save the collected data to an .npy file (~10000 points).
Step 3) Train a model (Feel free to use other libraries such as Keras or Tensorflow)
- I created my own Neuronetwork from scratch using numpy and sklearn for second order l-bfgs optimizing method. However, my model only have sigmoid activation function, which cannot avoid gradient vanishing problem. ELU or ReLU activation functions probably works better. 1.5) Balance your data
- Train your data. This has to be the most exciting part because you are about to know the result. My result was not great because of my cheap and inconsistent car servo. However, the project in general was great. I learned alot about the Raspberry pi and picamera. However, if you got a good car, you can take one step furthur than me and build a great self-driving RC car :).
Extra) Recognizing lanes (white papers) using OpenCV. See Lane-Finding.py based on a tutorial on PythonProgramming.net
See Lane-Finding.py