33import time , serial , math
44
55from 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