Visual grabbing kit
How to use visual grabbing kit
<<<<<<< HEAD
- How to install the visual grabbing kit
How to build the environment for Windows system
- Install python+pymycobot,pymycobot is a python package that conduct serial communication with myCobot ,it supports Python2,Python3.5 and later versions
Install OpenCV,OpenCV-Python is a Python based library,designed to solve computer vision problems.Before installation, ensure that the Python environment has been built
Open a console terminal(shotcut Win+R,type cmd to enter terminal),enter the following command:```Shell
The version numbers of the two should be consistent,here we install 4.5.5.62 version
=======
- 如何安装视觉抓取套装
Windows系统如何进行环境搭建
- 安装python+pymycobot,pymycobot 是一个和 myCobot 进行串口通讯的 Python 包,支持 Python2、Python3.5 及之后版本
安装OpenCV,OpenCV-Python是基于Python的库,旨在解决计算机视觉问题。安装之前需确保Python环境已完成搭建 打开一个控制台终端(快捷键Win+R,输入cmd进入终端),输入以下命令:
# 二者版本号需保持一致,这里安装4.5.5.62版本 >>>>>>> 482c782c42e70f0b2e44445345f6461e795f9189 pip install opencv-python==4.5.5.62 pip install opencv-contrib-python==4.5.5.62
<<<<<<< HEAD
- How to use Python
- How to use Python to open the camera
- How to use visual module to locate objects
- How to recognize object shape
- How to carry out color recognition
- How to recognize feature points
- How to carry out QR code recognition
- How to convert the positioned object to the coordinates recognized by the manipulator
- How to execute the converted coordinates
- How to control the suction pump/gripper to pick up objects
1 Environment construction
Using environment: Windows 10 or Windows 11、Python3+OpenCV+pymycobot、Git
Applicable equipment: ultraArm P340
1.1 Install Python+pymycobot
pymycobot is a python package that conduct serial communication with myCobot ,it supports Python2,Python3.5 and later versions
Before using pymycobot to control the robot arm, you need to build a Python environment,Please check the specific construction process on Pyhton Environment construction
1.2 Install OpenCV
OpenCV-Python is a Python based library,designed to solve computer vision problems.Before installation, ensure that the Python environment has been built
Open a console terminal(shotcut Win+R,type cmd to enter terminal),enter the following command:
# The version numbers of the two should be consistent,here we install 4.5.5.62 version
=======
- 如何使用Python
- 如何调整摄像头
- 如何使用视觉模块定位物体
- 如何进行物体形状识别
- 如何进行颜色识别
- 如何进行特征点识别
- 如何进行二维码识别
- 如何转换定位的物体为机械臂识别的坐标
- 如何执行转换后的坐标
- 如何控制吸泵/夹爪去拾取物体
### 1 环境搭建
- **使用环境:** Windows 10 或者 Windows 11、Python3+OpenCV+pymycobot、Git
- **适用设备:** ultraArm P340
#### **1.1 安装Python+pymycobot**
pymycobot 是一个和 myCobot 进行串口通讯的 Python 包,支持 Python2、Python3.5 及之后版本。
在使用 pymycobot控制机械臂之前需要搭建Python环境,具体搭建过程请查看 [Pyhton 环境搭建](../../3-HowToUseultraArm/2-SoftwareControl/4-Python/1-InstallingthePythonEnvironment.md) 章节。
#### **1.2 安装OpenCV**
OpenCV-Python是基于Python的库,旨在解决计算机视觉问题。安装之前需确保Python环境已完成搭建。
打开一个控制台终端(快捷键Win+R,输入cmd进入终端),输入以下命令:
```bash
# 二者版本号需保持一致,这里安装4.5.5.62版本
>>>>>>> 482c782c42e70f0b2e44445345f6461e795f9189
pip install opencv-python==4.5.5.62
pip install opencv-contrib-python==4.5.5.62
<<<<<<< HEAD
After the installation is successful, you can view the specific installed version and installation location through the following command:
# You can also view other python packages Format:pip show 'package name'
=======
<img src =../Resource/3-VisionPickingKit/opencv安装1.png
width ="500" align = "center">
<img src =../Resource/3-VisionPickingKit/opencv安装2.png
width ="500" align = "center">
安装成功后,可通过下面的命令查看具体安装的版本以及安装位置:
```bash
# 也可查看其他python库 格式:pip show 库名称
>>>>>>> 482c782c42e70f0b2e44445345f6461e795f9189
pip show opencv-python
pip show opencv-contrib-python
<<<<<<< HEAD
1.3 Install Git
Git is an open source distributed version control system, which is used to effectively and high-speed process version management of projects from very small to very large.
Git official download address: https://gitforwindows.org/
Click
Download
option,start downloading Git
- Double-click the downloaded exe file to install
- Select the installation location. It is under the C disk by default. If you do not want to place it under the C disk, you can change the location.Click next after selecting the installation location.
- The icon will not be created on the desktop by default,and it added a new feature,put GItBash to Windows terminal,which is recommended to check
- Next, by default, just select Next
- Select a default editor,default is Vim(Linux),there is also Notepad,I choose to use Vim
- Default name (master)after creating a branch in Git,If there is no special default setting, click next
- Next, by default, just select Next
- Next, by default, just select Next
- Next, by default, just select Next
- Next, by default, just select Next
- Next, by default, just select Next
- Next, by default, just select Next
- Next, by default, just select Next
- Next, by default, just select Next
- Additional configuration options,none of them are checked by default,You can check these two boxes,support(node、python,although there are still bugs)Then select install.
- After the installation, click FInish to complete the installation。
Inspect
Open a console terminal(shotcut Win+R,type cmd to enter terminal),enter the following command to check Git version:
1.3 安装Git
Git是一个开源的分布式版本控制系统,用以有效、高速的处理从很小到非常大的项目版本管理。
Git官方下载地址: https://gitforwindows.org/
点击
Download
选项,开始下载Git
- 双击下载好的exe文件进行安装
- 选择安装位置,默认是在C盘下,如果不想放在C盘下可以进行位置更换。选择好安装位置以后点击next。
- 默认不会在桌面创建图标,并且增加了一个新的特性,将GItBash添加到Windows terminal中,建议勾选。
- 下一步默认,选择Next即可。
- 选择一个默认的编辑器,默认为Vim(Linux的),里面还有Notepad,我选择使用Vim
- 在Git创建分支后的默认的名字(master),如果没有特别的使用默认的设置,点击next即可。
- 下一步默认,选择Next即可。
- 下一步默认,选择Next即可。
- 下一步默认,选择Next即可。
- 下一步默认,选择Next即可。
- 下一步默认,选择Next即可。
- 下一步默认,选择Next即可。
- 下一步默认,选择Next即可。
- 下一步默认,选择Next即可。
- 额外的配置选项,这里面默认都没有选,可以将这两个选框勾选上,支持(node、python虽然还有bug)然后选择install。
- 安装完成后,点击FInish安装完成。
检查
打开一个控制台终端(快捷键Win+R,输入cmd进入终端),输入以下命令查看Git版本信息:
482c782c42e70f0b2e44445345f6461e795f9189
git --version
<<<<<<< HEAD
Git is required for subsequent downloading of aikit package,The use of git can refer to the following link:
在后续下载 aikit 包需要用到git,git 的使用可以参考下面链接:
482c782c42e70f0b2e44445345f6461e795f9189
<<<<<<< HEAD
1.4 aikit_V2 installation
aikit_V2
was launched by ElephantRobotics,it is compatible with the AI package V2.0 of its desktop manipulator。
Project address:https://github.com/elephantrobotics/aikit_V2
- 1 Double-click the desktop
Git Bash
icon to enter the Git terminal。
* 2 Enter the following command at Git terminal:
1.4 aikit_V2 安装
aikit_V2
是 ElephantRobotics 推出的,适配旗下桌面机械臂的人工智能套装V2.0 程序包。
项目地址:https://github.com/elephantrobotics/aikit_V2
- 1 双击桌面
Git Bash
图标,进入Git终端。
- 2 在Git终端输入以下命令:
482c782c42e70f0b2e44445345f6461e795f9189
cd Desktop
git clone https://github.com/elephantrobotics/aikit_V2.git
<<<<<<< HEAD
Then aikit_V2
folder will appear on the desktop。
So far, the Windows environment has been built
How to use Python to open the camera
First, you need to use python to run OpenVideo.py under aikit_V2 package.If the enabled camera is a computer camera, you need to modify cap_num.For details, please refer to:Note。Ensure that the camera completely covers the entire recognition area, and that the recognition area is square in the video,as shown in the figure below。If the recognition area does not meet the requirements in the video, the position of the camera needs to be adjusted.
* Open a console terminal(shotcut Win+R,type cmd to enter terminal),enter the following command to enter the target folder
然后桌面会出现 aikit_V2
文件夹。
至此,Windows环境搭建完成。
2 如何使用Python
2.1 如何调整摄像头
首先,需要使用python运行aikit_V2包下的OpenVideo.py。若开启的摄像头为电脑摄像头则需要修改cap_num,具体可参考:注意事项。确保摄像头完全包含整个识别区域,且识别区域在视频中是正正方方的,如下图所示。若识别区域在视频中不符合要求,则需要调整摄像头的位置。
- 打开一个控制台终端(快捷键Win+R,输入cmd进入终端),输入以下命令进入目标文件夹
482c782c42e70f0b2e44445345f6461e795f9189
cd Desktop/aikit_V2/AiKit_ultraArm_P340/
<<<<<<< HEAD
* Enter the following command to open the camera for adjustment
- 输入以下命令,打开摄像头进行调整
482c782c42e70f0b2e44445345f6461e795f9189
python scripts/OpenVideo.py
<<<<<<< HEAD
1 How to use visual module to locate objects
- Before starting the recognition procedure, the camera needs to be calibrated to ensure that the camera completely covers the entire recognition area and that the recognition area is square in the video
- After starting the recognition program, place the recognizable object in the recognition area, and the program will locate the object through the recognition function provided by OpenCV
2 How to recognize object shape
- Open a console terminal(shotcut Win+R,type cmd to enter terminal),enter the following command to enter the target folder
2.2 如何使用视觉模块定位物体
- 启动识别程序前,需要校准摄像头,确保摄像头完全包含整个识别区域,且识别区域在视频中是正正方方的
- 启动识别程序后,将可识别物体放置识别区域中,程序将通过OpenCV自带的识别函数对物体进行定位。
2.3 如何进行物体形状识别
- 打开一个控制台终端(快捷键Win+R,输入cmd进入终端),输入以下命令进入目标文件夹:
482c782c42e70f0b2e44445345f6461e795f9189
cd Desktop/aikit_V2/AiKit_ultraArm_P340/
<<<<<<< HEAD
- Then enter the following command to start the shape recognition program.
- 再输入以下命令,启动形状识别程序。
482c782c42e70f0b2e44445345f6461e795f9189
python scripts/aikit_shape.py
<<<<<<< HEAD
- When the command terminal appears ok and the camera window can be opened normally, it indicates that the program runs successfully. At this time, the recognizable object can be placed in the recognition area for recognition and grabbing.
3 How to carry out color recognition
- Open a console terminal(shotcut Win+R,type cmd to enter terminal),enter the following command to enter the target folder
- 当命令终端出现ok并且摄像头窗口能正常开启时,说明程序运行成功,此时可将可识别的物体放置识别区域进行识别抓取。
2.4 如何进行颜色识别
- 打开一个控制台终端(快捷键Win+R,输入cmd进入终端),输入以下命令进入目标文件夹:
482c782c42e70f0b2e44445345f6461e795f9189
cd Desktop/aikit_V2/AiKit_ultraArm_P340/
<<<<<<< HEAD
- Then enter the following command to start the shape recognition program
- 再输入以下命令,启动形状识别程序。
482c782c42e70f0b2e44445345f6461e795f9189
python scripts/aikit_color.py
<<<<<<< HEAD
- When the command terminal appears ok and the camera window can be opened normally, it indicates that the program runs successfully. At this time, the recognizable object can be placed in the recognition area for recognition and grabbing.
4 How to add a new image of feature points
* Open a console terminal(shotcut Ctrl+Alt+T),enter the target folder
- 当命令终端出现ok并且摄像头窗口能正常开启时,说明程序运行成功,此时可将可识别的物体放置识别区域进行识别抓取。
2.5 如何添加特征点新图像
- 打开一个控制台终端(快捷键Ctrl+Alt+T),进入目标文件夹
482c782c42e70f0b2e44445345f6461e795f9189
cd ~/aikit_V2/AiKit_ultraArm_P340/
<<<<<<< HEAD
* Enter the following command to start the add image program
- 输入以下命令,启动添加图像的程序。
482c782c42e70f0b2e44445345f6461e795f9189
python scripts/add_img.py
<<<<<<< HEAD
- Operate according to the prompt input by the terminal, and intercept the image in the second image box that pops up.
- After the image capture area is completed, press the Enter key. According to the terminal prompt, enter numbers (1~4) to save to the corresponding image folder, and press the Enter key to save to the corresponding folder.
5 How to recognize feature point image
- Open a console terminal(shotcut Win+R,type cmd to enter terminal),enter the following command to enter the target folder
- 根据终端输入的提示进行操作,在弹出的第二个图像框中进行图像的截取。
- 图像截取区域完成后,按下回车键,根据终端提示,输入数字(1~4)保存到相对应图像的文件夹,按下回车键即可保存至对应文件夹。
2.6 如何进行特征点图像识别
- 打开一个控制台终端(快捷键Win+R,输入cmd进入终端),输入以下命令进入目标文件夹:
482c782c42e70f0b2e44445345f6461e795f9189
cd Desktop/aikit_V2/AiKit_ultraArm_P340/
<<<<<<< HEAD
- Then enter the following command to start the shape recognition program
=======
- 再输入以下命令,启动形状识别程序。
482c782c42e70f0b2e44445345f6461e795f9189
python scripts/aikit_img.py
<<<<<<< HEAD
- When the command terminal appears ok and the camera window can be opened normally, it indicates that the program runs successfully. At this time, the recognizable object can be placed in the recognition area for recognition and grabbing.
6 How to carry out QR code recognition
- Open a console terminal(shotcut Win+R,type cmd to enter terminal),enter the following command to enter the target folder
- 当命令终端出现ok并且摄像头窗口能正常开启时,说明程序运行成功,此时可将可识别的物体放置识别区域进行识别抓取。
2.7 如何进行二维码识别
- 打开一个控制台终端(快捷键Win+R,输入cmd进入终端),输入以下命令进入目标文件夹:
482c782c42e70f0b2e44445345f6461e795f9189
cd Desktop/aikit_V2/AiKit_ultraArm_P340/
<<<<<<< HEAD
- Then enter the following command to start the shape recognition program
- 再输入以下命令,启动形状识别程序。
482c782c42e70f0b2e44445345f6461e795f9189
python scripts/aikit_encode.py
<<<<<<< HEAD
- When the command terminal displays coordinate information and the camera window can be opened normally,it indicates that the program runs successfully. At this time, the recognizable object can be placed in the recognition area for recognition and grabbing.
7 How to convert the positioned object to the coordinates recognized by the manipulator
- The pixel distance M1 between two aruco can be calculated by measuring the pixel points of two aruco in the captured area,Measure the actual distance M2 of two aruco, so that we can get the ratio of pixel to actual distance(ratio = M2 / M1).We can calculate the pixel difference between the object block and the center of the grabbing area from the picture, so we can calculate the relative coordinates (x1, y1) of the object block from the center of the grabbing area.Add the relative coordinates (x1, y1) from the center of the grabbing area to the relative coordinates (x2, y2) of the manipulator to obtain the relative coordinates (x3, y3) of the block to the manipulator.
# Grab the coordinate of the center point relative to the manipulator
self.camera_x, self.camera_y = camera_x, camera_y
# The coordinates of the object relative to the manipulator,the parameters can be set as:
self.c_x = self.c_y = 0, 0
# Pixel to actual ratio:
self.ratio = 0
# Set the parameter function to calculate the coordinates between the cube and the manipulator
=======
<img src =../Resource/3-VisionPickingKit/ultraArm_encode.png
width ="500" align = "center">
- 当命令终端出现坐标信息并且摄像头窗口能正常开启时,说明程序运行成功,此时可将可识别的物体放置识别区域进行识别抓取。
#### 2.8 如何转换定位的物体为机械臂识别的坐标
- 通过测量抓取区域中的两个aruco的像素点位可计算出两个aruco的之间的像素距离M1,测量两个aruco的实际距离M2,这样我们就能获得像素与实际距离的比值 ratio = M2 / M1。我们可以从图片中计算出物块距离抓取区域中心的像素差,这样我们可以计算出物块实际距离抓取区域中心的相对坐标(x1, y1)。将相对坐标(x1, y1)加上抓取区域中心到机械臂的相对坐标(x2, y2),即可求得物块对于机械臂的相对坐标(x3, y3)。
```bash
# 抓取中心点相对于 机械臂 的坐标
self.camera_x, self.camera_y = camera_x, camera_y
# 物体相对于机械臂的坐标,可以设置参数为:
self.c_x = self.c_y = 0, 0
# 像素与实际比值为:
self.ratio = 0
# 设置参数函数用于计算立方体和 机械臂 之间的坐标
>>>>>>> 482c782c42e70f0b2e44445345f6461e795f9189
def set_params(self, c_x, c_y, ratio):
self.c_x = c_x
self.c_y = c_y
self.ratio = 220.0/ratio
<<<<<<< HEAD
# Obtain the position function, which is used to calculate the coordinates between the cube and the manipulator
def get_position(self, x, y):
return ((y - self.c_y)*self.ratio + self.camera_x), ((x - self.c_x)*self.ratio + self.camera_y)
# Next, the program will call the parameter setting function set_params(), used to calculate and set the real coordinates parameters between the object and the manipulator
=======
# 获取位置函数,用于计算立方体和 机械臂 之间的坐标
def get_position(self, x, y):
return ((y - self.c_y)*self.ratio + self.camera_x), ((x - self.c_x)*self.ratio + self.camera_y)
# 接下来,程序会调用设置参数函数set_params(),用于计算和设置物体和机械臂之间真实坐标的参数
>>>>>>> 482c782c42e70f0b2e44445345f6461e795f9189
detect.set_params(
(detect.sum_x1+detect.sum_x2)/20.0,
(detect.sum_y1+detect.sum_y2)/20.0,
abs(detect.sum_x1-detect.sum_x2)/10.0 +
abs(detect.sum_y1-detect.sum_y2)/10.0
)
<<<<<<< HEAD
# Then the function get_position() will be called ,used to calculate the real coordinates between the cube and the mycobot
real_x, real_y = detect.get_position(x, y)
# Finally, the real coordinates obtained will be slightly processed and passed into the decide_move() function for the use of the subsequent manipulator grabbing.
detect.decide_move(real_sx/20.0, real_sy/20.0, detect.color)
8 How to execute the converted coordinates
- The real coordinates obtained will be slightly processed and passed into the decide_move() function for the use of the subsequent manipulator grabbing.
然后将调用函数get_position() 用于 计算立方体和 mycobot 之间的真实坐标
real_x, real_y = detect.get_position(x, y)
最终将获取的真实坐标经过稍微处理,将其传入decide_move()函数,供后续机械臂抓取使用。
detect.decide_move(real_sx/20.0, real_sy/20.0, detect.color)
#### 2.9 如何执行转换后的坐标
- 程序将最终获取的真实坐标经过稍微处理,将其传入 **decide_move()** 函数,供后续机械臂抓取使用
>>>>>>> 482c782c42e70f0b2e44445345f6461e795f9189
```bash
detect.decide_move(real_sx/20.0, real_sy/20.0, detect.color)
<<<<<<< HEAD
- In the decide_move() function, the move() function will continue to be called to trigger the mobile grab of the manipulator
# Decide whether grab cube
def decide_move(self, x, y, color):
# Output and print plane coordinates of real objects
print(x, y, self.cache_x, self.cache_y)
# Detect the movement or operation of block state
=======
- 在decide_move()函数中,则会继续调用 **move()** 函数,用于触发机械臂的移动抓取
```bash
# 决定是否抓取物体 decide whether grab cube
def decide_move(self, x, y, color):
# 输出打印真实物体的平面坐标
print(x, y, self.cache_x, self.cache_y)
# 检测物块状态移动或运行
>>>>>>> 482c782c42e70f0b2e44445345f6461e795f9189
if (abs(x - self.cache_x) + abs(y - self.cache_y)) / 2 > 5: # mm
self.cache_x, self.cache_y = x, y
return
else:
self.cache_x = self.cache_y = 0
<<<<<<< HEAD
# Adjust the suction position of the suction pump,Y increases and moves to the left; Y decreases and moves to the right;X increases and moves forward; X decreases and moves backwards
# Call the move function to make the manipulator perform a series of grabbing actions
self.move(x, y, color)
- When the mechanical arm performs the grab movement operation, it indicates that the program executes the move() function
# 调整吸泵吸取位置,y增大,向左移动;y减小,向右移动;x增大,前方移动;x减小,向后方移动
# 调用move函数,使机械臂进行一系列抓取动作
self.move(x, y, color)
- 当机械臂进行抓取移动操作时,说明程序执行的是 **move()** 函数
>>>>>>> 482c782c42e70f0b2e44445345f6461e795f9189
```bash
def move(self, x, y, color):
print(color)
<<<<<<< HEAD
# Send the angle to move the manipulator at a speed of 50
self.ua.set_angles(self.move_angles[2], 50)
# Wait 3 seconds
time.sleep(3)
# Send coordinates to move the manipulator so that the end of the manipulator reaches directly above the block at a speed of 50
self.ua.set_coords([x, -y, 65.51], 50)
# Wait 1.5 seconds
time.sleep(1.5)
# Send coordinates to move the manipulator so that the end of the manipulator reaches the upper surface of the block at a speed of 50
self.ua.set_coords([x, -y, -32], 50)
# Wait 2 seconds
=======
# 发送角度去移动机械臂,速度为50
self.ua.set_angles(self.move_angles[2], 50)
# 等待3秒
time.sleep(3)
# 发送坐标去移动机械臂,使机械臂末端到达物块的正上方,速度为50
self.ua.set_coords([x, -y, 65.51], 50)
# 等待1.5秒
time.sleep(1.5)
# 发送坐标去移动机械臂,使机械臂末端到达物块的上表面,速度为50
self.ua.set_coords([x, -y, -32], 50)
# 等待2秒
>>>>>>> 482c782c42e70f0b2e44445345f6461e795f9189
time.sleep(2)
<<<<<<< HEAD
9 How to control the suction pump or gripper to pick up objects
- When the suction pump at the end of the mechanical arm reaches the top of the object and contacts the object, call the suction function and suck or release the object
# Start the suction pump
def pump_on(self):
self.ua.set_gpio_state(0)
# Stop the suction pump
=======
#### 2.10 如何控制吸泵/夹爪去拾取物体
- 当机械臂末端吸泵到达物体上方并接触物体时,调用吸泵函数并吸取或者释放物体
```bash
# 开启吸泵
def pump_on(self):
self.ua.set_gpio_state(0)
# 停止吸泵
>>>>>>> 482c782c42e70f0b2e44445345f6461e795f9189
def pump_off(self):
self.ua.set_gpio_state(1)
<<<<<<< HEAD
- Start the suction pump
- 开启吸泵
482c782c42e70f0b2e44445345f6461e795f9189
# open pump
self.pump_on()
<<<<<<< HEAD
- Close the suction pump
- 关闭吸泵
482c782c42e70f0b2e44445345f6461e795f9189
# open pump
self.pump_off()