Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.53 KB | None | 0 0
  1. function speedChange_callback(ui,id,newVal)
  2.     speed=minMaxSpeed[1]+(minMaxSpeed[2]-minMaxSpeed[1])*newVal/100
  3. end
  4.  
  5. function sysCall_init()
  6.     -- This is executed exactly once, the first time this script is executed
  7.     bubbleRobBase=sim.getObjectAssociatedWithScript(sim.handle_self) -- this is bubbleRob's handle
  8.     leftMotor=sim.getObjectHandle("bubbleRob_leftMotor") -- Handle of the left motor
  9.     rightMotor=sim.getObjectHandle("bubbleRob_rightMotor") -- Handle of the right motor
  10.     noseSensor=sim.getObjectHandle("bubbleRob_sensingNose") -- Handle of the proximity sensor
  11.     rightSensor=sim.getObjectHandle("bubbleRob_rightSensor") -- Handle of the right sensor
  12.     leftSensor=sim.getObjectHandle("bubbleRob_leftSensor") -- Handle of the left sensor
  13.     minMaxSpeed={50*math.pi/180,300*math.pi/180} -- Min and max speeds for each motor
  14.     backUntilTime=-1 -- Tells whether bubbleRob is in forward or backward mode
  15.     -- Create the custom UI:
  16.         xml = '<ui title="'..sim.getObjectName(bubbleRobBase)..' speed" closeable="false" resizeable="false" activate="false">'..[[
  17.         <hslider minimum="0" maximum="100" onchange="speedChange_callback" id="1"/>
  18.         <label text="" style="* {margin-left: 300px;}"/>
  19.         </ui>
  20.         ]]
  21.     ui=simUI.create(xml)
  22.     speed=(minMaxSpeed[1]+minMaxSpeed[2])*0.5
  23.     simUI.setSliderValue(ui,1,100*(speed-minMaxSpeed[1])/(minMaxSpeed[2]-minMaxSpeed[1]))
  24. end
  25.  
  26. function sysCall_actuation()
  27.     result=sim.readProximitySensor(noseSensor) -- Read the proximity sensor
  28.  
  29.     debrisRight=sim.readProximitySensor(rightSensor)
  30.     debrisLeft=sim.readProximitySensor(leftSensor)
  31.  
  32.     if (debrisRight>0) then
  33.         sim.setJointTargetVelocity(rightMotor,0)
  34.         sim.setJointTargetVelocity(leftMotor,speed/2)
  35.     end
  36.     if (debrisLeft>0) then
  37.         sim.setJointTargetVelocity(leftMotor,0)
  38.         sim.setJointTargetVelocity(rightMotor,speed/2)
  39.     end
  40.     -- If we detected something, we set the backward mode:
  41.     if (result>0) then backUntilTime=sim.getSimulationTime()+5 end
  42.  
  43.     if (backUntilTime<sim.getSimulationTime()) then
  44.         -- When in forward mode, we simply move forward at the desired speed
  45.         sim.setJointTargetVelocity(leftMotor,speed)
  46.         sim.setJointTargetVelocity(rightMotor,speed)
  47.     else
  48.         -- When in backward mode, we simply backup in a curve at reduced speed
  49.         sim.setJointTargetVelocity(leftMotor,-speed/2)
  50.         sim.setJointTargetVelocity(rightMotor,-speed/8)
  51.     end
  52. end
  53.  
  54.  
  55. function sysCall_cleanup()
  56.     simUI.destroy(ui)
  57. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement