其他接口

本节介绍使用 Adapter 的其他接口,具体包含以下接口:

通知服务

在 Mech-Viz 工程运行到某个分支或者某一步时,若希望调用 Adapter 程序相应的函数,则可以在 Mech-Viz 中添加一个通知步骤。

示例

例如,在 Adapter 中编写了一个使已拆数量加 1 的函数,那么就可以在拆垛过程的最后一个步骤的后面添加一个通知步骤,当运动到此处时,就可以触发 Adapter 并调用相应函数。实现该功能的示例如下所示。

  1. 编写一个继承 NotifyService 的类。

    from interface.services import NotifyService, register_service
    
    class NotifyService(NotifyService):
        service_type = "notify"
        service_name = "FANUC_M410IC_185_COMPACT"
    
        def __init__(self, update_success_num, update_fail_num):
            self.update_success_num = update_success_num
            self.update_fail_num = update_fail_num
    
        def handle_message(self, msg):
            if msg == "Success":
                self.update_success_num()
            elif msg == "Fail":
                self.update_fail_num()

    此通知可以实现如下功能:当 Mech-Viz 运行到发送 “Success” 的通知步骤时,可以使 Adapter 调用 update_success_num() 函数,当到 “Fail” 时,Adapter 调用 update_fail_num() 函数。

  2. 在控制 Mech-Viz 主程序的类中对 NotifyService 类进行实例化并注册该服务。

    class MyClient(TcpClientAdapter):
    
         def __init__(self, host_address):
             super().__init__(host_address)
             self._register_service()
    
         def _register_service(self):
             self.robot_service = NotifyService(self.update_success_num, self.update_fail_num)
             self.server, port = register_service(self.hub_caller, self.robot_service)
    
         def update_success_num(self):
             # the num of unstack successfully plus 1
             self.success_num += 1
    
         def update_fail_num(self):
             # the num of unstack fiplus 1
             self.fail_num += 1
  3. 在 Mech-Viz 中需要的位置添加相应的通知步骤。

    在通知步骤中,最主要的是填写 Adapter 服务名称消息 。这两处的值需要和上面 NotifyService 类中的 service_name 和 msg 值一致。

    一旦运行了程序,则在Mech-Vision日志窗口的控制台标签页中出现 service_type 和 service_name,表明注册通知服务成功。

VisionWatcher服务

当 Mech-Vision 运行结束后,会输出一些结果,例如,vision result:{‘noCloudInRoi’: False, ‘function’: ‘posesFound’, ‘vision_name’: ‘TJTvision-3’}。对于一些异常情况,Adapter 可以通过 VisionWatcher 服务发送错误信息进行提醒。

示例

  1. 编写继承 VisionResultSelectedAtService 的类。

    from interface.services import VisionResultSelectedAtService, register_service
    
    class VisionWatcher(VisionResultSelectedAtService):
        def __init__(self, send_err_no_cloud):
            super().__init__()
            self.send_err_no_cloud = send_err_no_cloud
    
        def poses_found(self,result):
            has_cloud_in_roi = not result.get("noCloudInRoi", False)
    
            if not has_cloud_in_roi:
                time.sleep(2)
                self.send_err_no_cloud()

    子类 VisionWatcher 需要重写父类的 poses_found() 函数,所以调用 Adapter 中 send_err_no_cloud() (发送错误信息的函数)的逻辑会在 poses_found() 中发生变化。在运行过程中,Mech-Vision 返回视觉点的值会传递给 poses_found() 中的 result 参数。

  2. 在控制 Mech-Viz 主程序的类中实例化 VisionWatcher。

    class MyClient(TcpClientAdapter):
        def __init__(self, host_address):
            super().__init__(host_address)
            self._register_service()
        def _register_service(self):
            self.robot_service = VisionWatcher(self.send_err_no_cloud)
            self.server, port = register_service(self.hub_caller, self.robot_service)
    
        def send_err_no_cloud(self):
            # send no cloud error message
            self.send("12,NoCloudErr,done".encode())

    在进行 VisionWatcher 类实列化的过程中,将 send_err_no_cloud() 函数作为参数传递给 VisionWatcher()。当无点云出现时,按照 poses_found() 中的逻辑,则会调用发送错误信息的函数。

    一旦程序运行以后,在Mech-Vision日志窗口的控制台标签页中出现已注册的服务,则表明 Adapter 注册 VisionWatcher 服务成功。

我们重视您的隐私

我们使用 cookie 为您在我们的网站上提供最佳体验。继续使用该网站即表示您同意使用 cookie。如果您拒绝,将使用一个单独的 cookie 来确保您在访问本网站时不会被跟踪或记住。