Как я могу сделать одну строку кода, чтобы запускаться один раз в цикле и запускать только следующую строку кода, пока предыдущая не запустилась один раз? - PullRequest
0 голосов
/ 18 июня 2019

Код скрипта предназначен для робота и состоит из блока кодов, которые являются путевыми точками для сброса товаров.Я хочу сохранить строки, выполнив одну строку кода только один раз, вернуться к началу цикла и запускать только следующую строку кода, пока предыдущая строка не будет выполнена один раз.

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 ]

1 голос
/ 18 июня 2019

Если вы хотите сократить дублирование кода (не глупый номер строки, вы можете заключить дублированный код в функцию.

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

Далее, вы 'd инкапсулируйте шаги

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

Вы можете также изменить свои другие функции, чтобы сделать их более краткими.

ps Эти ссылки могут быть не связаны напрямую, но я все же хочу упомянуть их здесь 1 , 2

1 голос
/ 18 июня 2019

Ну, вы установили флаг, который поможет вам вспомнить, запустили ли вы эту первую строку. Или вы используете счетчик цикла, если он у вас есть.

Во многих случаях достаточно просто запустить эту единовременную вещь, прежде чем войти в цикл. Но вот небольшой пример:

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...