Skip to content

Commit 91deee5

Browse files
committed
fix: get_coord() error
1 parent 134a964 commit 91deee5

File tree

3 files changed

+53
-29
lines changed

3 files changed

+53
-29
lines changed

pymycobot/mycobot.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def jog_angle(self, joint_id, direction, speed):
218218
speed = self._complement_zero(speed, digit=2)
219219
command += '{}{}{}fa'.format(joint_id, direction, speed)
220220
self._write(command)
221-
221+
222222
def jog_coord(self, coord, direction, speed):
223223
'''Coord control
224224
@@ -269,7 +269,7 @@ def is_moving(self):
269269

270270
def pause(self):
271271
self._write('fefe0226fa')
272-
272+
273273
def resume(self):
274274
self._write('fefe0228fa')
275275

@@ -281,7 +281,7 @@ def is_paused(self):
281281
data = self._read()
282282
flag = int(data.encode('hex'), 16)
283283
return False if flag else True
284-
284+
285285
def is_in_position(self, coords):
286286
if len(coords) != 6:
287287
print('The lenght of coords is not right')
@@ -322,23 +322,31 @@ def _parse_data(self, data, name):
322322
data = data.encode('hex')
323323
data = data[-28:]
324324
# print(data)
325-
if not (data.startswith('20') and data.endswith('fa')):
326-
return []
327325
if name == 'get_angles':
326+
if not (data.startswith('20') and data.endswith('fa')):
327+
return []
328328
data = data[-26:-2]
329329
for i in range(6):
330330
_hex = data[i * 4: (i * 4) + 4]
331331
degree = self._hex_to_degree(_hex)
332332
data_list.append(degree)
333333

334334
elif name == 'get_coords':
335+
if not (data.startswith('23') and data.endswith('fa')):
336+
return []
335337
data = data[-26:-2]
336-
for i in range(6):
338+
for i in range(3):
337339
_hex = data[i * 4: (i * 4) + 4]
338340
_coord = self._hex_to_int(_hex) / 10.0
339341
data_list.append(_coord)
342+
for i in range(3, 6):
343+
_hex = data[i * 4: (i * 4) + 4]
344+
_coord = self._hex_to_int(_hex) / 1000.0
345+
data_list.append(_coord)
340346

341347
elif name == 'get_angles_of_radian':
348+
if not (data.startswith('20') and data.endswith('fa')):
349+
return []
342350
data = data[-26:-2]
343351
for i in range(6):
344352
_hex = data[i * 4: (i * 4) + 4]
@@ -350,7 +358,7 @@ def _parse_data(self, data, name):
350358
def _hex_to_degree(self, _hex):
351359
_int = self._hex_to_int(_hex)
352360
return _int * 18 / 314
353-
361+
354362
def _hex_to_int(self, _hex):
355363
_int = int(_hex, 16)
356364
if _int > 0x8000:
@@ -378,7 +386,7 @@ def _coord_to_hex(self, coord):
378386
s = str(hex(int(coord)))[2:]
379387
s = self._complement_zero(s)
380388
return s
381-
389+
382390
def _complement_zero(self, s, digit=4):
383391
s_len = len(s)
384392
if s_len == digit:

pymycobot/mycobot3.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,12 @@ def set_color(self, rgb):
251251
rgs (str): example 'ff0000'
252252
253253
'''
254-
command = 'fe fe 05 6a {} fa'.format(rgb)
254+
command = 'fefe056a{}fa'.format(rgb)
255255
# print(command)
256256
self._write(command)
257257

258258
def is_moving(self):
259-
command = 'fe fe 02 2b fa'
259+
command = 'fefe022bfa'
260260
self._write(command)
261261
data = self._read(2)
262262
# print(data)
@@ -269,25 +269,25 @@ def is_moving(self):
269269
return False
270270

271271
def pause(self):
272-
self._write('fe fe 02 26 fa')
273-
272+
self._write('fefe0226fa')
273+
274274
def resume(self):
275-
self._write('fe fe 02 28 fa')
275+
self._write('fefe0228fa')
276276

277277
def stop(self):
278-
self._write('fe fe 02 29 fa')
278+
self._write('fefe0229fa')
279279

