Commit ebe8c0e6 authored by Douglas Gore's avatar Douglas Gore

Initial version

Basic version of the web controlled robot template, with some
instructions.
parent f8983bf9
Copyright (c) 2016 PiCymru
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\ No newline at end of file
Browser controlled robot
========================
This is a very simple Python based robot project for the Raspberry Pi designed for use with the Cam Jam 3 robotics kit. Using gpiozero and CherryPy the program will host it's own web page which can be used to control the robot.
Hardware required:
- Raspberry Pi
- CamJam EduKit 3 robotics kit
- Official Raspberry Pi camera (optional but recommended)
- Wi-Fi connection
Software required:
- CherryPy Python module
- gpiozero Python module
Cherry Py can be installed with the following command:
pip3 install cherrypy
The gpiozero module comes pre-installed on recent Raspbian releases but can be installed manually if not available with the following command:
sudo apt-get install python3-gpiozero
You can run the project from the command line simply by typing:
python webbot.py
Once running you can connect to the robot from any web browser on the same network as the robot. If your Raspberry Pi has the default network settings you can access it at the following URL:
http://raspberrypi.lan/static/
Once connected you can use the following keys on your keyboard to control the robot:
- Arrow keys for forwards, backwards, left turn and right turn
- 'c' key to capture and display an image from the camera if connected
- 'u' key to turn on/off an ultrasonic distance measurement every 2 seconds
This project has been developed by PiCymru as part of our education and learning programme. We are happy for you to use and adapt this project to your needs but please provide attribution as required.
If you do use this project or have any feedback we would love to hear from you, tweet us at @PiCymru or e-mail us at [email protected]
A big thanks to Tim Richardson & Mike Horne for the fantastic CamJam 3 and Ben Nuttall and the gpiozero team for such a simple library.
Visit us at http://picymru.org.uk/
\ No newline at end of file
<html>
<link rel="stylesheet" type="text/css" href="style.css">
<body>
<h1>Robot Control Terminal</h1>
<img id="camera">
<p id="status">TEST</p>
<p id="usStatus">Distance: --</p>
<script type="text/javascript">
var config = {
ultrasonics: false
};
var data = {
ultrasonicInterval: null
};
function robotResponse() {
document.getElementById("status").innerHTML = this.responseText;
}
function ultrasonicResponse() {
document.getElementById("usStatus").innerHTML = "Distance: " + this.responseText + " cm";
}
function updateUltrasonic() {
var httpRequest = new XMLHttpRequest();
httpRequest.addEventListener("load", ultrasonicResponse);
httpRequest.open("GET", "/ultrasonic");
httpRequest.send();
}
function keyEvent(e) {
var url;
console.log("Key pressed id = " + e.keyIdentifier);
switch(e.keyIdentifier) {
case "Left":
url = "/left";
break;
case "Right":
url = "/right";
break;
case "Up":
url = "/forwards";
break;
case "Down":
url = "/backwards";
break;
case "U+0043":
document.getElementById("status").innerHTML = "Acquiring image sensor data...";
document.getElementById("camera").src = "";
document.getElementById("camera").src = "/cheese";
break;
case "U+0055":
config.ultrasonics = !config.ultrasonics;
if (config.ultrasonics) {
data.ultrasonicInterval = setInterval(updateUltrasonic, 2000);
} else {
clearInterval(data.ultrasonicInterval);
data.ultrasonicInterval = null;
}
break;
}
if (url) {
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", robotResponse);
oReq.open("GET", url);
oReq.send();
}
}
document.addEventListener("keydown", keyEvent, false);
</script>
</body>
</html>
\ No newline at end of file
body {
background: black;
color: #00BB00;
font-family: sans-serif;
}
.h1 {
font-family: sans-serif;
}
#camera {
width: 400px;
height: 300px;
border: solid 1px black;
}
\ No newline at end of file
# Web controlled robot by PiCymru (@picymru / http://picymru.org.uk)
import subprocess
import time
import os
import cherrypy
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
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': '0.0.0.0'})
cherrypy.quickstart(WebControl(), '/', conf)
\ 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