暂停并恢复Zaber Console脚本

时间:2012-08-20 19:38:16

标签: .net zaber

我使用Zaber Console编写了一个脚本来控制在循环中执行多个步骤的Zaber设备。如何在循环中间暂停它并恢复它而不必回到起点?

1 个答案:

答案 0 :(得分:1)

我可以考虑四种选择来支持这一点。

  1. 让你的脚本在启动时以某种方式检测当前状态,并找出它应该从哪个部分开始。根据您的日常工作,您可以阅读current position以告知您的步骤。如果没有,您还可以使用user memory或将当前状态写入文本文件。其中一个示例脚本显示了如何write to a text file
  2. 除主脚本外,还要编写暂停脚本和简历脚本。主脚本注意到其他两个脚本的响应。我将在下面提供一个示例。
  3. 使用Zaber Joystick并编程按钮发送暂停和恢复命令,而不是单独的脚本暂停和恢复。以下示例也将涵盖这一点。
  4. 将您的脚本转换为运行plug in中主程序的BackgroundWorker。然后,您可以在控件类中保持状态,并添加“暂停/恢复”按钮。
  5. 以下是使用单独脚本暂停和恢复例程的示例。它还介绍了如何使用操纵杆按钮。

    The Original Routine

    首先,我创建了这个简单的例程。

    /* C# example of how to pause and resume a Zaber Console script.
     * This script is the original routine without the pause and resume logic.
     * It moves to position zero, then makes four moves of 10000 microsteps each 
     * and goes back to zero. This loops forever.
     */
    #template(simple)
    
    while (true)
    {
        for (var i = 0; i < 5; i++)
        {
            var position = i*10000;
            Output.WriteLine("moving to {0}", position);
            Conversation.Request(Command.MoveAbsolute, position);
        }
    }
    

    暂停

    这是暂停例程的脚本。

    /* C# example of how to pause and resume a Zaber Console script.
     * This script pauses the main routine by sending a Stop command. Running this 
     * script twice will stop the routine.
     */
    #template(simple)
    
    Conversation.Request(Command.Stop);
    

    stop命令将导致主脚本出错,但我们将更改主脚本以捕获该错误并添加暂停/恢复逻辑。

    简历

    这是恢复例程的脚本。

    /* C# example of how to pause and resume a Zaber Console script.
     * This script resumes the main routine by sending an EchoData command with a
     * magic number.
     */
    #template(simple)
    
    const int RESUME_NUMBER = 42;// Matches the main routine.
    Conversation.Request(Command.EchoData, RESUME_NUMBER);
    

    Routine Plus Pause Logic

    暂停和恢复脚本非常简单;真正的工作是让主脚本从stop命令中侦听错误并恢复魔法数字。这是所有添加的例程的新版本。在主脚本编辑器窗口中运行此脚本,并从主窗口的“脚本”选项卡中的网格中运行另外两个脚本。

    /* C# example of how to pause and resume a Zaber Console script.
     * This script is the main routine that moves to position zero, then makes four
     * moves of 10000 microsteps each and goes back to zero. This loops forever.
     * To pause the routine, run the Pause.cs script. To resume the routine, run
     * the Resume.cs script. Running the pause script twice will stop the routine.
     */
    #template(methods)
    
    /* We switched to the methods template so we can put the move with pause and
     * resume into a helper method.
     */
    public override void Run()
    {
        while ( ! IsCanceled)
        {
            for (var i = 0; i < 5 && ! IsCanceled; i++)
            {
                var position = i*10000;
                MoveTo(position);
            }
        }
    }
    
    /* This wraps the pause and resume logic around a simple MoveAbsolute command.
     * If your resume logic is more complicated, you can put more commands inside 
     * the try/catch block, or use the return value of this function to tell the
     * main routine what to do next.
     * When this method returns, either the move has successfully completed, or
     * IsCanceled is true.
     */
    private void MoveTo(int position)
    {
        bool isComplete = false;
        while ( ! isComplete && ! IsCanceled)
        {
            try
            {
                Output.WriteLine("moving to {0}", position);
                Conversation.Request(Command.MoveAbsolute, position);
                isComplete = true;
            }
            catch (RequestReplacedException ex)
            {
                Pause();
            }
            catch (RequestCollectionException ex)
            {
                /* If you are running against device number zero
                 * or some other alias, you get a slightly
                 * different exception.
                 */
                Pause();
            }
        }
    }
    
    /* Just wait for responses from your device. If a response is an EchoData 
     * command with the magic number, then this method will return. If the response
     * is a Stop command, then IsCanceled is set to true and this method will 
     * return. All other responses are ignored.
     */
    private void Pause()
    {
        Output.WriteLine("paused");
    
        /* Let the device finish processing the current stop command before
         * you start listening, otherwise you sometimes see the stop command
             * again.
             */
        Sleep(100); 
    
        const int RESUME_NUMBER = 42;// Matches the resume script.
        var listener = new DeviceListener(Conversation.Device);
        bool isPaused = ! IsCanceled;// Don't pause if already canceled.
        while (isPaused)
        {
            var response = listener.NextResponse();// wait
            if (response.Command == Command.EchoData && 
                response.Data == RESUME_NUMBER)
            {
                isPaused = false;
                Output.WriteLine("resumed");
            }
            else if (response.Command == Command.Stop)
            {
                isPaused = false;
                IsCanceled = true;
                Output.WriteLine("stopped");
            }
        }
    }
    

    操纵杆

    如果你有一个Zaber操纵杆,点击几个操纵杆按钮比在Zaber Console中运行暂停和恢复脚本更方便。操纵杆上按钮1的默认命令是停止,因此您已经完成了暂停操作。如果您编写其他按钮之一来发送Echo Data 42,那么可以恢复您的脚本。上面的最后一个脚本将使用暂停和恢复逻辑运行例程,无论您使用单独的脚本发送暂停和恢复命令,还是使用操纵杆按钮发送它们。