Raspberry Pi Robot with screen, controller, neopixels, and openCV

Raspberry Pi Robot with screen, controller, neopixels, and openCV

I bought and made this kit robot from Coretec, the Tiny4WD

Some tips on the initial build:

Now I have the controller, neopixels and camera working with OpenCV I thought I'd blog my notes on how I set everything up. All the python code I use to test is on github: https://github.com/Lorrainbow/PiWarsTiny4WD

Setup your Raspberry Pi

If you've never setup a pi before you can follow my other post: /set-up-your-raspberry-pi-without-a-monitor/ to setup a Raspberry Pi without a monitor.

I used an older version of Raspbian: 2019-04-08-raspbian-stretch and created a directory called piwars.

mkdir piwars

Turn on the camera

sudo raspi-config
interfacing options
enable camera: yes
Yes to reboot

Install software

Install explorer library with examples

The explorer library is the library to control the motors. It's from Pimoroni.

curl https://get.pimoroni.com/explorerhat | bash

Install python3 explorer library

sudo apt-get install python3-explorerhat 

Test your setup with:

sudo python3 testmotors.py

Make sure the wheels are all going in the right direction. You might want to hold onto your robot while testing this one!

Install neopixel library

I have a strip of neopixels on my robot, let's light em up.

sudo pip3 install adafruit-circuitpython-neopixel

Test with:

sudo python3 testlights.py

Test the camera

With the camera setup we just need to check the connections are good. Take a photo with this script and check the image is being saved. It should save it as image.jpg in the piwars directory. I use FileZilla to download files when not using a monitor with the pi.

sudo python3 testCamera.py

Install the screen

I have a small screen attached to the Pi. Let's get that working.

sudo apt-get install i2c-tools python-smbus python-pip python-dev python-imaging

Check for screen, should see 3c in the grid

i2cdetect -y 1

Get the library

sudo git clone https://github.com/tkurbad/sh1106.git
cd sh1106
sudo python3 setup.py install

Test the screen

sudo python3 testAll.py

This will test everything - the motors, neopixel and the screen. Lights, Camera, action!

Change directory to edit file to make it work with python3

sudo nano /usr/local/lib/python3.5/dist-packages/oled/device.py
  • replace xrange with range (6 times)
  • replace / 8 with // 8 (twice)

Test everything again

sudo python3 testAll.py

Install opencv

OpenCV is a library that allows your robot to recognise things through the camera. It can recognise colours, it can recognise objects. This will be useful if you want to try and compete autonomously in some of the piwars challenges.

sudo apt install libatlas3-base libwebp6 libtiff5 libjasper1 libilmbase12 libopenexr22 libilmbase12 libgstreamer1.0-0 libavcodec57 libavformat57 libavutil55 libswscale4 libgtk-3-0 libpangocairo-1.0-0 libpango-1.0-0 libatk1.0-0 libcairo-gobject2 libcairo2 libgdk-pixbuf2.0-0
sudo pip3 install opencv-python-headless
sudo pip3 install imutils

Test it, this code searches for some colours in an image taken by the camera. Edit it to change the colours for your photograph.
It will print:
Does it have red?
Answer: Yes

And it will also save the original photo and a new photo with just red highlighted.

sudo python3 cameraFindColours.py

Install the controller

I'm using the rock candy controller.

sudo pip3 install approxeng.input
sudo pip3 install inputs
sudo nano /usr/local/lib/python3.5/dist-packages/approxeng/input/__init__.py

We need to edit some code to make it work, on line 375 change to

xname = f'{rootname}x'
yname = f'{rootname}y'


xname = str(rootname)+'x'
yname = str(rootname)+'y'


sudo nano /usr/local/lib/python3.5/dist-packages/approxeng/input/controllers.py

remove line on 199

controller_class: Type[Controller]

Now run everything with rockcontroller.py. This script is based on this one:

sudo python3 rockcontroller.py

This will let you drive the robot with the rock candy controller, the neopixels will turn green and the screen will show your button presses. I edited the script to stop running when you press Select (instead of Home)

There you go! A Tiny4WD kit robot that works with a screen, neopixels, a camera and a controller. With some more code and a few more accessories this could be a good contender for PiWars.

What next?

It depends what you want to do!
Do you want to try and complete some of the challenges autonomously? You'll need a lot more code! But the code above is a good start.

Do you want to fire bullets at targets in one of the challenges? You'll need to build something extra and code it. Will the battery manage all of this?

I'm going to focus on the autonomous code, specifically spotting colours in a photo and heading towards them. Watch this space!

Related Article