添加接近傳感器 proximity sensor
Menu bar --> Add --> Proximity sensor --> Cone type
在 orientation dialog 對話框中
沿y軸和z軸旋轉90度 點擊Rotation Selection
在 position dialog 對話框中
x方向設置0.1 z方向設置0.12
修改距離傳感器的參數Show volume parameter
Offset 0.005, Angle 30 Range 0.15
Show detection parameter中將Don’t allow detection ifdistance smaller than取消
最後將傳感器連接到機器人的身體
添加視覺傳感器 vision sensor
Menu bar-->Add-->Vision sensor-->Perspective type
將視覺傳感器和距離傳感器放置一起且方向一致
1. 在視覺傳感器的屬性欄中配置Far clipping plane 爲1
Resolution x 和y 設爲256和256
2. 點擊show filter dialog
3. 選擇Edget detection on work image
然後點擊Add filter
4. 閾值設置爲0.2 點選OK
Floating view上右鍵 View-->Associate simulation with selected vision sensor
就可以看到視覺感測器的畫面
選擇對象BubbleRob--->右鍵選擇Add-->Associate child script-->Non threaded
腳本文件語言爲lua語言
function speedChange_callback(ui,id,newVal) speed=minMaxSpeed[1]+(minMaxSpeed[2]-minMaxSpeed[1])*newVal/100 end function sysCall_init() -- This is executed exactly once, the first time this script is executed bubbleRobBase=sim.getObjectAssociatedWithScript(sim.handle_self) -- this is bubbleRob's handle leftMotor=sim.getObjectHandle("bubbleRob_leftMotor") -- Handle of the left motor rightMotor=sim.getObjectHandle("bubbleRob_rightMotor") -- Handle of the right motor noseSensor=sim.getObjectHandle("bubbleRob_sensingNose") -- Handle of the proximity sensor minMaxSpeed={50*math.pi/180,300*math.pi/180} -- Min and max speeds for each motor backUntilTime=-1 -- Tells whether bubbleRob is in forward or backward mode -- Create the custom UI: xml = '<ui title="'..sim.getObjectName(bubbleRobBase)..' speed" closeable="false" resizeable="false" activate="false">'..[[ <hslider minimum="0" maximum="100" onchange="speedChange_callback" id="1"/> <label text="" style="* {margin-left: 300px;}"/> </ui> ]] ui=simUI.create(xml) speed=(minMaxSpeed[1]+minMaxSpeed[2])*0.5 simUI.setSliderValue(ui,1,100*(speed-minMaxSpeed[1])/(minMaxSpeed[2]-minMaxSpeed[1])) end function sysCall_actuation() result=sim.readProximitySensor(noseSensor) -- Read the proximity sensor -- If we detected something, we set the backward mode: if (result>0) then backUntilTime=sim.getSimulationTime()+4 end if (backUntilTime<sim.getSimulationTime()) then -- When in forward mode, we simply move forward at the desired speed sim.setJointTargetVelocity(leftMotor,speed) sim.setJointTargetVelocity(rightMotor,speed) else -- When in backward mode, we simply backup in a curve at reduced speed sim.setJointTargetVelocity(leftMotor,-speed/2) sim.setJointTargetVelocity(rightMotor,-speed/8) end end function sysCall_cleanup() simUI.destroy(ui) end
成品圖