From 575a9ec4f65f37365f120964ce683c5fce184b1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=AB=8B=E5=86=9B?= Date: Tue, 29 Dec 2020 17:31:38 +0800 Subject: [PATCH] feat(pythonapi): add test file, update README --- scripts/pythonAPI/README.md | 364 ++++++++++++++++++++++++++++++++++++ scripts/test.py | 57 +++++- 2 files changed, 418 insertions(+), 3 deletions(-) diff --git a/scripts/pythonAPI/README.md b/scripts/pythonAPI/README.md index fe58000..d0e31c0 100644 --- a/scripts/pythonAPI/README.md +++ b/scripts/pythonAPI/README.md @@ -1,2 +1,366 @@ # ros-python-api +**This is python API for mycobot.** + +We support Python2, Python3.5 or later. If you want to use the api, make sure `pyserial` is installed. + +```bash +pip2 install pyserial +# or +pip3 install pyserial +``` + +**Class**: +- [MyCobot](##MyCobot) +- [Angle](##Angle) +- [Coord](##Coord) + + +## MyCobot + +### MyCobot.power_on() + +- **Description** + + Robot arm power up. + +- **Parameters** + + None + +- **Returns** + + None + +### MyCobot.power_off() + +- **Description** + + Robot arm power down. + +- **Parameters** + + None + +- **Returns** + + None + +### MyCobot.set_free_mode() + +- **Description** + + Robot arm into free moving mode. + +- **Parameters** + + None + +- **Returns** + + None + +### MyCobot.get_angles() + +- **Description** + + Get the degree of all joints. + +- **Parameters** + + None + +- **Returns** + + A float list of degree. + +### MyCobot.get_angles_by_radian() + +- **Description** + + Get the radians of all joints. + +- **Parameters** + + None + +- **Returns** + + A float list of radian. + +### MyCobot.send_angle() + +- **Description** + + Send one degree of joint to robot arm. + +- **Parameters** + + id: Joint id(common.Angle) + + degree: degree value(float) + + speed: (int) + +- **Returns** + + None + +- **Example** + + ```python + from pythonAPI.mycobot import MyCobot + from pythonAPI.common import Angle + + + mycobot = MyCobot() + mycobot.send_angle(Angle.J2.value, 10, 50) + ``` + +### MyCobot.send_angles() + +- **Description** + + Send the degrees of all joints to robot arm. + +- **Parameters** + + degrees: a list of degree value(List[float]) + + speed: (int) + +- **Returns** + + None + +- **Example** + + ```python + from pythonAPI.mycobot import MyCobot + from pythonAPI.common import Angle + + + mycobot = MyCobot() + mycobot.send_angles([0,0,0,0,0,0], 80) + ``` + +### MyCobot.send_angles_by_radian() + +- **Description** + + Send the radians of all joint to robot arm. + +- **Parameters** + + degrees: a list of radian value(List[float]) + + speed: (int) + +- **Returns** + + None + +- **Example** + + ```python + from pythonAPI.mycobot import MyCobot + from pythonAPI.common import Angle + + + mycobot = MyCobot() + mycobot.send_angles_by_radian([1,1,1,1,1,1], 70) + ``` + +### MyCobot.get_coords() + +- **Description** + + Get the Coords from robot arm. + +- **Parameters** + + None + +- **Returns** + + A float list of coord. + +### MyCobot.send_coord() + +- **Description** + + Send one coord to robot arm. + +- **Parameters** + + id: coord name(common.Coord) + + coord: coord value(float) + + speed: (int) + +- **Returns** + + None + +- **Example** + + ```python + from pythonAPI.mycobot import MyCobot + from pythonAPI.common import Coord + + + mycobot = MyCobot() + mycobot.send_coord(Coord.X.value, -40, 70) + ``` + +### MyCobot.send_coords() + +- **Description** + + Send all coords to robot arm. + +- **Parameters** + + coords: a list of coords value(List[float]) + + speed: (int) + +- **Returns** + + None + +- **Example** + + ```python + from pythonAPI.mycobot import MyCobot + from pythonAPI.common import Coord + + + mycobot = MyCobot() + mycobot.send_coords([160, 160, 160, 0, 0, 0], 70, 0) + ``` +### MyCobot.set_color() + +- **Description** + + Set the color of the light on the top of the robot arm. + + +- **Parameters** + + rgb: (string) like: "FF0000" + +- **Returns** + + None +### MyCobot.is_moving() + +- **Description** + + Judge whether the manipulator is moving or not. + +- **Parameters** + + None + +- **Returns** + + bool: `True` - moving, `False` - not moving. + +### MyCobot.pause() + +- **Description** + + Pause movement. + +- **Parameters** + + None + +- **Returns** + + None + +### MyCobot.resume() + +- **Description** + + Recovery movement. + +- **Parameters** + + None + +- **Returns** + + None + +### MyCobot.stop() + +- **Description** + + Stop moving. + +- **Parameters** + + None + +- **Returns** + + None + +### MyCobot.is_pause() + +- **Description** + + Judge whether the manipulator pauses or not. + +- **Parameters** + + None + +- **Returns** + + bool: `True` - pause, `False` - not pause. + +### MyCobot.get_speed() + +- **Description** + + Get speed. + +- **Parameters** + + None + +- **Returns** + + speed: (int) + +### MyCobot.set_speed() + +- **Description** + + Set speed. + +- **Parameters** + + speed: (int) + +- **Returns** + + None + +## Angle + +**Description** + +Instance class of joint. It's recommended to use this class to select joint. + + +## Coord + +**Description** + +Instance class of coord. It's recommended to use this class to select coord. \ No newline at end of file diff --git a/scripts/test.py b/scripts/test.py index d4e38bf..f20e7c2 100644 --- a/scripts/test.py +++ b/scripts/test.py @@ -1,8 +1,59 @@ -#!/usr/bin/env python3 +import time, random from pythonAPI.mycobot import MyCobot +from pythonAPI.common import Angle, Coord if __name__ == '__main__': mycobot = MyCobot() - mycobot.set_color("ff0000") - print(mycobot.get_angles_of_radian()) + + print('Start check api\n') + # print() + + color_name = ['red', 'green', 'blue'] + color_code = ['ff0000', '00ff00', '0000ff'] + print('::ser_color()') + i = random.randint(0, len(color_code) - 1) + mycobot.set_color(color_code[i]) + print('==>set color {}\n'.format(color_name[i])) + time.sleep(0.5) + + print('::get_angles()') + print('==> degrees: {}\n'.format(mycobot.get_angles())) + time.sleep(0.5) + + print('::get_angles_of_radian()') + print('==> radians: {}\n'.format(mycobot.get_angles_of_radian())) + time.sleep(0.5) + print('::send_angles()') + mycobot.send_angles([0,0,0,0,0,0], 80) + print('==> set angles [0,0,0,0,0,0], speed 80\n') + time.sleep(3) + + print('::send_angles_by_radian') + mycobot.send_angles_by_radian([1,1,1,1,1,1], 70) + print('==> set raidans [1,1,1,1,1,1], speed 70\n') + time.sleep(1.5) + + print('::send_angle()') + mycobot.send_angle(Angle.J2.value, 10, 50) + print('==> angle: joint2, degree: 10, speed: 50\n') + time.sleep(1) + + print('::get_coords()') + print('==> coords {}\n'.format(mycobot.get_coords())) + time.sleep(0.5) + + print('::send_coords()') + mycobot.send_coords([160, 160, 160, 0, 0, 0], 70, 0) + print('==> send coords [160,160,160,0,0,0], speed 70, mode 0\n') + time.sleep(2.5) + + print('::send_coord()') + mycobot.send_coord(Coord.X.value, -40, 70) + print('==> send coord id: X, coord value: -40, speed: 70\n') + time.sleep(2) + + print('::set_free_mode()') + mycobot.set_free_mode() + print('==> into free moving mode.') + print('=== check end <==\n')