thread script 自带程序说明

本文介绍V-REP仿真环境中线程同步机制及模拟状态获取的方法。通过使用sim.switchThread()和sim.getSimulationState()等API,可以实现线程与模拟周期的精确同步,并获取当前模拟的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

function sysCall_threadmain()
    -- Put some initialization code here


    -- Put your main loop here, e.g.:
    --
    -- while sim.getSimulationState()~=sim.simulation_advancing_abouttostop do
    --     local p=sim.getObjectPosition(objHandle,-1)
    --     p[1]=p[1]+0.001
    --     sim.setObjectPosition(objHandle,-1,p)
    --     sim.switchThread() -- resume in next simulation step
    -- end
end

function sysCall_cleanup()
    -- Put some clean-up code here
end


-- ADDITIONAL DETAILS:
-- -------------------------------------------------------------------------
-- If you wish to synchronize a threaded loop with each simulation pass,
-- enable the explicit thread switching with 
--
-- sim.setThreadAutomaticSwitch(false)
--
-- then use
--
-- sim.switchThread()
--
-- When you want to resume execution in next simulation step (i.e. at t=t+dt)
--
-- sim.switchThread() can also be used normally, in order to not waste too much
-- computation time in a given simulation step
-- -------------------------------------------------------------------------

1.sim.getSimulationState()

1.1函数说明

DescriptionRetrieves current simulation state. See also the simulation state diagram.
C synopsissimInt simGetSimulationState()
C parametersNone
C return valueThe current state of the simulation (sim_simulation_stopped, sim_simulation_paused, etc. (see the simulation state values)), or -1 in case of an error
Lua synopsisnumber simulationState=sim.getSimulationState()
Lua parameters
Same as C-function
Lua return valuesSame as C-function
1.2 simulation  states


sim.simulation_stopped
sim.simulation_paused
sim.simulation_advancing_firstafterstop
sim.simulation_advancing_running
sim.simulation_advancing_lastbeforepause
sim.simulation_advancing_firstafterpause
sim.simulation_advancing_abouttostop
sim.simulation_advancing_lastbeforestop

1.3 simulation state digram

2.1 sim.switchThread()

DescriptionAllows specifying the exact moment at which the current thread should switch to another thread. If the current script doesn't run in a thread (i.e. if it runs in the application main thread), this function has no effect. By default, V-REP doesn't use "regular" threads, but something similar to hybrid threads (which behave like coroutines, but can also behave like regular threads). This allows much more flexibility and execution control of the threads: each thread (except for the main or application thread) has a switch timing associated, which specifies how long the thread will run before switching to other threads. By default this value is 2 millisecond, but can be modified with sim.setThreadSwitchTiming. That timing can be shortened with sim.switchThread. Use with care when calling this function from a plugin. See alsothe sim.setThreadAutomaticSwitchsim.setThreadResumeLocation and sim.setThreadIsFree functions.

2.2  sim.setThreadAutomaticSwitch()

DescriptionAllows to temporarily forbid thread switches. If the current script doesn't run in a thread (i.e. if it runs in the application main thread), this function has no effect. By default, V-REP doesn't use "regular" threads, but something similar to hybrid threads (which behave like coroutines, but can also behave like regular threads). This allows much more flexibility and execution control of the threads. For complete control over the switching moment, see also sim.getThreadAutomaticSwitchsim.setThreadSwitchTimingsim.switchThreadsim.setThreadIsFree and sim.setThreadResumeLocation.

参考来源:

               此文中所有代码,文档摘自Vrep官方手册。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值