我怎样才能使一行代码循环运行一次,并且只运行下一行代码,直到上一行运行一次?

时间:2019-06-18 02:08:13

标签: lua

脚本代码是针对机器人的,由代码块组成,这些代码块是放置产品的航点。我想通过只运行一行代码来保存行,然后返回到循环的顶部,只运行下一行代码,直到上一行运行一次。

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个用于放置产品的不同最终位置。

2 个答案:

答案 0 :(得分:1)

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

在许多情况下,只要进入循环,就只需运行一次即可。但这是一个小例子:

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

答案 1 :(得分: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

您也可以重构其他功能以使其更加简洁。

p.s。这些链接可能不会立即相关,但我仍然要在这里12

提及它们
相关问题