Wifi Websocket Robot

Overview

This project is a proof of concept of controlling a robotic car over WiFi using websockets from a webpage on the internet. The used hardware components include: mbed LPC1768, TB6612FNG Dual Motor Driver Carrier, ESP8266 Wifi Module, DC motors, and external 5v battery pack. The software elements include C++ on the mbed for digital and pwm control of the robot, HTML and PHP for handling controller data over the internet, and a Python webserver for handling websockets using Tornado web server.

http://kentaroallen.com/img/image1.jpg

Demonstration

This is a demonstration of using the webpage to control the robot to move forward then make a left turn. Also shown, is the robot's tendency for leftward dragging on forward commands. In this video it shows the processing of 1 command at a time. First the forward button is hit and in this time while the mbed is moving forward no other controls are processed used until it completes its action of moving 1 second. After the mbed has completed it's forward command will the mbed be able to accept new commands(even if pressing new button controls on the webpage during the movement). In this case after the mbed completes the forward movement, I press the left button which turns the mbed to the left at 90 degrees.

http://www.kentaroallen.com/img/webpage.jpg

Components & Library

Hardware Connections

ESP8266LPC1768330uf cap
VCC3.3V+
GNDGND-
Resetp26
RXp27-TX
TXp281-RX
CH_PDVout
LPC1768H-Bridge
p21PWMA
voutSTBY
p22PWMB
GNDGND
VOUTVCC
p18AO1
p17AO2
p20BO1
p19BO2
vinVMOT
H-BridgeMotor
AO1L-RED
AO2L-BLACK
BO2R-BLACK
BO1R-RED

Software

mBed

Import programwebsocketmbed

This is the mbed code for handling Wifi websocket control from a Python based Tornado Web Server.

Server

Uploading Content to a Webhost

Web Page Controls

"getpost.html" This is the code for creating a webpage that has buttons for control. Each button sends its value Forward=1, Backward=2, Left=3, Right=4 to a a PHP page called "motorvalue.php". This file needs to be uploaded to your webhost, or can run locally for a proof of concept test but a local test will not correctly interface with the following PHP page. The robot is only able to process 1 command at a time from this webpage. The total time to process the controls is sporadic where some instances are almost instantaneous or take <5 seconds. This is due to the strength of the WiFi signal.

To use this file open a text editor and paste the following code and save the file as "getpost.html".

<!DOCTYPE html>
<html>
<body>

<style>
  .hide { position:absolute; top:-1px; left:-1px; width:1px; height:1px; }
</style>

<iframe name="hiddenFrame" class="hide"></iframe>

<form action="motorvalue.php" method="get" target="hiddenFrame">
  <button type="submit" name="value" value="1">Forward</button>
  <button type="submit" name="value" value="2">Backward</button>
  <button type="submit" name="value" value="3">Left</button>
  <button type="submit" name="value" value="4">Right</button>
</form>

</body>
</html>

"motorvalue.php" This is the PHP page that handles the button requests from the webpage that writes the numerical 1-4 value to a text file which is used by the websocket server to transfer the numerical command. This PHP page must be uploaded in the same location in your webhost as the getpost.html page to function correctly. This PHP page will not function locally.

To use this file open a text editor and paste the following code and save the file as "motorvalue.php".

<?php
$myfile = fopen("val.txt", "w");
$txt = $_GET["value"];
//echo $_GET["value"];
fwrite($myfile,$txt);
fclose($myfile);
?>

Tornado Websocket Server

"TornadoPy.py" This is the websocket server that the mBed connects with to send and receive command information. The mbed sends a numerical command to the websocket server that indicates it is ready for a new instruction. The websocket server sends the numerical value to the mbed which is the motor command for forward,backward,left,and right (1,2,3,4). This websocket server can be run locally or uploaded to the same location in your webhost as the getpost.html and motorvalue.php but requires configuring the webhost to accept the Python Tornado library.

import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web
import socket
import urllib2
 
def getvalue():
    a = None
    for line in urllib2.urlopen("http://kentaroallen.com/val.txt"):
        a = line
    return a.strip()
 
 
class WSHandler(tornado.websocket.WebSocketHandler):
    def open(self):
        print 'new connection'
     
    def on_message(self, message):
        print message
        if message == "1": //wait for a 1 to process next control
            message_back = getvalue()
            print message_back
            self.write_message(message_back)
 
    def on_close(self):
        print 'connection closed'
 
    def check_origin(self, origin):
        return True
 
application = tornado.web.Application([
    (r'/ws', WSHandler),
])
 
 
if __name__ == "__main__":
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(8080)
    myIP = socket.gethostbyname(socket.gethostname())
    print '*** Websocket Server Started at %s***' % myIP
    tornado.ioloop.IOLoop.instance().start()

Problems Encountered

The robot has a tendency to drag to the left. This is probably due to the way the system is wired where the right motor receives the majority of the power rather than the left. This can be corrected but I felt like this is a project for another day.

The first few implementations of this project had errors with processing data over the WiFi module. The main culprit was there is an unidentifiable error in the official HTTPclient library where HTTP requests needed a special formatting of the GET and POST requests in order to be stable, otherwise 90% of requests were returned with HTTP 406 not acceptable. The first implementation used PHP to handle HTTP requests, this was a total failure due to the mbed requiring special formatting of the HTTP request which I could not handle with my knowledge of PHP. Second, I tried handling HTTP requests using a mixture of JSON and Javascript elements and although this attempt worked more so than the PHP attempt this was also a failure because this was only prone to only work when I was simultaneously testing HTTP requests with http://hurl.it. This un-explainable cause led me to abandon this attempt. Lastly, I settled for using Websockets which produced the most stable outcomes.

Future Work

Currently, the controls for the system are not as responsive as I would have hoped with a delay of 3-5 seconds between control input and the robotic action in some worst case scenarios. I would like to re-examine the official mbed HTTPclient library and make my own modifications such that no special formatting is required such that only the body of the request is an acceptable response. In doing so I believe this would make the system act more in real-time by having a more direct link between web page and controls. Second, I want to include the IMU module to help the robotic car maintain straight paths for self-correction because currently as it stands the car is prone to drift to the left


Please log in to post comments.