lukas / robot

Simple library for controlling a raspberry pi based robot
177 stars 53 forks source link
camera raspberry-pi robot robotics sonar wheel

robot

This will run a simple robot with a webserver on a raspberry PI with the Adafruit Motor Hat. I wrote this up for myself for fun and to help me remember how I set things up.

High level overview can be found in this article: https://www.oreilly.com/learning/how-to-build-a-robot-that-sees-with-100-and-tensorflow

Hardware

To get started, you should be able to make the robot work without the arm, sonar and servo hat.

Programs

Example Robots

Here are two robots I made that use this software

Robots

Wiring The Robot

Sonar

If you want to use the default sonar configuation, wire like this:

You can modify the pins by making a robot.conf file.

Wheels

You can easily change this but this is what wheels.py expects

Installation

basic setup

There are a ton of articles on how to do basic setup of a Raspberry PI - one good one is here https://www.howtoforge.com/tutorial/howto-install-raspbian-on-raspberry-pi/

You will need to turn on i2c and optionally the camera

raspi-config

Next you will need to download i2c tools and smbus

sudo apt-get install i2c-tools python-smbus python3-smbus

Test that your hat is attached and visible with

i2cdetect -y 1

Install this code

sudo apt-get install git
git clone https://github.com/lukas/robot.git
cd robot

Install dependencies

pip install -r requirements.txt

At this point you should be able to drive your robot locally, try:

./robot.py forward

server

To run a webserver in the background with a camera you need to setup gunicorn and nginx

nginx

Nginx is a lightway fast reverse proxy - we store the camera image in RAM and serve it up directly. This was the only way I was able to get any kind of decent fps from the raspberry pi camera. We also need to proxy to gunicorn so that the user can control the robot from a webpage.

copy the configuration file from nginx/nginx.conf to /etc/nginx/nginx.conf

sudo apt-get install nginx
sudo cp nginx/nginx.conf /etc/nginx/nginx.conf

restart nginx

sudo nginx -s reload

gunicorn

install gunicorn

copy configuration file from services/web.service /etc/systemd/system/web.service

sudo cp services/web.service /etc/systemd/system/web.service

start gunicorn web app service

sudo systemctl daemon-reload
sudo systemctl enable web
sudo systemctl start web

Your webservice should be started now. You can try driving your robot with buttons or arrow keys

camera

In order to stream from the camera you can use RPi-cam. It's documented at http://elinux.org/RPi-Cam-Web-Interface but you can also just run the following

git clone https://github.com/silvanmelchior/RPi_Cam_Web_Interface.git
cd RPi_Cam_Web_Interface
chmod u+x *.sh
./install.sh

Now a stream of images from the camera should be constantly updating the file at /dev/shm/mjpeg. Nginx will serve up the image directly if you request localhost/cam.jpg.

tensorflow

There is a great project at https://github.com/samjabrahams/tensorflow-on-raspberry-pi that gives instructions on installing tensorflow on the Raspberry PI. Recently it's gotten much easier, just do

wget https://github.com/samjabrahams/tensorflow-on-raspberry-pi/releases/download/v0.11.0/tensorflow-0.11.0-cp27-none-linux_armv7l.whl
sudo pip install tensorflow-0.11.0-cp27-none-linux_armv7l.whl

Next start a tensorflow service that loads up an inception model and does object recognition the the inception model

sudo cp services/inception.service /etc/systemd/system/inception.service
sudo systemctl daemon-reload
sudo systemctl enable inception
sudo systemctl start inception