关键词:
【中文标题】如何使一行代码在循环中运行一次,并且只运行下一行代码,直到前一行运行一次?【英文标题】: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 循环中使用循环计数器。我的意思是,如果它是循环的第一行,为什么不简单地在进入循环之前执行它。当我在批处理文件中运行下面的代码时,它只会执行第一行并且不会关闭命令窗口
】当我在批处理文件中运行下面的代码时,它只会执行第一行并且不会关闭命令窗口【英文标题】:Whenirunthecodebelowinabatchfile,itwillonlyexecutethefirstlineanddoesnotclosethecommandwindow【发布时间】:2016-05-0205:33:58【问题描述】:原来在命令... 查看详情
如何像启动画面一样只运行一次活动
】如何像启动画面一样只运行一次活动【英文标题】:HowtorunanactivityonlyoncelikeSplashscreen【发布时间】:2013-09-0721:36:50【问题描述】:在我的应用程序中,我只想在第一次运行时运行一次启动画面,但问题是我已经将这一行放在Man... 查看详情
使某些代码只运行一次
】使某些代码只运行一次【英文标题】:Makingsomecodeonlyrunonce【发布时间】:2012-03-0518:10:48【问题描述】:我有一些代码只想在我的MainViewController中运行一次。它应该在用户每次启动应用程序时运行,但仅在MainViewController加载后... 查看详情
如何设置循环遍历csv文件中每个值的jmeter测试?(代码片段)
...件。我有一个自定义Java采样器,它一次使用一行数据。如何设置测试,CSV文件是否经过预处理,每次运行时每行都被送入java采样器?(即每次运行javasampler时,CSV文件中的下一行都应该用作输入)。我的尝试如下所述,但我没... 查看详情
为什么switch在javascript数组的for循环中只运行一次?
我正在尝试编写一个Javascript代码,删除给定数组中的所有错误值。代码适用于第一个负值是查找并且不适用于后续的错误值,就好像一旦交换机运行,整个for循环被破坏。我的代码如下。functionbouncer(arr){for(vari=0;i<arr.length;i++){... 查看详情
如何在 Oracle SQL 中优化或在没有循环的情况下执行此操作
】如何在OracleSQL中优化或在没有循环的情况下执行此操作【英文标题】:HowtooptimizethisordothiswithoutaloopinOracleSQL【发布时间】:2020-03-2902:42:34【问题描述】:我有下面的代码,这个代码应该告诉我们给定表有多少数据块没有一行。... 查看详情
如何在运行下一行代码 Angular2 之前等待 AngularFire2 订阅完成
】如何在运行下一行代码Angular2之前等待AngularFire2订阅完成【英文标题】:HowtowaitforaAngularFire2subscriptiontofinishbeforerunningthenextlinesofcodeAngular2【发布时间】:2017-12-2123:14:03【问题描述】:我的Angular2类型脚本代码有问题。我正在尝... 查看详情
如何让异步函数在后台运行?
】如何让异步函数在后台运行?【英文标题】:HowcanImakeanasyncfunctionruninthebackground?【发布时间】:2019-08-2900:15:33【问题描述】:我不知道如何使异步函数不阻塞下一行代码/防止循环从顶部重新开始。异步函数:asyncdefupdateEmbed(sel... 查看详情
sql查询只打印第一行
】sql查询只打印第一行【英文标题】:Sqlqueryonlyprintingfirstrow【发布时间】:2009-06-1518:44:55【问题描述】:我在php中编码,代码从数组中获取数据以从mysql数据库中获取附加数据。因为我需要来自两个不同表的数据,所以我使用嵌... 查看详情
python文件单行循环读取的坑(一个程序中,文件默认只能按行循环读取一次,即使写到另一个循环里,它也只读取一次)(代码片段)
...获取a文件中有,但是b文件中没有的行;想到的方法是:1.一行一行提取a文件中数据,然后用a文件中的每一行与b文件中的每一行比较, 2.如果找到相同行就继续查找a中的下一... 查看详情
想要使用 for 循环多次运行查询并将每个结果添加到字典中。此代码即使在循环时也只执行一次
...使用for循环多次运行查询并将每个结果添加到字典中。此代码即使在循环时也只执行一次【英文标题】:Wanttorunaquerymultipletimesusingaforloopandaddeachresultintoadictionary.Thiscodeonlyexecuteonceevenasitloopsthrough【发布时间】:2021-06-1014:37:45【问... 查看详情
如何在@SpringBootTest 之前添加设置并且只运行一次?
】如何在@SpringBootTest之前添加设置并且只运行一次?【英文标题】:Howaddsetupbefore@SpringBootTestandonlyrunonce?【发布时间】:2019-05-0913:35:58【问题描述】:我有一个dockerDB设置方法,目前位于@BeforeAll。目前,构造如下@RunWith(SpringRunner.... 查看详情
如何使代码在 FOR 循环中运行?而不是那么多的IF
】如何使代码在FOR循环中运行?而不是那么多的IF【英文标题】:HowdoImakethecoderuninaFORloop?InsteadofsomanyIF【发布时间】:2019-12-2507:24:27【问题描述】:如何在这段代码中使用FOR循环?而是使用大量IF问题。我已经尝试了很多方法来... 查看详情
对 Pandas 数据框中的每一行只运行一次函数
】对Pandas数据框中的每一行只运行一次函数【英文标题】:RunfunctionexactlyonceforeachrowinaPandasdataframe【发布时间】:2016-08-0504:50:39【问题描述】:如果我有一个功能defdo_irreversible_thing(a,b):printa,b还有一个数据框,比如说df=pd.DataFrame([... 查看详情
datagridview中checkbox如何使其勾上
winform,.,.vs2005:dgpermissions.Rows[j].Cells[i].Value=true;这是我目前使用的代码,单独运行这个窗体可以使用,但我测试完这个窗体,将它加入子窗体中运行,这行代码虽然照样运行,但是却失去效果.,DataGridView中所有的CheckBox全部没被... 查看详情
如何使此代码在单个文件夹中运行多个图像?
】如何使此代码在单个文件夹中运行多个图像?【英文标题】:Howdoimakethiscodetorunmultipleimagesinasinglefolder?【发布时间】:2019-09-0810:22:33【问题描述】:有人可以帮助修改下面的代码,以便它可以从给定文件夹中循环运行多个图像... 查看详情
如何逐步在批处理文件中运行for循环
】如何逐步在批处理文件中运行for循环【英文标题】:Howtorunforloopinbatchfilestepbystep【发布时间】:2021-10-2623:12:47【问题描述】:我试图从批处理文件中多次调用MATLAB。这样,我多次使用for循环来执行我的MATLAB脚本。但是运行批处... 查看详情
如何使for-each循环向后运行(代码片段)
...元格值与列表中的值匹配则保留,否则将删除。我想知道如何让它向后运行,因为向前运行会产生问题。我已经对此进行了一些研究,并且我尝试将“Step-1”附加到开始for循环的行的末尾,但是在这种情况下这不起作用。SetRng=Ra... 查看详情