我使用Zaber Console编写了一个脚本来控制在循环中执行多个步骤的Zaber设备。如何在循环中间暂停它并恢复它而不必回到起点?
答案 0 :(得分:1)
我可以考虑四种选择来支持这一点。
以下是使用单独脚本暂停和恢复例程的示例。它还介绍了如何使用操纵杆按钮。
首先,我创建了这个简单的例程。
/* 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);
暂停和恢复脚本非常简单;真正的工作是让主脚本从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,那么可以恢复您的脚本。上面的最后一个脚本将使用暂停和恢复逻辑运行例程,无论您使用单独的脚本发送暂停和恢复命令,还是使用操纵杆按钮发送它们。