Skip to content

Commit bddda49

Browse files
committed
feat: add new method of MyCobot
* detail see pymycobot/README.md * change set_claw() -> set_gripper_state()
1 parent d9eeb7b commit bddda49

File tree

3 files changed

+234
-17
lines changed

3 files changed

+234
-17
lines changed

pymycobot/README.md

Lines changed: 96 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,26 @@ from pymycobot.mycobot import MyCobot
381381
- `1`: enbale
382382
- `-1`: error
383383

384+
### release_servo()
385+
386+
- **Description**
387+
388+
Power on designated servo
389+
390+
- **Parameters**
391+
392+
servo_id: 1 ~ 6
393+
394+
### focus_servo()
395+
396+
- **Description**
397+
398+
Power off designated servo
399+
400+
- **Parameters**
401+
402+
servo_id: 1 ~ 6
403+
384404
## Atom IO
385405

386406
### set_led_color()
@@ -393,15 +413,88 @@ from pymycobot.mycobot import MyCobot
393413

394414
rgb: (`string`) like: "FF0000"
395415

396-
### set_claw()
416+
### set_pin_mode()
417+
418+
- **Parameters**
419+
420+
pin_no (int):
421+
pin_mode (int): 0 - input, 1 - output, 2 - input_pullup
422+
423+
### set_digital_output()
424+
425+
- **Parameters**
426+
427+
pin_no (int):
428+
429+
pin_signal (int): 0 / 1
430+
431+
### get_digital_input()
397432

398433
- **Description**
399434

400-
Set the color of the light on the top of the robot arm.
435+
- **Parameters**
436+
437+
### set_pwm_mode()
438+
439+
- **Description**
401440

402441
- **Parameters**
403442

404-
flag: (`int`) 0 - open, 1 - close
443+
### set_pwm_output()
444+
445+
- **Description**
446+
447+
- **Parameters**
448+
449+
### get_gripper_value()
450+
451+
- **Description**
452+
453+
Get gripper value
454+
455+
### set_gripper_state()
456+
457+
- **Description**
458+
459+
Set gripper switch
460+
461+
- **Parameters**
462+
463+
flag (`int`): 0 - open, 1 - close
464+
465+
speed (`int`): 0 ~ 100
466+
467+
### set_gripper_value()
468+
469+
- **Description**
470+
471+
Set gripper value
472+
473+
- **Parameters**
474+
475+
value (int): 0 ~ 496
476+
477+
speed (int): 0 ~ 100
478+
479+
### set_gripper_ini()
480+
481+
- **Description**
482+
483+
Set the current position to zero
484+
485+
Current position value is `248`.
486+
487+
### is_gripper_moving()
488+
489+
- **Description**
490+
491+
Judge whether the gripper is moving or not
492+
493+
- Returns
494+
495+
- `0` : not moving
496+
- `1` : is moving
497+
- `-1`: error data
405498

406499
# Angle
407500

pymycobot/common.py

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,24 @@ class Command():
4242
# SERVO CONTROL
4343
IS_SERVO_ENABLE = 0x50
4444
IS_ALL_SERVO_ENABLE = 0x51
45+
RELEASE_SERVO = 0x56
46+
FOCUS_SERVO = 0x57
4547

4648
# ATOM IO
49+
SET_PIN_MODE = 0x60
50+
SET_DIGITAL_OUTPUT = 0x61
51+
GET_DIGITAL_INPUT = 0x62
52+
SET_PWM_MODE = 0x63
53+
SET_PWM_OUTPUT = 0x64
54+
GET_GRIPPER_VALUE = 0x65
55+
SET_GRIPPER_STATE = 0x66
56+
SET_GRIPPER_VALUE = 0x67
57+
SET_GRIPPER_INI = 0x68
58+
IS_GRIPPER_MOVING = 0x69
4759
SET_COLOR = 0x6a
48-
SET_CLAW = 0x66
4960

5061

51-
class DataProcesser():
62+
class MyCobotData():
5263
# Functional approach
5364
def _encode_int8(self, data):
5465
return struct.pack('b', data)
@@ -79,12 +90,15 @@ def _flatten(self, _list):
7990
for x in _list), [])
8091

8192
def _process_data_command(self, data, genre):
93+
# lenght = 0
8294
if not data:
8395
return []
8496

97+
# lenght >= 6
8598
if genre in [
86-
Command.SEND_ANGLES, Command.SEND_COORDS,
87-
Command.IS_IN_POSITION
99+
Command.SEND_ANGLES,
100+
Command.SEND_COORDS,
101+
Command.IS_IN_POSITION,
88102
]:
89103
_data_list = []
90104
for value in data[:6]:
@@ -93,16 +107,30 @@ def _process_data_command(self, data, genre):
93107
_data_list.append((value))
94108
return _data_list
95109

110+
# lenght = 3
96111
elif genre in [Command.SEND_ANGLE, Command.SEND_COORD]:
97112
return self._flatten(
98113
[data[0],
99114
self._flatten(self._encode_int16(data[1])), data[2]])
100115

116+
# lenght = 2
101117
elif genre in [
102-
Command.JOG_ANGLE, Command.JOG_COORD, Command.SET_COLOR
118+
Command.JOG_ANGLE,
119+
Command.JOG_COORD,
120+
Command.SET_PIN_MODE,
121+
Command.SET_DIGITAL_OUTPUT,
122+
Command.SET_PWM_MODE,
123+
Command.SET_PWM_OUTPUT,
124+
Command.SET_GRIPPER_STATE,
125+
Command.SET_COLOR,
103126
]:
104127
return data
105128

