CoppeliaSim传送带脚本分析:生成随机立方体
CoppeliaSim传送带脚本分析:生成随机立方体
本篇分析一个CoppeliaSim脚本,该脚本模拟传送带运输立方体的过程。传送带上生成的立方体具有随机的大小和颜色。
脚本代码分析
该脚本包含三个主要函数:
function sysCall_init(): 初始化函数,在脚本开始运行时调用一次。
lua 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
- 获取场景中关键对象的句柄:传送带传感器 (
conveyorBelt_sensor)、路径 (Path) 和传送带前进装置 (conveyorForwarder)。 - 设置路径的目标速度为 0。 - 初始化计数器变量 (tick,counter),用于控制立方体的生成频率和数量。 - 定义立方体的尺寸 (cubicSize,cubicSize_s)、重量 (cubicWeight) 以及颜色 (red,green)。 - 设置随机数种子,确保每次运行脚本时生成不同的随机立方体序列。 - 初始化不同大小和颜色的立方体计数器 (greenBigCounter,greenSmallCounter,redBigCounter,redSmallCounter,smallCounter,bigCounter,totalCounter)。 - 获取传送带前进装置的初始位置矩阵 (poseMatrix),并进行调整。
function sysCall_cleanup(): 清理函数,在脚本结束运行时调用一次。
lua function sysCall_cleanup() end
- 该函数当前为空,没有执行任何操作。
function sysCall_actuation(): 动作函数,在每个仿真步长中调用一次。
lua 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
- 获取传送带的速度 (
beltVelocity)。 - 读取传送带传感器的值,如果传感器检测到物体或接收到特定信号,则停止传送带。 - 更新传送带的位置,模拟传送带的运动。 - 模拟传送带前进装置的运动,使其与传送带同步。 - 每隔一定时间步长 (tick % 4 == 0),随机生成一个立方体,并设置其大小、颜色和位置。 - 控制立方体的生成数量,直到达到预设的数量 (totalCounter)。
总结
这个 CoppeliaSim 脚本展示了如何使用 Lua 脚本控制场景中的对象,并实现简单的逻辑和随机行为。通过修改脚本参数,可以调整传送带的速度、立方体的生成频率、大小和颜色等,以满足不同的仿真需求。
原文地址: https://www.cveoy.top/t/topic/f2n9 著作权归作者所有。请勿转载和采集!