如何使一行代码在循环中运行一次,并且只运行下一行代码,直到前一行运行一次?

Posted

技术标签:

【中文标题】如何使一行代码在循环中运行一次,并且只运行下一行代码,直到前一行运行一次?【英文标题】:How can I make a single line to code to run once in a loop and only run the next line of code until the previous has ran once? 【发布时间】:2019-10-31 14:42:57 【问题描述】:

脚本代码用于机器人,由一组代码组成,这些代码是放置产品的路点。我想通过只运行一行代码来节省行,然后返回循环顶部,只运行下一行代码,直到上一行运行一次。

function pim60()
script_common_interface("SICKCamera","takePhoto")
script_common_interface("SICKCamera","getResult")
Located = script_common_interface("SICKCamera","partLocated")
end


function intermediatemoves()
    X = script_common_interface("SICKCamera","getXposition")
    Y = script_common_interface("SICKCamera","getYposition")
    R = script_common_interface("SICKCamera","getRotation")
    z_offset = 0.24

    local offset_pose = 
    table.insert(offset_pose,X)
    table.insert(offset_pose,Y)
    table.insert(offset_pose,z_offset)

    camera_rz = rpy2quaternion(d2r(-180.0), d2r(0.0), d2r(-90.0) + d2r(R))

    move_joint(get_target_pose(offset_pose,camera_rz,false,0.0, 0.0, 0.0,1.0, 0.0, 0.0, 0.0),true)

    z_grab = 0.17

    local grab_pose = 
    table.insert(grab_pose,X)
    table.insert(grab_pose,Y)
    table.insert(grab_pose,z_grab)

    move_line(get_target_pose(grab_pose,camera_rz,false,0.0, 0.0, 0.0,1.0, 0.0, 0.0, 0.0),true)

     set_step_breakpoint()
     --(Logic tree item : Gripper) Gripper
     script_common_interface("Gripper", "set_robotiq_param|9, 155, 255, 255, true")
     set_step_breakpoint()
     --(Logic tree item : Move) Movedrp
     --(Logic tree item : Waypoint) Waypoint01
     move_joint(1.047662, -0.552625, -1.753926, 0.374278, -1.571027, 0.588803, true)
     set_step_breakpoint()
     --(Logic tree item : Waypoint) Waypoint02
     move_joint(2.307135, 0.811214, -0.349017, 0.045296, -1.569157, -0.006474, true)
     set_step_breakpoint()
end


function returntohome()
      --WAYPOINT FOR COLLISION AVOIDANCE
      --(Logic tree item : Waypoint) Waypoint02
      move_joint(2.307135, 0.811214, -0.349017, 0.045296, -1.569157, -0.006474, true)
      set_step_breakpoint()
      --!!HOME POSITION JOINT 5 CAN AFFECT PICK COLLISION WITH PART
      --(Logic tree item : Waypoint) Waypointhome
      move_joint(1.321444, -0.626547, -2.252496, -0.137991, -1.518901, -0.006779, true)
      set_step_breakpoint()
end

--Open gripper and home
   script_common_interface("Gripper", "set_robotiq_param|9, 0, 255, 255, false")
   --!!HOME POSITION JOINT 5 CAN AFFECT PICK COLLISION WITH PART
   --(Logic tree item : Waypoint) Waypointhome
   move_joint(1.321444, -0.626547, -2.252496, -0.137991, -1.518901, -0.006779, true)
    set_step_breakpoint()

--LoopA
repeat
pim60()
until (Located == 1)
intermediatemoves()
move_joint(2.337869, 1.478278, 0.177188, -0.416970, -1.569186, -0.006448, true)
script_common_interface("Gripper", "set_robotiq_param|9, 0, 0, 83, true")
returntohome()

--LoopB
repeat
pim60()
until (Located == 1) 
intermediatemoves()
move_joint(2.145543, 1.478292, 0.177206, -0.416904, -1.569186, -0.006415, true)
script_common_interface("Gripper", "set_robotiq_param|9, 0, 0, 83, true")
returntohome()

--LoopC
repeat
pim60()
until (Located == 1) 
intermediatemoves()
move_joint(2.020320, 1.478307, 0.177206, -0.416897, -1.569190, -0.006412, true)
script_common_interface("Gripper", "set_robotiq_param|9, 0, 0, 83, true")
returntohome()

--LoopD
repeat
pim60()
until (Located == 1) 
intermediatemoves()
move_joint(1.845862, 1.478325, 0.177202, -0.416893, -1.569190, -0.006412, true)
script_common_interface("Gripper", "set_robotiq_param|9, 0, 0, 83, true")
returntohome()

这是现在使用的代码,与所需的功能相去甚远。我创建了 4 个重复“循环”,其中包含 4 个不同的结束位置来放置产品。

【问题讨论】:

【参考方案1】:

如果你想减少代码重复(不是行号,那很傻),你可以将重复的代码包装在一个函数中。

function locate()
    repeat
        script_common_interface("SICKCamera","takePhoto")
        script_common_interface("SICKCamera","getResult")
    until (script_common_interface("SICKCamera","partLocated") == 1)
end

进一步,您将封装这些步骤

function go_fetch(destination)
    locate()
    intermediatemoves()
    move_joint(destination, true)
    script_common_interface("Gripper", "set_robotiq_param|9, 0, 0, 83, true")
    returntohome()
end
--instead of LoopsABCD
local destinations = 
   2.020320, 1.478307, 0.177206, -0.416897, -1.569190, -0.006412,
   2.145543, 1.478292, 0.177206, -0.416904, -1.569186, -0.006415,
   --etc...
   
for _,d in ipairs(destinations) do
    go_fetch(d)
end

您也可以将其他函数重构为更简洁。

附言这些链接可能不会立即相关,但我还是想在这里提一下1,2

【讨论】:

【参考方案2】:

你设置了一个标志来帮助你记住你是否已经运行了第一行。或者,如果您有循环计数器,则使用循环计数器。

在许多情况下,只需在进入循环之前运行该一次性的东西就足够了。但这里有一个小例子:

for i = 0, 10 do
  if i == 0 then
     print("Enter bar")
  else
    print("Drink beer no " .. i)
  end
end

 local inBar
 while not drunk do
      if not inBar then
         print("Enter bar")
         inBar = true
      else
        print("Drink another beer")
      end
 end

【讨论】:

为什么不只是if i == 0 then @moteus 因为这适用于任何类型的循环。当然,您也可以在 for 循环中使用循环计数器。我的意思是,如果它是循环的第一行,为什么不简单地在进入循环之前执行它。

以上是关于如何使一行代码在循环中运行一次,并且只运行下一行代码,直到前一行运行一次?的主要内容,如果未能解决你的问题,请参考以下文章

如何设置循环遍历CSV文件中每个值的jmeter测试?

我如何使每个查询折叠php

Python对一行按照字节位置读取想要的字符,文件有多行,如何循环运行

如何像启动画面一样只运行一次活动

当我在批处理文件中运行下面的代码时,它只会执行第一行并且不会关闭命令窗口

sql查询只打印第一行