129+
elif genre in [Command.SET_GRIPPER_VALUE]:
130+
return self._flatten(
131+
[self._flatten(self._encode_int16(data[0])), data[1]])
132+
133+
# lenght = 1
106134
else:
107135
return [data[0]]
108136

@@ -127,6 +155,8 @@ def _process_recived(self, data, genre):
127155
return []
128156
data_pos = idx + 4
129157
valid_data = (data[data_pos:data_pos + data_len])
158+
print(data_len)
159+
print(valid_data)
130160

131161
res = []
132162
if data_len == 12:
@@ -141,6 +171,3 @@ def _process_recived(self, data, genre):
141171

142172
def _process_single(self, data):
143173
return data[0] if data else -1
144-
145-
146-

pymycobot/mycobot.py

Lines changed: 103 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import time, serial, math
44

55
from pymycobot.error import MyCobotDataException, check_id
6-
from pymycobot.common import Command, DataProcesser
6+
from pymycobot.common import Command, MyCobotData
77

88

9-
class MyCobot(DataProcesser):
9+
class MyCobot(MyCobotData):
1010
'''MyCobot Python API
1111
1212
Possessed function:
@@ -46,10 +46,22 @@ class MyCobot(DataProcesser):
4646
# Servo control
4747
is_servo_enable()
4848
is_all_servo_enable()
49+
release_servo()
50+
focus_servo()
4951
5052
# Atom IO
5153
set_led_color()
5254
set_claw()
55+
set_pin_mode()
56+
set_digital_output()
57+
get_digital_input()
58+
set_pwm_mode()
59+
set_pwm_output()
60+
get_gripper_value()
61+
set_gripper_state()
62+
set_gripper_value()
63+
set_gripper_ini()
64+
is_gripper_moving()
5365
'''
5466

5567
def __init__(self, port, boudrate='115200', timeout=0.1):
@@ -353,6 +365,24 @@ def is_all_servo_enable(self):
353365
return self._process_single(
354366
self.__mesg(Command.IS_ALL_SERVO_ENABLE, has_reply=True))
355367

368+
def release_servo(self, servo_id):
369+
'''Power on designated servo
370+
371+
Args:
372+
servo_id: 1 ~ 6
373+
374+
'''
375+
self.__mesg(Command.RELEASE_SERVO, servo_id)
376+
377+
def focus_servo(self, servo_id):
378+
'''Power off designated servo
379+
380+
Args:
381+
servo_id: 1 ~ 6
382+
383+
'''
384+
self.__mesg(Command.RELEASE_SERVO, servo_id)
385+
356386
# Atom IO
357387
def set_led_color(self, rgb):
358388
'''Set the light color
@@ -375,14 +405,81 @@ def set_led_color(self, rgb):
375405
self._serial_port.flush()
376406
time.sleep(0.05)
377407

378-
def set_claw(self, flag):
379-
'''Set claw switch
408+
def set_pin_mode(self, pin_no, pin_mode):
409+
'''
410+
411+
Args:
412+
pin_no (int):
413+
pin_mode (int): 0 - input, 1 - output, 2 - input_pullup
414+
415+
'''
416+
self.__mesg(Command.SET_PIN_MODE, data=[pin_no, pin_mode])
417+
418+
def set_digital_output(self, pin_no, pin_signal):
419+
'''
380420
381421
Args:
382-
flag (int): 0 - open, 1 - close
422+
pin_no (int):
423+
pin_signal (int): 0 / 1
424+
425+
'''
426+
self.__mesg(Command.SET_DIGITAL_OUTPUT, data=[pin_no, pin_signal])
427+
428+
def get_digital_input(self, pin_no):
429+
return self._process_single(
430+
self.__mesg(Command.GET_DIGITAL_INPUT,
431+
data=[pin_no],
432+
has_reply=True))
433+
434+
def set_pwm_mode(self, pin_no, channel):
435+
self.__mesg(Command.SET_PWM_MODE, data=[pin_no, channel])
436+
437+
def set_pwm_output(self, channel, pin_val):
438+
self.__mesg(Command.SET_PWM_OUTPUT, data=[channel, pin_val])
439+
440+
def get_gripper_value(self):
441+
return self._process_single(
442+
self.__mesg(Command.GET_GRIPPER_VALUE, has_reply=True))
443+
444+
def set_gripper_state(self, flag, speed):
445+
'''Set gripper switch
446+
447+
Args:
448+
flag (int): 0 - open, 1 - close
449+
speed (int): 0 ~ 100
383450
384451
'''
385452
if not flag in [0, 1]:
386453
raise MyCobotDataException('eror flag, please input 0 or 1')
387454

388-
self.__mesg(Command.SET_CLAW, data=[flag])
455+
self.__mesg(Command.SET_GRIPPER_STATE, data=[flag, speed])
456+
457+
def set_gripper_value(self, value, speed):
458+
'''Set gripper value
459+
460+
Args:
461+
value (int): 0 ~ 496
462+
speed (int): 0 ~ 100
463+
464+
'''
465+
self.__mesg(Command.SET_GRIPPER_VALUE, data=[value, speed])
466+
467+
def set_gripper_ini(self):
468+
'''Set the current position to zero
469+
470+
Current position value is `248`.
471+
472+
'''
473+
self.__mesg(Command.SET_GRIPPER_INI)
474+
475+
def is_gripper_moving(self):
476+
'''Judge whether the gripper is moving or not
477+
478+
Returns:
479+
0 : not moving
480+
1 : is moving
481+
-1: error data
482+
483+
'''
484+
return self._process_single(
485+
self.__mesg(Command.IS_GRIPPER_MOVING, has_reply=True))

0 commit comments

Comments
 (0)