Introduction
I already made a remote control car using arduino in my previous project. Now I improved it a bit, added a raspberry pi with camera and wifi dongle, so I had live video and the ability to control it over wifi instead of bluetooth. So here is the result:Part 1 - Hardware and arduino code
It's 95% the same as in my previous project: http://nomartini-noparty.blogspot.hu/2014/01/bluetooth-remote-control-car-2wd.html The only difference is that now I don't need a bluetooth modul or any battery, the arduino is connected to the raspberry pi via a simple usb cable. It handles the serial communication and also gives enough power for the microcontroller and the motors (H-Bridge power pins are connected to Vin and GND on arduino).
Part 2 - Raspberry Pi, remote control
Let's start with a raspberry pi running raspbian. First I configured wifi as written on this tutorial. Then I installed apache, vsftpd and php as written here. Also tornado for the sockets need to be installed with the commands: sudo apt-get install python-pip and pip install tornado. Then I edited BrowserBot by Dexter Industries and came to this:
- Replace /var/www/index.html with this file. It is basically the BrowserBot Client Code, the only change I made was to replace
<input id="touch" style="height: 100px; width: 100px; position: absolute; left: 550px; top: 100px" type="button" value="Accelerate" onclick="accelerate(); " />
with
<input id="touch" style="height: 100px; width: 100px; position: absolute; left: 550px; top: 100px" type="button" value="Accelerate" onmousedown="accelerate(); " onmouseup="stop(); " />
for every button. So with this change the car will go only forward as long as you press the button and instantly stops if you release.
What this website actually does, opens a socket and send a character to the pi when a button is pressed. - On the raspberry side we need to handle the socket. For this task this file need to be run. This is an edition of browserBot server code I removed/commented out the code drived the LEGO Robot and added the following lines:
to the beginning of the file:
import serial
import time
global ser
under if __name__ == "__main__":
ser = serial.Serial('/dev/ttyACM0', 9600)
and under WSHandler
def on_message(self, message): # receives the data from the webpage and is stored in the variable message
print 'received:', message # prints the revived from the webpage
if message == "u": # up
ser.write('F')
print 'F sent to arduino'
elif message == "d": #down
ser.write('B')
print 'B sent to arduino'
elif message == "l": #left
ser.write('L')
print 'L sent to arduino'
elif message == "r": #right
ser.write('R')
print 'R sent to arduino'
elif message == "b": #middle
ser.write('S')
print 'S sent to arduino'
def on_close(self):
print 'connection closed...'
Now if you run python arduino_server.py your raspberry will wait for command coming through the socket, and if comes, it sends the corresponding data to the arduino through serial port. For opening the socket on client side you only need to be on the same network as your pi and enter your pi-s ip address on the browser. If apache is correctly configured and running you should see the control site. Don't forget to fill in the ip address box with your pi-s ip, otherwise the socket will not work!
Part 3 - Video stream
First I tried various web based solutions, like motion and ustream but the lag was always too long to control the car real time. So I searched for lagless video stream on raspberry pi and ended up with this article. Which is quite good, but sadly use a raspberry cam and Mac. So after some research I ended up with the following code (after installing gstreamer1.0 on pi and my ubuntu):
- first run on ubuntu: gst-launch-1.0 -v udpsrc port=5001 caps="application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264" ! rtph264depay ! avdec_h264 ! videoconvert ! autovideosink sync=false
- then run on pi: gst-launch-1.0 -v -e v4l2src device=/dev/video0 ! 'video/x-raw,format=I420,width=640,height=480,framerate=30/1' ! omxh264enc target-bitrate=6500000 control-rate=variable ! rtph264pay pt=96 ! udpsink host=192.168.1.103 port=5001
Summary
So that's it, there is a lot to develop on this project, but I had only one day and don't know when will I be able to continue, so it was good to write down what I did. And if I could help someone, I'm happy to do so. If you have any question or comment, please feel free to write.
Best regards:
Mark
No comments:
Post a Comment