-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed_command.py
354 lines (256 loc) · 7.41 KB
/
seed_command.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env python
from struct import*
import serial
import time
import array
import sys
import random
class SeedCommand:
def __init__(self,port=0,baud=460800):
self.ser=serial.Serial('/dev/ttyUSB'+str(port), baud, timeout=0.1)
#COM Port Open
def COM_Open(self):
self.ser.write("S8\r")
self.ser.write("O\r")
#Com Port Close
def COM_Close(self):
#self.ser.write("C\r")
self.ser.close()
#Binary data to Hex String data and decide number of byte
def num2str(self,value,byte=1):
if value < 0:
value = 0xFFFFFF + 1 + value
#return str(hex(value).upper()[3:][-2:].rjust(2**byte,"F"))
return str(hex(value).upper()[2:][-2:].rjust(2**byte,"0"))
#Binary data to Hex data and decide number of byte
def num2hex(self,value,byte=1):
if value < 0:
value = 0xFFFFFF + 1 + value
return hex(value).upper()[2:][-2:].rjust(2**byte,"0")
#SEED CAN data send
def SEED_CAN_Snd(self,SndData):
CAN_Transmit = ''.join(SndData) + '\r'
self.ser.write(CAN_Transmit)
print "Send Data \t:%s" % CAN_Transmit
#SEED CAN data read
def SEED_CAN_Read(self):
ret_str = ''
while(self.ser.inWaiting() < 1):
pass
while(self.ser.read(1) != 't'):
pass
ret_str = 't'
# Get Data Length
ret = self.ser.read(4)
data_len = int(ret[-1],16)
ret_str += ret
# Get All data
ret = self.ser.read(data_len*2)
ret_str += ret
self.ser.flushInput()
print "Receive Data \t:%s" % ret_str
return ret_str
#SEED UART data send
def SEED_UART_Snd(self,SndData):
self.ser.write(''.join(map(chr,SndData)))
#print "Send Data \t:%s" % ''.join("{:02X}".format(ord(c)) for c in ''.join(map(chr,SndData)))
#print ''.join("{:02X}".format(ord(c)) for c in ''.join(map(chr,SndData)))
#SEED UART data read
def SEED_UART_Read(self,number):
while(self.ser.inWaiting() < number):
pass
RcvData = ''.join("{:02X}".format(ord(c)) for c in self.ser.read(number))
if (len(RcvData) == number*2):
self.pre_RcvData = RcvData
print "Receive Data \t:%s" % RcvData
return RcvData
else :
print "Read Error"
return self.pre_RcvData
#print "Busy: %s, IO:%s" % (RcvData[9],RcvData[11])
#print "Receive Data \t:%s" % RcvData
#####################################################################################################
#####################################################################################################
########################## Dynamixel Function ##################################
#####################################################################################################
#Aero Data Read
def SEED_DX_Read(self):
while(self.ser.inWaiting() < 4):
pass
headder1 = ord(self.ser.read(1))
if (headder1 == 0xFF):
pass
else:
print "Read Error. Headder is %d" % headder1
self.ser.flushInput()
self.ser.flushOutput()
return None
headder2 = ord(self.ser.read(1))
id_data = ord(self.ser.read(1))
data_length = ord(self.ser.read(1))
timeout = time.time() + 1
while(self.ser.inWaiting() < data_length):
if(time.time() > timeout):
print "Read Error. data_length is %d" % data_length
self.ser.flushInput()
self.ser.flushOutput()
return None
else:
pass
RcvData = self.num2str(headder1) + self.num2str(headder2) + self.num2hex(id_data) + self.num2hex(data_length)\
+ ''.join("{:02X}".format(ord(c)) for c in self.ser.read(data_length))
#print "Receive Data \t:%s" % RcvData
return RcvData
def SEED_DX_Get(self,ID):
bCheckSum = 0
bCount = 0
bLength = 8
RS_TX= bLength*[0]
RS_TX[0] = 0xFF
RS_TX[1] = 0xFF
RS_TX[2] = ID
RS_TX[3] = 0x04 # Data Length
RS_TX[4] = 0x02 # Instruction
RS_TX[5] = 0x24 # Address
RS_TX[6] = 0x08 # Count
#check sum
for bCount in range(2,bLength-1,1):
bCheckSum += int(self.num2hex(RS_TX[bCount]),16)
RS_TX[bLength - 1] = int(self.num2hex(~bCheckSum),16)
#num to hex
for i in range(0,bLength-1):
RS_TX[i] = int(self.num2hex(RS_TX[i]),16)
self.SEED_UART_Snd(RS_TX)
def SEED_DX_Get_POS(self,ID):
bCheckSum = 0
bCount = 0
bLength = 8
RS_TX= bLength*[0]
RS_TX[0] = 0xFF
RS_TX[1] = 0xFF
RS_TX[2] = ID
RS_TX[3] = 0x04
RS_TX[4] = 0x02
RS_TX[5] = 0x24
RS_TX[6] = 0x02
#check sum
for bCount in range(2,bLength-1,1):
bCheckSum += int(self.num2hex(RS_TX[bCount]),16)
RS_TX[bLength - 1] = int(self.num2hex(~bCheckSum),16)
#num to hex
for i in range(0,bLength-1):
RS_TX[i] = int(self.num2hex(RS_TX[i]),16)
self.SEED_UART_Snd(RS_TX)
def SEED_DX_SRV(self,ID,SW):
bCheckSum = 0
bCount = 0
bLength = 9
RS_TX= bLength*[0]
RS_TX[0] = 0xFF
RS_TX[1] = 0xFF
RS_TX[2] = ID
RS_TX[3] = 0x05
RS_TX[4] = 0x03
RS_TX[5] = 0x18
RS_TX[6] = SW
RS_TX[7] = SW
#check sum
for bCount in range(2,bLength-1,1):
bCheckSum += int(self.num2hex(RS_TX[bCount]),16)
RS_TX[bLength - 1] = int(self.num2hex(~bCheckSum),16)
#num to hex
for i in range(0,bLength-1):
RS_TX[i] = int(self.num2hex(RS_TX[i]),16)
self.SEED_UART_Snd(RS_TX)
def SEED_DX_TURN(self,ID,SPD):
bCheckSum = 0
bCount = 0
bLength = 9
SndData = bLength*[0]
SndData[0] = 0xFF
SndData[1] = 0xFF
SndData[2] = ID
SndData[3] = 0x05 #LENGTH
SndData[4] = 0x03 #Instruction
SndData[5] = 0x20 #CMD
SndData[6] = SPD
SndData[7] = SPD >> 8
#check sum
for bCount in range(2,bLength-1,1):
bCheckSum += int(self.num2hex(SndData[bCount]),16)
SndData[bLength - 1] = int(self.num2hex(~bCheckSum),16)
#num to hex
for i in range(0,bLength-1):
SndData[i] = int(self.num2hex(SndData[i]),16)
self.SEED_UART_Snd(SndData)
def SEED_DX_MOV(self,ID,SPD,TRG):
bCheckSum = 0
bCount = 0
bLength = 11
SndData = bLength*[0]
SndData[0] = 0xFF
SndData[1] = 0xFF
SndData[2] = ID
SndData[3] = 0x07 #LENGTH
SndData[4] = 0x03 #Instruction
SndData[5] = 0x1E #CMD
SndData[6] = TRG
SndData[7] = TRG >> 8
SndData[8] = SPD
SndData[9] = SPD >> 8
#check sum
for bCount in range(2,bLength-1,1):
bCheckSum += int(self.num2hex(SndData[bCount]),16)
SndData[bLength - 1] = int(self.num2hex(~bCheckSum),16)
#num to hex
for i in range(0,bLength-1):
SndData[i] = int(self.num2hex(SndData[i]),16)
self.SEED_UART_Snd(SndData)
def SEED_DX_MODE(self,ID,CW_Limit,CCW_Limit):
bCheckSum = 0
bCount = 0
bLength = 11
SndData = bLength*[0]
SndData[0] = 0xFF
SndData[1] = 0xFF
SndData[2] = ID
SndData[3] = 0x07 #LENGTH
SndData[4] = 0x03 #Instruction
SndData[5] = 0x06 #CMD
SndData[6] = CW_Limit
SndData[7] = CW_Limit >> 8
SndData[8] = CCW_Limit
SndData[9] = CCW_Limit >> 8
#check sum
for bCount in range(2,bLength-1,1):
bCheckSum += int(self.num2hex(SndData[bCount]),16)
SndData[bLength - 1] = int(self.num2hex(~bCheckSum),16)
#num to hex
for i in range(0,bLength-1):
SndData[i] = int(self.num2hex(SndData[i]),16)
self.SEED_UART_Snd(SndData)
def SEED_DX_PING(self):
bCheckSum = 0
bCount = 0
bLength = 6
SndData = bLength*[0]
SndData[0] = 0xFF
SndData[1] = 0xFF
SndData[2] = 0xFE
SndData[3] = 0x02 #LENGTH
SndData[4] = 0x01 #Instruction
#check sum
for bCount in range(2,bLength-1,1):
bCheckSum += int(self.num2hex(SndData[bCount]),16)
SndData[bLength - 1] = int(self.num2hex(~bCheckSum),16)
#num to hex
for i in range(0,bLength-1):
SndData[i] = int(self.num2hex(SndData[i]),16)
self.SEED_UART_Snd(SndData)
rec = self.SEED_DX_Read()
if (rec == None):
print "False"
return None
else:
print "ID No. -> %d" % int(rec[4]+rec[5],16)
return int(rec[4]+rec[5],16)