280280
def is_paused(self):
281-
self._write('fe fe 02 27 fa')
281+
self._write('fefe0227fa')
282282
data = self._read()
283283
flag = int(data.hex(), 16)
284284
return False if flag else True
285-
285+
286286
def is_in_position(self, coords):
287287
if len(coords) != 6:
288288
print('The lenght of coords is not right')
289289
return
290-
command = 'fe fe 0d 2a '
290+
command = 'fefe0d2a '
291291
for coord in coords:
292292
_hex = self._coord_to_hex(coord)
293293

@@ -301,7 +301,7 @@ def is_in_position(self, coords):
301301
return False if flag else True
302302

303303
def get_speed(self):
304-
self._write('fe fe 02 40 fa')
304+
self._write('fefe0240fa')
305305
data = self._read()
306306
if data:
307307
return int(data.hex(), 16)
@@ -316,29 +316,37 @@ def set_speed(self, speed):
316316
if not 0 <= speed <= 100:
317317
raise Exception('speed value out of range (0 ~ 100)')
318318
_hex = str(hex(speed))[2:]
319-
self._write('fe fe 03 41 {} fa'.format(_hex))
319+
self._write('fefe0341{}fa'.format(_hex))
320320

321321
def _parse_data(self, data, name):
322322
data_list = []
323323
data = data.hex()
324324
data = data[-28:]
325-
if not (data.startswith('20') and data.endswith('fa')):
326-
return []
327325
if name == 'get_angles':
326+
if not (data.startswith('20') and data.endswith('fa')):
327+
return []
328328
data = data[-26:-2]
329329
for i in range(6):
330330
_hex = data[i * 4: (i * 4) + 4]
331331
degree = self._hex_to_degree(_hex)
332332
data_list.append(degree)
333333

334334
elif name == 'get_coords':
335+
if not (data.startswith('23') and data.endswith('fa')):
336+
return []
335337
data = data[-26:-2]
336-
for i in range(6):
338+
for i in range(3):
337339
_hex = data[i * 4: (i * 4) + 4]
338340
_coord = self._hex_to_int(_hex) / 10.0
339341
data_list.append(_coord)
342+
for i in range(3, 6):
343+
_hex = data[i * 4: (i * 4) + 4]
344+
_coord = self._hex_to_int(_hex) / 1000.0
345+
data_list.append(_coord)
340346

341347
elif name == 'get_angles_of_radian':
348+
if not (data.startswith('20') and data.endswith('fa')):
349+
return []
342350
data = data[-26:-2]
343351
for i in range(6):
344352
_hex = data[i * 4: (i * 4) + 4]
@@ -350,7 +358,7 @@ def _parse_data(self, data, name):
350358
def _hex_to_degree(self, _hex: str):
351359
_int = self._hex_to_int(_hex)
352360
return _int * 18 / 314
353-
361+
354362
def _hex_to_int(self, _hex: str):
355363
_int = int(_hex, 16)
356364
if _int > 0x8000:
@@ -378,7 +386,7 @@ def _coord_to_hex(self, coord):
378386
s = str(hex(coord))[2:]
379387
s = self._complement_zero(s)
380388
return s
381-
389+
382390
def _complement_zero(self, s, digit=4):
383391
s_len = len(s)
384392
if s_len == digit:

test/test.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
from pymycobot.mycobot import MyCobot
33
from pymycobot.common import Angle, Coord
44

5-
if __name__ == '__main__':
6-
port = subprocess.check_output(['echo -n /dev/ttyUSB*'],
7-
shell=True)
8-
mycobot = MyCobot(port)
9-
5+
def test(mycobot):
106
print('Start check api\n')
117
# print()
128

@@ -59,3 +55,15 @@
5955
mycobot.set_free_mode()
6056
print('==> into free moving mode.')
6157
print('=== check end <==\n')
58+
59+
60+
if __name__ == '__main__':
61+
port = subprocess.check_output(['echo -n /dev/ttyUSB*'],
62+
shell=True)
63+
mycobot = MyCobot(port)
64+
# test(mycobot)
65+
66+
while True:
67+
print(mycobot.get_coords())
68+
time.sleep(1)
69+

0 commit comments

Comments
 (0)