Week6

上課內容:

bubbleRob V-rep 模擬,延續上周部分。

1.Click Add new collection.

2.Select bubbleRob and then click Add in the collection dialog.

3.Click Add new distance object in distance dialog.

4.Select a distance pair: [collection] collection - all other measurable objects in the scene.

5.Add a graph object Click [Menu bar --> Add --> Graph].

6.Rename it to bubbleRob_graph.

7.Attach the graph to bubbleRob, and set the absolute coordinates to (0,0,0.005).

8.Uncheck Display XYZ-planes.

9.Click Add new data stream to record.

10.Select Object: absolute x-position for the Data stream type, and graph for the Object / item to record. record the y and z positions.

11.Select Distance: segment length for the Data stream type, and bubbleRob_distance for the Object / item to record.

12.Set x y z postion uncheck Visible.

13.Click Edit 3D curves, then click Add new curve. bubbleRob_x_pos for the X-value item

bubbleRob_y_pos for the Y-value item

bubbleRob_z_pos for the Z-value item

14.check the Relative to world item and set Curve width to 4.

15.Add a pure primitive cylinder:(0.1, 0.1, 0.2).

16.Disable Body is dynamic.

17.Enable Collidable, Measurable, Renderable and Detectable.

18.Copy and paste the cylinder a few times, and move them to positions around BubbleRob.

19.Select the model base (BubbleRob) then check items Object is model base and Object/model can transfer or accept DNA.

20.Select the two joints, the proximity sensor and the graph, then enable item Igonred by model bounding box and click Apply to selection.

21.Select the vision sensor, the two wheels, the slider, and the graph, then enable item Select base of model instead.

22.Add a vision sensor,click [Menu bar --> Add --> Vision sensor --> Perspective type].

23.At the same position and orientation as BubbleRob's proximity sensor,then attach the vision sensor to the proximity sensor.

24.Set the local position and orientation of the vision sensor to (0,0,0).

25.Set the Far clipping plane item to 1, and the Resolution x and Resolution y items to 256 and 256.

26.Clicking Show filter dialog. We select the filter component Edge detection on work image and click Add filter. We position the newly added filter in second position (one position up, using the up button).

27.Double-click the newly added filter component and adjust its Threshold item to 0.2.

28.Add a floating view.

29.Right-click [Popup menu --> View --> Associate view with selected vision sensor].

30.Select bubbleRob and click [Menu bar --> Add --> Associated child script --> Non threaded].

31.Copy and paste following code into the script editor.

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