Serial and better validation during calibration
parent
01e56514eb
commit
25e533df4f
|
@ -10,6 +10,8 @@ import random, pygame, sys
|
||||||
from pygame.locals import *
|
from pygame.locals import *
|
||||||
from math import fabs
|
from math import fabs
|
||||||
from math import ceil
|
from math import ceil
|
||||||
|
import serial
|
||||||
|
|
||||||
#import and init pygame
|
#import and init pygame
|
||||||
pygame.init()
|
pygame.init()
|
||||||
|
|
||||||
|
@ -82,22 +84,20 @@ def main():
|
||||||
screen.communicates.remove("Laser disabled")
|
screen.communicates.remove("Laser disabled")
|
||||||
screen.communicates.append("Laser enabled")
|
screen.communicates.append("Laser enabled")
|
||||||
elif event.type == KEYUP and event.key == K_c:
|
elif event.type == KEYUP and event.key == K_c:
|
||||||
point1 = [float(x) for x in ask(window, "Point 1 ").split(",")]
|
point1 = get_calibration_point(window, "Point1")
|
||||||
point2 = [float(x) for x in ask(window, "Point 2 ").split(",")]
|
point2 = get_calibration_point(window, "Point2")
|
||||||
point3 = [float(x) for x in ask(window, "Point 3 ").split(",")]
|
point3 = get_calibration_point(window, "Point3")
|
||||||
print point1, point2, point3
|
print point1, point2, point3
|
||||||
gunpoint.calibrate(point1, point2, point3)
|
gunpoint.calibrate(point1, point2, point3)
|
||||||
print gunpoint.horizontal_angle, gunpoint.vertical_angle
|
print gunpoint.horizontal_angle, gunpoint.vertical_angle
|
||||||
|
|
||||||
elif event.type == MOUSEBUTTONUP:
|
elif event.type == MOUSEBUTTONUP:
|
||||||
print "mouse button up"
|
|
||||||
mouseClicked = True
|
mouseClicked = True
|
||||||
screen.change_color()
|
screen.change_color()
|
||||||
|
cannon.send_data()
|
||||||
|
|
||||||
|
|
||||||
if mouseClicked :
|
if mouseClicked :
|
||||||
print("Mouse was clicked!")
|
|
||||||
coords = [gunpoint.aim((x/10.0,x/10.0)) for x in range(0,11)]
|
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)]
|
samples = [(x/10.0,x/10.0) for x in range(0, 11)]
|
||||||
print samples
|
print samples
|
||||||
|
@ -145,6 +145,21 @@ def ask(screen, question):
|
||||||
display_box(screen, question + ": " + "".join(current_string), 20, 400)
|
display_box(screen, question + ": " + "".join(current_string), 20, 400)
|
||||||
return "".join(current_string)
|
return "".join(current_string)
|
||||||
|
|
||||||
|
def get_calibration_point(screen, message):
|
||||||
|
point_string = ask(screen, message)
|
||||||
|
while(not (is_valid_point(point_string)) ):
|
||||||
|
point_string = ask(screen, "Again, " + message)
|
||||||
|
return [float(x) for x in point_string.split(",")]
|
||||||
|
|
||||||
|
def is_valid_point(string):
|
||||||
|
point = string.split(',')
|
||||||
|
if (len(point) != 2):
|
||||||
|
return False
|
||||||
|
if((not point[0].isdigit()) or (not point[1].isdigit())):
|
||||||
|
return False
|
||||||
|
if(float(point[0]) > 255 or float(point[1]) > 255 or float(point[0])< 0 or float(point[1]) <0 ):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
class Screen:
|
class Screen:
|
||||||
|
|
||||||
|
@ -169,7 +184,7 @@ class Screen:
|
||||||
if cannon.fired:
|
if cannon.fired:
|
||||||
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(cannon.get_data_to_send().__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)
|
||||||
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):
|
||||||
|
@ -195,22 +210,25 @@ class CannonController :
|
||||||
self.laser = False
|
self.laser = False
|
||||||
|
|
||||||
def move_left(self, change):
|
def move_left(self, change):
|
||||||
self.position[0] -= change
|
if(self.position[0] >= change):
|
||||||
|
self.position[0] -= change
|
||||||
|
|
||||||
def move_right(self, change):
|
def move_right(self, change):
|
||||||
self.position[0] += change
|
if(self.position[0] < 255 - change):
|
||||||
|
self.position[0] += change
|
||||||
def move_up(self, change):
|
def move_up(self, change):
|
||||||
self.position[1] += change
|
if(self.position[1] < 255 - change):
|
||||||
|
self.position[1] += change
|
||||||
def move_down(self, change):
|
def move_down(self, change):
|
||||||
self.position[1] -= change
|
if(self.position[1] >= change):
|
||||||
|
self.position[1] -= change
|
||||||
def check_validity_of_data(self):
|
def check_validity_of_data(self):
|
||||||
if(fabs(self.position[0]) > 255):
|
if(fabs(self.position[0]) > 255):
|
||||||
return False
|
return False
|
||||||
if (fabs(self.position[1]) > 255):
|
if (fabs(self.position[1]) > 255):
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
def get_data(self):
|
||||||
def get_data_to_send(self):
|
|
||||||
# ('a' - os X, 'b' - os Y, 'c' - Strzal, 'd' - laser)
|
|
||||||
if self.check_validity_of_data():
|
if self.check_validity_of_data():
|
||||||
data = []
|
data = []
|
||||||
data.extend(self.position)
|
data.extend(self.position)
|
||||||
|
@ -225,9 +243,26 @@ class CannonController :
|
||||||
data.append(0)
|
data.append(0)
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return [0, 0, 0, 0]
|
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):
|
||||||
|
ser = serial.Serial("/dev/ttyACM0", 115200, timeout=1)
|
||||||
|
data = self.get_data_to_send()
|
||||||
|
ser.write('a')
|
||||||
|
ser.write(data[0])
|
||||||
|
ser.write('b')
|
||||||
|
ser.write(data[1])
|
||||||
|
ser.write('c')
|
||||||
|
ser.write(data[2])
|
||||||
|
ser.write('d')
|
||||||
|
ser.write(data[3])
|
||||||
|
|
||||||
class Gunpoint :
|
class Gunpoint :
|
||||||
def __init__(self, point1, point2, point3):
|
def __init__(self, point1, point2, point3):
|
||||||
self.vertical_angle = point3[1] - point1[1]
|
self.vertical_angle = point3[1] - point1[1]
|
||||||
|
|
Loading…
Reference in New Issue