Cannon module, some small changes in gui.
parent
6c626fad92
commit
4192ace6d9
|
@ -7,10 +7,8 @@ Created on Nov 10, 2012
|
|||
|
||||
import random, pygame, sys
|
||||
from pygame.locals import *
|
||||
from math import fabs
|
||||
from math import ceil
|
||||
import serial
|
||||
import glob
|
||||
from Cannon import CannonController
|
||||
from Cannon import Gunpoint
|
||||
|
||||
pygame.init()
|
||||
|
||||
|
@ -24,14 +22,10 @@ def main():
|
|||
GREEN = (0, 150, 0)
|
||||
BLUE = (30, 30, 180)
|
||||
VERYLIGHT = (210, 210, 210)
|
||||
BACKGROUND_COLOR = GREEN
|
||||
|
||||
colors = [GRAY, VIOLET, RED, GREEN, BLUE, VERYLIGHT]
|
||||
|
||||
window.fill(BACKGROUND_COLOR)
|
||||
|
||||
#mouseClicked = False
|
||||
pygame.display.flip()
|
||||
|
||||
cannon = CannonController()
|
||||
|
||||
|
||||
|
@ -39,11 +33,9 @@ def main():
|
|||
screen.draw_surface(cannon)
|
||||
|
||||
gunpoint = Gunpoint((5, 4), (200, 4), (5, 210))
|
||||
coords = gunpoint.aim((0,0))
|
||||
#input handling ( code):
|
||||
|
||||
while True:
|
||||
screen.draw_surface(cannon)
|
||||
#mouseClicked = False
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
|
@ -82,18 +74,12 @@ def main():
|
|||
print point1, point2, point3
|
||||
gunpoint.calibrate(point1, point2, point3)
|
||||
print gunpoint.horizontal_angle, gunpoint.vertical_angle
|
||||
screen.communicates.append("Calibrated")
|
||||
|
||||
elif event.type == MOUSEBUTTONUP:
|
||||
#mouseClicked = True
|
||||
screen.change_color()
|
||||
|
||||
#if mouseClicked :
|
||||
#coords = [gunpoint.aim((x/10.0,x/10.0)) for x in range(0,11)]
|
||||
#samples = [(x/10.0,x/10.0) for x in range(0, 11)]
|
||||
#print samples
|
||||
#print coords
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def get_key():
|
||||
|
@ -105,14 +91,14 @@ def get_key():
|
|||
pass
|
||||
|
||||
def display_box(screen, message, xx, yy):
|
||||
"Print a message in a box in the middle of the screen"
|
||||
font_object = pygame.font.Font(None,18)
|
||||
pygame.draw.rect(screen, (20,20,20),(xx, yy, 200,20), 0)
|
||||
pygame.draw.rect(screen, (222,222,222),
|
||||
(xx -2, yy-2, 202,24), 1)
|
||||
"Print a message in a box on xx, yy"
|
||||
font_object = pygame.font.Font(None,26)
|
||||
pygame.draw.rect(screen, (222,222,222),(xx, yy, 300,30), 0)
|
||||
pygame.draw.rect(screen, (20,20,20),
|
||||
(xx -2, yy-2, 302,34), 1)
|
||||
|
||||
if len(message) != 0:
|
||||
screen.blit(font_object.render(message, 1, (255,255,255)),
|
||||
screen.blit(font_object.render(message, 3, (0,0,0)),
|
||||
(xx + 6, yy + 6) )
|
||||
|
||||
pygame.display.flip()
|
||||
|
@ -122,6 +108,7 @@ def ask(screen, question):
|
|||
pygame.font.init()
|
||||
current_string = []
|
||||
display_box(screen, question + ": " + "".join(current_string), 20, 400)
|
||||
display_box(screen, "Write calibration points below", 20, 360)
|
||||
while 1:
|
||||
inkey = get_key()
|
||||
if inkey == K_BACKSPACE:
|
||||
|
@ -132,8 +119,10 @@ def ask(screen, question):
|
|||
current_string.append("_")
|
||||
elif inkey <= 127:
|
||||
current_string.append(chr(inkey))
|
||||
display_box(screen, "Write calibration points below", 20, 360)
|
||||
display_box(screen, question + ": " + "".join(current_string), 20, 400)
|
||||
return "".join(current_string)
|
||||
|
||||
|
||||
def get_calibration_point(screen, message):
|
||||
point_string = ask(screen, message)
|
||||
|
@ -169,7 +158,7 @@ class Screen:
|
|||
def draw_surface(self, cannon):
|
||||
self.window.fill(self.color)
|
||||
for i, text in enumerate(self.communicates):
|
||||
self.print_text(text, 20, 20 + i*20, (0, 0, 0), 20, self.window)
|
||||
self.print_text(text, 20, 20 + i*20, (0, 0, 0), 30, self.window)
|
||||
|
||||
if cannon.fired:
|
||||
self.print_text("Fired!", 20, 150, (150, 20, 40), 40, self.window)
|
||||
|
@ -178,115 +167,11 @@ class Screen:
|
|||
pygame.display.flip()
|
||||
|
||||
def print_text(self, text,xx,yy,color,text_size, screen):
|
||||
font = pygame.font.SysFont("Courier New",text_size)
|
||||
font = pygame.font.SysFont(None,text_size)
|
||||
ren = font.render(text,1,color)
|
||||
screen.blit(ren, (xx,yy))
|
||||
|
||||
|
||||
class CannonController :
|
||||
def __init__(self):
|
||||
print("cannon controller started")
|
||||
self.position = [0, 0]
|
||||
self.fired = False
|
||||
self.laser = False
|
||||
ports = glob.glob("/dev/ttyACM*")
|
||||
|
||||
if(len(ports) == 1):
|
||||
self.ser = serial.Serial(ports[0], 115200, timeout=1)
|
||||
self.connected_to_serial = True
|
||||
else:
|
||||
self.ser = False
|
||||
self.connected_to_serial = False
|
||||
if len(ports) > 1 :
|
||||
raise "To many devices to handle"
|
||||
elif len(ports) == 0 :
|
||||
print "No device is connected"
|
||||
|
||||
def set_position(self, point):
|
||||
self.position = point
|
||||
|
||||
def fire(self):
|
||||
self.fired = True
|
||||
|
||||
def enable_laser(self):
|
||||
self.laser = True
|
||||
|
||||
def disable_laser(self):
|
||||
self.laser = False
|
||||
|
||||
def move_left(self, change):
|
||||
if(self.position[0] >= change):
|
||||
self.position[0] -= change
|
||||
def move_right(self, change):
|
||||
if(self.position[0] < 255 - change):
|
||||
self.position[0] += change
|
||||
def move_up(self, change):
|
||||
if(self.position[1] < 255 - change):
|
||||
self.position[1] += change
|
||||
def move_down(self, change):
|
||||
if(self.position[1] >= change):
|
||||
self.position[1] -= change
|
||||
def check_validity_of_data(self):
|
||||
if(fabs(self.position[0]) > 255):
|
||||
return False
|
||||
if (fabs(self.position[1]) > 255):
|
||||
return False
|
||||
return True
|
||||
def get_data(self):
|
||||
if self.check_validity_of_data():
|
||||
data = []
|
||||
data.extend(self.position)
|
||||
if(self.fired):
|
||||
data.append(255)
|
||||
else:
|
||||
data.append(0)
|
||||
|
||||
if(self.laser):
|
||||
data.append(255)
|
||||
else:
|
||||
data.append(0)
|
||||
|
||||
return data
|
||||
|
||||
else:
|
||||
return [0, 0, 0, 0]
|
||||
|
||||
def get_data_to_send(self):
|
||||
data = self.get_data()
|
||||
return [chr(x) for x in data]
|
||||
|
||||
def send_data(self):
|
||||
if(self.connected_to_serial):
|
||||
data = self.get_data_to_send()
|
||||
self.ser.write('a')
|
||||
self.ser.write(data[0])
|
||||
print "wrote %i" % ord(data[0])
|
||||
self.ser.write('b')
|
||||
self.ser.write(data[1])
|
||||
self.ser.write('c')
|
||||
self.ser.write(data[2])
|
||||
self.ser.write('d')
|
||||
self.ser.write(data[3])
|
||||
else:
|
||||
print "Cannot send data, not connected..."
|
||||
|
||||
class Gunpoint :
|
||||
def __init__(self, point1, point2, point3):
|
||||
self.vertical_angle = point3[1] - point1[1]
|
||||
self.horizontal_angle = point2[0] - point1[0]
|
||||
self.beginnig_horizontal = point1[0]
|
||||
self.beginnig_vertical = point1[1]
|
||||
|
||||
def calibrate(self, point1, point2, point3):
|
||||
self.vertical_angle = point3[1] - point1[1]
|
||||
self.horizontal_angle = point2[0] - point1[0]
|
||||
self.beginnig_horizontal = point1[0]
|
||||
self.beginnig_vertical = point1[1]
|
||||
|
||||
def aim(self, point):
|
||||
horizontal = ceil(self.beginnig_horizontal + self.horizontal_angle*point[0])
|
||||
vertical = ceil(self.beginnig_vertical + self.vertical_angle*point[1])
|
||||
return [horizontal, vertical]
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Reference in New Issue