Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- rightSensor=sim.getObjectHandle("bubbleRob_rightSensor") -- Handle of the right sensor
- leftSensor=sim.getObjectHandle("bubbleRob_leftSensor") -- Handle of the left 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
- debrisRight=sim.readProximitySensor(rightSensor)
- debrisLeft=sim.readProximitySensor(leftSensor)
- if (debrisRight>0) then
- sim.setJointTargetVelocity(rightMotor,0)
- sim.setJointTargetVelocity(leftMotor,speed/2)
- end
- if (debrisLeft>0) then
- sim.setJointTargetVelocity(leftMotor,0)
- sim.setJointTargetVelocity(rightMotor,speed/2)
- end
- -- If we detected something, we set the backward mode:
- if (result>0) then backUntilTime=sim.getSimulationTime()+5 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement