Camera is working!
parent
3f60dfa460
commit
8bd6089f9a
|
@ -14,10 +14,15 @@ import json
|
||||||
from pygame.locals import *
|
from pygame.locals import *
|
||||||
from Cannon import HackWAWCannonController
|
from Cannon import HackWAWCannonController
|
||||||
from Cannon import Gunpoint
|
from Cannon import Gunpoint
|
||||||
|
import pygame.camera
|
||||||
|
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
app.debug = True
|
app.debug = True
|
||||||
pygame.init()
|
pygame.init()
|
||||||
|
pygame.camera.init()
|
||||||
|
|
||||||
|
CAMERA_X = 400
|
||||||
|
CAMERA_Y = 30
|
||||||
|
|
||||||
cannon = None
|
cannon = None
|
||||||
|
|
||||||
|
@ -41,17 +46,32 @@ def move_down(amount):
|
||||||
cannon.move_down(amount)
|
cannon.move_down(amount)
|
||||||
return "OK"
|
return "OK"
|
||||||
|
|
||||||
|
@app.route("/fire/", methods=["POST"])
|
||||||
|
def fire():
|
||||||
|
cannon.fire();
|
||||||
|
|
||||||
|
@app.route("/stop_fire/", methods=["POST"])
|
||||||
|
def stop_fire():
|
||||||
|
cannon.fired = False;
|
||||||
|
|
||||||
@app.route("/status")
|
@app.route("/status")
|
||||||
def status():
|
def status():
|
||||||
return json.dumps(cannon.get_data())
|
return json.dumps(cannon.get_data())
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def root():
|
def root():
|
||||||
|
|
||||||
return flask.render_template("status.html", data=cannon.get_data())
|
return flask.render_template("status.html", data=cannon.get_data())
|
||||||
|
|
||||||
|
|
||||||
|
#Camera stuff
|
||||||
|
|
||||||
|
camera = pygame.camera.Camera("/dev/video0", (640, 480))
|
||||||
|
camera.start()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
#create the screen
|
#create the screen
|
||||||
window = pygame.display.set_mode((800, 600))
|
window = pygame.display.set_mode((1200, 800))
|
||||||
|
|
||||||
GRAY = ( 182, 182, 182)
|
GRAY = ( 182, 182, 182)
|
||||||
VIOLET = (150, 100, 190)
|
VIOLET = (150, 100, 190)
|
||||||
|
@ -71,6 +91,7 @@ def main():
|
||||||
|
|
||||||
gunpoint = Gunpoint((5, 4), (200, 4), (5, 210))
|
gunpoint = Gunpoint((5, 4), (200, 4), (5, 210))
|
||||||
|
|
||||||
|
|
||||||
keep_running = True
|
keep_running = True
|
||||||
while keep_running:
|
while keep_running:
|
||||||
screen.draw_surface(cannon)
|
screen.draw_surface(cannon)
|
||||||
|
@ -115,6 +136,14 @@ def main():
|
||||||
screen.communicates.append("Calibrated")
|
screen.communicates.append("Calibrated")
|
||||||
|
|
||||||
elif event.type == MOUSEBUTTONUP:
|
elif event.type == MOUSEBUTTONUP:
|
||||||
|
mousex, mousey = pygame.mouse.get_pos()
|
||||||
|
print mousex, mousey
|
||||||
|
x = (mousex - CAMERA_X)/640.0
|
||||||
|
y = (CAMERA_Y + 480 - mousey)/480.0
|
||||||
|
print x, y
|
||||||
|
position = gunpoint.aim((x, y))
|
||||||
|
print position
|
||||||
|
cannon.position = position
|
||||||
screen.change_color()
|
screen.change_color()
|
||||||
print "Pygame thread exited."
|
print "Pygame thread exited."
|
||||||
os.kill(os.getpid(), signal.SIGINT)
|
os.kill(os.getpid(), signal.SIGINT)
|
||||||
|
@ -203,6 +232,9 @@ class Screen:
|
||||||
self.print_text("Fired!", 20, 150, (150, 20, 40), 40, self.window)
|
self.print_text("Fired!", 20, 150, (150, 20, 40), 40, self.window)
|
||||||
|
|
||||||
self.print_text([str(x) for x in cannon.get_data()].__str__(), 20, 300, (0, 0, 0), 30, self.window)
|
self.print_text([str(x) for x in cannon.get_data()].__str__(), 20, 300, (0, 0, 0), 30, self.window)
|
||||||
|
image = camera.get_image()
|
||||||
|
self.window.blit(image, (CAMERA_X, CAMERA_Y))
|
||||||
|
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
||||||
def print_text(self, text,xx,yy,color,text_size, screen):
|
def print_text(self, text,xx,yy,color,text_size, screen):
|
||||||
|
|
Binary file not shown.
|
@ -2,33 +2,69 @@
|
||||||
{% block body %}
|
{% block body %}
|
||||||
<script type=text/javascript src="{{url_for('static', filename='jquery.min.js') }}"> </script>
|
<script type=text/javascript src="{{url_for('static', filename='jquery.min.js') }}"> </script>
|
||||||
<script type=text/javascript>
|
<script type=text/javascript>
|
||||||
$(document).ready(function(){
|
|
||||||
//
|
|
||||||
$('#dupa').text("I like bananas");
|
|
||||||
});
|
|
||||||
|
|
||||||
function pop_banana() {
|
function update() {
|
||||||
$('#banana').html('<a href="http://ranchochilamate.com/wp/wp-content/uploads/2010/10/banana.jpg">Click</a>');
|
update_status();
|
||||||
|
window.setTimeout(update, 100000000);
|
||||||
}
|
}
|
||||||
|
|
||||||
$.post("/right/20", function(data) {
|
$("document").ready(function(data) {
|
||||||
$('#dupa').html(data);
|
update();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function move_right() {
|
||||||
|
$.post("/right/20", function(data) {
|
||||||
|
update_status();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function move_left() {
|
||||||
|
$.post("/left/20", function(data) {
|
||||||
|
update_status();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function move_down() {
|
||||||
|
$.post("/down/20", function(data) {
|
||||||
|
update_status();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
function move_up() {
|
function move_up() {
|
||||||
$.post("{{url_for('move_up', amount="20")}}", function(data) {
|
$.post("{{url_for('move_up', amount="20")}}", function(data) {
|
||||||
$('#dupa').html(data);
|
update_status();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function fire() {
|
||||||
|
$.post("/fire", function(data) {
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function stop_fire() {
|
||||||
|
$.post("/stop_fire", function(data) {
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function update_status() {
|
||||||
|
$.get("/status", function(data) {
|
||||||
|
$('#status').html(data);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
<div style="width: 400px; height: 400px; margin-top: -200px; margin-left: -200px; position: absolute; top: 50%; left: 50%;">
|
<div style="width: 400px; height: 400px; margin-top: -200px; margin-left: -200px; position: absolute; top: 50%; left: 50%;">
|
||||||
|
<h2>Hello in cannon controller, adjust angles!</h2>
|
||||||
|
<p>Angles can be in range 0:255</p>
|
||||||
<a onclick="move_up()">Up</a>
|
<a onclick="move_up()">Up</a>
|
||||||
<a href="/down/20">Down</a>
|
<a onclick="move_down()">Down</a>
|
||||||
<a href="/left/20">Left</a>
|
<a onclick="move_left()">Left</a>
|
||||||
<a href ="/right/20"" onclick="move_right()">Right</a>
|
<a onclick="move_right()">Right</a>
|
||||||
<p id="dupa" onclick="move_right()"></p>
|
<p><a onclick="fire()">Fire!</a> <a onclick="stop_fire()">Stop</a></p>
|
||||||
<p id="banana"></p>
|
|
||||||
<h2>Status</h2> <p>{{data}}</p></div>
|
<h2>Status: </h2> <p id="status">{{data}}</p></div>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
Loading…
Reference in New Issue