forked from blockext/blockext
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasuro.py
executable file
·97 lines (74 loc) · 2.28 KB
/
asuro.py
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
# coding=utf-8
from __future__ import unicode_literals
import time
from blockext import *
import serial
import io
class Asuro:
def __init__(self):
self.foo = 0
for tty in ['/dev/ttyUSB0', '/dev/ttyUSB1', 'none']:
try:
self.ser = serial.Serial(tty, 2400, timeout=10)
#self.ser = serial.serial_for_url('socket://localhost:4444', timeout=10)
break
except:
if tty == 'none':
print "Unable to find Serial Port, Please plug in cable or check cable connections."
exit()
#self.sio = io.TextIOWrapper(io.BufferedRWPair(self.ser, self.ser))
def _problem(self):
if time.time() % 8 > 4:
return "Your Asuro is not connected."
def _is_connected(self):
try:
#self.ser.write("test")
return True
except:
return False
def _on_reset(self):
print("""
Reset! The red stop button has been clicked
""")
def asuro_cmd(self, cmd):
self.ser.write(cmd)
dummy = self.ser.read(len(cmd))
time.sleep(0.5)
out = ''
while self.ser.inWaiting() > 0:
out += self.ser.read(1)
print str(out)
return out
@predicate("Asuro bereit")
def ready(self):
print "Ready"
@command("Fahre %n cm", is_blocking=True)
def go(self, distance):
self.asuro_cmd("[x" + str(distance) + "y100f]")
@command("Drehe %n Grad links", is_blocking=True)
def turn(self, degree):
self.asuro_cmd("[x" + str(degree) + "y100l]")
@command("Motoren an")
def MotorOn(self):
self.asuro_cmd("[d]")
@command("Motoren aus")
def MotorOff(self):
self.asuro_cmd("[h]")
@reporter("Taster")
def get_bumper(self):
return self.asuro_cmd("[s]")
@reporter("Batterie")
def get_battery(self):
return self.asuro_cmd("[V]")
@reporter("Chirp")
def get_chirp(self):
return self.asuro_cmd("[u]")
descriptor = Descriptor(
name = "Asuro",
port = 8000,
#host = "localhost",
blocks = get_decorated_blocks_from_class(Asuro)
)
extension = Extension(Asuro, descriptor)
if __name__ == "__main__":
extension.run_forever(debug=True)