dev
Python 快速上手
用 ctypes 调 vmc.dll,20 行跑完一条标定链。
最小例子
import ctypes as C
import json
vmc = C.CDLL(r"C:\Program Files\PreciSim\vmc.dll")
vmc.vmc_open.restype = C.c_void_p
vmc.vmc_open.argtypes = [C.c_char_p]
vmc.vmc_axis_move_abs.argtypes = [C.c_void_p, C.c_char_p, C.c_double, C.c_int]
vmc.vmc_axis_position.argtypes = [C.c_void_p, C.c_char_p, C.POINTER(C.c_double)]
vmc.vmc_calib_run.argtypes = [C.c_void_p, C.c_char_p, C.c_char_p, C.c_char_p, C.c_int]
h = vmc.vmc_open(rb"C:\ProgramData\PreciSim\machines\demo-2axis-vision.json")
assert h, "打开机台失败"
# 点动
vmc.vmc_axis_move_abs(h, b"axisX", 120.5, -1) # -1 = 等到位
pos = C.c_double()
vmc.vmc_axis_position(h, b"axisX", C.byref(pos))
print(f"X = {pos.value:.4f} mm")
# 跑一条标定链
buf = C.create_string_buffer(8192)
rc = vmc.vmc_calib_run(h, b"C1-pixel-size", b"{}", buf, len(buf))
assert rc == 0, f"标定失败 rc={rc}"
result = json.loads(buf.value.decode("utf-8"))
print(json.dumps(result["verify"], indent=2, ensure_ascii=False))
vmc.vmc_close(h)输出:
{
"deviationMm": 0.018,
"toleranceMm": 0.05,
"pass": true
}抓一帧到 numpy
import numpy as np
vmc.vmc_cam_grab.argtypes = [
C.c_void_p, C.c_char_p, C.POINTER(C.c_ubyte), C.c_int,
C.POINTER(C.c_int), C.POINTER(C.c_int), C.POINTER(C.c_int),
]
buf = (C.c_ubyte * (4096 * 4096))()
w, hgt, stride = C.c_int(), C.c_int(), C.c_int()
vmc.vmc_cam_grab(h, b"cam0", buf, len(buf), C.byref(w), C.byref(hgt), C.byref(stride))
img = np.ctypeslib.as_array(buf)[: stride.value * hgt.value]
img = img.reshape(hgt.value, stride.value)[:, : w.value] # 去掉行对齐的填充在 CI 里跑回归测试
这是 vmc.dll 最有价值的用法:每次改标定算法,自动验证精度没有退化。
import pytest
CASES = [
("C1-pixel-size", 0.05),
("C2-hand-eye", 0.03),
("C3-intrinsics", 0.15),
]
@pytest.mark.parametrize("proc,tol", CASES)
def test_calibration_within_tolerance(vmc_handle, proc, tol):
result = run_calib(vmc_handle, proc)
assert result["verify"]["pass"], result["verify"]
assert result["verify"]["deviationMm"] <= tol配合误差注入,还能测「异常情况下程序会不会正确报错」:
def test_large_distortion_is_detected(vmc_handle):
set_fault(vmc_handle, "cam0", {"k1": -0.08})
result = run_calib(vmc_handle, "C1-pixel-size")
# 期望:程序应该发现超差并 FAIL,而不是悄悄通过
assert not result["verify"]["pass"]第二个测试比第一个更重要。 大多数事故不是「算错了」,是「算错了但没人发现」。
最后更新: 2026/9/21
这页有帮助吗?