Vrep的安裝與小車建模

準備工作

任務1:智能小車平臺建模

  • 參考文檔中 Tutorial - BubbleRob tutorial 部分,學習:
    • 車身與輪子物理模型的設計
    • 動力學模型等參數的設置
    • 傳感器的添加與使用
    • 驅動關節的設計與控制
    • 控制腳本的編寫
  • 實現要求:
    • 搭建一個四輪小車,小車搭建一個單目彩色攝像頭
    • 尺寸(如長寬高)及底盤參數(如速度限制)可參考 DJI RoboMaster S1 進行設計
    • 不採用麥克納姆輪
    • 編寫簡單腳本使得小車能呈S型路線行走,當碰到障礙物後能夠繞行
  • 提交內容:
    • report/實驗報告.pdf
    • src/項目文件

實現

搭建四輪小車

1. 添加小車 carRob

在場景中添加一個 320 × 240 × 270 mm(長 × 寬 × 高)的長方體並命名爲 carRob。在 Object Properties 中設置使能立方體的 Collidable、Measurable、Renderable 和 Detectable 屬性,便於之後傳感器計算物體之間的距離。將小車的位置沿 z 軸向上移動 2 cm。
在這裏插入圖片描述

2. 添加接近傳感器 proximity sensor

在場景中添加一個錐形接近傳感器,修改接近傳感器的位置和方向,在 Object Properties 中修改傳感器的參數。將其命名爲 carRob_sensingNose,並添加到 carRob 目錄下。
在這裏插入圖片描述

3. 添加車輪 wheel

在場景中添加一個半徑爲 0.1 m,高爲 0.02 m 的圓柱體,像 carRob 一樣調整圓柱體的 Collidable、Measurable、Renderable 和 Detectable 屬性。修改車輪的位置和方向,絕對位置爲(0.09,0.13,0.05),絕對歐拉角爲(-90,0,0),將其命名爲 carRob_front_leftWheel。複製粘貼前左輪,創建其餘三個車輪,並修改相應的位置和名稱。
在這裏插入圖片描述

4. 添加關節 revolute

在場景中添加一個關節,將前左輪的位置和角度信息添加到關節上,修改關節屬性,並重命名爲 carRob_front_leftMotor。以相同的方式創建其餘的三個關節,並修改相應的位置、屬性和名稱。將四個車輪分別添加到對應的四個電機目錄下,並添加到 carRob 目錄下。
在這裏插入圖片描述

5. 使 carRob 具有穩定性

運行仿真,小車有微小的振動。將車輪的質量和慣性矩均放大八倍。再次進行仿真,可以看到增加了質量和慣性之後小車的穩定性增強。
在這裏插入圖片描述

6. 創建 carRob collection

將 carRob 設置爲模型 carRob 中所有對象的基礎。定義一個表示 carRob 的對象集合。創建 calculation module,爲 carRob 添加一個最小距離函數。
在這裏插入圖片描述

7. 創建圖表 graph

在場景中添加一個 graph 並重命名爲 carRob_graph,將其添加到 carRob 目錄下,設置絕對座標爲(0,0,0.005)。將 carRob 的 X、Y 和 Z 的數據流添加進圖表中,再添加一個數據流,跟蹤機器人與其環境之間的最小距離,對四個數據流進行重命名,設置相應參數。建立一個三錐曲線來顯示 carRob 的軌跡並設置相應參數。
在這裏插入圖片描述

8. 添加障礙物 obstacles

在場景中創建大小爲(0.1,0.1,0.2)的圓柱體,設置障礙物的屬性爲靜態,拖動圓柱體到合適的位置,再複製粘貼添加多個障礙物。
在這裏插入圖片描述

9. 完成模型構建 carRob model

選中 carRob,在 object properties 裏面點擊 Object is model base 和 Object/model can transfer or accept DNA。選中 motors、graph 和 proximity sensor,在 object properties 裏面點擊 ignored by model bounding box,後點擊 Apply to selection。選中 motors,設置顯示圖層,隱藏 motors。選中 wheels、graph 和 proximity sensor,在 object properties 的 common 界面勾選 Select base of model instead,這樣當選中其中的任何一個 object 的時候,選中的都是 carRob,保護模型。
在這裏插入圖片描述

10. 添加單目彩色攝像頭 camera

在場景中添加一個 camera,設置相應屬性。在場景中添加一個浮動視圖並與 proximity sensor 聯繫起來。
在這裏插入圖片描述

編寫腳本

基本思路:在前二分之一的時間中使小車左輪的速度爲右輪的速度的四倍,實現向右轉;在後二分之一時間中,使小車右輪的速度爲左輪的速度的四倍,實現向左轉。小車交替着向左轉和向右轉,實現 S 型前進。在小車上搭載接近傳感器,使得小車碰到障礙物時能繞行。

完整代碼如下:

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
    carRobBase=sim.getObjectAssociatedWithScript(sim.handle_self) -- this is carRob's handle
    fleftMotor=sim.getObjectHandle("carRob_front_leftMotor") -- Handle of the front left motor
    frightMotor=sim.getObjectHandle("carRob_front_rightMotor") -- Handle of the front right motor
    bleftMotor=sim.getObjectHandle("carRob_back_leftMotor") -- Handle of the back left motor
    brightMotor=sim.getObjectHandle("carRob_back_rightMotor") -- Handle of the back right motor
    noseSensor=sim.getObjectHandle("carRob_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(carRobBase)..' 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
    speed=1
    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
        if(sim.getSimulationTime()%12<6) then
            sim.setJointTargetVelocity(fleftMotor,speed*4)
            sim.setJointTargetVelocity(bleftMotor,speed*4)
            sim.setJointTargetVelocity(frightMotor,speed)
            sim.setJointTargetVelocity(brightMotor,speed)
        else
            sim.setJointTargetVelocity(fleftMotor,speed)
            sim.setJointTargetVelocity(bleftMotor,speed)
            sim.setJointTargetVelocity(frightMotor,speed*4)
            sim.setJointTargetVelocity(brightMotor,speed*4)
        end
    else
        -- When in backward mode, we simply backup in a curve at reduced speed
        sim.setJointTargetVelocity(fleftMotor,-speed/2)
        sim.setJointTargetVelocity(bleftMotor,-speed/2)
        sim.setJointTargetVelocity(frightMotor,-speed/8)
        sim.setJointTargetVelocity(brightMotor,-speed/8)
    end
end
 
function sysCall_cleanup()
	simUI.destroy(ui)
end

添加腳本到 carRob 中,最後完成如圖:
在這裏插入圖片描述
S 型路線行走:
在這裏插入圖片描述
當碰到障礙物後能夠繞⾏:
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

仿真視頻

傳送門

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章