Saturday, January 24, 2015

Rover

WiFi Rover -Arduino and Raspberry Pi

This Rover uses:
  1. Adafruit motor shield 
  2. Sparkfun Magician Chassis
  3. Arduino Uno R3
  4. Raspberry Pi
  5. Wireless Dongle
  6. Raspberry Pi Camera
Install Raspian to SD card and used GUI to attach to wireless AP. Save Config.  This is the only time I used GUI.  The rest of project uses Python, Bash, and Arduino.  Completed this using Mac OS X but possible to do so with Windows using PuttY.

After obtaining IP Address, ssh into pi:

ssh pi@<IP-address>

Camera dependencies and sources
sudo apt-get install libjpeg8-dev imagemagick libv4l-dev apache2 build-essential

sudo ln -s /usr/include/linux/videodev2.h  /usr/include/linux/videodev.h

wget http://lilnetwork.com/download/raspberrypi/mjpg-streamer.tar.gz

tar xvzf mjpg-streamer.tar.gz

cd mjpg-streamer/mjpg-streamer

make

sudo cp mjpg_streamer /usr/local/bin

sudo cp output_http.so input_file.so /usr/local/lib/

sudo cp -R www /usr/local/www

mkdir /home/pi/streaming


Install PySerial to communicate with Arduino over Serial

https://pypi.python.org/pypi/pyserial

Download Source

tar -zxvf pyserial-2.7.tar.gz

cd pyserial-2.7

chmod +x setup.py

sudo python setup.py install

Arduino Code for Robot

Robot.ino
Upload code to Arduino on local computer.  Can test using Serial input in Arduino IDE


Python Script

nano bot.py



#import serial

ser = serial.Serial('/dev/ttyACM0', 9600)
while 1:
 
 var = raw_input();
 if var == 'p':
  print("Stop")
  ser.write('p')
 elif var == 'd':
  ser.write('d')
  print('Turn Right') 
 elif var == 'w':
  print("Forward")
  ser.write('w')
 elif var == 's':
  ser.write('s')
 elif var == 'a':
  print("Turn Left")
  ser.write('a')
 elif var == 'q':
  print("Quit") 
  quit()
 else:
  ser.write('p')

Control + o to save
Press enter to accept

Control + x to exit

chmod +x bot.py

sudo python bot.py

Bash Script
This will start the camera and publish to /usr/local/www

#!/bin/bash

raspistill --nopreview -w 640 -h 480 -q 5 -o /home/pi/streaming/pic.jpg -tl 100 -t 9999999 -th 0:0:0 &

sleep 3

LD_LIBRARY_PATH=/usr/local/lib mjpg_streamer -i "input_file.so -f /home/pi/streaming -n pic.jpg" -o "output_http.so -w /usr/local/www"

Save as cameraStart.sh
control + o

chmod +x cameraStart.sh

./cameraStart.sh

Make script start at boot

crontab -e

@reboot /home/pi/cameraStart.sh

sudo reboot

Now start a web browser on your computer
http://<IP-address>:8080

ssh pi@<IP-Address>

sudo python bot.py







To give credit where credit is due, for the camera streaming I used parts of this great tutorial.  Not all of it worked for me, so I modified it to work for me.  
My code may not work for you either and you'll need to be able to modify it to fit your project







No comments:

Post a Comment