/
gogobot.py
134 lines (109 loc) · 2.81 KB
/
gogobot.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import math
import time
import sys
from time import sleep, time
from threading import Thread
def asynchronus(fun, *args, **keyword):
# custom async fn
thread = Thread(None, fun, None, args)
if "autorun" in keyword or len(keyword) == 0:
thread.start()
return thread
# Arduino is currently contained in startscript.py
Left=-1
Right=1
class Arduino:
def __init__(self, dev, baud=9600):
return
def send(head, bytes):
return
def motor(side, speed):
return
class Timer(Thread):
# lets users run events relative to each other
def __init__(self):
self.queue = {} # change queue into a self sorting data struct
Thread.__init__(self)
return
def run(self):
while 1:
t = time()
for event,(function,args) in self.queue:
if event<=t:
asynchronus(function,*args)
del self.queue[event]
def append(self, reltime, fun, *args):
self.queue[time()+reltime]=(fun, *args)
return
# basic user functions - easy to use functions for beginners
# ---------------------
# if we have these functions then i can just get rid of the ``go'' class
def go(speed,angle=0):
return
def stop():
return
def gofor(timems,speed):
return
def turn(angle):
# what about radius?
return
ON = 1, OFF = 0
def light(state, time):
if state == 1:
return
else: # state == 0
return
def blink(time):
# blinking the light on the arduino
return
def display(text):
# displaying text on the LCD
return
def delayRun(timems,function,*args):
# will call `function' with the arguments in `args' after `timems' milliseconds
return
def getDirection():
# returns a float in degrees
return
def getDistance(IR):
# returns a float distance from any IR censor in cm
# `IR' isi the sensor pin (or id number)
return
# ---------------------
# internal functions - user will not need to use these functions directly
# ---------------------
def _find_arduino(dev = '/dev/ttyUSB',start=0,stop=2):
# cycles through usb drivers until arduino is found
for i in range(start,stop+1):
try:
serial = dev+str(i)
a = Arduino(serial)
head,body = a.send(INIT, 0)
# INIT not implimented yet
# should return a sequence that is unique to the gogobot arduino program
except:
pass
else:
return serial
# ---------------------
def setup():
# define classes and functions
arduino = Arduino(_find_arduino())
lcd = LCD()
terminal = sys.stdout
sys.stdout=lcd # redirect standard output to the lcd through the class method write(text)
try:
fin = open("user.gg", "r")
data = fin.read()
fin.close()
except:
print "File \"user.gg\" not found. Balls!"
return 1
try:
exec(data)
except Exception:
print Exception
return 1
return 0
if __name__=="__main__":
setup()