CoppeliaSim 传送带系统脚本 - 随机生成物品并控制运动
function sysCall_init()
sensor=sim.getObjectHandle('conveyorBelt_sensor')
pathHandle=sim.getObjectHandle('Path')
forwarder=sim.getObjectHandle('conveyorForwarder')
sim.setPathTargetNominalVelocity(pathHandle,0) -- for backward compatibility
tick=0
counter=1
cubicSize={0.05,0.05,0.05}
cubicSize_s={0.05,0.04,0.05}
cubicWeight=0.2
shapeType_Cubic=0
red={1.00,0.29,0.21}
green={0.28,1,0.21}
math.randomseed(tostring(os.time()):reverse():sub(1, 7))
greenBigCounter=18
greenSmallCounter=9
redBigCounter=18
redSmallCounter=9
smallCounter=18
bigCounter=36
totalCounter=bigCounter+smallCounter
poseMatrix=sim.getObjectMatrix(forwarder,-1)
poseMatrix[4]=poseMatrix[4]+0.4
poseMatrix[12]=poseMatrix[12]+0.15
end
function sysCall_cleanup()
end
function sysCall_actuation()
beltVelocity=sim.getScriptSimulationParameter(sim.handle_self,"conveyorBeltVelocity")
ss=sim.readProximitySensor(sensor)
--sim.addStatusbarMessage(string.format("ss=%f",ss))
if sim.readProximitySensor(sensor)>0 or sim.getIntegerSignal('getdata')==1 then
beltVelocity=0
end
local dt=sim.getSimulationTimeStep()
local pos=sim.getPathPosition(pathHandle)
pos=pos+beltVelocity*dt
sim.setPathPosition(pathHandle,pos) -- update the path's intrinsic position
-- Here we "fake" the transportation pads with a single static rectangle that we dynamically reset
-- at each simulation pass (while not forgetting to set its initial velocity vector) :
local relativeLinearVelocity={-beltVelocity,0,0}
-- Reset the dynamic rectangle from the simulation (it will be removed and added again)
sim.resetDynamicObject(forwarder)
-- Compute the absolute velocity vector:
local m=sim.getObjectMatrix(forwarder,-1)
m[4]=0 -- Make sure the translation component is discarded
m[8]=0 -- Make sure the translation component is discarded
m[12]=0 -- Make sure the translation component is discarded
local absoluteLinearVelocity=sim.multiplyVector(m,relativeLinearVelocity)
-- Now set the initial velocity of the dynamic rectangle:
sim.setObjectFloatParameter(forwarder,sim.shapefloatparam_init_velocity_x,absoluteLinearVelocity[1])
sim.setObjectFloatParameter(forwarder,sim.shapefloatparam_init_velocity_y,absoluteLinearVelocity[2])
sim.setObjectFloatParameter(forwarder,sim.shapefloatparam_init_velocity_z,absoluteLinearVelocity[3])
tick=tick+1
cubicHandles={}
if (tick%4==0)and(totalCounter>0)and(beltVelocity>0) then
if(math.random(bigCounter+smallCounter)<=smallCounter) then
cubicHandles[counter]=sim.createPureShape(shapeType_Cubic,14,cubicSize_s,cubicWeight,nil)
smallCounter=smallCounter-1
if(math.random(redSmallCounter+greenSmallCounter)<=redSmallCounter) then
sim.setShapeColor(cubicHandles[counter],nil,sim.colorcomponent_ambient_diffuse,red)
redSmallCounter=redSmallCounter-1
else
sim.setShapeColor(cubicHandles[counter],nil,sim.colorcomponent_ambient_diffuse,green)
greenSmallCounter=greenSmallCounter-1
end
else
cubicHandles[counter]=sim.createPureShape(shapeType_Cubic,14,cubicSize,cubicWeight,nil)
bigCounter=bigCounter-1
if(math.random(redBigCounter+greenBigCounter)<=redBigCounter) then
sim.setShapeColor(cubicHandles[counter],nil,sim.colorcomponent_ambient_diffuse,red)
redBigCounter=redBigCounter-1
else
sim.setShapeColor(cubicHandles[counter],nil,sim.colorcomponent_ambient_diffuse,green)
greenBigCounter=greenBigCounter-1
end
end
--sim.setObjectSpecialProperty(cubicHandles[counter],sim.objectspecialproperty_renderable )
sim.setObjectSpecialProperty(cubicHandles[counter],(sim.objectspecialproperty_detectable_all+sim.objectspecialproperty_renderable))
totalCounter=totalCounter-1
--sim.addStatusbarMessage(string.format("x=%f,y=%f,z=%f",poseMatrix[4],poseMatrix[8],poseMatrix[12]))
local CubicMatrix=poseMatrix
CubicMatrix[8]=poseMatrix[8]+0.05*math.random()-0.03
--CubicMatrix[8]=poseMatrix[8]+0.02*math.random()-0.01
if(CubicMatrix[8]>-2.0000e-01) then
CubicMatrix[8] = -2.0000e-01
end
if(CubicMatrix[8]<-3.0000e-01) then
CubicMatrix[8] = -3.0000e-01
end
sim.setObjectMatrix(cubicHandles[counter],-1,poseMatrix)
counter=counter+1
end
end
功能说明:
该脚本主要实现了一个传送带系统,包括以下功能:
- 获取传送带的速度参数,并根据该参数控制传送带的运动。
- 在传送带上随机生成不同大小、不同颜色的立方体物体,模拟物品在传送带上的运动。
- 使用传感器检测是否有物品到达传送带的末端,如果有则停止传送带的运动。
- 在物品到达末端时,将其从场景中移除。
- 控制传送带上物品的生成速率和比例,以及不同颜色物品的数量比例。
- 控制物品生成的位置和随机偏移量,以模拟物品在传送带上的运动轨迹。
- 使用动态物体模拟传送带上的运动,以及动态重置物体的位置和速度。
总的来说,该脚本实现了比较完整的传送带系统,并且可以通过参数控制不同的生成速率和比例,具有一定的灵活性和可定制性。
原文地址: https://www.cveoy.top/t/topic/f2n7 著作权归作者所有。请勿转载和采集!