mirror of
https://github.com/elephantrobotics/mycobot_ros.git
synced 2026-07-14 09:00:53 +00:00
feat(api): python api add new method and modify usage
This commit is contained in:
parent
e14ece5fc0
commit
411a2a4c65
3 changed files with 101 additions and 20 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -1,3 +1,5 @@
|
||||||
|
|
||||||
urdf.zip
|
urdf.zip
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
.vscode/settings.json
|
||||||
|
.vscode/c_cpp_properties.json
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import sys
|
import sys
|
||||||
sys.path.append('.')
|
sys.path.append('.')
|
||||||
import time, subprocess, serial, struct
|
import time, serial, struct
|
||||||
|
|
||||||
class MyCobot():
|
class MyCobot():
|
||||||
'''MyCobot Python API
|
'''MyCobot Python API
|
||||||
|
|
@ -16,6 +16,9 @@ class MyCobot():
|
||||||
set_color() :
|
set_color() :
|
||||||
get_coords() :
|
get_coords() :
|
||||||
send_coords() :
|
send_coords() :
|
||||||
|
jog_angle() :
|
||||||
|
jog_coord() :
|
||||||
|
jog_stop() :
|
||||||
is_moving() :
|
is_moving() :
|
||||||
pause() :
|
pause() :
|
||||||
resume() :
|
resume() :
|
||||||
|
|
@ -25,9 +28,10 @@ class MyCobot():
|
||||||
set_speed() :
|
set_speed() :
|
||||||
'''
|
'''
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, port):
|
||||||
_prot = subprocess.check_output(['echo -n /dev/ttyUSB*'],
|
# _prot = subprocess.check_output(['echo -n /dev/ttyUSB*'],
|
||||||
shell=True)
|
# shell=True)
|
||||||
|
_prot = port
|
||||||
_boudrate = '115200'
|
_boudrate = '115200'
|
||||||
_timeout = 0.1
|
_timeout = 0.1
|
||||||
|
|
||||||
|
|
@ -196,9 +200,42 @@ class MyCobot():
|
||||||
command += (_hex)
|
command += (_hex)
|
||||||
|
|
||||||
command += '{}{}fa'.format(speed, mode)
|
command += '{}{}fa'.format(speed, mode)
|
||||||
print(command)
|
# print(command)
|
||||||
self._write(command)
|
self._write(command)
|
||||||
|
|
||||||
|
def jog_angle(joint_id, direction, speed):
|
||||||
|
'''Joint control
|
||||||
|
|
||||||
|
joint_id: string
|
||||||
|
direction: int [0, 1]
|
||||||
|
speed: int (0 - 100)
|
||||||
|
'''
|
||||||
|
command = 'fefe0530'
|
||||||
|
direction = hex(direction)[2:]
|
||||||
|
direction = self._complement_zero(direction, digit=2)
|
||||||
|
speed = hex(speed)[2:]
|
||||||
|
speed = self._complement_zero(speed, digit=2)
|
||||||
|
command += '{}{}{}fa'.format(joint_id, direction, speed)
|
||||||
|
self._write(command)
|
||||||
|
|
||||||
|
def jog_coord(coord, direction, speed):
|
||||||
|
'''Coord control
|
||||||
|
|
||||||
|
coord: string
|
||||||
|
direction: int [0, 1]
|
||||||
|
speed: int (0 - 100)
|
||||||
|
'''
|
||||||
|
command = 'fefe0532'
|
||||||
|
direction = hex(direction)[2:]
|
||||||
|
direction = self._complement_zero(direction, digit=2)
|
||||||
|
speed = hex(speed)[2:]
|
||||||
|
speed = self._complement_zero(speed, digit=2)
|
||||||
|
command += '{}{}{}fa'.format(coord, direction, speed)
|
||||||
|
self._write(command)
|
||||||
|
|
||||||
|
def jog_stop(self):
|
||||||
|
self._write('fefe0234fa')
|
||||||
|
|
||||||
def is_servo_enable(self):
|
def is_servo_enable(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
@ -282,7 +319,10 @@ class MyCobot():
|
||||||
def _parse_data(self, data, name):
|
def _parse_data(self, data, name):
|
||||||
data_list = []
|
data_list = []
|
||||||
data = data.encode('hex')
|
data = data.encode('hex')
|
||||||
# print(data)
|
data = data[-28:]
|
||||||
|
print(data)
|
||||||
|
if not (data.startswith('20') and data.endswith('fa')):
|
||||||
|
return []
|
||||||
if name == 'get_angles':
|
if name == 'get_angles':
|
||||||
data = data[-26:-2]
|
data = data[-26:-2]
|
||||||
for i in range(6):
|
for i in range(6):
|
||||||
|
|
@ -294,14 +334,14 @@ class MyCobot():
|
||||||
data = data[-26:-2]
|
data = data[-26:-2]
|
||||||
for i in range(6):
|
for i in range(6):
|
||||||
_hex = data[i * 4: (i * 4) + 4]
|
_hex = data[i * 4: (i * 4) + 4]
|
||||||
_coord = self._hex_to_int(_hex) / 10
|
_coord = self._hex_to_int(_hex) / 10.0
|
||||||
data_list.append(_coord)
|
data_list.append(_coord)
|
||||||
|
|
||||||
elif name == 'get_angles_of_radian':
|
elif name == 'get_angles_of_radian':
|
||||||
data = data[-26:-2]
|
data = data[-26:-2]
|
||||||
for i in range(6):
|
for i in range(6):
|
||||||
_hex = data[i * 4: (i * 4) + 4]
|
_hex = data[i * 4: (i * 4) + 4]
|
||||||
_radian = self._hex_to_int(_hex) / 1000
|
_radian = self._hex_to_int(_hex) / 1000.0
|
||||||
data_list.append(_radian)
|
data_list.append(_radian)
|
||||||
|
|
||||||
return (data_list)
|
return (data_list)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import sys
|
import sys
|
||||||
sys.path.append('.')
|
sys.path.append('.')
|
||||||
import time, subprocess, serial, struct
|
import time, serial, struct
|
||||||
|
|
||||||
class MyCobot():
|
class MyCobot():
|
||||||
'''MyCobot Python API
|
'''MyCobot Python API
|
||||||
|
|
@ -16,6 +16,9 @@ class MyCobot():
|
||||||
set_color() :
|
set_color() :
|
||||||
get_coords() :
|
get_coords() :
|
||||||
send_coords() :
|
send_coords() :
|
||||||
|
jog_angle() :
|
||||||
|
jog_coord() :
|
||||||
|
jog_stop() :
|
||||||
is_moving() :
|
is_moving() :
|
||||||
pause() :
|
pause() :
|
||||||
resume() :
|
resume() :
|
||||||
|
|
@ -25,10 +28,11 @@ class MyCobot():
|
||||||
set_speed() :
|
set_speed() :
|
||||||
'''
|
'''
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self, port):
|
||||||
_prot = subprocess.run(['echo -n /dev/ttyUSB*'],
|
# _prot = subprocess.run(['echo -n /dev/ttyUSB*'],
|
||||||
stdout=subprocess.PIPE,
|
# stdout=subprocess.PIPE,
|
||||||
shell=True).stdout.decode('utf-8')
|
# shell=True).stdout.decode('utf-8')
|
||||||
|
_prot = port
|
||||||
_boudrate = '115200'
|
_boudrate = '115200'
|
||||||
_timeout = 0.1
|
_timeout = 0.1
|
||||||
|
|
||||||
|
|
@ -45,10 +49,10 @@ class MyCobot():
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
def power_on(self):
|
def power_on(self):
|
||||||
self._write('fe fe 02 10 fa')
|
self._write('fefe0210fa')
|
||||||
|
|
||||||
def power_off(self):
|
def power_off(self):
|
||||||
self._write('fe fe 02 11 fa')
|
self._write('fefe0211fa')
|
||||||
|
|
||||||
def set_free_mode(self):
|
def set_free_mode(self):
|
||||||
self._write('fefe0213fa')
|
self._write('fefe0213fa')
|
||||||
|
|
@ -150,7 +154,7 @@ class MyCobot():
|
||||||
data_list (list): [x, y, z, rx, ry, rz] (mm)
|
data_list (list): [x, y, z, rx, ry, rz] (mm)
|
||||||
|
|
||||||
'''
|
'''
|
||||||
command = 'fe fe 02 23 fa'
|
command = 'fefe0223fa'
|
||||||
self._write(command)
|
self._write(command)
|
||||||
if self.serial_port.inWaiting() > 0:
|
if self.serial_port.inWaiting() > 0:
|
||||||
data = self._read()
|
data = self._read()
|
||||||
|
|
@ -200,6 +204,39 @@ class MyCobot():
|
||||||
# print(command)
|
# print(command)
|
||||||
self._write(command)
|
self._write(command)
|
||||||
|
|
||||||
|
def jog_angle(joint_id, direction, speed):
|
||||||
|
'''Joint control
|
||||||
|
|
||||||
|
joint_id: string
|
||||||
|
direction: int [0, 1]
|
||||||
|
speed: int (0 - 100)
|
||||||
|
'''
|
||||||
|
command = 'fefe0530'
|
||||||
|
direction = hex(direction)[2:]
|
||||||
|
direction = self._complement_zero(direction, digit=2)
|
||||||
|
speed = hex(speed)[2:]
|
||||||
|
speed = self._complement_zero(speed, digit=2)
|
||||||
|
command += '{}{}{}fa'.format(joint_id, direction, speed)
|
||||||
|
self._write(command)
|
||||||
|
|
||||||
|
def jog_coord(coord, direction, speed):
|
||||||
|
'''Coord control
|
||||||
|
|
||||||
|
coord: string
|
||||||
|
direction: int [0, 1]
|
||||||
|
speed: int (0 - 100)
|
||||||
|
'''
|
||||||
|
command = 'fefe0532'
|
||||||
|
direction = hex(direction)[2:]
|
||||||
|
direction = self._complement_zero(direction, digit=2)
|
||||||
|
speed = hex(speed)[2:]
|
||||||
|
speed = self._complement_zero(speed, digit=2)
|
||||||
|
command += '{}{}{}fa'.format(coord, direction, speed)
|
||||||
|
self._write(command)
|
||||||
|
|
||||||
|
def jog_stop(self):
|
||||||
|
self._write('fefe0234fa')
|
||||||
|
|
||||||
def is_servo_enable(self):
|
def is_servo_enable(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
@ -283,7 +320,9 @@ class MyCobot():
|
||||||
def _parse_data(self, data, name):
|
def _parse_data(self, data, name):
|
||||||
data_list = []
|
data_list = []
|
||||||
data = data.hex()
|
data = data.hex()
|
||||||
# print(data)
|
data = data[-28:]
|
||||||
|
if not (data.startswith('20') and data.endswith('fa')):
|
||||||
|
return []
|
||||||
if name == 'get_angles':
|
if name == 'get_angles':
|
||||||
data = data[-26:-2]
|
data = data[-26:-2]
|
||||||
for i in range(6):
|
for i in range(6):
|
||||||
|
|
@ -295,14 +334,14 @@ class MyCobot():
|
||||||
data = data[-26:-2]
|
data = data[-26:-2]
|
||||||
for i in range(6):
|
for i in range(6):
|
||||||
_hex = data[i * 4: (i * 4) + 4]
|
_hex = data[i * 4: (i * 4) + 4]
|
||||||
_coord = self._hex_to_int(_hex) / 10
|
_coord = self._hex_to_int(_hex) / 10.0
|
||||||
data_list.append(_coord)
|
data_list.append(_coord)
|
||||||
|
|
||||||
elif name == 'get_angles_of_radian':
|
elif name == 'get_angles_of_radian':
|
||||||
data = data[-26:-2]
|
data = data[-26:-2]
|
||||||
for i in range(6):
|
for i in range(6):
|
||||||
_hex = data[i * 4: (i * 4) + 4]
|
_hex = data[i * 4: (i * 4) + 4]
|
||||||
_radian = self._hex_to_int(_hex) / 1000
|
_radian = self._hex_to_int(_hex) / 1000.0
|
||||||
data_list.append(_radian)
|
data_list.append(_radian)
|
||||||
|
|
||||||
return (data_list)
|
return (data_list)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue