summaryrefslogtreecommitdiffstats
path: root/Lasers_und_stuff/Cannon.py
blob: b997284e2f283551ebc430187208420a65cb99e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
'''
Created on Nov 11, 2012

@author: attero
'''
import serial
import glob
from math import ceil

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):
            try:
                self.ser = serial.Serial(ports[0], 115200, timeout=1)
            except serial.SerialException:
                raise Exception("Could not open serial connction (busy?)")
            self.connected_to_serial = True
        else:
            self.ser = False
            self.connected_to_serial = False
            if len(ports) > 1 :
                raise Exception("To many devices to handle")
            elif len(ports) == 0 :
                print Exception("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(abs(self.position[0]) > 255):
            return False
        if (abs(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]