/
lms.py
executable file
·326 lines (269 loc) · 10 KB
/
lms.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/env python
from __future__ import print_function
import bluetooth
import struct
def make_lc(n):
if -32 < n < 32:
return [n & 0x3f]
if -128 < n < 128:
return [0x81, n & 0xff]
if -32768 < n < 32768:
return [0x82, n & 0xff, (n >> 8) & 0xff]
return [0x83, n & 0xff, (n >> 8) & 0xff, (n >> 16) & 0xff, (n >> 24) & 0xff]
class Instruction:
def primpar(self, n):
if type(n) is int:
return "".join([chr(x) for x in make_lc(n)])
elif type(n) is str:
return "\x80" + n + "\x00"
else:
raise RuntimeError, "Unknown type"
def beep(self):
return self.tone(2, 880, 500)
def tone(self, volume, frequency, duration_ms):
return self.sound_tone(volume, frequency, duration_ms)
def sound_break(self):
return "\x94" + self.primpar(0)
def sound_tone(self, volume, frequency, duration_ms):
return "\x94" + self.primpar(1) + \
self.primpar(volume) + \
self.primpar(frequency) + \
self.primpar(duration_ms)
def sound_play(self, volume, filename):
return "\x94" + self.primpar(2) + \
self.primpar(volume) + \
self.primpar(filename)
def sound_repeat(self, volume, filename):
return "\x94" + self.primpar(3) + \
self.primpar(volume) + \
self.primpar(filename)
def sound_ready(self):
return "\x96"
def output_step_speed(self, layer = 0, nos = 0, speed = 0,
step_begin = 0, step_do = 0, step_end = 0, brake = 0):
return "\xae" + \
self.primpar(layer) + \
self.primpar(nos) + \
self.primpar(speed) + \
self.primpar(step_begin) + \
self.primpar(step_do) + \
self.primpar(step_end) + \
self.primpar(brake)
def output_step_sync(self, layer = 0, nos = 0, speed = 0,
turn = 0, step = 0, brake = 0):
return "\xb0" + \
self.primpar(layer) + \
self.primpar(nos) + \
self.primpar(speed) + \
self.primpar(turn) + \
self.primpar(step) + \
self.primpar(brake)
def output_ready(self, layer = 0, nos = 0):
return "\xaa" + \
self.primpar(layer) + \
self.primpar(nos)
def output_start(self, layer = 0, nos = 0):
return "\xa6" + \
self.primpar(layer) + \
self.primpar(nos)
def u16(n):
return chr(n & 0xff) + \
chr((n >> 8) & 0xff)
def u32(n):
return chr(n & 0xff) + \
chr((n >> 8) & 0xff) + \
chr((n >> 16) & 0xff) + \
chr((n >> 24) & 0xff)
class SysInstruction(Instruction):
def begin_download(self, length, filename):
"""The VM will download *length* bytes and store them at *filename*"""
return chr(0x92) + u32(length) + filename + chr(0)
def continue_download(self, handle, data):
return chr(0x93) + chr(handle) + data
def close_filehandle(self, handle):
return chr(0x98) + chr(handle)
def list_files(self, buf_size, filename):
return chr(0x99) + u16(buf_size) + filename + chr(0)
class MessageSender:
def __init__(self, socket):
self.counter = 0
self.socket = socket
def msgid(self):
self.counter += 1
return self.counter
def u16(self, n):
return chr(n & 0xff) + chr((n >> 8) & 0xff)
def direct_command(self, instr_bytes):
cmd_type = 0x80
var_alloc = 0 # FIXME
bytes = self.u16(self.msgid()) + chr(cmd_type) + \
self.u16(var_alloc) + instr_bytes
packet = self.u16(len(bytes)) + bytes
print("->")
print(repr(packet))
self.socket.send(packet)
def direct_command_with_reply(self, instr_bytes):
cmd_type = 0x00
var_alloc = 0 # FIXME
bytes = self.u16(self.msgid()) + chr(cmd_type) + \
self.u16(var_alloc) + instr_bytes
packet = self.u16(len(bytes)) + bytes
print("->", repr(packet), sep="")
self.socket.send(packet)
reply = self.socket.recv(1024)
print("<-", repr(reply), sep="")
def system_command(self, instr_bytes):
cmd_type = 0x81
bytes = self.u16(self.msgid()) + chr(cmd_type) + \
instr_bytes
packet = self.u16(len(bytes)) + bytes
print("->", repr(packet), sep="")
self.socket.send(packet)
def unmarshall_message(self, bytes):
size = struct.unpack("<H", bytes[0:2])[0]
msgid = struct.unpack("<H", bytes[2:4])[0]
assert size == len(bytes) - 2
return msgid, bytes[4:]
def system_command_with_reply(self, instr_bytes):
cmd_type = 0x01
cmd_id = self.msgid()
bytes = self.u16(cmd_id) + chr(cmd_type) + \
instr_bytes
packet = self.u16(len(bytes)) + bytes
print("->", repr(packet), sep="")
self.socket.send(packet)
reply = self.socket.recv(1024)
print("<-", repr(reply), sep="")
id, reply2 = self.unmarshall_message(reply)
assert id == cmd_id
rep_type = ord(reply2[0]) # 03 sysreply, 05 syserror
rep_cmd = ord(reply2[1])
rep_status = ord(reply2[2])
if rep_type == 0x05:
raise RuntimeError, "Error: {0}".format(rep_status)
payload = reply2[3:]
return payload
host = "00:16:53:53:EE:8C" # "KRALICEK"
port = 1 # the rfcomm port ev3 uses
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect((host, port))
ins = Instruction()
sins = SysInstruction()
ms = MessageSender(sock)
MOTOR_B = 2
MOTOR_C = 4
instrs = [
ins.tone(2, 1760, 500),
ins.sound_ready(),
ins.output_step_speed(nos = MOTOR_B | MOTOR_C,
speed = 50,
step_do = 1080),
ins.output_ready(nos = MOTOR_B | MOTOR_C),
ins.tone(2, 1760, 500),
ins.sound_ready(),
ins.output_step_sync(nos = MOTOR_B | MOTOR_C,
speed = +30,
turn = 200,
step = 1000),
ins.output_ready(nos = MOTOR_B | MOTOR_C),
ins.tone(2, 1760, 500),
ins.sound_ready(),
ins.output_step_speed(nos = MOTOR_B | MOTOR_C,
speed = 50,
step_do = 1080),
ins.output_ready(nos = MOTOR_B | MOTOR_C),
ins.beep(),
ins.sound_ready(),
]
def fwbk(step):
ins = Instruction()
return "".join([
ins.output_step_sync(nos = MOTOR_B | MOTOR_C,
speed = 50,
turn = 0,
step = step),
ins.output_ready(nos = MOTOR_B | MOTOR_C),
ins.output_step_sync(nos = MOTOR_B | MOTOR_C,
speed = 50,
turn = 0,
step = -step),
ins.output_ready(nos = MOTOR_B | MOTOR_C),
])
turnstep = 450
while True:
print("(ts={0}) > ".format(turnstep), end = '')
cmd = raw_input()
if cmd == "q":
break
elif cmd == "k":
instr = ins.output_step_speed(nos = MOTOR_B | MOTOR_C,
speed = 75,
step_begin = 180,
step_do = 360,
step_end = 180)
elif cmd == "z":
instr = ins.output_step_speed(nos = MOTOR_B | MOTOR_C,
speed = -75,
step_begin = 180,
step_do = 360,
step_end = 180)
elif cmd == "l":
instr = ins.output_step_sync(nos = MOTOR_B | MOTOR_C,
speed = +30,
turn = -200,
step = turnstep)
elif cmd == "r":
instr = ins.output_step_sync(nos = MOTOR_B | MOTOR_C,
speed = +30,
turn = +200,
step = turnstep)
elif cmd == "+":
turnstep += 50
continue
elif cmd == "-":
turnstep -= 50
continue
elif cmd == "f":
instr = ins.output_step_speed(nos = MOTOR_B | MOTOR_C,
speed = 50,
step_begin = 30,
step_do = 30,
step_end = 30)
elif cmd == "b":
instr = ins.output_step_speed(nos = MOTOR_B | MOTOR_C,
speed = -50,
step_begin = 30,
step_do = 30,
step_end = 30)
elif cmd == "p":
# instr = ins.sound_repeat(2, "../prjs/tracker/idle")
instr = ins.sound_play(100, "../prjs/mluveni/huu")
elif cmd == "s":
instr = ins.sound_break()
elif cmd == "L":
instr = None
sinstr = sins.list_files(9999, "/home/root/lms2012/prjs")
p = ms.system_command_with_reply(sinstr)
l = p[0:4]
print(p[4:])
elif cmd == "D":
instr = None
with open("../sound-rsf/huu.rsf") as f:
contents = f.read()
sinstr = sins.begin_download(len(contents), "/home/root/lms2012/prjs/mluveni/huu.rsf")
p = ms.system_command_with_reply(sinstr)
handle = ord(p[0])
sinstr = sins.continue_download(handle, contents)
p = ms.system_command_with_reply(sinstr)
sinstr = sins.close_filehandle(handle)
try:
p = ms.system_command_with_reply(sinstr)
except RuntimeError:
pass # why does it reply Unknown Handle?
else:
print("?")
if instr is not None:
ms.direct_command_with_reply(instr)
instr = ins.output_ready(nos = MOTOR_B | MOTOR_C)
ms.direct_command_with_reply(instr)
sock.close()