使用Python计算结果
功能描述
该步骤可通过Python运行用户自定义的脚本,并将计算结果输出到Mech-MSR。
该步骤特点如下:
-
支持多线程使用;
-
可实时加载Python脚本;
-
在C++和Python端互相传输数据时,支持多种数据类型的转化;
-
支持将Python端的日志重定向到Mech-MSR中。
安装和使用
安装方法
Mech-MSR中内置了Python 3.9.13 和 NumPy 、OpenCV 两个常用的 Python 库,“使用Python计算结果”步骤运行时将使用软件内置的 Python 环境。若在使用过程中出现缺少 Python 库的情况,需要将所缺少的 Python 库安装在Mech-MSR内置的Python环境中。安装方法如下:
-
进入 Mech-MSR 的 Python 安装目录。例如:C:\App\Mech-MSR-2.1.1\python;
-
在当前目录下,按住Shift键,单击鼠标右键,然后从右键菜单中选择在此处打开 Powershell 窗口选项;
-
在 Windows PowerShell 窗口中,执行
python -m pip install xxx
命令(xxx
为 Python 库的名称),下载安装对应的 Python 库。
因网络问题导致 Python 库安装失败时,可查看常见问题Python 库安装失败怎么办?进行解决。 |
使用方法
准备好Python脚本后,该步骤的使用方法如下(关于各参数的解释可参考 参数说明 ):
-
设置输入/输出端口数据类型。根据前后步骤的输入/输出端口数据类型,或根据实际需求,填写输入端口和输出端口的数据类型;
-
设置Python脚本路径。在脚本路径处选择需要加载的脚本的路径;
-
设置调用函数的名称。当选择脚本路径后,该步骤将自动获取该脚本内的函数名称,然后需在调用函数名称处的下拉栏中选择需要调用的脚本函数名称;
-
运行步骤。
编写Python脚本时,直接编写用于处理数据的函数即可,无需在脚本内编写获取Mech-MSR数据的语句。 |
使用注意事项
编写并使用该步骤运行Python脚本时的注意事项如下。
注意使用NumPy库
在Python支持的数据类型中,其中比较复杂的类型是通过NumPy库来作为中间格式的。若存在某个参数类型是NumPy的数组类型,但却没有导入NumPy,则会发生报错。所以此时需要脚本开头添加import numpy。
仅支持NumPy版本在1.19.3(含)至2.0.0(不含)之间,推荐使用1.22.4。使用2.0.0及以上版本可能会引发兼容性问题,导致该步骤运行异常。 |
导入预定义模块
当该步骤的输入或输出端口类型为 Surface、Profile 或 Shape3D 时,必须导入预定义模块。 |
在 Mech-MSR 软件中使用 Python 脚本处理 Surface、Profile、Shape3D 类型的数据时,应导入相应的预定义模块,以便在运行 Python 脚本时,Python 能够识别并操作这些数据结构。如果未导入相应的模块,脚本将无法识别这些类,运行时会出现错误。
你可以在软件安装目录下(例如:C:\Mech-MSR-2.1.1\python\Lib\py_module)找到这些预定义模块。预定义模块可用于处理以下三种数据:
-
表面数据
-
描述:表示对物体表面的扫描数据,通常包括深度图和强度图。
-
端口类型:Surface
-
需导入 Python 脚本的模块:
mmind_surface
使用说明
from mmind_surface import * # must be imported def test_surface(surface): print(surface) # is a class print(type(surface)) # is a class print(surface.depth) # is numpy.ndarray (n, m) print(surface.intensity) # is numpy.ndarray (n, m) print(surface.xResolution) print(surface.yResolution) print(surface.xOffset) print(surface.yOffset) print(surface.zOffset) return surface def test_surfaces(surfaces): print(type(surfaces)) # is list for surface in surfaces: print(type(surface)) # is class print(surface.depth) print(surface.intensity) print(surface.xResolution) print(surface.yResolution) print(surface.xOffset) print(surface.yOffset) print(surface.zOffset) return surfaces
-
-
轮廓线数据
-
描述:表示轮廓线数据。轮廓线由点构成,包含各点的X、Z值,反映物体一个截面高度的变化。
-
端口类型:Profile
-
需导入 Python 脚本的模块:
mmind_profile
使用说明
from mmind_profile import * # must be imported def test_surface(profile): print(profile) # is a class print(type(profile)) # is a class print(profile.points) # is numpy.ndarray (n,2) print(profile.yResolution) print(profile.xOffset) return profile def test_surfaces(profiles): print(type(profiles)) # is list for profile in profiles: print(type(profile)) # is a class print(profile.points) # is numpy.ndarray (n,2) print(profile.yResolution) print(profile.xOffset) return profiles
-
-
几何特征
-
描述:表示点、直线、平面、圆等几何特征。
-
端口类型:Shape3D
-
需导入 Python 脚本的模块:
mmind_feature
和mmind_util
使用说明(以Shape3D []为例)
from mmind_util import * # must be imported from mmind_feature import * # must be imported def test_shape3dList(point_list, line_list, plane_list, circle_list): point = point_list[0] # class PyPoint3D line = line_list[0] # class PyLine3D plane = plane_list[0] # class PyPlane3D circle = circle_list[0] # class PyCircle3D p_x = point.x # number p_y = point.y p_z = point.z l_p = line.point # class PyVector3D l_d = line.direction # class PyVector3D print(l_d.x, l_d.y, l_d.z) p_a = plane.a # number p_b = plane.b p_c = plane.c p_d = plane.d c_p = circle.point # class PyVector3D c_r = circle.radius # number c_n = circle.normal # class PyVector3D def create_shape3dList(): point1 = PyPoint3D(1.0, 2.0, 3.0) point2 = PyPoint3D() point2.x = 4.0 # must be a number point2.y = 5.0 point2.z = 6.0 line1 = PyLine3D(PyVector3D(1.0, 1.0, 1.0), PyVector3D(1.0, 1.0, 0.0)) line2 = PyLine3D() line2.point = PyVector3D(1.1, 1.2, 1.3) # can be PyVector3D or PyPoint3D line2.direction.x = 0.0 # must be a number line2.direction.y = 1.0 line2.direction.z = 1.0 plane1 = PyPlane3D(2.0, 0.0, 0.0, 10.0) plane2 = PyPlane3D() plane2.a = 0.0056 # must be a number plane2.b = 0.001 plane2.c = 0.0023 plane2.d = -3.22 circle1 = PyCircle3D(point1, 10.0, PyVector3D(0.0, 0.0, 1.0)) circle2 = PyCircle3D() circle2.point = point2 # can be PyVector3D or PyPoint3D circle2.radius = 100.0 # must be a number circle2.normal = PyVector3D(0.0, 1.0, 0.0) # must be unit vector return [point1, point2], [line1, line2], [plane1, plane2], [circle1, circle2]
-
参数说明
- 输入端口
-
参数解释:该参数用于设置该步骤输入端口的数据类型,输入的数据类型将会变为对应顺序的参数传入所调用的函数。
默认值:空。
- 输出端口
-
参数解释:该参数用于设置该步骤输出端口的数据类型,函数返回的各数据会按照对应顺序返回给该步骤,并且会按照对应数据类型进行解析。
默认值:空。
目前支持的数据类型如下表所示:
在基础数据类型后添加[] 可用于增加数据维度。请根据实际情况正确填写输入或输出端口的数据类型。
|
基础数据类型 | Python中的数据类型 | 输入/输出端口字段 | 示例数据 |
---|---|---|---|
Pose (自定义类型) |
|
|
[10, 20, 30, 0.951, 0.255, 0.168, 0.045] (前三个数值分别表示 X、Y、Z 坐标,后四个数值表示旋转信息,采用四元数,顺序为[w, x, y, z]。) |
|
[[10, 20, 30, 0.951, 0.255, 0.168, 0.045], [10, 20, 30, 0.951, 0.255, 0.168, 0.045]] |
||
Pose2D (自定义类型) |
|
|
[2, 0, 120] (前两个数值分别表示X、Y 坐标,第三个数值表示角度。) |
|
[[0, 0, 0], [2, 0, 120]] |
||
Number |
|
|
233 |
|
[1.1, 2, 999.9, -22] |
||
String |
|
|
'string_1' |
|
['string_1', 'string_2', 'string_3'] |
||
Index |
|
|
1 |
|
[45, 10, 90] |
||
Bool |
|
|
True |
|
[True, False, True] |
||
Surface (自定义类型) |
查看预定义模块 |
|
(表面数据,通常包含深度图和强度图) |
Profile (自定义类型) |
查看预定义模块 |
|
(轮廓线数据) |
Shape3D (自定义类型) |
查看预定义模块 |
|
PyPoint3D()、PyLine3D()、PyPlane3D()、PyCircle3D() (PyPoint3D、PyLine3D、PyPlane3D、PyCircle3D为自定义类型,分别表示点、直线、平面和圆这几种几何特征。) |
|
[PyPoint3D(), PyPoint3D(), …] [PyLine3D(), PyLine3D(), …] [PyPlane3D(), PyPlane3D(), …] [PyCircle3D(), PyCircle3D(), …] |
||
Image |
|
|
单通道灰度图( |
Cloud(XYZ) (自定义类型) |
|
|
[[0.1, 0.2, 0.3], [0.2, 0.3, 0.4], [0.3, 0.4, 0.5], [0.4, 0.5, 0.6], [0.5, 0.6, 0.7]] (每组数据中,三个数值分别表示点的 X、Y、Z 坐标。) |
Cloud(XYZ-Normal) (自定义类型) |
|
|
[[0.1, 0.2, 0.3, 0.4, 0.5, 0.6], [0.2, 0.3, 0.4, 0.5, 0.6, 0.7], [0.3, 0.4, 0.5, 0.6, 0.7, 0.8], [0.4, 0.5, 0.6, 0.7, 0.8, 0.9], [0.5, 0.6, 0.7, 0.8, 0.9, 1.0]] (每组数据中,前三个数值分别表示点的 X、Y、Z 坐标,后三个数值分别表示法向在 X、Y、Z 轴上的分量。) |
Cloud(XYZ-RGB) (自定义类型) |
|
|
[[0.1, 0.2, 0.3, 52, 64, 13], [0.2, 0.3, 0.4, 45, 21, 72], [0.3, 0.4, 0.5, 17, 69, 13], [0.4, 0.5, 0.6, 12, 8, 37], [0.5, 0.6, 0.7, 25, 58, 46]] (每组数据中,前三个数值分别表示点的 X、Y、Z 坐标,后三个数值分别表示点的红色分量、绿色分量、蓝色分量。) |
Size3D (自定义类型) |
|
|
[2.5, 5, 0.001] (分别表示X、Y、Z方向上的尺寸。) |
|
[[2.5, 5, 0.001], [6, 5, 0.02]] |
- 脚本路径
-
参数解释:该参数用于选择所需要加载的脚本的文件路径。
- 调用函数名称
-
参数解释:该参数用于设置被调用的脚本函数名称。
Python 例程
参考步骤使用方法了解如何在该步骤中调用函数。 |
读取测量项输出中的数据
以下例程展示了如何读取测量项输出中包含的信息,例如测量值、判定结果(OK或NG)、校正补偿值,以及为该测量项设定的上下限(最小值和最大值)。
import mmind_measure
# Or use wildcard import
# from mmind_measure import *
def test(numberList, stringList, boolList):
entries = mmind_measure.measurement_entries_at(1) # Calls the function with the port number as the argument.
# If wildcard import is used (e.g., from mmind_measure import *), the function can be called directly:
# entries = measurement_entries_at(1)
print(entries)
for entry in entries:
print(entry.observeValue) # Measured value(s) (float, str, bool)
print(entry.judgement) # Judgment result, OK or NG (str)
print(entry.offset) # Corrective compensation value (float)
print(entry.lowerLimit) # The set Min for the measurement item (float)
print(entry.upperLimit) # The set Max for the measurement item (float)
print(entry.compensation) # Compensated value(s) (float)
print(entry.entryName) # Port name (str)
错误排查
查看错误码列表了解通用的错误信息及解决方案。