Commit cbee49cb authored by Matthew Gall's avatar Matthew Gall

This is a major cleanup, and refactor using bottle.

Due to various issues with cherrypy, we have migrated the system to use bottle, and tornado.
So, we've stripped it all out, and started again, and used much better error correction for device support.
parent 170a6019
# Windows image file caches
Thumbs.db
ehthumbs.db
# Folder config file
Desktop.ini
# Created by https://www.gitignore.io/api/macos,python,virtualenv
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msm
*.msp
# Windows shortcuts
*.lnk
# =========================
# Operating System Files
# =========================
# OSX
# =========================
.DS_Store
### macOS ###
*.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
......@@ -38,6 +19,7 @@ $RECYCLE.BIN/
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
......@@ -45,3 +27,117 @@ $RECYCLE.BIN/
Network Trash Folder
Temporary Items
.apdisk
### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# dotenv
.env
# virtualenv
.venv
venv/
ENV/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
### VirtualEnv ###
# Virtualenv
# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/
[Bb]in
[Ii]nclude
[Ll]ib
[Ll]ib64
[Ll]ocal
[Ss]cripts
pyvenv.cfg
pip-selfcheck.json
# End of https://www.gitignore.io/api/macos,python,virtualenv
......@@ -52,7 +52,7 @@ function robotMove(direction) {
switch(direction) {
case 1:
case "Up":
url = "/forwards";
url = "/forward";
break;
case 2:
......@@ -62,7 +62,7 @@ function robotMove(direction) {
case 3:
case "Down":
url = "/backwards";
url = "/back";
break;
case 4:
......
cherrypy
gpiozero
\ No newline at end of file
gpiozero
bottle
tornado
\ No newline at end of file
......@@ -20,77 +20,86 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import subprocess
import time
import os
import socket
import cherrypy
import os, logging, subprocess, time
from bottle import route, request, response, redirect, hook, error, default_app, view, static_file, template, HTTPError
from gpiozero import Robot, DistanceSensor
robot = Robot(left=(10, 9), right=(8, 7))
sensor = DistanceSensor(18, 17)
class WebControl(object):
@cherrypy.expose
def left(self):
robot.left()
time.sleep(0.2)
robot.stop()
return "LEFT TURN"
@cherrypy.expose
def right(self):
robot.right()
time.sleep(0.2)
robot.stop()
return "RIGHT TURN"
@cherrypy.expose
def forwards(self):
robot.forward()
time.sleep(0.2)
robot.stop()
return "FORWARDS"
@cherrypy.expose
def backwards(self):
robot.backward()
time.sleep(0.2)
robot.stop()
return "BACKWARDS"
@cherrypy.expose
def ultrasonic(self):
return "{:.2f}".format(sensor.distance)
@cherrypy.expose
def cheese(self):
cherrypy.response.headers['Content-Type'] = 'image/jpeg'
cherrypy.response.headers['Cache-Control'] = 'no-store'
with subprocess.Popen(["raspistill", "-vf", "-w", "400", "-h", "300", "-o", "-"], stdout=subprocess.PIPE) as proc:
return proc.stdout.read()
return run_output.stdout
@route('/left')
def action_left():
robot.left()
time.sleep(0.2)
robot.stop()
return "LEFT TURN"
@route('/right')
def action_right():
robot.right()
time.sleep(0.2)
robot.stop()
return "RIGHT TURN"
@route('/forward')
def action_forward():
robot.forward()
time.sleep(0.2)
robot.stop()
return "FORWARDS"
@route('/back')
def action_back():
robot.backward()
time.sleep(0.2)
robot.stop()
return "BACKWARDS"
@route('/ultrasonic')
def ultrasonic():
return "{:.2f}".format(sensor.distance)
@route('/cheese')
def cheese():
response.content_type = 'image/jpeg'
response.cache_control = 'no-store'
with subprocess.Popen(["raspistill", "-vf", "-w", "400", "-h", "300", "-o", "-"], stdout=subprocess.PIPE) as proc:
return proc.stdout.read()
return run_output.stdout
@route('/')
def index():
return static_file('index.html', root='public')
@route('/style.css')
def index():
return static_file('style.css', root='public')
if __name__ == '__main__':
conf = {
'/': {
'tools.sessions.on': True,
'tools.staticdir.root': os.path.abspath(os.getcwd())
},
'/static': {
'tools.staticdir.on': True,
'tools.staticdir.dir': './public'
}
}
robot.stop()
cherrypy.config.update({'server.socket_host': os.getenv('IP', '0.0.0.0')})
# We'll make life easy, and detect the LAN IP to show you where to visit!
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 1))
print("Web controlled robot by PiCymru")
print("Want to control me? In your browser, visit http://{}:8080/static/index.html".format(s.getsockname()[0]))
cherrypy.quickstart(WebControl(), '/', conf)
\ No newline at end of file
app = default_app()
serverHost = os.getenv('IP', 'localhost')
serverPort = os.getenv('PORT', '5000')
# Now we're ready, so start the server
# Instantiate the logger
log = logging.getLogger('log')
console = logging.StreamHandler()
log.setLevel(logging.INFO)
log.addHandler(console)
try:
with open('/dev/gpiomem') as f:
f.read()
except:
exit(log.fatal('Running this application is only supported on a Raspberry Pi.'))
# If the above test failed, they're not running on a Raspberry Pi
robot = Robot(left=(10, 9), right=(8, 7))
sensor = DistanceSensor(18, 17)
robot.stop()
# Now we're ready, so start the server
try:
# We'll make life easy, and detect the LAN IP to show you where to visit!
print("Want to control me? In your browser, visit http://{}:5000/index.html".format(serverHost))
app.run(host=serverHost, port=serverPort, server='tornado')
except:
log.error("Failed to start application server")
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment