通常,如果在else下使用goto语句可以循环吗?

时间:2019-06-14 06:00:08

标签: lua robotics

因此,我要完成的任务是对机器人(AUBO)进行编程,以拾取不同的对象并将它们按特定顺序放置(点A,B,C,D)。 我正在使用一种称为pim60的视觉系统。因此,如果检测到一个物体,它将去拾取,该程序的其余部分是放置产品的航点。第一个问题是我希望它转到下一个航点以放下,第二件事是,直到检测到该放下点的对象之前,才能跳过下一个放下点。

在我自己的代码中,我编写了一个很长的程序,像这样。

::LoopA::
script_common_interface("SICKCamera","takePhoto")
script_common_interface("SICKCamera","getResult")
Located = script_common_interface("SICKCamera","partLocated")
if(Located == 1) then
.
.
.
Drop at position A
else 
goto LoopA
end

::LoopB::
script_common_interface("SICKCamera","takePhoto")
script_common_interface("SICKCamera","getResult")
Located = script_common_interface("SICKCamera","partLocated")
if(Located == 1) then
.
.
.
Drop at position B
else 
goto LoopB
end

::LoopC::
script_common_interface("SICKCamera","takePhoto")
script_common_interface("SICKCamera","getResult")
Located = script_common_interface("SICKCamera","partLocated")
if(Located == 1) then
.
.
.
Drop at position C
else 
goto LoopC
end

::LoopD::
script_common_interface("SICKCamera","takePhoto")
script_common_interface("SICKCamera","getResult")
Located = script_common_interface("SICKCamera","partLocated")
if(Located == 1) then
.
.
.
Drop at position D
else 
goto LoopD
end

没有错误,程序按预期运行。但是,我想知道是否还有更好的方法。

1 个答案:

答案 0 :(得分:1)

goto唯一被普遍接受的用例是错误处理,例如跳转到清理代码。但是即使如此,通常也应该避免。

您可能可以执行以下操作:

-- loop B
repeat
  take photo, etc.
  located = ...
until(located == 1)

Drop at position B

此外,如果要重复三次相同的代码,则应将其提取到一个函数中,并可能将该位置作为参数。或至少将整个内容放入for循环中。