-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubble.py
122 lines (99 loc) · 3.5 KB
/
bubble.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
import usys
import umath as m
from usys import stdin
from uselect import poll
from pybricks.hubs import TechnicHub
from pybricks.pupdevices import Motor
from pybricks.parameters import Port
from pybricks.tools import wait, StopWatch
from pybricks.tools import Matrix, vector, cross
hub = TechnicHub()
from linear import xaxis, yaxis, zaxis, rotate
import linear as lin
from joycode import JoyProtocol
#from swashplate import Swashplate
millis = StopWatch().time
# C is star, B is port, A is front, D is rotor
# max speed is about 1300
#rotor = Motor(Port.D)
#star = Motor(Port.C)
#port = Motor(Port.B)
#front = Motor(Port.A)
class Enum(object):
# this is good enough for state machines in micropython
def __init__(self, enumname, enums, start=1):
self.__enumname = enumname
for i, e in enumerate(enums):
setattr(self, e, i+start)
class MoveSM(object):
def __init__(self):
print("new MoveSM")
self.states = Enum("movestates", ['started', 'finished'])
self.state = self.states.finished
self.start_time = 0
def tick(self):
#print(f"MoveSM.tick(): {self.state} start: {self.start_time}")
if self.state == self.states.started:
if millis() > (self.start_time + 1000):
self.state = self.states.finished
print("<taskdone/>")
else:
print("not done yet.")
else:
# state is finished
pass
def moveto(self):
self.start_time = millis()
self.state = self.states.started
print(f"moveto() start_time: {self.start_time}")
def run_remote():
poller = poll()
# Register the standard input so we can read keyboard presses.
poller.register(stdin)
wvars = ['roll', 'pitch', 'yaw', 'coll', 'glyph']
wirep = JoyProtocol(wvars, 2, poller, stdin)
#jsman = JSMan()
last_input = 0 # last input from base station
#disk_def = 8 # degrees +-
#coll_range = .15 # % +-
#threshold = memory_calibrate() # max travel of cylinders, degrees
# arm radius, min cylinder, max cylinder
#rot = Rotor(60, 160, 200, threshold) # real robot
identify()
print("<awake/>")
msm = MoveSM()
while True:
if wirep.poll():
try:
last_input = millis()
wirep.decode_wire()
#print(wirep.vals)
if wirep.decode_raw('glyph') == 255:
pass
#print("got keepalive")
else:
print("got moveto")
msm.moveto()
msm.tick()
except Exception as e:
print("failure in main loop:")
print(e)
#rot.actuate()
def identify():
print(f"System name: {hub.system.name()}")
print(f"Version: {usys.version}")
print(f"Implementation: {usys.implementation}")
print(f"Version Info: {usys.version_info}")
print(f"Battery Voltage: {hub.battery.voltage()}mv")
if __name__ == "__main__":
# pybricksdev run ble -n bubble bubble.py
#teststorage()
#testwritestore()
#run_remote()
try:
#pass # when running above tests
run_remote() # full talks to remote run under ./rotorbase.py
#full_calibrate() # should be done with rotors and scissor link detached
except Exception as e:
print("General failure: ", e)
identify()