首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我如何使一行代码在一个循环中运行一次,直到前面的代码运行一次,然后只运行下一行代码?

我如何使一行代码在一个循环中运行一次,直到前面的代码运行一次,然后只运行下一行代码?
EN

Stack Overflow用户
提问于 2019-06-18 02:08:13
回答 2查看 8.8K关注 0票数 0

脚本代码是机器人的代码,由一个代码块组成,这些代码是丢弃产品的通道。我想通过只运行一行代码来保存行,然后返回到循环的顶部,然后只运行下一行代码,直到上一行运行了一次。

代码语言:javascript
复制
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个不同的终端位置,以丢弃产品。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-06-18 11:53:45

如果您想要删除代码复制(而不是行号,这很愚蠢),您可以将重复的代码包装在一个函数中。

代码语言:javascript
复制
function locate()
    repeat
        script_common_interface("SICKCamera","takePhoto")
        script_common_interface("SICKCamera","getResult")
    until (script_common_interface("SICKCamera","partLocated") == 1)
end

此外,您还封装了以下步骤

代码语言:javascript
复制
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

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

附注:这些链接可能不是直接相关的,但我仍然想在这里提及它们-- 12

票数 1
EN

Stack Overflow用户

发布于 2019-06-18 05:42:12

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

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

代码语言:javascript
复制
for i = 0, 10 do
  if i == 0 then
     print("Enter bar")
  else
    print("Drink beer no " .. i)
  end
end

代码语言:javascript
复制
 local inBar
 while not drunk do
      if not inBar then
         print("Enter bar")
         inBar = true
      else
        print("Drink another beer")
      end
 end
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56640896

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档